1*da0073e9SAndroid Build Coastguard Worker# `__torch_function__` micro-benchmarks 2*da0073e9SAndroid Build Coastguard Worker 3*da0073e9SAndroid Build Coastguard WorkerThis benchmark suite provides a systemic way to measure the performance of `__torch_function__` overhead. 4*da0073e9SAndroid Build Coastguard Worker 5*da0073e9SAndroid Build Coastguard Worker## Getting started 6*da0073e9SAndroid Build Coastguard Worker### Initial Setup 7*da0073e9SAndroid Build Coastguard WorkerInstall `py-spy` by doing: 8*da0073e9SAndroid Build Coastguard Worker 9*da0073e9SAndroid Build Coastguard Worker```bash 10*da0073e9SAndroid Build Coastguard Workerpip install py-spy 11*da0073e9SAndroid Build Coastguard Worker``` 12*da0073e9SAndroid Build Coastguard Worker 13*da0073e9SAndroid Build Coastguard WorkerNote that more extensive documentation on using `py-spy` is available in `CONTRIBUTING.md`. 14*da0073e9SAndroid Build Coastguard Worker 15*da0073e9SAndroid Build Coastguard Worker### Running the benchmark 16*da0073e9SAndroid Build Coastguard WorkerRun one of the following commands in the terminal, with the working directory being `${PYTORCH_CLONE_DIR}/benchmarks/overrides_benchmark`: 17*da0073e9SAndroid Build Coastguard Worker 18*da0073e9SAndroid Build Coastguard Worker```bash 19*da0073e9SAndroid Build Coastguard Worker# Benchmark all the cases 20*da0073e9SAndroid Build Coastguard Workerpython bench.py 21*da0073e9SAndroid Build Coastguard Worker 22*da0073e9SAndroid Build Coastguard Worker# Flame graph pertaining to each case. 23*da0073e9SAndroid Build Coastguard Workerpy-spy record -o tensor.svg --native -- python pyspybench.py Tensor 24*da0073e9SAndroid Build Coastguard Workerpy-spy record -o subtensor.svg --native -- python pyspybench.py SubTensor 25*da0073e9SAndroid Build Coastguard Workerpy-spy record -o overridden.svg --native -- python pyspybench.py WithTorchFunction 26*da0073e9SAndroid Build Coastguard Workerpy-spy record -o suboverridden.svg --native -- python pyspybench.py SubWithTorchFunction 27*da0073e9SAndroid Build Coastguard Worker``` 28*da0073e9SAndroid Build Coastguard Worker 29*da0073e9SAndroid Build Coastguard WorkerHere is a brief overview of what the results should look like, if run correctly: 30*da0073e9SAndroid Build Coastguard Worker 31*da0073e9SAndroid Build Coastguard Worker* Overhead for `torch` functions when run on `torch.Tensor` objects is on the order of 2 μs. 32*da0073e9SAndroid Build Coastguard Worker* `__torch_function__` should add zero overhead for `torch.Tensor` inputs, a small overhead for subclasses of `torch.Tensor`, and a couple of microseconds for `Tensor`-likes with `__torch_function__`. 33*da0073e9SAndroid Build Coastguard Worker* Changing the dispatching mechanism may result in changes that are on the order of 100 ns, which are hard to detect due to noise, but important. 34*da0073e9SAndroid Build Coastguard Worker 35*da0073e9SAndroid Build Coastguard Worker## Reporting benchmark results 36*da0073e9SAndroid Build Coastguard WorkerWhen modifying any of the machinery around `__torch_function__`, run the benchmark for both the feature branch and the point it diverges from `master`. For each of these: 37*da0073e9SAndroid Build Coastguard Worker 38*da0073e9SAndroid Build Coastguard Worker* Run `bench.py`, and include the output in your result. 39*da0073e9SAndroid Build Coastguard Worker* For each case where `bench.py` shows a regression, run the commands described above, prefixing the output SVG filename (the input to the `-o` switch) with `base-` or `branch-` depending on the commit you are running the benchmark on. 40*da0073e9SAndroid Build Coastguard Worker* For each SVG, open it in the browser, take a screenshot and include it in your result. Also include a ZIP file with all SVGs thus produced included. 41