From ece02218542b345efec8d98b29d3cd897772f28e Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Fri, 8 Oct 2021 08:23:18 -0700 Subject: [PATCH] Rename int to long, add more C++ types. (#66108) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/66108 BC-breaking change: intT is now longT (which aligns it more accurately with how the types are referred to in C++). The benefit for this is we can idiomatically express all C++ dtypes (with intT now mapping to int32_t). These types are needed for ufunc codegen in a latter patch. Signed-off-by: Edward Z. Yang Test Plan: Imported from OSS Reviewed By: albanD Differential Revision: D31385761 Pulled By: ezyang fbshipit-source-id: ec6f3a0953794313470dbe14911f23ac116be425 --- tools/autograd/gen_autograd_functions.py | 6 +++--- tools/autograd/gen_inplace_or_view_type.py | 8 ++++---- tools/autograd/load_derivatives.py | 10 +++++----- tools/codegen/api/types.py | 17 +++++++++++++++-- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/tools/autograd/gen_autograd_functions.py b/tools/autograd/gen_autograd_functions.py index 2fb0d3a7e94..0e79eb16a65 100644 --- a/tools/autograd/gen_autograd_functions.py +++ b/tools/autograd/gen_autograd_functions.py @@ -11,7 +11,7 @@ from typing import List, Sequence, Tuple from tools.codegen.api.autograd import (Derivative, DifferentiabilityInfo, SavedAttribute, uses_retain_variables, uses_single_grad) -from tools.codegen.api.types import (Binding, BaseCType, OptionalCType, tensorT, intT, +from tools.codegen.api.types import (Binding, BaseCType, OptionalCType, tensorT, longT, doubleT, scalarT, stringT, boolT, intArrayRefT, tensorListT, MutRefCType, ListCType, ArrayRefCType) from tools.codegen.code_template import CodeTemplate @@ -276,7 +276,7 @@ if (prop.isComplex()) { """ MISC_GETTER_DEFS = { - OptionalCType(BaseCType(intT)): (GETTER_DEFINITION_OPT, GETTER_BODY_INT64_T), + OptionalCType(BaseCType(longT)): (GETTER_DEFINITION_OPT, GETTER_BODY_INT64_T), BaseCType(doubleT): (GETTER_DEFINITION, GETTER_BODY_DOUBLE), OptionalCType(BaseCType(doubleT)): (GETTER_DEFINITION_OPT, GETTER_BODY_DOUBLE), BaseCType(boolT): (GETTER_DEFINITION, GETTER_BODY_BOOL), @@ -430,7 +430,7 @@ def process_function(info: DifferentiabilityInfo, template: CodeTemplate) -> str saved_variables.append(f'c10::OptionalArray {name};') getter_definitions.append(GETTER_DEFINITION_OPT_ARRAYREF.substitute( op=info.op, name=name, body=GETTER_BODY_ARRAYREF_DOUBLE)) - elif type == BaseCType(intT): + elif type == BaseCType(longT): saved_variables.append(f'{type.cpp_type()} {name} = 0;') getter_definitions.append(GETTER_DEFINITION.substitute( op=info.op, name=name, body=GETTER_BODY_INT64_T)) diff --git a/tools/autograd/gen_inplace_or_view_type.py b/tools/autograd/gen_inplace_or_view_type.py index cd8a5bd43fc..6e5ab73699c 100644 --- a/tools/autograd/gen_inplace_or_view_type.py +++ b/tools/autograd/gen_inplace_or_view_type.py @@ -4,7 +4,7 @@ from tools.codegen.api.autograd import ( dispatch_strategy, ) from tools.codegen.api.types import (Binding, DispatcherSignature, CppSignatureGroup, CType, - BaseCType, OptionalCType, intT, boolT, intArrayRefT) + BaseCType, OptionalCType, longT, boolT, intArrayRefT) from tools.codegen.code_template import CodeTemplate from tools.codegen.context import with_native_function from tools.codegen.model import ( @@ -244,8 +244,8 @@ def emit_view_lambda(f: NativeFunction, unpacked_bindings: List[Binding]) -> str replay_view_func = '' updated_unpacked_args: List[str] = [] known_view_arg_simple_types: List[CType] = [ - BaseCType(intT), - OptionalCType(BaseCType(intT)), + BaseCType(longT), + OptionalCType(BaseCType(longT)), BaseCType(boolT), BaseCType(intArrayRefT)] for unpacked_binding in unpacked_bindings: @@ -266,7 +266,7 @@ def emit_view_lambda(f: NativeFunction, unpacked_bindings: List[Binding]) -> str arg_vec = arg + '_vec' replay_view_func += ARRAYREF_TO_VEC.substitute(arg=arg, vec=arg_vec) updated_unpacked_args.append(arg_vec) - elif arg_type == OptionalCType(BaseCType(intT)): + elif arg_type == OptionalCType(BaseCType(longT)): # Materialize int64_t? to int64_t arg_value = arg + '_val' replay_view_func += OPTIONAL_TO_VAL.substitute(arg=arg, val=arg_value, default='0') diff --git a/tools/autograd/load_derivatives.py b/tools/autograd/load_derivatives.py index 27735c0dc70..70e65e89172 100644 --- a/tools/autograd/load_derivatives.py +++ b/tools/autograd/load_derivatives.py @@ -10,7 +10,7 @@ import yaml from tools.codegen.api.autograd import (Derivative, DifferentiabilityInfo, SavedAttribute, ForwardDerivative) from tools.codegen.api.types import (Binding, CppSignatureGroup, NamedCType, BaseCType, VectorCType, - intArrayRefT, tensorOptionsT, typeAndSizeT, intT, boolT, + intArrayRefT, tensorOptionsT, typeAndSizeT, longT, boolT, tensorGeometryT, scalarTypeT, SpecialArgName, OptionalCType, stringT) from tools.codegen.api import cpp @@ -518,17 +518,17 @@ def saved_variables( # replace self.size(2) with self_size_2 (r'{}.size\((\w+)\)', { 'suffix': lambda m: '_argsize_{}'.format(*m.groups()), - 'nctype': lambda name: NamedCType(name, BaseCType(intT)), + 'nctype': lambda name: NamedCType(name, BaseCType(longT)), }), # replace self.numel() with self_numel (r'{}.numel\(\)', { 'suffix': '_numel', - 'nctype': lambda name: NamedCType(name, BaseCType(intT)), + 'nctype': lambda name: NamedCType(name, BaseCType(longT)), }), # replace to_args_sizes(self) with self_args_sizes (r'to_args_sizes\({}\)', { 'suffix': '_args_sizes', - 'nctype': lambda name: NamedCType(name, VectorCType(VectorCType(BaseCType(intT)))), + 'nctype': lambda name: NamedCType(name, VectorCType(VectorCType(BaseCType(longT)))), }), # replace to_args_scalartypes(self) with self_args_scalartypes (r'to_args_scalartypes\({}\)', { @@ -547,7 +547,7 @@ def saved_variables( # replace self.dim() with self_dim (r'{}.dim\(\)', { 'suffix': '_dim', - 'nctype': lambda name: NamedCType(name, BaseCType(intT)), + 'nctype': lambda name: NamedCType(name, BaseCType(longT)), }), # replace self.strides() with self_strides (r'{}.strides\(\)', { diff --git a/tools/codegen/api/types.py b/tools/codegen/api/types.py index caa6f347c36..19802ec2e72 100644 --- a/tools/codegen/api/types.py +++ b/tools/codegen/api/types.py @@ -30,9 +30,22 @@ class BaseCppType: # The set of all non-templated, valid, fully-qualified names of C++ types that are used in the codegen. # Templated types get their own dataclass, mainly to make namespace parsing easier. -intT = BaseCppType('', 'int64_t') +byteT = BaseCppType('', 'uint8_t') +charT = BaseCppType('', 'int8_t') +shortT = BaseCppType('', 'int16_t') +# It would be more symmetric for this to be called intT, but it easy to mix +# this up with JIT int (which is int64_t in C++), so we intentionally don't +# define intT to make it obvious when you've stuffed it up +int32T = BaseCppType('', 'int32_t') +longT = BaseCppType('', 'int64_t') +halfT = BaseCppType('at', 'Half') doubleT = BaseCppType('', 'double') +floatT = BaseCppType('', 'float') +complexHalfT = BaseCppType('c10', 'complex') # stuffing template param here is an abuse +complexFloatT = BaseCppType('c10', 'complex') +complexDoubleT = BaseCppType('c10', 'complex') boolT = BaseCppType('', 'bool') +bfloat16T = BaseCppType('at', 'BFloat16') voidT = BaseCppType('', 'void') stringT = BaseCppType('c10', 'string_view') generatorT = BaseCppType('at', 'Generator') @@ -56,7 +69,7 @@ typeAndSizeT = BaseCppType('torch::autograd::generated', 'TypeAndSize') tensorGeometryT = BaseCppType('at', 'TensorGeometry') BaseTypeToCppMapping: Dict[BaseTy, BaseCppType] = { - BaseTy.int: intT, + BaseTy.int: longT, BaseTy.float: doubleT, BaseTy.bool: boolT, BaseTy.str: stringT,