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

One complex parameter should count as two params #193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
152 changes: 152 additions & 0 deletions tests/complex_test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"from torch.nn import functional as F\n",
"from torchinfo import summary\n",
"\n",
"class RealFFN(nn.Module):\n",
" \"\"\" A real 2-layer FFN \"\"\"\n",
" def __init__(self) -> None:\n",
" super().__init__()\n",
" self.fc1 = nn.Linear(128, 256)\n",
" self.fc2 = nn.Linear(256, 128)\n",
" def forward(self, x: torch.Tensor) -> torch.Tensor:\n",
" x = F.relu(self.fc1(x))\n",
" x = self.fc2(x)\n",
" return x\n",
"\n",
"realnet = RealFFN()\n",
"\n",
"class ComplexFFN(nn.Module):\n",
" \"\"\" A complex complex fc linear layer \"\"\"\n",
" def __init__(self) -> None:\n",
" super().__init__()\n",
" self.fc1 = nn.Linear(128, 256, dtype=torch.cfloat)\n",
" self.fc2 = nn.Linear(256, 128, dtype=torch.cfloat)\n",
" def forward(self, x: torch.Tensor) -> torch.Tensor:\n",
" x = torch.complex(x, x)\n",
" x = self.fc1(x)\n",
" x = torch.view_as_real(x)\n",
" x = F.relu(x)\n",
" x = torch.view_as_complex(x)\n",
" x = self.fc2(x)\n",
" return x\n",
"\n",
"complexnet = ComplexFFN()\n",
"input_size = (2, 101, 101, 128)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"==========================================================================================\n",
"Layer (type:depth-idx) Output Shape Param #\n",
"==========================================================================================\n",
"RealFFN [2, 101, 101, 128] --\n",
"├─Linear: 1-1 [2, 101, 101, 256] 33,024\n",
"├─Linear: 1-2 [2, 101, 101, 128] 32,896\n",
"==========================================================================================\n",
"Total params: 65,920\n",
"Trainable params: 65,920\n",
"Non-trainable params: 0\n",
"Total mult-adds (M): 0.13\n",
"==========================================================================================\n",
"Input size (MB): 10.45\n",
"Forward/backward pass size (MB): 62.67\n",
"Params size (MB): 0.26\n",
"Estimated Total Size (MB): 73.38\n",
"=========================================================================================="
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary(realnet, input_size=input_size, verbose=0)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"==========================================================================================\n",
"Layer (type:depth-idx) Output Shape Param #\n",
"==========================================================================================\n",
"ComplexFFN [2, 101, 101, 128] --\n",
"├─Linear: 1-1 [2, 101, 101, 256] 66,048\n",
"├─Linear: 1-2 [2, 101, 101, 128] 65,792\n",
"==========================================================================================\n",
"Total params: 131,840\n",
"Trainable params: 131,840\n",
"Non-trainable params: 0\n",
"Total mult-adds (M): 0.26\n",
"==========================================================================================\n",
"Input size (MB): 10.45\n",
"Forward/backward pass size (MB): 125.35\n",
"Params size (MB): 1.05\n",
"Estimated Total Size (MB): 136.85\n",
"=========================================================================================="
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summary(complexnet, input_size=input_size, verbose=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.10 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
9 changes: 8 additions & 1 deletion torchinfo/layer_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,17 @@ def get_param_count(
if name.endswith("_orig"):
without_suffix = name[:-5]
pruned_weights = rgetattr(module, f"{without_suffix}_mask")
param = rgetattr(module, name)
if pruned_weights is not None:
parameter_count = int(torch.sum(pruned_weights))
if torch.is_complex(param):
parameter_count *= 2
return parameter_count, without_suffix
return param.nelement(), name

parameter_count = (
2 * param.nelement() if torch.is_complex(param) else param.nelement()
)
return parameter_count, name

@staticmethod
def get_kernel_size(module: nn.Module) -> int | list[int] | None:
Expand Down