From 3b54592050359b7ba16db7009321858976053313 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Wed, 22 Mar 2023 16:12:02 -0700 Subject: [PATCH] [PyTorch] Add annotation_str benchmark (#96496) To be used to evaluate performance of following improvements. Baseline numbers: https://gist.github.com/swolchok/c8bcb92be1dc6e67c4f7efad498becd5 Differential Revision: [D43919653](https://our.internmc.facebook.com/intern/diff/D43919653/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D43919653/)! Pull Request resolved: https://github.com/pytorch/pytorch/pull/96496 Approved by: https://github.com/Skylion007 --- .../serialization/nested_annotation_str.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 benchmarks/serialization/nested_annotation_str.py diff --git a/benchmarks/serialization/nested_annotation_str.py b/benchmarks/serialization/nested_annotation_str.py new file mode 100644 index 00000000000000..0dad5c74ea7408 --- /dev/null +++ b/benchmarks/serialization/nested_annotation_str.py @@ -0,0 +1,23 @@ +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())