cdvae: InstantiationException: a leaf Variable that requires grad is being used in an in-place operation

Followed the faster conda installation instructions from the README on WSL2 (files and environment localized to the WSL2 storage) and got the same error as in https://github.com/txie-93/cdvae/issues/2#issuecomment-1153064071.

Exception has occurred: InstantiationException       (note: full exception trace is shown but execution is paused at: _run_module_as_main)
Error in call to target 'cdvae.pl_modules.model.CDVAE':
InstantiationException("Error in call to target 'cdvae.pl_modules.gnn.DimeNetPlusPlusWrap':\nRuntimeError('a leaf Variable that requires grad is being used in an in-place operation.')\nfull_key: encoder")
full_key: model
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 92, in _call_target
    return _target_(*args, **kwargs)
  File "/home/sgbaird/cdvae/cdvae/pl_modules/gnn.py", line 327, in __init__
    super(DimeNetPlusPlusWrap, self).__init__(
  File "/home/sgbaird/cdvae/cdvae/pl_modules/gnn.py", line 223, in __init__
    self.rbf = BesselBasisLayer(num_radial, cutoff, envelope_exponent)
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/torch_geometric/nn/models/dimenet.py", line 59, in __init__
    self.reset_parameters()
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/torch_geometric/nn/models/dimenet.py", line 62, in reset_parameters
    torch.arange(1, self.freq.numel() + 1, out=self.freq).mul_(PI)

The above exception was the direct cause of the following exception:

  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 92, in _call_target
    return _target_(*args, **kwargs)
  File "/home/sgbaird/cdvae/cdvae/pl_modules/model.py", line 140, in __init__
    self.encoder = hydra.utils.instantiate(
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 222, in instantiate
    return instantiate_node(
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 339, in instantiate_node
    return _call_target(_target_, partial, args, kwargs, full_key)
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 97, in _call_target
    raise InstantiationException(msg) from e

The above exception was the direct cause of the following exception:

  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 97, in _call_target
    raise InstantiationException(msg) from e
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 339, in instantiate_node
    return _call_target(_target_, partial, args, kwargs, full_key)
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/instantiate/_instantiate2.py", line 222, in instantiate
    return instantiate_node(
  File "/home/sgbaird/cdvae/cdvae/run.py", line 94, in run
    model: pl.LightningModule = hydra.utils.instantiate(
  File "/home/sgbaird/cdvae/cdvae/run.py", line 166, in main
    run(cfg)
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/core/utils.py", line 186, in run_job
    ret.return_value = task_function(task_cfg)
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/core/utils.py", line 260, in return_value
    raise self._return_value
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/hydra.py", line 132, in run
    _ = ret.return_value
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/utils.py", line 453, in <lambda>
    lambda: hydra.run(
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/utils.py", line 216, in run_and_report
    raise ex
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/utils.py", line 216, in run_and_report
    raise ex
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/utils.py", line 452, in _run_app
    run_and_report(
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/_internal/utils.py", line 389, in _run_hydra
    _run_app(
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/site-packages/hydra/main.py", line 90, in decorated_main
    _run_hydra(
  File "/home/sgbaird/cdvae/cdvae/run.py", line 170, in <module>
    main()
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/sgbaird/miniconda3/envs/cdvae/lib/python3.9/runpy.py", line 197, in _run_module_as_main (Current frame)
    return _run_code(code, main_globals, None,

About this issue

Most upvoted comments

One thing that might also help is installing using the env.ci-cu11.yml from https://github.com/knc6/cdvae/tree/pip. I was able to get this code running after installing using packages one by one following the env.ci-cu11.yml. Additionally, I reccommend installing torch-cluster torch-scatter torch-sparse torch-spline-conv all from source. Here is my pip list.


Package                   Version     Editable project location
------------------------- ----------- -------------------------------------
absl-py                   1.4.0
aiohttp                   3.8.5
aiosignal                 1.3.1
antlr4-python3-runtime    4.8
appdirs                   1.4.4
ase                       3.22.1
async-timeout             4.0.2
attrs                     23.1.0
cachetools                5.3.1
cdvae                     0.0.1       /home/*/Desktop/Projects/cdvae
certifi                   2023.7.22
cffi                      1.15.1
charset-normalizer        2.0.12
click                     8.1.6
configparser              6.0.0
contourpy                 1.1.0
cycler                    0.11.0
dill                      0.3.7
dnspython                 2.4.1
docker-pycreds            0.4.0
filelock                  3.9.0
fonttools                 4.41.1
frozenlist                1.4.0
fsspec                    2023.6.0
future                    0.18.3
gitdb                     4.0.10
GitPython                 3.1.32
gmpy2                     2.1.2
google-auth               2.22.0
google-auth-oauthlib      1.0.0
googledrivedownloader     0.4
grpcio                    1.56.2
h5py                      3.9.0
hydra-core                1.1.0
hydra-joblib-launcher     1.1.5
idna                      3.4
imageio                   2.31.1
importlib-metadata        6.8.0
importlib-resources       6.0.0
isodate                   0.6.1
Jinja2                    3.1.2
joblib                    1.1.0
jsonschema                4.18.4
jsonschema-specifications 2023.7.1
kiwisolver                1.4.4
latexcodec                2.0.1
lazy_loader               0.3
lightning-utilities       0.9.0
llvmlite                  0.40.1
Markdown                  3.4.4
MarkupSafe                2.1.1
matminer                  0.7.3
matplotlib                3.7.2
mkl-fft                   1.3.6
mkl-random                1.2.2
mkl-service               2.4.0
monty                     2023.5.8
mpmath                    1.2.1
multidict                 6.0.4
multiprocess              0.70.15
networkx                  2.8.4
numba                     0.57.1
numpy                     1.21.5
oauthlib                  3.2.2
omegaconf                 2.1.2
p-tqdm                    1.3.3
packaging                 21.3
palettable                3.3.3
pandas                    2.0.3
pathos                    0.3.1
pathtools                 0.1.2
Pillow                    10.0.0
Pint                      0.21.1
pip                       23.2.1
pkgutil_resolve_name      1.3.10
plotly                    5.15.0
plyfile                   1.0.1
pox                       0.3.3
ppft                      1.7.6.7
promise                   2.3
protobuf                  3.20.1
psutil                    5.9.0
pyasn1                    0.5.0
pyasn1-modules            0.3.0
pybtex                    0.24.0
pycparser                 2.21
pyDeprecate               0.3.0
pymatgen                  2022.4.26
pymongo                   4.4.1
pyparsing                 3.0.9
python-dateutil           2.8.2
python-dotenv             0.20.0
python-louvain            0.16
pytorch-lightning         1.3.8
pytz                      2023.3
PyWavelets                1.4.1
PyYAML                    5.4.1
rdflib                    6.3.2
referencing               0.30.0
requests                  2.31.0
requests-oauthlib         1.3.1
rpds-py                   0.9.2
rsa                       4.9
ruamel.yaml               0.17.32
ruamel.yaml.clib          0.2.7
scikit-learn              1.1.0
scipy                     1.7.3
sentry-sdk                1.28.1
setproctitle              1.3.2
setuptools                59.5.0
shortuuid                 1.0.11
six                       1.16.0
SMACT                     2.2.1
smmap                     5.0.0
spglib                    2.0.2
subprocess32              3.5.4
sympy                     1.11.1
tabulate                  0.9.0
tenacity                  8.2.2
tensorboard               2.13.0
tensorboard-data-server   0.7.1
threadpoolctl             3.1.0
tifffile                  2023.7.10
torch                     1.9.0+cu111
torch-cluster             1.6.0
torch-geometric           1.7.2
torch-scatter             2.0.9
torch-sparse              0.6.12
torch-spline-conv         1.2.1
torchmetrics              0.7.0
tqdm                      4.65.0
typing_extensions         4.7.1
tzdata                    2023.3
uncertainties             3.1.7
urllib3                   1.26.11
wandb                     0.10.33
watchdog                  3.0.0
Werkzeug                  2.3.6
wheel                     0.38.4
yarl                      1.9.2
zipp                      3.16.2

I am running on an NVIDIA A4500 with Ubuntu 22.04 and Cuda 11.7

@holywater2 wow! Nice work. I might loop back to this and try to prepare a Colab notebook.

@sgbaird Hi, I solved the problem. The error message

InstantiationException("Error in call to target 'cdvae.pl_modules.gnn.DimeNetPlusPlusWrap':\nRuntimeError('a leaf Variable that requires grad is being used in an in-place operation.')\nfull_key: encoder")

is due to the pyg version.

You can find this problem at #https://github.com/pyg-team/pytorch_geometric/pull/4424 and solved by #https://github.com/pyg-team/pytorch_geometric/pull/4424/files/67a1babccfd8509df7acc5dae326b9728b697c69#diff-902b09f3ae69a1b7cd16a3c3e592cce64922bb3b3fe25a4b95d741b5e6830b3b

So I think you can fix the error by downloading fixed-version of pyg, but I simply modified the dimenet code of pytorch geometric of version 2.0.4.

I modified the BesselBasisLayer in torch_geometric.nn.models.dimenet.py as follows:

class BesselBasisLayer(torch.nn.Module):
    def __init__(self, num_radial: int, cutoff: float = 5.0,
                 envelope_exponent: int = 5):
        super().__init__()
        self.cutoff = cutoff
        self.envelope = Envelope(envelope_exponent)

        self.freq = torch.nn.Parameter(torch.empty(num_radial))

        self.reset_parameters()

    def reset_parameters(self):
        with torch.no_grad():
            torch.arange(1, self.freq.numel() + 1, out=self.freq).mul_(PI)
        self.freq.requires_grad_()

    def forward(self, dist):
        dist = dist.unsqueeze(-1) / self.cutoff
        return self.envelope(dist) * (self.freq * dist).sin()

# class BesselBasisLayer(torch.nn.Module):
#     def __init__(self, num_radial, cutoff=5.0, envelope_exponent=5):
#         super().__init__()
#         self.cutoff = cutoff
#         self.envelope = Envelope(envelope_exponent)

#         self.freq = torch.nn.Parameter(torch.Tensor(num_radial))

#         self.reset_parameters()

#     def reset_parameters(self):
#         torch.arange(1, self.freq.numel() + 1, out=self.freq).mul_(PI)

#     def forward(self, dist):
#         dist = dist.unsqueeze(-1) / self.cutoff
#         return self.envelope(dist) * (self.freq * dist).sin()

@sgbaird @SillyUglyPig The error stopped occurring for me if I upgraded to pyg-nightly. However, I haven’t been able to run to code for other reasons. But I think they are unrelated to pyg.