
    Phr                     v    d dl mZmZ d dlmZ ddlmZmZmZm	Z	 	 ee
ef   Zdedeeef   fdZdededefd	Zy
)    )DictTuple)STATE_DICT_TYPE   )traverse_state_dictset_elementOBJ_PATHSTATE_DICT_ITEM
state_dictreturnc                 X    i i dt         dt        ddffd}t        | |       fS )a  
    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                 |    dj                  t        t        |             }|v rt        d|       ||<   | |<   y )N.zduplicated flatten key )joinmapstr
ValueError)r   r   new_fqn	flattenedmappingss      tC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/checkpoint/_nested_dict.py	flat_copyz%flatten_state_dict.<locals>.flat_copy*   sF    ((3sD>*i6wi@AA"	'     )r	   r
   r   )r   r   r   r   s     @@r   flatten_state_dictr      s@     "$I "H! ! !T ! 
I.hr   mappingc                 Z    i }| j                         D ]  \  }}t        |||   |        |S )zaRestore the original nested state_dict according to ``mapping`` and the flattened ``state_dict``.)itemsr   )r   r   nestedkeyr   s        r   unflatten_state_dictr"   5   s5     !F &&(
UFGCL%0 )Mr   N)typingr   r   %torch.distributed.checkpoint.metadatar   	_traverser   r   r	   r
   r   FLATTEN_MAPPINGr   r"    r   r   <module>r(      sh      sH}%
?O+,4*9r   