L ixdZddlmZmZmZerddlZej eZdZ dZ dZ d dZ d d Z y) z2HQQ (Half-Quadratic Quantization) integration file)is_hqq_availableis_torch_availableloggingNcB|jD] \}}||_yN) named_modulesname)modelr modules c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/integrations/hqq.pyautoname_modulesrs%++- f cdj|jdDcgc]}|dvs|jr|c}Scc}w)N.)r layers)joinsplit isnumeric)r ns r name_to_linear_tagr s; 88 3l1Q>Q5Q\]\g\g\iQl mmls AAActrddlm}t}|j D]F\}}t |t jjfs-|jt|Ht|S)Nr) HQQLinear) rhqq.core.quantizersetr isinstancetorchnnLinearaddrlist)r r linear_tagsr r s r get_linear_tagsr#%sd/%K++-6 f fuxx : ; OO.t4 56  rcJ|jD] \}}|g}|j|t|tjj rt |j}||vrW||R|||j|_ t||j|_ |j|jdd}dD]}t||dtt|j!dkDrt#|||\}}|j%d||fS)NFT)W_qmetar patch_paramshas_been_replaced)named_childrenappendrrrrrr _modules quant_configtype source_clsrequires_grad_setattrlenr!children_prepare_for_hqq_linearpop) r r(r)current_key_namer r linear_tagatt_s r r5r50s%,,.! f  #! % fehhoo .+FKK8J\) +78DZ8PENN4(56:6lENN4(3NN4(77> $ ' +T* + tFOO%& '! +#:)"3$ A R ?!B # ##rc|gn|}t|t||j}|j}t t t |z t |z t fd|Dr'tj}|j|ntj|}t|||\}}||j|d|j_ |stjd|S)a Prepares nn.Linear layers for HQQ quantization. Since each layer type can have separate quantization parameters, we need to do the following: 1- tag each module with its name via autoname_modules() 2- Extract linear_tags (e.g. ['self_attn.q_proj', ...]) 3- Map quantization parameters as a dictionary linear_tag -> quant_params as HQQLinear expects it, this is referred to as patch_params c3&K|]}|v ywr).0keyr"s r z)prepare_for_hqq_linear..ks 6#3+  6sr')r. quant_method skip_moduleszYYZK 6 66}}[1 L)}}[,? 6 LrQsH5AA   H % n "$J,r