
    sh                     z    S SK Jr  SSKJr  SSKJrJrJrJrJ	r	   \
\\4   rS\S\\\4   4S jrS\S\S\4S	 jrg
)    )STATE_DICT_TYPE   )_version)OBJ_PATHset_elementSTATE_DICT_ITEMtraverse_state_dicttraverse_state_dict_v_2_3
state_dictreturnc                    ^^ 0 m0 mS[         S[        SS4UU4S jjn[        R                  SL=(       a    [        R                  S:H  nU(       a  [	        X5        TT4$ [        X5        TT4$ )ao  
Flatten ``state_dict`` made of nested dicts and lists into a top level dictionary.

Use ``unflatten_state_dict`` to revert this process.
Returns:
    A tuple with the flatten state_dict and a mapping from original to new state_dict.
N.B. The new keys are derived from the object paths, joined by dot.
    For example: ``{ 'a': {'b':...}}`` results in the key `a.b`.
pathvaluer   Nc                    > SR                  [        [        U 5      5      nUT;   a  [        SU 35      eUTU'   U TU'   g )N.zduplicated flatten key )joinmapstr
ValueError)r   r   new_fqn	flattenedmappingss      }/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/distributed/checkpoint/_nested_dict.py	flat_copy%flatten_state_dict.<locals>.flat_copy*   sF    ((3sD>*i6wi@AA"	'     2_3)r   r   r   _derived_versionr
   r	   )r   r   	use_v_2_3r   r   s      @@r   flatten_state_dictr       s     "$I "H! ! !T ! ! 	!!-T(2K2Ku2T  !*8 h 	J2hr   mappingc                 Z    0 nU R                  5        H  u  p4[        X!U   U5        M     U$ )zaRestore the original nested state_dict according to ``mapping`` and the flattened ``state_dict``.)itemsr   )r   r!   nestedkeyr   s        r   unflatten_state_dictr&   >   s1     !F &&(
FCL%0 )Mr   N)%torch.distributed.checkpoint.metadatar    r   	_traverser   r   r   r	   r
   dictr   FLATTEN_MAPPINGtupler    r&    r   r   <module>r.      sj    B   sH}%  
?O+, F*9r   