fromcollectionsimportdefaultdictfrom.nodeimportNode,Argument,Target,map_arg,_type_repr,_get_qualified_nameimporttorch.utils._pytreeaspytreefrom.import_pytreeasfx_pytreefrom._compatibilityimportcompatibilityimportcontextlibfromtypingimportTYPE_CHECKING,Callable,Any,List,Dict,NamedTuple,Optional,Tuple,Set,FrozenSet,Typefromdataclassesimportdataclassfromcontextlibimportcontextmanagerimportcopyimporttorchimportkeywordimportreimportbuiltinsimportmathimportwarningsimportinspect__all__=["PythonCode","CodeGen","Graph"]ifTYPE_CHECKING:from.graph_moduleimportGraphModule# noqa: F401from._symbolic_traceimportTracer# noqa: F401# Mapping of builtins to their `typing` equivalent._origin_type_map={list:List,dict:Dict,set:Set,frozenset:FrozenSet,tuple:Tuple,}# Signature for functions thattransforms the body (`list[str]`) of the# generated codeTransformCodeFunc=Callable[[List[str]],List[str]]class_CustomBuiltin(NamedTuple):"""Additional objs that we add to every graph's globals. The repr() for some standard library objects is not valid Python code without an import. For common objects of this sort, we bundle them in the globals of every FX graph. """# How to import this object from the standard library.import_str:str# The actual object, produced from that import string.obj:Any_custom_builtins:Dict[str,_CustomBuiltin]={}def_register_custom_builtin(name:str,import_str:str,obj:Any):_custom_builtins[name]=_CustomBuiltin(import_str,obj)_register_custom_builtin('inf','from math import inf',math.inf)_register_custom_builtin('nan','from math import nan',math.nan)_register_custom_builtin('NoneType','NoneType = type(None)',type(None))_register_custom_builtin('torch','import torch',torch)_register_custom_builtin('device','from torch import device',torch.device)_register_custom_builtin('fx_pytree','import torch.fx._pytree as fx_pytree',fx_pytree)_register_custom_builtin('pytree','import torch.utils._pytree as pytree',pytree)def_is_magic(x:str)->bool:returnx.startswith('__')andx.endswith('__')def_snake_case(s:str)->str:""" Transforms the given string ``s`` to a Python-style variable name Examples: ``mod.snake_case`` -> ``mod.snake_case`` ``mod.pascalCase``-> ``mod.pascal_case`` ``mod.ALL_CAPS`` -> ``mod.all_caps`` """chars=[]prev_lower=Falseforcins:ifprev_lowerandc.isupper():chars.append('_')chars.append(c.lower())prev_lower=c.islower()return''.join(chars)def_is_from_torch(obj:Any)->bool:module_name=getattr(obj,'__module__',None)ifmodule_nameisnotNone:base_module=module_name.partition('.')[0]return(base_module=='torch'andnotmodule_name.startswith("torch._dynamo.")andnotmodule_name.startswith("torch._inductor."))name=getattr(obj,'__name__',None)# exclude torch because torch.torch.torch.torch works. idk mangifnameisnotNoneandname!='torch':forguessin[torch,torch.nn.functional]:ifgetattr(guess,name,None)isobj:returnTruereturnFalseclass_Namespace:"""A context for associating names uniquely with objects. The following invariants are enforced: - Each object gets a single name. - Each name is unique within a given namespace. - Names generated do not shadow builtins, unless the object is indeed that builtin. """def__init__(self):self._obj_to_name:Dict[Any,str]={}self._unassociated_names=set()self._used_names:Set[str]=set()self._base_count:Dict[str,int]=defaultdict(int)self._illegal_char_regex=re.compile('[^0-9a-zA-Z_]+')self._name_suffix_regex=re.compile(r"(.*)_(\d+)$")defcreate_name(self,candidate:str,obj:Optional[Any])->str:"""Create a unique name. Arguments: candidate: used as the basis for the unique name, relevant to the user. obj: If not None, an object that will be associated with the unique name. """ifobjisnotNoneandobjinself._obj_to_name:returnself._obj_to_name[obj]# delete all characters that are illegal in a Python identifiercandidate=self._illegal_char_regex.sub('_',candidate)ifnotcandidate:candidate='_unnamed'ifcandidate[0].isdigit():candidate=f'_{candidate}'match=self._name_suffix_regex.match(candidate)ifmatchisNone:base=candidatenum=Noneelse:base,num_str=match.group(1,2)num=int(num_str)candidate=baseifnumisNoneelsef'{base}_{num}'ifnotnum:num=self._base_count[base]whilecandidateinself._used_namesorself._is_illegal_name(candidate,obj):num+=1candidate=f'{base}_{num}'self._used_names.add(candidate)self._base_count[base]=numifobjisNone:self._unassociated_names.add(candidate)else:self._obj_to_name[obj]=candidatereturncandidatedefassociate_name_with_obj(self,name:str,obj:Any):"""Associate a unique name with an object. Neither `name` nor `obj` should be associated already. """assertobjnotinself._obj_to_nameassertnameinself._unassociated_namesself._obj_to_name[obj]=nameself._unassociated_names.remove(name)def_is_illegal_name(self,name:str,obj:Any)->bool:# 1. keywords are never allowed as names.ifnameinkeyword.kwlist:returnTrue# 2. Can't shadow a builtin name, unless you *are* that builtin.ifnameinbuiltins.__dict__:returnobjisnotbuiltins.__dict__[name]# 3. Can't shadow our custom builtins eitherifnamein_custom_builtins:returnobjisnot_custom_builtins[name].objreturnFalsedtype_abbrs={torch.bfloat16:'bf16',torch.float64:'f64',torch.float32:'f32',torch.float16:'f16',torch.float8_e4m3fn:'f8e4m3fn',torch.float8_e5m2:'f8e5m2',torch.complex32:'c32',torch.complex64:'c64',torch.complex128:'c128',torch.int8:'i8',torch.int16:'i16',torch.int32:'i32',torch.int64:'i64',torch.bool:'b8',torch.uint8:'u8',}@compatibility(is_backward_compatible=True)@dataclassclassPythonCode:""" Represents all the information necessary to exec or save a graph as Python code. """# Python source code for the forward function definition.src:str# Values in global scope during execution of `src_def`.globals:Dict[str,Any]def_format_target(base:str,target:str)->str:elems=target.split('.')r=baseforeinelems:ifnote.isidentifier():r=f'getattr({r}, "{e}")'else:r=f'{r}.{e}'returnrclass_InsertPoint:def__init__(self,graph,new_insert):self.graph=graphself.orig_insert,graph._insert=graph._insert,new_insertdef__enter__(self):passdef__exit__(self,type,value,tb):self.graph._insert=self.orig_insertclass_node_list:def__init__(self,graph:'Graph',direction:str='_next'):assertdirectionin['_next','_prev']self.graph=graphself.direction=directiondef__len__(self):returnself.graph._lendef__iter__(self):root,direction=self.graph._root,self.directioncur=getattr(root,direction)whilecurisnotroot:ifnotcur._erased:yieldcurcur=getattr(cur,direction)def__reversed__(self):return_node_list(self.graph,'_next'ifself.direction=='_prev'else'_prev')class_PyTreeInfo(NamedTuple):""" Contains extra info stored when we're using Pytrees """orig_args:List[str]in_spec:pytree.TreeSpecout_spec:Optional[pytree.TreeSpec]@compatibility(is_backward_compatible=False)classCodeGen:def__init__(self):self._body_transformer:Optional[TransformCodeFunc]=Nonedefgen_fn_def(self,free_vars:List[str],maybe_return_annotation:str)->str:""" Given the free variables and a return annotation, generates the beginning of the FX function. By default, `gen_fn_def(['a', 'b'], '') == 'def forward(a, b):'` """# If the original function didn't have self as its first argument, we# would have added it.iflen(free_vars)==0orfree_vars[0]!='self':free_vars.insert(0,'self')returnf"def forward({', '.join(free_vars)}){maybe_return_annotation}:"defgenerate_output(self,output_args:Argument)->str:""" Given the output arguments, generates the return statement of the FX function. Note: The returned statement should not be indented. """returnf'return {repr(output_args)}'defprocess_inputs(self,*args:Any)->Any:""" Transforms the inputs so that the graph can take them as arguments, as non-default codegen may result in the inputs to the function being different from the inputs to the graph. If the graph was directly runnable, this invariant should hold true `f.graph.process_outputs(f.graph(*f.graph.process_inputs(*inputs))) == f(*inputs)` """returnargsdefprocess_outputs(self,outputs:Any)->Any:""" Transforms the outputs of the graph to be identical to the codegen. See ``process_inputs`` for more details. """returnoutputsdefadditional_globals(self)->List[Tuple[str,Any]]:""" If your codegen uses extra global values, add tuples of (identifier,reference to the value) here. For example, return ['List', typing.List] if you need ``List`` in the global context. """return[]def_gen_python_code(self,nodes,root_module:str,namespace:_Namespace,*,verbose:bool=False)->PythonCode:free_vars:List[str]=[]body:List[str]=[]globals_:Dict[str,Any]={}wrapped_fns:Dict[str,None]={}# Wrap string in list to pass by referencemaybe_return_annotation:List[str]=['']defadd_global(name_hint:str,obj:Any):"""Add an obj to be tracked as a global. We call this for names that reference objects external to the Graph, like functions or types. Returns: the global name that should be used to reference 'obj' in generated source. """if_is_from_torch(obj)andobj!=torch.device:# to support registering torch.device# HACK: workaround for how torch custom ops are registered. We# can't import them like normal modules so they must retain their# fully qualified name.return_get_qualified_name(obj)# normalize the name hint to get a proper identifierglobal_name=namespace.create_name(name_hint,obj)ifglobal_nameinglobals_:assertglobals_[global_name]isobjreturnglobal_nameglobals_[global_name]=objreturnglobal_name# Pre-fill the globals table with registered builtins.forname,(_,obj)in_custom_builtins.items():add_global(name,obj)deftype_repr(o:Any):ifo==():# Empty tuple is used for empty tuple type annotation Tuple[()]return'()'typename=_type_repr(o)ifhasattr(o,'__origin__'):# This is a generic type, e.g. typing.List[torch.Tensor]origin_type=_origin_type_map.get(o.__origin__,o.__origin__)origin_typename=add_global(_type_repr(origin_type),origin_type)ifhasattr(o,'__args__'):# Assign global names for each of the inner type variables.args=[type_repr(arg)forargino.__args__]iflen(args)==0:# Bare type, such as `typing.Tuple` with no subscript# This code-path used in Python < 3.9returnorigin_typenamereturnf'{origin_typename}[{",".join(args)}]'else:# Bare type, such as `typing.Tuple` with no subscript# This code-path used in Python 3.9+returnorigin_typename# Common case: this is a regular module name like 'foo.bar.baz'returnadd_global(typename,o)def_format_args(args:Tuple[Argument,...],kwargs:Dict[str,Argument])->str:def_get_repr(arg):# Handle NamedTuples (if it has `_fields`) via add_global.ifisinstance(arg,tuple)andhasattr(arg,'_fields'):qualified_name=_get_qualified_name(type(arg))global_name=add_global(qualified_name,type(arg))returnf"{global_name}{repr(tuple(arg))}"elifisinstance(arg,torch._ops.OpOverload):qualified_name=_get_qualified_name(arg)global_name=add_global(qualified_name,arg)returnf"{global_name}"returnrepr(arg)args_s=', '.join(_get_repr(a)forainargs)kwargs_s=', '.join(f'{k} = {_get_repr(v)}'fork,vinkwargs.items())ifargs_sandkwargs_s:returnf'{args_s}, {kwargs_s}'returnargs_sorkwargs_s# Run through reverse nodes and record the first instance of a use# of a given node. This represents the *last* use of the node in the# execution order of the program, which we will use to free unused# valuesnode_to_last_use:Dict[Node,Node]={}user_to_last_uses:Dict[Node,List[Node]]={}defregister_last_uses(n:Node,user:Node):ifnnotinnode_to_last_use:node_to_last_use[n]=useruser_to_last_uses.setdefault(user,[]).append(n)fornodeinreversed(nodes):map_arg(node.args,lambdan:register_last_uses(n,node))map_arg(node.kwargs,lambdan:register_last_uses(n,node))defdelete_unused_values(user:Node):""" Delete values after their last use. This ensures that values that are not used in the remainder of the code are freed and the memory usage of the code is optimal. """ifuser.op=='placeholder':returnifuser.op=='output':body.append('\n')returnnodes_to_delete=user_to_last_uses.get(user,[])iflen(nodes_to_delete):to_delete_str=' = '.join([repr(n)forninnodes_to_delete]+['None'])body.append(f'; {to_delete_str}\n')else:body.append('\n')prev_stacktrace=Nonedefappend_stacktrace_summary(node:Node):""" Append a summary of the stacktrace to the generated code. This is useful for debugging. """nonlocalprev_stacktracepattern=re.compile(r"^File \"(.+)\", line (\d+), in (.+)$")ifnode.opnotin{'placeholder','output'}:ifnode.stack_trace:ifnode.stack_trace!=prev_stacktrace:prev_stacktrace=node.stack_tracelines=node.stack_trace.strip().split('\n')# stacktrace should have innermost frame last, so we# iterate backwards to find the first line that starts# with 'File 'summary_str=""foridxinrange(len(lines)-2,-1,-1):line=lines[idx].strip()matches=pattern.match(line)ifmatches:file=matches.group(1)lineno=matches.group(2)# next line should be the codecode=lines[idx+1].strip()summary_str=f'File: {file}:{lineno}, code: {code}'breakbody.append(f'\n# {summary_str}\n')elifprev_stacktrace!="":prev_stacktrace=""body.append('\n# No stacktrace found for following nodes\n')defstringify_shape(shape:torch.Size)->str:returnf"[{', '.join(str(x)forxinshape)}]"defemit_node(node:Node):maybe_type_annotation=''ifnode.typeisNoneelsef' : {type_repr(node.type)}'ifverbose:# override annotation with more detailed informationfromtorch._subclasses.fake_tensorimportFakeTensorfromtorch.fx.experimental.proxy_tensorimportpy_sym_typesfromtorch.fx.passes.shape_propimportTensorMetadatameta_val=node.meta.get('val',node.meta.get('tensor_meta',None))ifisinstance(meta_val,FakeTensor):maybe_type_annotation=f': {dtype_abbrs[meta_val.dtype]}{stringify_shape(meta_val.shape)}'elifisinstance(meta_val,py_sym_types):maybe_type_annotation=f': Sym({meta_val})'elifisinstance(meta_val,TensorMetadata):maybe_type_annotation=f': {dtype_abbrs[meta_val.dtype]}{stringify_shape(meta_val.shape)}'ifnode.op=='placeholder':assertisinstance(node.target,str)maybe_default_arg=''ifnotnode.argselsef' = {repr(node.args[0])}'free_vars.append(f'{node.target}{maybe_type_annotation}{maybe_default_arg}')raw_name=node.target.replace('*','')ifraw_name!=repr(node):body.append(f'{repr(node)} = {raw_name}\n')returnelifnode.op=='call_method':assertisinstance(node.target,str)body.append(f'{repr(node)}{maybe_type_annotation} = {_format_target(repr(node.args[0]),node.target)}'f'({_format_args(node.args[1:],node.kwargs)})')returnelifnode.op=='call_function':assertcallable(node.target)# pretty print operatorsifgetattr(node.target,"__module__","")=='_operator'andnode.target.__name__inmagic_methods:assertisinstance(node.args,tuple)body.append(f'{repr(node)}{maybe_type_annotation} = 'f'{magic_methods[node.target.__name__].format(*(repr(a)forainnode.args))}')return# pretty print inplace operators; required for jit.script to work properly# not currently supported in normal FX graphs, but generated by torchdynamoifgetattr(node.target,"__module__","")=='_operator'andnode.target.__name__ininplace_methods:body.append(f'{inplace_methods[node.target.__name__].format(*(repr(a)forainnode.args))}; 'f'{repr(node)}{maybe_type_annotation} = {repr(node.args[0])}')returnqualified_name=_get_qualified_name(node.target)global_name=add_global(qualified_name,node.target)# special case for getattr: node.args could be 2-argument or 3-argument# 2-argument: attribute access; 3-argument: fall through to attrib function call with default valueifglobal_name=='getattr'and \
isinstance(node.args,tuple)and \
isinstance(node.args[1],str)and \
node.args[1].isidentifier()and \
len(node.args)==2:body.append(f'{repr(node)}{maybe_type_annotation} = {_format_target(repr(node.args[0]),node.args[1])}')returnbody.append(f'{repr(node)}{maybe_type_annotation} = {global_name}({_format_args(node.args,node.kwargs)})')ifnode.meta.get('is_wrapped',False):wrapped_fns.setdefault(global_name)returnelifnode.op=='call_module':assertisinstance(node.target,str)body.append(f'{repr(node)}{maybe_type_annotation} = 'f'{_format_target(root_module,node.target)}({_format_args(node.args,node.kwargs)})')returnelifnode.op=='get_attr':assertisinstance(node.target,str)body.append(f'{repr(node)}{maybe_type_annotation} = {_format_target(root_module,node.target)}')returnelifnode.op=='output':ifnode.typeisnotNone:maybe_return_annotation[0]=f" -> {type_repr(node.type)}"body.append(self.generate_output(node.args[0]))returnraiseNotImplementedError(f'node: {node.op}{node.target}')fornodeinnodes:# NOTE: emit_node does not emit a string with newline. It depends# on delete_unused_values to append oneifverbose:append_stacktrace_summary(node)emit_node(node)delete_unused_values(node)iflen(body)==0:# If the Graph has no non-placeholder nodes, no lines for the body# have been emitted. To continue to have valid Python code, emit a# single pass statementbody.append('pass\n')iflen(wrapped_fns)>0:wrap_name=add_global('wrap',torch.fx.wrap)wrap_stmts='\n'.join([f'{wrap_name}("{name}")'fornameinwrapped_fns])else:wrap_stmts=''ifself._body_transformer:body=self._body_transformer(body)forname,valueinself.additional_globals():add_global(name,value)prologue=self.gen_fn_def(free_vars,maybe_return_annotation[0])code=''.join(body).lstrip('\n')code='\n'.join(' '+lineforlineincode.split('\n'))fn_code=f"""{wrap_stmts}{prologue}{code}"""returnPythonCode(fn_code,globals_)# Ideally, we'd like to refactor all of the pytree logic into this codegen# class. Unfortunately, there are 3 areas we currently need extra logic in FX.# 1. In the initial symbolic trace, the pytree logic is tied up with `concrete_args`.# 2. In the FX graph, we need to access 2 attributes - in_spec and out_spec.# Since we can't access .graph within the FX forward, we need to copy the attribute to the module.# 3. We currently can't register the pytree imports with `add_global` - not sure why.class_PyTreeCodeGen(CodeGen):def__init__(self,pytree_info:_PyTreeInfo):super().__init__()self.pytree_info:_PyTreeInfo=pytree_infodefprocess_inputs(self,*inputs:Any)->Any:flat_args,_=pytree.tree_flatten(inputs)returnflat_argsdefprocess_outputs(self,out:Any)->Any:ifself.pytree_infoisNoneorself.pytree_info.out_specisNone:returnoutifnotisinstance(out,(list,tuple)):out=[out]assert(self.pytree_info.out_specisnotNone)returnpytree.tree_unflatten(out,self.pytree_info.out_spec)defgen_fn_def(self,free_vars,maybe_return_annotation):# Given a user function/model:# myargs = [myargs0, myargs1]# mykwargs = {'mykwargs0': ..., 'mykwargs1': ...}# def forward(self, mypos, *myargs, mykey=None, **mykwargs):## The generated code flattens all keywords into positional arguments for `forward()`# e.g forward(self, mypos, myargs0, myargs1, mykey, mykwargs0, mykwargs1):## Within `forward`, `tree_flatten_spec``still parses args and kwargs separately# e.g. tree_flatten_spec(([mypos, myargs0, myargs1],# {'mykey':mykey, 'mykwargs0':mykwargs0, 'mykwargs1':mykwargs1}),# self._in_spec)## If the user function/model does not have keywords, the dict is suppressed from tree_flatten_spec# e.g. tree_flatten_spec([mypos, myargs0, myargs1]), self._in_spec)ifself.pytree_infoisNone:returnsuper().gen_fn_def(free_vars,maybe_return_annotation)fn_args=self.pytree_info.orig_argshas_orig_self=(fn_args[0]=='self')iflen(fn_args)>0elseFalseifhas_orig_self:free_vars.insert(0,'self')fn_definition=super().gen_fn_def(fn_args[:],maybe_return_annotation)iflen(free_vars)>0:# pytree has placeholders in it# when kwargs is present, in_spec is tuple(args, kwargs)has_args_kwargs_tuple=self.pytree_info.in_spec.type==tupleand \
len(self.pytree_info.in_spec.children_specs)==2and \
self.pytree_info.in_spec.children_specs[0].type==tupleand \
self.pytree_info.in_spec.children_specs[1].type==dictfn_kwargs='{}'fn_signature=f"[{', '.join(fn_args)}], self._in_spec"ifhas_args_kwargs_tuple:count_args=len(self.pytree_info.in_spec.children_specs[0].children_specs)fn_args=self.pytree_info.orig_args[:count_args]fn_kwargs='{'+', '.join(f"'{k}':{v}"fork,vinzip(self.pytree_info.in_spec.children_specs[1].context,self.pytree_info.orig_args[count_args:]))+'}'fn_signature=f"([{', '.join(fn_args)}], {fn_kwargs}), self._in_spec"fn_definition+=f"""{', '.join(free_vars)}, = fx_pytree.tree_flatten_spec({fn_signature})"""returnfn_definitiondefgenerate_output(self,output_args):ifself.pytree_infoandself.pytree_info.out_spec:returnf'return pytree.tree_unflatten({repr(output_args)}, self._out_spec)'else:returnsuper().generate_output(output_args)
[docs]@compatibility(is_backward_compatible=True)classGraph:""" ``Graph`` is the main data structure used in the FX Intermediate Representation. It consists of a series of ``Node`` s, each representing callsites (or other syntactic constructs). The list of ``Node`` s, taken together, constitute a valid Python function. For example, the following code .. code-block:: python import torch import torch.fx class MyModule(torch.nn.Module): def __init__(self): super().__init__() self.param = torch.nn.Parameter(torch.rand(3, 4)) self.linear = torch.nn.Linear(4, 5) def forward(self, x): return torch.topk(torch.sum(self.linear(x + self.linear.weight).relu(), dim=-1), 3) m = MyModule() gm = torch.fx.symbolic_trace(m) Will produce the following Graph:: print(gm.graph) .. code-block:: text graph(x): %linear_weight : [num_users=1] = self.linear.weight %add_1 : [num_users=1] = call_function[target=operator.add](args = (%x, %linear_weight), kwargs = {}) %linear_1 : [num_users=1] = call_module[target=linear](args = (%add_1,), kwargs = {}) %relu_1 : [num_users=1] = call_method[target=relu](args = (%linear_1,), kwargs = {}) %sum_1 : [num_users=1] = call_function[target=torch.sum](args = (%relu_1,), kwargs = {dim: -1}) %topk_1 : [num_users=1] = call_function[target=torch.topk](args = (%sum_1, 3), kwargs = {}) return topk_1 For the semantics of operations represented in the ``Graph``, please see :class:`Node`. """
[docs]@compatibility(is_backward_compatible=True)def__init__(self,owning_module:Optional["GraphModule"]=None,tracer_cls:Optional[Type["Tracer"]]=None,tracer_extras:Optional[Dict[str,Any]]=None):""" Construct an empty Graph. """self._root:Node=Node(self,'','root','',(),{})self._used_names:Dict[str,int]={}# base name -> numberself._insert=self._root.prependself._len=0self._graph_namespace=_Namespace()self._owning_module=owning_moduleself._tracer_cls=tracer_clsself._tracer_extras=tracer_extrasself._codegen=CodeGen()self._co_fields:Dict[str,Any]={}
@propertydefowning_module(self):returnself._owning_module@owning_module.setterdefowning_module(self,mod:Optional["GraphModule"]):self._owning_module=mod@propertydefnodes(self)->_node_list:""" Get the list of Nodes that constitute this Graph. Note that this ``Node`` list representation is a doubly-linked list. Mutations during iteration (e.g. delete a Node, add a Node) are safe. Returns: A doubly-linked list of Nodes. Note that ``reversed`` can be called on this list to switch iteration order. """return_node_list(self)
[docs]@compatibility(is_backward_compatible=True)defgraph_copy(self,g:'Graph',val_map:Dict[Node,Node],return_output_node=False)->'Optional[Argument]':""" Copy all nodes from a given graph into ``self``. Args: g (Graph): The source graph from which to copy Nodes. val_map (Dict[Node, Node]): a dictionary that will be populated with a mapping from nodes in ``g`` to nodes in ``self``. Note that ``val_map`` can be passed in with values in it already to override copying of certain values. Returns: The value in ``self`` that is now equivalent to the output value in ``g``, if ``g`` had an ``output`` node. ``None`` otherwise. """fornodeing.nodes:ifnodeinval_map:continueifnode.op=='output':rv=map_arg(node.args[0],lambdan:val_map[n])returnrvifnotreturn_output_nodeelse(rv,node)val_map[node]=self.node_copy(node,lambdan:val_map[n])returnNone
def__deepcopy__(self,memo=None)->'Graph':""" Explicitly implement __deepcopy__ to prevent excessive recursion depth from the default implementation. This uses graph_copy to copy the nodes in an iterative way, rather than recursive. It also populates the memoization table to prevent unnecessary copies (e.g. references to nodes or other parts of the Graph from a custom GraphModule implementation. """memo=memoifmemoelse{}g=Graph(tracer_cls=self._tracer_cls)output_vals=g.graph_copy(self,val_map=memo,return_output_node=True)g._codegen=copy.deepcopy(self._codegen)assertisinstance(output_vals,tuple)output_val,old_output_node=output_valsnew_output_node=g.output(output_val,type_expr=getattr(old_output_node,'type',None))new_output_node.meta=copy.copy(old_output_node.meta)returng
[docs]@compatibility(is_backward_compatible=True)defcreate_node(self,op:str,target:'Target',args:Optional[Tuple['Argument',...]]=None,kwargs:Optional[Dict[str,'Argument']]=None,name:Optional[str]=None,type_expr:Optional[Any]=None)->Node:""" Create a ``Node`` and add it to the ``Graph`` at the current insert-point. Note that the current insert-point can be set via :meth:`Graph.inserting_before` and :meth:`Graph.inserting_after`. Args: op (str): the opcode for this Node. One of 'call_function', 'call_method', 'get_attr', 'call_module', 'placeholder', or 'output'. The semantics of these opcodes are described in the ``Graph`` docstring. args (Optional[Tuple[Argument, ...]]): is a tuple of arguments to this node. kwargs (Optional[Dict[str, Argument]]): the kwargs of this Node name (Optional[str]): an optional string name for the ``Node``. This will influence the name of the value assigned to in the Python generated code. type_expr (Optional[Any]): an optional type annotation representing the Python type the output of this node will have. Returns: The newly-created and inserted node. """assertopin('call_function','call_method','get_attr','call_module','placeholder','output')args=()ifargsisNoneelseargskwargs={}ifkwargsisNoneelsekwargsassertisinstance(args,tuple),"args must be a tuple"assertisinstance(kwargs,dict),"kwargs must be a dict"candidate=nameifnameisnotNoneelseself._target_to_str(target)name=self._graph_namespace.create_name(candidate,None)n=Node(self,name,op,target,args,kwargs,type_expr)self._graph_namespace.associate_name_with_obj(name,n)self._insert(n)self._len+=1returnn
[docs]@compatibility(is_backward_compatible=False)defprocess_inputs(self,*args):""" Processes args so that they can be passed to the FX graph. """returnself._codegen.process_inputs(*args)
[docs]@compatibility(is_backward_compatible=True)deferase_node(self,to_erase:Node)->None:""" Erases a ``Node`` from the ``Graph``. Throws an exception if there are still users of that node in the ``Graph``. Args: to_erase (Node): The ``Node`` to erase from the ``Graph``. """iflen(to_erase.users)>0:raiseRuntimeError(f'Tried to erase Node {to_erase} but it still had {len(to_erase.users)} 'f'users in the graph: {to_erase.users}!')ifto_erase._erased:warnings.warn(f"erase_node({to_erase}) on an already erased node")returnto_erase._remove_from_list()to_erase._erased=True# iterators may retain handles to erased nodesself._len-=1# Null out this Node's argument nodes so that the Nodes referred to# can update their ``users`` accordinglynew_args=map_arg(to_erase.args,lambdan:None)assertisinstance(new_args,tuple)to_erase.args=new_argsnew_kwargs=map_arg(to_erase.kwargs,lambdan:None)assertisinstance(new_kwargs,dict)to_erase.kwargs=new_kwargs
[docs]@compatibility(is_backward_compatible=True)definserting_before(self,n:Optional[Node]=None):"""Set the point at which create_node and companion methods will insert into the graph. When used within a 'with' statement, this will temporary set the insert point and then restore it when the with statement exits:: with g.inserting_before(n): ... # inserting before node n ... # insert point restored to what it was previously g.inserting_before(n) # set the insert point permanently Args: n (Optional[Node]): The node before which to insert. If None this will insert before the beginning of the entire graph. Returns: A resource manager that will restore the insert point on ``__exit__``. """ifnisNone:returnself.inserting_after(self._root)assertn.graph==self,"Node to insert before is not in graph."return_InsertPoint(self,n.prepend)
[docs]@compatibility(is_backward_compatible=True)definserting_after(self,n:Optional[Node]=None):"""Set the point at which create_node and companion methods will insert into the graph. When used within a 'with' statement, this will temporary set the insert point and then restore it when the with statement exits:: with g.inserting_after(n): ... # inserting after node n ... # insert point restored to what it was previously g.inserting_after(n) # set the insert point permanently Args: n (Optional[Node]): The node before which to insert. If None this will insert after the beginning of the entire graph. Returns: A resource manager that will restore the insert point on ``__exit__``. """ifnisNone:returnself.inserting_before(self._root)assertn.graph==self,"Node to insert after is not in graph."return_InsertPoint(self,n.append)
[docs]@compatibility(is_backward_compatible=True)defplaceholder(self,name:str,type_expr:Optional[Any]=None,default_value:Any=inspect.Signature.empty)->Node:""" Insert a ``placeholder`` node into the Graph. A ``placeholder`` represents a function input. Args: name (str): A name for the input value. This corresponds to the name of the positional argument to the function this ``Graph`` represents. type_expr (Optional[Any]): an optional type annotation representing the Python type the output of this node will have. This is needed in some cases for proper code generation (e.g. when the function is used subsequently in TorchScript compilation). default_value (Any): The default value this function argument should take on. NOTE: to allow for `None` as a default value, `inspect.Signature.empty` should be passed as this argument to specify that the parameter does _not_ have a default value. .. note:: The same insertion point and type expression rules apply for this method as ``Graph.create_node``. """args=()ifdefault_valueisinspect.Signature.emptyelse(default_value,)returnself.create_node('placeholder',name,args=args,type_expr=type_expr)
[docs]@compatibility(is_backward_compatible=True)defget_attr(self,qualified_name:str,type_expr:Optional[Any]=None)->Node:""" Insert a ``get_attr`` node into the Graph. A ``get_attr`` ``Node`` represents the fetch of an attribute from the ``Module`` hierarchy. Args: qualified_name (str): the fully-qualified name of the attribute to be retrieved. For example, if the traced Module has a submodule named ``foo``, which has a submodule named ``bar``, which has an attribute named ``baz``, the qualified name ``foo.bar.baz`` should be passed as ``qualified_name``. type_expr (Optional[Any]): an optional type annotation representing the Python type the output of this node will have. Returns: The newly-created and inserted ``get_attr`` node. .. note:: The same insertion point and type expression rules apply for this method as ``Graph.create_node``. """def_get_attr_reference_exists(mod:torch.nn.Module,qualified_name:str)->bool:module_path,_,name=qualified_name.rpartition(".")try:submod:torch.nn.Module=mod.get_submodule(module_path)exceptAttributeError:warnings.warn(f"Failed to fetch module {module_path}!")returnFalseifnothasattr(submod,name):returnFalseres=getattr(submod,name)if(notisinstance(res,torch.nn.Module)andnotisinstance(res,torch.nn.Parameter)andnamenotinsubmod._buffers):returnFalsereturnTrueif(self.owning_moduleandnot_get_attr_reference_exists(self.owning_module,qualified_name)):warnings.warn("Attempted to insert a get_attr Node with no ""underlying reference in the owning ""GraphModule! Call ""GraphModule.add_submodule to add the ""necessary submodule, ""GraphModule.add_parameter to add the ""necessary Parameter, or ""nn.Module.register_buffer to add the ""necessary buffer",stacklevel=2)returnself.create_node('get_attr',qualified_name,type_expr=type_expr)
[docs]@compatibility(is_backward_compatible=True)defcall_module(self,module_name:str,args:Optional[Tuple['Argument',...]]=None,kwargs:Optional[Dict[str,'Argument']]=None,type_expr:Optional[Any]=None)->Node:""" Insert a ``call_module`` ``Node`` into the ``Graph``. A ``call_module`` node represents a call to the forward() function of a ``Module`` in the ``Module`` hierarchy. Args: module_name (str): The qualified name of the ``Module`` in the ``Module`` hierarchy to be called. For example, if the traced ``Module`` has a submodule named ``foo``, which has a submodule named ``bar``, the qualified name ``foo.bar`` should be passed as ``module_name`` to call that module. args (Optional[Tuple[Argument, ...]]): The positional arguments to be passed to the called method. Note that this should *not* include a ``self`` argument. kwargs (Optional[Dict[str, Argument]]): The keyword arguments to be passed to the called method type_expr (Optional[Any]): an optional type annotation representing the Python type the output of this node will have. Returns: The newly-created and inserted ``call_module`` node. .. note:: The same insertion point and type expression rules apply for this method as :meth:`Graph.create_node`. """if(self.owning_moduleandself.owning_module.get_submodule(module_name)isNone):warnings.warn("Attempted to insert a call_module Node with ""no underlying reference in the owning ""GraphModule! Call ""GraphModule.add_submodule to add the ""necessary submodule")returnself.create_node('call_module',module_name,args,kwargs,type_expr=type_expr)
[docs]@compatibility(is_backward_compatible=True)defcall_method(self,method_name:str,args:Optional[Tuple['Argument',...]]=None,kwargs:Optional[Dict[str,'Argument']]=None,type_expr:Optional[Any]=None)->Node:""" Insert a ``call_method`` ``Node`` into the ``Graph``. A ``call_method`` node represents a call to a given method on the 0th element of ``args``. Args: method_name (str): The name of the method to apply to the self argument. For example, if args[0] is a ``Node`` representing a ``Tensor``, then to call ``relu()`` on that ``Tensor``, pass ``relu`` to ``method_name``. args (Optional[Tuple[Argument, ...]]): The positional arguments to be passed to the called method. Note that this *should* include a ``self`` argument. kwargs (Optional[Dict[str, Argument]]): The keyword arguments to be passed to the called method type_expr (Optional[Any]): an optional type annotation representing the Python type the output of this node will have. Returns: The newly created and inserted ``call_method`` node. .. note:: The same insertion point and type expression rules apply for this method as :meth:`Graph.create_node`. """returnself.create_node('call_method',method_name,args,kwargs,type_expr=type_expr)
[docs]@compatibility(is_backward_compatible=True)defcall_function(self,the_function:Callable[...,Any],args:Optional[Tuple['Argument',...]]=None,kwargs:Optional[Dict[str,'Argument']]=None,type_expr:Optional[Any]=None)->Node:""" Insert a ``call_function`` ``Node`` into the ``Graph``. A ``call_function`` node represents a call to a Python callable, specified by ``the_function``. Args: the_function (Callable[..., Any]): The function to be called. Can be any PyTorch operator, Python function, or member of the ``builtins`` or ``operator`` namespaces. args (Optional[Tuple[Argument, ...]]): The positional arguments to be passed to the called function. kwargs (Optional[Dict[str, Argument]]): The keyword arguments to be passed to the called function type_expr (Optional[Any]): an optional type annotation representing the Python type the output of this node will have. Returns: The newly created and inserted ``call_function`` node. .. note:: The same insertion point and type expression rules apply for this method as :meth:`Graph.create_node`. """returnself.create_node('call_function',the_function,args,kwargs,type_expr=type_expr)
[docs]@compatibility(is_backward_compatible=True)defnode_copy(self,node:Node,arg_transform:Callable[[Node],'Argument']=lambdax:x)->Node:""" Copy a node from one graph into another. ``arg_transform`` needs to transform arguments from the graph of node to the graph of self. Example:: # Copying all the nodes in `g` into `new_graph` g : torch.fx.Graph = ... new_graph = torch.fx.graph() value_remap = {} for node in g.nodes: value_remap[node] = new_graph.node_copy(node, lambda n : value_remap[n]) Args: node (Node): The node to copy into ``self``. arg_transform (Callable[[Node], Argument]): A function that transforms ``Node`` arguments in node's ``args`` and ``kwargs`` into the equivalent argument in ``self``. In the simplest case, this should retrieve a value out of a table mapping Nodes in the original graph to ``self``. """args=map_arg(node.args,arg_transform)kwargs=map_arg(node.kwargs,arg_transform)assertisinstance(args,tuple)assertisinstance(kwargs,dict)result_node=self.create_node(node.op,node.target,args,kwargs,node.name,node.type)result_node.meta=copy.copy(node.meta)returnresult_node
[docs]@compatibility(is_backward_compatible=True)defoutput(self,result:'Argument',type_expr:Optional[Any]=None):""" Insert an ``output`` ``Node`` into the ``Graph``. An ``output`` node represents a ``return`` statement in Python code. ``result`` is the value that should be returned. Args: result (Argument): The value to be returned. type_expr (Optional[Any]): an optional type annotation representing the Python type the output of this node will have. .. note:: The same insertion point and type expression rules apply for this method as ``Graph.create_node``. """returnself.create_node(op='output',target='output',args=(result,),type_expr=type_expr)
[docs]@compatibility(is_backward_compatible=True)defpython_code(self,root_module:str,*,verbose:bool=False)->PythonCode:""" Turn this ``Graph`` into valid Python code. Args: root_module (str): The name of the root module on which to look-up qualified name targets. This is usually 'self'. Returns: A PythonCode object, consisting of two fields: src: the Python source code representing the object globals: a dictionary of global names in `src` -> the objects that they reference. """# NOTE: [Graph Namespaces]## There are two types of symbols in generated Python source code:# locals and globals.# Locals are locally defined by the output of a node in the Graph.# Globals are references to external objects, like functions or types.## When generating Python code, we need to make sure to name things# appropriately. In particular:# - All names should be unique, to avoid weird shadowing bugs.# - These names need to be consistent, e.g. a object should always be# referenced by the same name.## To do this, we create a new namespace just for this source. All names# that get printed must come from this namespace.## Why can't we re-use node.name? Because it was generated within the# namespace `self._graph_namespace`. In order to provide uniqueness# over both locals (node.name) *and* globals, we create a completely# new namespace to put all identifiers in.namespace=_Namespace()# Override Node's repr to generate a valid name within our namespace.# Since repr() is designed to produce a valid Python expression, it# makes sense to re-use it. This way, it's easy to print something like# Tuple[Node, Node] by simply calling repr() on it. Node's __repr__ is# implemented cooperatively to allow this.defnode_repr(n:Node):returnnamespace.create_name(n.name,n)@contextmanagerdefoverride_node_repr(graph:Graph):orig_repr_fns={}fornodeingraph.nodes:orig_repr_fns[node]=node._repr_fnnode._repr_fn=node_reprtry:yieldNonefinally:# restore the original repr functionsfornodeingraph.nodes:node._repr_fn=orig_repr_fns[node]withoverride_node_repr(self):returnself._python_code(root_module,namespace,verbose=verbose)
def_python_code(self,root_module:str,namespace:_Namespace,*,verbose:bool=False)->PythonCode:returnself._codegen._gen_python_code(self.nodes,root_module,namespace,verbose=verbose)def__str__(self)->str:""" Return a human-readable (not machine-readable) string representation of this Graph """placeholder_names:List[str]=[]# This is a one-element array just so ``format_node`` can modify the closed# over valuemaybe_return_typename:List[str]=['']node_strs=[node.format_node(placeholder_names)fornodeinself.nodes]param_str=', '.join(placeholder_names)s=f'graph({param_str}){maybe_return_typename[0]}:'fornode_strinnode_strs:ifnode_str:s+='\n '+node_strreturns
[docs]@compatibility(is_backward_compatible=True)defprint_tabular(self):""" Prints the intermediate representation of the graph in tabular format. Note that this API requires the ``tabulate`` module to be installed. """try:fromtabulateimporttabulateexceptImportError:print("`print_tabular` relies on the library `tabulate`, ""which could not be found on this machine. Run `pip ""install tabulate` to install the library.")raisenode_specs=[[n.op,n.name,n.target,n.args,n.kwargs]forninself.nodes]print(tabulate(node_specs,headers=['opcode','name','target','args','kwargs']))
[docs]@compatibility(is_backward_compatible=True)deflint(self):""" Runs various checks on this Graph to make sure it is well-formed. In particular: - Checks Nodes have correct ownership (owned by this graph) - Checks Nodes appear in topological order - If this Graph has an owning GraphModule, checks that targets exist in that GraphModule """# Check topo orderdefcheck_arg(arg:Node,n:Optional[Node]=None)->None:context_str=f' of Node \'{n}\' 'ifnelse' 'ifarg.graphisnotself:raiseRuntimeError(f'Argument \'{arg}\'{context_str}does not belong to this Graph, 'f'but was used as an argument! If you are copying nodes from another graph, make 'f'sure to use ``arg_transform`` on node_copy() to remap values\n{self}')ifargnotinseen_values:raiseRuntimeError(f'Argument \'{arg}\'{context_str}was used before it has been 'f'defined! Please check that Nodes in the graph are topologically ordered\n{self}')seen_names:Set[str]=set()seen_values:Set[Node]=set()fornodeinself.nodes:ifnode.opnotin['placeholder','call_method','call_module','call_function','get_attr','output']:raiseRuntimeError(f'Node {node} had unknown opcode {node.op}!')ifnode.graphisnotself:raiseRuntimeError(f'Node \'{node}\' does not belong to this Graph!')map_arg(node.args,lambdaarg:check_arg(arg,node))map_arg(node.kwargs,lambdaarg:check_arg(arg,node))seen_values.add(node)ifnode.nameinseen_names:raiseRuntimeError(f'Node redefined name {node.name}!')seen_names.add(node.name)# Check targets are legitifself.owning_module:fornodeinself.nodes:ifnode.op=='call_function':ifnotcallable(node.target):raiseValueError(f'Node {node} target {node.target} has type {torch.typename(node.target)} but ''a Callable is expected')else:ifnotisinstance(node.target,str):raiseValueError(f'Node {node} target {node.target} has type {torch.typename(node.target)} but ''a str is expected')ifnode.opin['get_attr','call_module']:target_atoms=node.target.split('.')m_itr=self.owning_modulefori,atominenumerate(target_atoms):new_m_itr=getattr(m_itr,atom,None)seen_qualname='.'.join(target_atoms[:i])ifnew_m_itrisNone:raiseRuntimeError(f'Node {node} target {node.target} references nonexistent attribute 'f'{atom} of {seen_qualname}')if(node.op=="call_module"andnotisinstance(new_m_itr,torch.nn.Module)):raiseRuntimeError(f'Node {node} target {node.target}{atom} of {seen_qualname} does ''not reference an nn.Module')elif(node.op=="get_attr"andnotisinstance(new_m_itr,torch.nn.Module)andnotisinstance(new_m_itr,torch.nn.Parameter)andatomnotinm_itr._buffers):warnings.warn(f'Node {node} target {node.target}{atom} of {seen_qualname} does ''not reference an nn.Module, nn.Parameter, or buffer, which is ''what \'get_attr\' Nodes typically target')else:m_itr=new_m_itr
[docs]@compatibility(is_backward_compatible=True)defeliminate_dead_code(self):""" Remove all dead code from the graph, based on each node's number of users, and whether the nodes have any side effects. The graph must be topologically sorted before calling. Returns: bool: Whether the graph was changed as a result of the pass. Example: Before dead code is eliminated, `a` from `a = x + 1` below has no users and thus can be eliminated from the graph without having an effect. .. code-block:: python def forward(self, x): a = x + 1 return x + self.attr_1 After dead code is eliminated, `a = x + 1` has been removed, and the rest of `forward` remains. .. code-block:: python def forward(self, x): return x + self.attr_1 .. warning:: Dead code elimination has some heuristics to avoid removing side-effectful nodes (see Node.is_impure) but in general coverage is very bad, so you should assume that this method is not sound to call unless you know that your FX graph consists entirely of functional operations. """# Lint the graph first to make sure its topologically sorted, otherwise# DCE below will not behave as expected.self.lint()# Reverse iterate so that when we remove a node, any nodes used as an# input to that node have an updated user count that no longer reflects# the removed node.changed=Falsefornodeinreversed(self.nodes):ifnotnode.is_impure()andlen(node.users)==0:self.erase_node(node)changed=Truereturnchanged
[docs]@compatibility(is_backward_compatible=False)defon_generate_code(self,make_transformer:Callable[[Optional[TransformCodeFunc]],TransformCodeFunc]):"""Register a transformer function when python code is generated Args: make_transformer (Callable[[Optional[TransformCodeFunc]], TransformCodeFunc]): a function that returns a code transformer to be registered. This function is called by `on_generate_code` to obtain the code transformer. This function is also given as its input the currently registered code transformer (or None if nothing is registered), in case it is not desirable to overwrite it. This is useful to chain code transformers together. Returns: a context manager that when used in a `with` statement, to automatically restore the previously registered code transformer. Example: .. code-block:: python gm: fx.GraphModule = ... # This is a code transformer we want to register. This code # transformer prepends a pdb import and trace statement at the very # beginning of the generated torch.fx code to allow for manual # debugging with the PDB library. def insert_pdb(body): return ["import pdb; pdb.set_trace()\\n", *body] # Registers `insert_pdb`, and overwrites the current registered # code transformer (given by `_` to the lambda): gm.graph.on_generate_code( lambda _: insert_pdb ) # Or alternatively, registers a code transformer which first # runs `body` through existing registered transformer, then # through `insert_pdb`: gm.graph.on_generate_code( lambda current_trans: ( lambda body: insert_pdb( current_trans(body) if current_trans else body ) ) ) gm.recompile() gm(*inputs) # drops into pdb This function can also be used as a context manager, with the benefit to automatically restores the previously registered code transformer: .. code-block:: python # ... continue from previous example with gm.graph.on_generate_code(lambda _: insert_pdb): # do more stuff with `gm`... gm.recompile() gm(*inputs) # drops into pdb # now previous code transformer is restored (but `gm`'s code with pdb # remains - that means you can run `gm` with pdb here too, until you # run next `recompile()`). """on_gen_code_old=self._codegen._body_transformerself._codegen._body_transformer=make_transformer(on_gen_code_old)@contextlib.contextmanagerdefon_generate_code_context_manager():try:yieldfinally:self._codegen._body_transformer=on_gen_code_oldreturnon_generate_code_context_manager()
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.