L iddlmZmZmZer ddlZddlmZerddlmZeje Z ddZ ddZ y) )is_accelerate_availableis_eetq_availableloggingN)init_empty_weightsc $ |g}|jD]h\}}|j|t|tjr||vrdj | t fd|Dst5|j}|j} tj|| |jdu|jj|j|<|r2|j|j!|jjd}|j|j#ddddt%t'|j)dkDrt+||||||\} }|j-dk||fS#1swYZxYw) z Private method that wraps the recursion for module replacement. Returns the converted model and a boolean that indicates if the conversion has been successful or not. N.c3:K|]}|dzvxs|k(yw)r N).0keycurrent_key_name_strs d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/integrations/eetq.py z,_replace_with_eetq_linear..2s.Y\s22T?S8STsTFr)has_been_replaced pre_quantized)named_childrenappend isinstancennLinearjoinanyr in_features out_featureseetq EetqLinearbiasweightdevice_modulesregister_scalerequires_grad_lenlistchildren_replace_with_eetq_linearpop) modelmodules_to_not_convertcurrent_key_namequantization_configrrnamemodulerr_rs @rr(r(s},,.! f% vryy )t;Q/Q#&88,<#= `v() ?"("4"4K#)#6#6L+/??#\6;;d3JFMML`L`,ENN4(%t,;;FMM ? tFOO%& '! +#<& #"3+ $ A  R ?!@ # ##/ ? ?s ;B2FF c|dgn|}|j|j|jtt|}t |||||\}}|st j d|S)a A helper function to replace all `torch.nn.Linear` modules by `eetq.EetqLinear` modules from the `eetq` library. This will enable running your models using high performance int8 weight-only gemm kerner from FasterTransformer and TensorRT-LLM. Make sure `eetq` compiled with the correct CUDA version of your hardware is installed before running this function. EETQ shall be installed via the source 'https://github.com/NetEase-FuXi/EETQ' The function will be run recursively and replace all `torch.nn.Linear` modules except for the `lm_head` that should be kept as a `torch.nn.Linear` module. The replacement is done under `init_empty_weights` context manager so no CPU/GPU memory is required to run this function. Each weight will be quantized along the channel. Parameters: model (`torch.nn.Module`): Input model or `torch.nn.Module` as the function is run recursively. modules_to_not_convert (`list[`str`]`, *optional*, defaults to `["lm_head"]`): Names of the modules to not convert in `EetqLinear`. In practice we keep the `lm_head` in full precision for numerical stability reasons. current_key_name (`list[`str`]`, *optional*): An array to track the current key of the recursion. This is used to check whether the current key (part of it) is not in the list of modules to not convert (for instances modules that are offloaded to `cpu` or `disk`). lm_head)rzYou are loading your model using eetq but no linear modules were found in your model. Please double check your model architecture, or submit an issue on github if you think this is a bug.)r+extendr&setr(loggerwarning)r*r+r,r-rrs rreplace_with_eetq_linearr7Os4-C,Ji[Pf11=%%&9&P&PQ!#&<"=>8 %'79L\i E    L)NNNFF)NNNF)utilsrrrrtorch.nnr accelerater get_logger__name__r5r(r7r r8rr>sZHG-   H %   0$hhm*r8