From f98ab184452e748d7b79629bbd079fcbb909d838 Mon Sep 17 00:00:00 2001 From: Jiakai Liu Date: Thu, 19 Nov 2020 12:27:23 -0800 Subject: [PATCH] [pytorch][codegen] move is_abstract property to NativeFunction model (#48252) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/48252 Moved to a shared place so that gen_variable_type.py can reuse it. Test Plan: Imported from OSS Reviewed By: ezyang Differential Revision: D25087808 Pulled By: ljk53 fbshipit-source-id: 1f32e506956fc4eb08734cfde0add47b3e666bd9 --- tools/codegen/gen.py | 23 +---------------------- tools/codegen/model.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/tools/codegen/gen.py b/tools/codegen/gen.py index 5324246d6c8e..a0a1f21d13dc 100644 --- a/tools/codegen/gen.py +++ b/tools/codegen/gen.py @@ -845,12 +845,6 @@ def compute_declaration_yaml(f: NativeFunction) -> object: is_factory_method = any(isinstance(a.argument, TensorOptionsArguments) for a in cpp_args) \ and Variant.method not in f.variants - if f.structured_delegate: - # Structured functions MUST have a dispatch table - is_abstract = True - else: - is_abstract = f.dispatch.keys() != {'Math'} - return OrderedDict([ ('name', cpp.name(f.func)), ('operator_name', str(f.func.name.name)), @@ -869,22 +863,7 @@ def compute_declaration_yaml(f: NativeFunction) -> object: ('returns', returns), ('inplace', f.func.name.name.inplace), ('is_factory_method', is_factory_method), - # Note [Abstract ATen methods] - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - # An abstract ATen method is one whose dispatch differs between - # types. These are implemented in derived types (with a - # standard (throwing) definition in Type). A concrete ATen - # method is one which has the same dispatch for all types; - # we just implement it in the base Type. This is exposed - # in Declarations.yaml via a field named 'abstract'. - # - # Although this is what we have historically exposed, it is - # actually not all that useful for end users, who are also interested - # whether or not there is an explicit entry in derivatives.yaml - # for the entry or not (as this affects whether or not the operation is - # overrideable or not.) Once this all gets cleaned up, this - # property will be obsolete. - ('abstract', is_abstract), + ('abstract', f.is_abstract), ('device_guard', f.device_guard), ('with_gil', False), ('deprecated', False), diff --git a/tools/codegen/model.py b/tools/codegen/model.py index bddbfd4f5338..c7425444c165 100644 --- a/tools/codegen/model.py +++ b/tools/codegen/model.py @@ -125,6 +125,22 @@ class NativeFunction: # in terms of the out kernel referenced by the string here. structured_delegate: Optional['OperatorName'] + # Note [Abstract ATen methods] + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + # An abstract ATen method is one whose dispatch differs between + # types. These are implemented in derived types (with a + # standard (throwing) definition in Type). A concrete ATen + # method is one which has the same dispatch for all types; + # we just implement it in the base Type. This is exposed + # in Declarations.yaml via a field named 'abstract'. + @property + def is_abstract(self) -> bool: + if self.structured_delegate: + # Structured functions MUST have a dispatch table + return True + else: + return self.dispatch.keys() != {'Math'} + # NB: The benefit of defining a dataclass is that we automatically get # a constructor defined for all the fields we specify. No need # to explicitly write it out.