L idZddlmZddlmZmZer ddlZddlmZeje Z ed de fdZ ed dd de fd Zd d Zy)a Since, https://github.com/huggingface/transformers/pull/36963, loading is always performed with models on meta device. But since the `init_empty_weights` and `find_tied_parameters` functions are from accelerate, and accelerate is somewhat still a soft dependency, we copy the functions here to be used natively in Transformers. The `init_empty_weights` and `init_on_device` functions were copied from `accelerate.big_modeling.py`, and the `find_tied_parameters` was copied from `accelerate.utils.modeling.py` )contextmanager)is_torch_availableloggingNinclude_buffersc#~Kttjd|5}|dddy#1swYyxYww)a  A context manager under which models are initialized with all parameters on the meta device, therefore creating an empty model. Useful when just initializing the model would blow the available RAM. Args: include_buffers (`bool`, *optional*): Whether or not to also put all buffers on the meta device while initializing. Example: ```python import torch.nn as nn from accelerate import init_empty_weights # Initialize a model with 100 billions parameters in no time and without using any RAM. with init_empty_weights(): tst = nn.Sequential(*[nn.Linear(10000, 10000) for _ in range(1000)]) ``` Any model created under this context manager has no weights. As such you can't do something like `model.to(some_device)` with it. To load weights inside your empty model, see [`load_checkpoint_and_dispatch`]. Make sure to overwrite the default device_map param for [`load_checkpoint_and_dispatch`], otherwise dispatch is not called. meta)rN)init_on_devicetorchdevice)rfs j/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/integrations/accelerate.pyinit_empty_weightsr$s7<  V,o NRSs!=1 =:=r z torch.devicec #F K|r5ddddytjj |rtjj fd}dfd }|rdDcic]}|t t |}}ni}fd} |tj_|r|tj_|D]'}t t ||t t |)d tj_|rtj_|jD]\}}t t ||y#1swYyxYwcc}w# tj_|rtj_|jD]\}}t t ||wxYww)a) A context manager under which models are initialized with all parameters on the specified device. Args: device (`torch.device`): Device to initialize all parameters on. include_buffers (`bool`, *optional*): Whether or not to also put all buffers on the meta device while initializing. Example: ```python import torch.nn as nn from accelerate import init_on_device with init_on_device(device=torch.device("cuda")): tst = nn.Linear(100, 100) # on `cuda` device ``` Nc||||st|j|}|j|j}|j|d<||j|j fi||j|<yy)N requires_grad)type _parameters__dict__rto)modulenameparam param_clskwargsr old_register_parameters rregister_empty_parameterz0init_on_device..register_empty_parameterdsvtU3  V//56I''-66F&+&9&9F? #'01C1CD1I1L1LV1T'_X^'_F  t $ cz|||||,|j|j|j|<yy)N) persistent)_buffersr)rrbufferr r old_register_buffers rregister_empty_bufferz-init_on_device..register_empty_bufferls>FD&ZH  $*OOD$9$<$.patch_tensor_constructor..wrapper{s%F8 t&v& &rr+)r-r.r s` rpatch_tensor_constructorz0init_on_device..patch_tensor_constructorzs 'r)T)nnModuleregister_parameterregister_buffergetattrr setattritems) r rrr$torch_function_nametensor_constructors_to_patchr/old_torch_functionr#rs ` @@rr r Fs*    YY99 ii77`E (J( # 0C!D D( $( (*$ D'? $ (=BII %#? o  E.0HQVXkIl0m n o '= $ (;BII %7S7Y7Y7[ D 3 !3 E.0B C Da *( ,(> $ (;BII %7S7Y7Y7[ D 3 !3 E.0B C DsCF!D6AF!!E9 F!AE AF!6D?; F!AFF!c t|jd}t|jd}t|jt|jz }i}|D]A}||}|j D]'\}} | |us ||vrg||<||j |)C|j D cgc]&\} } t | gtt| z(c} } Scc} } w)a Find the tied parameters in a given model. The signature accepts keyword arguments, but they are for the recursive part of this function and you should ignore them. Args: model (`torch.nn.Module`): The model to inspect. Returns: list[list[str]]: A list of lists of parameter names being all tied together. Example: ```py >>> from collections import OrderedDict >>> import torch.nn as nn >>> model = nn.Sequential(OrderedDict([("linear1", nn.Linear(4, 4)), ("linear2", nn.Linear(4, 4))])) >>> model.linear2.weight = model.linear1.weight >>> find_tied_parameters(model) [['linear1.weight', 'linear2.weight']] ``` F)remove_duplicateT)dictnamed_parameterssetkeysr6appendsortedlist) modelrall_named_parametersno_duplicate_named_parameterstied_param_namestied_param_groupstied_param_name tied_param param_namerweighttieds rfind_tied_parametersrMs>  6 6 6 NO%))?)?QU)?)V$W!/4467#>[>`>`>b:cc+F)/: !>!D!D!F F J "%6646%j1!*-44_E  FFDUCZCZC\ ]<64FF8d3t9o- . ]] ]s+C4)F)rCz nn.Module)__doc__ contextlibrutilsrrr torch.nnr0 get_logger__name__loggerboolrr rMr+rrrVs&/   H %BFD>FDDFDFDR4^r