
    oi                    N    S SK Jr  S SKrS SKJr  S SKrS SKJr   " S S\5      rg)    )annotationsN)OrderedDict)Modulec                     ^  \ rS rSrSrSSU 4S jjjrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rSrU =r$ )
BufferDict   a  
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U ]  5         X l        Ub  U R                  U5        gg)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      R/home/james-whalen/.local/lib/python3.13/site-packages/peft/tuners/_buffer_dict.pyr   BufferDict.__init__2   s,     	$KK      c                     U R                   U   $ N_buffersr   keys     r   __getitem__BufferDict.__getitem__?   s    }}S!!r   c                8    U R                  XU R                  S9  g )Nr   )register_bufferr   )r   r   buffers      r   __setitem__BufferDict.__setitem__B   s    ST__Er   c                    U R                   U	 g r   r   r   s     r   __delitem__BufferDict.__delitem__E   s    MM#r   c                ,    [        U R                  5      $ r   )lenr   r   s    r   __len__BufferDict.__len__H   s    4==!!r   c                H    [        U R                  R                  5       5      $ r   )iterr   keysr'   s    r   __iter__BufferDict.__iter__K   s    DMM&&())r   c                    XR                   ;   $ r   r   r   s     r   __contains__BufferDict.__contains__N   s    mm##r   c                8    U R                   R                  5         g)z%Remove all items from the BufferDict.N)r   clearr'   s    r   r3   BufferDict.clearQ   s    r   c                    X   nX	 U$ )zuRemove key from the BufferDict and return its buffer.

Args:
    key (`str`):
        Key to pop from the BufferDict
 )r   r   vs      r   popBufferDict.popU   s     IIr   c                6    U R                   R                  5       $ )z*Return an iterable of the BufferDict keys.)r   r,   r'   s    r   r,   BufferDict.keys`   s    }}!!##r   c                6    U R                   R                  5       $ )z5Return an iterable of the BufferDict key/value pairs.)r   itemsr'   s    r   r=   BufferDict.itemsd   s    }}""$$r   c                6    U R                   R                  5       $ )z,Return an iterable of the BufferDict values.)r   valuesr'   s    r   r@   BufferDict.valuesh   s    }}##%%r   c           	        [        U[        R                  R                  5      (       d!  [	        S[        U5      R                  -   5      e[        U[        [        45      (       a  UR                  5        H	  u  p#X0U'   M     g	[        U[        R                  R                  5      (       a'  [        UR                  5       5       H	  u  p#X0U'   M     g	[        U5       H  u  pE[        U[        R                  R                  5      (       d0  [	        S[        U5      -   S-   [        U5      R                  -   5      e[        U5      S:X  d2  [        S[        U5      -   S-   [        [        U5      5      -   S-   5      eUS   XS   '   M     g	)
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   r=   Mappingsorted	enumeratestrr&   
ValueError)r   r   r   r   jps         r   r   BufferDict.updatel   s[    ';??#;#;<<8:>w-:P:PQ 
 gZ899&}}"S	  /!8!899%gmmo6"S	  7 "'*!![__%=%=>>#>QGJccfjklfmfvfvv  1v{$!!f%'568;CFDFWX  qTqT
 +r   c                   / nU R                   R                  5        H  u  p#SR                  S UR                  5        5       5      nUR                  R
                  nUS:X  a  SO$SUR                  5        SUR                  5        S3nS[        R                  " U5       S	U U S
3nUR                  SU-   S-   U-   5        M     SR                  U5      nU$ )Nxc              3  8   #    U  H  n[        U5      v   M     g 7fr   )rO   ).0sizes     r   	<genexpr>(BufferDict.extra_repr.<locals>.<genexpr>   s     ?hdD		hs   cpu z ( )zBuffer containing: [z	 of size ]z  (z): 
)r   r=   joinrX   devicerJ   upper
get_devicetorchtypenameappend)	r   child_lineskrR   size_strdevice_type
device_strparastrtmpstrs	            r   
extra_reprBufferDict.extra_repr   s    MM'')DAxx?affh??H((--K*e32k>O>O>Q=RRSTUT`T`TbSccd9eJ,U^^A->,?y
S]R^^_`Guqy507:; * ;'r   c                    [        S5      e)Nz BufferDict should not be called.)RuntimeError)r   inputs     r   __call__BufferDict.__call__   s    =>>r   r   )NF)r   bool)rK   
__module____qualname____firstlineno____doc__r   r   r    r#   r(   r-   r0   r3   r8   r,   r=   r@   r   ro   rt   __static_attributes____classcell__)r   s   @r   r   r      s_    <! !"F"*$	$%&%"N	? ?r   r   )
__future__r   rF   r   re   torch.nnr   r   r6   r   r   <module>r      s$    #  #  L? L?r   