Files
pytorch/benchmarks/serialization/nested_annotation_str.py
Xuehai Pan c0ed38e644 [BE][Easy][3/19] enforce style for empty lines in import segments in benchmarks/ (#129754)
See https://github.com/pytorch/pytorch/pull/129751#issue-2380881501. Most changes are auto-generated by linter.

You can review these PRs via:

```bash
git diff --ignore-all-space --ignore-blank-lines HEAD~1
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/129754
Approved by: https://github.com/ezyang
2024-07-17 14:34:42 +00:00

34 lines
884 B
Python

import torch
import torch.utils.benchmark as benchmark
MEMO = {}
def create_nested_dict_type(layers):
if layers == 0:
return torch._C.StringType.get()
if layers not in MEMO:
less_nested = create_nested_dict_type(layers - 1)
result = torch._C.DictType(
torch._C.StringType.get(), torch._C.TupleType([less_nested, less_nested])
)
MEMO[layers] = result
return MEMO[layers]
nesting_levels = (1, 3, 5, 10)
types = (reasonable, medium, big, huge) = [
create_nested_dict_type(x) for x in nesting_levels
]
timers = [
benchmark.Timer(stmt="x.annotation_str", globals={"x": nested_type})
for nested_type in types
]
for nesting_level, typ, timer in zip(nesting_levels, types, timers):
print("Nesting level:", nesting_level)
print("output:", typ.annotation_str[:70])
print(timer.blocked_autorange())