importbuiltinsimportfunctoolsimportinspectimportmathimportosfromtypesimportCodeType,FunctionType,ModuleTypefromtypingimportAny,Dict,NamedTuple,Optional,Set,Tuple,Type,List,Callable,Unionfromitertoolsimportchainimporttorchfromtorch._CimportScriptObject# type: ignore[attr-defined]importtorch.utils._pytreeaspytreefrom._compatibilityimportcompatibilityfrom.nodeimportArgument,map_aggregate,base_typesfrom.graphimportGraph,_PyTreeInfofrom.graph_moduleimportGraphModulefrom.proxyimportTracerBase,Proxy,ParameterProxyHAS_VARSTUFF=inspect.CO_VARARGS|inspect.CO_VARKEYWORDS# These need to run in global scope to handle nested calls correctly_orig_module_call:Callable=torch.nn.Module.__call___orig_module_getattr:Callable=torch.nn.Module.__getattr___proxyable_classes:Dict[Type,None]={}@compatibility(is_backward_compatible=True)classProxyableClassMeta(type):""" ProxyableClassMeta allows you to make construction of a given Python class symbolically traceable. For example:: import torch import torch.fx class TensorPair(metaclass=torch.fx.ProxyableClassMeta): def __init__(self, left, right): self.left, self.right = left, right def add(self, other): l = self.left + other.left r = self.right + other.right return TensorPair(l, r) def mul(self, other): l = self.left * other.left r = self.right * other.right return TensorPair(l, r) def use_tensor_pair_ctor(x : TensorPair, y : torch.Tensor): s = x.add(TensorPair(y, y)) return s.mul(x) x = TensorPair(torch.randn(5, 3), torch.randn(5, 3)) y = torch.randn(5, 3) ref_out = use_tensor_pair_ctor(x, y) traced = torch.fx.symbolic_trace(use_tensor_pair_ctor) print(traced.code) ''' def forward(self, x : __main___TensorPair, y : torch.Tensor): tensor_pair = __main___TensorPair(y, y); y = None add = x.add(tensor_pair); tensor_pair = None mul = add.mul(x); add = x = None return mul ''' From this example, we can see that contruction of a class (``TensorPair``) defined with ``ProxyableClassMeta`` as metaclass can be recorded in symbolic tracing. """def__init__(cls,name,bases,attrs):_proxyable_classes.setdefault(cls)super().__init__(name,bases,attrs)def__call__(cls,*args,**kwargs):instance=cls.__new__(cls)# type: ignore[call-overload]found_proxies=[]defcheck_proxy(a):ifisinstance(a,Proxy):found_proxies.append(a)map_aggregate(args,check_proxy)map_aggregate(kwargs,check_proxy)iflen(found_proxies)!=0:tracer=found_proxies[0].tracerreturntracer.create_proxy('call_function',cls,args,kwargs)else:cls.__init__(instance,*args,**kwargs)# type: ignore[misc]returninstancedef_patch_function(fn:FunctionType,nargs:int)->FunctionType:co=fn.__code__co_flags=co.co_flags&~HAS_VARSTUFFco_args:tupleifhasattr(co,"co_posonlyargcount"):co_args=(nargs,0,0,co.co_nlocals,co.co_stacksize,co_flags,co.co_code,co.co_consts,co.co_names,co.co_varnames,co.co_filename,co.co_name,co.co_firstlineno,co.co_lnotab,co.co_freevars,co.co_cellvars)else:co_args=(nargs,0,co.co_nlocals,co.co_stacksize,co_flags,co.co_code,co.co_consts,co.co_names,co.co_varnames,co.co_filename,co.co_name,co.co_firstlineno,co.co_lnotab,co.co_freevars,co.co_cellvars)new_code=CodeType(*co_args)# type: ignore[arg-type]returnFunctionType(new_code,fn.__globals__,fn.__name__,fn.__defaults__,fn.__closure__)# we need to insert placeholder nodes for *args and **kwargs# we can't call this function normally, otherwise it would try to unpack them# instead, let's make python think that args and kwargs are normal variables@compatibility(is_backward_compatible=False)classPHBase(object):""" Object representing an input placeholder to `concrete_args` """def__repr__(self):return'PH'PH=PHBase()
[docs]@compatibility(is_backward_compatible=True)classTracer(TracerBase):# Reference: https://github.com/pytorch/pytorch/issues/54354# The first line of this docstring overrides the one Sphinx generates for the# documentation. We need it so that Sphinx doesn't leak `math`s path from the# build environment (e.g. `<module 'math' from '/leaked/path')."""Tracer(autowrap_modules=(math,), autowrap_functions=()) ``Tracer`` is the class that implements the symbolic tracing functionality of ``torch.fx.symbolic_trace``. A call to ``symbolic_trace(m)`` is equivalent to ``Tracer().trace(m)``. Tracer can be subclassed to override various behaviors of the tracing process. The different behaviors that can be overridden are described in the docstrings of the methods on this class. """# Not checking BC on this API because the default value for `autowrap_modules`# includes the local filepath to the `math` module, which would jitter# across machines.@compatibility(is_backward_compatible=True)def__init__(self,autowrap_modules:Tuple[ModuleType]=(math,),autowrap_functions:Tuple[Callable,...]=(),param_shapes_constant:bool=False)->None:# This method's signature is overridden by the first line of this class'# docstring. If this method's signature is modified, the signature that# overrides it also should be modified accordingly.""" Construct a Tracer object. Args: autowrap_modules (Tuple[ModuleType]): defaults to `(math, )`, Python modules whose functions should be wrapped automatically without needing to use fx.wrap(). Backward-compatibility for this parameter is guaranteed. autowrap_function (Tuple[Callable, ...]): defaults to `()`, Python functions that should be wrapped automatically without needing to use fx.wrap(). Backward compabilibility for this parameter is guaranteed. param_shapes_constant (bool): When this flag is set, calls to shape, size and a few other shape like attributes of a module's parameter will be evaluted directly, rather than returning a new Proxy value for an attribute access. Backward compatibility for this parameter is guaranteed. """super().__init__()# Functions we will eagerly wrap when we see them while tracing# this captures both `math.sqrt()` and `from math import sqrt` automaticallyself._autowrap_function_ids:Set[int]={id(value)forname,valueinchain(*[m.__dict__.items()forminautowrap_modules])ifnotname.startswith("_")andcallable(value)}self._autowrap_function_ids.update(set([id(f)forfinautowrap_functions]))# Python modules to apply autowrap to at the start, in addition to# modules we see while tracingself._autowrap_search:List[ModuleType]=list(autowrap_modules)self.param_shapes_constant=param_shapes_constantself.submodule_paths:Optional[Dict[torch.nn.Module,str]]=None
[docs]@compatibility(is_backward_compatible=True)defcreate_arg(self,a:Any)->'Argument':""" A method to specify the behavior of tracing when preparing values to be used as arguments to nodes in the ``Graph``. By default, the behavior includes: #. Iterate through collection types (e.g. tuple, list, dict) and recursively call ``create_args`` on the elements. #. Given a Proxy object, return a reference to the underlying IR ``Node`` #. Given a non-Proxy Tensor object, emit IR for various cases: * For a Parameter, emit a ``get_attr`` node referring to that Parameter * For a non-Parameter Tensor, store the Tensor away in a special attribute referring to that attribute. This method can be overridden to support more types. Args: a (Any): The value to be emitted as an ``Argument`` in the ``Graph``. Returns: The value ``a`` converted into the appropriate ``Argument`` """# The base tracer is used to construct Graphs when there is no associated# module hierarchy, so it can never create parameter references.# The default tracer adds the ability to refer to parameters when# tracing modules.ifisinstance(a,torch.nn.Parameter):forn,pinself.root.named_parameters():ifaisp:returnself.create_node('get_attr',n,(),{})raiseNameError('parameter is not a member of this module')elifisinstance(a,torch.Tensor):forn_,p_inself.root.named_buffers():ifaisp_:returnself.create_node('get_attr',n_,(),{})elifisinstance(a,torch.nn.Module):forn_,p_inself.root.named_modules():ifaisp_:returnself.create_node('get_attr',n_,(),{})# For NamedTuple instances that appear literally as args, we emit# a node to construct the NamedTuple and use that Node as the argument.ifisinstance(a,tuple)andhasattr(a,'_fields'):args=tuple(self.create_arg(elem)forelemina)returnself.create_node('call_function',a.__class__,args,{})# Tensors do not have a reliable string repr() from which they can be# constructed (and we probably don't want to rely on that, either), so# for any constant Tensor values we encounter, first search for if they# are an attribute of some module in the module hierarchy. If so, emit# a get_attr to retrieve that tensor. Otherwise, we'll store away the# tensor value into a special attribute on the Module s.t. we can# retrieve it with a get_attr.ifisinstance(a,(torch.Tensor,ScriptObject)):qualname:Optional[str]=self.tensor_attrs.get(a)# Tensor was not found in the Module hierarchy, stow it away in a# special attribute and set the qualname to refer to thatifnotqualname:i=0whileTrue:qualname=f'_tensor_constant{i}'ifnothasattr(self.root,qualname):breaki+=1self.tensor_attrs[a]=qualnamesetattr(self.root,qualname,a)returnself.create_node('get_attr',qualname,(),{})iftype(a)in_proxyable_classes:# This is an instance of a proxyable class for which we did not# witness its construction. Intern this as a constant attribute# TODO: binary searchi=0whileTrue:qualname=f'_{a.__class__.__name__}_constant_{i}'ifnothasattr(self.root,qualname):breaki+=1setattr(self.root,qualname,a)returnself.create_node('get_attr',qualname,(),{})returnsuper().create_arg(a)
[docs]@compatibility(is_backward_compatible=True)defis_leaf_module(self,m:torch.nn.Module,module_qualified_name:str)->bool:""" A method to specify whether a given ``nn.Module`` is a "leaf" module. Leaf modules are the atomic units that appear in the IR, referenced by ``call_module`` calls. By default, Modules in the PyTorch standard library namespace (torch.nn) are leaf modules. All other modules are traced through and their constituent ops are recorded, unless specified otherwise via this parameter. Args: m (Module): The module being queried about module_qualified_name (str): The path to root of this module. For example, if you have a module hierarchy where submodule ``foo`` contains submodule ``bar``, which contains submodule ``baz``, that module will appear with the qualified name ``foo.bar.baz`` here. """returnm.__module__.startswith('torch.nn')andnotisinstance(m,torch.nn.Sequential)
[docs]@compatibility(is_backward_compatible=True)defpath_of_module(self,mod:torch.nn.Module)->str:""" Helper method to find the qualified name of ``mod`` in the Module hierarchy of ``root``. For example, if ``root`` has a submodule named ``foo``, which has a submodule named ``bar``, passing ``bar`` into this function will return the string "foo.bar". Args: mod (str): The ``Module`` to retrieve the qualified name for. """# Prefer the O(1) algorithmifself.submodule_paths:path=self.submodule_paths.get(mod)ifpathisNone:raiseNameError('module is not installed as a submodule')assertisinstance(path,str)returnpath# O(N^2) fallback in the case that we didn't store the submodule# paths.else:forn,pinself.root.named_modules():ifmodisp:returnnraiseNameError('module is not installed as a submodule')
[docs]@compatibility(is_backward_compatible=True)defcall_module(self,m:torch.nn.Module,forward:Callable[...,Any],args:Tuple[Any,...],kwargs:Dict[str,Any])->Any:""" Method that specifies the behavior of this ``Tracer`` when it encounters a call to an ``nn.Module`` instance. By default, the behavior is to check if the called module is a leaf module via ``is_leaf_module``. If it is, emit a ``call_module`` node referring to ``m`` in the ``Graph``. Otherwise, call the ``Module`` normally, tracing through the operations in its ``forward`` function. This method can be overridden to--for example--create nested traced GraphModules, or any other behavior you would want while tracing across ``Module`` boundaries. Args: m (Module): The module for which a call is being emitted forward (Callable): The forward() method of the ``Module`` to be invoked args (Tuple): args of the module callsite kwargs (Dict): kwargs of the module callsite Return: The return value from the Module call. In the case that a ``call_module`` node was emitted, this is a ``Proxy`` value. Otherwise, it is whatever value was returned from the ``Module`` invocation. """module_qualified_name=self.path_of_module(m)ifnotself.is_leaf_module(m,module_qualified_name):returnforward(*args,**kwargs)returnself.create_proxy('call_module',module_qualified_name,args,kwargs)
# This method will be refactored
[docs]@compatibility(is_backward_compatible=False)defcreate_args_for_root(self,root_fn,is_module,concrete_args=None):""" Create ``placeholder`` nodes corresponding to the signature of the ``root`` Module. This method introspects root's signature and emits those nodes accordingly, also supporting ``*args`` and ``**kwargs``. """# In some cases, a function or method has been decorated with a wrapper# defined via ``functools.wraps``. In this case, the outer code object# will likely not contain the actual parameters we care about, so unwrap# the function to get to the innermost callable.fn_for_analysis=inspect.unwrap(root_fn)co=fn_for_analysis.__code__total_args=co.co_argcount+co.co_kwonlyargcountorig_args=list(co.co_varnames)names_iter=iter(co.co_varnames)args:List[Any]=[]skip_arg_idx=0ifis_module:iftotal_args==0:raiseRuntimeError('``self`` argument cannot be part of *args expansion!')skip_arg_idx=1next(names_iter)# skip selfargs.append(self.root)sig=inspect.signature(fn_for_analysis)defproxy_placeholder(name:str):ifconcrete_argsisnotNoneandnameinconcrete_args:cnt=0defreplace_ph(x):nonlocalcntcnt+=1param=sig.parameters[name]default=()ifparam.defaultisinspect.Parameter.emptyelse(param.default,)out=self.create_proxy('placeholder',f'{name}_{str(cnt)}',default,{})ifx==PH:returnout# Union[int, bool] == bool in Python <= 3.6iftype(x)==boolortype(x)inbase_typesandtype(x)!=torch.Tensor:torch._assert(out==x,f"{name} has been specialized to have value {x}")else:torch.warnings.warn("Was not able to add assertion to guarantee correct inputs to ""specialized function. It is up to the user to make sure that your inputs match the ""inputs you specialized the function with.")returnxreturnpytree.tree_map(replace_ph,concrete_args[name])ifname[0]=='*':default=()else:param=sig.parameters[name]default=()ifparam.defaultisinspect.Parameter.emptyelse(param.default,)# type: ignore[assignment]returnself.create_proxy('placeholder',name,default,{},type_expr=fn_for_analysis.__annotations__.get(name,None))arg_names=[next(names_iter)foridxinrange(skip_arg_idx,total_args)]ifisinstance(concrete_args,tuple):assert(len(arg_names)==len(concrete_args))concrete_args={name:valforname,valinzip(arg_names,concrete_args)}args.extend(proxy_placeholder(names)fornamesinarg_names)ifco.co_kwonlyargcount>0orco.co_flags&HAS_VARSTUFF:# TODO: type annotations for *args and **kwargsifco.co_flags&inspect.CO_VARARGS:args.append(proxy_placeholder('*'+next(names_iter)))ifco.co_flags&inspect.CO_VARKEYWORDS:args.append(proxy_placeholder('**'+next(names_iter)))root_fn=_patch_function(root_fn,len(args))flat_args,in_spec=pytree.tree_flatten(tuple(args))ifany(notisinstance(i,pytree.LeafSpec)foriinin_spec.children_specs):# In the case that we have pytree-flattened inputs in# `concrete_args`, generate a flattening wrapper around the# original root function and return that.self.graph._pytree_info=_PyTreeInfo(orig_args[:total_args],in_spec,None)defflatten_fn(*args):tree_args=pytree.tree_unflatten(list(args),in_spec)tree_out=root_fn(*tree_args)out_args,out_spec=pytree.tree_flatten(tree_out)assert(self.graph._pytree_infoisnotNone)self.graph._pytree_info=self.graph._pytree_info._replace(out_spec=out_spec)returnout_argsreturnflatten_fn,flat_argsreturnroot_fn,args
[docs]@compatibility(is_backward_compatible=True)deftrace(self,root:Union[torch.nn.Module,Callable[...,Any]],concrete_args:Optional[Dict[str,Any]]=None)->Graph:""" Trace ``root`` and return the corresponding FX ``Graph`` representation. ``root`` can either be an ``nn.Module`` instance or a Python callable. Note that after this call, ``self.root`` may be different from the ``root`` passed in here. For example, when a free function is passed to ``trace()``, we will create an ``nn.Module`` instance to use as the root and add embedded constants to. Args: root (Union[Module, Callable]): Either a ``Module`` or a function to be traced through. Backwards-compatibility for this parameter is guaranteed. concrete_args (Optional[Dict[str, any]]): Concrete arguments that should not be treated as Proxies. This parameter is experimental and its backwards-compatibility is *NOT* guaranteed. Returns: A ``Graph`` representing the semantics of the passed-in ``root``. """ifisinstance(root,torch.nn.Module):self.root=rootfn=type(root).forwardself.submodule_paths={mod:nameforname,modinroot.named_modules()}else:self.root=torch.nn.Module()fn=roottracer_cls:Optional[Type['Tracer']]=getattr(self,'__class__',None)self.graph=Graph(tracer_cls=tracer_cls)# When we encounter a Tensor value that's not a parameter, we look if it# is some other attribute on the model. Construct a dict mapping Tensor# values to the qualified name here for efficiency. This is used downstream# in create_argself.tensor_attrs:Dict[Union[torch.Tensor,ScriptObject],str]={}defcollect_tensor_attrs(m:torch.nn.Module,prefix_atoms:List[str]):fork,vinm.__dict__.items():ifisinstance(v,(torch.Tensor,ScriptObject)):self.tensor_attrs[v]='.'.join(prefix_atoms+[k])fork,vinm.named_children():collect_tensor_attrs(v,prefix_atoms+[k])collect_tensor_attrs(self.root,[])assertisinstance(fn,FunctionType)fn_globals=fn.__globals__# run before it gets patchedfn,args=self.create_args_for_root(fn,isinstance(root,torch.nn.Module),concrete_args)parameter_proxy_cache:Dict[str,Proxy]={}# Reduce number of get_attr calls# Method dispatch on parameters is not recorded unless it's directly used.# Thus, we need to insert a proxy when __getattr__ requests a parameter.@functools.wraps(_orig_module_getattr)defmodule_getattr_wrapper(mod,attr):attr_val=_orig_module_getattr(mod,attr)returnself._module_getattr(attr,attr_val,parameter_proxy_cache)@functools.wraps(_orig_module_call)defmodule_call_wrapper(mod,*args,**kwargs):defforward(*args,**kwargs):return_orig_module_call(mod,*args,**kwargs)_autowrap_check(patcher,getattr(getattr(mod,"forward",mod),"__globals__",{}),self._autowrap_function_ids)returnself.call_module(mod,forward,args,kwargs)with_Patcher()aspatcher:# allow duplicate patches to support the case of nested callspatcher.patch_method(torch.nn.Module,"__getattr__",module_getattr_wrapper,deduplicate=False)patcher.patch_method(torch.nn.Module,"__call__",module_call_wrapper,deduplicate=False)_patch_wrapped_functions(patcher)_autowrap_check(patcher,fn_globals,self._autowrap_function_ids)formoduleinself._autowrap_search:_autowrap_check(patcher,module.__dict__,self._autowrap_function_ids)self.create_node('output','output',(self.create_arg(fn(*args)),),{},type_expr=fn.__annotations__.get('return',None))self.submodule_paths=Nonereturnself.graph
# List of pairs of (global dict, function name) functions# to patch for the purposes of the wrap() API._wrapped_fns_to_patch:List[Tuple[dict,str]]=[]# List of methods on classes to wrap (class type, function name)# this currently only works for Tensor.* methods that aren't traced properly_wrapped_methods_to_patch:List[Tuple[type,str]]=[]ifos.environ.get("FX_PATCH_GETITEM")=="1":# This change is needed to trace models like PositionalEmbedding from BERT:# https://github.com/pytorch/benchmark/blob/master/torchbenchmark/models/BERT_pytorch/bert_pytorch/model/embedding/position.py# but causes issues in quantization documented here:# https://github.com/pytorch/pytorch/issues/50710# once that is fixed we can make this the default behavior._wrapped_methods_to_patch.append((torch.Tensor,"__getitem__"))def_find_proxy(*objects_to_search):""" Recursively search a data structure for a Proxy() and return it, return None if not found. """proxy=Nonedeffind_proxy(x):nonlocalproxyifisinstance(x,Proxy):proxy=xmap_aggregate(objects_to_search,find_proxy)returnproxydef_create_wrapped_func(orig_fn):@functools.wraps(orig_fn)defwrapped(*args,**kwargs):""" Given an closed-over ``orig_function`` to invoke, search the args and kwargs for a Proxy object. If there is one, emit a ``call_function`` node to preserve the call to this leaf function directly. Otherwise, just return the results of this function call, as this function is not being traced. """proxy=_find_proxy(args,kwargs)ifproxyisnotNone:return_proxy=proxy.tracer.create_proxy('call_function',orig_fn,args,kwargs)return_proxy.node.meta['is_wrapped']=Truereturnreturn_proxyreturnorig_fn(*args,**kwargs)returnwrappeddef_create_wrapped_method(cls,name):orig_fn=getattr(cls,name)@functools.wraps(orig_fn)defwrapped(*args,**kwargs):""" Search the args and kwargs for a Proxy object. If there is one, emit a ``call_method`` node to preserve the call to this method directly. Otherwise, just return the results of this function call, as this function is not being traced. """proxy=_find_proxy(args,kwargs)ifproxyisnotNone:returnproxy.tracer.create_proxy('call_method',name,args,kwargs)returnorig_fn(*args,**kwargs)returnwrappedclass_PatchedFn(NamedTuple):frame_dict:Anyfn_name:strorig_fn:Anydefrevert(self):raiseNotImplementedError()class_PatchedFnSetItem(_PatchedFn):defrevert(self):self.frame_dict[self.fn_name]=self.orig_fnclass_PatchedFnDel(_PatchedFn):defrevert(self):delself.frame_dict[self.fn_name]class_PatchedFnSetAttr(_PatchedFn):defrevert(self):setattr(self.frame_dict,self.fn_name,self.orig_fn)class_Patcher(object):def__init__(self):super(_Patcher,self).__init__()self.patches_made:List[_PatchedFn]=[]self.visited:Set[int]=set()defpatch(self,frame_dict:Dict[str,Any],name:str,new_fn:Callable,deduplicate:bool=True):""" Replace frame_dict[name] with new_fn until we exit the context manager. """new_fn.__fx_already_patched=deduplicate# type: ignore[attr-defined]ifnamenotinframe_dictandhasattr(builtins,name):self.patches_made.append(_PatchedFnDel(frame_dict,name,None))elifgetattr(frame_dict[name],"__fx_already_patched",False):return# already patched, no need to do it againelse:self.patches_made.append(_PatchedFnSetItem(frame_dict,name,frame_dict[name]))frame_dict[name]=new_fndefpatch_method(self,cls:type,name:str,new_fn:Callable,deduplicate:bool=True):""" Replace object_or_dict.name with new_fn until we exit the context manager. """new_fn.__fx_already_patched=deduplicate# type: ignore[attr-defined]orig_fn=getattr(cls,name)ifgetattr(orig_fn,"__fx_already_patched",False):return# already patched, no need to do it againself.patches_made.append(_PatchedFnSetAttr(cls,name,orig_fn))setattr(cls,name,new_fn)defvisit_once(self,thing:Any):""" Return True on the first call to with thing, otherwise false """idx=id(thing)ifidxinself.visited:returnFalseself.visited.add(idx)returnTruedef__enter__(self):returnselfdef__exit__(self,exc_type,exc_val,exc_tb):""" Undo all the changes made via self.patch() and self.patch_method() """whileself.patches_made:# unpatch in reverse order to handle duplicates correctlyself.patches_made.pop().revert()self.visited.clear()def_patch_wrapped_functions(patcher:_Patcher):""" Go through ``_wrapped_fn_patch_table`` and, for each frame object, wrap the listed global functions in the `_create_wrapped_func` wrapper. """forframe_dict,namein_wrapped_fns_to_patch:ifnamenotinframe_dictandhasattr(builtins,name):orig_fn=getattr(builtins,name)else:orig_fn=frame_dict[name]patcher.patch(frame_dict,name,_create_wrapped_func(orig_fn))forcls,namein_wrapped_methods_to_patch:patcher.patch_method(cls,name,_create_wrapped_method(cls,name))def_autowrap_check(patcher:_Patcher,frame_dict:Dict[str,Any],function_ids:Set[int]):""" Some methods, like `math.sqrt` are common enough we want to automatically wrap them as we see them. This method searches a scope for them and patches them if found. """ifpatcher.visit_once(frame_dict):forname,valueinframe_dict.items():ifnotname.startswith("_")andcallable(value)andid(value)infunction_ids:patcher.patch(frame_dict,name,_create_wrapped_func(value))
[docs]@compatibility(is_backward_compatible=True)defwrap(fn_or_name:Union[str,Callable]):""" This function can be called at module-level scope to register fn_or_name as a "leaf function". A "leaf function" will be preserved as a CallFunction node in the FX trace instead of being traced through:: # foo/bar/baz.py def my_custom_function(x, y): return x * x + y * y torch.fx.wrap('my_custom_function') def fn_to_be_traced(x, y): # When symbolic tracing, the below call to my_custom_function will be inserted into # the graph rather than tracing it. return my_custom_function(x, y) This function can also equivalently be used as a decorator:: # foo/bar/baz.py @torch.fx.wrap def my_custom_function(x, y): return x * x + y * y A wrapped function can be thought of a "leaf function", analogous to the concept of "leaf modules", that is, they are functions that are left as calls in the FX trace rather than traced through. Args: fn_or_name (Union[str, Callable]): The function or name of the global function to insert into the graph when it's called """ifnotcallable(fn_or_name)andnotisinstance(fn_or_name,str):raiseRuntimeError('Unsupported type for global function! Must be either a callable or ''string name')ifhasattr(fn_or_name,'__code__'):assertnotisinstance(fn_or_name,str)# to make mypy happyfn_name=fn_or_name.__code__.co_nameelse:assertisinstance(fn_or_name,str),"fn_or_name must be a global function or string name"fn_name=fn_or_namecurrentframe=inspect.currentframe()assertcurrentframeisnotNonef=currentframe.f_backassertfisnotNoneiff.f_code.co_name!='<module>':raiseNotImplementedError('wrap must be called at the top level of a module')# consider implementing Callable version of this via _autowrap_function_ids / _autowrap_search# semantics would be slightly different, but would add support `from x import wrapped_function`_wrapped_fns_to_patch.append((f.f_globals,fn_name))returnfn_or_name
[docs]@compatibility(is_backward_compatible=True)defsymbolic_trace(root:Union[torch.nn.Module,Callable[...,Any]],concrete_args:Optional[Dict[str,Any]]=None)->GraphModule:""" Symbolic tracing API Given an ``nn.Module`` or function instance ``root``, this function will return a ``GraphModule`` constructed by recording operations seen while tracing through ``root``. ``concrete_args`` allows you to partially specialize your function, whether it's to remove control flow or data structures. For example:: def f(a, b): if b == True: return a else: return a*2 FX can typically not trace through this due to the presence of control flow. However, we can use `concrete_args` to specialize on the value of `b` to trace through this. f = fx.symbolic_trace(f, concrete_args={'b': False}) assert f(3, False) == 6 Note that although you can still pass in different values of `b`, they will be ignored. We can also use `concrete_args` to eliminate data-structure handling from our function. This will use pytrees to flatten your input. To avoid overspecializing, pass in `fx.PH` for values that shouldn't be specialized. For example:: def f(x): out = 0 for v in x.values(): out += v return out f = fx.symbolic_trace(f, concrete_args={'x': {'a': fx.PH, 'b': fx.PH, 'c': fx.PH}}) assert f({'a': 1, 'b': 2, 'c': 4}) == 7 Args: root (Union[torch.nn.Module, Callable]): Module or function to be traced and converted into a Graph representation. concrete_args (Optional[Dict[str, any]]): Inputs to be partially specialized Returns: GraphModule: a Module created from the recorded operations from ``root``. """tracer=Tracer()graph=tracer.trace(root,concrete_args)name=root.__class__.__name__ifisinstance(root,torch.nn.Module)elseroot.__name__returnGraphModule(tracer.root,graph,name)
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.