|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Dict, Iterable |
| 16 | +import torch |
| 17 | +from megatron.core import parallel_state |
| 18 | +from nemo.tron.config import ConfigContainer |
| 19 | + |
| 20 | + |
| 21 | +def get_batch_on_this_tp_rank(data_iterator: Iterable, cfg: ConfigContainer) -> Dict[str, torch.Tensor]: |
| 22 | + def _broadcast(item): |
| 23 | + if item is not None: |
| 24 | + torch.distributed.broadcast( |
| 25 | + item, |
| 26 | + parallel_state.get_tensor_model_parallel_src_rank(), |
| 27 | + group=parallel_state.get_tensor_model_parallel_group(), |
| 28 | + ) |
| 29 | + |
| 30 | + if parallel_state.get_tensor_model_parallel_rank() == 0: |
| 31 | + if data_iterator is not None: |
| 32 | + data = next(data_iterator) |
| 33 | + else: |
| 34 | + data = None |
| 35 | + |
| 36 | + batch = { |
| 37 | + "tokens": data["tokens"].cuda(non_blocking=True), |
| 38 | + "labels": data["labels"].cuda(non_blocking=True), |
| 39 | + "loss_mask": data["loss_mask"].cuda(non_blocking=True), |
| 40 | + "attention_mask": None if "attention_mask" not in data else data["attention_mask"].cuda(non_blocking=True), |
| 41 | + "position_ids": data["position_ids"].cuda(non_blocking=True), |
| 42 | + } |
| 43 | + |
| 44 | + if cfg.model_config.pipeline_model_parallel_size == 1: |
| 45 | + _broadcast(batch["tokens"]) |
| 46 | + _broadcast(batch["labels"]) |
| 47 | + _broadcast(batch["loss_mask"]) |
| 48 | + _broadcast(batch["attention_mask"]) |
| 49 | + _broadcast(batch["position_ids"]) |
| 50 | + |
| 51 | + elif parallel_state.is_pipeline_first_stage(): |
| 52 | + _broadcast(batch["tokens"]) |
| 53 | + _broadcast(batch["attention_mask"]) |
| 54 | + _broadcast(batch["position_ids"]) |
| 55 | + |
| 56 | + elif parallel_state.is_pipeline_last_stage(): |
| 57 | + _broadcast(batch["labels"]) |
| 58 | + _broadcast(batch["loss_mask"]) |
| 59 | + _broadcast(batch["attention_mask"]) |
| 60 | + |
| 61 | + else: |
| 62 | + mbs = cfg.train_config.micro_batch_size |
| 63 | + seq_length = cfg.model_config.seq_length |
| 64 | + tokens = torch.empty( |
| 65 | + (mbs, seq_length), |
| 66 | + dtype=torch.int64, |
| 67 | + device=torch.cuda.current_device(), |
| 68 | + ) |
| 69 | + labels = torch.empty( |
| 70 | + (mbs, seq_length), |
| 71 | + dtype=torch.int64, |
| 72 | + device=torch.cuda.current_device(), |
| 73 | + ) |
| 74 | + loss_mask = torch.empty( |
| 75 | + (mbs, seq_length), |
| 76 | + dtype=torch.float32, |
| 77 | + device=torch.cuda.current_device(), |
| 78 | + ) |
| 79 | + if cfg.dataset_config.create_attention_mask: |
| 80 | + attention_mask = torch.empty( |
| 81 | + ( |
| 82 | + mbs, |
| 83 | + 1, |
| 84 | + seq_length, |
| 85 | + seq_length, |
| 86 | + ), |
| 87 | + dtype=torch.bool, |
| 88 | + device=torch.cuda.current_device(), |
| 89 | + ) |
| 90 | + else: |
| 91 | + attention_mask = None |
| 92 | + position_ids = torch.empty( |
| 93 | + (mbs, seq_length), |
| 94 | + dtype=torch.int64, |
| 95 | + device=torch.cuda.current_device(), |
| 96 | + ) |
| 97 | + |
| 98 | + if cfg.model_config.pipeline_model_parallel_size == 1: |
| 99 | + _broadcast(tokens) |
| 100 | + _broadcast(labels) |
| 101 | + _broadcast(loss_mask) |
| 102 | + _broadcast(attention_mask) |
| 103 | + _broadcast(position_ids) |
| 104 | + |
| 105 | + elif parallel_state.is_pipeline_first_stage(): |
| 106 | + labels = None |
| 107 | + loss_mask = None |
| 108 | + |
| 109 | + _broadcast(tokens) |
| 110 | + _broadcast(attention_mask) |
| 111 | + _broadcast(position_ids) |
| 112 | + |
| 113 | + elif parallel_state.is_pipeline_last_stage(): |
| 114 | + tokens = None |
| 115 | + position_ids = None |
| 116 | + |
| 117 | + _broadcast(labels) |
| 118 | + _broadcast(loss_mask) |
| 119 | + _broadcast(attention_mask) |
| 120 | + |
| 121 | + batch = { |
| 122 | + "tokens": tokens, |
| 123 | + "labels": labels, |
| 124 | + "loss_mask": loss_mask, |
| 125 | + "attention_mask": attention_mask, |
| 126 | + "position_ids": position_ids, |
| 127 | + } |
| 128 | + |
| 129 | + return batch |
0 commit comments