Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hacky_wrapper from VariableType and TraceType #44005

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 0 additions & 16 deletions tools/autograd/gen_autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,6 @@ def has_tensoroptions_argument(declaration):
return True
return False

def process_schema_order_arg(schema_order_arg):
if schema_order_arg == 'dtype':
return 'optTypeMetaToScalarType(options.dtype_opt())'
elif schema_order_arg == 'layout':
return 'options.layout_opt()'
elif schema_order_arg == 'device':
return 'options.device_opt()'
elif schema_order_arg == 'pin_memory':
return 'options.pinned_memory_opt()'
elif schema_order_arg == 'memory_format':
return 'c10::impl::check_tensor_options_and_extract_memory_format(options, memory_format)'
else:
return schema_order_arg


def load_aten_declarations(path):
with open(path, 'r') as f:
Expand All @@ -151,8 +137,6 @@ def load_aten_declarations(path):
for arg in declaration['schema_order_arguments']]
declaration['args'] = [arg['name'] for arg in declaration['arguments']]
declaration['schema_order_args'] = [arg['name'] for arg in declaration['schema_order_arguments']]
if has_tensoroptions_argument(declaration):
declaration['schema_order_args'] = [process_schema_order_arg(arg) for arg in declaration['schema_order_args']]
declaration['api_name'] = declaration['name']
if declaration.get('overload_name'):
declaration['type_wrapper_name'] = "{}_{}".format(
Expand Down
37 changes: 31 additions & 6 deletions tools/autograd/gen_variable_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,17 @@

WRAPPER_REGISTRATION = CodeTemplate("""\
m.impl("${unqual_operator_name_with_overload}",
c10::impl::hacky_wrapper_for_legacy_signatures<${schema_order_cpp_signature}>(TORCH_FN(${class_type}::${type_wrapper_name}))
TORCH_FN(${class_type}::${type_wrapper_name})
);
""")

UNPACK_TENSOR = CodeTemplate("""\
auto${ref} ${arg_name}_ = unpack${suffix}(${arg_name}, "${arg_name}", ${arg_pos});""")

UNPACK_OPTIONS = CodeTemplate("""\
auto ${arg_name}_ = TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory);""")

LEGACY_UNPACK_OPTIONS = CodeTemplate("""\
auto ${arg_name}_ = TensorOptions(${arg_name});""")

DECLARE_GRAD_FN = CodeTemplate("""\
Expand Down Expand Up @@ -491,8 +494,15 @@ def format_trace_op_name(declaration):


def format_trace_inputs(declaration):
gather_tensor_options = "TensorOptions().dtype(dtype).layout(layout).device(device).pinned_memory(pin_memory)"

def dispatch_trace_input(arg_spec):
name, value, simple_type, nullable = arg_spec
if declaration['use_c10_dispatcher'] == 'full':
if value == "options":
value = gather_tensor_options
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
smessmer marked this conversation as resolved.
Show resolved Hide resolved
# XXX: For arg that have type of Tensor?[], tracer will pass allow_undefined to addInputs
if simple_type == 'TensorList' and nullable:
return '''jit::tracer::addInputs(node, "{}", {}, {});'''.format(name, value, "true")
Expand All @@ -515,7 +525,13 @@ def dispatch_trace_input(arg_spec):
if is_out_overload(declaration):
# for *_out functions, handle the result argument differently for inplace/outplace.
# For inplace: just add the input to the end to confirm with the JIT schema
inplace = ADD_TRACE_INPUT.substitute(name=out_input['name'], input=out_input['name'])
value = out_input['name']
if declaration['use_c10_dispatcher'] == 'full':
if value == "options":
value = gather_tensor_options
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
smessmer marked this conversation as resolved.
Show resolved Hide resolved
inplace = ADD_TRACE_INPUT.substitute(name=out_input['name'], input=value)

# for outplace: do nothing, except if the declaration is a factory.
# Factories are a bit special because their out-of-place overloads
Expand Down Expand Up @@ -681,12 +697,17 @@ def gen_variable_type_shard(out, aten_declarations, template_path, suffix, heade

for declaration in aten_declarations:
formal_types = [arg['type'] for arg in declaration['arguments']]
type_declarations.append(METHOD_DECLARATION.substitute(declaration))
if declaration['use_c10_dispatcher'] == 'full':
formals = declaration['schema_order_formals']
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
smessmer marked this conversation as resolved.
Show resolved Hide resolved
formals = declaration['formals']
type_declarations.append(METHOD_DECLARATION.substitute(declaration, formals=formals))
strategy = dispatch_strategy(declaration)
if declaration['name'] not in MANUAL_AUTOGRAD and strategy == 'use_derived':
body = emit_body(declaration)
type_definitions.append(METHOD_DEFINITION.substitute(
declaration, type_definition_body=body))
declaration, type_definition_body=body, formals=formals))
if declaration['use_c10_dispatcher'] == 'full':
wrapper_registrations.append(WRAPPER_REGISTRATION.substitute(
declaration, class_type='VariableType'))
Expand All @@ -702,7 +723,7 @@ def gen_variable_type_shard(out, aten_declarations, template_path, suffix, heade
if declaration['name'] not in MANUAL_TRACER:
trace_body = emit_trace_body(declaration)
trace_method_definitions.append(METHOD_DEFINITION.substitute(
declaration, type_definition_body=trace_body))
declaration, type_definition_body=trace_body, formals=formals))

if declaration['use_c10_dispatcher'] == 'full':
trace_wrapper_registrations.append(WRAPPER_REGISTRATION.substitute(
Expand Down Expand Up @@ -1224,7 +1245,11 @@ def requires_unpack(arg):
# Okay, we are abusing the definition of 'unpack' here a bit,
# although it's still getting the non-variable from the variable
# (in this case via TensorOptions rather than Variable/Tensor).
body.append(UNPACK_OPTIONS.substitute(arg_name=arg['name']))
if declaration['use_c10_dispatcher'] == 'full':
body.append(UNPACK_OPTIONS.substitute(arg_name=arg['name']))
else:
assert declaration['use_c10_dispatcher'] == 'with_codegenerated_unboxing_wrapper'
body.append(LEGACY_UNPACK_OPTIONS.substitute(arg_name=arg['name']))

unpacked_args.append(arg['name'] + '_')
unpacked_args_simple_type[arg['name'] + '_'] = arg['simple_type']
Expand Down
1 change: 0 additions & 1 deletion tools/autograd/templates/VariableType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <ATen/TypeDefault.h>
#include <torch/library.h>
#include <ATen/core/op_registration/hacky_wrapper_for_legacy_signatures.h>

// ${generated_comment}

Expand Down