def argument(a: Union[Argument, SelfArgument, TensorOptionsArguments], *, is_out: bool) -> List[Binding]: # Ideally, we NEVER default native functions. However, there are a number # of functions that call native:: directly and rely on the defaulting # existing. So for BC, we generate defaults for non-out variants (but not # for out variants, where it is impossible to generate an appropriate # default) should_default = not is_out if isinstance(a, Argument): default: Optional[str] = None if should_default and a.default is not None: default = cpp.default_expr(a.default, a.type) return [ Binding( nctype=argument_type(a, binds=a.name), name=a.name, default=default, argument=a, ) ] elif isinstance(a, SelfArgument): # Erase SelfArgument from the distinction return argument(a.argument, is_out=is_out) elif isinstance(a, TensorOptionsArguments): default = None if should_default: default = '{}' # TODO: Not sure why the arguments assigned here are for # TensorOptionsArguments and not the constituent pieces. It seems # to matter return [ Binding( nctype=NamedCType('dtype', OptionalCType(BaseCType(scalarTypeT))), name='dtype', default=default, argument=a, ), Binding( nctype=NamedCType('layout', OptionalCType(BaseCType(layoutT))), name='layout', default=default, argument=a, ), Binding( nctype=NamedCType('device', OptionalCType(BaseCType(deviceT))), name='device', default=default, argument=a, ), Binding( nctype=NamedCType('pin_memory', OptionalCType(BaseCType(boolT))), name='pin_memory', default=default, argument=a, ) ] else: assert_never(a)
def compute_argument_yaml(a: Argument, *, schema_order: bool, kwarg_only_set: Set[str], out_arg_set: Set[str], name_to_field_name: Dict[str, str]) -> object: arg: Dict[str, object] = { 'annotation': str(a.annotation) if a.annotation else None, 'dynamic_type': dynamic_type(a.type), 'is_nullable': a.type.is_nullable(), 'name': a.name, 'type': cpp.argument_type(a), } if a.default is not None: arg['default'] = pythonify_default(cpp.default_expr(a.default, a.type)) if a.name in kwarg_only_set: arg['kwarg_only'] = True if a.name in out_arg_set: arg['output'] = True arg['allocate'] = True # See Note [name and field_name] if a.name in name_to_field_name: arg['field_name'] = name_to_field_name[a.name] # Historically, booleans don't get their size recorded, because it # is already built into the cpp type (e.g., std::array<bool, 4>) l = a.type.is_list_like() if l is not None and l.size is not None and str(l.elem) != 'bool': arg['size'] = l.size return arg
def argument( a: Union[Argument, ThisArgument, TensorOptionsArguments] ) -> LegacyDispatcherArgument: if isinstance(a, Argument): return LegacyDispatcherArgument( type=argument_type(a), name=a.name, default=cpp.default_expr(a.default, a.type) if a.default is not None else None, argument=a, ) elif isinstance(a, ThisArgument): # Erase ThisArgument from the distinction return LegacyDispatcherArgument( type=argument_type(a.argument), name=a.argument.name, default=None, argument=a.argument, ) elif isinstance(a, TensorOptionsArguments): # TODO: expunge this logic entirely default = None if all(x.default == "None" for x in a.all()): default = '{}' elif a.dtype.default == "long": default = 'at::kLong' # TODO: this is wrong return LegacyDispatcherArgument( type='const TensorOptions &', name='options', default=default, argument=a, ) else: assert_never(a)
def argument(a: Argument) -> PythonArgument: return PythonArgument( name=a.name, type=a.type, # TODO: directly translate a.default to python default default=str(pythonify_default(cpp.default_expr(a.default, a.type))) if a.default is not None else None, default_init=None, )
def argument(a: Union[Argument, SelfArgument, TensorOptionsArguments]) -> List[Binding]: if isinstance(a, Argument): return [Binding( ctype=argument_type(a, binds=a.name), name=a.name, default=cpp.default_expr(a.default, a.type) if a.default is not None else None, argument=a, )] elif isinstance(a, SelfArgument): # Erase SelfArgument from the distinction return argument(a.argument) elif isinstance(a, TensorOptionsArguments): if local.use_c10_dispatcher() in [UseC10Dispatcher.hacky_wrapper_for_legacy_signatures, UseC10Dispatcher.with_codegenerated_unboxing_wrapper]: # TODO: expunge this logic entirely default = None if all(x.default == "None" for x in a.all()): default = '{}' elif a.dtype.default == "long": default = 'at::kLong' # TODO: this is wrong return [Binding( ctype=ConstRefCType(BaseCType('TensorOptions', 'options')), name='options', default=default, argument=a, )] else: assert local.use_c10_dispatcher() == UseC10Dispatcher.full return [ Binding( ctype=OptionalCType(BaseCType('ScalarType', 'dtype')), name='dtype', default='{}', argument=a, ), Binding( ctype=OptionalCType(BaseCType('Layout', 'layout')), name='layout', default='{}', argument=a, ), Binding( ctype=OptionalCType(BaseCType('Device', 'device')), name='device', default='{}', argument=a, ), Binding( ctype=OptionalCType(BaseCType('bool', 'pin_memory')), name='pin_memory', default='{}', argument=a, )] else: assert_never(a)
def argument(cpp_arg: CppArgument) -> PythonArgument: a = cpp_arg.argument if not isinstance(a, Argument): # cpp's TensorOptionsArguments is ignored, we will reintroduce the # scattered fields in tensor_options_args. raise RuntimeError(f'unsupported cpp argument: \'{cpp_arg}\'') return PythonArgument( name=a.name, type=a.type, # TODO: directly translate a.default to python default default=str(pythonify_default(cpp.default_expr(a.default, a.type))) if a.default is not None else None, default_init=None, )
def compute_argument_yaml(a: Argument, *, schema_order: bool, kwarg_only_set: Set[str], out_arg_set: Set[str], name_to_field_name: Dict[str, str]) -> object: arg: Dict[str, object] = { 'annotation': str(a.annotation) if a.annotation else None, 'dynamic_type': dynamic_type(a.type), 'is_nullable': a.type.is_nullable(), 'name': a.name, 'type': cpp.argument_type(a), } if a.default is not None: arg['default'] = pythonify_default(cpp.default_expr(a.default, a.type)) if a.name in kwarg_only_set: arg['kwarg_only'] = True # See Note [Byte-for-byte compatibility] # The default value of kwarg_only is False; this case exists for # byte-for-byte compatibility elif a.name in out_arg_set: arg['kwarg_only'] = False if a.name in out_arg_set: arg['output'] = True # See Note [Byte-for-byte compatibility] # This is probably a bug in the original implementation, where # the specification of allocate was not properly propagated to # the schema-order arguments. In any case, this field # is redundant with the output field if not schema_order: arg['allocate'] = True # See Note [name and field_name] if a.name in name_to_field_name: arg['field_name'] = name_to_field_name[a.name] # Historically, booleans don't get their size recorded, because it # is already built into the cpp type (e.g., std::array<bool, 4>) l = a.type.is_list_like() if l is not None and l.size is not None and str(l.elem) != 'bool': arg['size'] = l.size return arg
def argument(a: Union[Argument, SelfArgument, TensorOptionsArguments], *, is_out: bool) -> List[Binding]: # Ideally, we NEVER default native functions. However, there are a number # of functions that call native:: directly and rely on the defaulting # existing. So for BC, we generate defaults for non-out variants (but not # for out variants, where it is impossible to generate an appropriate # default) should_default = not is_out or local.use_c10_dispatcher( ) is not UseC10Dispatcher.full if isinstance(a, Argument): default: Optional[str] = None if should_default and a.default is not None: default = cpp.default_expr(a.default, a.type) return [ Binding( ctype=argument_type(a, binds=a.name), name=a.name, default=default, argument=a, ) ] elif isinstance(a, SelfArgument): # Erase SelfArgument from the distinction return argument(a.argument, is_out=is_out) elif isinstance(a, TensorOptionsArguments): if local.use_c10_dispatcher() in [ UseC10Dispatcher.hacky_wrapper_for_legacy_signatures, UseC10Dispatcher.with_codegenerated_unboxing_wrapper ]: # TODO: expunge this logic entirely default = None if should_default: if all(x.default == "None" for x in a.all()): default = '{}' elif a.dtype.default == "long": default = 'at::kLong' # TODO: this is wrong return [ Binding( ctype=ConstRefCType(BaseCType('TensorOptions', 'options')), name='options', default=default, argument=a, ) ] else: assert local.use_c10_dispatcher() == UseC10Dispatcher.full default = None if should_default: default = '{}' # TODO: Not sure why the arguments assigned here are for # TensorOptionsArguments and not the constituent pieces. It seems # to matter return [ Binding( ctype=OptionalCType(BaseCType('ScalarType', 'dtype')), name='dtype', default=default, argument=a, ), Binding( ctype=OptionalCType(BaseCType('Layout', 'layout')), name='layout', default=default, argument=a, ), Binding( ctype=OptionalCType(BaseCType('Device', 'device')), name='device', default=default, argument=a, ), Binding( ctype=OptionalCType(BaseCType('bool', 'pin_memory')), name='pin_memory', default=default, argument=a, ) ] else: assert_never(a)
def argument( a: Union[Argument, ThisArgument, TensorOptionsArguments] ) -> Sequence[NativeArgument]: if isinstance(a, Argument): return [ NativeArgument( type=argument_type(a), name=a.name, default=cpp.default_expr(a.default, a.type) if a.default is not None else None, argument=a, ) ] elif isinstance(a, ThisArgument): # Erase ThisArgument from the distinction return [ NativeArgument( type=argument_type(a.argument), name=a.argument.name, default=None, argument=a.argument, ) ] elif isinstance(a, TensorOptionsArguments): if local.use_c10_dispatcher() in [ UseC10Dispatcher.hacky_wrapper_for_legacy_signatures, UseC10Dispatcher.with_codegenerated_unboxing_wrapper ]: # TODO: expunge this logic entirely default = None if all(x.default == "None" for x in a.all()): default = '{}' elif a.dtype.default == "long": default = 'at::kLong' # TODO: this is wrong return [ NativeArgument( type='const TensorOptions &', name='options', default=default, argument=a, ) ] else: assert local.use_c10_dispatcher() == UseC10Dispatcher.full return [ NativeArgument( type='c10::optional<ScalarType>', name='dtype', default='{}', argument=a, ), NativeArgument( type='c10::optional<Layout>', name='layout', default='{}', argument=a, ), NativeArgument( type='c10::optional<Device>', name='device', default='{}', argument=a, ), NativeArgument( type='c10::optional<bool>', name='pin_memory', default='{}', argument=a, ) ] else: assert_never(a)