o
    Zh                     @   sv   d dl mZ ddlmZ ddlmZmZmZmZm	Z	 	 e
eef Zdedeeef fddZded	edefd
dZdS )    )STATE_DICT_TYPE   )_version)OBJ_PATHset_elementSTATE_DICT_ITEMtraverse_state_dicttraverse_state_dict_v_2_3
state_dictreturnc                    s`   i  i dt dtddf fdd}tjduotjdk}|r't| |  fS 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                    s:   d tt| }| v rtd| | |< | |< d S )N.zduplicated flatten key )joinmapstr
ValueError)r   r   Znew_fqnZ	flattenedZmappings X/var/www/auris/lib/python3.10/site-packages/torch/distributed/checkpoint/_nested_dict.py	flat_copy*   s
   z%flatten_state_dict.<locals>.flat_copyZ2_3)r   r   r   Z_derived_versionr	   r   )r
   r   Z	use_v_2_3r   r   r   flatten_state_dict   s   

r   mappingc                 C   s*   i }|   D ]\}}t||| | q|S )zaRestore the original nested state_dict according to ``mapping`` and the flattened ``state_dict``.)itemsr   )r
   r   nestedkeyr   r   r   r   unflatten_state_dict>   s   r   N)Z%torch.distributed.checkpoint.metadatar    r   	_traverser   r   r   r   r	   dictr   ZFLATTEN_MAPPINGtupler   r   r   r   r   r   <module>   s"   	

#