Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/025' into 025
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher committed Jun 1, 2024
2 parents 44206f9 + 796d3b9 commit ebfb234
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benchmark/evaluate_famous_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
dsize = (1, 3, 299, 299)
inputs = torch.randn(dsize).to(device)
total_ops, total_params = profile(model, (inputs,), verbose=False)
print("%s | %.2f | %.2f" % (name, total_params / (1000 ** 2), total_ops / (1000 ** 3)))
print("%s | %.2f | %.2f" % (name, total_params / (1000**2), total_ops / (1000**3)))
2 changes: 1 addition & 1 deletion tests/test_conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ def test_conv2d_random(self):
flops, params = profile(net, inputs=(data,))
print(flops, params)
assert (
flops == n * out_c * oh * ow // g * in_c * kh * kw
flops == n * out_c * oh * ow // g * in_c * kh * kw
), f"{flops} v.s. {n * out_c * oh * ow // g * in_c * kh * kw}"
1 change: 0 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from thop import utils


Expand Down
3 changes: 1 addition & 2 deletions thop/fx_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ def fx_profile(mod: nn.Module, input: th.Tensor, verbose=False):


if __name__ == "__main__":

class MyOP(nn.Module):
def forward(self, input):
"""Performs forward pass on given input data."""
return input / 1


class MyModule(torch.nn.Module):
def __init__(self):
"""Initializes MyModule with two linear layers and a custom MyOP operator."""
Expand All @@ -233,7 +233,6 @@ def forward(self, x):
out2 = self.linear2(x).clamp(min=0.0, max=1.0)
return self.myop(out1 + out2)


net = MyModule()
data = th.randn(20, 5)
flops = fx_profile(net, data, verbose=False)
Expand Down
8 changes: 4 additions & 4 deletions thop/vision/basic_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ def count_adap_avgpool(m, x, y):
def count_upsample(m, x, y):
"""Update the total operations counter in the given module for supported upsampling modes."""
if m.mode not in (
"nearest",
"linear",
"bilinear",
"bicubic",
"nearest",
"linear",
"bilinear",
"bicubic",
): # "trilinear"
logging.warning("mode %s is not implemented yet, take it a zero op" % m.mode)
m.total_ops += 0
Expand Down
2 changes: 0 additions & 2 deletions thop/vision/efficientnet.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@


register_hooks = {}
16 changes: 8 additions & 8 deletions thop/vision/onnx_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def onnx_counter_conv(diction, node):
group = attr.i
# print(dim_dil)
dim_input = diction[node.input[0]]
output_size = np.append(dim_input[0: -np.array(dim_kernel).size - 1], dim_weight[0])
hw = np.array(dim_input[-np.array(dim_kernel).size:])
output_size = np.append(dim_input[0 : -np.array(dim_kernel).size - 1], dim_weight[0])
hw = np.array(dim_input[-np.array(dim_kernel).size :])
for i in range(hw.size):
hw[i] = int((hw[i] + 2 * dim_pad[i] - dim_dil[i] * (dim_kernel[i] - 1) - 1) / dim_stride[i] + 1)
output_size = np.append(output_size, hw)
Expand Down Expand Up @@ -238,15 +238,15 @@ def onnx_counter_averagepool(diction, node):
dim_dil = attr.ints
# print(dim_dil)
dim_input = diction[node.input[0]]
hw = dim_input[-np.array(dim_kernel).size:]
hw = dim_input[-np.array(dim_kernel).size :]
if dim_pad is not None:
for i in range(hw.size):
hw[i] = int((hw[i] + 2 * dim_pad[i] - dim_kernel[i]) / dim_stride[i] + 1)
output_size = np.append(dim_input[0: -np.array(dim_kernel).size], hw)
output_size = np.append(dim_input[0 : -np.array(dim_kernel).size], hw)
else:
for i in range(hw.size):
hw[i] = int((hw[i] - dim_kernel[i]) / dim_stride[i] + 1)
output_size = np.append(dim_input[0: -np.array(dim_kernel).size], hw)
output_size = np.append(dim_input[0 : -np.array(dim_kernel).size], hw)
# print(macs, output_size, output_name)
return macs, output_size, output_name

Expand Down Expand Up @@ -293,15 +293,15 @@ def onnx_counter_maxpool(diction, node):
dim_dil = attr.ints
# print(dim_dil)
dim_input = diction[node.input[0]]
hw = dim_input[-np.array(dim_kernel).size:]
hw = dim_input[-np.array(dim_kernel).size :]
if dim_pad is not None:
for i in range(hw.size):
hw[i] = int((hw[i] + 2 * dim_pad[i] - dim_kernel[i]) / dim_stride[i] + 1)
output_size = np.append(dim_input[0: -np.array(dim_kernel).size], hw)
output_size = np.append(dim_input[0 : -np.array(dim_kernel).size], hw)
else:
for i in range(hw.size):
hw[i] = int((hw[i] - dim_kernel[i]) / dim_stride[i] + 1)
output_size = np.append(dim_input[0: -np.array(dim_kernel).size], hw)
output_size = np.append(dim_input[0 : -np.array(dim_kernel).size], hw)
# print(macs, output_size, output_name)
return macs, output_size, output_name

Expand Down

0 comments on commit ebfb234

Please sign in to comment.