Skip to content

Commit e988736

Browse files
committed
update compute
1 parent 668601f commit e988736

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

fasterbench/compute.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ def as_dict(self) -> Dict[str, float | str]:
4141
def compute_compute(model: nn.Module, dummy_input: torch.Tensor) -> ComputeMetrics:
4242
"""Return **ComputeMetrics** for a single forward pass."""
4343

44+
try:
45+
model_device = next(model.parameters()).device
46+
except StopIteration:
47+
model_device = torch.device("cpu") # model has no parameters
48+
49+
if dummy_input.device != model_device:
50+
dummy_input = dummy_input.to(model_device)
51+
4452
params_m = round(
4553
sum(p.numel() for p in model.parameters() if p.requires_grad) / 1e6, 3
4654
)

nbs/03_compute.ipynb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@
6767
"def compute_compute(model: nn.Module, dummy_input: torch.Tensor) -> ComputeMetrics:\n",
6868
" \"\"\"Return **ComputeMetrics** for a single forward pass.\"\"\"\n",
6969
"\n",
70+
" try:\n",
71+
" model_device = next(model.parameters()).device\n",
72+
" except StopIteration:\n",
73+
" model_device = torch.device(\"cpu\") # model has no parameters\n",
74+
"\n",
75+
" if dummy_input.device != model_device:\n",
76+
" dummy_input = dummy_input.to(model_device)\n",
77+
"\n",
7078
" params_m = round(\n",
7179
" sum(p.numel() for p in model.parameters() if p.requires_grad) / 1e6, 3\n",
7280
" )\n",
@@ -99,7 +107,13 @@
99107
"source": []
100108
}
101109
],
102-
"metadata": {},
110+
"metadata": {
111+
"kernelspec": {
112+
"display_name": "python3",
113+
"language": "python",
114+
"name": "python3"
115+
}
116+
},
103117
"nbformat": 4,
104-
"nbformat_minor": 2
118+
"nbformat_minor": 4
105119
}

0 commit comments

Comments
 (0)