a
    8Sicz                     @   s   d dl Z d dlmZmZmZmZmZmZ d dlZd dlm	Z	 dgZ
ddddZdd	 Zeedd
ddZe jdeee	f ed dddZed dee eedddZddeee	f eeeef dddZdS )    N)AnyCallableDictIteratorListTuple)Tensorfunctional_call)returnc                    sj   | j | j ttd fdd}ttd d fdd}tdj f||d}|| _ | _d S )	N)namer
   c                    s    | v r |  S  | |S N)__getattribute__)selfr   Zattr_to_pathclsparams_and_buffers T/var/www/html/django/DPS/env/lib/python3.9/site-packages/torch/nn/utils/stateless.py_getattribute   s    z$_change_class.<locals>._getattribute)r   valuer
   c                    s(   | v r| | < n | ||S d S r   )__setattr__)r   r   r   r   r   r   _setattr   s    z_change_class.<locals>._setattrZStatelessReplacer)r   r   )	__class___attr_to_pathstrr   type__name___orig_class)moduler   r   r   	param_clsr   r   r   _change_class   s    
	r    c                    s   t t td d fdd}|S )N)tensor_name	full_pathtensorr
   c                    s4   t | dr|| j|< ni | _|| j|< t|   d S )Nr   )hasattrr   r    )r   r!   r"   r#   r   r   r   _swap_parameters'   s
    

z-_create_swap_params.<locals>._swap_parameters)r   r   )r   r&   r   r%   r   _create_swap_params&   s    	r'   )r   r"   r
   c                 C   s*   t | dr&| j| _t| d t| d d S )Nr   r   )r$   r   r   delattr)r   r   r"   r   r   r   _remove_swap3   s    

r)   ztorch.nn.Module)r   parameters_and_buffersr
   c                 c   s   |  D ]$\}}tt|| |d||f qz*d V  W |D ]}tt| |d|d q<n"|D ]}tt| |d|d q^0 d S )N.r   )items_apply_func_submodulesr'   splitr)   )r   r*   r   r#   r   r   r   _reparametrize_module:   s"    r/   ).Nfuncr   pathr"   argsc                 C   sL   t |dkr&| ||d |g|R   n"t| t||d |dd  || d S )N   r   )lenr-   getattrr0   r   r   r   r-   L   s    r-   )r   r*   r3   kwargsc                 C   s   t j s0t j s0t| t jjt jjt jjfr8td|du rDi }t	| |: t|t
rj| |i |}n| |fi |}W d   n1 s0    Y  |S )a`  Performs a functional call on the module by replacing the module parameters
    and buffers with the provided ones.

    .. 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(())}
            >>> 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.)

    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 (tuple): arguments to be passed to the module call
        kwargs (dict): keyword arguments to be passed to the module call

    Returns:
        Any: the result of calling ``module``.
    z3The stateless API can't be used with Jitted modulesN)torchjit
is_tracingis_scripting
isinstanceRecursiveScriptModuleScriptModuleScriptFunctionRuntimeErrorr/   tuple)r   r*   r3   r7   outr   r   r   r	   Y   s$    '	
.)N)
contextlibtypingr   r   r   r   r   r   r8   r   __all__r    r'   r   r)   contextmanagerr/   r-   r	   r   r   r   r   <module>   s2    
 

