L i* UdZddlZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z ddl Z ddl mZddlmZddl mZmZddlmZddlmZddlmZmZdd lmZmZmZdd lmZm Z m!Z!m"Z"dd l#m$Z$m%Z%m&Z&m'Z'm(Z(dd l)m*Z*m+Z+m,Z,dd l-m.Z.ddl/m0Z0m1Z1m2Z2ddl3m4Z4m5Z5ddl6m7Z7ddl8m9Z9ea:ee jvjx_=ee jvj|_=e jvj|Z>de>_de>_?de>_@e7e>ddZAeAe>_Be$rejdddgZDndZDdeD_dZEdZFdZGGd d!ZHGd"d#eHZIGd$d%eJZKGd&d'ZLGd(d)eMZNd*ZOGd+d,ZPd-e5d.eQd/e jj\fd0ZSe$rgd1ZTGd2d3ZUeTD]ZVd4ZWeXeUeVeWGd5d6e.eK7ZYGd8d9eYZZeZjjD]<\Z]Z^e_e^s e`e^ease]jd:s eceYe]r3eXeYe]e^>d;Zdhd<Zed=Zfede jj\D]V\Z]Zge]jd:se]jd>r)e]eZjvs8e]eevs=eXeZegj~efe]Xn4Gd?d3ZUGd@d6e jj\ZYGdAd9eYZZdBZidCZjdDZkd`dEZldFameneodG< dadHe epeqere epeqfdffdIZs dadHe epeqere epeqfdffdJZtdKZudLZvdMZwdNZxdOZydPZze jvjZ{e7e{ddbdQeQdRe|dSe|dTeQfdUZ}GdVdWZ~GdXdYZGdZd[Zd\Zeed]eejd^ee0d_ee1d_ee2d_y)czTorchScript. This module contains functionality to support the JIT's scripting frontend, notably: - torch.jit.script This is not intended to be imported directly; please use the exposed functionalities in `torch.jit`. N)AnyCallableUnion)classes) _get_model_id_qualified_name)log_torchscript_usage)_register_builtin) _graph_for_script_method_graph_for)JitTypeTraceConfigJitTypeTraceStoremonkeytype_trace)_compile_and_register_classinfer_methods_to_compileScriptMethodStubwrap_cpp_module)_enabled_set_jit_function_cache_set_jit_overload_cache_try_get_jit_cached_function_try_get_jit_cached_overloads)get_default_argsget_jit_class_def get_jit_def)Module)has_torch_functionhas_torch_function_unaryhas_torch_function_variadic)PackageExporterPackageImporter) set_module)validate_map_locationz Functionally equivalent to a :class:`ScriptModule`, but represents a single function and does not have any attributes or Parameters. ScriptFunctiontorch.jit.ScriptFunctionz torch.jitc,tjd)Nz ScriptFunction cannot be pickledpickle PickleErrorclss W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/jit/_script.py_reducer.Is   ? @@ Attributevaluetypec|SN)r1r2s r-r0r0Ts r/a This method is a pass-through function that returns `value`, mostly used to indicate to the TorchScript compiler that the left-hand side expression is a class instance attribute with type of `type`. Note that `torch.jit.Attribute` should only be used in `__init__` method of `jit.ScriptModule` subclasses. Though TorchScript can infer correct type for most Python expressions, there are some cases where type inference can be wrong, including: - Empty containers like `[]` and `{}`, which TorchScript assumes to be container of `Tensor` - Optional types like `Optional[T]` but assigned a valid value of type `T`, TorchScript would assume it is type `T` rather than `Optional[T]` In eager mode, it is simply a pass-through function that returns `value` without other implications. Example: .. testcode:: import torch from typing import Dict class AttributeModule(torch.jit.ScriptModule): def __init__(self) -> None: super().__init__() self.foo = torch.jit.Attribute(0.1, float) # we should be able to use self.foo as a float here assert 0.0 < self.foo self.names_ages = torch.jit.Attribute({}, Dict[str, int]) self.names_ages["someone"] = 20 assert isinstance(self.names_ages["someone"], int) m = AttributeModule() # m will contain two attributes # 1. foo of type float # 2. names_ages of type Dict[str, int] .. testcleanup:: del AttributeModule del m Note: it's now preferred to instead use type annotations instead of `torch.jit.Attribute`: .. testcode:: import torch from typing import Dict class AttributeModule(torch.nn.Module): names: Dict[str, int] def __init__(self) -> None: super().__init__() self.names = {} m = AttributeModule() .. testcleanup:: del AttributeModule del m Args: value: An initial value to be assigned to attribute. type: A Python type Returns: Returns `value` ctSr4) type_trace_dbr5r/r-_get_type_trace_dbr8s r/ct||dSr4)getattr)r,names r-_get_function_from_typer<s 3d ##r/cRt|drdt|vxs t|dSy)N __class____dict__ __slots__)hasattrdirr+s r-_is_new_style_classrCs,sK SX%Bk)BB!r/cBeZdZdZdZdZdZdZdZdZ dZ d Z y ) OrderedDictWrapperc||_yr4)_c)selfrGs r-__init__zOrderedDictWrapper.__init__s r/cT|jDcgc]\}}| c}}Scc}}wr4itemsrHkvs r-keyszOrderedDictWrapper.keys "jjl+da+++ $cT|jDcgc]\}}| c}}Scc}}wr4rKrMs r-valueszOrderedDictWrapper.valuesrQrRc4t|jSr4)lenrTrHs r-__len__zOrderedDictWrapper.__len__s4;;=!!r/ctd)Nz6cannot delete methods or parameters of a script module RuntimeErrorrHrNs r- __delitem__zOrderedDictWrapper.__delitem__sSTTr/c6|jjSr4)rGrLrWs r-rLzOrderedDictWrapper.itemssww}}r/c`||vrtd||jj||y)NzICan't add a new parameter after ScriptModule construction. Tried to add ')r[rGsetattrrMs r- __setitem__zOrderedDictWrapper.__setitem__s6 D=[\][^_  1r/c8|jj|Sr4)rGcontainsr\s r- __contains__zOrderedDictWrapper.__contains__sww""r/cV||vr t||jj|Sr4)KeyErrorrGr:r\s r- __getitem__zOrderedDictWrapper.__getitem__s& D=1+ wwq!!r/N) __name__ __module__ __qualname__rIrPrTrXr]rLrardrgr5r/r-rErEs0,,"U#"r/rEc6eZdZfdZdZdZdZdZxZS)OrderedModuleDictclt|tjj |||_yr4)superrItorch_C ModuleDict_python_modules)rHmodule python_dictr>s r-rIzOrderedModuleDict.__init__s) ,,V45 +r/c:|jj}|Sr4)rrrLrHrs r-rLzOrderedModuleDict.itemss  & & (r/c||jvSr4rrr\s r-rdzOrderedModuleDict.__contains__sD((((r/ct|tr,|jj||||j|<yt d|d|)NzgCannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing module 'z': ) isinstance ScriptModulerGr`rrr[rMs r-razOrderedModuleDict.__setitem__sS a & GGOOAq !&'D  #==>Cs1#G r/c |j|Sr4ryr\s r-rgzOrderedModuleDict.__getitem__ s##A&&r/) rhrirjrIrLrdrarg __classcell__r>s@r-rlrls+)&'r/rlceZdZfdZxZS) ScriptMetac i_ttdd_t |D]i}t|dij D]\}}|j|<t|dt}jj |_kt|j D]E\}}t|tst||j|jj<Gtddrt 9|||ytdd tj  fd }|_t 9|||y) N __constants__r5_methods_constants_set_disable_script_metaFrIcyr4r5rWs r-z%ScriptMeta.__init__..0sr/ct j} |g|i|t j|kD}t| k(rd}tjj j ||| |jd<|jj}|jD]}t|||jD]\}}t||dD]}t||yy)Nct|}t|dr6t|jj Dcgc]\}}| c}}St |Scc}}w)Nr)r2rAsortedrrLr)rsr,rNrOs r- make_stubszAScriptMeta.__init__..init_then_script..make_stubs:sMv,CsJ/.4S\\5G5G5I.JKdaKK7?? Ls A) share_types_actual_script_module) _parameters_buffers_modules) rVrr2rojit _recursivecreate_script_moduler?r_concrete_typeget_attributesdelattr get_modules) rHargskwargs num_methodsadded_methods_in_initr concrete_typer;_r, original_inits r-init_then_scriptz-ScriptMeta.__init__..init_then_script2scll+K $ 0 0 0$' $5 $C !DzS @II((==j:O6O> 56!% : : I I )88:(DD$'(,88:(GD!D$'(C(DD$'(/!r/)rsetr:rreversedrLunionrr{rroriginal_methodrhrnrI functoolswraps) r,r;basesattrsbaserNrObase_constantsrrr>s ` @r-rIzScriptMeta.__init__sH')  or!BCUO JDj"5;;= $1"# Q $")$0@#%"HN!$!3!3!9!9.!IC   J5;;=) =DAq!-.Q;< Q..778 = 3. 6 G T5% 0 Z1BC  ' ( ( (>(  ue,r/rhrirjrIr~rs@r-rrs :-:-r/rceZdZdZy)_CachedForwardc$|jdS)Nforward) __getattr__)rHobjr,s r-__get__z_CachedForward.__get__Ws **r/N)rhrirjrr5r/r-rrVs+r/rc eZdZy) ScriptWarningNrhrirjr5r/r-rr[sr/rcts|Stjd}t||jd}t |||S)N frames_upr|) self_name)r _jit_internal!createResolutionCallbackFromFramerrhr)fn_rcbasts r- script_methodr_s=    : :Q GD b"++ @C D#r **r/ceZdZdZdZy)ConstMapc||_yr4 const_mapping)rHrs r-rIzConstMap.__init__ts *r/c |j|Sr4r)rHattrs r-rzConstMap.__getattr__ws!!$''r/N)rhrirjrIrr5r/r-rrss +(r/rimporterscript_module_idreturncZt|jtjjs t dtjj }tjj||j|jt|j|}t|S)z Call by ``torch.package.PackageImporter``'s Pickler's ``persistent_load`` function. Performs work of loading and returning a ScriptModule from a ``torch.package`` archive. z{Loading ScriptObjects from a PackageImporter created from a directory is not supported. Use a package archive file instead.) r{ zip_readerrorpPyTorchFileReaderr[CompilationUnit_import_ir_module_from_packagestorage_contextr$last_map_locationr)rrcu cpp_modules r-unpackage_script_moduler{s h))588+E+E F N    ! ! #B88   h889 J : &&r/)__iter__rX__neg____mul__rd__add____sub____pow__ __truediv____mod____ne____eq____lt____gt____le____ge____and____or____xor__rgra__call____int__ __float____bool____str__ __enter____exit__cHeZdZdZfdZfdZfdZdZdZdZ xZ S)RecursiveScriptClassaXWrapper for a TorchScript class instance for use in Python. An analogue of RecursiveScriptModule for regular objects that are not modules. This class is a wrapper around a torch._C.ScriptObject that represents an instance of a TorchScript class and allows it to be used in Python. Attributes: _c [torch._C.ScriptObject]: The C++ object to which attribute lookups and method calls are forwarded. _props [Dict[str, property]]: A dictionary of properties fetched from self._c and exposed on this wrppaer. c"t|d|jd<||_|jj Dcic]-}|j t |j|j/c}|_ d|jd<ycc}w)NT _initializingF) rnrIr?rG _propertiesr;propertygettersetter_props)rH cpp_classpropr>s r-rIzRecursiveScriptClass.__init__sy G  -1DMM/ *DG !GG//1 8DKK==DK .3DMM/ * s2B c|jjdrt| |S||jvr|j|j St |j|SNr)r?getrnrrfgetr:rGrHrr>s r-rz RecursiveScriptClass.__getattr__sZ}}  1w*400t{{"{{4(--//477D) )r/c|jjdrt| ||S||jvr|j|j |St |j||yr)r?rrn __setattr__rfsetr`rGrHrr1r>s r-rz RecursiveScriptClass.__setattr__s]}}  1w*477t{{"{{4(--e44 DGGT5 )r/cv|jj|st|j|}||i|Sr4)rG _has_method TypeErrorrrH method_namerr self_methods r-forward_magic_methodz)RecursiveScriptClass.forward_magic_methods977&&{3**;7K// /r/c,tjd)NzScriptClasses cannot be pickledr(rWs r- __getstate__z!RecursiveScriptClass.__getstate__s$$%FG Gr/c|jjdr|jd|S|jd|S)N__iadd__r)rGrr)rHothers r-r zRecursiveScriptClass.__iadd__s:ww"":.00UCC00EBBr/) rhrirj__doc__rIrrrr r r~rs@r-rrs(  3 * * 0 H Cr/rc6|jtg|i|Sr4)rrrHrrs r-method_templaters,4,,[J4J6J Jr/c|eZdZUdZgdZd fd ZeZede fe d<fdZ fdZ dZ d Zd efd ZxZS) r|a'Wrapper for C++ torch::jit::Module with methods, attributes, and parameters. A wrapper around C++ ``torch::jit::Module``. ``ScriptModule``\s contain methods, attributes, parameters, and constants. These can be accessed the same way as on a normal ``nn.Module``. )codecode_with_constantsgraph inlined_graph original_namec"t|yr4rnrI)rHr>s r-rIzScriptModule.__init__ G  r/.rcjd|jvrt| |St|j|S)Nr)r?rnrr:rrs r-rzScriptModule.__getattr__s2&dmm;w*400455t< rr2r1rnrr`rrs r-rzScriptModule.__setattr__s|&dmm; eY/ )0G0GG9;616D((.!KKEw*477 D..e -@A Ar/rN)rhrirjr __jit_unused_properties__rIrrrrrrrrr#r r*r~rs@r-r|r|sQ % ! '5&6#s(#6 =  =( N, M B Br/r|) metaclassc eZdZdZdZfdZedZedZdZ e dZ e dZ e d Z e d Zd Zd Zd ZdZdZdZdZe dZdZfdZfdZdZdZdZdZdZdZdZ fdZ!dZ"dZ#xZ$S) RecursiveScriptModuleaZRetain the existing isinstance(ScriptModule) behavior. The core data structure in TorchScript is the ``ScriptModule``. It is an analogue of torch's ``nn.Module`` and represents an entire model as a tree of submodules. Like normal modules, each individual module in a ``ScriptModule`` can have submodules, parameters, and methods. In ``nn.Module``\s methods are implemented as Python functions, but in ``ScriptModule``\s methods are implemented as TorchScript functions, a statically-typed subset of Python that contains all of PyTorch's built-in Tensor operations. This difference allows your ``ScriptModule``\s code to run without the need for a Python interpreter. ``ScriptModule``\s should not be created manually, instead use either :func:`tracing ` or :func:`scripting `. Tracing and scripting can be applied incrementally and :ref:`composed as necessary `. * Tracing records the tensor operations as executed with a set of example inputs and uses these operations to construct a computation graph. You can use the full dynamic behavior of Python with tracing, but values other than Tensors and control flow aren't captured in the graph. * Scripting inspects the Python code of the model and compiles it to TorchScript. Scripting allows the use of many `types`_ of values and supports dynamic control flow. Many, but not all features of Python are supported by the compiler, so changes to the source code may be necessary. Tcfd|jd<||_t| t |dy)NTrtraining)r?rGrnrIr)rHrr>s r-rIzRecursiveScriptModule.__init__ts/-1DMM/ * DG G   D* %r/cVt|}||tj||S)a Construct a RecursiveScriptModule that's ready for use. PyTorch code should use this to construct a RecursiveScriptModule instead of instead of calling `__init__` directly, as it makes sure the object is properly finalized (and in the future, we may take control of how the RecursiveScriptModule instance is created). Args: cpp_module: The C++ Module that will hold the actual state of this RecursiveScriptModule instance. init_fn: Lambda that initializes the RecursiveScriptModule passed to it. )r/_finalize_scriptmodule)rinit_fn script_modules r- _constructz RecursiveScriptModule._construct}s,2*=M M " " 8 8 G r/c8ttjj|j|_ttjj |j|_t|j|j|_ d|_ y)NF) rErorp ParameterDictrGr BufferDictrrlrrr5s r-r3z,RecursiveScriptModule._finalize_scriptmodulesx(:&&}'7'78)M %&8##M$4$45&M "&7  -"8"8&M "+0M 'r/c:|j|tjjj |j j |_i}tjj|j jD]\}}t|||<t|j ||_ ttjj|j |_ttjj!|j |_|j$jDcic],\}}t'|tjj(s||.c}}|_d|j$d<ycc}}w)z Re-construct an instance of RecursiveScriptModule using an instance of a C++ module. Args: cpp_module: The C++ module that this RecursiveScriptModule will be rebuilt around. FrN)rIrorpConcreteModuleType from_jit_typerG_typerrqrLrrlrrEr8rr9rr?r{ ScriptMethod)rHrmodulesr;rNrOs r- _reconstructz"RecursiveScriptModule._reconstructs. MM* %#((("="="K"K #D  G$)HH$7$7$@$F$F$H < j / ;  <-dggw?DM 2%((2H2H2QRD .uxx/B/B477/KLDM !MM//1Aq!!UXX%:%:;1DM .3DMM/ * s 1FcL|jjdjS)zPReturn a string representation of the internal graph for the ``forward`` method.r)rG _get_methodrrWs r-rzRecursiveScriptModule.graphs77&&y177 7r/c.|jjS)z Return a string representation of the internal graph for the ``forward`` method. This graph will be preprocessed to inline all function and method calls. )rrrWs r-rz#RecursiveScriptModule.inlined_graphs<<-- -r/c.|jjS)z Return a pretty-printed representation (as valid Python syntax) of the internal graph for the ``forward`` method. )rrrWs r-rzRecursiveScriptModule.codes <<$$ $r/cT|jj}|dt|dfS)a|Return a tuple. Returns a tuple of: [0] a pretty-printed representation (as valid Python syntax) of the internal graph for the ``forward`` method. See `code`. [1] a ConstMap following the CONSTANT.cN format of the output in [0]. The indices in the [0] output are keys to the underlying constant's values. rr#)rrrrvs r-rz)RecursiveScriptModule.code_with_constantss* 00AaD(1Q4.) )r/c N|jjt|fi|S)amSave with a file-like object. save(f, _extra_files={}) See :func:`torch.jit.save ` which accepts a file-like object. This function, torch.save(), converts the object to a string, treating it as a path. DO NOT confuse these two functions when it comes to the 'f' parameter functionality. )rGsavestr)rHfrs r-rHzRecursiveScriptModule.saves" 477<<A1&1 1r/c:|jj|i|S)azAdd (or update) the bytecode session to the script model. _save_for_lite_interpreter(f) The updated model is used in lite interpreter for mobile applications. Args: f: a string containing a file name. _extra_files: Map from filename to contents which will be stored as part of 'f'. )rG_save_for_mobilers r-_save_for_lite_interpreterz0RecursiveScriptModule._save_for_lite_interpreters ,477++Tr;rWs r-rz#RecursiveScriptModule.original_namesGDzS!5!5!788tww}}++-. .r/c~tjd}|jj|j||y)Nr#r)rrrG_definer)rHr r!s r-rzRecursiveScriptModule.defines. AAANC GGOOD//c :r/cd|jvr td|jrt||S||j vr|j |S|j j|r|j j|S|j j|r,|j j|}||j|<|St||S)NrzKScriptModule has not been initialized, did you forget to call super's init?) r?r[rrnrrrGrAr:rrC)rHrrr>s r-rz!RecursiveScriptModule.__getattr__!sdmm3"a!!w*400t}}$}}T**&wwt,,$$T* $ 3 3D 9 '4 d#$$7&t, ,r/c|jrt| ||S||jvr||j|<y|jj |r|jj ||yt |dr<||jjjvrtd|d|dt| ||S)Nrz+Cannot mutate TorchScript constant value: 'z '. Value: '') rrnrrrGrAr`r get_constantsrPAttributeErrorrs r-rz!RecursiveScriptModule.__setattr__9s!!w*477t}}$&+ d#&e,./D//==?DDFF%A${SXRYYZ[w*477r/ctjjjt j|j Sr4)rorrrcopyrGrWs r-__copy__zRecursiveScriptModule.__copy__Ts*99''77 $''8JK Kr/ctjjjt j |j |Sr4)rorrrrddeepcopyrG)rHmemos r- __deepcopy__z"RecursiveScriptModule.__deepcopy__Ws,99''77 dggt8TU Ur/crt||}t|ddtt|k(rt||i|S)N__func__)r:r/NotImplementedErrorrs r-rz*RecursiveScriptModule.forward_magic_method^sC!$ 4K{J5%{:*)// /r/c$|jdS)NrrrWs r-rzRecursiveScriptModule.__iter__fs,,Z8 8r/c&|jd|S)Nrgrn)rHidxs r-rgz!RecursiveScriptModule.__getitem__is,,]C@ @r/c$|jdS)NrXrnrWs r-rXzRecursiveScriptModule.__len__ls,,Y7 7r/c&|jd|S)Nrdrn)rHkeys r-rdz"RecursiveScriptModule.__contains__os,,^SA Ar/c|j}|jttdk(rt|S|S)N__dir__)rurkr<r/rn)rHrr>s r-ruzRecursiveScriptModule.__dir__ts=,,K$$*+@)LMw((= r/cd|j}|jttdk(ry|S)NrT)rrkr<r/)rHrs r-rzRecursiveScriptModule.__bool__s2--K$$*+@*MN= r/cdd}tj|jj|S)Ncyr4r5r:s r-r4zCRecursiveScriptModule._replicate_for_data_parallel..init_fnsr/)r/r6rGr#)rHr4s r-r#z2RecursiveScriptModule._replicate_for_data_parallels. )33446 r/)%rhrirjr rrI staticmethodr6r3rArrrrrrHrMrPrRrTrVrXrrrrrerirrrgrXrdrurr#r~rs@r-r/r/Vs 0 $ &  !  !,  0  0 3@  8  8  .  .  %  %  *  * 2 = G ; - 9 A  /  / ; -0 86 L V 0 9 A 8 B  ! ! r/r/__c:ddlj|fdS)NrcPj|xsj|Sr4) isfunctionismethod)xinspects r-rz_get_methods..s(%7W%7%7%:%Q>Ng>N>Nq>Qr/) predicate)r getmembers)r,rs @r- _get_methodsrs#"w!! Q  r/>%tocpucudaevalhalfr2applyfloattrain_applydoublebuffersrr@children _get_name zero_grad add_modulerV parameters state_dict share_memory _slow_forward _tracing_name named_buffers named_modules_named_membersnamed_childrenget_extra_stateload_state_dictregister_bufferregister_moduleset_extra_statenamed_parametersregister_parameter_save_to_state_dict_load_from_state_dictcfd}|S)Nc tdz)Nz" is not supported on ScriptModulesrZ)rHrrr;s r-failz_make_fail..failst&JJK Kr/r5)r;rs` r- _make_failrs L r/ _call_implc eZdZy)rNrr5r/r-rrs r/c eZdZdfd ZxZS)r|c"t|yr4rrHargr>s r-rIzScriptModule.__init__rr/r4rrs@r-r|r|   r/c eZdZdfd ZxZS)r/c"t|yr4rrs r-rIzRecursiveScriptModule.__init__rr/r4rrs@r-r/r/rr/c^t|tjjs|St |}||vr|t |St |dr|j n|}|||<i}|jjD]\}}|dk(r-|jD]\}}t||||<|||<8t|tjjr t|tst||||<||||<|jD]\}}||j<|S)N__prepare_scriptable__r) r{ronnridrArr?rL!call_prepare_scriptable_func_implr|)rrhobj_id new_obj_dictr; sub_modulerNrOs r-rrs9 c588?? + WF~BsG})05M(N""$TW DLLLL..0 ,j : "((* K1 A!T J 1  K!+L   EHHOO 4Z  > "C:t!TL !+L  ,""$1 T Jr/ci}t||Sr4)r)rrhs r-call_prepare_scriptable_funcrs')D ,S$ 77r/c@tjj|S)a Create a ``torch._C.ScriptDict`` instance with the data from ``obj``. Args: obj (dict): The Python dictionary that is used to initialize the ``ScriptDict`` returned by this function. Returns: An instance of ``torch._C.ScriptDict`` that has the same data as ``obj`` and can be passed between Python and TorchScript with reference semantics and zero copy overhead. )rorp ScriptDict)rs r-create_script_dictrs 88  s ##r/c@tjj|S)a Create a ``torch._C.ScriptList`` instance with the data from ``obj``. Args: obj (dict): The Python list that is used to initialize the ``ScriptList`` returned by this function. Returns: An instance of ``torch._C.ScriptList`` that has the same data as ``obj`` and can be passed between Python and TorchScript with reference semantics and zero copy overhead. )rorp ScriptList)r type_hints r-create_script_listr,s 88  s ##r/T _TOPLEVELexample_inputsc|tjdtdt|tr|St|t r|St|t r|S|rtatrtt}t|5t|tr%|jD]\}}|D]}|| n(t|tr |D]} ||  n tddddntjdt|tj j"rWt%|}tj&j(j+|tj&j(j,St/|dr|j1n|}t|tr t3|St|tr t5|St7j8|rt;|} t=|tj j"rt?d|dt=|t@jBr|StE|s t?d tG|jId kDr t?d |tKjL|d z}tO||| |St7jP|st7jR|r t;|} t/|d r!|jT}tKjV|}t/|drt?d|jXzt[|t]|} | r || _/| Sta||jb} |tKjV|}tjdjg| | |ti|} |jj| _5d| _1d| _6|| _/to|| | Stj&j(jq|S#1swY xYw)Nz^`optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution()` instead) stacklevelzError: Unable to infer types. Please format the inputs to type `List[Tuple]` or `Dict[Callable, List[Tuple]]` to be run with MonkeyType.zWarning: monkeytype is not installed. Please install https://github.com/Instagram/MonkeyType to enable Profile-Directed Typing in TorchScript. Refer to https://github.com/Instagram/MonkeyType/blob/master/README.rst to install MonkeyType. rzType 'zO' cannot be compiled since it inherits from nn.Module, pass an instance insteadzLTorchScript classes must be new-style classes. Please inherit from 'object'.rz\TorchScript classes does not support inheritance yet. Please directly inherit from 'object'.r#__script_if_tracing_wrapper__script_unsupportedzTorchScript error: r%r&)9warningswarn FutureWarningr{rr|r%rr7rr dictrLlist ValueErrorrorrrrrrrrArrrrisclassr issubclassr[enumEnumrCrVmrorrrr}r~ __original_fn#createResolutionCallbackFromClosurer"_check_directly_compile_overloadedr_torchdynamo_inlinerrhrp_jit_script_compilerr rjrcreate_script_class)roptimize _frames_uprrmonkeytype_configrs example_inputexampleexamplesqualified_namemaybe_already_compiled_fnrrs r- _script_implr>s  A   #+, #|$ #~&  *+  2= A !"34 nd3 2@1E1E1G-- '4-G"G,-- 5$2'X'%W  & MMi  #uxx'*3/yy##88 %%>>  s45  & & ( #t!#&&#t!#&&s(- c588?? +lm  c499 %J"3'0  swwy>A 9  < BB:PQ>RD#C~>   C G$4$4S$9(- 35 6##C DDSID 3. /4s7O7OOP P*3/$@$E! $>Pcts|S t}dat|||dz||}|rtdt |||aS#awxYw)aScript the function. Scripting a function or ``nn.Module`` will inspect the source code, compile it as TorchScript code using the TorchScript compiler, and return a :class:`ScriptModule` or :class:`ScriptFunction`. TorchScript itself is a subset of the Python language, so not all features in Python work, but we provide enough functionality to compute on tensors and do control-dependent operations. For a complete guide, see the :ref:`language-reference`. Scripting a dictionary or list copies the data inside it into a TorchScript instance than can be subsequently passed by reference between Python and TorchScript with zero copy overhead. ``torch.jit.script`` can be used as a function for modules, functions, dictionaries and lists and as a decorator ``@torch.jit.script`` for torchscript-classes and functions. Args: obj (Callable, class, or nn.Module): The ``nn.Module``, function, class type, dictionary, or list to compile. example_inputs (Union[List[Tuple], Dict[Callable, List[Tuple]], None]): Provide example inputs to annotate the arguments for a function or ``nn.Module``. Returns: If ``obj`` is ``nn.Module``, ``script`` returns a :class:`ScriptModule` object. The returned :class:`ScriptModule` will have the same set of sub-modules and parameters as the original ``nn.Module``. If ``obj`` is a standalone function, a :class:`ScriptFunction` will be returned. If ``obj`` is a ``dict``, then ``script`` returns an instance of `torch._C.ScriptDict`. If ``obj`` is a ``list``, then ``script`` returns an instance of `torch._C.ScriptList`. **Scripting a function** The ``@torch.jit.script`` decorator will construct a :class:`ScriptFunction` by compiling the body of the function. Example (scripting a function): .. testcode:: import torch @torch.jit.script def foo(x, y): if x.max() > y.max(): r = x else: r = y return r print(type(foo)) # torch.jit.ScriptFunction # See the compiled graph as Python code print(foo.code) # Call the function using the TorchScript interpreter foo(torch.ones(2, 2), torch.ones(2, 2)) .. testoutput:: :hide: ... ****Scripting a function using example_inputs** Example inputs can be used to annotate a function arguments. Example (annotating a function before scripting): .. testcode:: import torch def test_sum(a, b): return a + b # Annotate the arguments to be int scripted_fn = torch.jit.script(test_sum, example_inputs=[(3, 4)]) print(type(scripted_fn)) # torch.jit.ScriptFunction # See the compiled graph as Python code print(scripted_fn.code) # Call the function using the TorchScript interpreter scripted_fn(20, 100) .. testoutput:: :hide: ... **Scripting an nn.Module** Scripting an ``nn.Module`` by default will compile the ``forward`` method and recursively compile any methods, submodules, and functions called by ``forward``. If a ``nn.Module`` only uses features supported in TorchScript, no changes to the original module code should be necessary. ``script`` will construct :class:`ScriptModule` that has copies of the attributes, parameters, and methods of the original module. Example (scripting a simple module with a Parameter): .. testcode:: import torch class MyModule(torch.nn.Module): def __init__(self, N, M): super().__init__() # This parameter will be copied to the new ScriptModule self.weight = torch.nn.Parameter(torch.rand(N, M)) # When this submodule is used, it will be compiled self.linear = torch.nn.Linear(N, M) def forward(self, input): output = self.weight.mv(input) # This calls the `forward` method of the `nn.Linear` module, which will # cause the `self.linear` submodule to be compiled to a `ScriptModule` here output = self.linear(output) return output scripted_module = torch.jit.script(MyModule(2, 3)) Example (scripting a module with traced submodules): .. testcode:: import torch import torch.nn as nn import torch.nn.functional as F class MyModule(nn.Module): def __init__(self) -> None: super().__init__() # torch.jit.trace produces a ScriptModule's conv1 and conv2 self.conv1 = torch.jit.trace(nn.Conv2d(1, 20, 5), torch.rand(1, 1, 16, 16)) self.conv2 = torch.jit.trace(nn.Conv2d(20, 20, 5), torch.rand(1, 20, 16, 16)) def forward(self, input): input = F.relu(self.conv1(input)) input = F.relu(self.conv2(input)) return input scripted_module = torch.jit.script(MyModule()) To compile a method other than ``forward`` (and recursively compile anything it calls), add the :func:`@torch.jit.export ` decorator to the method. To opt out of compilation use :func:`@torch.jit.ignore ` or :func:`@torch.jit.unused `. Example (an exported and ignored method in a module):: import torch import torch.nn as nn class MyModule(nn.Module): def __init__(self) -> None: super().__init__() @torch.jit.export def some_entry_point(self, input): return input + 10 @torch.jit.ignore def python_only_fn(self, input): # This function won't be compiled, so any # Python APIs can be used import pdb pdb.set_trace() def forward(self, input): if self.training: self.python_only_fn(input) return input * 99 scripted_module = torch.jit.script(MyModule()) print(scripted_module.some_entry_point(torch.randn(2, 2))) print(scripted_module(torch.randn(2, 2))) Example ( Annotating forward of nn.Module using example_inputs):: import torch import torch.nn as nn from typing import NamedTuple class MyModule(NamedTuple): result: List[int] class TestNNModule(torch.nn.Module): def forward(self, a) -> MyModule: result = MyModule(result=a) return result pdt_model = TestNNModule() # Runs the pdt_model in eager model with the inputs provided and annotates the arguments of forward scripted_model = torch.jit.script(pdt_model, example_inputs={pdt_model: [([10, 20, ], ), ], }) # Run the scripted_model with actual inputs print(scripted_model([20])) Fr#)rrrrrscript)model_id)rrrr r)rrrrrprevrets r-rrs[`   !A~)    !(]35G H D s 4AAc|jD]>\}}||vs |||k7stjjj |d|y)NzDefault parameters on overloads do not affect the runtime so they must equal to the default parameter on the implementation function. Found on parameter )rLrorfrontend FrontendError) impl_defaultsoverload_defaultslocr;overload_values r-_check_overload_defaultsrsb 1 7 7 9n } $ d(;~(M))$$22!F$ r/ct||jj}tjj j |ddtj|}t||j}t|}t|}tj|}t|||jtjj||||||} | Sr4)rrhdeclror annotations get_signaturerr~rrrrrangerp_jit_script_compile_overload) overload_fn qual_nameimpl_fn overload_decloverload_signatureimpl_astrimplementation_defaultsrrs r-_compile_function_with_overloadrs [-A-ABGGIM..<<T4!1!1+!>7G$4$45H(5.w7  < $I(::9E#$$ ""  F FzSV W  0  ( YDL ,|; C.%%i0 sBcxt|}tj|s t|rt d|dy)Nz Function z cannot be directly compiled because it is overloaded. It must be used in a context of a function where its inputs can determine which overload to call.)rrrrr[)rrs r-rrsI$I&&y15RSV5W {#F F  6Xr/ctj|s tdt|s tdt |t j jxrt|jdk(}|s't|jdkDr tdt|}tjd}t||j}t jj!||||}||_|S)aVDecorate to annotate classes or modules of different types. This decorator can be used to define an interface that can be used to annotate classes or modules of different types. This can be used for to annotate a submodule or attribute class that could have different types that implement the same interface, or which could be swapped at runtime; or to store a list of modules or classes of varying types. It is sometimes used to implement "Callables" - functions or modules that implement an interface but whose implementations differ and which can be swapped out. Example: .. testcode:: import torch from typing import List @torch.jit.interface class InterfaceType: def run(self, x: torch.Tensor) -> torch.Tensor: pass # implements InterfaceType @torch.jit.script class Impl1: def run(self, x: torch.Tensor) -> torch.Tensor: return x.relu() class Impl2(torch.nn.Module): def __init__(self) -> None: super().__init__() self.val = torch.rand(()) @torch.jit.export def run(self, x: torch.Tensor) -> torch.Tensor: return x + self.val def user_fn(impls: List[InterfaceType], idx: int, val: torch.Tensor) -> torch.Tensor: return impls[idx].run(val) user_fn_jit = torch.jit.script(user_fn) impls = [Impl1(), torch.jit.script(Impl2())] val = torch.rand(4, 4) user_fn_jit(impls, 0, val) user_fn_jit(impls, 1, val) z$interface must be applied to a classz1TorchScript interfaces must inherit from 'object'rrzmTorchScript interface does not support inheritance yet. Please directly inherit from 'object' or 'nn.Module'.r#)rrr[rCrrorrrVrrrrrrhrp_jit_script_interface_compile__torch_script_interface__)ris_module_interfacerr!rmangled_classnames r- interfacers` ??3 ABB s #NOO %S%((//:Rs3779~QR?R 3swwy>A#5 D  %S)N  9 9! >S"5&7C" Jr/ct|}tjj||}t j |}t |||Sr4)rrorp CallStackr'createResolutionCallbackForClassMethodsr)rr _qual_name error_stackr!s r-_recursive_compile_classrFsC %J(($$Z5K  ? ? DC &sC <>A  4>>1G w/ /GGHLM93c$56MM4;;5t;;Ns%#C.N)r) rhrirjrIr)rIrr$r*r5r/r-rrYs4's's'' "c"#"z-_ScriptProfile.dump_string..s'TtTSS)9%::Ts02zLine #Hitsz Time (ns)z Line Contentsrr#z )r? _dump_statssourcetext splitlinesminstarting_linenorVrrline_mapr$rcount duration_nsr-rr&r:r)rHr4 source_stats source_ref source_linesdedentr8 start_lineend_liner/r"hitstime_ns line_contentsstatsstattables r-r:z_ScriptProfile.dump_strings LL446 0L%,,.J%??,779LT|TTF6BCdDMCLC#335J!C $55H X6L)(3F'/D*;7G0!QGM ))+E$ >tT*%%dL 9J,KLyy#LLtzz|4OOD$*:*:*<=  >(w 6\8JE NN5,,. /3 04{{7##-Ds% F,c6t|jyr4)printr:rWs r-dumpz_ScriptProfile.dumps d !r/r+) rhrirjrIrArCrIr:rbr5r/r-r<r<s":$S$<"r/r<c|Jd|S)NzUnwrapping null optionalr5)rs r-_unwrap_optionalrds =444= Hr/zaten::_unwrap_optionalzaten::is_scriptingzaten::has_torch_functionr4)NrNN)rrF)r  collectionsrdrrrr)rtypingrrrrotorch._jit_internalrtorch._classesrrrtorch._utils_internalr torch.jit._builtinsr torch.jit._fuserr r torch.jit._monkeytype_configr rrtorch.jit._recursiverrrrtorch.jit._staterrrrrtorch.jit.frontendrrrtorch.nnrtorch.overridesrrr torch.packager r! torch.utilsr"_serializationr$r7rpr?rXr%rhrjr. __reduce__ namedtupler0r8r<rCrErlr2rrWarningrrrrIrr_magic_methodsrrrr`r|r/r?rLr;itemcallabler{r startswithrAr_compiled_methods_allowlistrmethodendswithrrrrrboolrrtuplerrrrrrrrrrr)rrr-r<rd is_scriptingr5r/r-rsk  '' +">71A  PO ;"1"# ":$.!((+8 >;' A$ & &&{Wf4EFII X $ C( " "F&'*&'b;-;-|++  G +(((''14' XX__'0 N>=C=C~&D  K $k?C DSBvSBj| |F ,44::<* d~jx&@  ??4 GL$$?   dD)* &#P %UXX__5N f ??4 DMM,$?  -66 677 )6??Jt