Upload apex-master/tests/L0/run_transformer/test_microbatches.py with huggingface_hub
Browse files
apex-master/tests/L0/run_transformer/test_microbatches.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from typing import List, Optional
|
| 3 |
+
|
| 4 |
+
from torch.testing._internal import common_utils
|
| 5 |
+
|
| 6 |
+
logging.getLogger("torch").setLevel(logging.WARNING)
|
| 7 |
+
|
| 8 |
+
from apex.transformer import parallel_state
|
| 9 |
+
from apex.transformer.pipeline_parallel.utils import (
|
| 10 |
+
_reconfigure_microbatch_calculator,
|
| 11 |
+
get_micro_batch_size,
|
| 12 |
+
get_num_microbatches,
|
| 13 |
+
get_current_global_batch_size,
|
| 14 |
+
update_num_microbatches,
|
| 15 |
+
)
|
| 16 |
+
from apex.transformer.testing.distributed_test_base import NcclDistributedTestBase
|
| 17 |
+
from apex.transformer.testing.distributed_test_base import UccDistributedTestBase
|
| 18 |
+
|
| 19 |
+
logging.getLogger("apex").setLevel(logging.WARNING)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class MicrobatchCalculatorTestBase:
|
| 23 |
+
|
| 24 |
+
GLOBAL_BATCH_SIZE: int = 1024
|
| 25 |
+
MICRO_BATCH_SIZE: int = 1
|
| 26 |
+
|
| 27 |
+
def _test(self, rampup_batch_size: Optional[List[int]]) -> None:
|
| 28 |
+
for data_parallel_size in range(1, self.world_size + 1):
|
| 29 |
+
|
| 30 |
+
expected_global_batch_size = self.GLOBAL_BATCH_SIZE
|
| 31 |
+
expected_micro_batch_size = self.MICRO_BATCH_SIZE
|
| 32 |
+
if rampup_batch_size:
|
| 33 |
+
expected_global_batch_size = rampup_batch_size[0]
|
| 34 |
+
num_consumed_samples = 0
|
| 35 |
+
step_of_global_batch_size = rampup_batch_size[1]
|
| 36 |
+
threshold = rampup_batch_size[2]
|
| 37 |
+
|
| 38 |
+
if data_parallel_size > 1 and data_parallel_size % 2 != 0:
|
| 39 |
+
continue
|
| 40 |
+
if self.world_size % data_parallel_size != 0:
|
| 41 |
+
continue
|
| 42 |
+
msg = f"data_parallel_size: {data_parallel_size}"
|
| 43 |
+
parallel_state.initialize_model_parallel(
|
| 44 |
+
tensor_model_parallel_size_=self.world_size // data_parallel_size,
|
| 45 |
+
pipeline_model_parallel_size_=1,
|
| 46 |
+
)
|
| 47 |
+
self.assertEqual(data_parallel_size, parallel_state.get_data_parallel_world_size(), msg=msg)
|
| 48 |
+
|
| 49 |
+
_reconfigure_microbatch_calculator(
|
| 50 |
+
self.rank,
|
| 51 |
+
rampup_batch_size,
|
| 52 |
+
self.GLOBAL_BATCH_SIZE,
|
| 53 |
+
self.MICRO_BATCH_SIZE,
|
| 54 |
+
data_parallel_size,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
self.assertEqual(get_micro_batch_size(), expected_micro_batch_size, msg=msg)
|
| 58 |
+
self.assertEqual(get_num_microbatches(), expected_global_batch_size / expected_micro_batch_size / data_parallel_size, msg=msg)
|
| 59 |
+
current_global_batch_size = get_current_global_batch_size()
|
| 60 |
+
self.assertEqual(current_global_batch_size, expected_global_batch_size, msg=msg)
|
| 61 |
+
|
| 62 |
+
# Make sure `global_batch_size` equals to the final global batch size after
|
| 63 |
+
# certain number of updates.
|
| 64 |
+
if rampup_batch_size:
|
| 65 |
+
update_num_microbatches(current_global_batch_size)
|
| 66 |
+
for i in range(100):
|
| 67 |
+
current_global_batch_size = get_current_global_batch_size()
|
| 68 |
+
update_num_microbatches(current_global_batch_size)
|
| 69 |
+
current_global_batch_size = get_current_global_batch_size()
|
| 70 |
+
self.assertEqual(get_current_global_batch_size(), self.GLOBAL_BATCH_SIZE, msg=msg)
|
| 71 |
+
parallel_state.destroy_model_parallel()
|
| 72 |
+
|
| 73 |
+
def test_constant_microbatch_calculator(self):
|
| 74 |
+
self._test(rampup_batch_size=None)
|
| 75 |
+
|
| 76 |
+
def test_dynamic_microbatch_calculator(self):
|
| 77 |
+
self._test(rampup_batch_size=[256, 128, 500])
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class NcclMicrobatchCalculatorTest(MicrobatchCalculatorTestBase, NcclDistributedTestBase): pass
|
| 81 |
+
class UccMicrobatchCalculatorTest(MicrobatchCalculatorTestBase, UccDistributedTestBase): pass
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
common_utils.run_tests()
|