o
    xi                     @  s   d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZ edeeef Z	 dZG d	d
 d
ZdddZdddddZdS )    )annotationsN)Sequence)AnyNewType)wandb_internal_pb2)
proto_util	telemetryBackendConfigDict_wandbc                   @  sb   e Zd ZdZd+d,ddZd-d
dZd.ddZd/ddZd0ddZd1d"d#Z	d2d'd(Z
d3d)d*ZdS )4ConfigStatezThe configuration of a run.Ntreedict[str, Any] | NonereturnNonec                 C  s   |pi | _ d S N)_tree)selfr    r   T/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/internal/sender_config.py__init__   s   
zConfigState.__init__dict[str, Any]c                 C  s   dd | j  D S )z+Returns the config settings minus "_wandb".c                 S  s   i | ]\}}|t kr||qS r   )_WANDB_INTERNAL_KEY).0kvr   r   r   
<dictcomp>    s    z3ConfigState.non_internal_config.<locals>.<dictcomp>)r   items)r   r   r   r   non_internal_config   s   zConfigState.non_internal_configconfig_recordwandb_internal_pb2.ConfigRecordc                 C  sB   |j D ]}| t|t|j q|jD ]	}| t| qdS )z#Applies update and remove commands.N)update_update_at_path	_key_pathjsonloads
value_jsonremove_delete_at_path)r   r   config_itemr   r   r   update_from_proto"   s   


zConfigState.update_from_protoold_config_treec                 C  s*   |  |g  dD ]
}|  |t|g qdS )z2Merges the config from a run that's being resumed.)viz	visualizezmask/class_labelsN)_add_unset_keys_from_subtreer   )r   r*   keyr   r   r   merge_resumed_config0   s   z ConfigState.merge_resumed_configpathSequence[str]c                 C  sX   t ||dd}|sdS t | j|dd}|dusJ | D ]\}}||vr)|||< qdS )z8Uses the given subtree for keys that aren't already set.FcreateNT)_subtreer   r   )r   r*   r0   old_subtreenew_subtreer.   valuer   r   r   r-   >   s   z(ConfigState._add_unset_keys_from_subtreetelemetry_recordtelemetry.TelemetryRecord	framework
str | Nonestart_time_millisintmetric_pbdictsSequence[dict[int, Any]]environment_record$wandb_internal_pb2.EnvironmentRecordr	   c                 C  s   | j  }|ti }|j}|r||d< |j}	|	r|	|d< |r#||d< |j}
|
r,|
|d< |jj|d< |jj	|d< ||d< t
||d< |rI||d	< |j}|rYt
|}||i|d
< tdd | j  D S )a  Returns a dictionary representation expected by the backend.

        The backend expects the configuration in a specific format, and the
        config is also used to store additional metadata about the run.

        Args:
            telemetry_record: Telemetry information to insert.
            framework: The detected framework used in the run (e.g. TensorFlow).
            start_time_millis: The run's start time in Unix milliseconds.
            metric_pbdicts: List of dict representations of metric protobuffers.
        python_versioncli_versionr:   huggingface_versionis_jupyter_runis_kaggle_kernel
start_timetmec                 S  s   i | ]
\}}|d |dqS )N)descr7   r   )r   r.   r7   r   r   r   r      s    	z/ConfigState.to_backend_dict.<locals>.<dictcomp>)r   copy
setdefaultr   rB   rC   rD   envjupyterkaggler   proto_encode_to_dict	writer_idmessage_to_dictr	   r   )r   r8   r:   r<   r>   r@   backend_dictwandb_internal
py_versionrC   rD   rR   environment_dictr   r   r   to_backend_dictO   s8   

	zConfigState.to_backend_dictkey_pathr7   r   c                 C  s4   t | j|dd dd}|dusJ |||d < dS )z.Sets the value at the path in the config tree.NTr2   r4   r   )r   rY   r7   subtreer   r   r   r!      s   zConfigState._update_at_pathc                 C  s.   t | j|dd dd}|r||d = dS dS )z3Removes the subtree at the path in the config tree.NrZ   Fr2   r[   )r   rY   r\   r   r   r   r'      s   zConfigState._delete_at_pathr   )r   r   r   r   )r   r   )r   r   r   r   )r*   r   r   r   )r*   r   r0   r1   r   r   )r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   r   r	   )rY   r1   r7   r   r   r   )rY   r1   r   r   )__name__
__module____qualname____doc__r   r   r)   r/   r-   rX   r!   r'   r   r   r   r   r      s    





Kr   r(   wandb_internal_pb2.ConfigItemr   r1   c                 C  s"   | j r| j S | jr| jgS td)z3Returns the key path referenced by the config item.z8Invalid ConfigItem: either key or nested_key must be set)
nested_keyr.   AssertionError)r(   r   r   r   r"      s   r"   Fr2   r   r   rY   r3   boolr   c                C  s8   |D ]}|  |}|s|ri }|| |< n dS |} q| S )z$Returns a subtree at the given path.N)get)r   rY   r3   r.   r\   r   r   r   r4      s   

r4   )r(   ra   r   r1   )r   r   rY   r1   r3   rd   r   r   )
__future__r   r#   collections.abcr   typingr   r   wandb.protor   wandb.sdk.libr   r   dictstrr	   r   r   r"   r4   r   r   r   r   <module>   s     
 