|
| 1 | +""" |
| 2 | +Copyright 2020 The Microsoft DeepSpeed Team |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +from .constants import * |
| 7 | + |
| 8 | + |
| 9 | +class ElasticityError(Exception): |
| 10 | + """ |
| 11 | + Base exception for all elasticity related errors |
| 12 | + """ |
| 13 | + pass |
| 14 | + |
| 15 | + |
| 16 | +class ElasticityConfigError(ElasticityError): |
| 17 | + """ |
| 18 | + Elasticity configuration error |
| 19 | + """ |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +class ElasticityIncompatibleWorldSize(ElasticityError): |
| 24 | + """ |
| 25 | + Attempting to run a world size that is incompatible with a given elastic config |
| 26 | + """ |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +class ElasticityConfig: |
| 31 | + """ |
| 32 | + Elastic config object, constructed from a param dictionary that only contains elastic |
| 33 | + config parameters, example below: |
| 34 | +
|
| 35 | + If elasticity is enabled, user must specify (at least) max_train_batch_size |
| 36 | + and micro_batch_sizes. |
| 37 | +
|
| 38 | + { |
| 39 | + "enabled": true, |
| 40 | + "max_train_batch_size": 2000, |
| 41 | + "micro_batch_sizes": [2,4,6], |
| 42 | + "min_gpus": 1, |
| 43 | + "max_gpus" : 10000 |
| 44 | + "min_time": 20 |
| 45 | + "ignore_non_elastic_batch_info": false |
| 46 | + "version": 0.1 |
| 47 | + } |
| 48 | + """ |
| 49 | + def __init__(self, param_dict): |
| 50 | + self.enabled = param_dict.get(ENABLED, ENABLED_DEFAULT) |
| 51 | + if self.enabled: |
| 52 | + if MAX_ACCEPTABLE_BATCH_SIZE in param_dict: |
| 53 | + self.max_acceptable_batch_size = param_dict[MAX_ACCEPTABLE_BATCH_SIZE] |
| 54 | + else: |
| 55 | + raise ElasticityConfigError( |
| 56 | + f"Elasticity config missing {MAX_ACCEPTABLE_BATCH_SIZE}") |
| 57 | + if MICRO_BATCHES in param_dict: |
| 58 | + self.micro_batches = param_dict[MICRO_BATCHES] |
| 59 | + else: |
| 60 | + raise ElasticityConfigError(f"Elasticity config missing {MICRO_BATCHES}") |
| 61 | + else: |
| 62 | + self.max_acceptable_batch_size = param_dict.get( |
| 63 | + MAX_ACCEPTABLE_BATCH_SIZE, |
| 64 | + MAX_ACCEPTABLE_BATCH_SIZE_DEFAULT) |
| 65 | + self.micro_batches = param_dict.get(MICRO_BATCHES, MICRO_BATCHES_DEFAULT) |
| 66 | + self.min_gpus = param_dict.get(MIN_GPUS, MIN_GPUS_DEFAULT) |
| 67 | + self.max_gpus = param_dict.get(MAX_GPUS, MAX_GPUS_DEFAULT) |
| 68 | + self.min_time = param_dict.get(MIN_TIME, MIN_TIME_DEFAULT) |
| 69 | + self.version = param_dict.get(VERSION, VERSION_DEFAULT) |
| 70 | + self.prefer_larger_batch_size = param_dict.get(PREFER_LARGER_BATCH, |
| 71 | + PREFER_LARGER_BATCH_DEFAULT) |
| 72 | + self.ignore_non_elastic_batch_info = param_dict.get( |
| 73 | + IGNORE_NON_ELASTIC_BATCH_INFO, |
| 74 | + IGNORE_NON_ELASTIC_BATCH_INFO_DEFAULT) |
| 75 | + |
| 76 | + def repr(self): |
| 77 | + return self.__dict__ |
| 78 | + |
| 79 | + def __repr__(self): |
| 80 | + return json.dumps(self.__dict__, sort_keys=True, indent=4) |
0 commit comments