
    Ǆgt,                     Z   d dl mZ d dlZd dlmZ  G d dej
                  j                        Z G d dej                  e      Z	 G d	 d
      Z
d Z G d de
e	      Z G d dej
                  j                        Z G d dej                  e      Z G d de
ej                        Zy)    )OrderedDictN)_disabled_torch_function_implc                        e Zd Z fdZ xZS )_ParameterMetac                     | t         u r(t        |t        j                        rt	        |dd      ryt
        |   |      S )N	_is_paramFT)	Parameter
isinstancetorchTensorgetattrsuper__instancecheck__selfinstance	__class__s     Z/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/nn/parameter.pyr   z _ParameterMeta.__instancecheck__
   s<    9(ELL1g+u7 w(22    __name__
__module____qualname__r   __classcell__r   s   @r   r   r          3 3r   r   c                   :     e Zd ZdZddZd Z fdZd ZeZ	 xZ
S )r	   a  A kind of Tensor that is to be considered a module parameter.

    Parameters are :class:`~torch.Tensor` subclasses, that have a
    very special property when used with :class:`Module` s - when they're
    assigned as Module attributes they are automatically added to the list of
    its parameters, and will appear e.g. in :meth:`~Module.parameters` iterator.
    Assigning a Tensor doesn't have such effect. This is because one might
    want to cache some temporary state, like last hidden state of the RNN, in
    the model. If there was no such class as :class:`Parameter`, these
    temporaries would get registered too.

    Args:
        data (Tensor): parameter tensor.
        requires_grad (bool, optional): if the parameter requires gradient. Note that
            the torch.no_grad() context does NOT affect the default behavior of
            Parameter creation--the Parameter will still have `requires_grad=True` in
            :class:`~no_grad` mode. See :ref:`locally-disable-grad-doc` for more
            details. Default: `True`
    c                    |t        j                  d      }t        |      t         j                  u st        |      t        u r!t         j                  j                  | ||      S |j                         j                  |      }t        |      t        |      ur8t        dt        |      j                   dt        |      j                   d      d|_
        |S )Nr   z.Creating a Parameter from an instance of type zN requires that detach() returns an instance of the same type, but return type z was found instead. To use the type as a Parameter, please correct the detach() semantics defined by its __torch_dispatch__() implementation.T)r   emptytyper   r	   _make_subclassdetachrequires_grad_RuntimeErrorr   r   )clsdatarequires_gradts       r   __new__zParameter.__new__(   s    <;;q>D:%dy)@ <<..sD-HH KKM((77$t*$@dATAT@U VQ(() *;;  r   c                     t        |       |v r|t        |          S  t        |       | j                  j                  t        j
                        | j                        }||t        |       <   |S )N)memory_format)idr    r&   cloner   preserve_formatr'   r   memoresults      r   __deepcopy__zParameter.__deepcopy__?   s`    d8t4>!T$Z		e.C.CDdFXFXF $DDNMr   c                 &    dt         |          z   S )NzParameter containing:
)r   __repr__)r   r   s    r   r4   zParameter.__repr__I   s    (57+;+===r   c                 &   t         j                  j                  |       }t               }|s3t         j                  j                  | j
                  | j                  |ffS t         j                  j                  | j
                  | j                  ||ffS N)r   _utils_get_obj_stater   _rebuild_parameterr&   r'   _rebuild_parameter_with_state)r   protostatehookss       r   __reduce_ex__zParameter.__reduce_ex__L   s|    ++D1 //D..6  LL66YY**E59
 	
r   )NT)r   r   r   __doc__r)   r2   r4   r>   r   __torch_function__r   r   s   @r   r	   r	      s$    (.>
  7r   r	   )	metaclassc                       e Zd Zej                  j
                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                   ej                  j"                  ej                  j$                  ej                  j&                  ej                  j(                  ej*                  gZddZed        Zd Zd Zd Zed fd	       Z xZS )	UninitializedTensorMixinc                     || j                   j                  }|| j                   j                  }t        j                  |||      | _         | j
                  | _        y)a  Create a Parameter or Tensor with the same properties of the uninitialized one.

        Given a shape, it materializes a parameter in the same device
        and with the same `dtype` as the current one or the specified ones in the
        arguments.

        Args:
            shape : (tuple): the shape for the materialized tensor.
            device (:class:`torch.device`): the desired device of the parameters
                and buffers in this module. Optional.
            dtype (:class:`torch.dtype`): the desired floating point type of
                the floating point parameters and buffers in this module. Optional.
        Ndevicedtype)r&   rF   rG   r   r   cls_to_becomer   )r   shaperF   rG   s       r   materializez$UninitializedTensorMixin.materializet   sK     >YY%%F=IIOOEKKfEB	++r   c                     t        d      )Na  Can't access the shape of an uninitialized parameter or buffer. This error usually happens in `load_state_dict` when trying to load an uninitialized parameter into an initialized one. Call `forward` to initialize the parameters before accessing their attributes.r$   r   s    r   rI   zUninitializedTensorMixin.shape   s    ]
 	
r   c                     t        d      )NzCan't share memory on an uninitialized parameter or buffer. Call `forward` to initialize the parameters before calling `module.share_memory()`.rL   rM   s    r   share_memory_z&UninitializedTensorMixin.share_memory_   s    '
 	
r   c                 6    d| j                   j                   dS )N<>)r   r   rM   s    r   r4   z!UninitializedTensorMixin.__repr__   s    4>>**+1--r   c                 4    | j                   | j                  ffS r6   )r   r'   )r   r;   s     r   r>   z&UninitializedTensorMixin.__reduce_ex__   s    !3!3 566r   c                     || j                   v s|j                  j                  dk(  r|i }t        |   ||||      S t        d| d| j                   d      )Nzmethod-wrapperz/Attempted to use an uninitialized parameter in zf. This error happens when you are using a `LazyModule` or explicitly manipulating `torch.nn.parameter.z` objects. When using LazyModules Call `forward` with a dummy batch to initialize the parameters before calling torch functions)_allowed_methodsr   r   r   r@   
ValueError)r%   functypesargskwargsr   s        r   r@   z+UninitializedTensorMixin.__torch_function__   su     3'''4>>+B+BFV+V~7-dE4HH=dV D;;><<. IJJ
 	
r   )NN) N) r   r   r   r   r   __hash__sizecopy_
is_complexis_floating_pointhalffloatdoublecharshortintlongcudacputo
get_device!_has_compatible_shallow_copy_typerU   rJ   propertyrI   rO   r4   r>   classmethodr@   r   r   s   @r   rC   rC   _   s   &&//#(,* 
 

.7 
 
r   rC   c                 "    t        | t              S r6   )r
   rC   )params    r   is_lazyrq      s    e566r   c                   $    e Zd ZdZeZdddZd Zy)UninitializedParametera  A parameter that is not initialized.

    Uninitialized Parameters are a a special case of :class:`torch.nn.Parameter`
    where the shape of the data is still unknown.

    Unlike a :class:`torch.nn.Parameter`, uninitialized parameters
    hold no data and attempting to access some properties, like their shape,
    will throw a runtime error. The only operations that can be performed on a uninitialized
    parameter are changing its datatype, moving it to a different device and
    converting it to a regular :class:`torch.nn.Parameter`.

    The default device or dtype to use when the parameter is materialized can be set
    during construction using e.g. ``device='cuda'``.
    Nc                 x    ||d}t        j                  di |}t         j                  j                  | ||      S )NrE   r   )r   r   r   r!   )r%   r'   rF   rG   factory_kwargsr&   s         r   r)   zUninitializedParameter.__new__   s6    $*U;{{//||**3mDDr   c                     t        |       |v r|t        |          S  t        |       | j                  | j                  j                  | j                  j
                        }||t        |       <   |S r6   )r,   r    r'   r&   rF   rG   r/   s      r   r2   z#UninitializedParameter.__deepcopy__   sZ    d8t4>!T$Z 2 2DII4D4DdiiooVF#DDNMr   )TNNreturnN)r   r   r   r?   r	   rH   r)   r2   r[   r   r   rs   rs      s     ME
r   rs   c                        e Zd Z fdZ xZS )_BufferMetac                     | t         u r(t        |t        j                        rt	        |dd      ryt
        |   |      S )N
_is_bufferFT)Bufferr
   r   r   r   r   r   r   s     r   r   z_BufferMeta.__instancecheck__   s;    6>(ELL1g,7 w(22r   r   r   s   @r   r{   r{      r   r   r{   c                   "    e Zd ZdZddddZeZy)r~   a,  A kind of Tensor that should not be considered a model
    parameter. For example, BatchNorm's ``running_mean`` is not a parameter, but is part of the module's state.

    Buffers are :class:`~torch.Tensor` subclasses, that have a
    very special property when used with :class:`Module` s -- when they're
    assigned as Module attributes they are automatically added to the list of
    its buffers, and will appear e.g. in :meth:`~torch.nn.Module.buffers` iterator.
    Assigning a Tensor doesn't have such effect. One can still assign a Tensor as explicitly by using
    the :meth:`~torch.nn.Module.register_buffer` function.

    Args:
        data (Tensor): buffer tensor.
        persistent (bool, optional): whether the buffer is part of the module's
            :attr:`state_dict`. Default: ``True``
    NT)
persistentc                    |t        j                  d      }|j                         j                  |j                        }||_        d|_        |S )Nr   T)r   r   r"   r#   r'   r   r}   )r%   r&   r   r(   s       r   r)   zBuffer.__new__   sC    <;;q>DKKM((););<!r   r6   )r   r   r   r?   r)   r   r@   r[   r   r   r~   r~      s     d  7r   r~   c                   6    e Zd ZdZej
                  Z	 d	 ddZy)UninitializedBuffera  A buffer that is not initialized.

    Uninitialized Buffer is a a special case of :class:`torch.Tensor`
    where the shape of the data is still unknown.

    Unlike a :class:`torch.Tensor`, uninitialized parameters
    hold no data and attempting to access some properties, like their shape,
    will throw a runtime error. The only operations that can be performed on a uninitialized
    parameter are changing its datatype, moving it to a different device and
    converting it to a regular :class:`torch.Tensor`.

    The default device or dtype to use when the buffer is materialized can be set
    during construction using e.g. ``device='cuda'``.
    Nc                     ||d}t        j                  di |}t         j                  j                  | ||      }||_        d|_        |S )NrE   Tru   )r   r   r   r!   r   r}   )r%   r'   rF   rG   r   rv   r&   rets           r   r)   zUninitializedBuffer.__new__  sJ     %+U;{{//ll))#t]C#
r   )FNNTrx   )r   r   r   r?   r   r   rH   r)   r[   r   r   r   r      s&     LLM GK	r   r   )collectionsr   r   torch._Cr   _C_TensorMetar   r   r	   rC   rq   rs   r{   r~   r   r[   r   r   <module>r      s    #  23UXX)) 3I7 I7XO
 O
d75y B3%((&& 37U\\[ 7:2ELL r   