L i-ddlZddlmZmZmZddlmZddlZddlmZddl m Z dgZ ddd e e efd e e effd Zej dddd e e efd ededef dZede ddd dddd e e efdeeeefdee e efd edef dZ ddd dddd e e efdeeeefdee e efd edef dZy)N)AnyOptionalUnion) deprecated)Tensor)NamedMemberAccessorfunctional_callmoduleztorch.nn.Moduleparameters_and_buffersreturnci}|j|jd|j|jdi}|jD]*\}}||vr t ||<||j |,i}|j D]}t|dkDs|D]}|||< t |j} t } | D]}||vs| j || D]\} || }t|j| dkDs&t|Dchc]}|| c}dk7sFtdt|d|j} | D]} || D] }|| | |< | Scc}w)a Unties all tied tensors in the module to parameters_and_buffers. This function returns a new untied_parameters_and_buffers dictionary and leave the original untied_parameters_and_buffers dictionary unchanged. It adds new (missing) keys for tied tensors in the module to untied_parameters_and_buffers. The value of the new key is the user-given value in the original parameters_and_buffers dictionary. If there are more than one user-given values for the same tied tensor, it will raise an error. For example, if the module has two tied weights self.foo and self.tied_foo and the user passes {'foo': foo_value, ...}, this will return {'foo': foo_value, 'tied_foo': foo_value, ...}. If the user passes {'foo': foo_value, 'tied_foo': tied_foo_value, ...}, it will raise an error. If the user passes {'foo': foo_value, 'tied_foo': foo_value, ...}, it will not raise an error. Args: module (torch.nn.Module): the module to determine which tensors are tied. parameters_and_buffers (Dict[str, Tensor]): a map of {name: tensor} for reparamaterizing the module. Returns: A new untied version of the parameters_and_buffers dictionary. Raises: ValueError: if there are more than one user-given values for the same tied tensor. F)remove_duplicatez-functional_call got multiple values for keys z2, which are tied. Consider using tie_weights=False) updatenamed_parameters named_buffersitemssetaddvalueslenkeys intersection ValueErrorsortedcopy) r r all_named_tensorstensor_to_tied_names_mapnametensortied_names_map tied_names tied_name given_namesgiven_names_for_tied_tensors given_nameuntied_parameters_and_bufferss ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/nn/utils/stateless.py_untie_named_tensors_mapr)s<,.V44e4LMV1151IJ8:)//13 f 1 1/2u $V , (,,T23+-N.5577 z?Q ' 7 ,6y) 77 ,1134K.1U 3 > ! ( , ,T 233  #J/   ''(DE F J S9+I6ST?z@R?STCD  "%;$?$?$A!2 ' 3 I7M8 )) 4  )(!Ts' F F tie_weightsstrict stack_weightsc #K|}|}|r t||}n|}t|}|r|j|\}}g} t|dkDr2| j ddj t t|dt|dkDr2| j ddj t t|dt| dkDr8tdj|jdj | i} |j|d \} } d|r"tt| j} |j| d \} } |j|D cic] } | | vr| | | c} ycc} w#|r"tt| j} |j| d \} } |j|D cic] } | | vr| | | ncc} wc} wxYww) NrzUnexpected key(s): z, .zMissing key(s): z'Error(s) in reparametrizing for {}: {}z T) allow_missing)r)r check_keysrappendjoinmaprepr RuntimeErrorformat _get_nameswap_tensors_dictdictreversedrr)r r r*r+r,r'accessor missing_keysunexpected_keys error_msgsorig_parameters_and_buffers_new_parameters_and_buffersks r(_reparametrize_modulerCbsH4!M(@ *) %)?%"6*H (0(;(; )) % o  ! #   %diiD/0J&K%LAN  | q    03t\;R1S0TTUV W z?Q ;BB$$& J(?  68 )1)C)C )*D* &#Q  *.4::<=+ ')1(B(B 't)C) %"A %%0 22-a00    *.4::<=+ ')1(B(B 't)C) %"A %%0 22-a00   s8DG1FA G1'F8 G1A G.G! G..G1z`torch.nn.utils.stateless.functional_call` is deprecated as of PyTorch 2.0 and will be removed in a future version of PyTorch. Please use `torch.func.functional_call` instead which is a drop-in replacement.)categoryTr*r+argskwargsc$t||||||S)a Perform a functional call on the module by replacing the module parameters and buffers with the provided ones. .. warning:: This API is deprecated as of PyTorch 2.0 and will be removed in a future version of PyTorch. Please use :func:`torch.func.functional_call` instead, which is a drop-in replacement for this API. .. note:: If the module has active parametrizations, passing a value in the :attr:`parameters_and_buffers` argument with the name set to the regular parameter name will completely disable the parametrization. If you want to apply the parametrization function to the value passed please set the key as ``{submodule_name}.parametrizations.{parameter_name}.original``. .. note:: If the module performs in-place operations on parameters/buffers, these will be reflected in the `parameters_and_buffers` input. Example:: >>> a = {'foo': torch.zeros(())} >>> # xdoctest: +SKIP >>> mod = Foo() # does self.foo = self.foo + 1 >>> print(mod.foo) # tensor(0.) >>> functional_call(mod, a, torch.ones(())) >>> print(mod.foo) # tensor(0.) >>> print(a['foo']) # tensor(1.) .. note:: If the module has tied weights, whether or not functional_call respects the tying is determined by the tie_weights flag. Example:: >>> a = {'foo': torch.zeros(())} >>> # xdoctest: +SKIP >>> mod = Foo() # has both self.foo and self.foo_tied which are tied. Returns x + self.foo + self.foo_tied >>> print(mod.foo) # tensor(1.) >>> mod(torch.zeros(())) # tensor(2.) >>> functional_call(mod, a, torch.zeros(())) # tensor(0.) since it will change self.foo_tied too >>> functional_call(mod, a, torch.zeros(()), tie_weights=False) # tensor(1.)--self.foo_tied is not updated >>> new_a = {'foo': torch.zeros(()), 'foo_tied': torch.zeros(())} >>> functional_call(mod, new_a, torch.zeros()) # tensor(0.) Args: module (torch.nn.Module): the module to call parameters_and_buffers (dict of str and Tensor): the parameters that will be used in the module call. args (Any or tuple): arguments to be passed to the module call. If not a tuple, considered a single argument. kwargs (dict): keyword arguments to be passed to the module call tie_weights (bool, optional): If True, then parameters and buffers tied in the original model will be treated as tied in the reparamaterized version. Therefore, if True and different values are passed for the tied parameters and buffers, it will error. If False, it will not respect the originally tied parameters and buffers unless the values passed for both weights are the same. Default: True. strict (bool, optional): If True, then the parameters and buffers passed in must match the parameters and buffers in the original module. Therefore, if True and there are any missing or unexpected keys, it will error. Default: False. Returns: Any: the result of calling ``module``. rE)_functional_callr r rFrGr*r+s r(r r s$T    c.tjjsutjjsWt |tjj tjj tjjfr tdt |tjjr td|i}|d}nt |ts|f}t||||5||i|cdddS#1swYyxYw)Nz3The stateless API can't be used with Jitted modulesz;The stateless API can't be used with nn.DataParallel modulerE) torchjit is_tracing is_scripting isinstanceRecursiveScriptModule ScriptModuleScriptFunctionr5nn DataParalleltuplerCrJs r(rIrIs   99 ! ! #   // && ((  PQQ&%((//0 I  ~ | e $w &K 't&v&'''s 9D  D)FFF)NN) contextlibtypingrrrtyping_extensionsrrNr%torch.nn.utils._named_member_accessorr__all__r9strr)contextmanagerboolrC FutureWarningrXr rIrMrKr(rbs''( E  Q) Q) f-Q) #v+Q)h  < <  f-< <   <  < < ~ V )-'+ K K K f-K 5e$ %K T#s(^ $ K  K K  Kb)-'+ $' $' $' f-$' 5e$ %$' T#s(^ $ $'  $' $'rK