pytorch
c975ca43 - [Static Runtime] Simplify out variant overload implementations (#65384)

Commit
3 years ago
[Static Runtime] Simplify out variant overload implementations (#65384) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/65384 The following pattern appears frequently in `ops.cpp`: ``` if (!n->matches(schema_1) && !n->matches(schema_2) && ... && !n->matches(schema_n)) { LogAndDumpSchema(n); return nullptr; } return [](ProcessedNode* p_node) { if (p_node->Output(0).isNone()) { if (p_node->Input(i).isSomeType()) { // special logic for schema 1 } else if (p_node->Input(i).isSomeOtherType()) { // special logic for schema 2 } else if (...) { // special logic for schema3 } // and so on } else { // another complicated type checking chain } }; ``` A much cleaner way to implement operator overloads is like this: ``` if (n->matches(schema_1)) { return schema_1_impl; } else if (n->matches(schema_2)) { return schema_2_impl; } // and so on ``` This has a few advantages: * Significantly reduces complexity of the out variant implementations, especially for ops with more than 2 overloads. One implementation corresponds to one schema. This makes the implementation more readable/maintainable. * Adhering to this convention makes it easier to add a new overload. Just add a new `n->matches(...)` case instead of working the schema into existing complicated logic. * Ops are marginally faster since we don't have to check types at runtime. Note: there are a few cases where this actually made the code less concise (`aten::div`), so I left those ops untouched. Thanks for pointing this out in another diff d1jang Test Plan: `buck test caffe2/benchmarks/static_runtime:static_runtime_cpptest` Reviewed By: hlu1 Differential Revision: D31072328 fbshipit-source-id: c40a4f7e6a79881e94c9ec49e9008ed75cfc8688
Author
Mike Iovine
Parents
Loading