|
1 |
| -# PyTorch/Caffe2 Operator Micro-benchmarks |
| 1 | +# PyTorch Operator Micro-benchmarks |
2 | 2 |
|
3 |
| -This benchmark suite provides a systemic way to measure the performance of operators for a wide range of inputs. The generated benchmark data fully characterized the performance of an operator in terms of execution time and the efficiency of the PyTorch/Caffe2 frameworks used. |
| 3 | +This benchmark suite provides a systemic way to measure the performance of operators for a wide range of inputs. The generated benchmark data fully characterized the performance of an operator in terms of execution time and the efficiency of the PyTorch frameworks used. |
4 | 4 |
|
5 | 5 | ## Features
|
6 | 6 |
|
7 | 7 | Key Features:
|
8 | 8 |
|
9 | 9 | 1\. Language used: Python
|
10 | 10 |
|
11 |
| -2\. Supported Frameworks: PyTorch and Caffe2 |
| 11 | +2\. Supported Frameworks: PyTorch |
12 | 12 |
|
13 | 13 | 3\. Supported PyTorch mode: eager and JIT
|
14 | 14 |
|
@@ -49,7 +49,7 @@ python -m benchmark_all_test
|
49 | 49 | ```
|
50 | 50 |
|
51 | 51 | ## Code to support `torch.add` in the benchmark
|
52 |
| -The following example shows the code to support `torch.add` with 27 different tests. In the subpages of this wiki, we'll step through the complete flow of adding PyTorch and Caffe2 operators to the benchmark suite. Existing benchmarks for operators are in `pt` and `c2` directories and we highly recommend putting your new operators in those locations. |
| 52 | +The following example shows the code to support `torch.add` with 27 different tests. In the subpages of this wiki, we'll step through the complete flow of adding PyTorch operators to the benchmark suite. Existing benchmarks for operators are in the `pt` directory and we highly recommend putting your new operators in those locations. |
53 | 53 |
|
54 | 54 | ```python
|
55 | 55 | add_short_configs = op_bench.cross_product_configs(
|
@@ -77,7 +77,7 @@ op_bench.generate_pt_test(add_short_configs, AddBenchmark)
|
77 | 77 | The output is intended to be a human readable format. Here is an example output for `torch.add`:
|
78 | 78 | ```
|
79 | 79 | # ----------------------------------------
|
80 |
| -# PyTorch/Caffe2 Operator Micro-benchmarks |
| 80 | +# PyTorch Operator Micro-benchmarks |
81 | 81 | # ----------------------------------------
|
82 | 82 | # Tag : short
|
83 | 83 |
|
@@ -146,7 +146,7 @@ python -m pt.add_test --tag-filter long
|
146 | 146 | ```
|
147 | 147 |
|
148 | 148 | ## Adding New Operators to the Benchmark Suite
|
149 |
| -In the previous sections, we gave several examples to show how to run the already available operators in the benchmark suite. In the following sections, we'll step through the complete flow of adding PyTorch and Caffe2 operators to the benchmark suite. Existing benchmarks for operators are in `pt` and `c2` directories and we highly recommend putting your new operators in those directories as well. |
| 149 | +In the previous sections, we gave several examples to show how to run the already available operators in the benchmark suite. In the following sections, we'll step through the complete flow of adding PyTorch operators to the benchmark suite. Existing benchmarks for operators are in the `pt` directory and we highly recommend putting your new operators in those directories as well. |
150 | 150 |
|
151 | 151 | ### Add a New PyTorch Operator
|
152 | 152 | Let's say you want to measure the execution time of the following operator:
|
@@ -260,55 +260,6 @@ if __name__ == "__main__":
|
260 | 260 | ```
|
261 | 261 | That's it. You just added a new operator to the benchmark suite!
|
262 | 262 |
|
263 |
| - |
264 |
| -### Add a New Caffe2 Operator |
265 |
| -The steps to add a new Caffe2 operator is the same as that for a PyTorch operator. The code below shows how to add Caffe2 `Add` operator: |
266 |
| -```python |
267 |
| -import operator_benchmark as op_bench |
268 |
| -from caffe2.python import core |
269 |
| - |
270 |
| -add_long_configs = op_bench.cross_product_configs( |
271 |
| - M=[8, 64, 128], |
272 |
| - N=range(2, 10, 3), |
273 |
| - K=[2 ** x for x in range(0, 3)], |
274 |
| - tags=["long"] |
275 |
| -) |
276 |
| - |
277 |
| -add_short_configs = op_bench.config_list( |
278 |
| - attrs=[ |
279 |
| - [8, 16, 32], |
280 |
| - [16, 16, 64], |
281 |
| - [64, 64, 128], |
282 |
| - ], |
283 |
| - attr_names=["M", "N", "K"], |
284 |
| - tags=["short"], |
285 |
| -) |
286 |
| - |
287 |
| -class AddBenchmark(op_bench.Caffe2BenchmarkBase): |
288 |
| - |
289 |
| - def init(self, M, N, K): |
290 |
| - self.input_one = self.tensor(M, N, K) |
291 |
| - self.input_two = self.tensor(M, N, K) |
292 |
| - self.output = self.tensor(M, N, K) |
293 |
| - self.set_module_name("add") |
294 |
| - |
295 |
| - def forward(self): |
296 |
| - op = core.CreateOperator( |
297 |
| - "Add", [self.input_one, self.input_two], self.output, **self.args |
298 |
| - ) |
299 |
| - |
300 |
| - return op |
301 |
| - |
302 |
| -op_bench.generate_c2_test(add_long_configs + add_short_configs, AddBenchmark) |
303 |
| - |
304 |
| -if __name__ == "__main__": |
305 |
| - op_bench.benchmark_runner.main() |
306 |
| -``` |
307 |
| -There are two things worth mentioning in this code: |
308 |
| -* `self.tensor` is a helper function which takes shapes and returns a Caffe2 blob. It is designed to make the tensor creation step easier compared to the standard Caffe2 way. |
309 |
| -* `generate_c2_test` is used to register Caffe2 tests with the benchmark. |
310 |
| - |
311 |
| - |
312 | 263 | ### Add a List of Operators
|
313 | 264 | In the previous sections, we introduced the steps required to add a single operator to the benchmark suite. There are scenarios where you want to extend the benchmark suite with a list of operators which can share the same inputs. For example, to benchmark `abs` and `acos` operators, you can use the same set of inputs for both.
|
314 | 265 |
|
@@ -416,37 +367,3 @@ The example below shows the relevant code for that:
|
416 | 367 | self.input_one = torch.rand(M, N, K, requires_grad=True)
|
417 | 368 | generate_pt_gradient_test(long_configs + short_configs, TorchAddBenchmark)
|
418 | 369 | ```
|
419 |
| -#### For Caffe2 Gradient Ops |
420 |
| -To add Caffe2 gradient ops, we need to implement a new backward method in the benchmark class: |
421 |
| -```python |
422 |
| -class AddBenchmark(op_bench.Caffe2BenchmarkBase): |
423 |
| - |
424 |
| - def init(self, M, N, K): |
425 |
| - self.input_one = self.tensor(M, N, K) |
426 |
| - self.input_two = self.tensor(M, N, K) |
427 |
| - self.input_one_grad = self.tensor(M, N, K) |
428 |
| - self.input_two_grad = self.tensor(M, N, K) |
429 |
| - self.output = self.tensor(M, N, K) |
430 |
| - self.set_module_name("add") |
431 |
| - |
432 |
| - def forward(self): |
433 |
| - op = core.CreateOperator( |
434 |
| - "Add", [self.input_one, self.input_two], self.output, **self.args |
435 |
| - ) |
436 |
| - |
437 |
| - return op |
438 |
| - |
439 |
| - def backward(self): |
440 |
| - grad_op = core.CreateOperator( |
441 |
| - "AddGradient", |
442 |
| - [self.output, self.input_one, self.input_two], |
443 |
| - [self.input_one_grad, self.input_two_grad], **self.args |
444 |
| - ) |
445 |
| - |
446 |
| - return grad_op |
447 |
| - |
448 |
| -op_bench.generate_c2_gradient_test(long_configs + short_configs,AddBenchmark) |
449 |
| -``` |
450 |
| -After the class is implemented, we need to register the tests with `generate_c2_gradient_test` function. |
451 |
| - |
452 |
| -This concludes the overview of the operator benchmark suite. |
0 commit comments