# mypy: allow-untyped-defs import warnings from abc import ABC, abstractmethod from collections.abc import Callable, Sequence from dataclasses import dataclass from typing import Any, get_args import torch import torch._library.utils as library_utils import torch.utils._pytree as pytree from torch import Tensor from torch._C import DispatchKey from torch._higher_order_ops.utils import ( _has_gen_schema, call_op, HopInstance, HopSchema, materialize_callable_in_args, register_fake, unique_graph_id, ) from torch._ops import HigherOrderOperator, OperatorBase, OpOverload from torch._prims_common import clone_preserve_strides from torch.fx.experimental.proxy_tensor import ( disable_proxy_modes_tracing, ProxyTorchDispatchMode, track_tensor_tree, ) class SchemaHolder: def __init__(self, schema: torch.FunctionSchema): self.schema = schema def __eq__(self, other): return self.schema == other.schema def __hash__(self) -> int: return hash(self.schema) @classmethod def from_tree_spec(cls, tree_spec: pytree.TreeSpec): if tree_spec is None: raise AssertionError("tree_spec cannot be None") return cls(pytree.tree_unflatten([], tree_spec).schema) # register_constant allows us to get a tree_spec from pytree.tree_flatten(SchemaHolder(FunctionSchema)). # The tree_spec is proxable in the graph and we can get back the schema via # schema = pytree.tree_unflatten([], tree_spec).schema pytree.register_constant(SchemaHolder) def get_base(tensor): if torch.is_inference_mode_enabled(): return tensor._inference_mode_base else: return tensor._base class ViewInfo(ABC): base_index: int def __init__(self, base_index): self.base_index = base_index @abstractmethod def regenerate_view(self, bases_list: list[Tensor]): pass @dataclass class AsStridedViewInfo(ViewInfo): size: Sequence[int | torch.SymInt] stride: Sequence[int | torch.SymInt] storage_offset: int def __init__(self, base_index, size, stride, storage_offset): super().__init__(base_index) self.size = size self.stride = stride self.storage_offset = storage_offset def regenerate_view(self, bases_list: list[Tensor]): return torch.as_strided( bases_list[self.base_index], self.size, self.stride, self.storage_offset, ) @dataclass class SliceViewInfo(ViewInfo): dim: int | torch.SymInt start: int | torch.SymInt end: int | torch.SymInt def __init__(self, base_index, dim, start, end): super().__init__(base_index) self.dim = dim self.start = start self.end = end def regenerate_view(self, bases_list: list[Tensor]): return torch.ops.aten.slice.Tensor( bases_list[self.base_index], self.dim, self.start, self.end ) @dataclass class AliasViewInfo(ViewInfo): def __init__(self, base_index): super().__init__(base_index) def regenerate_view(self, bases_list: list[Tensor]): return torch.ops.aten.alias.default(bases_list[self.base_index]) @dataclass class NotView(ViewInfo): def __init__(self, base_index): super().__init__(base_index) def regenerate_view(self, bases_list: list[Tensor]): return bases_list[self.base_index] def is_alias(base, tensor): from torch.fx.experimental.symbolic_shapes import statically_known_true, sym_eq return all( statically_known_true(a) for a in [ sym_eq(base.storage_offset(), tensor.storage_offset()), sym_eq(base.stride(), tensor.stride()), sym_eq(base.size(), tensor.size()), ] ) # return None or (dim, start, end) def try_use_slice(base, tensor): from torch.fx.experimental.symbolic_shapes import statically_known_true, sym_eq # This condition should never be triggered. if is_alias(base, tensor): return (0, 0, base.size()[0]) # TODO is there cases can we use slice even if stride or len(sizes) are not equal? if not statically_known_true(sym_eq(tensor.stride(), base.stride())): return None if not statically_known_true(sym_eq(len(tensor.size()), len(base.size()))): return None dim = None count = 0 for i in range(len(tensor.size())): if base.size()[i] != tensor.size()[i]: dim = i count = count + 1 if count != 1: return None if tensor.storage_offset() % tensor.stride()[dim] != 0: return None start = tensor.storage_offset() // tensor.stride()[dim] end = start + tensor.size()[dim] return (dim, start, end) def write_view_information_to_args( mutable_arg_names: list[str], mutable_arg_types: list[torch.Type], kwargs: dict[str, Any], arg_to_base_index: dict[str, Any], ): """ This function writes the view information into kwargs. It reads mutable_args from kwargs. and uses arg_to_base_index and tensor information to write ViewInfo into kwargs. mutable_arg_names: mutable custom operator arg names. mutable_arg_types: mutable custom operator arg types. kwargs: the original custom operator args. arg_to_base_index: maps mutable_arg_name to int | [int] that refers to the base tensor that corresponds to the input tensor """ def write_single_view(prefix: str, tensor: Tensor, base_index: int): if f"{prefix}_base_index" in kwargs: raise AssertionError(f"{prefix}_base_index already in kwargs") if f"{prefix}_size" in kwargs: raise AssertionError(f"{prefix}_size already in kwargs") if f"{prefix}_stride" in kwargs: raise AssertionError(f"{prefix}_stride already in kwargs") if f"{prefix}_storage_offset" in kwargs: raise AssertionError(f"{prefix}_storage_offset already in kwargs") if f"{prefix}_slice_dim" in kwargs: raise AssertionError(f"{prefix}_slice_dim already in kwargs") if f"{prefix}_slice_start" in kwargs: raise AssertionError(f"{prefix}_slice_start already in kwargs") if f"{prefix}_slice_end" in kwargs: raise AssertionError(f"{prefix}_slice_end already in kwargs") def use_as_strided(tensor): kwargs[f"{prefix}_size"] = tensor.size() kwargs[f"{prefix}_stride"] = tensor.stride() kwargs[f"{prefix}_storage_offset"] = tensor.storage_offset() def use_slice(dim, start, end): kwargs[f"{prefix}_slice_dim"] = dim kwargs[f"{prefix}_slice_start"] = start kwargs[f"{prefix}_slice_end"] = end def use_alias(): kwargs[f"{prefix}_alias"] = True # The start if the function if tensor is None: kwargs[f"{prefix}_base_index"] = None else: base = get_base(tensor) kwargs[f"{prefix}_base_index"] = base_index if base is None: # no need to add anything else other than _base_index return elif is_alias(base, tensor): use_alias() elif (slice_info := try_use_slice(base, tensor)) is not None: use_slice(*slice_info) else: use_as_strided(tensor) for arg_name, arg_type in zip(mutable_arg_names, mutable_arg_types): arg = kwargs[arg_name] if library_utils.is_tensorlist_like_type(arg_type): if arg is None: kwargs[f"_{arg_name}_length"] = None else: kwargs[f"_{arg_name}_length"] = len(arg) for i, elem in enumerate(arg): write_single_view( f"_{arg_name}_{i}", elem, arg_to_base_index[arg_name][i] ) elif library_utils.is_tensor_like_type(arg_type): write_single_view( f"_{arg_name}", kwargs[arg_name], arg_to_base_index.get(arg_name), # type: ignore[arg-type] ) else: raise RuntimeError(f"Unsupported type {arg_type}") # Returns a dict of arg_name -> ViewInfo | [ViewInfo] def read_view_information_from_args( mutable_arg_names: list[str], mutable_arg_types: list[torch.Type], kwargs: dict[str, Any], all_bases: list[Tensor], ): """ This reads the view information added by `write_view_information_to_args` from kwargs, pop them, and returns a dict arg_name -> ViewInfo | [ViewInfo](if the input is list). that maps each mutable arg to its view information. mutable_arg_names: mutable custom operator arg names. mutable_arg_types: mutable custom operator arg types. kwargs : args of auto_functionalize(custom_op, kwargs) """ def get_arg(name): return kwargs.pop(name) def read_single_view(prefix): base_index = get_arg(f"{prefix}_base_index") if base_index is None: return None elif f"{prefix}_alias" in kwargs: get_arg(f"{prefix}_alias") return AliasViewInfo(base_index) elif f"{prefix}_storage_offset" in kwargs: # The view is regenerated using as_strided. size = get_arg(f"{prefix}_size") stride = get_arg(f"{prefix}_stride") storage_offset = get_arg(f"{prefix}_storage_offset") return AsStridedViewInfo(base_index, size, stride, storage_offset) elif f"{prefix}_slice_dim" in kwargs: dim = get_arg(f"{prefix}_slice_dim") start = get_arg(f"{prefix}_slice_start") end = get_arg(f"{prefix}_slice_end") return SliceViewInfo(base_index, dim, start, end) else: # This means that the argument is the base tensor return NotView(base_index) args_view_info: dict[str, Any] = {} for arg_name, arg_type in zip(mutable_arg_names, mutable_arg_types): if library_utils.is_tensorlist_like_type(arg_type): length = get_arg(f"_{arg_name}_length") if length is None: # The whole list is None. args_view_info[arg_name] = None else: args_view_info[arg_name] = [ read_single_view(f"_{arg_name}_{i}") for i in range(length) ] elif library_utils.is_tensor_like_type(arg_type): args_view_info[arg_name] = read_single_view(f"_{arg_name}") else: raise RuntimeError(f"Unsupported type {arg_type}") return args_view_info # NOTE: [auto-functionalizing custom ops] # Users may wish to torch.compile custom ops that mutate their inputs. # torch.compile will automatically support this op without anyone needing # to provide a functionalization kernel for it. Here's how. # # Let's say we have a hypothetical mylib::sin_(Tensor(a!) x) -> () # op. First, when FakeTensor sees this op: # - If the schema says it returns nothing, we can generate a trivial # FakeTensor rule for it (that returns nothing). # - Otherwise, the user needs to provide a FakeTensor impl (fake impl) # # Next, when Python FunctionalTensor sees the op, it will functionalize # it by emitting a call to an auto_functionalize(op, ["x"], {"x": ...}) # HOP and replacing the mutated inputs with corresponding outputs of this HOP. # This HOP effectively runs the functional version of the op when # called: it clones inputs that will be mutated, runs the op, and # then returns (output, Tensors with the new values) # # auto_functionalize_v2 is an improved version of auto_functionalize that better handle # re-inplacing views. class AutoFunctionalized(HigherOrderOperator): """auto_functionalized(_mutable_op, **kwargs) This HOP runs a "functional" version of _mutable_op. Concretely, it looks at all the arguments that are mutable through _mutable_op's operator schema, clones those kwargs, runs `out = _mutable_op(**kwargs)` with the cloned values, and then returns the operator output concatenated with the cloned values that were mutated. We have some restrictions on `_mutable_op`. See `can_auto_functionalize` for the restrictions. We can likely lift many of these if users request it. The reason why _mutable_op is prefixed with an underscore is to prevent collisions with kwarg names in **kwargs. """ def __init__(self) -> None: super().__init__("auto_functionalized", cacheable=True) def __call__( self, /, _mutable_op: OpOverload, **kwargs: Any, ) -> tuple[Any, tuple[Tensor, ...]]: if not can_auto_functionalize(_mutable_op): raise AssertionError(f"Cannot auto-functionalize op {_mutable_op}") if not isinstance(kwargs, dict): raise AssertionError(f"kwargs must be a dict, got {type(kwargs)}") # pyrefly: ignore [missing-attribute] return super().__call__(_mutable_op, **kwargs) auto_functionalized = AutoFunctionalized() auto_functionalized.__module__ = "torch.ops.higher_order" auto_functionalized.fallthrough(DispatchKey.AutogradCPU) auto_functionalized.fallthrough(DispatchKey.AutogradCUDA) _MutableOpType = OpOverload | HigherOrderOperator class AutoFunctionalizedV2(HigherOrderOperator): """auto_functionalized_v2(_mutable_op, **kwargs) This HOP runs a "functional" version of _mutable_op. Unlike AutoFunctionalized, this version is improved to better handle view tensors. This version is only used in non export mode. """ def __init__(self) -> None: super().__init__("auto_functionalized_v2", cacheable=True) def __call__( self, /, _mutable_op: _MutableOpType, **kwargs: Any, ) -> tuple[Any, tuple[Tensor, ...]]: _op_to_check: OpOverload | HopInstance | None = None if isinstance(_mutable_op, HigherOrderOperator): _op_to_check = HopInstance( _mutable_op, SchemaHolder.from_tree_spec(kwargs.get("_op_schema")).schema, # type: ignore[arg-type] ) else: _op_to_check = _mutable_op if _op_to_check is None: raise AssertionError("_op_to_check cannot be None") if not can_auto_functionalize(_op_to_check): raise AssertionError(f"Cannot auto-functionalize op {_op_to_check}") if not isinstance(kwargs, dict): raise AssertionError(f"kwargs must be a dict, got {type(kwargs)}") # pyrefly: ignore [missing-attribute] return super().__call__(_mutable_op, **kwargs) auto_functionalized_v2 = AutoFunctionalizedV2() auto_functionalized_v2.__module__ = "torch.ops.higher_order" auto_functionalized_v2.fallthrough(DispatchKey.AutogradCPU) auto_functionalized_v2.fallthrough(DispatchKey.AutogradCUDA) def can_auto_functionalize( op: OperatorBase | HopInstance, ) -> bool: if isinstance(op, HopInstance): # HOPs that implement gen_schema and schema is not functional are auto_functionalizable. if not _has_gen_schema(op._op): return False else: if not isinstance(op, OpOverload): return False if torch._library.utils.is_builtin(op): # We control the built-ins. These may (in rare cases) # do input metadata mutation (which we have banned on custom ops) return False schema = op._schema if not schema.is_mutable: return False schema = op._schema for arg in schema.arguments: if arg.alias_info is None: continue if not arg.alias_info.is_write: continue if torch._library.utils.is_tensor_like_type(arg.type): continue if torch._library.utils.is_tensorlist_like_type(arg.type): continue return False if len(schema.returns) == 1 and isinstance(schema.returns[0].type, torch.NoneType): # Skip schema returns -> None return True if isinstance(op, OpOverload): if torch._library.utils.is_out(op): # Out ops have aliased returns (returns alias the mutable args). # This is fine because the mutable args are write-only output buffers. pass elif torch._library.utils.is_inplace(op): # Inplace ops have aliased returns (return aliases the first mutable arg). pass else: # The returns of OpOverload must not alias anything for ret in schema.returns: if ret.alias_info is None and type(ret.type) is torch.TensorType: continue # Not yet supported: List[Tensor] return. return False if torch._C._dispatch_has_kernel_for_dispatch_key(op.name(), "Functionalize"): return False return True def get_mutable_args_from_schema( schema: torch.FunctionSchema, ) -> tuple[list[str], list[torch.Type]]: """ Returns the list of argument names that get mutated according to the schema and their types. """ mutable_args_names = [ arg.name for arg in schema.arguments if arg.alias_info is not None and arg.alias_info.is_write ] mutable_args_types = [ arg.type for arg in schema.arguments if arg.alias_info is not None and arg.alias_info.is_write ] return mutable_args_names, mutable_args_types # type: ignore[return-value] def get_mutable_args(op: OpOverload) -> tuple[list[str], list[torch.Type]]: return get_mutable_args_from_schema(op._schema) def do_auto_functionalize( mode: "torch._subclasses.functional_tensor.FunctionalTensorMode", op: OpOverload, args: tuple[Any, ...], kwargs: dict[str, Any], ) -> Any: """Functionalizes a call to op(*args, **kwargs) by emitting a call to `outs = auto_functionalized(op, normalized_kwargs)` and replacing the mutated (args, kwargs) with the corresponding outputs. The normalized_kwargs are just the (args, kwargs), but all in kwarg form. This makes handling easier for the auto_functionalized HOP. """ from torch._subclasses.functional_tensor import PythonFunctionalizeAPI ctx = PythonFunctionalizeAPI(mode=mode) # All of the (args, kwargs), but all as kwargs. The names for the # args come from the schema. This makes it easier for us to work with them. normalized_kwargs = {} schema = op._schema for idx, arg in enumerate(schema.arguments): # NB: torch_dispatch kwargs are the args defined as kwarg-only in the schema if arg.name in kwargs: normalized_kwargs[arg.name] = kwargs[arg.name] elif idx < len(args): # if its out of bounds we don't need to do anything # as it means the optional arg was passed with its default # value normalized_kwargs[arg.name] = args[idx] else: normalized_kwargs[arg.name] = arg.default_value unwrapped_kwargs = ctx.unwrap_tensors(normalized_kwargs) # type: ignore[arg-type] if "self" in unwrapped_kwargs or "self_" in unwrapped_kwargs: warnings.warn( "Using `self` or `self_` as an argument in the definition of custom ops may lead to ambiguous parsing. " "Please consider using a different name for this argument to avoid potential issues.", stacklevel=2, ) with ctx.redispatch_to_next(): unwrapped_outs = auto_functionalized( op, **unwrapped_kwargs, # type: ignore[arg-type] ) # List of the name of args that get mutated (according to the schema) mutable_args_names, _ = get_mutable_args(op) unwrapped_actual_out: Any | tuple[Any] = unwrapped_outs[: -len(mutable_args_names)] unwrapped_mutable_out = unwrapped_outs[-len(mutable_args_names) :] if len(op._schema.returns) == 0: if unwrapped_actual_out[0] is not None: raise AssertionError( f"Expected None for op with no returns, got {unwrapped_actual_out[0]}" ) unwrapped_actual_out = None elif len(op._schema.returns) == 1: if len(unwrapped_actual_out) != 1: raise AssertionError(f"Expected 1 output, got {len(unwrapped_actual_out)}") unwrapped_actual_out = unwrapped_actual_out[0] else: if len(unwrapped_actual_out) != len(op._schema.returns): raise AssertionError( f"Expected {len(op._schema.returns)} outputs, got {len(unwrapped_actual_out)}" ) for name, unwrapped_out in zip(mutable_args_names, unwrapped_mutable_out): # Can be None if input was `Tensor(a!)?` if unwrapped_out is None: continue # We only handle Tensor or List[Tensor] here for now. def sync_update(o, orig_arg): ctx.replace(orig_arg, o) ctx.commit_update(orig_arg) ctx.sync(orig_arg) orig_arg = normalized_kwargs[name] if isinstance(unwrapped_out, torch.Tensor): sync_update(unwrapped_out, orig_arg) elif isinstance(unwrapped_out, list) and all( isinstance(o, torch.Tensor) for o in unwrapped_out ): if len(orig_arg) != len(unwrapped_out): raise AssertionError( f"orig_arg length ({len(orig_arg)}) != unwrapped_out length ({len(unwrapped_out)})" ) for orig_a, o in zip(orig_arg, unwrapped_out): sync_update(o, orig_a) else: raise RuntimeError( f"unsupported type for auto-functionalization: {unwrapped_out}" ) return ctx.wrap_tensors(unwrapped_actual_out) # type: ignore[arg-type] # Wrapper for GraphModule that applies functionalization during execution to enable # epilogue graph inlining and better fusion opportunities in subgraphs # When tracing this wrapper, we'll get a graph module with epilogue. # # We want to hash it according to the original graph module, so that when we go # from Functional mode -> fake mode for multiple invoke_subgraph calls that share, # the same inner graph module, we can hit the cache. class FunctionalCallableWithEpilogue: def __init__(self, orig_callable: Callable): self.orig_callable = orig_callable # Propagate so callers pass inputs as a list, enabling input deallocation. self._boxed_call = getattr(orig_callable, "_boxed_call", False) def __call__(self, *args, **kwargs): # Functionalize to inline the epilogue graph (copy_ ops) for better fusion. # Python functionalization so mutable custom ops reach auto_functionalized_v2. from torch._subclasses.functional_tensor import ( dispatch_functionalize, FunctionalTensorMode, ) functionalized = dispatch_functionalize( self.orig_callable, FunctionalTensorMode(), propagate_input_mutations=True, ) if self._boxed_call: # Not all callers respect _boxed_call (e.g. reenter_make_fx # always calls f(*args)). Detect which convention was used. if len(args) == 1 and isinstance(args[0], list): return tuple(functionalized(args[0])) return tuple(functionalized(list(args))) return functionalized(*args, **kwargs) def __hash__(self): return id(self.orig_callable) def do_auto_functionalize_v2( mode: "torch._subclasses.functional_tensor.FunctionalTensorMode", op: OpOverload | HopInstance, args: tuple[Any, ...], kwargs: dict[str, Any], ) -> Any: from torch._subclasses.functional_tensor import PythonFunctionalizeAPI ctx = PythonFunctionalizeAPI(mode=mode) # All of the (args, kwargs), but all as kwargs. The names for the # args come from the schema. This makes it easier for us to work with them. normalized_kwargs = {} schema = op._schema # pyrefly: ignore [bad-assignment] op = op._op if isinstance(op, HopInstance) else op if not isinstance(op, get_args(_MutableOpType)): raise AssertionError(f"Expected _MutableOpType, got {type(op)}") subgraph_arg_names = { arg_info.name for arg_info in schema.arguments if isinstance(arg_info.type, torch._C.AnyType) } def _maybe_functionalize(name: str, arg: Any) -> Any: if name in subgraph_arg_names and callable(arg): return FunctionalCallableWithEpilogue(arg) return arg args = tuple( _maybe_functionalize(schema.arguments[i].name, a) if i < len(schema.arguments) else a for i, a in enumerate(args) ) kwargs = {k: _maybe_functionalize(k, v) for k, v in kwargs.items()} for idx, arg in enumerate(schema.arguments): # NB: torch_dispatch kwargs are the args defined as kwarg-only in the schema if arg.name in kwargs: normalized_kwargs[arg.name] = kwargs[arg.name] elif idx < len(args): # if its out of bounds we don't need to do anything # as it means the optional arg was passed with its default # value normalized_kwargs[arg.name] = args[idx] else: normalized_kwargs[arg.name] = arg.default_value if isinstance(op, OpOverload) and torch._library.utils.is_out(op): return _do_auto_functionalize_v2_for_out_operator( ctx, op, schema, normalized_kwargs ) if isinstance(op, OpOverload) and torch._library.utils.is_inplace(op): return _do_auto_functionalize_v2_for_inplace_operator( ctx, op, schema, normalized_kwargs ) return _do_auto_functionalize_v2_for_generic_mutable_operator( ctx, op, schema, normalized_kwargs ) def _do_auto_functionalize_v2_for_out_operator( ctx: Any, op: OpOverload, schema: Any, normalized_kwargs: dict[str, Any], ) -> Any: """Handle functionalization for out= operators. Out= operators have write-only mutable args. These are not inputs to auto_functionalized_v2; we encode their tensor properties so the dense impl can create empty tensors. """ mutable_args_names, _ = get_mutable_args_from_schema(schema) # Save references to the original FunctionalTensors for post-call sync out_arg_originals = [normalized_kwargs[name] for name in mutable_args_names] # Encode tensor properties and remove the actual out tensors from kwargs for arg_name in mutable_args_names: arg = normalized_kwargs[arg_name] normalized_kwargs[f"_{arg_name}_size"] = arg.size() normalized_kwargs[f"_{arg_name}_stride"] = arg.stride() normalized_kwargs[f"_{arg_name}_dtype"] = arg.dtype normalized_kwargs[f"_{arg_name}_device"] = arg.device del normalized_kwargs[arg_name] unwrapped_kwargs = ctx.unwrap_tensors(normalized_kwargs) # type: ignore[arg-type] auto_func_kwargs = dict(unwrapped_kwargs, _all_bases=[]) with ctx.redispatch_to_next(): unwrapped_outs = auto_functionalized_v2( op, **auto_func_kwargs, # type: ignore[arg-type] ) # For out= ops, the functional HOP returns exactly the out tensors. # auto_functionalized_v2 returns a bare tensor for single return, # tuple for multiple. results = unwrapped_outs if isinstance(unwrapped_outs, tuple) else (unwrapped_outs,) for orig_arg, result in zip(out_arg_originals, results, strict=True): ctx.replace(orig_arg, result) ctx.commit_update(orig_arg) ctx.sync(orig_arg) return ctx.wrap_tensors(unwrapped_outs) # type: ignore[arg-type] def _do_auto_functionalize_v2_for_inplace_operator( ctx: Any, op: OpOverload, schema: Any, normalized_kwargs: dict[str, Any], ) -> Any: """Handle functionalization for inplace operators. Inplace operators mutate their first positional argument (self) and return it. Unlike out= operators (write-only), self is read by the operator, so it must be passed as a base for clone analysis during reinplacing. """ mutable_args_names, mutable_args_types = get_mutable_args_from_schema(schema) self_arg_name = mutable_args_names[0] self_arg = normalized_kwargs[self_arg_name] # self is both read and written; pass its base for clone analysis base = self_arg if get_base(self_arg) is None else get_base(self_arg) arg_to_base_index: dict[str, Any] = {self_arg_name: 0} write_view_information_to_args( mutable_args_names, mutable_args_types, normalized_kwargs, arg_to_base_index ) del normalized_kwargs[self_arg_name] unwrapped_kwargs = ctx.unwrap_tensors(normalized_kwargs) # type: ignore[arg-type] all_bases_unwrapped = ctx.unwrap_tensors([base]) auto_func_kwargs = dict(unwrapped_kwargs, _all_bases=all_bases_unwrapped) with ctx.redispatch_to_next(): unwrapped_outs = auto_functionalized_v2( op, **auto_func_kwargs, # type: ignore[arg-type] ) # HOP returns (op_result, mutated_self_base). # op_result aliases mutated_self_base (inplace semantics). unwrapped_result = unwrapped_outs[0] unwrapped_mutated_base = unwrapped_outs[1] ctx.replace(base, unwrapped_mutated_base) ctx.commit_update(base) ctx.sync(base) return ctx.wrap_tensors(unwrapped_result) # type: ignore[arg-type] def _do_auto_functionalize_v2_for_generic_mutable_operator( ctx: Any, op: Any, schema: Any, normalized_kwargs: dict[str, Any], ) -> Any: """Handle functionalization for generic mutable operators via the auto_functionalized_v2 HOP.""" mutable_args_names, mutable_args_types = get_mutable_args_from_schema(schema) # A list of all bases of mutable args without duplication all_bases: list[Any] = [] all_bases_addresses: list[int] = [] # Map arg_name to the index of its base in all_bases. arg_to_base_index: dict[str, Any] = {} def update_dict(tensor, arg_name, index=None): base = tensor if get_base(tensor) is None else get_base(tensor) def set_result(base_index): if index is None: arg_to_base_index[arg_name] = base_index else: arg_to_base_index[arg_name][index] = base_index if not all_bases_addresses.__contains__(base._cdata): all_bases_addresses.append(base._cdata) all_bases.append(base) set_result(len(all_bases) - 1) else: set_result(all_bases_addresses.index(base._cdata)) for arg_name in mutable_args_names: arg = normalized_kwargs[arg_name] if arg is None: continue if isinstance(arg, list): arg_to_base_index[arg_name] = {} for i, tensor in enumerate(arg): if tensor is None: arg_to_base_index[arg_name].append(None) continue update_dict(tensor, arg_name, i) else: update_dict(arg, arg_name) # add view_meta for each args into unwrapped_kwargs. write_view_information_to_args( mutable_args_names, mutable_args_types, normalized_kwargs, arg_to_base_index, ) # remove mutated args from the kwargs (its a function of _all_bases now) for arg_name in mutable_args_names: del normalized_kwargs[arg_name] # type: ignore[arg-type] unwrapped_kwargs = ctx.unwrap_tensors(normalized_kwargs) # type: ignore[arg-type] if "self" in unwrapped_kwargs or "self_" in unwrapped_kwargs: warnings.warn( "Using `self` or `self_` as an argument in the definition of custom ops may lead to ambiguous parsing. " "Please consider using a different name for this argument to avoid potential issues.", stacklevel=2, ) all_basis_unwrapped = ctx.unwrap_tensors(all_bases) if "_all_bases" in unwrapped_kwargs: raise AssertionError(f"_all_bases already in unwrapped_kwargs for {op}") auto_func_kwargs = dict(unwrapped_kwargs, _all_bases=all_basis_unwrapped) if isinstance(op, HigherOrderOperator): if "_ops_schema" in unwrapped_kwargs: raise AssertionError(f"_ops_schema already in unwrapped_kwargs for {op}") # We pass in the tree_spec of tree_flatten(SchemaHolder) to make it proxable auto_func_kwargs.update( {"_op_schema": pytree.tree_flatten(SchemaHolder(schema))[1]} ) with ctx.redispatch_to_next(): unwrapped_outs = auto_functionalized_v2( op, **auto_func_kwargs, # type: ignore[arg-type] ) unwrapped_actual_out: Any | tuple[Any] = ( unwrapped_outs if len(all_bases) == 0 else unwrapped_outs[: -len(all_bases)] ) unwrapped_mutable_out = ( [] if len(all_bases) == 0 else unwrapped_outs[-len(all_bases) :] ) if isinstance(op, HigherOrderOperator): if len(schema.returns) <= 0: raise AssertionError( f"hop is expected to return at least one output {schema}." ) if len(unwrapped_actual_out) != len(schema.returns): raise AssertionError( f"Expected {len(schema.returns)} outputs, got {len(unwrapped_actual_out)}" ) else: if len(schema.returns) == 0: if unwrapped_actual_out[0] is not None: raise AssertionError( f"Expected None for op with no returns, got {unwrapped_actual_out[0]}" ) unwrapped_actual_out = None elif len(schema.returns) == 1: if len(unwrapped_actual_out) != 1: raise AssertionError( f"Expected 1 output, got {len(unwrapped_actual_out)}" ) unwrapped_actual_out = unwrapped_actual_out[0] else: if len(unwrapped_actual_out) != len(schema.returns): raise AssertionError( f"Expected {len(schema.returns)} outputs, got {len(unwrapped_actual_out)}" ) for orig_arg, unwrapped_out in zip(all_bases, unwrapped_mutable_out): # Can be None if input was `Tensor(a!)?` if unwrapped_out is None: continue # We only handle Tensor or List[Tensor] here for now. def sync_update(o, orig_arg): ctx.replace(orig_arg, o) ctx.commit_update(orig_arg) ctx.sync(orig_arg) if isinstance(unwrapped_out, torch.Tensor): sync_update(unwrapped_out, orig_arg) elif isinstance(unwrapped_out, list) and all( isinstance(o, torch.Tensor) for o in unwrapped_out ): if len(orig_arg) != len(unwrapped_out): raise AssertionError( f"orig_arg length ({len(orig_arg)}) != unwrapped_out length ({len(unwrapped_out)})" ) for orig_a, o in zip(orig_arg, unwrapped_out): sync_update(o, orig_a) else: raise RuntimeError( f"unsupported type for auto-functionalization: {unwrapped_out}" ) return ctx.wrap_tensors(unwrapped_actual_out) # type: ignore[arg-type] # auto_functionalize functions @auto_functionalized.py_impl(DispatchKey.CompositeExplicitAutograd) def auto_functionalized_dense( _mutable_op: OpOverload, _only_clone_these_tensors: tuple[str, ...] | None = None, **kwargs: Any, ) -> tuple[Any, tuple[Tensor, ...]]: new_kwargs = dict(**kwargs) result = [] _mutable_args_names, _ = get_mutable_args(_mutable_op) for name in _mutable_args_names: if ( _only_clone_these_tensors is not None and name not in _only_clone_these_tensors ): new_kwargs[name] = kwargs[name] else: new_kwargs[name] = ( [clone_preserve_strides(x) for x in kwargs[name]] if kwargs[name] is not None and isinstance(kwargs[name], list) else ( clone_preserve_strides(kwargs[name]) if kwargs[name] is not None else None ) ) result.append(new_kwargs[name]) out = _mutable_op(**new_kwargs) if isinstance(out, tuple): return (*out, *result) # type: ignore[return-value] else: return (out, *result) # type: ignore[return-value] @register_fake(auto_functionalized, skip_cache=True) def auto_functionalized_fake( _mutable_op: OpOverload, **kwargs: Any, ) -> tuple[Any, tuple[Tensor, ...]]: result = auto_functionalized_dense( _mutable_op, _only_clone_these_tensors=None, **kwargs ) return result @auto_functionalized.py_impl(ProxyTorchDispatchMode) def auto_functionalized_proxy( mode, _mutable_op: OpOverload, **kwargs: Any, ) -> tuple[Any, tuple[Tensor, ...]]: with disable_proxy_modes_tracing(): out = auto_functionalized(_mutable_op, **kwargs) proxy_kwargs = pytree.tree_map(mode.tracer.unwrap_proxy, kwargs) out_proxy = mode.tracer.create_proxy( "call_function", auto_functionalized, (_mutable_op,), proxy_kwargs, ) result = track_tensor_tree(out, out_proxy, constant=None, tracer=mode.tracer) return result @auto_functionalized.py_functionalize_impl def auto_functionalized_func(ctx, _mutable_op, **kwargs): unwrapped_kwargs = ctx.unwrap_tensors(kwargs) with ctx.redispatch_to_next(): result = auto_functionalized(_mutable_op, **unwrapped_kwargs) return ctx.wrap_tensors(result) # auto_functionalized_v2 functions @auto_functionalized_v2.py_impl(DispatchKey.CompositeExplicitAutograd) def auto_functionalized_v2_dense( _mutable_op: _MutableOpType, _only_clone_these_bases: tuple[int, ...] | None = None, **kwargs: Any, ) -> tuple[Any, tuple[Tensor, ...]]: _all_bases: list[Tensor] = kwargs.pop("_all_bases", []) if _only_clone_these_bases is None: _only_clone_these_bases = tuple(range(len(_all_bases))) if isinstance(_mutable_op, OpOverload): schema: torch._C.FunctionSchema = _mutable_op._schema else: schema = pytree.tree_unflatten([], kwargs.pop("_op_schema")).schema if isinstance(_mutable_op, OpOverload): _callable_op: HopInstance | OpOverload = _mutable_op else: if not isinstance(schema, HopSchema): raise AssertionError(f"Expected HopSchema, got {type(schema)}") _callable_op = HopInstance(_mutable_op, schema) _is_out = isinstance(_mutable_op, OpOverload) and torch._library.utils.is_out( _mutable_op ) op_kwargs_new, all_bases_new = _generate_new_op_kwargs_from_bases( schema, kwargs, _all_bases, _only_clone_these_bases, _is_out, ) out = call_op( _callable_op, tuple(), op_kwargs_new, ) if _is_out: return out # type: ignore[return-value] if isinstance(out, tuple): return (*out, *all_bases_new) # type: ignore[return-value] else: return (out, *all_bases_new) # type: ignore[return-value] def _generate_new_op_kwargs_from_bases( schema, kwargs, all_bases, _only_clone_these_bases, _is_out ): mutable_args_names, mutable_args_types = get_mutable_args_from_schema(schema) if _is_out: # For out= ops, _all_bases is empty. Create empty tensors from the # metadata that was encoded by _do_auto_functionalize_v2_for_out_operator. new_kwargs = dict(**kwargs) created_out_tensors = [] for arg_name in mutable_args_names: size = new_kwargs.pop(f"_{arg_name}_size") stride = new_kwargs.pop(f"_{arg_name}_stride") dtype = new_kwargs.pop(f"_{arg_name}_dtype") device = new_kwargs.pop(f"_{arg_name}_device") t = torch.empty_strided(size, stride, dtype=dtype, device=device) new_kwargs[arg_name] = t created_out_tensors.append(t) return new_kwargs, created_out_tensors args_view_info = read_view_information_from_args( mutable_args_names, mutable_args_types, kwargs, all_bases ) def maybe_copy(i, t): if t is None: return None if i in _only_clone_these_bases: return clone_preserve_strides(t) else: return t all_bases_new = [maybe_copy(i, t) for i, t in enumerate(all_bases)] # create new args new_kwargs = dict(**kwargs) # re-generate all inputs from all_bases_new using args_view_info and add them to new_kwargs. for arg_name in mutable_args_names: if args_view_info[arg_name] is None: new_kwargs[arg_name] = None elif isinstance(args_view_info[arg_name], list): new_kwargs[arg_name] = [] for i, elem in enumerate(args_view_info[arg_name]): if elem is None: new_kwargs[arg_name].append(None) else: view_info = args_view_info[arg_name][i] new_kwargs[arg_name].append( view_info.regenerate_view(all_bases_new) ) else: new_kwargs[arg_name] = args_view_info[arg_name].regenerate_view( all_bases_new ) return new_kwargs, all_bases_new @register_fake(auto_functionalized_v2, skip_cache=True) def auto_functionalized_v2_fake( _mutable_op: _MutableOpType, **kwargs: dict[str, Any], ) -> tuple[Any, tuple[Tensor, ...]]: result = auto_functionalized_v2_dense( _mutable_op, _only_clone_these_bases=None, **kwargs ) return result @auto_functionalized_v2.py_impl(ProxyTorchDispatchMode) def auto_functionalized_v2_proxy( mode, _mutable_op: _MutableOpType, **kwargs: Any, ) -> tuple[Any, tuple[Tensor, ...]]: if isinstance(_mutable_op, HigherOrderOperator): # Note [materialize callable inputs as graph] # Below code materializes the callable inputs to the hop as graph modules. # kwargs may contain general callables, that are not proxable e.g. FunctionWithNoFreeVars # this could happen when we auto_functionalize the backward of the hop, # where backward fn is a callable that wraps forward graph module. # This function materialize the callable args according to the schema of the hop. # We cannot materialize the callables in kwargs directly because the inputs to callable # vary from hops to hop. To make the materialiation process generic to all hops, # we trace a function that wraps the hop and let each hop itself figure out how to trace # its callable inputs. Then we look at the schema of the traced hop node and replace the # callable in original kwarg with the traced subgraphs. # # Specifically, we first trace a wrapped_fn that calls into the hop. Then we look for the # hop node in the traced graph and graph module inputs to the hop. Finally, we replace the # original kwarg's callable with the graph module. all_bases = kwargs.get("_all_bases", []) _only_clone_these_bases = kwargs.get("_only_clone_these_bases") if _only_clone_these_bases is None: _only_clone_these_bases = tuple(range(len(all_bases))) schema = pytree.tree_unflatten([], kwargs.get("_op_schema")).schema # type: ignore[arg-type] new_kwargs, _ = _generate_new_op_kwargs_from_bases( schema, {k: v for k, v in kwargs.items() if k not in ("_all_bases", "_op_schema")}, all_bases, _only_clone_these_bases, _is_out=False, ) _, materialized_kwargs = materialize_callable_in_args( HopInstance(_mutable_op, schema), tuple(), new_kwargs ) # Only replace the callables in kwargs with the materialized subgraphs. # The rest of the kwargs are kept unchanged. subgraph_arg_names = { arg_info.name for arg_info in schema.arguments if isinstance(arg_info.type, torch._C.AnyType) } for k, v in kwargs.items(): if k in subgraph_arg_names and callable(v): if k not in materialized_kwargs or not isinstance( materialized_kwargs[k], torch.fx.GraphModule ): raise AssertionError( f"Expected {k} to be in materialized_kwargs as a GraphModule" ) kwargs[k] = materialized_kwargs[k] with disable_proxy_modes_tracing(): out = auto_functionalized_v2(_mutable_op, **kwargs) proxy_kwargs = pytree.tree_map(mode.tracer.unwrap_proxy, kwargs) if isinstance(_mutable_op, HigherOrderOperator): def _maybe_register_subgraph(val: Any): if isinstance(val, torch.fx.GraphModule): _, graph_name = unique_graph_id( mode, prefix="auto_functionalized_subgraph" ) mode.tracer.root.register_module(graph_name, val) return val return val proxy_kwargs = pytree.tree_map(_maybe_register_subgraph, proxy_kwargs) out_proxy = mode.tracer.create_proxy( "call_function", auto_functionalized_v2, (_mutable_op,), proxy_kwargs, ) result = track_tensor_tree(out, out_proxy, constant=None, tracer=mode.tracer) return result @auto_functionalized_v2.py_functionalize_impl def auto_functionalized_v2_func(ctx, _mutable_op, **kwargs): unwrapped_kwargs = ctx.unwrap_tensors(kwargs) with ctx.redispatch_to_next(): result = auto_functionalized_v2(_mutable_op, **unwrapped_kwargs) return ctx.wrap_tensors(result)