
    ^jw                       d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZmZ d
dlmZ d
dlmZ d
dlmZmZmZmZ  e       rddlZddlmZ ddlmZ ddl m!Z!  ejD                  e#      Z$ G d de      Z%e
dFd       Z&e
dGd       Z' edd      d        Z( edd      d        Z) edddddddd       d!        Z*	 	 	 dH	 	 	 	 	 	 	 	 	 dId"Z+ edd#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6      d7        Z,dJd8Z-dKd9Z.dLd:Z/dMd;Z0dNd<Z1dOd=Z2dPd>Z3dQd?Z4dRd@Z5dSdAZ6dTdBZ7dUdCZ8dDZ9e
dVdE       Z:y)Wu  Dynamo exporter.

Wraps `torch.export.export(strict=False)` with helpers that make Transformers
models exportable. The export pipeline uses five sections, in execution order:

1. **Model signature patch** (`patch_forward_signature`): replaces `model.forward`
   with a flat explicit signature derived from `sample_inputs` so `torch.export` does
   not expand `**kwargs` into a `combined_args` bundle that mismatches `dynamic_shapes`.
   This is the entry contract `torch.export` reads before tracing.
2. **Model patches** (`_PATCHES["dynamo"]` via `apply_patches("dynamo")`): reversible
   class-attribute swaps applied during tracing to replace non-exportable model patterns
   (data-dependent loops, in-place ops, mask checks) with export-safe equivalents.
   Modeling code itself is not updated because these patches are too model-specific.
3. **Pytree registration** (`register_cache_pytrees_for_model`): flatten/unflatten
   hooks (via `torch.utils._pytree.register_pytree_node`) for Cache subclasses and
   custom containers so `torch.export` can trace through them.
4. **Dynamic shapes** (`get_auto_dynamic_shapes`): automatic `Dim.AUTO` inference
   for all tensor and cache inputs when `DynamoConfig.dynamic=True`.
5. **Model state cleanup** (`reset_model_state`): non-Cache stateful module attributes
   (`_STATEFUL_CACHE_ATTRS`) are saved on entry, set to `None` during the trace, and
   restored on exit — so a previous eager forward doesn't leak into the trace and any
   FakeTensors the trace planted are discarded before the next eager forward.
    )annotationsN)MutableMapping)contextmanager)Any   )logging)is_detectron2_availableis_torch_availabletorch_compilable_check   )
HfExporter)DynamoConfig)apply_patchespatch_attributesprepare_for_exportregister_patch)ExportedProgram)Cache)PreTrainedModelc                  >    e Zd ZdZdgZddiZddiZ	 	 	 	 	 	 	 	 ddZy)DynamoExporterao  Exporter that converts a [`PreTrainedModel`] to an `ExportedProgram`.

    Example:

    ```python
    >>> from transformers.exporters.exporter_dynamo import DynamoExporter, DynamoConfig

    >>> exporter = DynamoExporter()
    >>> exported = exporter.export(model, inputs, config=DynamoConfig(dynamic=True))
    >>> outputs = exported.module()(**inputs)
    ```
    torchz2.11.0z2.12.0c                   t        |t              rt        di |}n't        |t              st        dt	        |             t        ||      \  }}}|j                  }|j                  r|t        |      }t        |       t        d      5  t        |      5  t        ||      5  t        ||      5  t        j                  j                  |dt!        j"                  t        |            |j$                  ||j&                        }d d d        d d d        d d d        d d d        S # 1 sw Y   #xY w# 1 sw Y   'xY w# 1 sw Y   +xY w# 1 sw Y   S xY w)Nz2Expected config to be a DynamoConfig or dict, got dynamo )argskwargsstrictdynamic_shapes+prefer_deferred_runtime_asserts_over_guards)
isinstancedictr   	TypeErrortyper   r   dynamicget_auto_dynamic_shapes register_cache_pytrees_for_modelr   reset_model_statepatch_model_configpatch_forward_signaturer   exportcopydeepcopyr   r    )selfmodelsample_inputsconfigoutput_flagsr   exported_programs          q/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/exporters/exporter_dynamo.pyr+   zDynamoExporter.exportV   sL    fd#!+F+FFL1PQUV\Q]P^_``-?}-U*}l..>>n44]CN(/ (#	e$	 ul3	 $E=9		 160C0C}}T-%89}}-<B<n<n 1D 1	 	 	 	  	 	 	 	 	 	 	  sU   EE(D95AD-	D9EE-D62D99E>EE	
EEN)r/   r   r0   zMutableMapping[str, Any]r1   zDynamoConfig | dict[str, Any]returnr   )__name__
__module____qualname____doc__required_packagesmin_versionstested_versionsr+   r       r4   r   r   D   sP     !	X&L)O" "  0"  .	" 
 
" r=   r   c              #    K   g }|j                         D ]K  \  }}|"t        | d      rt        | j                  |      s+|j                  | j                  ||fdf       M | j	                         D ]E  }t        |d      st        |j                  d      s'|j                  |j                  dd f       G t        |      5  d ddd       y# 1 sw Y   yxY ww)uK  Reversibly tweak `model.config` for the trace:

    - Applies `output_flags` (popped from inputs by `prepare_for_export`) onto
      `model.config.<flag>` so the model picks them up via its usual `<flag> if <flag> is
      not None else self.config.<flag>` fallback.
    - Disables `use_mamba_kernels` on every submodel's config that declares it (mamba/jamba
      kernels are not exportable).

    Originals are restored on exit. Flags whose value is `None`, or that the config doesn't
    declare, are silently skipped — useful for submodels that don't accept every parent flag.
    Nr1   c                    |S Nr   )	_originalvs     r4   <lambda>z$patch_model_config.<locals>.<lambda>   s    a r=   use_mamba_kernelsc                     yNFr   rA   s    r4   rC   z$patch_model_config.<locals>.<lambda>       r=   )itemshasattrr1   appendmodulesr   )r/   r2   config_patchesflagvaluemodules         r4   r)   r)      s      N#))+ Re=x 8VZ@[u||Tu3OPQR --/ a68$@S)T!!6==2EG^"_`a 
.	)   s*   B C C ,C C	C CC c           
   #  $  K   | j                   fd}t        j                  |D cg c]2  }t        j                  |t        j                  j                  d      4 c}      |_        	 || _         d | _         yc c}w # | _         w xY ww)a  Temporarily replace `model.forward` with a flat explicit signature derived from `inputs`.

    `torch.export` infers the exported function signature from `model.forward.__signature__`.
    Most transformers models use `**kwargs: Unpack[TransformersKwargs]`, which causes
    `torch.export` to expand the signature into a large `combined_args` bundle that
    mismatches the `dynamic_shapes` dict. This patch replaces the forward with a
    minimal signature containing only the keys present in `inputs`.
    c                      di | S )Nr   r   )r   original_forwards    r4   _flat_forwardz.patch_forward_signature.<locals>._flat_forward   s    )&))r=   N)default)forwardinspect	Signature	ParameterPOSITIONAL_OR_KEYWORD__signature__)r/   inputsrT   krS   s       @r4   r*   r*      s}      }}* #*"3"3^deYZ		1g//EEt	Te#M)%( 	f )s(   %B7A?B,B 7B	BBr   zQtransformers.models.nllb_moe.modeling_nllb_moe.NllbMoeTop2Router._cast_classifierc                    d S )z:Disable classifier dtype cast in nllb-moe (not traceable).c                     y r@   r   )r.   r   r   s      r4   rC   z(_patch_classifier_cast.<locals>.<lambda>   rH   r=   r   rG   s    r4   _patch_classifier_castr`      s
     .-r=   z0torch.nn.functional.scaled_dot_product_attentionc                *     ddl mm  fd}|S )u4  Route SDPA through the MATH backend on CPU during tracing — CPU SDPA's flash/efficient
    paths guard on ``Eq(batch, 1)`` (upstream https://github.com/pytorch/pytorch/issues/180202),
    which trips ``GuardOnDataDependentSymNode`` whenever the batch dim comes from a data-dependent
    op like ``pixel_values[bool_mask]`` (Idefics2/3 and most VLMs). The MATH decomposition has no
    batch-1 dispatch, so the guard never fires. CUDA exports are left alone — the GPU kernels
    don't have this guard, and we want the flash/efficient decompositions there.
    r   )
SDPBackendsdpa_kernelc                    | j                   j                  dk(  r( j                        5   | g|i |cd d d        S  | g|i |S # 1 sw Y   xY w)Ncpu)devicer$   MATH)queryr   r   rb   originalrc   s      r4   patchz_patch_sdpa.<locals>.patch   sb    <<%Z__- 87778 8////8 8s   AA)torch.nn.attentionrb   rc   )ri   rj   rb   rc   s   ` @@r4   _patch_sdparl      s     ;0 Lr=   z4transformers.utils.import_utils.is_kernels_availablez'transformers.utils.is_kernels_availablez0transformers.modeling_utils.is_kernels_availablezGtransformers.models.sam3_video.modeling_sam3_video.is_kernels_availablez9transformers.models.mra.modeling_mra.is_kernels_availablez;transformers.models.rwkv.modeling_rwkv.is_kernels_availablez;transformers.models.yoso.modeling_yoso.is_kernels_availablec                    d S )u   Force-disable the optional ``kernels`` library during export — its kernels
    call into native code that ``torch.export`` cannot trace, and the pure-PyTorch
    fallbacks in each model are always traceable.c                      yrF   r   )r   r   s     r4   rC   z-_patch_is_kernels_available.<locals>.<lambda>   rH   r=   r   rG   s    r4   _patch_is_kernels_availablero      s
    " )(r=   c                   |j                   dk(  }|r|j                  d      }|j                  d   }t        |dk7  d       |j                  d   dz
  t        |z  dk(  d       t	        | d      r@t	        | d      rt	        | d      r| j
                  | j                  k7  r| j                  |      j                  | j
                  | j                  | j                  gd	
      \  }	}
}|	j                  || j                  | j                        }	|
j                  || j                  | j                        }
|j                  || j                  | j                        }n | j                  |      j                  |d| j                  d	      j                  dd      j                  d      \  }	}
}nt!        | dt!        | dd            }t!        | dt!        | dd            }t!        | dt!        | dd            } ||      j                  || j                  | j                        }	 ||      j                  || j                  | j                        }
 ||      j                  || j                  | j                        }|t"        j$                  t'        |       j(                     j*                  }t-        |t.        t0        f      r|\  }} ||	|
||      \  }	}
nN ||	j3                  d      |      j                  d      }	 ||
j3                  d      |      j                  d      }
|z  fd} ||	      }	 ||
      }
 ||      }t        |	j                  d   dk7  d       t        |	j                  d   dk7  d       t4        j6                  j8                  j;                  |	|
|d| j<                  | j>                  sdn| j@                  t!        | d| j                        | j                  k7        }|j                  dd      j                  |d	      jC                         }t	        | d      r| jD                  n| jF                  } ||      }|r|j3                  d      }|r|dfS |S )zExport-safe chunked vision/audio attention: reshape segments into a batch dim,
    apply rotary if provided, run one SDPA call, project, and re-emit in the original layout.   r   z1Chunked vision attention received an empty input.r   zChunked vision attention requires uniform segment lengths during export. Ensure all images have the same resolution (use do_resize=True in the processor) or pad inputs to a common size.qkvq_dimkv_dim)dimq_projqNk_projr]   v_projrB   c                L    | j                  df      j                  dd      S )Nr   r   r   )	unflatten	transpose)tnum_segmentsseg_lens    r4   _to_batchedz7_reshaped_vision_attention_forward.<locals>._to_batchedO  s&    {{1|W56@@AFFr=   z1Reshaped chunked-vision attention got zero batch.r   z/Reshaped chunked-vision attention got zero seq.Fg        num_key_value_heads)	is_causalscale	dropout_p
enable_gqaproj)$ndimsqueezeshaper   rJ   rs   rt   rr   splitview	num_headshead_dimr   reshaper}   unbindgetattrsysrL   r$   r7   apply_rotary_pos_emb_visionr!   tuplelist	unsqueezer   nn
functionalscaled_dot_product_attentionscalingtrainingattention_dropout
contiguousr   out_proj)r.   hidden_states
cu_seqlensrotary_pos_embposition_embeddingsreturns_tupler   needs_batch_restore
seq_lengthquery_states
key_statesvalue_statesrw   ry   rz   r   cossinr   attn_outputr   r   r   s                        @@r4   "_reshaped_vision_attention_forwardr     s    (,,1%--a0$$Q'Ja; ##A&*L\!Q&	* tU 4!gdH&=$**PTP[P[B[59XXm5L5R5RT[[$++6B 6S 62L*l (,,ZWL#T5M5Mt}}]J',,Z9Q9QSWS`S`aL '//
At~~rR\\]^`abiijkl 3L*l xsD)ABxsD)ABxsD)ABm,11*dnndmm\M*//
DNNDMMZ
m,11*dnndmm\&
 '*kk$t*2G2G&H&d&d#)E4=9*HC'B<Q[]`be'f$L* 7|7M7Ma7PRefnnopqL4Z5I5I!5LNabjjklmJL(GG |,LZ(J|,L<--a0A57jk<--a0A57hi((%%BBll!]]#0F0F4!6G4>>Y C K ''1-55j"EPPRK#D&1tyyt}}H;'K!++A."/K@[@r=   zFtransformers.models.qwen2_vl.modeling_qwen2_vl.VisionAttention.forwardzTtransformers.models.qwen2_5_vl.modeling_qwen2_5_vl.Qwen2_5_VLVisionAttention.forwardzMtransformers.models.qwen3_vl.modeling_qwen3_vl.Qwen3VLVisionAttention.forwardzXtransformers.models.qwen3_vl_moe.modeling_qwen3_vl_moe.Qwen3VLMoeVisionAttention.forwardzKtransformers.models.qwen3_5.modeling_qwen3_5.Qwen3_5VisionAttention.forwardzVtransformers.models.qwen3_5_moe.modeling_qwen3_5_moe.Qwen3_5MoeVisionAttention.forwardz^transformers.models.qwen3_omni_moe.modeling_qwen3_omni_moe.Qwen3OmniMoeVisionAttention.forwardzEtransformers.models.glm4v.modeling_glm4v.Glm4vVisionAttention.forwardzPtransformers.models.glm4v_moe.modeling_glm4v_moe.Glm4vMoeVisionAttention.forwardzJtransformers.models.glm_ocr.modeling_glm_ocr.GlmOcrVisionAttention.forwardzbtransformers.models.ernie4_5_vl_moe.modeling_ernie4_5_vl_moe.Ernie4_5_VLMoeVisionAttention.forwardzRtransformers.models.exaone4_5.modeling_exaone4_5.Exaone4_5_VisionAttention.forwardzPtransformers.models.glm_image.modeling_glm_image.GlmImageVisionAttention.forwardzYtransformers.models.qwen2_5_omni.modeling_qwen2_5_omni.Qwen2_5OmniVisionAttention.forwardz[transformers.models.video_llama_3.modeling_video_llama_3.VideoLlama3VisionAttention.forwardzWtransformers.models.paddleocr_vl.modeling_paddleocr_vl.PaddleOCRVisionAttention.forwardzWtransformers.models.minicpmv4_6.modeling_minicpmv4_6.MiniCPMV4_6VisionAttention.forwardzXtransformers.models.qwen2_5_omni.modeling_qwen2_5_omni.Qwen2_5OmniAudioAttention.forwardz]transformers.models.qwen3_omni_moe.modeling_qwen3_omni_moe.Qwen3OmniMoeAudioAttention.forwardzOtransformers.models.qwen3_asr.modeling_qwen3_asr.Qwen3ASRAudioAttention.forwardc                P    t        j                  |       }d|v xs d|v fd}|S )zPBind `returns_tuple` once per class by inspecting the original forward's source.zreturn attn_output, attn_weightzreturn attn_output, Nonec                &    t        | g|di|S )Nr   )r   )r.   r   r   r   s      r4   rV   z0_patch_chunked_vision_attention.<locals>.forward  s    1$ee]e^deer=   )rW   	getsource)ri   srcrV   r   s      @r4   _patch_chunked_vision_attentionr   m  s9    @ 

H
%C5<a@Z^a@aMf Nr=   c                8    | j                    d| j                   S )N:)r7   r8   )clss    r4   _class_to_pathr     s    nnQs//011r=   c                    | j                  dd      \  }}t        j                  |      }|j                  d      D ]  }t        ||      } |S )Nr   r   .)r   	importlibimport_moduler   )pathmodule_namequalnameobjparts        r4   _path_to_classr     sP     JJsA.K

!
!+
.Cs# !c4 !Jr=   c                X   | !t        |       t        t        t        t        fv r| S t        |       t
        u r| D cg c]  }t        ||       c}S t        |       t        u r.| j                         D ci c]  \  }}|t        ||       c}}S t        | t        j                        r!t        |      }|j                  |        d|dS t        | t        j                        rdt        |       dS t        | t        j                        rdt	        |       dS t        | t        j                         rdt	        |       j#                  d      d	S t        | t        j$                        rd
t	        |       j#                  d      d	S t        | t        j&                  t        j(                  t        j*                  f      r!t        |      }|j                  |        d|dS t        | t               rdt-        |       dS t        |       }t        | t              r;dt-        |      | j                         D ci c]  \  }}|t        ||       c}}dS t        | t.        t
        t0        t2        f      r'dt-        |      | D cg c]  }t        ||       c}dS t5        | d      rDdt-        |      t7        |       j                         D ci c]  \  }}|t        ||       c}}dS t9        dt        |       j:                   d      c c}w c c}}w c c}}w c c}w c c}}w )z]Single-pass: recursively build a JSON-native context while collecting tensors into `tensors`.tensor)_tisize)r   rB   rf   )r   sdtypeztorch.)r   nlayoutsymr$   )r   pmap)r   r   rB   seq__dict__r   )r   r   r   zCannot flatten z for pytree context)r$   boolintfloatstrr   _flatten_to_contextr"   rI   r!   r   TensorlenrK   Sizerf   r   removeprefixr   SymIntSymFloatSymBoolr   r   set	frozensetrJ   varsr#   r6   )r   tensorsr   r]   rB   idxr   s          r4   r   r     s    {d3iD#uc#::
CyD9<=A#Aw/==CyD?Byy{Ktq!&q'22KK #u||$'lsS))#uzz"49--#u||$SX..#u{{#CH$9$9($CDD#u||$SX%:%:8%DEE#ennemmDE'ls#&& #t>##677 s)C#t$ADMA!(G44M
 	

 #tS)45$;>?a%a1?
 	

 sJ$AEcARSA!(G44S
 	
 od3i&8&8%99LM
NN_ >K< N @ Ts   L5L:L
L!L&
c                   | !t        |       t        t        t        t        fv r| S t        |       t
        u r| D cg c]  }t        ||       c}S t        |       t        u r2d| vr.| j                         D ci c]  \  }}|t        ||       c}}S | d   }|dk(  r|| d      S |dk(  rt        t        | d         S |dk(  rt        t        | d         S |dk(  rt        j                  | d         S |d	k(  rt        j                  | d
         S |dk(  r|| d      S |dk(  rt        | d         S |dk(  rEt        | d         } || d
   j                         D ci c]  \  }}|t        ||       c}}      S |dk(  r3t        | d         }| d
   D cg c]  }t        ||       }}	  ||      S |dk(  rlt        | d         }| d   j                         D ci c]  \  }}|t        ||       }}}|j                  |      }	|	j                   j#                  |       |	S t        d|d      c c}w c c}}w c c}}w c c}w # t        $ r  || cY S w xY wc c}}w )zVReconstruct an object from its JSON-native context, substituting tensor index markers.r   r   r   r   r   r   rf   r   r   rB   r   r$   r   r   r   r   zUnknown tag z in pytree context)r$   r   r   r   r   r   _unflatten_from_contextr"   rI   r   r   rf   r   r   r#   __new__r   update)
ctxr   r   r]   rB   r~   r   rI   stateinstances
             r4   r   r     sx    {d3iD#uc#::
CyD=@A'73AACyDT_CF99;O41a*1g66OO 	D	AH}s3x  H}uc#h''G|uc#h''H}||CH%%F{zz#c(##Ezs3x   	F{c#h'' 	EzSX&s3x~~GWXtq!A.q'::XYYEzSX&>A#hG(G4GG	u: 	EzSX&DGHNNDTUDAq+Aw77UU;;s#  '
l1%'9:
;;S BO0 Y H  	;	 Vs/   H29H7H=
I*I IIIc                &    g }t        | |      }||fS r@   )r   )r   r   contexts      r4   _pytree_flattenr     s    G!#w/GGr=   c                    t        |       \  }}t        |      D cg c]0  \  }}t        j                  j                  j                  |      |f2 c}}|fS c c}}w r@   )r   	enumerater   utils_pytreeSequenceKey)r   leavesr   r   leafs        r4   _pytree_flatten_with_keysr     sN    %c*OFGFOPVFWX71dU[[  ,,Q/6XZaaaXs   5Ac                ,    t        |t        |             S r@   )r   r   )valuesr   s     r4   _pytree_unflattenr   !  s    "7DL99r=   c                    	 t         j                  j                  j                  | t        t
        t        |       t               y # t        $ r}dt        |      vr Y d }~y d }~ww xY w)N)serialized_type_nameflatten_with_keys_fnz!already registered as pytree node)
r   r   r   register_pytree_noder   r   r   r   
ValueErrorr   )
object_clses     r4   _register_pytree_noder   %  s^    
00!/
!;!: 	1 	
  .c!f< =s   AA 	A'A""A'c              #  h   K   | j                         D ]  }| t        |      E d {     y 7 wr@   )__subclasses___iter_subclasses)r   subclasss     r4   r   r   3  s4     &&( .#H---.-s   &202c                   t        t              D ]  }t        |        t        j                  t        j
                  |             D ]w  \  }}t        j                  |      s|j                  | j                  j                  k(  s@|j                  j                  d      s\t        |t              rmt        |       y t               r=t        | t              r,| j                  j                   dk(  rddlm} t        |       yyyy)zCRegister all relevant cache types as pytree nodes for torch.export.r   
layoutlmv2r   )	ImageListN)r   r   r   rW   
getmembers	getmoduleisclassr7   	__class__r6   endswith
issubclassr	   r!   r   r1   
model_type detectron2.structures.image_listr  )r/   
cache_type_r   r  s        r4   r'   r'   9  s     'u- *
j)* $$W%6%6u%=> '3OOC %//"<"<<%%g.sE*!#&'  Z%GELLLcLcgsLs>i( Mt%G r=   c                    t         j                  t        | j                               t        j
                  j                  j                        S )zPGenerate a dynamic shape with all dimensions set to Dim.AUTO for a given tensor.)r"   fromkeysrangerv   r   r+   DimAUTO)r   s    r4   _auto_dynamic_shaper  U  s.    ==vzz|,ell.>.>.C.CDDr=   c                   t        | t        j                        rt        |       S |  t        | t        t
        t        t        f      ryt        | d      rt        |       \  }}t        |      S t        |       t        t        t        t        fv r t        |       d | D              S t        |       t         u r-| j#                         D ci c]  \  }}|t        |       c}}S yc c}}w )u  Recursively build dynamic shapes for any input value.

    - Tensors → per-dimension Dim.AUTO spec.
    - Scalars / None → None (no dynamic dims).
    - Objects with ``__dict__`` (ModelOutput, Cache, …) → flat list of leaf specs,
      matching the ``TreeSpec(list, …)`` that torch.export produces for these types.
    - Lists / tuples → same container type, recursed element-wise.
    - Plain dicts → recursed dict of specs.
    - Everything else → None.
    Nr   c              3  2   K   | ]  }t        |        y wr@   )r&   ).0rB   s     r4   	<genexpr>z*get_auto_dynamic_shapes.<locals>.<genexpr>m  s     G13A6Gs   )r!   r   r   r  r   r   r   r   rJ   r   r&   r$   r   r   r   r   r"   rI   )r\   r   r  r]   rB   s        r4   r&   r&   Z  s     &%,,'"6**~FS%s,CDvz"#F+	&v..F|eS)44tF|GGGGF|t:@,,.I$!Q*1--II Js   C&)_cached_decode_position_ids_prefill_len"cached_rotary_positional_embeddingcached_sequence_lengthc              #  B  K   | j                         D cg c](  }t        D ]  }t        ||      r||t        ||      f * }}}|D ]  \  }}}t	        ||d        	 d |D ]  \  }}}t	        |||        yc c}}w # |D ]  \  }}}t	        |||        w xY ww)zSave each `_STATEFUL_CACHE_ATTRS` value, null it for the trace, restore on exit.

    FakeTensors that `torch.export` plants into these attributes during the trace are
    discarded by the restore.
    N)rL   _STATEFUL_CACHE_ATTRSrJ   r   setattr)r/   rP   attr	originalsr  ri   s         r4   r(   r(     s      mmo) 64  
wvt,--I  % $ad#$,&/ 	,"FD(FD(+	, '0 	,"FD(FD(+	,s(   B-A<BB #BBB)r/   r   r2   dict[str, Any])r/   r   r\   r   )NNF)
r   torch.Tensorr   r!  r   ztorch.Tensor | Noner   z(tuple[torch.Tensor, torch.Tensor] | Noner   r   )r   r$   r5   r   )r   r   r5   r$   )r   r   r   r   r5   r   )r   r   r   r   r5   r   )r   r   r5   ztuple[list, Any])r   r   )r   r   r5   r   )r   r$   )r   r$   )r/   r   )r   r!  r5   zdict[int, torch.export.Dim])r\   r   r5   r   )r/   ztorch.nn.Module);r9   
__future__r   r,   r   rW   r   collections.abcr   
contextlibr   typingr   r   r   utils.import_utilsr	   r
   r   baser   configsr   r   r   r   r   r   torch.exportr   cache_utilsr   modeling_utilsr   
get_logger__file__loggerr   r)   r*   r`   rl   ro   r   r   r   r   r   r   r   r   r   r   r   r'   r  r&   r  r(   r   r=   r4   <module>r/     s   0 #    
 * %   d d  ! V V ,#0 
		H	%4 Z 4 ~  0 ) )R mn. o.
 LM N& :- 7M?AA))< +/DHcAcA cA (	cA
 BcA cAL LZS^Q\dKVPhXV_a]]^cU9<=<*25Op/<db
:.)8E
H  , ,r=   