+
    „°ñj›2  ã                   óž   € ^ RI HtHt ^ RIt^ RIHt ^ RIt ! R R4      t ! R R]P                  4      t
R tR R	 ltR
 tR tRR R lltR# )é    )ÚCallableÚOptionalNc                   óà   a € ] tR t^	t o RtRtRt]V 3R lR l4       tV 3R lR lt	]V 3R lR	 l4       t
R
 t]P                  R3R ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R ltRtV tR# )ÚWeightAdapterBasea|  
Base class for weight adapters (LoRA, LoHa, LoKr, OFT, etc.)

Bypass Mode:
    All adapters follow the pattern: bypass(f)(x) = g(f(x) + h(x))

    - h(x): Additive component (LoRA path). Returns delta to add to base output.
    - g(y): Output transformation. Applied after base + h(x).

    For LoRA/LoHa/LoKr: g = identity, h = adapter(x)
    For OFT/BOFT: g = transform, h = 0
ç      ð?Nc                ó„   <€ V ^8„  d   QhRS[ RS[S[ S[P                  3,          RS[RS[P                  RS[R,          /# )é   ÚxÚloraÚalphaÚ
dora_scaleÚreturnr   )ÚstrÚdictÚtorchÚTensorÚfloatr   )ÚformatÚ__classdict__s   "€Ú=C:\Ai_Coding\apps\studio\ComfyUI\comfy\weight_adapter\base.pyÚ__annotate__ÚWeightAdapterBase.__annotate__    sU   ø€ ÷ "ñ "áð"ñ ‘3™Ÿ™Ð$Õ%ð"ñ ð	"ñ
 —L‘Lð"ñ 
Ð%Õ	&ñ"ó    c                ó   € \         h©N©ÚNotImplementedError)Úclsr
   r   r   r   s   &&&&&r   ÚloadÚWeightAdapterBase.load   s
   € ô "Ð!r   c                ó   <€ V ^8„  d   QhRR/# ©r	   r   ÚWeightAdapterTrainBase© )r   r   s   "€r   r   r   )   s   ø€ ÷ "ñ "Ð2ñ "r   c                ó   € \         hr   r   ©Úselfs   &r   Úto_trainÚWeightAdapterBase.to_train)   s   € Ü!Ð!r   c                ó   <€ V ^8„  d   QhRR/# r"   r$   )r   r   s   "€r   r   r   -   s   ø€ ÷ "ñ "Ð,Dñ "r   c                ó   € \         h)z|
weight: The original weight tensor to be modified.
*args: Additional arguments for configuration, such as rank, alpha etc.
r   )r   ÚweightÚargss   &&*r   Úcreate_trainÚWeightAdapterBase.create_train,   s
   € ô "Ð!r   c                ó   € R # r   r$   )r'   Úkeys   &&r   Úcalculate_shapeÚ!WeightAdapterBase.calculate_shape4   s   € ñ r   c	                ó   € \         hr   r   )	r'   r,   r1   ÚstrengthÚstrength_modelÚoffsetÚfunctionÚintermediate_dtypeÚoriginal_weights	   &&&&&&&&&r   Úcalculate_weightÚ"WeightAdapterBase.calculate_weight:   s
   € ô "Ð!r   c                óh   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  /# ©r	   r
   Úbase_outr   ©r   r   )r   r   s   "€r   r   r   N   s.   ø€ ÷ *ñ *‘5—<‘<ð *©5¯<©<ð *¹E¿L¹Lñ *r   c                ó.   € \         P                  ! V4      # )a‡  
Additive bypass component: h(x, base_out)

Computes the adapter's contribution to be added to base forward output.
For adapters that only transform output (OFT/BOFT), returns zeros.

Note:
    This method does NOT access original model weights. Bypass mode is
    designed for quantized models where weights may not be in a usable format.
    All shape info comes from module attributes set by BypassForwardHook.

Args:
    x: Input tensor
    base_out: Output from base forward f(x), can be used for shape reference

Returns:
    Delta tensor to add to base output. Shape matches base output.

Reference: LyCORIS LoConModule.bypass_forward_diff
)r   Ú
zeros_like©r'   r
   r?   s   &&&r   ÚhÚWeightAdapterBase.hN   s   € ô. ×Ò Ó)Ð)r   c                óN   <€ V ^8„  d   QhRS[ P                  RS[ P                  /# ©r	   Úyr   r@   )r   r   s   "€r   r   r   g   s#   ø€ ÷ ñ ‘5—<‘<ð ¡E§L¡Lñ r   c                ó   € V# )a-  
Output transformation: g(y)

Applied after base forward + h(x). For most adapters this is identity.
OFT/BOFT override this to apply orthogonal transformation.

Args:
    y: Combined output (base + h(x))

Returns:
    Transformed output

Reference: LyCORIS OFTModule applies orthogonal transform here
r$   ©r'   rH   s   &&r   ÚgÚWeightAdapterBase.gg   s	   € ð  ˆr   c                óT   <€ V ^8„  d   QhRS[ RS[P                  RS[P                  /# ©r	   Úorg_forwardr
   r   ©r   r   r   )r   r   s   "€r   r   r   y   s1   ø€ ÷  (ñ  (áð (ñ �<‰<ð (ñ 
�‰ñ (r   c                ój   € V! V.VO5/ VB pV P                  W%4      pV P                  WV,           4      # )aù  
Full bypass forward: g(f(x) + h(x, f(x)))

Note:
    This method does NOT take org_weight/org_bias parameters. Bypass mode
    is designed for quantized models where weights may not be accessible.
    The original forward function handles weight access internally.

Args:
    org_forward: Original module forward function
    x: Input tensor
    *args, **kwargs: Additional arguments for org_forward

Returns:
    Output with adapter applied in bypass mode

Reference: LyCORIS LoConModule.bypass_forward
©rD   rK   ©r'   rO   r
   r-   Úkwargsr?   Úh_outs   &&&*,  r   Úbypass_forwardÚ WeightAdapterBase.bypass_forwardy   s<   € ñ4 ˜qÐ2 4Ò2¨6Ñ2ˆð —‘�qÓ#ˆð �v‰v�hÕ&Ó'Ð'r   c                óŠ   <€ V ^8„  d   Qh/ S[ ;R&   S[S[ ,          ;R&   S[S[P                  ,          ;R&   S[;R&   S[;R&   # )r	   ÚnameÚloaded_keysÚweightsÚ
multiplierÚshape)r   ÚsetÚlistr   r   r   Útuple)r   r   s   "€r   r   r   	   sR   ø‡ ‚ ñ �Iñ ñ ‘S•Ññ ñ  ‘%—,‘,ÕÑñ! ñ& Ññ' ñ( Ñò) r   r$   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r\   r]   Úclassmethodr   r(   r.   r2   r   Úfloat32r;   rD   rK   rV   Ú__annotate_func__Ú__static_attributes__Ú__classdictcell__)r   s   @r   r   r   	   s‚   ø‡ € ñð$ €JØ€Eà÷"ó ð"÷"ð "ð ÷"ó ð"òð !Ÿ=™=Øô"÷(*ð *÷2ð ÷$ (ð  (÷a ƒ r   r   c                   ó¦   a a€ ] tR t^œt oRtRtRt^ t/ tRt	Rt
RtV 3R ltR tV3R lR ltV3R	 lR
 ltV3R lR ltR tR tV3R ltRtVtV ;t# )r#   aK  
Base class for trainable weight adapters (LoRA, LoHa, LoKr, OFT, etc.)

Bypass Mode:
    All adapters follow the pattern: bypass(f)(x) = g(f(x) + h(x))

    - h(x): Additive component (LoRA path). Returns delta to add to base output.
    - g(y): Output transformation. Applied after base + h(x).

    For LoRA/LoHa/LoKr: g = identity, h = adapter(x)
    For OFT: g = transform, h = 0

Note:
    Unlike WeightAdapterBase, TrainBase classes have simplified weight formats
    with fewer branches (e.g., LoKr only has w1/w2, not w1_a/w1_b decomposition).

We follow the scheme of PR #7032
r   FNc                ó$   <€ \         SV `  4        R # r   )ÚsuperÚ__init__)r'   Ú	__class__s   &€r   rn   ÚWeightAdapterTrainBase.__init__º   s   ø€ Ü‰ÑÖr   c                ó   € \         h)z“
Weight modification mode: returns modified weight.

Args:
    w: The original weight tensor to be modified.

Returns:
    Modified weight tensor.
r   )r'   Úws   &&r   Ú__call__ÚWeightAdapterTrainBase.__call__½   s
   € ô "Ð!r   c                óh   <€ V ^8„  d   QhRS[ P                  RS[ P                  RS[ P                  /# r>   r@   )r   r   s   "€r   r   Ú#WeightAdapterTrainBase.__annotate__Ë   s.   ø€ ÷ 
ñ 
‘5—<‘<ð 
©5¯<©<ð 
¹E¿L¹Lñ 
r   c                óF   € \        V P                  P                   R24      h)a�  
Additive bypass component: h(x, base_out)

Computes the adapter's contribution to be added to base forward output.
For adapters that only transform output (OFT), returns zeros.

Args:
    x: Input tensor
    base_out: Output from base forward f(x), can be used for shape reference

Returns:
    Delta tensor to add to base output. Shape matches base output.

Subclasses should override this method.
zD.h() not implemented. Subclasses must implement h() for bypass mode.)r   ro   ra   rC   s   &&&r   rD   ÚWeightAdapterTrainBase.hË   s+   € ô  "Ø�~‰~×&Ñ&Ð'ð (=ð =ó
ð 	
r   c                óN   <€ V ^8„  d   QhRS[ P                  RS[ P                  /# rG   r@   )r   r   s   "€r   r   rv   à   s#   ø€ ÷ ñ ‘5—<‘<ð ¡E§L¡Lñ r   c                ó   € V# )zé
Output transformation: g(y)

Applied after base forward + h(x). For most adapters this is identity.
OFT overrides this to apply orthogonal transformation.

Args:
    y: Combined output (base + h(x))

Returns:
    Transformed output
r$   rJ   s   &&r   rK   ÚWeightAdapterTrainBase.gà   s	   € ð ˆr   c                óT   <€ V ^8„  d   QhRS[ RS[P                  RS[P                  /# rN   rP   )r   r   s   "€r   r   rv   ð   s1   ø€ ÷ (ñ (áð(ñ �<‰<ð(ñ 
�‰ñ(r   c                ój   € V! V.VO5/ VB pV P                  W%4      pV P                  WV,           4      # )zë
Full bypass forward: g(f(x) + h(x, f(x)))

Args:
    org_forward: Original module forward function
    x: Input tensor
    *args, **kwargs: Additional arguments for org_forward

Returns:
    Output with adapter applied in bypass mode
rR   rS   s   &&&*,  r   rV   Ú%WeightAdapterTrainBase.bypass_forwardð   s<   € ñ& ˜qÐ2 4Ò2¨6Ñ2ˆð —‘�qÓ#ˆð �v‰v�hÕ&Ó'Ð'r   c                ó   € \        R 4      h)z'passive_memory_usage is not implementedr   r&   s   &r   Úpassive_memory_usageÚ+WeightAdapterTrainBase.passive_memory_usage  s   € Ü!Ð"KÓLÐLr   c                óD   € V P                  V4       V P                  4       # r   )Útor€   )r'   Údevices   &&r   Úmove_toÚWeightAdapterTrainBase.move_to  s   € Ø�‰�ŒØ×(Ñ(Ó*Ð*r   c                ón   <€ V ^8„  d   Qh/ S[ ;R&   S[;R&   S[;R&   S[;R&   S[;R&   S[;R&   S[;R&   # )r	   r\   Úis_convÚconv_dimÚkw_dictÚkernel_sizeÚin_channelsÚout_channels)r   ÚboolÚintr   r`   )r   r   s   "€r   r   rv   œ   s`   ø‡ ‚ ñ, Ññ- ñ. Ññ/ ñ0 Ññ1 ñ2 Ññ3 ñ4 Ññ5 ñ6 Ññ7 ñ8 Ñò9 r   r$   )ra   rb   rc   rd   re   r\   rˆ   r‰   rŠ   r‹   rŒ   r�   rn   rs   rD   rK   rV   r€   r…   rh   ri   rj   Ú__classcell__)ro   r   s   @@r   r#   r#   œ   sj   ù‡ € ñð* €JØ€GØ€HØ€GØ€KØ€KØ€Lõò
"÷
ð 
÷*ð ÷ (ð (ò6Mò+÷e … r   r#   c                 ó"  € \         P                  P                  WP                  V4      p W#,          pW! V4      P	                  VP
                  4      ,           pV P                  ^ ,          VP                  ^ ,          8H  pV'       dv   VP                  VP                  ^ ,          R4      P                  ^RR7      P                  ! VP                  ^ ,          .^.VP                  4       ^,
          ,          O5!  p	M”VP                  ^ ^4      P                  VP                  ^,          R4      P                  ^RR7      P                  ! VP                  ^,          .^.VP                  4       ^,
          ,          O5!  P                  ^ ^4      p	V	\        P                  ! VP
                  4      P                  ,           p	WpV	,          P	                  VP
                  4      ,          pVR8w  d   Wq,          pWV,          ,          pV# WqR&   V# )r   T)ÚdimÚkeepdimr   :NNNéÿÿÿÿ)ÚcomfyÚmodel_managementÚcast_to_devicer„   ÚtypeÚdtyper]   ÚreshapeÚnormr’   Ú	transposer   ÚfinfoÚeps)
r   r,   Ú	lora_diffr   r5   r9   r8   Úweight_calcÚwd_on_output_axisÚweight_norms
   &&&&&&&   r   Úweight_decomposer£     sÂ  € ô ×'Ñ'×6Ñ6Ø—M‘MÐ#5ó€Jð Õ€IØ˜8 IÓ.×3Ñ3°F·L±LÓAÕA€Kà"×(Ñ(¨Õ+¨{×/@Ñ/@ÀÕ/CÑCÐßà�N‰N˜6Ÿ<™<¨�?¨BÓ/ß‰T�a ˆTÓ&ß‰Wðà—\‘\ !•_ðAà() s¨f¯j©j«l¸QÕ.>Õ'?óAñ 	ð ×!Ñ! ! QÓ'ß‰W�[×&Ñ& qÕ)¨2Ó.ß‰T�a ˆTÓ&ß‰Wðð !×&Ñ& qÕ)ðKð ./¨C°;·?±?Ó3DÀqÕ3HÕ,IóK÷ ‰Y�q˜!‹_ð 	ð ¤§¢¨F¯L©LÓ 9× =Ñ =Õ=€Kà Õ,×2Ñ2°6·<±<Ó@Õ@€KØ�3„ØÕˆØ˜kÕ*Õ*ˆð €Mð  ˆq‰	Ø€Mr   c                óz   € V ^8„  d   QhR\         P                  R\        \        ,          R\         P                  /# )r	   ÚtensorÚ	new_shaper   )r   r   r_   r�   )r   s   "r   r   r   6  s-   € ÷ #ñ #¤§¡ð #¼¼c½ð #ÄuÇ|Á|ñ #r   c                óˆ  € \        \        \        V4      4       Uu. uF  q!V,          V P                  V,          8  NK!  	  up4      '       d   \	        R4      h\        V4      \        V P                  4      8w  d   \	        R4      h\
        P                  ! WP                  V P                  R7      p\        ;QJ d    . R V P                   4       F  NK  	  5M! R V P                   4       4      p\        ;QJ d    . R V P                   4       F  NK  	  5M! R V P                   4       4      pW,          W5&   V# u upi )a’  
Pad a tensor to a new shape with zeros.

Args:
    tensor (torch.Tensor): The original tensor to be padded.
    new_shape (List[int]): The desired shape of the padded tensor.

Returns:
    torch.Tensor: A new tensor padded with zeros to the specified shape.

Note:
    If the new shape is smaller than the original tensor in any dimension,
    the original tensor will be truncated in that dimension.
zGThe new shape must be larger than the original tensor in all dimensionszLThe new shape must have the same number of dimensions as the original tensor)r™   r„   c              3   ó:   "  € T F  p\        ^ V4      x € K  	  R# 5i©r   N©Úslice©Ú.0r’   s   & r   Ú	<genexpr>Ú&pad_tensor_to_shape.<locals>.<genexpr>S  s   é € Ð>±¨#œ˜a Ÿ˜³ùó   ‚c              3   ó:   "  € T F  p\        ^ V4      x € K  	  R# 5ir©   rª   r¬   s   & r   r®   r¯   T  s   é € Ð=±¨”u˜Q —}�}³ùr°   )
ÚanyÚrangeÚlenr]   Ú
ValueErrorr   Úzerosr™   r„   r`   )r¥   r¦   ÚiÚpadded_tensorÚorig_slicesÚ
new_slicess   &&    r   Úpad_tensor_to_shaper»   6  sð   € ô ´E¼#¸i».Ô4IÓJÑ4I¨q�a�L˜6Ÿ<™<¨�?Ô*Ñ4IÑJ×KÒKÜØUó
ð 	
ô ˆ9ƒ~œ˜VŸ\™\Ó*Ô*ÜØZó
ð 	
ô
 —K’K 	·±ÀfÇmÁmÔT€M÷ ”%Ñ>°·²Ó>—%‘%Ñ>°·²Ó>Ó>€Kß”Ñ=°·²Ó=—‘Ñ=°·²Ó=Ó=€Jð !'Õ 3€MÑàÐùò) Ks   œ%D?c                 óò   € V P                  V P                  ^ 4      V P                  ^4      4      p VP                  VP                  ^ 4      VP                  ^4      4      p\        P                  ! RW V4      # )r   zm n ..., i m, n j -> i j ...)rš   Úsizer   Úeinsum)ÚupÚdownÚmids   &&&r   Útucker_weight_from_convrÂ   \  sV   € Ø	�‰�B—G‘G˜A“J §¡¨£
Ó	+€BØ�<‰<˜Ÿ	™	 !› d§i¡i°£lÓ3€DÜ�<Š<Ð6¸ÀÓFÐFr   c                 ó^   € \         P                  ! R W!4      p\         P                  ! RW04      # )zi j ..., j r -> i r ...zi j ..., i r -> r j ...)r   r¾   )ÚwaÚwbÚtÚtemps   &&& r   Útucker_weightrÈ   b  s%   € Ü�<Š<Ð1°1Ó9€DÜ�<Š<Ð1°4Ó<Ð<r   c                ó^   € V ^8„  d   QhR\         R\         R\        \         \         3,          /# )r	   Ú	dimensionÚfactorr   )r�   r`   )r   s   "r   r   r   g  s)   € ÷ %ñ %œSð %¬#ð %´u¼SÄ#¸Xµñ %r   c                ó:  € V^ 8”  d0   W,          ^ 8X  d#   W^,          8¼  d   TpW,          pW#8”  d   Y#r#W#3# V^ 8  d   T p^T r2W#,           pW#8  dB   V^,           pW,          ^ 8w  d   V^,          pK  W,          pWV,           V8”  g   WQ8”  d   MYVr2KG  W#8”  d   Y#r#W#3# )aÔ  
return a tuple of two value of input dimension decomposed by the number closest to factor
second value is higher or equal than first value.

examples)
factor
    -1               2                4               8               16               ...
127 -> 1, 127   127 -> 1, 127    127 -> 1, 127   127 -> 1, 127   127 -> 1, 127
128 -> 8, 16    128 -> 2, 64     128 -> 4, 32    128 -> 8, 16    128 -> 8, 16
250 -> 10, 25   250 -> 2, 125    250 -> 2, 125   250 -> 5, 50    250 -> 10, 25
360 -> 8, 45    360 -> 2, 180    360 -> 4, 90    360 -> 8, 45    360 -> 12, 30
512 -> 16, 32   512 -> 2, 256    512 -> 4, 128   512 -> 8, 64    512 -> 16, 32
1024 -> 32, 32  1024 -> 2, 512   1024 -> 4, 256  1024 -> 8, 128  1024 -> 16, 64
r$   )rÊ   rË   ÚmÚnÚlengthÚnew_mÚnew_ns   &&     r   ÚfactorizationrÒ   g  s§   € ð  �„z�yÕ)¨aÔ/°IÈÅÔ4JØˆØÕˆØŒ5ØˆqØˆtˆØ�„zØˆØˆi€qØ�U€FØ
Œ%Ø�A•ˆØÕ 1Ô$Ø�Q�JŠEØÕ"ˆØ�=˜6Ô! U¤^ØàŠqØ„uØˆ1Øˆ4€Kr   )r”   )Útypingr   r   r   Útorch.nnÚnnÚcomfy.model_managementr•   r   ÚModuler#   r£   r»   rÂ   rÈ   rÒ   r$   r   r   Ú<module>rØ      sR   ðß %ã Ý ã ÷P(ñ P(ôft+˜RŸY™Yô t+òn õF#òLGò=÷
%ñ %r   