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

AttributeError: 'tuple' object has no attribute 'size' #195

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pylint
pytest
pytest-cov
pre-commit
transformers
46 changes: 46 additions & 0 deletions tests/test_output/huggingface.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
==============================================================================================================
Layer (type:depth-idx) Output Shape Param #
==============================================================================================================
T5ForConditionalGeneration [2, 100, 512] --
├─T5Stack: 1-1 [2, 100, 512] 35,332,800
├─T5Stack: 1-2 -- (recursive)
│ └─Embedding: 2-1 [2, 100, 512] 16,449,536
├─T5Stack: 1-3 -- (recursive)
│ └─Dropout: 2-2 [2, 100, 512] --
│ └─ModuleList: 2-3 -- --
│ │ └─T5Block: 3-1 [2, 100, 512] 2,360,512
│ │ └─T5Block: 3-2 [2, 100, 512] 2,360,320
│ │ └─T5Block: 3-3 [2, 100, 512] 2,360,320
│ │ └─T5Block: 3-4 [2, 100, 512] 2,360,320
│ │ └─T5Block: 3-5 [2, 100, 512] 2,360,320
│ │ └─T5Block: 3-6 [2, 100, 512] 2,360,320
│ │ └─T5Block: 3-7 [2, 100, 512] 2,360,320
│ │ └─T5Block: 3-8 [2, 100, 512] 2,360,320
│ └─T5LayerNorm: 2-4 [2, 100, 512] 512
│ └─Dropout: 2-5 [2, 100, 512] --
├─T5Stack: 1-4 [2, 6, 100, 64] 16,449,536
│ └─Embedding: 2-6 [2, 100, 512] (recursive)
│ └─Dropout: 2-7 [2, 100, 512] --
│ └─ModuleList: 2-8 -- --
│ │ └─T5Block: 3-9 [2, 100, 512] 3,147,456
│ │ └─T5Block: 3-10 [2, 100, 512] 3,147,264
│ │ └─T5Block: 3-11 [2, 100, 512] 3,147,264
│ │ └─T5Block: 3-12 [2, 100, 512] 3,147,264
│ │ └─T5Block: 3-13 [2, 100, 512] 3,147,264
│ │ └─T5Block: 3-14 [2, 100, 512] 3,147,264
│ │ └─T5Block: 3-15 [2, 100, 512] 3,147,264
│ │ └─T5Block: 3-16 [2, 100, 512] 3,147,264
│ └─T5LayerNorm: 2-9 [2, 100, 512] 512
│ └─Dropout: 2-10 [2, 100, 512] --
├─Linear: 1-5 [2, 100, 32128] 16,449,536
==============================================================================================================
Total params: 76,961,152
Trainable params: 76,961,152
Non-trainable params: 0
Total mult-adds (M): 186.86
==============================================================================================================
Input size (MB): 0.00
Forward/backward pass size (MB): 217.84
Params size (MB): 307.84
Estimated Total Size (MB): 525.69
==============================================================================================================
13 changes: 13 additions & 0 deletions tests/torchinfo_xl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,16 @@ def test_google() -> None:
# Check googlenet in training mode since InceptionAux layers are used in
# forward-prop in train mode but not in eval mode.
summary(google_net, (1, 3, 112, 112), depth=7, mode="train")


def test_huggingface():
from transformers import AutoModelForSeq2SeqLM

model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
inputs = {
"input_ids": torch.zeros(2, 100).long(),
"attention_mask": torch.zeros(2, 100).long(),
"labels": torch.zeros(2, 100).long(),
}
with torch.no_grad():
summary(model, input_data=inputs)
6 changes: 6 additions & 0 deletions torchinfo/layer_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ def nested_list_size(inputs: Sequence[Any]) -> tuple[list[int], int]:
size = []
elem_bytes = list(inputs.values())[0].element_size()
for _, output in inputs.items():
# fix for huggingface modules that return tuple
if not isinstance(output, torch.Tensor):
temp = output
while isinstance(temp, tuple):
temp = temp[0]
output = temp
size = list(output.size())
if batch_dim is not None:
size = [size[:batch_dim] + [1] + size[batch_dim + 1 :]]
Expand Down