3
EgI @ s d Z ddlZddlZddlZddlZddlmZ ddlm Z ddl
mZ ddlm
Z
mZ e
rfddlZejeZdd ZG d
d dZG dd
d
eZdadd Zdd Zdd Zdd Zdd ZdddZdS )z
Integration with Deepspeed
N)deepcopy)
partialmethod )dep_version_check)is_torch_availableloggingc C s t jjdd k S )N deepspeed) importlibutil find_spec r r @/usr/local/lib/python3.6/site-packages/transformers/deepspeed.pyis_deepspeed_available# s r c @ s\ e Zd ZdZdd Zdd ZdddZdd
dZdd
Zdd Z dd Z
dd Zdd ZdS )HfDeepSpeedConfigaJ
This object contains a DeepSpeed configuration dictionary and can be quickly queried for things like zero stage.
A `weakref` of this object is stored in the module's globals to be able to access the config from areas where
things like the Trainer object is not available (e.g. `from_pretrained` and `_get_resized_embeddings`). Therefore
it's important that this object remains alive while the program is still running.
[`Trainer`] uses the `HfTrainerDeepSpeedConfig` subclass instead. That subclass has logic to sync the configuration
with values of [`TrainingArguments`] by replacing special placeholder values: `"auto"`. Without this special logic
the DeepSpeed configuration is not modified in any way.
Args:
config_file_or_dict (`Union[str, Dict]`): path to DeepSpeed config file or dict.
c C s t | td t|tr$t|}n:t|trVtj|ddd}tj |}W d Q R X nt
d|| _| jdd| _
d| _| j s| j rtd d
g}t| jd| jdg}t||@ d
krd| _d S )Nr rzutf-8)encodingzJexpecting either a path to a DeepSpeed config file or a pre-populated dictzzero_optimization.stager FcpuZnvmez*zero_optimization.offload_optimizer.devicez&zero_optimization.offload_param.devicer T)set_hf_deepspeed_configr
isinstancedictr strioopenjsonload
ValueErrorconfig get_value_stage_offloadis_zero2is_zero3setlen)selfconfig_file_or_dictr fZoffload_devices_validZoffload_devicesr r r
__init__8 s$
zHfDeepSpeedConfig.__init__c C sH | j }|jd}|j }x&|D ]}|j|}|d krd |fS qW ||fS )N.)r splitpopget)r% ds_key_longr nodesds_keynoder r r
find_config_node[ s
z"HfDeepSpeedConfig.find_config_nodeNc C s&