Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions deepspeed/runtime/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ def __init__(self, config: Union[str, dict], mpu=None, mesh_device=None):
self.world_size = 1
logger.info(f"Config mesh_device {mesh_device} world_size = {self.world_size}")
# If elastic-mode enabled, update compute + update _param_dict
elastic_batch_params = {}
self.elasticity_enabled = elasticity_enabled(self._param_dict)
if self.elasticity_enabled:
logger.info("DeepSpeed elasticity support enabled")
Expand Down Expand Up @@ -784,12 +785,18 @@ def __init__(self, config: Union[str, dict], mpu=None, mesh_device=None):

logger.info(f"[Elasticity] valid GPU counts: {valid_gpus}")

self._param_dict[TRAIN_BATCH_SIZE] = final_batch_size
self._param_dict[TRAIN_MICRO_BATCH_SIZE_PER_GPU] = micro_batch_size
self._param_dict[GRADIENT_ACCUMULATION_STEPS] = gradient_accu_steps

# Pass a copy so that user json is unmodified, e.g. for logging
self._initialize_params(copy.copy(self._param_dict))
elastic_batch_params = {
TRAIN_BATCH_SIZE: final_batch_size,
TRAIN_MICRO_BATCH_SIZE_PER_GPU: micro_batch_size,
GRADIENT_ACCUMULATION_STEPS: gradient_accu_steps,
}

# Pass a copy so that user json is unmodified, e.g. for logging. The elasticity
# overrides go into that copy for the same reason -- all three are top-level
# keys, so the shallow copy keeps them off the caller's dict.
param_dict = copy.copy(self._param_dict)
param_dict.update(elastic_batch_params)
self._initialize_params(param_dict)
self._configure_train_batch_size()
self._do_sanity_check()

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/runtime/test_ds_config_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,44 @@ def test_max_grad_norm_leaves_caller_config_untouched():
assert config_dict["optimizer"]["params"]["max_grad_norm"] == 1.0


def test_elasticity_leaves_caller_config_untouched():
# Same contract as above on the elasticity path: the resolved batch sizes belong in
# the copy handed to _initialize_params, not in the caller's dict.
#
# ignore_non_elastic_batch_info is deliberately left out. With it on, the batch-key
# guard is skipped and the second parse below cannot fail, so the test would pass
# whether or not the write-back is there.
config_dict = {
"elasticity": {
"enabled": True,
"max_train_batch_size": 4,
"micro_batch_sizes": [1, 2],
"min_gpus": 1,
"max_gpus": 4,
"min_time": 0,
"version": 0.1,
}
}
keys_before = set(config_dict)

ds_config = DeepSpeedConfig(config_dict)

assert set(config_dict) == keys_before
# The resolved values still reach the parsed config.
assert ds_config.train_batch_size == 4
assert ds_config.train_micro_batch_size_per_gpu == 2
assert ds_config.gradient_accumulation_steps == 2

# The write-back made the same dict unparsable a second time: the injected keys
# trip the guard that rejects batch parameters under elasticity, and its message
# names three keys the caller never wrote.
second = DeepSpeedConfig(config_dict)

assert second.train_batch_size == ds_config.train_batch_size
assert second.train_micro_batch_size_per_gpu == ds_config.train_micro_batch_size_per_gpu
assert second.gradient_accumulation_steps == ds_config.gradient_accumulation_steps


class TestConfigLoad(DistributedTest):
world_size = 1

Expand Down
Loading