Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SparseAdam exhausts generator params before initializing Optimizer #47594

Open
shukob opened this issue Nov 9, 2020 · 2 comments · May be fixed by #47724
Open

SparseAdam exhausts generator params before initializing Optimizer #47594

shukob opened this issue Nov 9, 2020 · 2 comments · May be fixed by #47724

Comments

@shukob
Copy link

@shukob shukob commented Nov 9, 2020

🐛 Bug

__init__ on SparseAdam iterates over params to check if sparse Tensors are included, but it does not take a possibility that the params is actually a generator for example in a case where we use model.parameters. Therefore it fails the initialization with ValueError: optimizer got an empty parameter list in such cases.

To Reproduce

Steps to reproduce the behavior:

  1. Define a model that extends nn.Module, and include at least one trainable parameter.
  2. Initialize a SparseAdam instance by SparseAdam(model.parameters())
optimizer = optim.SparseAdam(model.parameters())
~/anaconda3/envs/SkipGram/lib/python3.7/site-packages/torch/optim/sparse_adam.py in __init__(self, params, lr, betas, eps)
     47 
     48         defaults = dict(lr=lr, betas=betas, eps=eps)
---> 49         super(SparseAdam, self).__init__(params, defaults)
     50 
     51     @torch.no_grad()

~/anaconda3/envs/SkipGram/lib/python3.7/site-packages/torch/optim/optimizer.py in __init__(self, params, defaults)
     45         param_groups = list(params)
     46         if len(param_groups) == 0:
---> 47             raise ValueError("optimizer got an empty parameter list")
     48         if not isinstance(param_groups[0], dict):
     49             param_groups = [{'params': param_groups}]

ValueError: optimizer got an empty parameter list

Expected behavior

Generator param should be copied before check using functions such as itertools.tee().

Environment

PyTorch version: 1.7.0
Is debug build: True
CUDA used to build PyTorch: 11.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.1 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: 10.0.0-4ubuntu1
CMake version: version 3.16.3

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 2080 Ti
Nvidia driver version: 455.32.00
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.0.4
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.0.4
/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] torch==1.7.0
[pip3] torchaudio==0.7.0a0+ac17b64
[pip3] torchvision==0.8.1
[conda] blas 1.0 mkl
[conda] cudatoolkit 11.0.221 h6bb024c_0
[conda] mkl 2020.2 256
[conda] mkl-service 2.3.0 py37he904b0f_0
[conda] mkl_fft 1.2.0 py37h23d657b_0
[conda] mkl_random 1.1.1 py37h0573a6f_0
[conda] numpy 1.19.2 py37h54aff64_0
[conda] numpy-base 1.19.2 py37hfa32c7d_0
[conda] pytorch 1.7.0 py3.7_cuda11.0.221_cudnn8.0.3_0 pytorch
[conda] torchaudio 0.7.0 py37 pytorch
[conda] torchvision 0.8.1 py37_cu110 pytorch

cc @vincentqb

@heitorschueroff
Copy link
Contributor

@heitorschueroff heitorschueroff commented Nov 9, 2020

@shukob Thank you for reporting this issue. We would accept a PR adding support for generators.

@mariosasko mariosasko linked a pull request that will close this issue Nov 11, 2020
@mariosasko
Copy link
Contributor

@mariosasko mariosasko commented Nov 11, 2020

Someone had the same issue on the forum so here is a quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants
You can’t perform that action at this time.