L i ddlZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z ddlmZddlmZmZmZddlmZmZmZmZddlmZej6dd Zej6d d Zgd Zd ZdZ dZ!dZ"dZ#e$e%e&e'e(dejRejTejVejXf Z-dZ.Gddej^j`jbZ2dZ3d/dZ4GddZ5e5Z6dZ7dZ8d/dZ9dZ:d0dZ;dZd!Z?d"Z@d#ZAd$ZBd%ZCd&ZDd'ZEd(ZFd)ZGd*ZHd+ZId,ZJd-ZKd.ZLy)1N) fake_range) _find_builtin)AttributeTypeIsSupportedChecker)_add_script_class_get_script_class _python_cu)get_class_propertiesget_default_argsget_jit_class_def get_jit_def)ModuleScriptMethodStub)resolution_callbackdef_original_method PropertyStub)rr)_version _parameters_buffers_non_persistent_buffers_set_backward_hooks_backward_pre_hooks_forward_hooks_forward_hooks_with_kwargs_forward_pre_hooks_forward_pre_hooks_with_kwargs_forward_hooks_always_called_state_dict_hooks_state_dict_pre_hooks_load_state_dict_pre_hooks_load_state_dict_post_hooks_modules _initializing dump_patchesct|}|smt||j}tjj j |}tjj||||}t|||SN) rr __name__torchjitfrontendget_default_args_for_class_C_jit_script_class_compiler)objrcbqualified_name script_classastdefaultss Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/jit/_recursive.py_compile_and_register_classr56sh$S)L S\\299%%@@Exx99 C3  #|, cbtj|}t||d}t|||SNRecursiveScriptModule self_name) _jit_internal#createResolutionCallbackFromClosurer r)funcnamer/r2s r4 make_stubr@Ds0  ; ;D AC dD,C DC Cd ++r6cVt||}t|tr|St||Sr&)getattr isinstancerr@) nn_module method_namer>s r4make_stub_from_methodrFJs- 9k *D$()  T; ''r6cg}t|D]Z}t||d}tj|tjj us@|j t||\|Sr&)dirrBr<get_torchscript_modifierFunctionModifiersEXPORTappendrF)modstubsr?items r4 make_stubs_from_exported_methodsrPYsg EC;sD$'  2 24 8..55 6 LL.sD9 : ; Lr6ct|dg}d}|t|}t}|D]}||vs|j||S)N__jit_ignored_attributes__ct|jDchc]\}}t|ts|c}}Scc}}wr&)varsitemsrCproperty)modulekvs r4get_properties_namesz4jit_ignored_properties..get_properties_namesks0"6l002NdajH6MNNNs>>)rBtypesetadd)rW!user_annotated_ignored_attributesrZ propertiesuser_annoted_ignored_properties ignored_attrs r4jit_ignored_propertiesrbfs`(/,b)%O&d6l3J&)e#9> : % + / / => +*r6c Ft|tr|St|ttfrtfd|DSdj dtD}t t jdtjt|ddd|d ) Nc38K|]}t|ywr&)_get_valid_constant).0xattr owner_types r4 z&_get_valid_constant..sI!(q*=Isz, c3FK|]}tj|ywr&)r(typename)rftyps r4rjz&_get_valid_constant..sI#%..-Is!z 'z' object in attribute '.zo' is not a valid constant. Valid constants are: 1. a nn.ModuleList 2. a value of type {z,} 3. a list or tuple of (2) ) rC_constant_typestuplelistjoin TypeErrortextwrapdedentr(rlr[)rhrYri constantss` ` r4reres!_% At} %IqIII IIII  ..a ! ""9*QtfM([)     r6ceZdZfdZxZS) SourceContextc*t|||||yr&)super__init__)selfsourcefilename file_linenoleading_whitespace_len __class__s r4r{zSourceContext.__init__s ;8NOr6)r' __module__ __qualname__r{ __classcell__)rs@r4rxrxsPPr6rxctjdkr t|diStj|}|r|Sfdt |t r|n t |}|S)N) __annotations__cxtj|}|r|S|jD]}|}|s|cSiSr&)inspectget_annotations __bases__)clscls_annotationsbaseget_cls_annotationss r4rz,get_annotations..get_cls_annotationssI!11#6 " "MM 'D1$7O&& ' r6)sys version_inforBrrrCr[)r. annotationsrrs @r4rrsb '!s-r22 ))#.KC&#DIC s ##r6c Jtjjt|}t |tj j r|jt |tj jtj jfr|jt |tj jr|jt |tj jr|jt|t |tj j"j$rit'|dg}|j)|t+|}fd}t-}|j.j1D]g\}}||vr |t |tj2sJ|||\} } |j5|| jdd|j7|i|j8j1D]g\}}||vr |t |tj2sJ|||\} } |j5|| jdd|j7|i|j:j1D]\}}||vr |||\} } |#|j5|| jdd<| j=rX| jj?sJtjj@jC| j} n tE||} |jG|| |j7|t-t'|dd} j1D]6\}} tjHjK| s&| j7|8| D]}||vr[||j:vrd}n-||j8vrd }n||j.vrd }n tMd tOjPd |d |dbtS||stOjPd |dt'||}|jU|tW||t|jX|j7|t'|di}|j[t]t_|||j1D]\}}|ja|||jbj1D]\}}|tdvs|jgdr!||vr&||vr+t |tjhjj}|r |jl}tojp|r_ tjrju|}|jw|tjjy|j|t|}|r|j||t |tjrjr@|jw|tjjy|j|U|||\} }| j=r$|j5|| jdd|rdnd}| jd|}dtjt|d|d}|j}|||jjD]}|j||jjD]}|j||S#tz$r"}d|}|j}||Yd}~d}~wwxYw)z Build a ConcreteModuleTypeBuilder from an nn.Module. This ConcreteModuleType doesn't have a JIT type associated with it yet, it must be filled in by the caller. rRc d} |vr|tjjjdk7rUtjj j |t}tjj|}nt|tjjr\tjj j |jt}tjj|}n!tjj|}d}||fS#t$r}td|d|d||d}~wwxYw)NFforwardTzError inferring type for z: )r(nnr rr)r ann_to_typerr, InferredTyperC Attributer[_jit_try_infer_type RuntimeError)r?rOinferredr attr_typereclass_annotationss r4 infer_typez/infer_concrete_type_builder..infer_types   Y))%d+88??229=>$ii33??%d+Z\ "HH11+> D%))"5"56#ii33?? :<X !HH11+> !HH88> ("" Y!:4&4&2$OPVX X YsD'D00 E9EENTF __constants__ submodulebuffer parameterz3added_names must be submodule, parameter, or buffer'zA' was found in ScriptModule constants, but it is a non-constant z. Consider removing it.zb' was found in ScriptModule constants, but was not actually set in __init__. Consider removing it. __overloads____z(This function exists as an attribute on the Python module, but we failed to compile it to a TorchScript function. The error stack is reproduced here: zFIts type was inferred; try adding a type annotation for the attribute.z. zT(This attribute exists on the Python module, but we failed to convert Python type: 'z' to a TorchScript type. ))Ir(r,ConcreteModuleTypeBuilderr[rCr ModuleDictset_module_dict ModuleList Sequentialset_module_list ParameterListset_parameter_list ParameterDictset_parameter_dictrao quantization QuantWrapperrBadd_ignored_attributesrbr\rrUTensor add_attributer]rr"successis_interface_typeConcreteModuleType from_jit_typeget_module_concrete_type add_moduler<is_finalAssertionErrorwarningswarnhasattr add_constantrer'updateget_overload_name_mappingget_overload_annotations add_overload__dict__ignored_attributes startswith_opsOpOverloadPacketopr isfunctionr)scriptadd_function_attributer Exceptionadd_failed_attributeradd_builtin_functionScriptFunctionreasonrlrvaluesadd_forward_hookradd_forward_pre_hook)rD share_typesconcrete_type_builderr^ignored_propertiesr added_namesr?rOr_sub_concrete_type constants_setannhintvalue overloadsoverloaded_namesisoverloadpacket scripted_fnebuiltin_symbol_namer inferred_msgadditional_infohookpre_hookrs @r4infer_concrete_type_builderrs8"HH>>tIO)ehh113--/)ehh115883F3FGH--/)ehh446002)ehh446002' 2)ehh33@@B)0/)%001RS/ :#8%K++113  d 4 4 |z$ ===!$- 1 ++D)..2BD%P  ((..0 d 4 4 |z$ ===!$- 1++D)..2BE4P ((..0 d 4 4 !$- 1 < " / /inn6Fu U     >>#557 77 % ; ; I I !  !9{ K ((/@A+0 ?B?@M',,.$ c    ' ' ,   d #$! ; y)))"+++..."$I MMD6--1F2IK  y$' MMD6((   4(** %dE4 ?3K3KL  C!H ?B7I ! $Y0B C  #,//"3C**41ABC!))//1DC e % %)>  4 4  ;  %eUZZ-H-HI HHE   e $ G#ii..u5 %<<%((66{CHHJE ,E2  ! 6 6t=P Q  eUYY55 6 ! 8 8ehh2259>>@%  )u5 8     ! / /inn6Fu U Y  "+!1!1!3 4B|nEO::?..e:U9VW**9):!=  " 6 6tT BIDCN((//15..t4500779=228<= ! k G >>?SB &::4FF GsA]77 ^"^^"ceZdZUeeeeejjfe d<e ejje d<ddZ dZ y)ConcreteTypeStore type_storemethods_compiledNc0i|_t|_yr&)rr\r)r|s r4r{zConcreteTypeStore.__init__s #r6ct|}t|}||jvrg|j|<|j|}|D]}|j|s|cS|j }|j|j ||S)zaInfer a ConcreteType from this `nn.Module` instance. Underlying JIT types are reused if possible.)rr[requalsbuildrL)r|rDrnn_module_type known_types known_type concrete_types r4get_or_create_concrete_typez-ConcreteTypeStore.get_or_create_concrete_types ;I Fi  0.0DOON +oon5 % "J  !67!! " .335  '..}=r6)returnN)r'rrdictr[r rqr(r,rrr\r{rrr6r4rrsDT&\4(C(C#DDEE%((5566& r6rcl|Dcgc]}|j}}|Dcgc]}|j}}|Dcgc]}t|j}}|Dcgc]}|j}}|Dcgc]}|j} }|j || |||ycc}wcc}wcc}wcc}wcc}wr&)rrr r_create_methods_and_properties) r method_stubsproperty_stubsm method_defs method_rcbsmethod_defaultsp property_defs property_rcbss r4(create_methods_and_properties_from_stubsr s$00a1660K02>?Q1((?K?DPQq'(9(9:QOQ%34QVV4M44BCqQ**CMC00}k;1?Q4CsBB"B'B,2B1c|Dcgc]}|j}}|Dcgc]}|j}}|Dcgc]}|j}}|Dcgc]}|j}}|j||||ycc}wcc}wcc}wcc}wr&)rr _create_hooks)r hook_stubspre_hook_stubsh hook_defs hook_rcbs pre_hook_defs pre_hook_rcbss r4create_hooks_from_stubsrs!+,A,I,0:;1&&;I;%34QVV4M44BCqQ**CMC 9m]S -;4CsA:A?BB c,t|tsJt|tjjrt |dr |j S|rtj|}|St||}|j|j}|S)a Get a concrete type for nn_modules. If share_types is True, the concrete type is fetched from concrete_type_store. If it is False, a new concrete type is created without first searching concrete_type_store. Args: nn_module: The original Python nn.Module that we are creating a ScriptModule for. share_types = Whether to share underlying JIT types between modules (if possible). Returns: A concrete type for nn_module. _concrete_type) rCr r(r) ScriptModulerrconcrete_type_storerr set_poisonedr)rDrrrs r4rrs i (( ()UYY334#:'''+GG R   !.init_fn?s"002779 1D D1Jj%))*=*=>      tZ 0 1(5'@'@'B 4 #D# D1Jj&1 *4 +;*<= 1,44K+uxx'='=>+KDJ (>(>?%5 18   tX .+3M " "4 (% 4, N 3D))9dD1D%-*E*Ed*K#*9d#;#D#D /77 F  t\:33D9 tT2 3(5 $r6__len__zdef __len__(self): return  __contains__z3def __contains__(self, key: str): return key in z2def __contains__(self, key: str): return False N)3r(r,_create_module_with_typer3get_property_stubsget_hook_stubsrbr)r9 _constructrrr r_run_emit_module_hookr] enumerate_c_get_forward_pre_hooksr_get_forward_hooksrrCrrrr _method_namesdefinelenr1reprrqrr'rr? _get_method functoolswrapsr getter_name setter_namerVrHrBr<rIrJCOPY_TO_SCRIPT_WRAPPER!add_python_attr_to_scripted_model)rDrr+rrrrrBr;idxfnr1 method_stubr? script_methodwrapped_script_method property_stub property_namefgetrWfsetrOrArs``` @@r4r*r*0s22=3I3IJJI&L' 2N!/ !:J/ :0505fII33>>z7SM/@@@0 < z>J &&z2,,00?]--DDFG3R02 ((-3]--@@BC/R,. $$S)/  ++UXX-@-@%((BUBUV  Z557 7=c)n=MRPQ9ehh112 *":":"< < y~~ Y^^-./D  FtfBO   !W X$=  & & . **33 ;##((*// / "..t4 !M  0K0K L ! (= t$/=4(T %**//166 %%m&8&8&D&D&F&K&KL#((446 ;Fz%%k&6&67D08d0S }- TIN % % y$-  2 24 8..EE F .mY MN r6c|t||d}|yttjj|d}|y||k7S)NF)rBr(r)r9) script_modelrh script_attr default_attrs r4script_model_defines_attrrgsE,d3K599::D$GL , &&r6cft||r%t||rt||t||yyyr&)rrgr!rB)rdorigrhs r4rYrYs2tT8tL dGD$$78 Mr6ci}tt|D]}||vrt||d}t|s!t |ds.|j ;t j||j}|^|j|vr)tt jd|jtt|Dcgc]}|dzt|z}}tt!||||<|Scc}w)Nrmethodr)rHr[rBcallablerrr<_get_overloaded_methodsrr8r,get_overload_no_implementation_error_messagerangerQstrrqzip)rMrbrr?rOmethod_overloadsinamess r4rrsIDIA ) ) sD$'~  4 &4??+F,DDcmm   '}} 00"!NN $-- 49=M9N3OPaTD[3q6)PEP"3u.>#?@IdO/A2 QsC8ci}|jD]8\}}|j}||vrg||<|D]\}}||j|:|Sr&)rUr'rL) overload_infooverload_name_mappingsorig_fnr original_name overload_namers r4rrsy46+113H((  6 646 "= 1 ) H M1 "= 1 8 8 G H H "!r6ctjjj|dt t j |}|#tj|}td|y)Nz>Must explicitly add type annotations to overloaded functions: ) r(r)r get_signaturerrr6r<rr)r> signature qual_names r4_check_no_signaturer sf %%33 dJL'"2"24"8I!11$7 LYK X  r6c g}|jD]\}}t||jd}|D]\}}t|t||jd}tj j |j||}tj|} |jt| |||Sr8) rUr r'rr(r,_replace_overloaded_method_decldeclr<r=rLr) rvoverload_stubsrxrorig_astrz overload_fnover_astnew_ast_rcbs r4make_stubs_for_overloadsrsN+113 P W%%1H +4 P &M;  ,"[11=THhh>> =G!DDWMD  ! !"24+"N O P P r6c t|tjjsJt |ds+t dtj t|dt |ds|jjD]_\}}| tjjj|s3t dtj t|d|d|jjD]_\}}| tjjj|s3t dtj t|d|dyy)Nrrz=' has not been initialized, did you forget to call 'super()'?remote_parametersz' has uninitialized parameters z'. Did you forget to run a forward pass?z' has uninitialized buffers ) rCr(rr rrrlr[rrUris_lazyr)rMr?parambufs r4r(r(*s9 c588?? ++ + 3 &tCy)**g h  3+ ,??002 KD% UXX%7%7%?%?%F"tCy122QRVQWW~  ++- ID#588#5#5#=#=c#B"tCy122NtfT{|  -r6c^t|t|}g}t|drctj|j sDt |j dd}t tjjdd}||k7rdg}g}t|D]U}||vrt ||d}tj|tjjusE|j|W||z}tt |dit!||}j#t%|t'|} |_fd} t+| |} t-} g} | D])}|| vr| j|| j/|+| Dcgc]}t1||}}| |zScc}w)zImplement the default rules for which methods should act as starting points for compilation. (TODO add a link when the rules are published). rr8Nrc |vSr&r)rErws r4ignore_overloadedz3infer_methods_to_compile..ignore_overloadedes"888r6)r(rbrr<r7rrBr(rr rHrIrJrKrLrrrrrrfilterr\r]rF)rDrmethods forward_funcmodule_forwardexportedr?rOrvrrfiltered_methodsuniqueruniqued_methodsrkrNrws @r4infer_methods_to_compiler?s Y'/ :Gy)$]-H-H.y00*dC  )TB > ) kGHI" % % y$-  2 24 8..55 6 OOD !" G!')_b"IJ,Y8JKM!!";M"JK-mt d|jdt |jd|||j<|jt||jg}|jjD]}|j|vrSt |t ||jk7r>t d|jdt |jd|||j<|jt||j||fS)z3Return forward hook and pre_hook ScriptModuleStubs.zHook 'z' on zV has at least two different python definitions. Please use unique names for all hooks.z Pre-hook ') r(rrr'idrr[rLr@r)rDhook_maprrrrs r4rHrHxswY'HJ((//1 : ==H $$x2ht}}566"T]]O5i1I1I0JK>> '+HT]] #)D$--89 :N00779 F    ((|r(8+<+<"=>>" !2!2 35i9Q9Q8RS>> +3HX&& 'i(2C2CDE F ~ %%r6ct|}t|d}i}t|D]j}t||d}t |t s!|j std|d|jdtj|j ||<l|Dcgc])}t||jj|+}}|Scc}w)zjCreate property stubs for the properties of the module by creating method stubs for the getter and setter.r9r:Nz Property z of z must have a getter) r[r rHrBrCrVrarr'r<r=rr?)rD module_typroperties_astsrcbsr?rOr2rNs r4rGrGsYI*9@WXO DIVy$- dH %99"vT)*<*<)==PQ'JJ499UDJVBQ Q#\$sxxz/ 5 QE Q L Rs.C ct|tjjr|St |fd}t ||S)a  Make a ScriptModule from an nn.Module, using the interface methods rule for determining which methods to compile. Args: mod_interface: the interface type that the module have nn_module: The original Python nn.Module that we are creating a ScriptModule for. c`jDcgc]}t||}}|Scc}w)zRule to infer the methods from the interface type. It is used to know which methods need to act as starting points for compilation. )getMethodNamesrF)rDrkrN mod_interfaces r4"infer_interface_methods_to_compilez.infer_interface_methods_to_compiles>(668  ")V 4    s+)rCr(r)rr(r-)rrDrs` r4r5r5s<)UYY334Y'  +M NNr6ctj|ryt|tjj ryt j|s't j|std|d|dt|dr|jn|}tj|}tjj||S)N`z|` is not a function. Recursive scripting only supports Python functions or methods currently. Consider manually annotating `z` with @torch.jit.script.__prepare_scriptable__)r)r<r7rCr(rr rrr6rrrr=r)r)r[locr/s r4try_compile_fnrs""2&"ehhoo&   b !'*:*:2*>t--/D0I K  )04L(M " " $SUB  ; ;B ?C 99  BS  ))r6c@tjj|S)z;Wrap this torch._C.Object in a Python RecursiveScriptClass.)r(r)RecursiveScriptClass) cpp_classs r4r"r"s 99 ) )) 44r6c\d}tjjj||S)zYWrap this torch._C.ScriptModule in a Python ScriptModule, recursively for all submodules.c"tjj|jj D]\}}t ||t |tjjj|jj|_ t|jjD]\}}||j|<t|jjD]\}}||j|<yr&)r(r,rrLrUr!wrap_cpp_modulerr_typerrKrMrrNr)r;r?rArZr[s r4rBz wrap_cpp_module..init_fns % 3 3M4D4D E K K M F D* M4)D E F',xx'B'B'P'P    " " $( $!!1!1!H!H!JK 7GC46M , ,S 1 7 !1!1!D!D!FG 3GC02M ( ( - 3r6)r(r)r9rI)rArBs r4rrs& 3 99 * * 5 5j' JJr6ctj|ryt||j}tjj 5t ||fdddd|S#1swY|SxYw)Nr)r<r7r@r'r(_disable_emit_hooksr )rr[stubs r4compile_unbound_methodrsf""2& R %D    0 0 2M 1LM K M Ks A$$A.cfd}|_j|_tjj ||S)z Return a function that lazily binds `unbound_method` to a provided Module IValue, then invokes the method. We do this so that any Python shenanigans that will poison type sharing are impossible at compile time. cfd}tjjj||}t j |}||S)Ncj}t|D]2}t||d}tj|s&t |||4j jD]\}}t |||yr&)py_classrHrBr<r7r! get_constantsrU)r; orig_classr?rOrrs r4rBz7lazy_bind..lazy_binding_method..init_fns~&//JJ 7z46 ..t4M46 7 -::<BBD 4 e tU3 4r6)r(r)r9rItypes MethodType)rAargsrBr;rkrunbound_methods r4lazy_binding_methodz&lazy_bind..lazy_binding_methodsB 4 77BB:wW !!.-@t}r6) original_fnr'r(r<copy_torchscript_modifier)rrrs`` r4 lazy_bindrs?('5##1#:#:  11.BUV r6)T)TF)M collectionsrTrrrtrrr(torch._jit_internalr<torch._sourcesrtorch.jit._builtinsrtorch.jit._checkrtorch.jit._staterrrtorch.jit.frontendr r r r torch.nnr namedtuplerrrr5r@rFrPrbboolfloatintrpr[devicelayoutdtypeqschemerorer,_jit_tree_viewsSourceRangeFactoryrxrrrrr rrr&r-r*rgrYrrrrr(rrHrGr5rr"rrrrr6r4rs   +%-<MM *;))J&{%%n6UV , , ( +,  J LL LL KK MM &PEHH,,??P $2k!\<() TB&,I0Y~'9 @ " &*6"r&D(O6*85 K$ r6