I got this error when trying to use the AdaptiveAvgPool3D in Pytorch. Below is the error trace
Traceback (most recent call last):
File "/scratch/a.bip5/BraTS 2021/./sisa.py", line 395, in outputs = model(inputs) File "/home/a.bip5/.conda/envs/pix2pix/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "/home/a.bip5/.conda/envs/pix2pix/lib/python3.9/site-packages/torch/nn/parallel/data_parallel.py", line 166, in forward return self.module(*inputs[0], **kwargs[0]) File "/home/a.bip5/.conda/envs/pix2pix/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "/scratch/a.bip5/BraTS 2021/./sisa.py", line 96, in forward x1 = self.pool1(x) File "/home/a.bip5/.conda/envs/pix2pix/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "/scratch/a.bip5/BraTS 2021/./sisa.py", line 135, in forward x1=aa(x) File "/home/a.bip5/.conda/envs/pix2pix/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "/home/a.bip5/.conda/envs/pix2pix/lib/python3.9/site-packages/torch/nn/modules/pooling.py", line 1166, in forward return F.adaptive_avg_pool3d(input, self.output_size) File "/home/a.bip5/.conda/envs/pix2pix/lib/python3.9/site-packages/torch/nn/functional.py", line 1148, in adaptive_avg_pool3d return torch._C._nn.adaptive_avg_pool3d(input, _output_size) TypeError: adaptive_avg_pool3d(): argument 'output_size' (position 2) must be tuple of ints, not list
When looking into the error stack I found this piece of code in ../functional.py:
if has_torch_function_unary(input):
return handle_torch_function(
adaptive_max_pool3d_with_indices, (input,), input, output_size, return_indices=return_indices
)
_output_size = _list_with_default(output_size, input.size())
return torch._C._nn.adaptive_max_pool3d(input, _output_size)
Printing out types for output_size and _output_size shows one's a tuple(as it is supposed to be) and the other is turned into a list before being passed through in the lib function itself. What I don't understand is why this conversion to list is taking place if the function using this list doesn't like it? If the purpose of this is to give out an error, what are the conditions that satisfy the has_torch_function_unary
condition?
Any help would be appreciated.
Edit:
I tried to side-step the problem by using output_size
in the final return statement instead of _output_size
. That led to an even more cryptic error-
TypeError: adaptive_avg_pool3d(): argument 'output_size' (position 2) must be tuple of ints, not tuple
How does pytorch differentiate between tuple of ints and a tuple?
The reason behind the error turned out to be a type error. When passing the size to AdaptiveAvgPool3d I was getting the values for the size through division. Although the remainder was 0, division by default saves values as float in python. This meant the tuple being passed as the output_size
was in fact a tuple of float which the _list_with_default turns into a list but likely doesn't if the tuple was made of int. Simply using int(..) for each dimension passed to AdaptiveAvgPool3d was the solution.