
    biX                    N    d dl mZ d dlZd dlmZ d dlZd dlmZ  G d de      Zy)    )annotationsN)OrderedDict)Modulec                  z     e Zd ZdZdd fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Z xZS )
BufferDictaq  
    Holds buffers in a dictionary.

    BufferDict can be indexed like a regular Python dictionary, but buffers it contains are properly registered, and
    will be visible by all Module methods. `torch.nn.BufferDict` is an **ordered** dictionary that respects

    * the order of insertion, and
    * in `torch.nn.BufferDict.update`, the order of the merged `OrderedDict` or another `torch.nn.BufferDict` (the
      argument to `torch.nn.BufferDict.update`).

    Note that `torch.nn.BufferDict.update` with other unordered mapping types (e.g., Python's plain `dict`) does not
    preserve the order of the merged mapping.

    Args:
        buffers (iterable, optional):
            a mapping (dictionary) of (string : `torch.Tensor`) or an iterable of key-value pairs of type (string,
            `torch.Tensor`)

    ```python
    class MyModule(nn.Module):
        def __init__(self):
            super().__init__()
            self.buffers = nn.BufferDict({"left": torch.randn(5, 10), "right": torch.randn(5, 10)})

        def forward(self, x, choice):
            x = self.buffers[choice].mm(x)
            return x
    ```
    c                X    t         |           || _        || j                  |       yy)z
        Args:
            buffers (`dict`):
                A mapping (dictionary) from string to `torch.Tensor`, or an iterable of key-value pairs of type
                (string, `torch.Tensor`).
        N)super__init__
persistentupdate)selfbuffersr   	__class__s      S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/peft/tuners/_buffer_dict.pyr
   zBufferDict.__init__2   s.     	$KK      c                     | j                   |   S N_buffersr   keys     r   __getitem__zBufferDict.__getitem__?   s    }}S!!r   c                @    | j                  ||| j                         y )N)r   )register_bufferr   )r   r   buffers      r   __setitem__zBufferDict.__setitem__B   s    S&T__Er   c                    | j                   |= y r   r   r   s     r   __delitem__zBufferDict.__delitem__E   s    MM#r   c                ,    t        | j                        S r   )lenr   r   s    r   __len__zBufferDict.__len__H   s    4==!!r   c                H    t        | j                  j                               S r   )iterr   keysr!   s    r   __iter__zBufferDict.__iter__K   s    DMM&&())r   c                    || j                   v S r   r   r   s     r   __contains__zBufferDict.__contains__N   s    dmm##r   c                8    | j                   j                          y)z%Remove all items from the BufferDict.N)r   clearr!   s    r   r*   zBufferDict.clearQ   s    r   c                    | |   }| |= |S )zRemove key from the BufferDict and return its buffer.

        Args:
            key (`str`):
                Key to pop from the BufferDict
         )r   r   vs      r   popzBufferDict.popU   s     IIr   c                6    | j                   j                         S )z*Return an iterable of the BufferDict keys.)r   r%   r!   s    r   r%   zBufferDict.keys`   s    }}!!##r   c                6    | j                   j                         S )z5Return an iterable of the BufferDict key/value pairs.)r   itemsr!   s    r   r1   zBufferDict.itemsd   s    }}""$$r   c                6    | j                   j                         S )z,Return an iterable of the BufferDict values.)r   valuesr!   s    r   r3   zBufferDict.valuesh   s    }}##%%r   c           	        t        |t        j                  j                        s!t	        dt        |      j                  z         t        |t        t        f      r|j                         D ]
  \  }}|| |<    y	t        |t        j                  j                        r't        |j                               D ]
  \  }}|| |<    y	t        |      D ]  \  }}t        |t        j                  j                        s0t	        dt        |      z   dz   t        |      j                  z         t        |      dk(  s2t        dt        |      z   dz   t        t        |            z   dz         |d   | |d   <    y	)
a	  
        Update the `torch.nn.BufferDict` with the key-value pairs from a mapping or an iterable, overwriting existing
        keys.

        Note:
            If `buffers` is an `OrderedDict`, a `torch.nn.BufferDict`, or an iterable of key-value pairs, the order of
            new elements in it is preserved.

        Args:
            buffers (iterable):
                a mapping (dictionary) from string to `torch.Tensor`, or an iterable of key-value pairs of type
                (string, `torch.Tensor`).
        zQBuffersDict.update should be called with an iterable of key/value pairs, but got z$BufferDict update sequence element #z should be Iterable; is   z has length z; 2 is required   r   N)
isinstancecollectionsabcIterable	TypeErrortype__name__r   r   r1   Mappingsorted	enumeratestrr    
ValueError)r   r   r   r   jps         r   r   zBufferDict.updatel   sj    ';??#;#;<8:>w-:P:PQ 
 gZ89&}} #V"S	#!8!89%gmmo6 #V"S	# "'* 
"1!![__%=%=>#>QGJccfjklfmfvfvv  1v{$!!f%'568;CFDFWX  qTQqT

"r   c                p   g }| j                   j                         D ]  \  }}dj                  d |j                         D              }|j                  sdnd|j                          d}dt        j                  |       d| | d}|j                  d	|z   d
z   |z           dj                  |      }|S )Nxc              3  2   K   | ]  }t        |        y wr   )rA   ).0sizes     r   	<genexpr>z(BufferDict.extra_repr.<locals>.<genexpr>   s     ?dD	?s    z (GPU )zBuffer containing: [z	 of size ]z  (z): 
)	r   r1   joinrI   is_cuda
get_devicetorchtypenameappend)r   child_lineskrD   size_str
device_strparastrtmpstrs           r   
extra_reprzBufferDict.extra_repr   s    MM'') 	<DAqxx?affh??H#$99F1<<>:J!2LJ,U^^A->,?y
S]R^^_`Guqy507:;		<
 ;'r   c                    t        d      )Nz BufferDict should not be called.)RuntimeError)r   inputs     r   __call__zBufferDict.__call__   s    =>>r   )NF)r   bool)r=   
__module____qualname____doc__r
   r   r   r   r"   r&   r(   r*   r.   r%   r1   r3   r   r[   r_   __classcell__)r   s   @r   r   r      sU    <!"F"*$	$%&%"N?r   r   )
__future__r   r8   r   rR   torch.nnr   r   r,   r   r   <module>rg      s$    #  #  K? K?r   