repo
stringlengths
7
90
file_url
stringlengths
81
315
file_path
stringlengths
4
228
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 14:38:15
2026-01-05 02:33:18
truncated
bool
2 classes
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/inference/hybridengine/polices/llama.py
colossalai/legacy/inference/hybridengine/polices/llama.py
from functools import partial from typing import List import torch from torch.nn import Module from transformers.models.llama.modeling_llama import ( LlamaAttention, LlamaDecoderLayer, LlamaForCausalLM, LlamaModel, LlamaRMSNorm, ) from colossalai.shardformer.policies.base_policy import ModulePolicyDescription, SubModuleReplacementDescription # import colossalai from colossalai.shardformer.policies.llama import LlamaForCausalLMPolicy from ..modeling._utils import init_to_get_rotary from ..modeling.llama import LlamaInferenceForwards try: from colossalai.kernel.triton import rmsnorm_forward HAS_TRITON_RMSNORM = True except: print("you should install triton from https://github.com/openai/triton") HAS_TRITON_RMSNORM = False def get_triton_rmsnorm_forward(): if HAS_TRITON_RMSNORM: def _triton_rmsnorm_forward(self: LlamaRMSNorm, hidden_states: torch.Tensor): return rmsnorm_forward(hidden_states, self.weight.data, self.variance_epsilon) return _triton_rmsnorm_forward else: return None class LlamaModelInferPolicy(LlamaForCausalLMPolicy): def __init__(self) -> None: super().__init__() def module_policy(self): policy = super().module_policy() if self.shard_config.inference_gptq: from colossalai.inference.quant.gptq.cai_gptq import ColCaiQuantLinear, RowCaiQuantLinear decoder_attribute_replacement = { "self_attn.hidden_size": self.model.config.hidden_size // self.shard_config.tensor_parallel_size, "self_attn.num_heads": self.model.config.num_attention_heads // self.shard_config.tensor_parallel_size, } policy[LlamaDecoderLayer] = ModulePolicyDescription( attribute_replacement=decoder_attribute_replacement, sub_module_replacement=[ SubModuleReplacementDescription( suffix="self_attn.q_proj", target_module=ColCaiQuantLinear, kwargs={"split_num": 1}, ), SubModuleReplacementDescription( suffix="self_attn.k_proj", target_module=ColCaiQuantLinear, kwargs={"split_num": 1}, ), SubModuleReplacementDescription( suffix="self_attn.v_proj", target_module=ColCaiQuantLinear, kwargs={"split_num": 1}, ), SubModuleReplacementDescription( suffix="self_attn.o_proj", target_module=RowCaiQuantLinear, kwargs={"split_num": 1}, ), SubModuleReplacementDescription( suffix="mlp.gate_proj", target_module=ColCaiQuantLinear, kwargs={"split_num": 1}, ), SubModuleReplacementDescription( suffix="mlp.up_proj", target_module=ColCaiQuantLinear, kwargs={"split_num": 1}, ), SubModuleReplacementDescription( suffix="mlp.down_proj", target_module=RowCaiQuantLinear, kwargs={"split_num": 1}, ), ], ) self.shard_config._infer() infer_forward = LlamaInferenceForwards.llama_model_forward method_replacement = {"forward": partial(infer_forward)} self.append_or_create_method_replacement(description=method_replacement, policy=policy, target_key=LlamaModel) infer_forward = LlamaInferenceForwards.llama_decoder_layer_forward method_replacement = {"forward": partial(infer_forward)} self.append_or_create_method_replacement( description=method_replacement, policy=policy, target_key=LlamaDecoderLayer ) infer_forward = LlamaInferenceForwards.llama_flash_attn_kvcache_forward method_replacement = {"forward": partial(infer_forward)} self.append_or_create_method_replacement( description=method_replacement, policy=policy, target_key=LlamaAttention ) if self.pipeline_stage_manager: # set None as default self.set_pipeline_forward( model_cls=LlamaForCausalLM, new_forward=LlamaInferenceForwards.llama_causal_lm_forward, policy=policy ) infer_forward = None if HAS_TRITON_RMSNORM: infer_forward = get_triton_rmsnorm_forward() if infer_forward is not None: method_replacement = {"forward": partial(infer_forward)} self.append_or_create_method_replacement( description=method_replacement, policy=policy, target_key=LlamaRMSNorm ) return policy def postprocess(self): init_to_get_rotary(self.model.model) return self.model def get_held_layers(self) -> List[Module]: """Get pipeline layers for current stage.""" stage_manager = self.pipeline_stage_manager held_layers = super().get_held_layers() if stage_manager.is_first_stage(): held_layers.append(self.model.lm_head) return held_layers
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/inference/hybridengine/polices/__init__.py
colossalai/legacy/inference/hybridengine/polices/__init__.py
from .llama import LlamaModelInferPolicy __all__ = ["LlamaModelInferPolicy"]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/inference/pipeline/microbatch_manager.py
colossalai/legacy/inference/pipeline/microbatch_manager.py
from enum import Enum from typing import Dict import torch from ..tensor_parallel.batch_infer_state import BatchInferState from ..tensor_parallel.kvcache_manager import MemoryManager __all__ = "MicroBatchManager" class Status(Enum): PREFILL = 1 GENERATE = 2 DONE = 3 COOLDOWN = 4 class MicroBatchDescription: """ This is the class to record the information of each microbatch, and also do some update operation. This class is the base class of `HeadMicroBatchDescription` and `BodyMicroBatchDescription`, for more details, please refer to the doc of these two classes blow. Args: inputs_dict (Dict[str, torch.Tensor]): the inputs of current stage. The key should have `input_ids` and `attention_mask`. output_dict (Dict[str, torch.Tensor]): the outputs of previous stage. The key should have `hidden_states` and `past_key_values`. """ def __init__( self, inputs_dict: Dict[str, torch.Tensor], max_input_len: int, max_output_len: int, cache_manager: MemoryManager, ) -> None: self.mb_length = inputs_dict["input_ids"].shape[-1] self.target_length = self.mb_length + max_output_len self.infer_state = BatchInferState.init_from_batch( batch=inputs_dict, max_input_len=max_input_len, max_output_len=max_output_len, cache_manager=cache_manager ) # print(f"[init] {inputs_dict}, {max_input_len}, {max_output_len}, {cache_manager}, {self.infer_state}") def update(self, *args, **kwargs): pass @property def state(self): """ Return the state of current micro batch, when current length is equal to target length, the state is DONE, otherwise GENERATE """ # TODO: add the condition for early stopping if self.cur_length == self.target_length: return Status.DONE elif self.cur_length == self.target_length - 1: return Status.COOLDOWN else: return Status.GENERATE @property def cur_length(self): """ Return the current sequence length of micro batch """ class HeadMicroBatchDescription(MicroBatchDescription): """ This class is used to record the information of the first stage of pipeline, the first stage should have attributes `input_ids` and `attention_mask` and `new_tokens`, and the `new_tokens` is the tokens generated by the first stage. Also due to the schedule of pipeline, the operation to update the information and the condition to determine the state is different from other stages. Args: inputs_dict (Dict[str, torch.Tensor]): the inputs of current stage. The key should have `input_ids` and `attention_mask`. output_dict (Dict[str, torch.Tensor]): the outputs of previous stage. The key should have `hidden_states` and `past_key_values`. """ def __init__( self, inputs_dict: Dict[str, torch.Tensor], max_input_len: int, max_output_len: int, cache_manager: MemoryManager, ) -> None: super().__init__(inputs_dict, max_input_len, max_output_len, cache_manager) assert inputs_dict is not None assert inputs_dict.get("input_ids") is not None and inputs_dict.get("attention_mask") is not None self.input_ids = inputs_dict["input_ids"] self.attn_mask = inputs_dict["attention_mask"] self.new_tokens = None def update(self, new_token: torch.Tensor = None): if new_token is not None: self._update_newtokens(new_token) if self.state is not Status.DONE and new_token is not None: self._update_attnmask() def _update_newtokens(self, new_token: torch.Tensor): if self.new_tokens is None: self.new_tokens = new_token else: self.new_tokens = torch.cat([self.new_tokens, new_token], dim=-1) def _update_attnmask(self): self.attn_mask = torch.cat( (self.attn_mask, torch.ones((self.attn_mask.shape[0], 1), dtype=torch.int64, device="cuda")), dim=-1 ) @property def cur_length(self): """ When there is no new_token, the length is mb_length, otherwise the sequence length is `mb_length` plus the length of new_token """ if self.new_tokens is None: return self.mb_length else: return self.mb_length + len(self.new_tokens[0]) class BodyMicroBatchDescription(MicroBatchDescription): """ This class is used to record the information of the stages except the first stage of pipeline, the stages should have attributes `hidden_states` and `past_key_values`, Args: inputs_dict (Dict[str, torch.Tensor]): will always be `None`. Other stages only receive hiddenstates from previous stage. """ def __init__( self, inputs_dict: Dict[str, torch.Tensor], max_input_len: int, max_output_len: int, cache_manager: MemoryManager, ) -> None: super().__init__(inputs_dict, max_input_len, max_output_len, cache_manager) @property def cur_length(self): """ When there is no kv_cache, the length is mb_length, otherwise the sequence length is `kv_cache[0][0].shape[-2]` plus 1 """ return self.infer_state.seq_len.max().item() class MicroBatchManager: """ MicroBatchManager is a class that manages the micro batch. Args: stage (int): stage id of current stage. micro_batch_size (int): the micro batch size. micro_batch_buffer_size (int): the buffer size for micro batch. Normally, it should be the same as the number of pipeline stages. """ def __init__( self, stage: int, micro_batch_size: int, micro_batch_buffer_size: int, max_input_len: int, max_output_len: int, cache_manager_list: MemoryManager, ): self.stage = stage self.micro_batch_size = micro_batch_size self.buffer_size = micro_batch_buffer_size self.max_input_len = max_input_len self.max_output_len = max_output_len self.cache_manager_list = cache_manager_list self.mb_description_buffer = {} self.new_tokens_buffer = {} self.idx = 0 def add_description(self, inputs_dict: Dict[str, torch.Tensor]): if self.stage == 0: self.mb_description_buffer[self.idx] = HeadMicroBatchDescription( inputs_dict, self.max_input_len, self.max_output_len, self.cache_manager_list[self.idx] ) else: self.mb_description_buffer[self.idx] = BodyMicroBatchDescription( inputs_dict, self.max_input_len, self.max_output_len, self.cache_manager_list[self.idx] ) def step(self, new_token: torch.Tensor = None): """ Update the state if microbatch manager, 2 conditions. 1. For first stage in PREFILL, receive inputs and outputs, `_add_description` will save its inputs. 2. For other condition, only receive the output of previous stage, and update the description. Args: inputs_dict (Dict[str, torch.Tensor]): the inputs of current stage. The key should have `input_ids` and `attention_mask`. output_dict (Dict[str, torch.Tensor]): the outputs of previous stage. The key should have `hidden_states` and `past_key_values`. new_token (torch.Tensor): the new token generated by current stage. """ # Add description first if the description is None self.cur_description.update(new_token) return self.cur_state def export_new_tokens(self): new_tokens_list = [] for i in self.mb_description_buffer.values(): new_tokens_list.extend(i.new_tokens.tolist()) return new_tokens_list def is_micro_batch_done(self): if len(self.mb_description_buffer) == 0: return False for mb in self.mb_description_buffer.values(): if mb.state != Status.DONE: return False return True def clear(self): self.mb_description_buffer.clear() for cache in self.cache_manager_list: cache.free_all() def next(self): self.idx = (self.idx + 1) % self.buffer_size def _remove_description(self): self.mb_description_buffer.pop(self.idx) @property def cur_description(self) -> MicroBatchDescription: return self.mb_description_buffer.get(self.idx) @property def cur_infer_state(self): if self.cur_description is None: return None return self.cur_description.infer_state @property def cur_state(self): """ Return the state of current micro batch, when current description is None, the state is PREFILL """ if self.cur_description is None: return Status.PREFILL return self.cur_description.state
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/inference/pipeline/__init__.py
colossalai/legacy/inference/pipeline/__init__.py
from .microbatch_manager import MicroBatchManager __all__ = ["MicroBatchManager"]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/inference/pipeline/benchmark/benchmark.py
colossalai/legacy/inference/pipeline/benchmark/benchmark.py
import argparse import time import torch import torch.distributed as dist import transformers import colossalai from colossalai.inference import PPInferEngine from colossalai.inference.pipeline.policies import LlamaModelInferPolicy GIGABYTE = 1024**3 MEGABYTE = 1024 * 1024 colossalai.launch_from_torch() def data_gen(batch_size: int = 4, seq_len: int = 512): input_ids = torch.randint(10, 30000, (1, seq_len), dtype=torch.int32) attention_mask = torch.ones((1, seq_len), dtype=torch.int32) data = dict(input_ids=input_ids, attention_mask=attention_mask) for k, v in data.items(): if torch.is_tensor(v) or "Tensor" in v.__class__.__name__: new_shape = [1] * v.dim() new_shape[0] = batch_size data[k] = v.to("cuda").repeat(*new_shape) return data def print_details_info(timestamps, model_config, args, whole_end2end): if dist.get_rank() == 0: prefill = [] encoder = [] end2end = [] for timestamp in timestamps: prefill.append(timestamp[1] - timestamp[0]) encoder.append( sum(timestamp[i + 1] - timestamp[i] for i in range(1, len(timestamp) - 1)) / (len(timestamp) - 2) ) end2end.append(timestamp[-1] - timestamp[0]) print(whole_end2end) with open( f"{args.log_path}/llama-{args.model}{args.dtype}_pp{args.pp_size}_{args.seq_len}_{args.new_length}_bsz{args.batch_size}_mbsz{args.mb_size}.log", "w+", ) as f: mb_avg_end2end = sum(end2end) / len(end2end) mb_avg_latency = mb_avg_end2end / (args.new_length * args.mb_size) whole_avg_latency = whole_end2end / (args.new_length * args.batch_size) num_layers = getattr(model_config, "num_layers", model_config.num_hidden_layers) num_parameters = num_layers * model_config.hidden_size * model_config.hidden_size * 12 / args.pp_size if args.dtype in ["fp16", "bf16"]: num_bytes = 2 else: num_bytes = 4 f.write( f"llama-{args.model}{args.dtype}_pp{args.pp_size}, input_len:{args.seq_len}, output_len:{args.new_length}, bsz:{args.batch_size}, mbsz:{args.mb_size}\n" ) f.write("Average prefill time: {0:8.2f} ms\n".format(sum(prefill) / len(prefill) * 1000)) f.write("Average encode time: {0:8.2f} ms\n".format(sum(encoder) / len(encoder) * 1000)) f.write("Average micro batch end2end time: {0:8.2f} ms\n".format(mb_avg_end2end * 1000)) f.write("Average micro batch Per Token Latency: {0:8.2f} ms\n".format(mb_avg_latency * 1000)) f.write("Whole batch end2end time: {0:8.2f} ms\n".format(whole_end2end * 1000)) f.write("Whole batch Per Token Latency: {0:8.2f} ms\n".format(whole_avg_latency * 1000)) f.write("Throughput: {} tokens/s\n".format((1000 / (whole_avg_latency * 1000)))) f.write("flops: {0:8.2f} TFlops/s\n".format(1 / whole_avg_latency * num_parameters * num_bytes / 1e12)) f.write("----------------------------------------------------------\n") if torch.cuda.is_available(): current_device = torch.cuda.current_device() # free memory and the total available memory in bytes global_free_memory, total_GPU_memory_occupied = torch.cuda.mem_get_info() memory_allocated = torch.cuda.memory_allocated() max_memory_allocated = torch.cuda.max_memory_allocated() memory_reserved = torch.cuda.memory_reserved() max_memory_reserved = torch.cuda.max_memory_reserved() with open( f"{args.log_path}/llama-{args.model}{args.dtype}_pp{args.pp_size}_{args.seq_len}_{args.new_length}_bsz{args.batch_size}_mbsz{args.mb_size}.log", "a", ) as f: f.write( f"\nCurrently using GPU: {current_device}\n" f"free memory : {global_free_memory / GIGABYTE:.4f} GB,\n" f"total memory: {total_GPU_memory_occupied / GIGABYTE:.4f} GB,\n" f"memory allocated: {memory_allocated / GIGABYTE:.4f} GB,\n" f"Max CUDA memory allocated: {max_memory_allocated / GIGABYTE:.4f} GB,\n" f"memory reserved/cached: {memory_reserved / GIGABYTE:.4f} GB,\n" f"Max CUDA memory reserved/cached: {max_memory_reserved / GIGABYTE:.4f} GB,\n" ) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--model", default="toy", help="the size of model") parser.add_argument("-b", "--batch_size", type=int, default=8, help="batch size") parser.add_argument("-s", "--seq_len", type=int, default=8, help="sequence length") parser.add_argument("--new_length", type=int, default=4, help="new tokens length") parser.add_argument("--mb_size", type=int, default=1, help="micro_batch_size") parser.add_argument("--pp_size", type=int, default=2, help="pipeline size") parser.add_argument("--log_path", type=str, default="./log", help="where to store the benchmark log") parser.add_argument("--dtype", type=str, default="fp16", help="data type") args = parser.parse_args() if args.model == "toy": model = transformers.LlamaForCausalLM(transformers.LlamaConfig(num_hidden_layers=8)) elif args.model == "7b": model = transformers.LlamaForCausalLM(transformers.AutoConfig.from_pretrained("decapoda-research/llama-7b-hf")) elif args.model == "13b": model = transformers.LlamaForCausalLM(transformers.AutoConfig.from_pretrained("decapoda-research/llama-13b-hf")) else: raise NotImplementedError engine = PPInferEngine( pp_size=args.pp_size, dtype=args.dtype, micro_batch_size=args.mb_size, new_length=args.new_length, model=model, model_policy=LlamaModelInferPolicy(), verbose=True, max_batch_size=args.mb_size, max_input_len=args.seq_len, max_output_len=args.seq_len + args.new_length + 256, ) data = data_gen(args.batch_size, args.seq_len) torch.cuda.synchronize() whole_end2end = time.time() output, timestamps = engine.inference([data]) torch.cuda.synchronize() whole_end2end = time.time() - whole_end2end print_details_info(timestamps, model.config, args, whole_end2end)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/registry/registry.py
colossalai/legacy/registry/registry.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- from types import ModuleType from typing import List class Registry: """This is a registry class used to register classes and modules so that a universal object builder can be enabled. Args: name (str): The name of the registry . third_party_library (list, optional): List of third party libraries which are used in the initialization of the register module. """ def __init__(self, name: str, third_party_library: List[ModuleType] = None): self._name = name self._registry = dict() self._third_party_lib = third_party_library @property def name(self): return self._name def register_module(self, module_class): """Registers a module represented in `module_class`. Args: module_class (class): The module to be registered. Returns: class: The module to be registered, so as to use it normally if via importing. Raises: AssertionError: Raises an AssertionError if the module has already been registered before. """ module_name = module_class.__name__ assert module_name not in self._registry, f"{module_name} not found in {self.name}" self._registry[module_name] = module_class # return so as to use it normally if via importing return module_class def get_module(self, module_name: str): """Retrieves a module with name `module_name` and returns the module if it has already been registered before. Args: module_name (str): The name of the module to be retrieved. Returns: :class:`object`: The retrieved module or None. Raises: NameError: Raises a NameError if the module to be retrieved has neither been registered directly nor as third party modules before. """ if module_name in self._registry: return self._registry[module_name] elif self._third_party_lib is not None: for lib in self._third_party_lib: if hasattr(lib, module_name): return getattr(lib, module_name) raise NameError(f"Module {module_name} not found in the registry {self.name}") def has(self, module_name: str): """Searches for a module with name `module_name` and returns a boolean value indicating whether the module has been registered directly or as third party modules before. Args: module_name (str): The name of the module to be searched for. Returns: bool: A boolean value indicating whether the module has been registered directly or as third party modules before. """ found_flag = module_name in self._registry if self._third_party_lib: for lib in self._third_party_lib: if hasattr(lib, module_name): found_flag = True break return found_flag
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/registry/__init__.py
colossalai/legacy/registry/__init__.py
import torch.distributed.optim as dist_optim import torch.nn as nn import torch.optim as optim from .registry import Registry LAYERS = Registry("layers", third_party_library=[nn]) MODELS = Registry("models") OPTIMIZERS = Registry("optimizers", third_party_library=[optim, dist_optim]) DATASETS = Registry("datasets") DIST_GROUP_INITIALIZER = Registry("dist_group_initializer") GRADIENT_HANDLER = Registry("gradient_handler") LOSSES = Registry("losses", third_party_library=[nn]) HOOKS = Registry("hooks") TRANSFORMS = Registry("transforms") DATA_SAMPLERS = Registry("data_samplers") LR_SCHEDULERS = Registry("lr_schedulers") SCHEDULE = Registry("schedules") OPHOOKS = Registry("ophooks")
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/parallel_context.py
colossalai/legacy/context/parallel_context.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import random import socket from collections import Counter from typing import Union import numpy as np import torch import torch.distributed as dist from colossalai.context.config import Config from colossalai.context.singleton_meta import SingletonMeta from colossalai.legacy.constants import ALLOWED_MODES, INITIALIZER_MAPPING from colossalai.legacy.global_variables import tensor_parallel_env as env from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from colossalai.logging import get_dist_logger from .parallel_mode import ParallelMode from .random import add_seed, get_seeds, set_mode class ParallelContext(metaclass=SingletonMeta): """This class provides interface functions for users to get the parallel context, such as the global rank, the local rank, the world size, etc. of each device. Note: The parallel_mode used in this class should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ def __init__(self): # distributed settings self._global_ranks = dict() self._local_ranks = dict() self._world_sizes = dict() self._groups = dict() self._cpu_groups = dict() self._ranks_in_group = dict() # load config from file self._config = None # default 3D parallel args, will be overwritten during process group initialization self.world_size = 1 self.data_parallel_size = 1 self.pipeline_parallel_size = 1 self.tensor_parallel_size = 1 self.num_processes_on_current_node = -1 self.virtual_pipeline_parallel_size = None self.virtual_pipeline_parallel_rank = None # logging self._verbose = False self._logger = None @property def config(self): return self._config @property def verbose(self): return self._verbose @verbose.setter def verbose(self, verbose_: bool): self._verbose = verbose_ @property def logger(self): if self._logger is None: self._logger = get_dist_logger() return self._logger def load_config(self, config: Union[dict, str]): """Loads the configuration from either a dict or a file. Args: config (dict or str): Either a dict containing the configuration information or the filename of a file containing the configuration information. Raises: TypeError: Raises a TypeError if `config` is neither a dict nor a str. """ if isinstance(config, str): self._config = Config.from_file(config) elif isinstance(config, dict): self._config = Config(config) else: raise TypeError("Invalid type for config, only dictionary or string is supported") def detect_num_processes_on_current_node(self): hostname = socket.gethostname() hostname_list = [None for _ in range(self.get_world_size(ParallelMode.GLOBAL))] dist.all_gather_object(hostname_list, hostname, group=self.get_group(ParallelMode.GLOBAL)) counter = Counter(hostname_list) self.num_processes_on_current_node = counter[hostname] @staticmethod def _check_parallel_mode(parallel_mode: ParallelMode): assert isinstance( parallel_mode, ParallelMode ), f"expected the argument parallel_mode to be of enum ParallelMode, but got {type(parallel_mode)}" def get_global_rank(self): """Returns the global rank of the current device. Returns: int: The global rank of the current device """ return self._global_ranks[ParallelMode.GLOBAL] def add_global_rank(self, parallel_mode: ParallelMode, rank: int): """Adds the global rank of the current device for `parallel_mode` to the context. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The parallel mode for the rank. rank (int): The rank to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._global_ranks[parallel_mode] = rank def get_local_rank(self, parallel_mode: ParallelMode): """Returns the local rank of the current device. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: int: The local rank of the current device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) return self._local_ranks[parallel_mode] def _add_local_rank(self, parallel_mode: ParallelMode, rank: int): """Adds the local rank of the current device for `parallel_mode` to the context. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The parallel mode for the rank. rank (int): The rank to be added. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._local_ranks[parallel_mode] = rank def get_next_global_rank(self, parallel_mode: ParallelMode): """Returns the global rank of the next device. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: int: The global rank of the next device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) # get rank and world size local_rank = self.get_local_rank(parallel_mode) world_size = self.get_world_size(parallel_mode) ranks_in_group = self.get_ranks_in_group(parallel_mode) return ranks_in_group[(local_rank + 1) % world_size] def get_prev_global_rank(self, parallel_mode: ParallelMode): """Returns the global rank of the previous device. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: int: The global rank of the previous device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) # get rank and world size local_rank = self.get_local_rank(parallel_mode) world_size = self.get_world_size(parallel_mode) ranks_in_group = self.get_ranks_in_group(parallel_mode) return ranks_in_group[(local_rank - 1) % world_size] def is_first_rank(self, parallel_mode: ParallelMode): """Returns a boolean value indicating whether the current device is the first one among its group for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: bool: a boolean value indicating whether the current device is the first one among its group for `parallel_mode`. """ rank = self.get_local_rank(parallel_mode) return rank == 0 def is_last_rank(self, parallel_mode: ParallelMode): """Returns a boolean value indicating whether the current device is the last one among its group for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: bool: a boolean value indicating whether the current device is the first one among its group for `parallel_mode`. """ rank = self.get_local_rank(parallel_mode) world_size = self.get_world_size(parallel_mode) return rank == world_size - 1 def is_pipeline_first_stage(self, ignore_virtual=False): if not ignore_virtual: if self.virtual_pipeline_parallel_size is not None and self.virtual_pipeline_parallel_rank != 0: return False return self.is_first_rank(ParallelMode.PIPELINE) def is_pipeline_last_stage(self, ignore_virtual=False): if not ignore_virtual: if ( self.virtual_pipeline_parallel_size is not None and self.virtual_pipeline_parallel_rank != self.virtual_pipeline_parallel_size - 1 ): return False return self.is_last_rank(ParallelMode.PIPELINE) def get_world_size(self, parallel_mode: ParallelMode): """Returns the world size for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: int: The world size for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) return self._world_sizes[parallel_mode] def _add_world_size(self, parallel_mode: ParallelMode, world_size: int): """Adds world size for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The parallel mode corresponding to the process group world_size (int): The world size to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._world_sizes[parallel_mode] = world_size def get_group(self, parallel_mode: ParallelMode): """Returns the group of the current device for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: torch.distributed.ProcessGroup: The group of the current device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) return self._groups[parallel_mode] def _add_group(self, parallel_mode: ParallelMode, group: dist.ProcessGroup): """Adds the group of the current device for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. group (torch.distributed.ProcessGroup): The group to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._groups[parallel_mode] = group def get_cpu_group(self, parallel_mode: ParallelMode): """Returns the Gloo group of the current device for `parallel_mode`. :param parallel_mode: The chosen parallel mode :type parallel_mode: :class:`colossalai.legacy.context.ParallelMode` :raises AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode` :return: The group of the current device for `parallel_mode` :rtype: torch.distributed.ProcessGroup """ self._check_parallel_mode(parallel_mode) return self._cpu_groups[parallel_mode] def _add_cpu_group(self, parallel_mode: ParallelMode, group: dist.ProcessGroup): """Adds the Gloo group of the current device for `parallel_mode`. :param parallel_mode: The chosen parallel mode :type parallel_mode: :class:`colossalai.legacy.context.ParallelMode` :param group: The group to be added :type group: torch.distributed.ProcessGroup :raises AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode` """ self._check_parallel_mode(parallel_mode) self._cpu_groups[parallel_mode] = group def get_ranks_in_group(self, parallel_mode: ParallelMode): """Returns the rank of the current device for `parallel_mode` in the group. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. Returns: int: The rank of the current device for `parallel_mode` in the group. """ self._check_parallel_mode(parallel_mode) return self._ranks_in_group[parallel_mode] def _add_ranks_in_group(self, parallel_mode: ParallelMode, ranks: list): """Adds the ranks of the current device for `parallel_mode` in the group. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. ranks (list): List of ranks to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._ranks_in_group[parallel_mode] = ranks def init_global_dist(self, rank: int, world_size: int, backend: str, host: str, port: int): """Initializes the global distributed environment Args: rank (int): rank for the default process group. world_size (int): world size of the default process group. backend (str): backend for ``torch.distributed`` host (str): the master address for distributed training. port (str): the master port for distributed training """ # initialize the default process group init_method = f"tcp://[{host}]:{port}" dist.init_process_group(rank=rank, world_size=world_size, backend=backend, init_method=init_method) # None will give the default global process group for pytorch dist operations ranks = list(range(world_size)) cpu_group = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else None self._register_dist(rank, world_size, dist.GroupMember.WORLD, cpu_group, ranks, ParallelMode.GLOBAL) self.add_global_rank(ParallelMode.GLOBAL, rank) def _register_dist(self, local_rank, world_size, process_group, cpu_group, ranks_in_group, mode): self._add_local_rank(mode, local_rank) self._add_world_size(mode, world_size) self._add_group(mode, process_group) self._add_cpu_group(mode, cpu_group) self._add_ranks_in_group(mode, ranks_in_group) def check_sanity(self): """Checks sanity of the parallel context. Raises: AssertionError: Raises an AssertionError if the world size does not equal to the product of data parallel size, pipeline parallel size and tensor parallel size. """ dps = self.data_parallel_size pps = self.pipeline_parallel_size tps = self.tensor_parallel_size ws = self.world_size assert ws == dps * pps * tps, ( f"Expected the world size {ws} to be equal to data" f" parallel size ({dps}) * pipeline parallel size " f"({pps}) * tensor parallel size ({tps})" ) def _set_parallel_size_from_config(self, config: dict, key: str, attr_name: str): if key in config: ele = config[key] if isinstance(ele, int): setattr(self, attr_name, ele) elif isinstance(ele, dict): setattr(self, attr_name, ele["size"]) else: raise NotImplementedError( f'{"Parallel configuration does not support this kind of argument, please use int or dict"}' ) def init_parallel_groups(self): """Initializes the parallel groups. Raises: AssertionError: Raises an AssertionError if the field parallel is not present in the config file. """ # get rank and world size rank = self.get_global_rank() world_size = self.get_world_size(ParallelMode.GLOBAL) self.world_size = world_size # set parallel size as attributes for global context parallel_config = self.config.get("parallel", None) if parallel_config is not None: self._set_parallel_size_from_config(parallel_config, "pipeline", "pipeline_parallel_size") self._set_parallel_size_from_config(parallel_config, "tensor", "tensor_parallel_size") # the user should not set the data parallel size manually # instead, it should be calculated based on other parallel config self.data_parallel_size = self.world_size // (self.pipeline_parallel_size * self.tensor_parallel_size) # get the tensor parallel mode and check tensor_parallel_mode = None if parallel_config is not None and "tensor" in parallel_config and "mode" in parallel_config["tensor"]: tensor_parallel_mode = parallel_config["tensor"]["mode"] assert ( tensor_parallel_mode in ALLOWED_MODES ), f"mode in the parallel config must be set to one of {ALLOWED_MODES}" env.mode = tensor_parallel_mode self.check_sanity() pg_init = [] # LSG: init data parallel process group for compatibility with other parallel module such as zero pg_init.append(dict(type=INITIALIZER_MAPPING["data"])) # LSG: init model parallel process group for compatibility with amp and clip grad pg_init.append(dict(type=INITIALIZER_MAPPING["model"])) if self.pipeline_parallel_size > 1: pg_init.append(dict(type=INITIALIZER_MAPPING["pipeline"])) pg_init.append(dict(type=INITIALIZER_MAPPING["tensor"])) # init specific tensor parallel group if tensor_parallel_mode is not None: tensor_parallel_cfg = parallel_config["tensor"].copy() # remove duplicate parameters tensor_parallel_cfg.pop("mode") tensor_parallel_cfg.pop("size") # add this config to initialize later pg_init.append(dict(type=INITIALIZER_MAPPING[tensor_parallel_mode.lower()], **tensor_parallel_cfg)) # run initialization of different process groups for initializer_cfg in pg_init: cfg = initializer_cfg.copy() initializer_type = cfg.pop("type") initializer = DIST_GROUP_INITIALIZER.get_module(initializer_type)( rank, world_size, self.config, self.data_parallel_size, self.pipeline_parallel_size, self.tensor_parallel_size, **cfg, ) parallel_setting = initializer.init_dist_group() if isinstance(parallel_setting, list): for args in parallel_setting: self._register_dist(*args) else: self._register_dist(*parallel_setting) def is_initialized(self, parallel_mode: ParallelMode): """Returns a boolean value indicating whether `parallel_mode` is initialized in the current system. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Returns: bool: a boolean value indicating whether `parallel_mode` is initialized in the current system. """ return parallel_mode in self._groups def destroy(self): """Destroys the current distributed parallel environment.""" for mode, group in self._groups.items(): if mode is not ParallelMode.GLOBAL: dist.destroy_process_group(group) # destroy global process group dist.destroy_process_group() self._groups.clear() def set_device(self, device_ordinal: int = None): """Sets distributed processes to be bound to devices. Args: device_ordinal (int, optional): the device id to be bound to """ global_rank = self.get_global_rank() if device_ordinal is None: devices_per_node = torch.cuda.device_count() device_ordinal = global_rank % devices_per_node torch.cuda.set_device(device_ordinal) if self._verbose: self.logger.info(f"process rank {global_rank} is bound to device {device_ordinal}") def set_seed(self, seed: int): """Sets seeds for all random libraries. Args: seed (int): seed for random states """ random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) global_rank = self.get_global_rank() if torch.cuda.is_available(): # create random seed for different parallel modes # data parallel seed are kept the same parallel_seed = seed add_seed(ParallelMode.DATA, parallel_seed) # model parallel seeds are different across ranks pipeline_offset = self._local_ranks.get(ParallelMode.PIPELINE, 0) # add seed for data parallel and tensor parallel only if self.is_initialized(ParallelMode.TENSOR): tp_rank = self.get_local_rank(ParallelMode.TENSOR) # 100 is only to increase the diff in seeds between pipeline stages tp_rank_with_offset = tp_rank + pipeline_offset * 1024 tp_seed = seed + tp_rank_with_offset add_seed(ParallelMode.TENSOR, tp_seed) set_mode(ParallelMode.DATA) seeds = get_seeds() seed_str = ", ".join([f"{k}: {v}" for k, v in seeds.items()]) if self._verbose: self.logger.info( f"initialized seed on rank {global_rank}, " f"numpy: {seed}, python random: {seed}, {seed_str}," f"the default parallel seed is {ParallelMode.DATA}." ) else: if self._verbose: self.logger.info( f"initialized seed on rank {global_rank}, " f"numpy: {seed}, python random: {seed}, pytorch: {seed}", ranks=[0], ) self.logger.info( "WARNING: CUDA is not available, thus CUDA RNG cannot be used to track CUDA random number states", ranks=[0], ) def set_virtual_pipeline_parallel_size(self, size): self.virtual_pipeline_parallel_size = size def set_virtual_pipeline_parallel_rank(self, rank): self.virtual_pipeline_parallel_rank = rank global_context = ParallelContext()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/parallel_mode.py
colossalai/legacy/context/parallel_mode.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- from enum import Enum # parallel modes class ParallelMode(Enum): """This is an enumeration class containing all possible parallel modes.""" GLOBAL = "global" # common parallel DATA = "data" # model parallel - containing tensor and pipeline parallel groups # this is added to facilitate amp and grad clipping in hybrid parallel MODEL = "model" # pipeline parallel PIPELINE = "pipe" # containing all ranks in tensor parallel TENSOR = "tensor" # sequence parallel SEQUENCE = "sequence" SEQUENCE_DP = "sequence_dp" # 1D Parallel PARALLEL_1D = "1d" # 2D parallel PARALLEL_2D_ROW = "2d_row" PARALLEL_2D_COL = "2d_col" # 3D parallel PARALLEL_3D_INPUT = "3d_input" PARALLEL_3D_WEIGHT = "3d_weight" PARALLEL_3D_OUTPUT = "3d_output" PARALLEL_3D_INPUT_X_WEIGHT = "3d_input_x_weight" PARALLEL_3D_OUTPUT_X_WEIGHT = "3d_output_x_weight" # 2.5D parallel PARALLEL_2P5D_ROW = "2p5d_row" PARALLEL_2P5D_COL = "2p5d_col" PARALLEL_2P5D_DEP = "2p5d_dep" PARALLEL_2P5D_XZ = "2p5d_xz"
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/__init__.py
colossalai/legacy/context/__init__.py
from .parallel_context import ParallelContext from .parallel_mode import ParallelMode from .process_group_initializer import * from .random import *
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/process_group_initializer.py
colossalai/legacy/context/process_group_initializer/process_group_initializer.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- from abc import ABC, abstractmethod from colossalai.context import Config class ProcessGroupInitializer(ABC): """An object, knowing the parallelism configuration, that initializes parallel groups. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__( self, rank: int, world_size: int, config: Config, data_parallel_size: int, pipeline_parallel_size: int, tensor_parallel_size: int, ): self.rank = rank self.world_size = world_size self.data_parallel_size = data_parallel_size self.config = config self.pipeline_parallel_size = pipeline_parallel_size self.tensor_parallel_size = tensor_parallel_size super().__init__() @abstractmethod def init_dist_group(self): pass
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_data.py
colossalai/legacy/context/process_group_initializer/initializer_data.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- from torch import distributed as dist from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_Data(ProcessGroupInitializer): """A ProcessGroupInitializer for data parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_data_parallel_group = self.world_size // self.data_parallel_size def init_dist_group(self): """Initialize data parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): A Data parallelism's information tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.DATA for i in range(self.num_data_parallel_group): ranks = [i + j * self.num_data_parallel_group for j in range(self.data_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_2p5d.py
colossalai/legacy/context/process_group_initializer/initializer_2p5d.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import math import torch.distributed as dist from colossalai.context import Config from colossalai.legacy.global_variables import tensor_parallel_env as env from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer def _check_tesseract_env_var(tesseract_dim: int, tesseract_dep: int): # check global variable for TESSERACT env_tesseract_dim = env.tesseract_dim env_tesseract_dep = env.tesseract_dep if env_tesseract_dim and env_tesseract_dep: assert int(env_tesseract_dim) == tesseract_dim, ( "TESSERACT_DIM has been set in the current environment and " "does not match with the value passed to this initialized" ) assert int(env_tesseract_dep) == tesseract_dep, ( "TESSERACT_DEP has been set in the current environment and " "does not match with the value passed to this initialized" ) else: env.tesseract_dim = tesseract_dim env.tesseract_dep = tesseract_dep # i row j col k dep class Initializer_2p5D_ROW(ProcessGroupInitializer): """2.5d tensor parallel initialization among rows. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_ROW, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim assert ( self.tensor_parallel_size == self.tesseract_dim**2 * self.tesseract_dep ), "Tensor parallel size should be depth * dim ** 2 in 2.5D parallel" def init_dist_group(self): """Initialize 2.5D tensor row parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor row parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_ROW for h in range(self.num_group): for j in range(self.tesseract_dim): for k in range(self.tesseract_dep): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for i in range(self.tesseract_dim) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_2p5D_Col(ProcessGroupInitializer): """2.5d tensor parallel initialization among cols. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_Col, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim def init_dist_group(self): """Initialize 2.5D tensor col parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor col parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_COL for h in range(self.num_group): for i in range(self.tesseract_dim): for k in range(self.tesseract_dep): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for j in range(self.tesseract_dim) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_2p5D_Dep(ProcessGroupInitializer): """2.5D tensor parallel initialization among depths. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_Dep, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim def init_dist_group(self): """Initialize 2.5D tensor depth parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor depth parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_DEP for h in range(self.num_group): for i in range(self.tesseract_dim): for j in range(self.tesseract_dim): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for k in range(self.tesseract_dep) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode # i row j col k dep class Initializer_2p5D_XZ(ProcessGroupInitializer): """2.5d tensor parallel initialization among cols times dep. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_XZ, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim def init_dist_group(self): """Initialize 2.5D tensor colXdepth parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor colXdepth parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_XZ for h in range(self.num_group): for i in range(self.tesseract_dim): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for k in range(self.tesseract_dep) for j in range(self.tesseract_dim) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_2p5D(ProcessGroupInitializer): """ Serve as the single entry point to Tesseract parallel initialization. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. depth (int): The depth of 2.5d parallel. """ def __init__( self, rank: int, world_size: int, config: Config, data_parallel_size: int, pipeline_parallel_size: int, tensor_parallel_size: int, depth: int, ): args = (rank, world_size, config, data_parallel_size, pipeline_parallel_size, tensor_parallel_size) super().__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dim = int(math.sqrt(self.tensor_parallel_size / depth)) self.tesseract_dep = depth assert ( self.tensor_parallel_size == self.tesseract_dim**2 * self.tesseract_dep ), "2.5D tesseract dim should equal to (tensor parallel size / tesseract dep) ^ 0.5" _check_tesseract_env_var(self.tesseract_dim, self.tesseract_dep) self.col_initializer = Initializer_2p5D_Col(self.tesseract_dim, self.tesseract_dep, *args) self.row_initializer = Initializer_2p5D_ROW(self.tesseract_dim, self.tesseract_dep, *args) self.dep_initializer = Initializer_2p5D_Dep(self.tesseract_dim, self.tesseract_dep, *args) self.xz_initializer = Initializer_2p5D_XZ(self.tesseract_dim, self.tesseract_dep, *args) def init_dist_group(self): """Initialize 2.5D tensor row, col, depth, and colXdepth parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: Whole 2.5D tensor parallelism's information in a list of tuples. """ parallel_setting = [ self.col_initializer.init_dist_group(), self.row_initializer.init_dist_group(), self.dep_initializer.init_dist_group(), self.xz_initializer.init_dist_group(), ] return parallel_setting
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_3d.py
colossalai/legacy/context/process_group_initializer/initializer_3d.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import math import torch.distributed as dist from colossalai.legacy.global_variables import tensor_parallel_env as env from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer def _check_depth_env_var(depth): # check global variable env_depth = env.depth_3d if env_depth: assert int(env_depth) == depth, ( "DEPTH_3D has been set in the current environment and " "does not match with the value passed to this initialized" ) else: env.depth_3d = depth class Initializer_3D_Input(ProcessGroupInitializer): """3D tensor parallel initialization among input. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among input, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among input in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_INPUT env.input_group_3d = mode for h in range(self.num_group): for i in range(self.depth): for k in range(self.depth): ranks = [h * self.depth**3 + i + self.depth * (j + self.depth * k) for j in range(self.depth)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_Weight(ProcessGroupInitializer): """3D tensor parallel initialization among weight. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among weight, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among weight in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_WEIGHT env.weight_group_3d = mode for h in range(self.num_group): for k in range(self.depth): for j in range(self.depth): ranks = [h * self.depth**3 + i + self.depth * (j + self.depth * k) for i in range(self.depth)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_Output(ProcessGroupInitializer): """3D tensor parallel initialization among output. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among output, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among output in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_OUTPUT env.output_group_3d = mode for h in range(self.num_group): for i in range(self.depth): for j in range(self.depth): ranks = [h * self.depth**3 + i + self.depth * (j + self.depth * k) for k in range(self.depth)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_InputxWeight(ProcessGroupInitializer): """3D tensor parallel initialization among input. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among input, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among input in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_INPUT_X_WEIGHT env.input_x_weight_group_3d = mode for h in range(self.num_group): for k in range(self.depth): ranks = [ h * self.depth**3 + i + self.depth * (j + self.depth * k) for j in range(self.depth) for i in range(self.depth) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_OutputxWeight(ProcessGroupInitializer): """3D tensor parallel initialization among input. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among input, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among input in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_OUTPUT_X_WEIGHT env.output_x_weight_group_3d = mode for h in range(self.num_group): for j in range(self.depth): ranks = [ h * self.depth**3 + i + self.depth * (j + self.depth * k) for k in range(self.depth) for i in range(self.depth) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_3D(ProcessGroupInitializer): """Serve as the single entry point to 3D parallel initialization. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args): super().__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.depth = round(math.pow(self.tensor_parallel_size, 1 / 3)) assert ( self.tensor_parallel_size == self.depth**3 ), f"3D depth ({self.depth}) if not cube root of tensor parallel size ({self.tensor_parallel_size})" _check_depth_env_var(self.depth) self.input_initializer = Initializer_3D_Input(self.num_group, self.depth, *args) self.weight_initializer = Initializer_3D_Weight(self.num_group, self.depth, *args) self.output_initializer = Initializer_3D_Output(self.num_group, self.depth, *args) self.input_x_weight_initializer = Initializer_3D_InputxWeight(self.num_group, self.depth, *args) self.output_x_weight_initializer = Initializer_3D_OutputxWeight(self.num_group, self.depth, *args) def init_dist_group(self): """Initialize 3D tensor parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: Whole 3D tensor parallelism's information in a list of tuples. """ parallel_setting = [ self.input_initializer.init_dist_group(), self.weight_initializer.init_dist_group(), self.output_initializer.init_dist_group(), self.input_x_weight_initializer.init_dist_group(), self.output_x_weight_initializer.init_dist_group(), ] return parallel_setting
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_1d.py
colossalai/legacy/context/process_group_initializer/initializer_1d.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.legacy.global_variables import tensor_parallel_env as env from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_1D(ProcessGroupInitializer): """A ProcessGroupInitializer for 1d tensor parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_group = self.world_size // self.tensor_parallel_size def init_dist_group(self): """Initialize 1D tensor parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 1D tensor parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_1D env.parallel_input_1d = False for i in range(self.num_group): ranks = [i * self.tensor_parallel_size + j for j in range(self.tensor_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_sequence.py
colossalai/legacy/context/process_group_initializer/initializer_sequence.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .initializer_tensor import Initializer_Tensor from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_Sequence_DP(ProcessGroupInitializer): """A ProcessGroupInitializer for sequence parallelism all-reduce. In Sequence Parallelism, each GPU holds the full copy of model weights, thus, gradient all-reduce occurs across all processes in the same pipeline stage Args: rank (int): The rank of current process world_size (int): Size of whole communication world config (Config): Running configuration data_parallel_size (int): Size of data parallel pipeline_parallel_size (int): Size of pipeline parallel tensor_parallel_size (int): Size of tensor parallel """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.dp_size = self.world_size // self.pipeline_parallel_size self.num_group = self.pipeline_parallel_size def init_dist_group(self): """Initialize Sequence Parallel process groups used for gradient all-reduce. Returns: Tuple: A tuple (local_rank, group_world_size, process_group, ranks_in_group, mode). """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.SEQUENCE_DP for i in range(self.num_group): ranks = [i * self.dp_size + j for j in range(self.dp_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_Sequence(ProcessGroupInitializer): """A ProcessGroupInitializer for sequence parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # reuse tensor parallel initializer code self._sequence_initializer = Initializer_Tensor(*args, **kwargs) self._sequence_dp_initializer = Initializer_Sequence_DP(*args, **kwargs) def init_dist_group(self): """Initialize Sequence parallel process groups and assign local_ranks and groups to each gpu. Sequence parallelism requires 2 process groups. The first is for model forward where several processes exchange partial query, key and value embedding to compute self attention values. The second is for all-reduce to synchronize the model parameters. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: A Sequence parallelism's information in list of tuples. """ parallel_setting = [] ( local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode, ) = self._sequence_initializer.init_dist_group() # change mode to sequence mode = ParallelMode.SEQUENCE parallel_setting.append((local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode)) parallel_setting.append(self._sequence_dp_initializer.init_dist_group()) return parallel_setting
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_model.py
colossalai/legacy/context/process_group_initializer/initializer_model.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_Model(ProcessGroupInitializer): """A ProcessGroupInitializer for model parallelism (model parallel group contains pipeline and tensor parallel groups). Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.model_parallel_size = self.tensor_parallel_size * self.pipeline_parallel_size self.num_group = self.world_size // self.model_parallel_size def init_dist_group(self): """Initialize model parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): A Model parallelism's information tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.MODEL for i in range(self.num_group): ranks = [i * self.model_parallel_size + j for j in range(self.model_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_2d.py
colossalai/legacy/context/process_group_initializer/initializer_2d.py
import math import torch.distributed as dist from colossalai.legacy.global_variables import tensor_parallel_env as env from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer def _check_summa_env_var(summa_dim): # check environment variable for SUMMA env_summa_dim = env.summa_dim if env_summa_dim: assert int(env_summa_dim) == summa_dim, ( "SUMMA_DIM has been set in the current environment and " "does not match with the value passed to this initialized" ) else: env.summa_dim = summa_dim class Initializer_2D_Row(ProcessGroupInitializer): """2d tensor parallel initialization among rows. Args: num_group (int): The number of all tensor groups. summa_dim (int): The dimension of SUMMA. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group, summa_dim, *args, **kwargs): super(Initializer_2D_Row, self).__init__(*args, **kwargs) self.num_group = num_group self.summa_dim = summa_dim def init_dist_group(self): """Initialize 2D tensor row parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2D tensor row parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2D_ROW for i in range(self.num_group): for j in range(self.summa_dim): ranks = [i * self.tensor_parallel_size + j * self.summa_dim + k for k in range(self.summa_dim)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_2D_Col(ProcessGroupInitializer): """2d tensor parallel initialization among cols. Args: num_group (int): The number of all tensor groups. summa_dim (int): The dimension of SUMMA. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group, summa_dim, *args, **kwargs): super(Initializer_2D_Col, self).__init__(*args, **kwargs) self.num_group = num_group self.summa_dim = summa_dim def init_dist_group(self): """Initialize 2D tensor row parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2D tensor col parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2D_COL for i in range(self.num_group): for j in range(self.summa_dim): ranks = [i * self.tensor_parallel_size + j + k * self.summa_dim for k in range(self.summa_dim)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_2D(ProcessGroupInitializer): """ Serve as the single entry point to 2D parallel initialization. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_group = self.world_size // self.tensor_parallel_size self.summa_dim = int(math.sqrt(self.tensor_parallel_size)) assert self.tensor_parallel_size == self.summa_dim**2, "2D summa dim should equal to tensor parallel size ^ 0.5" _check_summa_env_var(self.summa_dim) self.col_initializer = Initializer_2D_Col(self.num_group, self.summa_dim, *args, **kwargs) self.row_initializer = Initializer_2D_Row(self.num_group, self.summa_dim, *args, **kwargs) def init_dist_group(self): """Initialize 2D tensor row and col parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: 2D tensor parallelism's information in a list of tuples. """ parallel_setting = [self.row_initializer.init_dist_group(), self.col_initializer.init_dist_group()] return parallel_setting
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/__init__.py
colossalai/legacy/context/process_group_initializer/__init__.py
from .initializer_1d import Initializer_1D from .initializer_2d import Initializer_2D from .initializer_2p5d import Initializer_2p5D from .initializer_3d import Initializer_3D from .initializer_data import Initializer_Data from .initializer_model import Initializer_Model from .initializer_pipeline import Initializer_Pipeline from .initializer_sequence import Initializer_Sequence from .initializer_tensor import Initializer_Tensor from .process_group_initializer import ProcessGroupInitializer __all__ = [ "Initializer_Tensor", "Initializer_Sequence", "Initializer_Pipeline", "Initializer_Data", "Initializer_2p5D", "Initializer_2D", "Initializer_3D", "Initializer_1D", "ProcessGroupInitializer", "Initializer_Model", ]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_pipeline.py
colossalai/legacy/context/process_group_initializer/initializer_pipeline.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- from torch import distributed as dist from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_Pipeline(ProcessGroupInitializer): """A ProcessGroupInitializer for pipeline parallelism. Args: rank (int): The rank of current process world_size (int): Size of whole communication world config (Config): Running configuration data_parallel_size (int): Size of data parallel pipeline_parallel_size (int): Size of pipeline parallel tensor_parallel_size (int): Size of tensor parallel """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.data_group_size = self.world_size // self.data_parallel_size self.pipeline_stage_size = self.data_group_size // self.pipeline_parallel_size def init_dist_group(self): """Initialize pipeline parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: A Pipeline parallelism's information in list of tuples. """ dist_settings = list() for i in range(self.data_parallel_size): for j in range(self.pipeline_stage_size): pipe_ranks = list( range(i * self.data_group_size + j, (i + 1) * self.data_group_size, self.pipeline_stage_size) ) pipe_group_size = len(pipe_ranks) pipe_group = dist.new_group(pipe_ranks) group_cpu = dist.new_group(pipe_ranks, backend="gloo") if dist.get_backend() != "gloo" else pipe_group if self.rank in pipe_ranks: local_rank = pipe_ranks.index(self.rank) group_world_size = pipe_group_size process_group = pipe_group cpu_group = group_cpu ranks_in_group = pipe_ranks dist_settings.append( tuple( ( local_rank, group_world_size, process_group, cpu_group, ranks_in_group, ParallelMode.PIPELINE, ) ) ) return dist_settings
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/process_group_initializer/initializer_tensor.py
colossalai/legacy/context/process_group_initializer/initializer_tensor.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.legacy.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_Tensor(ProcessGroupInitializer): """A ProcessGroupInitializer for tensor parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_tensor_parallel_group = self.world_size // self.tensor_parallel_size def init_dist_group(self): """Initialize tensor parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): A Tensor parallelism's information tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.TENSOR for i in range(self.num_tensor_parallel_group): ranks = [i * self.tensor_parallel_size + j for j in range(self.tensor_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend="gloo") if dist.get_backend() != "gloo" else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/random/seed_manager.py
colossalai/legacy/context/random/seed_manager.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch from torch import Tensor from colossalai.legacy.context.parallel_mode import ParallelMode class SeedManager: """This class is a manager of all random seeds involved in the system. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ def __init__(self): self._current_mode = None self._seeds = dict() self._seed_states = dict() @property def current_mode(self): return self._current_mode @property def seeds(self): return self._seeds @property def seed_states(self): return self._seed_states def set_state(self, parallel_mode: ParallelMode, state: Tensor): """Sets the state of the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. state (:class:`torch.Tensor`): the state to be set. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not found in the seed manager. """ assert parallel_mode in self._seed_states, f"Parallel mode {parallel_mode} is not found in the seed manager" self._seed_states[parallel_mode] = state def set_mode(self, parallel_mode: ParallelMode): """Sets the current mode of the seed manager. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. """ if self.current_mode: # save the current state for current mode self._seed_states[self._current_mode] = torch.cuda.get_rng_state() # set the new state for new mode self._current_mode = parallel_mode torch.cuda.set_rng_state(self._seed_states[parallel_mode]) def add_seed(self, parallel_mode: ParallelMode, seed: int, overwrite: bool = False): """Adds a seed to the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. seed (int): The seed to be added. overwrite (bool, optional): Whether allows to overwrite the seed that has been set already Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode` or the seed for `parallel_mode` has been added. """ assert isinstance(parallel_mode, ParallelMode), "A valid ParallelMode must be provided" if overwrite is False: assert parallel_mode not in self._seed_states, f"The seed for {parallel_mode} has been added" elif parallel_mode in self._seed_states: print(f"Warning: {parallel_mode} seed has been overwritten.", flush=True) current_state = torch.cuda.get_rng_state() torch.cuda.manual_seed(seed) self._seed_states[parallel_mode] = torch.cuda.get_rng_state() self._seeds[parallel_mode] = seed torch.cuda.set_rng_state(current_state) def reset(self): self._current_mode = None self._seeds = dict() self._seed_states = dict()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/random/_helper.py
colossalai/legacy/context/random/_helper.py
#!/usr/bin/env python # -*- encoding: utf-8 -*- import functools from contextlib import contextmanager import torch.cuda from torch import Tensor from ..parallel_mode import ParallelMode from .seed_manager import SeedManager _SEED_MANAGER = SeedManager() def get_seeds(): """Returns the seeds of the seed manager. Returns: dict: The seeds of the seed manager. """ return _SEED_MANAGER.seeds def get_states(copy=False): """Returns the seed states of the seed manager. Returns: dict: The seed states of the seed manager. """ states = _SEED_MANAGER.seed_states if copy: new_states = dict() for parallel_mode, state in states.items(): new_states[parallel_mode] = state.clone() return new_states else: return _SEED_MANAGER.seed_states def get_current_mode(): """Returns the current mode of the seed manager. Returns: :class:`torch.ByteTensor`: The current mode of the seed manager. """ return _SEED_MANAGER.current_mode def add_seed(parallel_mode: ParallelMode, seed: int, overwrite: bool = False): """Adds a seed to the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. seed (int): The seed to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.legacy.context.ParallelMode` or the seed for `parallel_mode` has been added. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ _SEED_MANAGER.add_seed(parallel_mode, seed, overwrite) def set_mode(parallel_mode: ParallelMode): """Sets the current mode of the seed manager. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ _SEED_MANAGER.set_mode(parallel_mode) def set_seed_states(parallel_mode: ParallelMode, state: Tensor): """Sets the state of the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.legacy.context.ParallelMode`): The chosen parallel mode. state (:class:`torch.Tensor`): the state to be set. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not found in the seed manager. """ _SEED_MANAGER.set_state(parallel_mode, state) def sync_states(): current_mode = get_current_mode() current_states = torch.cuda.get_rng_state() set_seed_states(current_mode, current_states) @contextmanager def seed(parallel_mode: ParallelMode): """A context for seed switch Examples: >>> with seed(ParallelMode.DATA): >>> output = F.dropout(input) Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ try: # set to new mode current_mode = _SEED_MANAGER.current_mode yield _SEED_MANAGER.set_mode(parallel_mode) finally: # recover _SEED_MANAGER.set_mode(current_mode) def with_seed(func, parallel_mode: ParallelMode): """ A function wrapper which executes the function with a specified seed. Examples: >>> # use with decorator >>> @with_seed(ParallelMode.DATA) >>> def forward(input): >>> return F.dropout(input) >>> out = forward(input) >>> # OR use it inline >>> def forward(input): >>> return F.dropout(input) >>> wrapper_forward = with_seed(forward, ParallelMode.DATA) >>> out = wrapped_forward(input) Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ @functools.wraps(func) def wrapper(*args, **kwargs): # switch mode current_mode = _SEED_MANAGER.current_mode _SEED_MANAGER.set_mode(parallel_mode) # exec func out = func(*args, **kwargs) # recover state _SEED_MANAGER.set_mode(current_mode) return out return wrapper def moe_set_seed(seed): if torch.cuda.is_available(): from colossalai.legacy.core import global_context as gpc global_rank = gpc.get_global_rank() diff_seed = seed + global_rank add_seed(ParallelMode.TENSOR, diff_seed, True) print(f"moe seed condition: {global_rank} with tensor seed {diff_seed}", flush=True) def reset_seeds(): _SEED_MANAGER.reset()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/context/random/__init__.py
colossalai/legacy/context/random/__init__.py
from ._helper import ( add_seed, get_current_mode, get_seeds, get_states, moe_set_seed, reset_seeds, seed, set_mode, set_seed_states, sync_states, with_seed, ) __all__ = [ "seed", "set_mode", "with_seed", "add_seed", "get_seeds", "get_states", "get_current_mode", "set_seed_states", "sync_states", "moe_set_seed", "reset_seeds", ]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/load_balance.py
colossalai/legacy/moe/load_balance.py
from copy import deepcopy from typing import List, Optional, Tuple import torch import torch.distributed as dist from torch import Tensor, nn from torch.distributed import ProcessGroup from colossalai.cluster import ProcessGroupMesh from colossalai.legacy.moe.manager import MOE_MANAGER from colossalai.shardformer.layer.moe import MLPExperts from colossalai.zero.low_level import LowLevelZeroOptimizer class LoadBalancer: def __init__( self, experts: MLPExperts, gate: nn.Parameter, local_expert_num: int, expert_num: int, ep_group: ProcessGroup, dp_group: ProcessGroup, tolerance: Optional[float] = 0.1, beam_width: Optional[int] = 8, group_swap_factor: Optional[float] = 0.4, ) -> None: self.experts: MLPExperts = experts self.gate: nn.Parameter = gate self.moe_ep_group: ProcessGroup = ep_group self.moe_ep_ranks = MOE_MANAGER.parallel_info_dict[dist.get_world_size(self.moe_ep_group)].ep_group_ranks self.moe_dp_group: ProcessGroup = dp_group self.tolerance = tolerance self.beam_width = beam_width self.group_swap_factor = group_swap_factor self.local_expert_num = local_expert_num self.expert_num = expert_num self.local_load = None # TODO: use a global process group mesh pp_size = 1 if MOE_MANAGER.pp_size is None else MOE_MANAGER.pp_size global_dp_group = ProcessGroupMesh(pp_size, dist.get_world_size() // pp_size) self.global_dp_group = global_dp_group.get_group_along_axis(1) self.global_dp_rank = dist.get_rank(self.global_dp_group) self.global_dp_size = dist.get_world_size(self.global_dp_group) def _clear_load(self) -> None: self.local_load = None def _sync_load(self) -> Tensor: new_load = self.local_load.clone().detach() # all reduce load between ep group dist.all_reduce(new_load, group=self.moe_ep_group) # all reduce load between dp group dist.all_reduce(new_load, group=self.moe_dp_group) return new_load @staticmethod def _get_diff_from_avg(data: List, group: int, avg: float) -> float: return abs(sum(data[group]) / len(data[group]) - avg) @staticmethod def _swap_data(data: List, group_i: int, index_i: int, group_j: int, index_j: int) -> None: data[group_i][index_i], data[group_j][index_j] = ( data[group_j][index_j], data[group_i][index_i], ) @staticmethod def _normalize_data(data: List) -> List: max_value = max(max(sublist) for sublist in data) data = [[i / max_value for i in sublist] for sublist in data] return data @staticmethod def _get_swap_loss( group_swap_factor: float, swap_list: List, group_i: int, index_i: int, group_j: int, index_j: int, ) -> float: """ Get swap loss. The swap loss is used to avoid the situation that the same index is swapped twice and the same group is swapped for multiple times. """ swap_loss = 0 for swap in swap_list: for group_id, index_id in zip([group_i, group_j], [index_i, index_j]): # the group has been swapped if group_id in [swap[0], swap[2]]: # the index has been swapped # we want to avoid the situation that the same index is swapped twice if index_id in [swap[1], swap[3]]: swap_loss += 1e5 # the index has not been swapped # this is acceptable but as less as possible else: swap_loss += group_swap_factor return swap_loss @staticmethod def _check_convergence(data: List, avg: float, tolerance: float): """ Check whether the data is converged after swap. """ for sublist in data: if abs(sum(sublist) / len(sublist) - avg) > tolerance * avg: return False return True def _beam_search( self, inputs: Tuple[List, float, List], beam_width: int, avg: float, group_swap_factor: float, ) -> List: """ Beam search for the best swap combination. Specifically, we swap two elements from two groups and calculate the score. The score is the difference between the origin group sum and the new group sum. The larger the score, the better the swap combination. Args: inputs (Tuple): (data, origin_score, swap_list) beam_width (int): beam width for beam search avg (float): average value of the data group_swap_factor (float): group loss for group swap loss Returns: List: results list """ data, origin_score, swap_list = inputs results = [] group_num = len(data) group_size = len(data[0]) origin_diff_list = [self._get_diff_from_avg(data, i, avg) for i in range(group_num)] for group_num_i in range(group_num): for group_size_i in range(group_size): for group_num_j in range(group_num_i + 1, group_num): for group_size_j in range(group_size): new_data = deepcopy(data) # calculate origin group sum origin_diff = origin_diff_list[group_num_i] + origin_diff_list[group_num_j] # swap data self._swap_data( new_data, group_num_i, group_size_i, group_num_j, group_size_j, ) # calculate new group sum new_diff = self._get_diff_from_avg(new_data, group_num_i, avg) + self._get_diff_from_avg( new_data, group_num_j, avg ) # caculate score new_score = origin_diff - new_diff if new_score > 0: new_score = origin_score + new_score # get swap loss swap_loss = self._get_swap_loss( group_swap_factor, swap_list, group_num_i, group_size_i, group_num_j, group_size_j, ) new_score = new_score - swap_loss # update swap list new_swap_list = swap_list + [(group_num_i, group_size_i, group_num_j, group_size_j)] results.append((new_data, new_score, new_swap_list)) # sort results results.sort(key=lambda x: x[1], reverse=True) # select top k results results = results[:beam_width] return results def _load_to_list(self, load: Tensor) -> List: load_len = len(load) assert load_len % self.local_expert_num == 0 load_list = [] tmp_list = [] for i in range(len(load)): tmp_list.append(float(load[i])) if (i + 1) % self.local_expert_num == 0: load_list.append(tmp_list) tmp_list = [] return load_list def _search_balance( self, data: List, tolerance: Optional[float] = 0.1, beam_width: Optional[int] = 8, group_swap_factor: Optional[float] = 0.4, return_swapped_data: Optional[bool] = False, ) -> Tuple[List, List]: """ Search for the best swap combination to balance the data within the specified tolerance. And return the balanced data and the swap list. The swap list is used to record the swap. The swap list is a list of tuples. Each tuple is a swap operation. Args: data (List): expert load list. E.g. [[9.2, 8.3], [2.3, 10.0], [6.1, 7.2], [5.3, 3.2]] This means there are 4 devices and each devices has 2 experts. The value is the load of the expert. tolerance (float): tolerance for balance. beam_width (int): beam width for beam search. group_swap_factor (float): group swap factor for group swap loss. The bigger it is, the less times a group will be swapped. return_swapped_data (bool): whether to return the swapped data. Returns: Tuple: (balanced data, swap list). The swap list is a list of tuples. Each tuple is a swap operation. E.g. [(0, 0, 1, 0), (...), (...)]. The first tuple means the first expert of the first device is swapped with the first expert of the second device. """ norm_data = self._normalize_data(data) avg = sum(sum(sublist) / len(sublist) for sublist in norm_data) / len(norm_data) results = [(norm_data, 0, [])] stop_flag = False while stop_flag == False: new_results = [] best_score = results[0][1] for i in range(len(results)): new_results.extend(self._beam_search(results[i], beam_width, avg, group_swap_factor)) if len(new_results) == 0: stop_flag = True break new_results.sort(key=lambda x: x[1], reverse=True) new_best_score = new_results[0][1] if new_best_score == best_score: stop_flag = True break new_results = new_results[:beam_width] results = new_results for i in results: if self._check_convergence(results[0][0], avg, tolerance): stop_flag = True break swap_list = results[0][2] if return_swapped_data: out = deepcopy(data) for swap in swap_list: self._swap_data(out, *swap) return out, swap_list else: return swap_list @staticmethod def _swap_expert_single_tensor( weight: nn.Parameter, expert_idx: int, comm_group: ProcessGroup, send_first: bool, comm_rank: int, ): # exchange weight local_weight = weight.data[expert_idx] new_weight = torch.empty_like(local_weight) if send_first: dist.send(local_weight, dst=comm_rank, group=comm_group) dist.recv(new_weight, src=comm_rank, group=comm_group) else: dist.recv(new_weight, src=comm_rank, group=comm_group) dist.send(local_weight, dst=comm_rank, group=comm_group) weight.data[expert_idx] = new_weight def _swap_expert_param_and_optim( self, weight: nn.Parameter, expert_idx: int, comm_group: ProcessGroup, send_first: bool, comm_rank: int, optim: LowLevelZeroOptimizer, ): # need to update master and working param if master param exists # else just update working param if weight in optim.optim.state: master_weight_ptr = None working_weight_ptr = weight exp_avg_ptr = optim.optim.state[working_weight_ptr]["exp_avg"] exp_avg_sq_ptr = optim.optim.state[working_weight_ptr]["exp_avg_sq"] else: master_weight_ptr = optim.working_to_master_param[id(weight)] working_weight_ptr = weight exp_avg_ptr = optim.optim.state[master_weight_ptr]["exp_avg"] exp_avg_sq_ptr = optim.optim.state[master_weight_ptr]["exp_avg_sq"] # exchange weight self._swap_expert_single_tensor( working_weight_ptr, expert_idx, comm_group, send_first, comm_rank, ) if master_weight_ptr is not None: # TODO: exchange master weight, skip for now # master weight is shared by dp group tmp = working_weight_ptr.view(-1).split( working_weight_ptr.numel() // dist.get_world_size(self.moe_dp_group) )[dist.get_rank(self.moe_dp_group)] master_weight_ptr.data.copy_(tmp.clone().detach().to(master_weight_ptr.device).to(master_weight_ptr.dtype)) # exchange optim self._swap_expert_single_tensor(exp_avg_ptr, expert_idx, comm_group, send_first, comm_rank) self._swap_expert_single_tensor(exp_avg_sq_ptr, expert_idx, comm_group, send_first, comm_rank) def _gather_global_dp_group(self, data: Tensor) -> Tensor: data_list = [torch.zeros_like(data) for _ in range(self.global_dp_size)] dist.all_gather(data_list, data, group=self.global_dp_group) data_list = torch.cat(data_list, dim=0) return data_list def _swap_moe_param(self, swap_list: List, optim: LowLevelZeroOptimizer) -> None: """ Swap moe param and optim. We use different strategies to swap expert and gate. For expert, we exchange the param and optim of the expert by p2p. For gate, we all gather the gate choose the part we want. Args: swap_list (List) optim (LowLevelZeroOptimizer) """ # get all experts weights local_rank = dist.get_rank(self.moe_ep_group) if self.experts.gated: weight_list = [self.experts.wi_up, self.experts.wi_gate] else: weight_list = [self.experts.wi] weight_list.append(self.experts.wo) # gate optim should be obtained first gate_shape = self.gate.shape # get master weight and optim master_gate_weight = optim.working_to_master_param[id(self.gate)] gate_exp_avg = optim.optim.state[master_gate_weight]["exp_avg"] gate_exp_avg_sq = optim.optim.state[master_gate_weight]["exp_avg_sq"] # gather global_master_gate_weight = self._gather_global_dp_group(master_gate_weight).view(gate_shape) global_gate_exp_avg = self._gather_global_dp_group(gate_exp_avg).view(gate_shape) global_gate_exp_avg_sq = self._gather_global_dp_group(gate_exp_avg_sq).view(gate_shape) assert ( self.gate.shape == global_master_gate_weight.shape == global_gate_exp_avg.shape == global_gate_exp_avg_sq.shape ) for swap in swap_list: source_group, source_idx, target_group, target_idx = swap source_rank = self.moe_ep_ranks[source_group] target_rank = self.moe_ep_ranks[target_group] # exchange expert if local_rank in [source_group, target_group]: for weight in weight_list: if local_rank == source_group: self._swap_expert_param_and_optim( weight, source_idx, self.moe_ep_group, True, target_rank, optim, ) elif local_rank == target_group: self._swap_expert_param_and_optim( weight, target_idx, self.moe_ep_group, False, source_rank, optim, ) # exchange gate source_expert_pos = source_group * self.local_expert_num + source_idx target_expert_pos = target_group * self.local_expert_num + target_idx for gate in [ self.gate, global_master_gate_weight, global_gate_exp_avg, global_gate_exp_avg_sq, ]: origin_source = gate.data[source_expert_pos].clone().detach() origin_target = gate.data[target_expert_pos].clone().detach() gate.data[source_expert_pos], gate.data[target_expert_pos] = ( origin_target, origin_source, ) # update gate global_master_gate_weight = global_master_gate_weight.view(-1).split( global_master_gate_weight.numel() // self.global_dp_size )[self.global_dp_rank] master_gate_weight.data.copy_(global_master_gate_weight) global_gate_exp_avg = global_gate_exp_avg.view(-1).split(global_gate_exp_avg.numel() // self.global_dp_size)[ self.global_dp_rank ] gate_exp_avg.data.copy_(global_gate_exp_avg) global_gate_exp_avg_sq = global_gate_exp_avg_sq.view(-1).split( global_gate_exp_avg_sq.numel() // self.global_dp_size )[self.global_dp_rank] gate_exp_avg_sq.data.copy_(global_gate_exp_avg_sq) @torch.no_grad() def update_load(self, load: Tensor) -> None: if len(load) != self.expert_num: padding_size = self.expert_num - len(load) padding = torch.zeros(padding_size, dtype=load.dtype, device=load.device) load = torch.cat((load, padding), dim=0) if self.local_load is None: self.local_load = load else: self.local_load += load @torch.no_grad() def balance_load(self, optim: LowLevelZeroOptimizer) -> None: # prepare load load = self._sync_load() load = self._load_to_list(load) # search balance swap_list = self._search_balance(load) if dist.get_rank() == 0: if len(swap_list) > 0: print(f"[Load Balance] Applying expert swap...") else: print(f"[Load Balance] Invalid swap, skip...") # swap expert and gate self._swap_moe_param(swap_list, optim) # clear load self._clear_load()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/utils.py
colossalai/legacy/moe/utils.py
import contextlib import os from typing import Any, Callable, Dict, List, Optional, Tuple import torch import torch.distributed as dist import torch.nn as nn import torch.nn.functional as F from torch.distributed.distributed_c10d import get_process_group_ranks from colossalai.accelerator import get_accelerator from colossalai.legacy.moe.manager import MOE_MANAGER from colossalai.tensor.moe_tensor.api import is_moe_tensor class ForceFP32Parameter(torch.nn.Parameter): def half(self, memory_format=None): return self.data.clone() class NormalNoiseGenerator: """Generates a random noisy mask for logits tensor. All noise is generated from a normal distribution :math:`(0, 1 / E^2)`, where `E = the number of experts`. Args: num_experts (int): The number of experts. """ def __init__(self, num_experts: int): self.normal = torch.distributions.normal.Normal( loc=torch.tensor(0.0, device=get_accelerator().get_current_device()), scale=torch.tensor(1.0 / num_experts**2, device=get_accelerator().get_current_device()), ).rsample def __call__(self, inputs: torch.Tensor): noisy = self.normal(inputs.shape) return inputs + noisy class UniformNoiseGenerator: """Generates a random noisy mask for logits tensor. copied from mesh tensorflow: Multiply values by a random number between :math:`1-epsilon` and :math:`1+epsilon`. Makes models more resilient to rounding errors introduced by bfloat16. This seems particularly important for logits. Args: eps (float, optional): Epsilon in generator, defaults 1e-2. """ def __init__(self, eps: float = 1e-2): self.uniform = torch.distributions.uniform.Uniform( low=torch.tensor(1.0 - eps, device=get_accelerator().get_current_device()), high=torch.tensor(1.0 + eps, device=get_accelerator().get_current_device()), ).rsample def __call__(self, inputs: torch.Tensor): noisy = self.uniform(inputs.shape) return inputs * noisy def autocast_softmax(logit: torch.Tensor, dim: int): return F.softmax(logit, dim=dim, detype=torch.float32) def get_noise_generator(noise_type: str, num_experts: int) -> Callable: if noise_type is None: return None elif noise_type == "Jitter": noisy_func = UniformNoiseGenerator() elif noise_type == "Gaussian": noisy_func = NormalNoiseGenerator(num_experts) else: raise NotImplementedError("Unsupported input noisy policy") return noisy_func def get_activation(act: str) -> Callable: if act is None or act == "relu": return torch.nn.ReLU() elif act == "gelu": return torch.nn.GELU() elif act == "swiglu": return SwiGLU elif act == "silu": return torch.nn.SiLU() else: raise NotImplementedError("Unsupported activation function") def SwiGLU(x): """Gated linear unit activation function. Args: x : input array axis: the axis along which the split should be computed (default: -1) """ size = x.shape[-1] assert size % 2 == 0, "axis size must be divisible by 2" x1, x2 = torch.split(x, size // 2, -1) return x1 * (x2 * torch.sigmoid(x2)) @contextlib.contextmanager def skip_init(): """ skip param random init """ def _skip_init(*args, **kwargs): pass init_func = { "constant_": torch.nn.init.constant_, "uniform_": torch.nn.init.uniform_, "normal_": torch.nn.init.normal_, "kaiming_uniform_": torch.nn.init.kaiming_uniform_, "kaiming_normal_": torch.nn.init.kaiming_normal_, "xavier_normal_": torch.nn.init.xavier_normal_, "xavier_uniform_": torch.nn.init.xavier_uniform_, "trunc_normal_": torch.nn.init.trunc_normal_, } for method_name, original_init in init_func.items(): setattr(torch.nn.init, method_name, _skip_init) yield for method_name, original_init in init_func.items(): setattr(torch.nn.init, method_name, original_init) return def get_moe_epsize_param_dict(model: nn.Module) -> Dict[int, List[nn.Parameter]]: """Returns a parameter dictionary, the key of which is the expert parallel size of every parameter. Since the parameters in data parallelism is replicated in each GPU, we set their ep_size to 1. Args: model (:class:`torch.nn.Module`): A pyTorch `nn.Module` from which we get dict. """ epsize_param_dict = dict() for param in model.parameters(): if not is_moe_tensor(param): ep_size = 1 # set ep_size to 1 for dp parameters else: ep_size = dist.get_world_size(param.ep_group) if ep_size not in epsize_param_dict: epsize_param_dict[ep_size] = [] epsize_param_dict[ep_size].append(param) return epsize_param_dict def sync_moe_model_param(model: nn.Module): """Make sure model parameters are consistent in MoE parallel context. Args: model (:class:`torch.nn.Module`): A pyTorch model on whose parameters you check the consistency. """ param_dict = get_moe_epsize_param_dict(model) # synchronize the parameters whose dp_group is the whole world if 1 in param_dict: for param in param_dict[1]: dist.broadcast(param, src=0) for ep_size in param_dict: # When ep_size = world_size, communication is not needed if ep_size != 1 and ep_size != MOE_MANAGER.world_size: for param in param_dict[ep_size]: src_rank = get_process_group_ranks(param.dp_group)[0] dist.broadcast(param, src=src_rank, group=param.dp_group) def set_moe_args(config: Any, args: dict): for k, v in args.items(): setattr(config, k, v) def create_ep_hierarchical_group( ep_group_ranks: List[int], nproc_per_node: Optional[int] = None, ) -> Tuple[int, dist.ProcessGroup, Optional[dist.ProcessGroup]]: """ e.g., If ep_group = [1, 2, 5, 6], and nproc_per_node = 4 Then, ep_intra_group = [1, 2] & [5, 6], ep_inter_group = [1, 5] & None """ assert dist.is_initialized(), "Please initialize torch.distributed first." rank = dist.get_rank() if nproc_per_node is None: nproc_per_node = os.environ.get("LOCAL_WORLD_SIZE") assert nproc_per_node is not None, "Please use torchrun to launch the job, or specify nproc_per_node manually." nproc_per_node = int(nproc_per_node) else: assert dist.get_world_size() % nproc_per_node == 0, "nproc_per_node should be a divisor of world_size." num_node = dist.get_world_size() // nproc_per_node intra_src_rank = None ep_intra_node_group = None for i in range(num_node): ep_intra_ranks = [i * nproc_per_node + j for j in range(nproc_per_node) if j in ep_group_ranks] group = dist.new_group(ep_intra_ranks) if rank in ep_intra_ranks: assert ep_intra_node_group is None ep_intra_node_group = group intra_src_rank = ep_intra_ranks[0] ep_inter_node_group = None ep_inter_ranks = [ep_group_ranks[0] + i * nproc_per_node for i in range(num_node)] if len(ep_inter_ranks) > 1: group = dist.new_group(ep_inter_ranks) if rank in ep_inter_ranks: ep_inter_node_group = group return intra_src_rank, ep_intra_node_group, ep_inter_node_group
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/manager.py
colossalai/legacy/moe/manager.py
from typing import Tuple import torch import torch.distributed as dist from colossalai.context.singleton_meta import SingletonMeta from colossalai.tensor.moe_tensor.api import get_moe_info from colossalai.tensor.moe_tensor.moe_info import MoeParallelInfo class MoEManager(metaclass=SingletonMeta): """MoE manager. This class manages different parallel groups in MoE context and MoE loss in training. """ def __init__(self): self.parallel = None self.mode = None self.use_ep_inside = None self.world_size = None self._parallel_info_dict = dict() # router self.router_aux_loss = [] self.router_z_loss = [] # fixed mode self.pp_size = None self.dp_size = None self.ep_size = None # dynamic mode # Users may want to set maximum expert parallel size smaller than the world size # since very low bandwidth across nodes may constrain the performance of MoE # When we have a maximum expert parallel size, we have a minimum data parallel size naturally self.max_ep_size = None self.has_setup = False @property def parallel_info_dict(self): return self._parallel_info_dict @property def is_initialized(self): return self.has_setup def setup( self, parallel: str = None, mode: str = "dynamic", max_ep_size: int = 8, fixed_dp_size: int = 0, fixed_ep_size: int = 0, fixed_pp_size: int = 0, use_ep_inside: bool = True, ) -> None: """ Setup MoE distributed context. Args: seed (int): Random seed. Defaults to 42. use_kernel_optim (bool, optional): Use cuda kernel. Defaults to True. parallel (bool, optional): Parallel mode, should be EP, TP or None. Defaults to None. mode (str, optional): Should be "fixed" or "dynamic". Defaults to "dynamic". In fixed mode, the ep size and dp size is fixed. In dynamic mode, the ep size and dp size will be changed according to num experts. max_ep_size (int, optional): Max ep size in dynamic mode. Defaults to 8. fixed_dp_size (int, optional): Fixed dp size in fixed mode. Defaults to 0. fixed_ep_size (int, optional): Fixed ep size in fixed mode. Defaults to 0. fixed_pp_size (int, optional): Fixed pp size in fixed mode. Defaults to 0. use_ep_inside (bool, optional): Use ep inside dp if True, dp inside ep if False. Defaults to True. """ assert not self.is_initialized, "MoE distributed context shouldn't be set up again" assert torch.cuda.is_available(), "MoE requires to enable CUDA first" self.parallel = parallel self.use_ep_inside = use_ep_inside self.world_size = dist.get_world_size() # init by mode self.mode = mode assert self.mode in ["fixed", "dynamic"], "mode should be fixed or dynamic" if self.mode == "dynamic": self.max_ep_size = min(max_ep_size, self.world_size) else: assert ( fixed_dp_size > 0 and fixed_ep_size > 0 and fixed_pp_size > 0 ), "dp_size, ep_size and pp_size should be greater than 0" assert ( isinstance(fixed_dp_size, int) and isinstance(fixed_ep_size, int) and isinstance(fixed_pp_size, int) ), "dp_size, ep_size and pp_size should be int" self.ep_size = fixed_ep_size self.dp_size = fixed_dp_size self.pp_size = fixed_pp_size self.has_setup = True def get_info(self, num_experts: int, use_tp: bool = False) -> Tuple[int, MoeParallelInfo]: """Calculate the Data Parallel Group and Expert Parallel Group. Parameters ---------- num_experts : int The number experts Returns ------- int, MoeParallelInfo number of local experts, the MoeParallelInfo of the current ep_size """ if self.mode == "dynamic": gt_flag = num_experts % self.max_ep_size == 0 # check whether num_experts is greater lt_flag = self.max_ep_size % num_experts == 0 # check whether num_experts is less assert gt_flag or lt_flag, ( "Automatic experts placement dose not not support expert number" " is not a multiple of ep size or vice versa." ) dp_size = 1 if gt_flag else self.world_size // num_experts ep_size = min(self.world_size // dp_size, self.max_ep_size) dp_size = self.world_size // ep_size pp_size = 1 else: dp_size = self.dp_size ep_size = self.ep_size pp_size = self.pp_size # Calculate the number of experts for each GPU if use_tp: num_local_experts = num_experts else: if self.mode == "dynamic": num_local_experts = 1 if lt_flag else num_experts // self.max_ep_size else: num_local_experts = num_experts // ep_size if not (ep_size in self.parallel_info_dict): self.parallel_info_dict[ep_size] = get_moe_info(ep_size, dp_size, pp_size, ep_inside=self.use_ep_inside) if dist.get_rank() == 0: if self.use_ep_inside: print(f"MoE Parallel: pp {pp_size}, dp {dp_size}, ep {ep_size}") else: print(f"MoE Parallel: pp {pp_size}, ep {ep_size}, dp {dp_size}") return num_local_experts, self.parallel_info_dict[ep_size] def reset_loss(self): self.router_aux_loss, self.router_z_loss = [], [] def add_loss(self, aux_loss: float = 0.0, z_loss: float = 0.0): self.router_aux_loss.append(aux_loss) self.router_z_loss.append(z_loss) def get_loss(self): cur_loss = self.router_aux_loss, self.router_z_loss return cur_loss def get_parallel(self): return self.parallel MOE_MANAGER = MoEManager()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/train.py
colossalai/legacy/moe/openmoe/train.py
import argparse import os from functools import partial from typing import Dict import torch import torch.distributed as dist from datasets import load_dataset from huggingface_hub import snapshot_download from model.modeling_openmoe import OpenMoeForCausalLM, set_openmoe_args from model.openmoe_policy import OpenMoeForCausalLMPolicy from torch.utils.data import Dataset from tqdm import tqdm from transformers import T5Tokenizer from transformers.models.llama import LlamaConfig import colossalai from colossalai.accelerator import get_accelerator from colossalai.booster import Booster from colossalai.booster.plugin.moe_hybrid_parallel_plugin import MoeHybridParallelPlugin from colossalai.cluster import DistCoordinator from colossalai.legacy.moe.utils import skip_init from colossalai.nn.optimizer import HybridAdam from colossalai.shardformer.layer.moe import apply_load_balance def move_to_cuda(batch, device): return {k: v.to(device) for k, v in batch.items()} def load_ckpt(repo_name: str, model: OpenMoeForCausalLM, booster: Booster): ckpt_path = snapshot_download(repo_name) # single ckpt if os.path.exists(os.path.join(ckpt_path, "pytorch_model.bin")): ckpt_path = os.path.join(ckpt_path, "pytorch_model.bin") # shard ckpt elif os.path.exists(os.path.join(ckpt_path, "pytorch_model.bin.index.json")): ckpt_path = os.path.join(ckpt_path, "pytorch_model.bin.index.json") else: raise ValueError(f"Invalid checkpoint path: {ckpt_path}") booster.load_model(model, ckpt_path) def tokenize_data(batch, tokenizer: T5Tokenizer, max_length: int) -> Dict: texts = ["<pad>" + sample["prompt"] + sample["completion"] for sample in batch] data = tokenizer( texts, return_tensors="pt", padding="max_length", truncation=True, max_length=max_length, add_special_tokens=False, ) data = {k: v.cuda() for k, v in data.items()} data["labels"] = data["input_ids"].clone() return data class RandomDataset(Dataset): def __init__(self, num_samples: int = 1000, max_length: int = 2048, vocab_size: int = 32000, tokenizer=None): self.num_samples = num_samples self.max_length = max_length self.input_ids = torch.randint( 0, vocab_size, (num_samples, max_length), device=get_accelerator().get_current_device() ) self.attention_mask = torch.ones_like(self.input_ids) def __len__(self): return self.num_samples def __getitem__(self, idx): return { "input_ids": self.input_ids[idx], "attention_mask": self.attention_mask[idx], "labels": self.input_ids[idx], } def parse_args(): # basic settings parser = argparse.ArgumentParser() parser.add_argument( "--model_name", type=str, default="base", choices=["base", "8b", "test"], help="Path to pretrained model or model identifier from huggingface.co/models.", ) parser.add_argument( "--plugin", type=str, default="hybrid", choices=["ep", "ep_zero", "hybrid"], help="Parallel methos. ep_zero is recommended for general cases. ep can provides least memory consumption and hybrid suits large scale training.", ) parser.add_argument( "--output_path", type=str, default="./outputs", help="The path of your saved model after finetuning.", ) parser.add_argument("--num_epoch", type=int, default=1, help="Number of epochs.") parser.add_argument( "--batch_size", type=int, default=1, help="Batch size (per dp group) for the training dataloader.", ) parser.add_argument( "--save_interval", type=int, default=1000, help=" The interval (steps) of saving checkpoints.", ) parser.add_argument( "--precision", type=str, default="bf16", choices=["fp32", "bf16", "fp16"], help="The mixed precision training.", ) parser.add_argument("--max_length", type=int, default=2048, help="Max sequence length.") parser.add_argument("--seed", type=int, default=42, help="A seed for reproducible training.") parser.add_argument( "--dataset", type=str, default="yizhongw/self_instruct", help="dataset name from `datasets` repo.", ) parser.add_argument( "--task_name", type=str, default="super_natural_instructions", help="task of corresponding dataset.", ) # optim parser.add_argument("--lr", type=float, default=1e-5, help="Learning rate.") parser.add_argument("--weight_decay", type=float, default=0.0, help="Weight decay to use.") # zero stage for all plugins parser.add_argument("--zero_stage", type=int, default=2, help="zero stage.") # ep_zero plugin parser.add_argument( "--extra_dp_size", type=int, default=1, help="ep_zero plugin's moe dp size. Recommended to be 2 or 4." ) # hybrid plugin parser.add_argument("--pp_size", type=int, default=2, help="pp size for hybrid plugin") parser.add_argument("--dp_size", type=int, default=1, help="dp size for hybrid plugin") parser.add_argument("--ep_size", type=int, default=2, help="ep size for hybrid plugin") parser.add_argument("--microbatch_size", type=int, default=1, help="Microbatch size in pipeline for hybrid plugin") # kernel parser.add_argument( "--use_kernel", action="store_true", help="Use kernel optim. Need to install flash attention and triton to enable all kernel optimizations. Skip if not installed.", ) parser.add_argument( "--use_layernorm_kernel", action="store_true", help="Use layernorm kernel. Need to install apex. Raise error if not installed.", ) # loss parser.add_argument( "--router_aux_loss_factor", type=float, default=0.01, help="Moe router z loss. You can refer to STMoE for details.", ) parser.add_argument( "--router_z_loss_factor", type=float, default=0.0001, help="Moe router aux loss. You can refer to STMoE for details.", ) parser.add_argument("--label_smoothing", type=float, default=0.0, help="Label smoothing.") parser.add_argument( "--z_loss_factor", type=float, default=0.0001, help="The final outputs' classification z loss factor." ) # load balance parser.add_argument( "--load_balance", action="store_true", help="Expert load balance. Defaults to False. Recommend to enable." ) parser.add_argument("--load_balance_interval", type=int, default=1000, help="Expert load balance interval.") # communicate overlap parser.add_argument( "--comm_overlap", action="store_true", help="Use communication overlap for MoE. Recommended to enable for multi-node training.", ) # hierarchical all-to-all parser.add_argument( "--hierarchical_alltoall", action="store_true", help="Use hierarchical all-to-all for MoE. Recommended to enable for multi-node training.", ) args = parser.parse_args() return args def main(): args = parse_args() # Launch ColossalAI colossalai.launch_from_torch(seed=args.seed) coordinator = DistCoordinator() test_mode = args.model_name == "test" # Set plugin booster_kwargs = {} hybrid_dict = { "tp_size": 1, "custom_policy": OpenMoeForCausalLMPolicy(), "enable_fused_normalization": args.use_layernorm_kernel, "enable_jit_fused": args.use_kernel, "precision": args.precision, "zero_stage": args.zero_stage, } if args.plugin == "ep": dp_size = dist.get_world_size() plugin = MoeHybridParallelPlugin( pp_size=1, ep_size=args.ep_size, **hybrid_dict, ) # MOE_MANAGER.setup( # parallel="EP", # max_ep_size=dp_size, # **mgr_dict, # ) elif args.plugin == "ep_zero": dp_size = dist.get_world_size() use_ep_inside = False plugin = MoeHybridParallelPlugin( pp_size=1, ep_size=dp_size // args.ep_size, use_ep_inside=use_ep_inside, **hybrid_dict, ) # MOE_MANAGER.setup( # parallel="EP", # max_ep_size=dp_size // args.extra_dp_size, # use_ep_inside=use_ep_inside, # **mgr_dict, # ) elif args.plugin == "hybrid": dp_size = dist.get_world_size() // args.pp_size plugin = MoeHybridParallelPlugin( pp_size=args.pp_size, ep_size=args.ep_size, microbatch_size=args.microbatch_size, **hybrid_dict, ) # MOE_MANAGER.setup( # parallel="EP", # mode="fixed", # fixed_dp_size=args.dp_size, # fixed_ep_size=args.ep_size, # fixed_pp_size=args.pp_size, # **mgr_dict, # ) else: raise ValueError(f"Invalid plugin {args.plugin}") coordinator.print_on_master(f"Set plugin as {plugin.__class__.__name__}") # Build OpenMoe model if test_mode: config = LlamaConfig.from_pretrained("hpcai-tech/openmoe-base") config.hidden_size = 128 config.intermediate_size = 256 config.vocab_size = 32000 else: repo_name = "hpcai-tech/openmoe-" + args.model_name config = LlamaConfig.from_pretrained(repo_name) set_openmoe_args( config, num_experts=config.num_experts, moe_layer_interval=config.moe_layer_interval, router_aux_loss_factor=args.router_aux_loss_factor, router_z_loss_factor=args.router_z_loss_factor, z_loss_factor=args.z_loss_factor, enable_load_balance=args.load_balance, enable_comm_overlap=args.comm_overlap, enable_hierarchical_alltoall=args.hierarchical_alltoall, enable_kernel=args.use_kernel, ) with skip_init(): model = OpenMoeForCausalLM(config) coordinator.print_on_master(f"Finish init model with config:\n{config}") # Enable gradient checkpointing model.gradient_checkpointing_enable() # Prepare tokenizer and dataloader tokenizer = T5Tokenizer.from_pretrained("google/umt5-small") if test_mode: dataset = RandomDataset(num_samples=20, tokenizer=tokenizer) collate_fn = None else: dataset = load_dataset(args.dataset, args.task_name) dataset = dataset["train"] collate_fn = partial(tokenize_data, tokenizer=tokenizer, max_length=args.max_length) dataloader = plugin.prepare_dataloader( dataset, batch_size=args.batch_size, shuffle=True, drop_last=True, collate_fn=collate_fn ) # Set optimizer optimizer = HybridAdam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) # Set booster booster = Booster(plugin=plugin, **booster_kwargs) if not test_mode: load_ckpt(repo_name, model, booster) model, optimizer, _, dataloader, _ = booster.boost(model=model, optimizer=optimizer, dataloader=dataloader) use_pipeline = isinstance(booster.plugin, MoeHybridParallelPlugin) and booster.plugin.pp_size > 1 is_pp_last_stage = use_pipeline and booster.plugin.stage_manager.is_last_stage() coordinator.print_on_master(f"Finish init booster") # Start finetuning coordinator.print_on_master(f"Start finetuning") for epoch in range(args.num_epoch): model.train() train_dataloader_iter = iter(dataloader) total_len = len(train_dataloader_iter) with tqdm( range(total_len), desc=f"Epoch [{epoch + 1}/{args.num_epoch}]", disable=not coordinator.is_master(), ) as pbar: for step in pbar: if use_pipeline: # Forward pass outputs = booster.execute_pipeline( train_dataloader_iter, model, lambda x, y: x.loss, optimizer, return_loss=True, ) # Backward and optimize if is_pp_last_stage: loss = outputs["loss"] pbar.set_postfix({"loss": loss.item()}) else: # Forward pass data = next(train_dataloader_iter) data = move_to_cuda(data, torch.cuda.current_device()) outputs = model(**data) loss = outputs["loss"] # Backward booster.backward(loss, optimizer) pbar.set_postfix({"loss": loss.item()}) optimizer.step() optimizer.zero_grad() # Apply load balance if ( args.load_balance and args.load_balance_interval > 0 and (step + 1) % args.load_balance_interval == 0 ): coordinator.print_on_master(f"Apply load balance") apply_load_balance(model, optimizer) # save checkpoint if (step + 1) % args.save_interval == 0: coordinator.print_on_master(f"Saving model checkpoint to {args.output_path}") booster.save_model(model, args.output_path, shard=True) # save checkpoint at the end of each epochs booster.save_model(model, args.output_path, shard=True) coordinator.print_on_master(f"Saving model checkpoint to {args.output_path}") # Finish training coordinator.print_on_master(f"Finish training") if __name__ == "__main__": main()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/infer.py
colossalai/legacy/moe/openmoe/infer.py
from argparse import ArgumentParser import torch from model.modeling_openmoe import OpenMoeForCausalLM, set_openmoe_args from transformers import T5Tokenizer from transformers.models.llama import LlamaConfig def parse_args(): parser = ArgumentParser() parser.add_argument("--model", default="base", type=str, help="model path", choices=["base", "8b", "test"]) return parser.parse_args() def inference(args): tokenizer = T5Tokenizer.from_pretrained("google/umt5-small") if args.model == "test": config = LlamaConfig.from_pretrained("hpcai-tech/openmoe-base") set_openmoe_args( config, num_experts=config.num_experts, moe_layer_interval=config.moe_layer_interval, enable_kernel=True ) model = OpenMoeForCausalLM(config) else: config = LlamaConfig.from_pretrained(f"hpcai-tech/openmoe-{args.model}") set_openmoe_args( config, num_experts=config.num_experts, moe_layer_interval=config.moe_layer_interval, enable_kernel=False ) model = OpenMoeForCausalLM.from_pretrained(f"hpcai-tech/openmoe-{args.model}", config=config) model = model.eval().bfloat16() model = model.to(torch.cuda.current_device()) input_str = """``` y = list(map(int, ['1', 'hello', '2'])) ``` What error does this program produce? ValueError: invalid literal for int() with base 10: 'hello' ``` sum = 0 for i in range(100): sum += i ``` What is the value of sum immediately after the 10th time line 3 is executed?""" # print("model config: ", model.config) input_ids = tokenizer("<pad>" + input_str, return_tensors="pt", add_special_tokens=False) input_ids = input_ids.input_ids.to(torch.cuda.current_device()) generation_output = model.generate(input_ids, use_cache=True, do_sample=True, max_new_tokens=64) out = tokenizer.decode(generation_output[0], skip_special_tokens=False) print(f"output: \n{out}\n") if __name__ == "__main__": args = parse_args() inference(args)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/model/openmoe_policy.py
colossalai/legacy/moe/openmoe/model/openmoe_policy.py
from functools import partial from typing import Callable, Dict, List, Optional, Union import torch import torch.nn as nn import torch.nn.functional as F from torch import Tensor from torch.nn import Module from transformers.modeling_outputs import CausalLMOutputWithPast from transformers.utils import logging from colossalai.legacy.moe.manager import MOE_MANAGER from colossalai.pipeline.stage_manager import PipelineStageManager from colossalai.shardformer.layer import FusedRMSNorm, Linear1D_Col from colossalai.shardformer.policies.base_policy import ModulePolicyDescription, Policy, SubModuleReplacementDescription from .modeling_openmoe import OpenMoeDecoderLayer, OpenMoeForCausalLM, OpenMoeModel __all__ = ["OpenMoePolicy", "OpenMoeForCausalLMPolicy"] class OpenMoePolicy(Policy): def config_sanity_check(self): pass def preprocess(self): if self.shard_config.enable_tensor_parallelism: # Resize embedding vocab_size = self.model.config.vocab_size world_size = self.shard_config.tensor_parallel_size if vocab_size % world_size != 0: new_vocab_size = vocab_size + world_size - vocab_size % world_size self.model.resize_token_embeddings(new_vocab_size) return self.model def module_policy(self) -> Dict[Union[str, nn.Module], ModulePolicyDescription]: policy = {} if self.shard_config.enable_sequence_parallelism: self.shard_config.enable_sequence_parallelism = False raise NotImplementedError( "openmoe doesn't support sequence parallelism now, will ignore the sequence parallelism flag." ) if self.shard_config.enable_tensor_parallelism: raise NotImplementedError("Tensor parallelism is not supported for openmoe model now.") # optimization configuration if self.shard_config.enable_fused_normalization: self.append_or_create_submodule_replacement( description=[ SubModuleReplacementDescription( suffix="input_layernorm", target_module=FusedRMSNorm, ), SubModuleReplacementDescription( suffix="post_attention_layernorm", target_module=FusedRMSNorm, ), SubModuleReplacementDescription( suffix="pre_extra_mlp_layernorm", target_module=FusedRMSNorm, ignore_if_not_exist=True, ), ], policy=policy, target_key=OpenMoeDecoderLayer, ) self.append_or_create_submodule_replacement( description=SubModuleReplacementDescription( suffix="norm", target_module=FusedRMSNorm, ), policy=policy, target_key=OpenMoeModel, ) if self.shard_config.enable_flash_attention: raise NotImplementedError("Flash attention has already been replaced in openmoe.") return policy def postprocess(self): return self.model def set_pipeline_forward(self, model_cls: nn.Module, new_forward: Callable, policy: Dict) -> None: """If under pipeline parallel setting, replacing the original forward method of huggingface to customized forward method, and add this changing to policy.""" if self.pipeline_stage_manager: stage_manager = self.pipeline_stage_manager if self.model.__class__.__name__ == "OpenMoeModel": module = self.model else: module = self.model.model layers_per_stage = stage_manager.distribute_layers(len(module.layers)) stage_index = stage_manager.get_stage_index(layers_per_stage) method_replacement = {"forward": partial(new_forward, stage_manager=stage_manager, stage_index=stage_index)} self.append_or_create_method_replacement( description=method_replacement, policy=policy, target_key=model_cls ) return def get_held_layers(self) -> List[Module]: """Get pipeline layers for current stage.""" assert self.pipeline_stage_manager is not None if self.model.__class__.__name__ == "OpenMoeModel": module = self.model else: module = self.model.model stage_manager = self.pipeline_stage_manager held_layers = [] layers_per_stage = stage_manager.distribute_layers(len(module.layers)) if stage_manager.is_first_stage(): held_layers.append(module.embed_tokens) start_idx, end_idx = stage_manager.get_stage_index(layers_per_stage) held_layers.extend(module.layers[start_idx:end_idx]) if stage_manager.is_last_stage(): held_layers.append(module.norm) return held_layers def distribute_layers(self, num_layers: int, num_stages: int) -> List[int]: """Divide layers into stages""" if num_layers == 24 and num_stages == 4: return [7, 7, 7, 3] elif num_layers == 24 and num_stages == 2: return [15, 9] elif num_layers == 12 and num_stages == 4: return [5, 5, 5, 1] elif num_layers == 12 and num_stages == 2: return [8, 4] else: print(f"num_layers: {num_layers}, num_stages: {num_stages} not optimized, use origin pp policy") return super().distribute_layers(num_layers, num_stages) class OpenMoeModelPolicy(OpenMoePolicy): def __init__(self) -> None: super().__init__() def module_policy(self): policy = super().module_policy() if self.pipeline_stage_manager: # set None as default self.set_pipeline_forward( model_cls=OpenMoeModel, new_forward=OpenMoePipelineForwards.openmoe_model_forward, policy=policy, ) return policy def get_held_layers(self) -> List[Module]: """Get pipeline layers for current stage.""" held_layers = super().get_held_layers() return held_layers def get_shared_params(self) -> List[Dict[int, Tensor]]: """No shared params in llama model""" return [] class OpenMoeForCausalLMPolicy(OpenMoePolicy): def module_policy(self): policy = super().module_policy() if self.shard_config.enable_tensor_parallelism: # add a new item for causal lm # TODO: recursively assign ep group foe all modules new_item = { OpenMoeForCausalLM: ModulePolicyDescription( sub_module_replacement=[ SubModuleReplacementDescription( suffix="lm_head", target_module=Linear1D_Col, kwargs=dict(gather_output=True), ) ] ) } policy.update(new_item) if self.pipeline_stage_manager: # set None as default self.set_pipeline_forward( model_cls=OpenMoeForCausalLM, new_forward=OpenMoePipelineForwards.llama_for_causal_lm_forward, policy=policy, ) return policy def get_held_layers(self) -> List[Module]: """Get pipeline layers for current stage.""" stage_manager = self.pipeline_stage_manager held_layers = super().get_held_layers() if stage_manager.is_last_stage(): held_layers.append(self.model.lm_head) return held_layers def get_shared_params(self) -> List[Dict[int, Tensor]]: llama_model = self.model.model if self.pipeline_stage_manager and self.pipeline_stage_manager.num_stages > 1: if ( id(llama_model.embed_tokens.weight) == id(self.model.lm_head.weight) and self.pipeline_stage_manager.num_stages > 1 ): # tie weights return [ { 0: llama_model.embed_tokens.weight, self.pipeline_stage_manager.num_stages - 1: self.model.lm_head.weight, } ] return [] class OpenMoePipelineForwards: """ This class serves as a micro library for forward function substitution of Llama models under pipeline setting. """ @staticmethod def openmoe_model_forward( self: OpenMoeModel, input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[List[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, stage_manager: Optional[PipelineStageManager] = None, hidden_states: Optional[torch.FloatTensor] = None, stage_index: Optional[List[int]] = None, past_router_aux_loss: Optional[torch.FloatTensor] = None, past_router_z_loss: Optional[torch.FloatTensor] = None, ): # reset moe loss for different data MOE_MANAGER.reset_loss() logger = logging.get_logger(__name__) output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) use_cache = use_cache if use_cache is not None else self.config.use_cache return_dict = return_dict if return_dict is not None else self.config.use_return_dict # retrieve input_ids and inputs_embeds if stage_manager.is_first_stage(): if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time") elif input_ids is not None: batch_size, seq_length = input_ids.shape elif inputs_embeds is not None: batch_size, seq_length, _ = inputs_embeds.shape else: raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds") device = input_ids.device if input_ids is not None else inputs_embeds.device if inputs_embeds is None: inputs_embeds = self.embed_tokens(input_ids) hidden_states = inputs_embeds else: input_shape = hidden_states.shape[:-1] batch_size, seq_length = input_shape device = hidden_states.device seq_length_with_past = seq_length past_key_values_length = 0 # TODO(jianghai): left the recording kv-value tensors as () or None type, this feature may be added in the future. if output_attentions: logger.warning_once("output_attentions=True is not supported for pipeline models at the moment.") output_attentions = False if output_hidden_states: logger.warning_once("output_hidden_states=True is not supported for pipeline models at the moment.") output_hidden_states = False if use_cache: logger.warning_once("use_cache=True is not supported for pipeline models at the moment.") use_cache = False if past_key_values is not None: past_key_values_length = past_key_values[0][0].shape[2] seq_length_with_past = seq_length_with_past + past_key_values_length if position_ids is None: position_ids = torch.arange( past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device, ) position_ids = position_ids.unsqueeze(0).view(-1, seq_length) else: position_ids = position_ids.view(-1, seq_length).long() # embed positions, for the first stage, hidden_states is the input embeddings, # for the other stages, hidden_states is the output of the previous stage if attention_mask is None: attention_mask = torch.ones( (batch_size, seq_length_with_past), dtype=torch.bool, device=hidden_states.device, ) attention_mask = self._prepare_decoder_attention_mask( attention_mask, (batch_size, seq_length), hidden_states, past_key_values_length, ) if self.gradient_checkpointing and self.training: if use_cache: logger.warning_once( "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..." ) use_cache = False # decoder layers all_hidden_states = () if output_hidden_states else None all_self_attns = () if output_attentions else None next_decoder_cache = () if use_cache else None start_idx, end_idx = stage_index[0], stage_index[1] for idx, decoder_layer in enumerate(self.layers[start_idx:end_idx], start=start_idx): if output_hidden_states: all_hidden_states += (hidden_states,) past_key_value = past_key_values[idx] if past_key_values is not None else None if self.gradient_checkpointing and self.training: def create_custom_forward(module): def custom_forward(*inputs): # None for past_key_value return module(*inputs, output_attentions, None) return custom_forward layer_outputs = torch.utils.checkpoint.checkpoint( create_custom_forward(decoder_layer), hidden_states, attention_mask, position_ids, None, ) else: layer_outputs = decoder_layer( hidden_states, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, ) hidden_states = layer_outputs[0] if use_cache: next_decoder_cache += (layer_outputs[2 if output_attentions else 1],) if output_attentions: all_self_attns += (layer_outputs[1],) if stage_manager.is_last_stage(): hidden_states = self.norm(hidden_states) # add hidden states from the last decoder layer if output_hidden_states: all_hidden_states += (hidden_states,) next_cache = next_decoder_cache if use_cache else None # concat past losses with current ones router_aux_loss, router_z_loss = MOE_MANAGER.get_loss() if past_router_aux_loss is not None and past_router_z_loss is not None: router_aux_loss = past_router_aux_loss + router_aux_loss router_z_loss = past_router_z_loss + router_z_loss if stage_manager.is_last_stage(): return tuple( [ hidden_states, next_cache, all_hidden_states, all_self_attns, router_aux_loss, router_z_loss, ] ) # always return dict for imediate stage return { "hidden_states": hidden_states, "router_aux_loss": router_aux_loss, "router_z_loss": router_z_loss, } @staticmethod def llama_for_causal_lm_forward( self: OpenMoeForCausalLM, input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_values: Optional[List[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, stage_manager: Optional[PipelineStageManager] = None, hidden_states: Optional[torch.FloatTensor] = None, stage_index: Optional[List[int]] = None, chunk_head: Optional[bool] = True, past_router_aux_loss: Optional[torch.FloatTensor] = None, past_router_z_loss: Optional[torch.FloatTensor] = None, ): r""" Args: labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. Returns: Example: ```python >>> from transformers import AutoTokenizer, LlamaForCausalLM >>> model = LlamaForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS) >>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER) >>> prompt = "Hey, are you consciours? Can you talk to me?" >>> inputs = tokenizer(prompt, return_tensors="pt") >>> # Generate >>> generate_ids = model.generate(inputs.input_ids, max_length=30) >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] "Hey, are you consciours? Can you talk to me?\nI'm not consciours, but I can talk to you." ```""" logger = logging.get_logger(__name__) output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) return_dict = return_dict if return_dict is not None else self.config.use_return_dict # TODO(jianghai): left the recording kv-value tensors as () or None type, this feature may be added in the future. if output_attentions: logger.warning_once("output_attentions=True is not supported for pipeline models at the moment.") output_attentions = False if output_hidden_states: logger.warning_once("output_hidden_states=True is not supported for pipeline models at the moment.") output_hidden_states = False # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn) outputs = OpenMoePipelineForwards.openmoe_model_forward( self.model, input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, stage_manager=stage_manager, hidden_states=hidden_states, stage_index=stage_index, past_router_aux_loss=past_router_aux_loss, past_router_z_loss=past_router_z_loss, ) if stage_manager.is_last_stage(): ( hidden_states, past_key_values, all_hidden_states, attentions, router_aux_loss, router_z_loss, ) = outputs if self.pretraining_tp > 1: lm_head_slices = self.lm_head.weight.split(self.vocab_size // self.pretraining_tp, dim=0) logits = [F.linear(hidden_states, lm_head_slices[i]) for i in range(self.pretraining_tp)] logits = torch.cat(logits, dim=-1) loss = None # if no training, just do forward if labels is None: logits = self.lm_head(hidden_states) logits = logits.float() # the vocab size for openmoe is 30w+ # which causes great activation memory in training, up to 20G for one sequence # so we use chunk and checkpoint to reduce memory else: if chunk_head == True: def create_custom_forward(module): def custom_forward(*inputs): logits = module(inputs[0]) logits = logits.float() # Shift so that tokens < n predict n shift_logits = logits[..., :-1, :].contiguous().float() shift_labels = inputs[1][..., 1:].contiguous() # Flatten the tokens loss = self._calculate_loss(shift_logits, shift_labels) return loss return custom_forward aux_loss, z_loss = self._calculate_router_loss(router_aux_loss, router_z_loss) loss = aux_loss + z_loss for batch_idx in range(hidden_states.shape[0]): loss = loss + torch.utils.checkpoint.checkpoint( create_custom_forward(self.lm_head), hidden_states[batch_idx : batch_idx + 1, :], labels[batch_idx : batch_idx + 1, :], ) logits = None else: logits = self.lm_head(hidden_states) logits = logits.float() # Shift so that tokens < n predict n shift_logits = logits[..., :-1, :].contiguous() shift_labels = labels[..., 1:].contiguous() # Flatten the tokens aux_loss, z_loss = self._calculate_router_loss(router_aux_loss, router_z_loss) loss = aux_loss + z_loss loss = loss + self._calculate_loss(shift_logits, shift_labels) if not return_dict: output = (logits,) + outputs[1:] return (loss,) + output if loss is not None else output return CausalLMOutputWithPast( loss=loss, logits=logits, past_key_values=past_key_values, hidden_states=all_hidden_states, attentions=attentions, ) else: hidden_states = outputs["hidden_states"] router_aux_loss = outputs["router_aux_loss"] router_z_loss = outputs["router_z_loss"] return { "hidden_states": hidden_states, "past_router_aux_loss": router_aux_loss, "past_router_z_loss": router_z_loss, }
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/model/convert_openmoe_ckpt.py
colossalai/legacy/moe/openmoe/model/convert_openmoe_ckpt.py
# coding=utf-8 # Copyright 2022 Google LLC and HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ Convert T5X checkpoint to PyTorch Steps: - Install gsutil according to https://cloud.google.com/storage/docs/gsutil_install - Get a T5X checkpoint at https://github.com/google-research/t5x/blob/main/docs/models.md#t5-11-checkpoints Example: `gsutil -m cp -r gs://t5-data/pretrained_models/t5x/t5_1_1_small $HOME/` - Create or download a corresponding config for the downloaded model. E.g. for T5 v1.1 small, you can use https://huggingface.co/google/t5-v1_1-small/blob/main/config.json - Convert: ``` python3 convert_t5x_checkpoint_to_pytorch.py --t5x_checkpoint_path=$HOME/t5_1_1_small --config_file=config.json\ --pytorch_dump_path=$HOME/t5_1_1_small_pt ``` """ import argparse import collections import torch from flax import traverse_util from modeling_openmoe import OpenMoeForCausalLM from t5x import checkpoints from transformers import LlamaConfig from transformers.utils import logging logging.set_verbosity_info() def t5x_attention_lookup(params, i, prefix, layer_name="attention"): """Returns the KOQV parameters of (self-)attention. Does not transpose.""" k = params[f"{prefix}/layers_{i}/{layer_name}/key/kernel"] o = params[f"{prefix}/layers_{i}/{layer_name}/out/kernel"] q = params[f"{prefix}/layers_{i}/{layer_name}/query/kernel"] v = params[f"{prefix}/layers_{i}/{layer_name}/value/kernel"] return k, o, q, v def t5x_mlp_lookup(params, i, prefix, split_mlp_wi=False): """Returns the MLP parameters of a layer. Does not transpose.""" if split_mlp_wi: wi_0 = params[f"{prefix}/layers_{i}/mlp/wi_0/kernel"] wi_1 = params[f"{prefix}/layers_{i}/mlp/wi_1/kernel"] wi = (wi_0, wi_1) else: wi = params[f"{prefix}/layers_{i}/mlp/wi/kernel"] wo = params[f"{prefix}/layers_{i}/mlp/wo/kernel"] return wi, wo def t5x_extra_mlp_lookup(params, i, prefix, split_mlp_wi=False): """Returns the MLP parameters of a layer. Does not transpose.""" if split_mlp_wi: wi_0 = params[f"{prefix}/layers_{i}/extra_mlp/wi_0/kernel"] wi_1 = params[f"{prefix}/layers_{i}/extra_mlp/wi_1/kernel"] wi = (wi_0, wi_1) else: wi = params[f"{prefix}/layers_{i}/extra_mlp/wi/kernel"] wo = params[f"{prefix}/layers_{i}/extra_mlp/wo/kernel"] return wi, wo def t5x_experts_lookup(params, i, prefix, split_mlp_wi=False): """Returns the MLP parameters of a layer. Does not transpose.""" if split_mlp_wi: wi_0 = params[f"{prefix}/layers_{i}/mlp/expert/wi_0/kernel"] wi_1 = params[f"{prefix}/layers_{i}/mlp/expert/wi_1/kernel"] wi = (wi_0, wi_1) else: wi = params[f"{prefix}/layers_{i}/mlp/expert/wi/kernel"] wo = params[f"{prefix}/layers_{i}/mlp/expert/wo/kernel"] return wi, wo def t5x_gate_lookup(params, i, prefix, split_mlp_wi=False): """Returns the MLP parameters of a layer. Does not transpose.""" return params[f"{prefix}/layers_{i}/mlp/router/router_weights/w/kernel"] def t5x_layer_norm_lookup(params, i, prefix, layer_name): """Returns the layer norm param of a layer.""" return params[f"{prefix}/layers_{i}/{layer_name}/scale"] def convert_t5x_to_pytorch(variables: dict, *, num_layers: int, moe_interval: int): """Converts the parameters from T5X-Flax to Transformers-PyTorch.""" old = traverse_util.flatten_dict(variables["target"]) old = {"/".join(k): v for k, v in old.items()} # v1.1 models have a gated GeLU with wi_0 and wi_1 instead of wi split_mlp_wi = True print("Split MLP:", split_mlp_wi) new = collections.OrderedDict() print(old.keys()) for key, value in old.items(): print(f"{key}: {value.shape}") # Shared embeddings. new["model.embed_tokens.weight"] = old["token_embedder/embedding"] # Decoder. for i in range(num_layers): # Block i, layer 0 (Self Attention). layer_norm = t5x_layer_norm_lookup(old, i, "decoder", "pre_self_attention_layer_norm") k, o, q, v = t5x_attention_lookup(old, i, "decoder", "self_attention") new[f"model.layers.{i}.input_layernorm.weight"] = layer_norm new[f"model.layers.{i}.self_attn.k_proj.weight"] = k.T new[f"model.layers.{i}.self_attn.o_proj.weight"] = o.T new[f"model.layers.{i}.self_attn.q_proj.weight"] = q.T new[f"model.layers.{i}.self_attn.v_proj.weight"] = v.T # Block i, layer 2 (MLP). layer_norm = t5x_layer_norm_lookup(old, i, "decoder", "pre_mlp_layer_norm") new[f"model.layers.{i}.post_attention_layernorm.weight"] = layer_norm if (i + 1) % moe_interval == 0: # moe gate = t5x_gate_lookup(old, i, "decoder", split_mlp_wi) new[f"model.layers.{i}.mlp.gate_weight"] = gate.T wi, wo = t5x_experts_lookup(old, i, "decoder", split_mlp_wi) new[f"model.layers.{i}.mlp.experts.wi_gate"] = wi[0] new[f"model.layers.{i}.mlp.experts.wi_up"] = wi[1] new[f"model.layers.{i}.mlp.experts.wo"] = wo # extra layer_norm = t5x_layer_norm_lookup(old, i, "decoder", "pre_extra_mlp_layer_norm") new[f"model.layers.{i}.pre_extra_mlp_layernorm.weight"] = layer_norm wi, wo = t5x_extra_mlp_lookup(old, i, "decoder", split_mlp_wi) new[f"model.layers.{i}.extra_mlp.gate_proj.weight"] = wi[0].T new[f"model.layers.{i}.extra_mlp.up_proj.weight"] = wi[1].T new[f"model.layers.{i}.extra_mlp.down_proj.weight"] = wo.T else: wi, wo = t5x_mlp_lookup(old, i, "decoder", split_mlp_wi) new[f"model.layers.{i}.mlp.gate_proj.weight"] = wi[0].T new[f"model.layers.{i}.mlp.up_proj.weight"] = wi[1].T new[f"model.layers.{i}.mlp.down_proj.weight"] = wo.T new["model.norm.weight"] = old["decoder/decoder_norm/scale"] # LM Head (only in v1.1 checkpoints, in v1.0 embeddings are used instead) if "decoder/logits_dense/kernel" in old: new["lm_head.weight"] = old["decoder/logits_dense/kernel"].T return new def make_state_dict(converted_params): """Prepares a state dict for the PyTorch model.""" # Make a state dict with torch tensors. state_dict = collections.OrderedDict([(k, torch.from_numpy(v.copy())) for (k, v) in converted_params.items()]) return state_dict def load_t5x_weights_in_t5(model, config, t5x_checkpoint_path): """Replaces the params in model witht the T5X converted params.""" variables = checkpoints.load_t5x_checkpoint(t5x_checkpoint_path) converted = convert_t5x_to_pytorch( variables, num_layers=config.num_hidden_layers, moe_interval=config.moe_layer_interval ) state_dict = make_state_dict(converted) model.load_state_dict(state_dict, strict=True) def convert_t5x_checkpoint_to_pytorch(t5x_checkpoint_path, config_file, pytorch_dump_path): """Loads the config and model, converts the T5X checkpoint, and saves a PyTorch checkpoint.""" # Initialise PyTorch model config = LlamaConfig.from_json_file(config_file) print(f"Building PyTorch model from configuration: {config}") # Non-v1.1 checkpoints could also use T5Model, but this works for all. # The v1.0 checkpoints will simply have an LM head that is the word embeddings. model = OpenMoeForCausalLM(config) # Load weights from tf checkpoint load_t5x_weights_in_t5(model, config, t5x_checkpoint_path) # Save pytorch-model print(f"Save PyTorch model to {pytorch_dump_path}") model.save_pretrained(pytorch_dump_path) # Verify that we can load the checkpoint. model.from_pretrained(pytorch_dump_path) print("Done") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Converts a native T5X checkpoint into a PyTorch checkpoint.") # Required parameters parser.add_argument( "--t5x_checkpoint_path", default=None, type=str, required=True, help="Path to the T5X checkpoint." ) parser.add_argument( "--config_file", default=None, type=str, required=True, help="The config json file corresponding to the pre-trained T5 model.\nThis specifies the model architecture.", ) parser.add_argument( "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model." ) args = parser.parse_args() convert_t5x_checkpoint_to_pytorch(args.t5x_checkpoint_path, args.config_file, args.pytorch_dump_path)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/model/__init__.py
colossalai/legacy/moe/openmoe/model/__init__.py
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/model/modeling_openmoe.py
colossalai/legacy/moe/openmoe/model/modeling_openmoe.py
# coding=utf-8 # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. # # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX # and OPT implementations in this library. It has been modified from its # original forms to accommodate minor architectural differences compared # to GPT-NeoX and OPT used by the Meta AI team that trained the model. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ PyTorch OpenMoE model.""" import math from typing import List, Optional, Tuple, Union import torch import torch.nn.functional as F import torch.utils.checkpoint from torch import nn from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast from transformers.modeling_utils import PreTrainedModel from transformers.models.llama.modeling_llama import LlamaConfig, LlamaRMSNorm from transformers.utils import ( add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings, ) try: # TODO: remove this after updating openmoe example # NOTE(yuanheng-zhao): This is a temporary fix for the issue that # the flash_attention module is not imported correctly for different CI tests. # We replace the import path `colossalai.kernel.extensions.flash_attention` # because in the current example test, colossalai version <= 0.3.6 is installed, # where `colossalai.kernel.extensions.flash_attention` is still valid; # however in unit test `test_moe_checkpoint`, the lastest version of colossalai is installed, # where extension has been refactored and the path is not valid. import flash_attention # noqa HAS_FLASH_ATTN = True except: HAS_FLASH_ATTN = False from colossalai.kernel.triton.llama_act_combine_kernel import HAS_TRITON from colossalai.legacy.moe.manager import MOE_MANAGER from colossalai.legacy.moe.utils import get_activation, set_moe_args from colossalai.shardformer.layer.moe import SparseMLP if HAS_TRITON: from colossalai.kernel.triton.llama_act_combine_kernel import LlamaActCombine logger = logging.get_logger(__name__) _CONFIG_FOR_DOC = "LlamaConfig" def set_openmoe_args( config: LlamaConfig, num_experts: int, moe_layer_interval: int, router_topk: int = 2, router_capacity_factor_train: float = 1.25, router_capacity_factor_eval: float = 2.0, router_min_capacity: int = 4, router_noisy_policy: str = None, router_drop_tks: bool = True, router_aux_loss_factor: float = 0.01, router_z_loss_factor: float = 0.0001, mlp_gated: bool = True, label_smoothing: float = 0.001, z_loss_factor: float = 0.01, enable_load_balance: bool = False, load_balance_tolerance: float = 0.1, load_balance_beam_width: int = 8, load_balance_group_swap_factor: float = 0.4, enable_kernel: bool = False, enable_comm_overlap: bool = False, enable_hierarchical_alltoall: bool = True, ) -> None: """ MoE related arguments. It inserts the MoE arguments into the Llama config. Args: config (LlamaConfig): Transformers Llama config. num_experts (int, optional): Number of experts. moe_layer_interval (int, optional): The interval moe layer. router_topk (int, optional): Moe router top k. Defaults to 2. router_capacity_factor_train (float, optional): Moe router max capacity for train. Defaults to 1.25. router_capacity_factor_eval (float, optional): Moe router max capacity for eval. Defaults to 2.0. router_min_capacity (int, optional): Moe router min capacity. Defaults to 4. router_noisy_policy (str, optional): Moe router noisy policy. You can choose [Jitter, Gaussian, None]. Defaults to None. router_drop_tks (bool, optional): Whether moe router drop tokens which exceed max capacity. Defaults to True. router_aux_loss_factor (float, optional): Moe router aux loss. You can refer to STMoE for details. Defaults to 0.01. router_z_loss_factor (float, optional): Moe router z loss. You can refer to STMoE for details. Defaults to 0.01. mlp_gated (bool, optional): Use gate in mlp. Defaults to True. label_smoothing (float, optional): Label smoothing. Defaults to 0.001. z_loss_factor (float, optional): The final outputs' classification z loss factor. Defaults to 0.01. enable_load_balance (bool, optional): Expert load balance. Defaults to False. load_balance_tolerance (float, optional): Expert load balance search's difference tolerance. Defaults to 0.1. load_balance_beam_width (int, optional): Expert load balance search's beam width. Defaults to 8. load_balance_group_swap_factor (float, optional): Expert load balance group swap factor. Longer value encourages less swap. Defaults to 0.4. enable_kernel (bool, optional): Use kernel optimization. Defaults to False. enable_comm_overlap (bool, optional): Use communication overlap for MoE. Recommended to enable for multi-node training. Defaults to False. enable_hierarchical_alltoall (bool, optional): Use hierarchical alltoall for MoE. Defaults to False. """ moe_args = dict( num_experts=num_experts, moe_layer_interval=moe_layer_interval, router_topk=router_topk, router_capacity_factor_train=router_capacity_factor_train, router_capacity_factor_eval=router_capacity_factor_eval, router_min_capacity=router_min_capacity, router_noisy_policy=router_noisy_policy, router_drop_tks=router_drop_tks, router_aux_loss_factor=router_aux_loss_factor, router_z_loss_factor=router_z_loss_factor, mlp_gated=mlp_gated, label_smoothing=label_smoothing, z_loss_factor=z_loss_factor, enable_load_balance=enable_load_balance, load_balance_tolerance=load_balance_tolerance, load_balance_beam_width=load_balance_beam_width, load_balance_group_swap_factor=load_balance_group_swap_factor, enable_kernel=enable_kernel, enable_comm_overlap=enable_comm_overlap, enable_hierarchical_alltoall=enable_hierarchical_alltoall, ) set_moe_args(config, moe_args) # Copied from transformers.models.bart.modeling_bart._make_causal_mask def _make_causal_mask( input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0 ): """ Make causal mask used for bi-directional self-attention. """ bsz, tgt_len = input_ids_shape mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device) mask_cond = torch.arange(mask.size(-1), device=device) mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0) mask = mask.to(dtype) if past_key_values_length > 0: mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1) return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length) # Copied from transformers.models.bart.modeling_bart._expand_mask def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None): """ Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`. """ bsz, src_len = mask.size() tgt_len = tgt_len if tgt_len is not None else src_len expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype) inverted_mask = 1.0 - expanded_mask return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min) def generate_fixed_pos_embedding(features, length, min_timescale=1.0, max_timescale=10000.0): """Generate Sin/Cos for Rotary Embeddings. Args: features: an integer length: an integer min_timescale: an optional float max_timescale: an optional float Returns: output_sin: a float32 Tensor with shape [length, features] output_cos: a float32 Tensor with shape [length, features] """ fraction = torch.arange(0, features, 2, dtype=torch.float32).cuda() / features timescale = min_timescale * (max_timescale / min_timescale) ** fraction rotational_frequency = 1.0 / timescale sinusoid_inp = torch.einsum("i,j->ij", torch.arange(length, dtype=torch.float32).cuda(), rotational_frequency) sinusoid_inp = torch.cat([sinusoid_inp, sinusoid_inp], dim=-1) return torch.sin(sinusoid_inp), torch.cos(sinusoid_inp) def apply_rotary_embedding(q, k, cos, sin, decode=False, rotary_index=None): """Helper function to apply Rotary Embeddings.""" cos = cos.to(q.dtype) sin = sin.to(q.dtype) if len(k.shape) == 3: # for multi query attention k = k.unsqueeze(2) multiquery = True else: multiquery = False batch, qlen, qheads, d = q.shape kbatch, klen, kheads, kd = k.shape assert batch == kbatch, f"{batch} != {kbatch}" assert d == kd, f"{d} != {kd}" if decode and qlen == 1 and rotary_index is not None: qcos = cos[rotary_index + 1, :] qsin = sin[rotary_index + 1, :] qcos = qcos.unsqueeze(2) qsin = qsin.unsqueeze(2) kcos, ksin = cos[:klen, :], sin[:klen, :] kcos = kcos.unsqueeze(0).unsqueeze(2) ksin = ksin.unsqueeze(0).unsqueeze(2) else: qcos, qsin = cos[:qlen, :], sin[:qlen, :] qcos = qcos.unsqueeze(0).unsqueeze(2) qsin = qsin.unsqueeze(0).unsqueeze(2) kcos, ksin = qcos, qsin out_q = (q * qcos) + (rotate_half(q) * qsin) out_k = (k * kcos) + (rotate_half(k) * ksin) if multiquery: out_k = out_k.squeeze(2) return out_q, out_k def rotate_half(x): """Rotates half the hidden dims of the input.""" x1 = x[..., : x.shape[-1] // 2] x2 = x[..., x.shape[-1] // 2 :] return torch.cat((-x2, x1), dim=-1) def SwiGLU(x): """Gated linear unit activation function. Args: x : input array axis: the axis along which the split should be computed (default: -1) """ size = x.shape[-1] assert size % 2 == 0, "axis size must be divisible by 2" x1, x2 = torch.split(x, size // 2, -1) return x1 * (x2 * torch.sigmoid(x2)) class OpenMoeMLP(nn.Module): def __init__(self, config: LlamaConfig): super().__init__() self.pretraining_tp = config.pretraining_tp self.hidden_size = config.hidden_size self.intermediate_size = config.intermediate_size self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size * 2, bias=False) self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False) self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False) self.hidden_act = config.hidden_act self.act_fn = get_activation(self.hidden_act) self.use_kernel = config.enable_kernel def forward(self, x): if self.pretraining_tp > 1: slice = self.intermediate_size // self.pretraining_tp gate_proj_slices = self.gate_proj.weight.split(slice, dim=0) up_proj_slices = self.up_proj.weight.split(slice, dim=0) down_proj_slices = self.down_proj.weight.split(slice, dim=1) gate_proj = torch.cat([F.linear(x, gate_proj_slices[i]) for i in range(self.pretraining_tp)], dim=-1) up_proj = torch.cat([F.linear(x, up_proj_slices[i]) for i in range(self.pretraining_tp)], dim=-1) intermediate_states = (self.act_fn(gate_proj) * up_proj).split(slice, dim=2) down_proj = [F.linear(intermediate_states[i], down_proj_slices[i]) for i in range(self.pretraining_tp)] down_proj = sum(down_proj) else: if HAS_TRITON and self.use_kernel and self.hidden_act == "swiglu": down_proj = self.down_proj(LlamaActCombine.apply(self.gate_proj(x), self.up_proj(x))) else: down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x)) return down_proj def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: """ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) """ batch, num_key_value_heads, slen, head_dim = hidden_states.shape if n_rep == 1: return hidden_states hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) class OpenMoeAttention(nn.Module): """Multi-headed attention from 'Attention Is All You Need' paper""" def __init__(self, config: LlamaConfig): super().__init__() self.config = config self.hidden_size = config.hidden_size self.num_heads = config.num_attention_heads self.head_dim = config.head_dim self.num_key_value_heads = config.num_key_value_heads self.num_key_value_groups = self.num_heads // self.num_key_value_heads self.pretraining_tp = config.pretraining_tp self.max_position_embeddings = config.max_position_embeddings self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False) self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False) self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False) self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False) self.sin, self.cos = generate_fixed_pos_embedding(self.head_dim, self.max_position_embeddings, 1.0, 1e4) def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int): return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous() def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Tuple[torch.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, use_kernel: bool = True, ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() if self.pretraining_tp > 1: key_value_slicing = (self.num_key_value_heads * self.head_dim) // self.pretraining_tp query_slices = self.q_proj.weight.split((self.num_heads * self.head_dim) // self.pretraining_tp, dim=0) key_slices = self.k_proj.weight.split(key_value_slicing, dim=0) value_slices = self.v_proj.weight.split(key_value_slicing, dim=0) query_states = [F.linear(hidden_states, query_slices[i]) for i in range(self.pretraining_tp)] query_states = torch.cat(query_states, dim=-1) key_states = [F.linear(hidden_states, key_slices[i]) for i in range(self.pretraining_tp)] key_states = torch.cat(key_states, dim=-1) value_states = [F.linear(hidden_states, value_slices[i]) for i in range(self.pretraining_tp)] value_states = torch.cat(value_states, dim=-1) else: query_states = self.q_proj(hidden_states) key_states = self.k_proj(hidden_states) value_states = self.v_proj(hidden_states) query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2) kv_seq_len = key_states.shape[-2] if past_key_value is not None: kv_seq_len += past_key_value[0].shape[-2] # cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len) # query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) if past_key_value is not None: # reuse k, v, self_attention key_states = torch.cat([past_key_value[0], key_states], dim=2) value_states = torch.cat([past_key_value[1], value_states], dim=2) past_key_value = (key_states, value_states) if use_cache else None query_states = query_states.transpose(1, 2) key_states = key_states.transpose(1, 2) max_length = max(query_states.shape[1], key_states.shape[1]) assert max_length <= self.sin.shape[0] sin, cos = self.sin[:max_length], self.cos[:max_length] # TODO: for inference, we can add emb kv into cache to avoid computation query_states, key_states = apply_rotary_embedding( query_states, key_states, cos, sin, decode=True if q_len == 1 else False, rotary_index=position_ids ) query_states = query_states.transpose(1, 2) key_states = key_states.transpose(1, 2) # repeat k/v heads if n_kv_heads < n_heads key_states = repeat_kv(key_states, self.num_key_value_groups) value_states = repeat_kv(value_states, self.num_key_value_groups) if HAS_FLASH_ATTN and use_kernel: from flash_attn import flash_attn_func query_states = query_states.transpose(1, 2) key_states = key_states.transpose(1, 2) value_states = value_states.transpose(1, 2) attn_output = flash_attn_func(query_states, key_states, value_states, softmax_scale=1.0, causal=True) attn_output = attn_output.transpose(1, 2).contiguous() else: attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len): raise ValueError( f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is" f" {attn_weights.size()}" ) if attention_mask is not None: if attention_mask.size() != (bsz, 1, q_len, kv_seq_len): raise ValueError( f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}" ) if self.training: attention_mask = attention_mask.clone().detach() attention_mask[:, :, :, 0] = 0 attn_weights = attn_weights + attention_mask # upcast attention to fp32 attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype) attn_output = torch.matmul(attn_weights, value_states) if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim): raise ValueError( f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is" f" {attn_output.size()}" ) attn_output = attn_output.transpose(1, 2).contiguous() attn_output = attn_output.reshape(bsz, q_len, self.num_heads * self.head_dim) if self.pretraining_tp > 1: attn_output = attn_output.split(self.hidden_size // self.pretraining_tp, dim=2) o_proj_slices = self.o_proj.weight.split(self.hidden_size // self.pretraining_tp, dim=1) attn_output = sum([F.linear(attn_output[i], o_proj_slices[i]) for i in range(self.pretraining_tp)]) else: attn_output = self.o_proj(attn_output) if not output_attentions: attn_weights = None return attn_output, attn_weights, past_key_value class OpenMoeDecoderLayer(nn.Module): def __init__(self, config: LlamaConfig, moe: bool): super().__init__() self.hidden_size = config.hidden_size self.moe = moe self.self_attn = OpenMoeAttention(config=config) self.input_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.post_attention_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps) if self.moe: self.mlp = SparseMLP( num_experts=config.num_experts, hidden_size=config.hidden_size, intermediate_size=config.intermediate_size, router_top_k=config.router_topk, router_capacity_factor_train=config.router_capacity_factor_train, router_capacity_factor_eval=config.router_capacity_factor_eval, router_min_capacity=config.router_min_capacity, router_noisy_policy=config.router_noisy_policy, router_drop_tks=config.router_drop_tks, mlp_activation=config.hidden_act, mlp_gated=config.mlp_gated, enable_load_balance=config.enable_load_balance, load_balance_tolerance=config.load_balance_tolerance, load_balance_beam_width=config.load_balance_beam_width, load_balance_group_swap_factor=config.load_balance_group_swap_factor, enable_kernel=config.enable_kernel, enable_hierarchical_comm=config.enable_hierarchical_alltoall, ) self.pre_extra_mlp_layernorm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.extra_mlp = OpenMoeMLP(config) else: self.mlp = OpenMoeMLP(config) def forward( self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` attention_mask (`torch.FloatTensor`, *optional*): attention mask of size `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states """ residual = hidden_states hidden_states = self.input_layernorm(hidden_states) # Self Attention hidden_states, self_attn_weights, present_key_value = self.self_attn( hidden_states=hidden_states, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, ) hidden_states = residual + hidden_states # Fully Connected residual = hidden_states hidden_states = self.post_attention_layernorm(hidden_states) hidden_states = self.mlp(hidden_states) hidden_states = residual + hidden_states if self.moe: residual = hidden_states hidden_states = self.pre_extra_mlp_layernorm(hidden_states) hidden_states = self.extra_mlp(hidden_states) hidden_states = residual + hidden_states outputs = (hidden_states,) if output_attentions: outputs += (self_attn_weights,) if use_cache: outputs += (present_key_value,) return outputs LLAMA_START_DOCSTRING = r""" This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.) This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. Parameters: config ([`LlamaConfig`]): Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights. """ @add_start_docstrings( "The bare LLaMA Model outputting raw hidden-states without any specific head on top.", LLAMA_START_DOCSTRING, ) class OpenMoePreTrainedModel(PreTrainedModel): config_class = LlamaConfig base_model_prefix = "model" supports_gradient_checkpointing = True _no_split_modules = ["LlamaDecoderLayer"] _skip_keys_device_placement = "past_key_values" def _init_weights(self, module): std = self.config.initializer_range if isinstance(module, nn.Linear): module.weight.data.normal_(mean=0.0, std=std) if module.bias is not None: module.bias.data.zero_() elif isinstance(module, nn.Embedding): module.weight.data.normal_(mean=0.0, std=std) if module.padding_idx is not None: module.weight.data[module.padding_idx].zero_() def _set_gradient_checkpointing(self, module, value=False): if isinstance(module, OpenMoeModel): module.gradient_checkpointing = value LLAMA_INPUTS_DOCSTRING = r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. [What are attention masks?](../glossary#attention-mask) Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see `past_key_values`). If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`] and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more information on the default strategy. - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids) past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`. Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention blocks) that can be used (see `past_key_values` input) to speed up sequential decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `decoder_input_ids` of shape `(batch_size, sequence_length)`. inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert `input_ids` indices into associated vectors than the model's internal embedding lookup matrix. use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail. return_dict (`bool`, *optional*): Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple. """ @add_start_docstrings( "The bare LLaMA Model outputting raw hidden-states without any specific head on top.", LLAMA_START_DOCSTRING, ) class OpenMoeModel(OpenMoePreTrainedModel): """ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`LlamaDecoderLayer`] Args: config: LlamaConfig """ def __init__(self, config: LlamaConfig): super().__init__(config) self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx) self.layers = nn.ModuleList( [ OpenMoeDecoderLayer(config, moe=True if (i + 1) % config.moe_layer_interval == 0 else False) for i in range(config.num_hidden_layers) ] ) self.norm = LlamaRMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.gradient_checkpointing = False # Initialize weights and apply final processing self.post_init() def get_input_embeddings(self): return self.embed_tokens def set_input_embeddings(self, value): self.embed_tokens = value # Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length): # create causal mask # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len] combined_attention_mask = None if input_shape[-1] > 1: combined_attention_mask = _make_causal_mask( input_shape, inputs_embeds.dtype, device=inputs_embeds.device, past_key_values_length=past_key_values_length, ) if attention_mask is not None: # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len] expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to( inputs_embeds.device ) combined_attention_mask = ( expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask ) return combined_attention_mask
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
true
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/benchmark/benchmark_fsdp.py
colossalai/legacy/moe/openmoe/benchmark/benchmark_fsdp.py
import argparse import functools import os import torch import torch.distributed as dist import tqdm from model.modeling_openmoe import LlamaConfig, OpenMoeDecoderLayer, OpenMoeForCausalLM, set_openmoe_args from torch.distributed.fsdp import FullyShardedDataParallel as FSDP from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision from torch.distributed.fsdp.wrap import transformer_auto_wrap_policy from torch.utils.data import Dataset from torch.utils.data.distributed import DistributedSampler from transformers.models.llama import LlamaConfig from utils import PerformanceEvaluator, get_model_numel from colossalai.legacy.moe.manager import MOE_MANAGER class RandomDataset(Dataset): def __init__(self, num_samples: int = 1000, max_length: int = 2048, vocab_size: int = 32000): self.num_samples = num_samples self.max_length = max_length self.input_ids = torch.randint(0, vocab_size, (num_samples, max_length)) self.attention_mask = torch.ones_like(self.input_ids) def __len__(self): return self.num_samples def __getitem__(self, idx): return { "input_ids": self.input_ids[idx], "attention_mask": self.attention_mask[idx], "labels": self.input_ids[idx], } def fsdp_main(rank, world_size, args): # initialize the process group # initialize the process group dist.init_process_group("nccl") MOE_MANAGER.setup(parallel=None) dp_size = dist.get_world_size() dataset = RandomDataset( max_length=args.seq_length, num_samples=args.batch_size * (args.warmup + args.active) * dp_size, ) sampler = DistributedSampler(dataset, rank=rank, num_replicas=world_size, shuffle=False) train_kwargs = {"batch_size": args.batch_size, "sampler": sampler} train_loader = torch.utils.data.DataLoader(dataset, **train_kwargs) torch.cuda.set_device(rank) config = LlamaConfig.from_pretrained("hpcai-tech/openmoe-%s" % args.model_name) set_openmoe_args( config, num_experts=config.num_experts, moe_layer_interval=config.moe_layer_interval, enable_load_balance=False, enable_kernel=False, enable_comm_overlap=False, ) torch.set_default_dtype(torch.float16) model = OpenMoeForCausalLM(config) torch.set_default_dtype(torch.float32) auto_wrap_policy = functools.partial( transformer_auto_wrap_policy, transformer_layer_cls={ OpenMoeDecoderLayer, }, ) model = FSDP( model, mixed_precision=MixedPrecision( param_dtype=torch.bfloat16, reduce_dtype=torch.bfloat16, buffer_dtype=torch.bfloat16, ), auto_wrap_policy=auto_wrap_policy, device_id=torch.cuda.current_device(), ) optimizer = torch.optim.Adam(model.parameters(), weight_decay=0.01, lr=1e-5) model.train() model_numel = get_model_numel(model) performance_evaluator = PerformanceEvaluator( model_numel, enable_grad_checkpoint=True, ignore_steps=args.warmup, dp_world_size=dist.get_world_size(), ) for step, data in tqdm.tqdm(enumerate(train_loader), total=len(train_loader)): performance_evaluator.on_step_start(step) input_ids, attention_mask, labels = ( data["input_ids"].cuda(), data["attention_mask"].cuda(), data["labels"].cuda(), ) optimizer.zero_grad() output = model( input_ids=input_ids, labels=labels, attention_mask=attention_mask, chunk_head=False, ) loss = output["loss"] loss.backward() optimizer.step() performance_evaluator.on_step_end(input_ids) performance_evaluator.on_fit_end() if dist.get_rank() == 0: print(f"Max CUDA memory usage: {torch.cuda.max_memory_allocated()/1024**2:.2f} MB") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--model_name", type=str, default="base", choices=["base", "8b"], help="base or 8b", ) parser.add_argument("--batch_size", type=int, default=1) parser.add_argument("--seq_length", type=int, default=2048) parser.add_argument("--warmup", type=int, default=20) parser.add_argument("--active", type=int, default=20) args = parser.parse_args() torch.manual_seed(42) world_size = int(os.environ["WORLD_SIZE"]) local_rank = int(os.environ["LOCAL_RANK"]) fsdp_main(local_rank, world_size, args)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/benchmark/utils.py
colossalai/legacy/moe/openmoe/benchmark/utils.py
from time import time from typing import Optional import torch import torch.distributed as dist import torch.nn as nn from torch import Tensor from colossalai.logging import DistributedLogger def print_model_numel(logger: DistributedLogger, model: nn.Module) -> None: B = 1024**3 M = 1024**2 K = 1024 outputs = "Model param count: " model_param = sum(p.numel() for p in model.parameters() if p.requires_grad) if model_param >= B: outputs += f"{model_param / B:.2f} B\n" elif model_param >= M: outputs += f"{model_param / M:.2f} M\n" elif model_param >= K: outputs += f"{model_param / K:.2f} K\n" else: outputs += f"{model_param}\n" logger.info(outputs, ranks=[0]) def get_model_numel(model: nn.Module) -> None: model_param = sum(p.numel() for p in model.parameters() if p.requires_grad) return model_param def divide(x: float, y: float) -> float: if y == 0: return float("inf") elif y == float("inf"): return float("nan") return x / y @torch.no_grad() def all_reduce_mean(x: float, world_size: int) -> float: if world_size == 1: return x tensor = torch.tensor([x], device=torch.cuda.current_device()) dist.all_reduce(tensor) tensor = tensor / world_size return tensor.item() class Timer: def __init__(self) -> None: self.start_time: Optional[float] = None self.duration: float = 0.0 def start(self) -> None: self.start_time = time() def end(self) -> None: assert self.start_time is not None self.duration += time() - self.start_time self.start_time = None def reset(self) -> None: self.duration = 0.0 class PerformanceEvaluator: """ Callback for valuate the performance of the model. Args: actor_num_params: The number of parameters of the actor model. critic_num_params: The number of parameters of the critic model. initial_model_num_params: The number of parameters of the initial model. reward_model_num_params: The number of parameters of the reward model. enable_grad_checkpoint: Whether to enable gradient checkpointing. ignore_episodes: The number of episodes to ignore when calculating the performance. """ def __init__( self, model_numel: int, enable_grad_checkpoint: bool = False, ignore_steps: int = 0, dp_world_size: Optional[int] = None, ) -> None: self.model_numel = model_numel self.enable_grad_checkpoint = enable_grad_checkpoint self.ignore_steps = ignore_steps self.dp_world_size = dp_world_size self.world_size = dist.get_world_size() self.disable: bool = False self.timer = Timer() self.num_samples: int = 0 self.flop: int = 0 def on_step_start(self, step: int) -> None: self.disable = self.ignore_steps > 0 and step < self.ignore_steps if self.disable: return torch.cuda.synchronize() self.timer.start() def on_step_end(self, input_ids: Tensor, **kwargs) -> None: if self.disable: return torch.cuda.synchronize() self.timer.end() batch_size, seq_len = input_ids.shape self.num_samples += batch_size self.flop += batch_size * seq_len * self.model_numel * 2 * (3 + int(self.enable_grad_checkpoint)) def on_fit_end(self) -> None: avg_duration = all_reduce_mean(self.timer.duration, self.world_size) avg_throughput = self.num_samples * self.dp_world_size / (avg_duration + 1e-12) mp_world_size = self.world_size // self.dp_world_size avg_tflops_per_gpu = self.flop / 1e12 / (avg_duration + 1e-12) / mp_world_size if dist.get_rank() == 0: print( f"num_samples: {self.num_samples}, dp_world_size: {self.dp_world_size}, flop: {self.flop}, avg_duration: {avg_duration}, " f"avg_throughput: {avg_throughput}" ) print(f"Throughput: {avg_throughput:.2f} samples/sec, TFLOPS per GPU: {avg_tflops_per_gpu:.2f}")
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/openmoe/benchmark/benchmark_cai.py
colossalai/legacy/moe/openmoe/benchmark/benchmark_cai.py
import argparse import json import os import torch import torch.distributed as dist from huggingface_hub import snapshot_download from model.modeling_openmoe import OpenMoeForCausalLM, set_openmoe_args from model.openmoe_policy import OpenMoeForCausalLMPolicy from torch.utils.data import Dataset from tqdm import tqdm from transformers import T5Tokenizer from transformers.models.llama import LlamaConfig from utils import PerformanceEvaluator, get_model_numel import colossalai from colossalai.accelerator import get_accelerator from colossalai.booster import Booster from colossalai.booster.plugin.moe_hybrid_parallel_plugin import MoeHybridParallelPlugin from colossalai.cluster import DistCoordinator from colossalai.legacy.moe.manager import MOE_MANAGER from colossalai.legacy.moe.utils import skip_init from colossalai.moe.layers import apply_load_balance from colossalai.nn.optimizer import HybridAdam def move_to_cuda(batch, device): return {k: v.to(device) for k, v in batch.items()} def load_ckpt(repo_name: str, model: OpenMoeForCausalLM, booster: Booster): ckpt_path = snapshot_download(repo_name) # single ckpt if os.path.exists(os.path.join(ckpt_path, "pytorch_model.bin")): ckpt_path = os.path.join(ckpt_path, "pytorch_model.bin") # shard ckpt elif os.path.exists(os.path.join(ckpt_path, "pytorch_model.bin.index.json")): ckpt_path = os.path.join(ckpt_path, "pytorch_model.bin.index.json") else: raise ValueError(f"Invalid checkpoint path: {ckpt_path}") booster.load_model(model, ckpt_path) class RandomDataset(Dataset): def __init__( self, num_samples: int = 1000, max_length: int = 2048, vocab_size: int = 256384, tokenizer: T5Tokenizer = None ): self.num_samples = num_samples self.max_length = max_length if os.path.exists("./mock_data.json"): self.input_ids = [] self.attention_mask = [] with open("./mock_data.json", "r") as f: data = json.load(f) for v in data.values(): d = v["text"] encode = tokenizer( "<pad>" + d, return_tensors="pt", add_special_tokens=False, max_length=max_length, truncation=True, padding="max_length", ) self.input_ids.append(encode["input_ids"]) self.attention_mask.append(encode["attention_mask"]) self.input_ids = torch.cat(self.input_ids, dim=0).to(get_accelerator().get_current_device()) self.attention_mask = torch.cat(self.attention_mask, dim=0).to(get_accelerator().get_current_device()) repeat_times = num_samples // self.input_ids.shape[0] + 1 self.input_ids = self.input_ids.repeat(repeat_times, 1)[:num_samples] self.attention_mask = self.attention_mask.repeat(repeat_times, 1)[:num_samples] else: self.input_ids = torch.randint( 0, vocab_size, (num_samples, max_length), device=get_accelerator().get_current_device() ) self.attention_mask = torch.ones_like(self.input_ids) def __len__(self): return self.num_samples def __getitem__(self, idx): return { "input_ids": self.input_ids[idx], "attention_mask": self.attention_mask[idx], "labels": self.input_ids[idx], } def parse_args(): # basic settings parser = argparse.ArgumentParser() parser.add_argument( "--model_name", type=str, default="base", choices=["base", "8b"], help="Path to pretrained model or model identifier from huggingface.co/models.", ) parser.add_argument( "--batch_size", type=int, default=4, help="Batch size (per dp group) for the training dataloader.", ) parser.add_argument( "--seq_length", type=int, default=2048, help="sequence length for the training dataloader.", ) parser.add_argument("--seed", type=int, default=42, help="A seed for reproducible training.") parser.add_argument( "--plugin", type=str, default="hybrid", help="parallel plugin", ) # hybrid plugin parser.add_argument("--pp_size", type=int, default=2, help="pp size") parser.add_argument("--dp_size", type=int, default=1, help="dp size") parser.add_argument("--ep_size", type=int, default=2, help="ep size") parser.add_argument("--zero_stage", type=int, default=2, help="zero stage in hybrid plugin") parser.add_argument("--microbatch_size", type=int, default=1, help="microbatch size") parser.add_argument("--extra_dp_size", type=int, default=1) # kernel parser.add_argument( "--use_kernel", action="store_true", help="Use kernel optim. Need to install flash attention, apex, triton to enable all kernel optimizations.", ) # bench parser.add_argument("--warmup", type=int, default=20) parser.add_argument("--active", type=int, default=20) # load balance parser.add_argument("--load_balance", action="store_true") # overlap communication parser.add_argument("--overlap_comm", action="store_true") # hierarchical all-to-all parser.add_argument("--hierarchical_alltoall", action="store_true") args = parser.parse_args() return args def main(): args = parse_args() # Launch ColossalAI colossalai.launch_from_torch(seed=args.seed) coordinator = DistCoordinator() # Set plugin booster_kwargs = {} hybrid_dict = { "tp_size": 1, "custom_policy": OpenMoeForCausalLMPolicy(), "enable_fused_normalization": args.use_kernel, "enable_jit_fused": args.use_kernel, "precision": "bf16", "zero_stage": args.zero_stage, } mgr_dict = {} if args.plugin == "ep": dp_size = dist.get_world_size() plugin = MoeHybridParallelPlugin( pp_size=1, **hybrid_dict, ) MOE_MANAGER.setup( parallel="EP", max_ep_size=dp_size, **mgr_dict, ) elif args.plugin == "ep_zero": dp_size = dist.get_world_size() use_ep_inside = False plugin = MoeHybridParallelPlugin( pp_size=1, ep_size=args.ep_size, use_ep_inside=use_ep_inside, **hybrid_dict, ) MOE_MANAGER.setup( parallel="EP", max_ep_size=dp_size // args.extra_dp_size, use_ep_inside=use_ep_inside, **mgr_dict, ) elif args.plugin == "hybrid": dp_size = dist.get_world_size() // args.pp_size plugin = MoeHybridParallelPlugin( pp_size=args.pp_size, zero_stage=args.zero_stage, microbatch_size=args.microbatch_size, **hybrid_dict, ) MOE_MANAGER.setup( parallel="EP", mode="fixed", fixed_dp_size=args.dp_size, fixed_ep_size=args.ep_size, fixed_pp_size=args.pp_size, **mgr_dict, ) else: raise ValueError(f"Invalid plugin {args.plugin}") coordinator.print_on_master(f"Set plugin as {plugin}") # Build OpenMoe model repo_name = "hpcai-tech/openmoe-" + args.model_name config = LlamaConfig.from_pretrained(repo_name) set_openmoe_args( config, num_experts=config.num_experts, moe_layer_interval=config.moe_layer_interval, enable_load_balance=args.load_balance, enable_kernel=args.use_kernel, enable_comm_overlap=args.overlap_comm, enable_hierarchical_alltoall=args.hierarchical_alltoall, ) with skip_init(): model = OpenMoeForCausalLM(config) coordinator.print_on_master(f"Finish init model with config:\n{config}") # Enable gradient checkpointing model.gradient_checkpointing_enable() # Prepare tokenizer and dataloader tokenizer = T5Tokenizer.from_pretrained("google/umt5-small") dataset = RandomDataset( num_samples=args.batch_size * (args.warmup + args.active + 1) * dp_size, max_length=args.seq_length, tokenizer=tokenizer, ) dataloader = plugin.prepare_dataloader(dataset, batch_size=args.batch_size) # Set optimizer optimizer = HybridAdam(model.parameters(), weight_decay=0.01, lr=1e-5) model_numel = get_model_numel(model) performance_evaluator = PerformanceEvaluator( model_numel, enable_grad_checkpoint=True, ignore_steps=args.warmup, dp_world_size=dp_size, ) # Set booster booster = Booster(plugin=plugin, **booster_kwargs) load_ckpt(repo_name, model, booster) model, optimizer, _, dataloader, _ = booster.boost(model=model, optimizer=optimizer, dataloader=dataloader) use_pipeline = isinstance(booster.plugin, MoeHybridParallelPlugin) and booster.plugin.pp_size > 1 is_pp_last_stage = use_pipeline and booster.plugin.stage_manager.is_last_stage() coordinator.print_on_master(f"Finish init booster") # Start finetuning coordinator.print_on_master(f"Start training") model.train() train_dataloader_iter = iter(dataloader) total_len = len(train_dataloader_iter) - 1 exmaple_data = next(train_dataloader_iter) with tqdm(range(total_len), disable=not coordinator.is_master()) as pbar: for step in pbar: performance_evaluator.on_step_start(step) if use_pipeline: # Forward pass outputs = booster.execute_pipeline( train_dataloader_iter, model, lambda x, y: x.loss, optimizer, return_loss=True, ) # Backward and optimize if is_pp_last_stage: loss = outputs["loss"] pbar.set_postfix({"loss": loss.item()}) else: # Forward pass data = next(train_dataloader_iter) data = move_to_cuda(data, torch.cuda.current_device()) outputs = model(**data) loss = outputs["loss"] # Backward booster.backward(loss, optimizer) pbar.set_postfix({"loss": loss.item()}) optimizer.step() optimizer.zero_grad() performance_evaluator.on_step_end(exmaple_data["input_ids"]) if (step == args.warmup // 2) and args.load_balance: coordinator.print_on_master(f"Apply load balance") apply_load_balance(model, optimizer) performance_evaluator.on_fit_end() if __name__ == "__main__": main()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/layer/experts.py
colossalai/legacy/moe/layer/experts.py
import math from typing import Callable, Optional, Tuple import torch import torch.nn as nn from colossalai.kernel.triton.llama_act_combine_kernel import HAS_TRITON from colossalai.legacy.moe.manager import MOE_MANAGER from colossalai.legacy.moe.utils import get_activation from colossalai.moe._operation import EPGradScalerIn, EPGradScalerOut from colossalai.shardformer.layer.utils import Randomizer from colossalai.tensor.moe_tensor.api import get_ep_rank, get_ep_size if HAS_TRITON: from colossalai.kernel.triton.llama_act_combine_kernel import LlamaActCombine class MLPExperts(nn.Module): """ SparseMLP is a multi-layer perceptron with sparse expert parallel layers. Args: num_experts (int): The number of experts hidden_size (int): The hidden size of MLP intermediate_size (int): The intermediate size of MLP expert_parallel (str, optional): The parallelism of experts. Now we have None, EP and TP. activation (optional): The activation function of MLP drop_rate (float, optional): The drop rate of MLP gated (bool, optional): Whether to use gated MLP use_kernel (bool, optional): Whether to use kernel optimization """ def __init__( self, num_experts: int, hidden_size: int, intermediate_size: int, expert_parallel: Optional[str] = "EP", activation: Optional[Callable] = None, drop_rate: Optional[float] = 0, gated: Optional[bool] = False, use_kernel: Optional[bool] = False, ): super().__init__() assert expert_parallel in ["EP", "TP", None] self.expert_parallel = expert_parallel self.num_total_experts = num_experts self.gated = gated self.use_kernel = use_kernel self.hidden_size = hidden_size self.intermediate_size = intermediate_size # get expert parallel info if expert_parallel is not None: self.num_local_experts, self.moe_info = MOE_MANAGER.get_info( num_experts, use_tp=True if expert_parallel == "TP" else False ) # get settings for different parallel self.ep_size = get_ep_size(self) if expert_parallel == "TP": intermediate_size = intermediate_size // self.ep_size num_experts = self.num_total_experts else: num_experts = self.num_local_experts else: self.num_local_experts = self.num_total_experts self.ep_size = 1 if gated: self.wi_gate = nn.Parameter( torch.empty( num_experts, hidden_size, intermediate_size * 2 if activation == "swiglu" else intermediate_size ) ) self.wi_up = nn.Parameter(torch.empty(num_experts, hidden_size, intermediate_size)) else: self.wi = nn.Parameter(torch.empty(num_experts, hidden_size, intermediate_size)) self.wo = nn.Parameter(torch.empty(num_experts, intermediate_size, hidden_size)) self.act_name = activation self.act = get_activation(activation) self.drop = nn.Dropout(p=drop_rate) if expert_parallel is not None: for param in self.parameters(): set_moe_tensor_info(param, self.moe_info) # init param self.reset_parameters() @torch.no_grad() def reset_parameters(self): # expert param should be different if self.expert_parallel is not None: seed_ctx = Randomizer(get_ep_rank(self)).fork_rng(enable_cpu=True) else: seed_ctx = Randomizer(42).fork_rng(enable_cpu=True) with seed_ctx: if self.gated: torch.nn.init.normal_(self.wi_gate, std=math.sqrt(0.1 / self.hidden_size)) torch.nn.init.normal_(self.wi_up, std=math.sqrt(0.1 / self.hidden_size)) else: torch.nn.init.normal_(self.wi, std=math.sqrt(0.1 / self.hidden_size)) torch.nn.init.normal_(self.wo, std=math.sqrt(0.1 / self.intermediate_size)) def forward( self, x: torch.Tensor, param_slice: Tuple[slice] = (slice(None),), use_sparse: bool = True, ) -> torch.Tensor: """ forward: hidden_size --> intermediate_size --> hidden_size Args: x (torch.Tensor): The input tensor of shape (num_groups, num_experts, capacity, hidden_size) Returns: torch.Tensor: The output tensor of shape (num_groups, num_experts, capacity, hidden_size) """ x = EPGradScalerIn.apply(x, self.ep_size) e = x.size(1) h = x.size(-1) x = x.transpose(0, 1) inshape = x.shape x = x.reshape(e, -1, h) if self.use_kernel and use_sparse: seq_len = x.shape[1] with torch.no_grad(): mask = x[:, :, 0] != 0.0 mask = torch.sum(mask, dim=-1) x_list = [] for i in range(e): x_list.append(x[i, : mask[i]]) x = x_list if self.gated: x_gate = [torch.mm(x[i], self.wi_gate[param_slice][i]) for i in range(e)] x_up = [torch.mm(x[i], self.wi_up[param_slice][i]) for i in range(e)] if self.use_kernel and HAS_TRITON and self.act_name == "swiglu": x = [LlamaActCombine.apply(x_gate[i], x_up[i]) for i in range(e)] else: x = [self.act(x_gate[i]) * x_up[i] for i in range(e)] else: x = [torch.mm(x[i], self.wi[param_slice][i]) for i in range(e)] x = [self.act(x[i]) for i in range(e)] x = [self.drop(x[i]) for i in range(e)] x = [torch.mm(x[i], self.wo[param_slice][i]) for i in range(e)] if self.use_kernel and use_sparse: for i in range(e): x[i] = torch.nn.functional.pad(x[i], (0, 0, 0, seq_len - x[i].shape[0]), mode="constant", value=0) x = torch.cat([x[i].unsqueeze(0) for i in range(e)], dim=0) x = x.reshape(inshape) x = x.transpose(0, 1).contiguous() x = EPGradScalerOut.apply(x, self.ep_size) return x
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/layer/__init__.py
colossalai/legacy/moe/layer/__init__.py
from .experts import * from .layers import * from .routers import *
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/layer/routers.py
colossalai/legacy/moe/layer/routers.py
import math from typing import Callable, Optional, Tuple import torch import torch.nn as nn from colossalai.kernel.triton.llama_act_combine_kernel import HAS_TRITON from colossalai.legacy.moe.manager import MOE_MANAGER from colossalai.legacy.moe.utils import get_activation from colossalai.moe._operation import EPGradScalerIn, EPGradScalerOut from colossalai.shardformer.layer.utils import Randomizer from colossalai.tensor.moe_tensor.api import get_ep_rank, get_ep_size if HAS_TRITON: from colossalai.kernel.triton.llama_act_combine_kernel import LlamaActCombine class MLPExperts(nn.Module): """ SparseMLP is a multi-layer perceptron with sparse expert parallel layers. Args: num_experts (int): The number of experts hidden_size (int): The hidden size of MLP intermediate_size (int): The intermediate size of MLP expert_parallel (str, optional): The parallelism of experts. Now we have None, EP and TP. activation (optional): The activation function of MLP drop_rate (float, optional): The drop rate of MLP gated (bool, optional): Whether to use gated MLP use_kernel (bool, optional): Whether to use kernel optimization """ def __init__( self, num_experts: int, hidden_size: int, intermediate_size: int, expert_parallel: Optional[str] = "EP", activation: Optional[Callable] = None, drop_rate: Optional[float] = 0, gated: Optional[bool] = False, use_kernel: Optional[bool] = False, ): super().__init__() assert expert_parallel in ["EP", "TP", None] self.expert_parallel = expert_parallel self.num_total_experts = num_experts self.gated = gated self.use_kernel = use_kernel self.hidden_size = hidden_size self.intermediate_size = intermediate_size # get expert parallel info if expert_parallel is not None: self.num_local_experts, self.moe_info = MOE_MANAGER.get_info( num_experts, use_tp=True if expert_parallel == "TP" else False ) # get settings for different parallel self.ep_size = get_ep_size(self) if expert_parallel == "TP": intermediate_size = intermediate_size // self.ep_size num_experts = self.num_total_experts else: num_experts = self.num_local_experts else: self.num_local_experts = self.num_total_experts self.ep_size = 1 if gated: self.wi_gate = nn.Parameter( torch.empty( num_experts, hidden_size, intermediate_size * 2 if activation == "swiglu" else intermediate_size ) ) self.wi_up = nn.Parameter(torch.empty(num_experts, hidden_size, intermediate_size)) else: self.wi = nn.Parameter(torch.empty(num_experts, hidden_size, intermediate_size)) self.wo = nn.Parameter(torch.empty(num_experts, intermediate_size, hidden_size)) self.act_name = activation self.act = get_activation(activation) self.drop = nn.Dropout(p=drop_rate) if expert_parallel is not None: for param in self.parameters(): set_moe_tensor_info(param, self.moe_info) # init param self.reset_parameters() @torch.no_grad() def reset_parameters(self): # expert param should be different if self.expert_parallel is not None: seed_ctx = Randomizer(get_ep_rank(self)).fork_rng(enable_cpu=True) else: seed_ctx = Randomizer(42).fork_rng(enable_cpu=True) with seed_ctx: if self.gated: torch.nn.init.normal_(self.wi_gate, std=math.sqrt(0.1 / self.hidden_size)) torch.nn.init.normal_(self.wi_up, std=math.sqrt(0.1 / self.hidden_size)) else: torch.nn.init.normal_(self.wi, std=math.sqrt(0.1 / self.hidden_size)) torch.nn.init.normal_(self.wo, std=math.sqrt(0.1 / self.intermediate_size)) def forward( self, x: torch.Tensor, param_slice: Tuple[slice] = (slice(None),), use_sparse: bool = True, ) -> torch.Tensor: """ forward: hidden_size --> intermediate_size --> hidden_size Args: x (torch.Tensor): The input tensor of shape (num_groups, num_experts, capacity, hidden_size) Returns: torch.Tensor: The output tensor of shape (num_groups, num_experts, capacity, hidden_size) """ x = EPGradScalerIn.apply(x, self.ep_size) e = x.size(1) h = x.size(-1) x = x.transpose(0, 1) inshape = x.shape x = x.reshape(e, -1, h) if self.use_kernel and use_sparse: seq_len = x.shape[1] with torch.no_grad(): mask = x[:, :, 0] != 0.0 mask = torch.sum(mask, dim=-1) x_list = [] for i in range(e): x_list.append(x[i, : mask[i]]) x = x_list if self.gated: x_gate = [torch.mm(x[i], self.wi_gate[param_slice][i]) for i in range(e)] x_up = [torch.mm(x[i], self.wi_up[param_slice][i]) for i in range(e)] if self.use_kernel and HAS_TRITON and self.act_name == "swiglu": x = [LlamaActCombine.apply(x_gate[i], x_up[i]) for i in range(e)] else: x = [self.act(x_gate[i]) * x_up[i] for i in range(e)] else: x = [torch.mm(x[i], self.wi[param_slice][i]) for i in range(e)] x = [self.act(x[i]) for i in range(e)] x = [self.drop(x[i]) for i in range(e)] x = [torch.mm(x[i], self.wo[param_slice][i]) for i in range(e)] if self.use_kernel and use_sparse: for i in range(e): x[i] = torch.nn.functional.pad(x[i], (0, 0, 0, seq_len - x[i].shape[0]), mode="constant", value=0) x = torch.cat([x[i].unsqueeze(0) for i in range(e)], dim=0) x = x.reshape(inshape) x = x.transpose(0, 1).contiguous() x = EPGradScalerOut.apply(x, self.ep_size) return x
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/moe/layer/layers.py
colossalai/legacy/moe/layer/layers.py
import dataclasses import math from typing import Any, Optional, Tuple import torch import torch.distributed as dist import torch.nn as nn import torch.nn.functional as F from colossalai.legacy.moe.load_balance import LoadBalancer from colossalai.legacy.moe.utils import create_ep_hierarchical_group, get_noise_generator from colossalai.moe._operation import AllGather, AllToAll, HierarchicalAllToAll, MoeCombine, MoeDispatch, ReduceScatter from colossalai.shardformer.layer.moe import MLPExperts from colossalai.tensor.moe_tensor.api import get_dp_group, get_ep_group, get_ep_group_ranks, get_ep_size class SparseMLP(nn.Module): """A class for users to create MoE modules in their models. Args: dim_model (int): Hidden dimension of training model num_experts (int): The number experts top_k (int, optional): The number of experts for dispatchment of each token parallel (str): parallel mode. Should be "EP", "TP" or None capacity_factor_train (float, optional): Capacity factor in routing during training capacity_factor_eval (float, optional): Capacity factor in routing during evaluation min_capacity (int, optional): The minimum number of the capacity of each expert noisy_policy (str, optional): The policy of noisy function. Now we have 'Jitter' and 'Gaussian'. 'Jitter' can be found in `Switch Transformer paper`_. 'Gaussian' can be found in `ViT-MoE paper`_. drop_tks (bool, optional): Whether drops tokens in evaluation use_residual (bool, optional): Makes this MoE layer a Residual MoE. More information can be found in `Microsoft paper`_. residual_instance (nn.Module, optional): The instance of residual module in Residual MoE expert_instance (MoeExperts, optional): The instance of experts module in MoeLayer expert_cls (Type[nn.Module], optional): The class of each expert when no instance is given expert_args (optional): The args of expert when no instance is given .. _Switch Transformer paper: https://arxiv.org/abs/2101.03961 .. _ViT-MoE paper: https://arxiv.org/abs/2106.05974 .. _Microsoft paper: https://arxiv.org/abs/2201.05596 """ def __init__( self, num_experts: int, hidden_size: int, intermediate_size: int, router_top_k: int = 1, parallel: str = "EP", router_loss: bool = True, router_norm: bool = False, router_capacity_factor_train: float = 1.25, router_capacity_factor_eval: float = 2.0, router_min_capacity: int = 4, router_noisy_policy: Optional[str] = None, router_drop_tks: bool = True, mlp_activation: Optional[str] = None, mlp_gated: bool = False, enable_load_balance: bool = False, load_balance_tolerance: float = 0.1, load_balance_beam_width: int = 8, load_balance_group_swap_factor: float = 0.4, enable_kernel: bool = False, enable_comm_overlap: bool = False, enable_hierarchical_comm: bool = True, return_gate_logits: bool = False, ): super().__init__() self.hidden_size = hidden_size self.intermediate_size = intermediate_size self.num_experts = num_experts self.gated = mlp_gated self.return_gate_logits = return_gate_logits self.enable_kernel = enable_kernel self.enable_comm_overlap = enable_comm_overlap # self.expert_parallel = MOE_MANAGER.get_parallel() assert parallel in ["EP", "TP", None], "parallel mode must be EP, TP or None" self.parallel = parallel self.router_loss = router_loss self.router_norm = router_norm # moe router noisy_func = get_noise_generator(router_noisy_policy, num_experts) router_cls = get_router_cls(router_top_k) self.topk = router_top_k self.router: MoeRouter = router_cls( capacity_factor_train=router_capacity_factor_train, capacity_factor_eval=router_capacity_factor_eval, min_capacity=router_min_capacity, noisy_func=noisy_func, drop_tks=router_drop_tks, ) # gate self.gate_weight = torch.nn.Parameter(torch.empty(num_experts, self.hidden_size)) # moe experts self.experts = MLPExperts( num_experts=self.num_experts, expert_parallel=self.parallel, hidden_size=self.hidden_size, intermediate_size=self.intermediate_size, activation=mlp_activation, gated=mlp_gated, use_kernel=self.enable_kernel, ) # get parallel settings if self.parallel is not None: self.ep_group = get_ep_group(self.experts) self.ep_size = get_ep_size(self.experts) self.ep_hierarchical_group = None if enable_hierarchical_comm: # TODO: move to plugin self.ep_intra_src_rank, *self.ep_hierarchical_group = create_ep_hierarchical_group( get_ep_group_ranks(self.experts) ) self.dp_group = get_dp_group(self.experts) else: self.ep_group = None self.dp_group = None self.num_local_experts = self.experts.num_local_experts # load balance self.enable_load_balance = enable_load_balance if self.enable_load_balance == True: self.load_balancer = LoadBalancer( experts=self.experts, gate=self.gate_weight, local_expert_num=self.num_local_experts, expert_num=self.num_experts, ep_group=self.ep_group, dp_group=self.dp_group, tolerance=load_balance_tolerance, beam_width=load_balance_beam_width, group_swap_factor=load_balance_group_swap_factor, ) # init param self.reset_parameters() @torch.no_grad() def reset_parameters(self): torch.nn.init.normal_(self.gate_weight, std=math.sqrt(0.1 / self.hidden_size)) def forward(self, inputs: torch.Tensor) -> torch.Tensor: """ Args: inputs (torch.Tensor): The input tensor of shape (batch_size, seq_len, hidden_size) Returns: torch.Tensor: The output tensor of shape (batch_size, seq_len, hidden_size) """ # reshape the input tokens tokens = inputs.reshape(-1, self.hidden_size) # the data type of the inputs in the gating should be fp32 gate_logits = F.linear(tokens, self.gate_weight) gate_output = gate_logits.to(torch.float) # update expert load if self.enable_load_balance == True: with torch.no_grad(): # TODO: optimize computation expert_load = torch.topk(gate_output, k=self.topk, dim=-1)[1] # TODO: bincount introduces synchronize, fix it expert_load = torch.bincount(expert_load.view(-1)) self.load_balancer.update_load(expert_load) # the result from the router used_capacity, *route_result_list = self.router( inputs=gate_output, use_kernel=self.enable_kernel, ep_group=self.ep_group, use_loss=self.router_loss, use_norm=self.router_norm, ) # dispatch_data: (num_experts, capacity, hidden_size) if self.enable_kernel: dispatch_data = MoeDispatch.apply(tokens, *route_result_list[1:]) dispatch_data = dispatch_data.reshape(self.num_experts, -1, self.hidden_size) else: sec_mask_f = route_result_list[1].type_as(inputs) dispatch_data = torch.matmul(sec_mask_f.permute(1, 2, 0), tokens) # expert_output: (num_groups, num_experts, capacity, hidden_size) if self.parallel == "EP": expert_output = self._ep_process(dispatch_data, used_capacity, overlap=self.enable_comm_overlap) elif self.parallel == "TP": expert_output = self._tp_process(dispatch_data, used_capacity, overlap=self.enable_comm_overlap) elif self.parallel is None: expert_output = self._local_process(dispatch_data) else: raise NotImplementedError( "This kind of communication has not been implemented yet.\n" "Please use Experts build function." ) if self.enable_kernel: expert_output = expert_output.reshape(-1, self.hidden_size) ans = MoeCombine.apply(expert_output, *route_result_list) else: combine_weights = route_result_list[0].type_as(inputs) combine_weights = combine_weights.view(combine_weights.shape[0], -1) expert_output = expert_output.view(-1, expert_output.shape[-1]) ans = torch.matmul(combine_weights, expert_output) ans = ans.reshape(inputs.shape) if self.return_gate_logits: return ans, gate_logits else: return ans def _local_process(self, expert_in: torch.Tensor) -> torch.Tensor: expert_in = expert_in.unsqueeze(0) expert_out = self.experts(expert_in) return expert_out def _ep_process( self, dispatch_data: torch.Tensor, used_capacity: torch.Tensor, overlap: bool = False ) -> torch.Tensor: """ Expert Parallel Args: dispatch_data (torch.Tensor): (num_experts, capacity, hidden_size) Returns: torch.Tensor: (num_experts, capacity, hidden_size) """ if not overlap or dist.get_world_size(self.ep_group) == 1: if self.ep_hierarchical_group is not None: expert_input = HierarchicalAllToAll.apply( dispatch_data, self.ep_hierarchical_group, self.ep_intra_src_rank ) expert_input = expert_input.reshape(self.ep_size, self.num_local_experts, -1, self.hidden_size) expert_output = self.experts(expert_input) expert_output = HierarchicalAllToAll.apply( expert_output, self.ep_hierarchical_group, self.ep_intra_src_rank ) return expert_output else: expert_input = AllToAll.apply(dispatch_data, self.ep_group, False)[0] expert_input = expert_input.reshape(self.ep_size, self.num_local_experts, -1, self.hidden_size) expert_output = self.experts(expert_input) expert_output = AllToAll.apply(expert_output, self.ep_group, False)[0] return expert_output else: @dataclasses.dataclass class Capsule: data: torch.Tensor handle: Any = None NUM_CHUNK = 4 NUM_STAGES = 4 assert dispatch_data.shape[1] % NUM_CHUNK == 0, "arbitrary chunk num is not supported yet" chunk_size = dispatch_data.shape[1] // NUM_CHUNK input_shape = (self.ep_size, self.num_local_experts, -1, self.hidden_size) dispatch_data = dispatch_data.reshape(*input_shape) chunk_data = torch.split(dispatch_data, chunk_size, dim=2) output = torch.empty_like(dispatch_data) offset = 0 _expert_in, expert_in, _expert_out, expert_out = None, None, None, None for i in range(NUM_CHUNK + NUM_STAGES - 1): if expert_out is not None: expert_out.handle.wait() output[:, :, offset : offset + chunk_size, :] = expert_out.data offset += chunk_size expert_out = None # all2all last output if _expert_out is not None: expert_out = Capsule( *AllToAll.apply(_expert_out.data, self.ep_group, True), ) _expert_out = None # all2all next input if 0 <= i < NUM_CHUNK: _expert_in = Capsule(*AllToAll.apply(chunk_data[i].contiguous(), self.ep_group, True)) # compute if expert_in is not None: expert_in.handle.wait() _expert_out = Capsule(data=self.experts(expert_in.data), handle=None) expert_in = None if _expert_in is not None: expert_in = _expert_in _expert_in = None return output def _tp_process( self, dispatch_data: torch.Tensor, used_capacity: torch.Tensor, overlap: bool = False ) -> torch.Tensor: """ without overlap: | C | | A | | R | with overlap: | C1 || C2 || C3 || C4 | | A1 || A2 | | R1 | A3 || R2 | A4 || R3 | | R4 | where C is computation, A is all gather, R is reduce scatter. Args: dispatch_data (torch.Tensor): (num_experts, capacity, hidden_size) Returns: torch.Tensor: (num_experts, capacity, hidden_size) """ if not overlap or dist.get_world_size(self.ep_group) == 1: expert_in = AllGather.apply(dispatch_data, self.ep_group, False)[0] expert_out = self.experts(expert_in) expert_out = ReduceScatter.apply(expert_out, self.ep_group, False)[0] return expert_out else: @dataclasses.dataclass class Capsule: data: torch.Tensor handle: Any indices: Tuple NUM_CHUNK = 4 NUM_STAGES = 4 assert ( dispatch_data.shape[0] % NUM_CHUNK == 0 ), "arbitrary chunk num is not supported yet, please use chunk num that can divide num_experts" chunk_size = dispatch_data.shape[0] // NUM_CHUNK chunk_data = torch.split(dispatch_data, chunk_size, dim=0) output = torch.empty_like(dispatch_data) def get_chunk_slice(idx: int, chunk_size: int) -> Tuple[slice]: return (slice(idx * chunk_size, (idx + 1) * chunk_size),) _expert_in, expert_in, _expert_out, expert_out = None, None, None, None for i in range(NUM_CHUNK + NUM_STAGES - 1): if expert_out is not None: expert_out.handle.wait() output[expert_out.indices] = expert_out.data expert_out = None # reduce scatter last output if _expert_out is not None: expert_out = Capsule( *ReduceScatter.apply(_expert_out.data, self.ep_group, True), indices=_expert_out.indices, ) _expert_out = None # all gather next input if 0 <= i < NUM_CHUNK: _expert_in = Capsule( *AllGather.apply(chunk_data[i].contiguous(), self.ep_group, True), indices=get_chunk_slice(i, chunk_size), ) # compute if expert_in is not None: expert_in.handle.wait() _expert_out = Capsule( self.experts(expert_in.data, expert_in.indices), handle=None, indices=expert_in.indices, ) expert_in = None if _expert_in is not None: expert_in = _expert_in _expert_in = None return output def apply_load_balance(model: nn.Module, optim: Any) -> None: """ apply load balance to every experts in the model """ def _apply_recursive(module: nn.Module): for _, sub_module in module.named_children(): if isinstance(sub_module, SparseMLP): if sub_module.enable_load_balance == True: sub_module.load_balancer.balance_load(optim) _apply_recursive(sub_module) torch.cuda.empty_cache() _apply_recursive(model) torch.cuda.empty_cache()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/pipeline_process_group.py
colossalai/legacy/pipeline/pipeline_process_group.py
import threading from typing import List import torch.distributed as dist from torch.distributed import rpc from colossalai.legacy.tensor import ProcessGroup class PipelineProcessGroup: # TODO : flexible API for DP size and TP size # In the future design mode, dp_degree and tp_degree should be removed def __init__(self) -> None: self.is_initialize = False def set_global_info( self, rank: int, world_size: int, dp_degree: int = 1, tp_degree: int = 1, num_worker_threads: int = 1, device: str = "cuda", ) -> None: device_mesh_size = dp_degree * tp_degree assert world_size % device_mesh_size == 0, "world_size must be the multiple of dp_degree * tp_degree !!!" self._num_worker_threads = num_worker_threads self._device_mesh_size = device_mesh_size self._rank = rank self._world_size = world_size self._dp_degree = dp_degree self._tp_degree = tp_degree self.device = device self._stage_num = world_size // device_mesh_size self._pp_rank = rank // device_mesh_size self._pp_ranks = [(rank % device_mesh_size) + i * device_mesh_size for i in range(self._stage_num)] self._local_stage_ranks = [(rank // device_mesh_size * device_mesh_size) + i for i in range(device_mesh_size)] # pp_ranks self._initialize_pp_process_group() # initialise tp dp process groups self._initialize_tp_dp_process_group() # status self._is_first_pp_rank = self._pp_rank == 0 self._is_last_pp_rank = self._pp_rank == self._stage_num - 1 self.is_initialize = True # lock self.initialise_lock = threading.Lock() self.chimera_lock = threading.Lock() def _initialize_process_group(self): stage_num = self.get_stage_num() if stage_num == 1: return device = self.device world_size = self.get_world_size() rank = self.get_global_rank() backend = "nccl" if device == "cuda" else "gloo" dist.init_process_group(backend, world_size=world_size, rank=rank, group_name="main_group") def _initialize_pp_process_group(self) -> None: rank = self.get_global_rank() world_size = self.get_world_size() # build rpc connection options = rpc.TensorPipeRpcBackendOptions(num_worker_threads=self._num_worker_threads) for pp_rank in self._pp_ranks: options.set_device_map(f"work{pp_rank}", {rank: pp_rank}) rpc.init_rpc(name=f"work{rank}", rank=rank, world_size=world_size, rpc_backend_options=options) def _initialize_tp_dp_process_group(self) -> None: rank = self.get_global_rank() local_stage_ranks = self.get_local_stage_global_ranks() dp_degree = self.get_dp_degree() tp_degree = self.get_tp_degree() self._tp_dp_process_group = ProcessGroup(rank, local_stage_ranks, tp_degree, dp_degree) def get_global_rank(self): return self._rank def get_world_size(self): return self._world_size def get_dp_degree(self) -> int: return self._dp_degree def get_tp_degree(self) -> int: return self._tp_degree def get_local_device_mesh_size(self) -> int: return self._device_mesh_size def get_device_mesh_num(self) -> int: pass def get_stage_num(self) -> int: return self._stage_num def is_first_stage(self) -> bool: return self._is_first_pp_rank def is_last_stage(self) -> bool: return self._is_last_pp_rank def check_pp_rank_valid(self, pp_rank: int) -> bool: return -1 < pp_rank < self._stage_num def get_local_pp_rank(self) -> int: return self._pp_rank def get_prev_pp_rank(self) -> int: prev_pp_rank = self._pp_rank - 1 if not self.check_pp_rank_valid(prev_pp_rank): assert ValueError(f"current rank's pp_rank: {self._pp_rank} doesn't have a previous stage!") return prev_pp_rank def get_next_pp_rank(self) -> int: next_pp_rank = self._pp_rank + 1 if not self.check_pp_rank_valid(next_pp_rank): assert ValueError(f"current rank's pp_rank: {self._pp_rank} doesn't have a next stage!") return next_pp_rank def get_local_stage_global_ranks(self) -> List[int]: return self._local_stage_ranks def local_dp_rank(self) -> int: return self._tp_dp_process_group.dp_local_rank() def local_tp_rank(self) -> int: return self._tp_dp_process_group.tp_local_rank() def get_pp_global_ranks(self) -> int: return self._pp_ranks def get_dp_global_ranks(self): pass def get_tp_global_ranks(self): pass def get_chimera_all_reduce_group(self, pp_rank: int): with self.chimera_lock: if not hasattr(self, "chimera_groups"): world_size = self.get_world_size() stage_num = self.get_stage_num() assert world_size % 2 == 0, "world_size must be even in chimera!" self.chimera_groups = {} for rank in range(world_size // 2): pair = [rank, world_size - 1 - rank] group = dist.new_group(pair) self.chimera_groups[pair[0]] = group self.chimera_groups[pair[1]] = group self.chimera_groups[pair[0] + stage_num] = group self.chimera_groups[pair[1] + stage_num] = group self.chimera_step_lock = threading.Lock() self.chimera_step_lock.acquire() return self.chimera_groups[pp_rank] ppg = PipelineProcessGroup()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/pipelinable.py
colossalai/legacy/pipeline/pipelinable.py
import torch from colossalai.legacy.context import ParallelMode from colossalai.legacy.core import global_context as gpc from colossalai.legacy.nn.layer.utils import CheckpointModule from colossalai.tensor import ColoParameter from colossalai.utils.model.utils import InsertPostInitMethodToModuleSubClasses from .layer_spec import LayerSpec from .utils import ( build_kwargs_for_module, call_module, customized_partition, exec_funcs_with_kwargs, partition_balanced, partition_uniform, ) class PipelinableContext(InsertPostInitMethodToModuleSubClasses): """ A context manager to split the model into pipeline stages. """ def __init__(self, policy: str = "balanced"): super().__init__() self._layer_spec_dict = {} self._root_children = None self._model = None self._layer_spec_list = [] self._func_dict = {} self._policy = policy @property def policy(self): return self._policy @policy.setter def policy(self, policy: str): self._policy = policy @property def layers_count(self): return len(self._layer_spec_list) @property def funcs_count(self): return len(self._func_dict) def _pre_context_exec(self): """ The Callback function when entering the context """ # reserve rng states self.cpu_rng_state = torch.get_rng_state() self.cuda_rng_state = torch.cuda.get_rng_state() def _post_context_exec(self): """ The callback function when exiting context. """ # reset rng states torch.set_rng_state(self.cpu_rng_state) torch.cuda.set_rng_state(self.cuda_rng_state) def _post_init_method(self, module: torch.nn.Module, *args, **kwargs): """ The function to call at the end of the constructor of each module. NOTE() The module may be passed to this function multiple times. """ # iterate over the positional arguments # to check if an argument is a torch Module # if found any torch Module, replace it with its layer spec # for storage purpose modified_args = [] for arg in args: if isinstance(arg, torch.nn.Module): # if nn.Module is an argument of a non-root module, then we should convert it to layer spec, which make sure the correct init method used in the real build. # if nn.Module is an argument of the root module, then we should just record the module instance itself, because those instance has been built outside of the context. if id(arg) in self._layer_spec_dict: arg = self._layer_spec_dict[id(arg)] modified_args.append(arg) # to the same for the keyword arguments modified_kwargs = {} for k, v in kwargs.items(): if isinstance(v, torch.nn.Module): v = self._layer_spec_dict[id(v)] # (lyl)TODO: analyze ColoTensor as well modified_kwargs[k] = v # keep track of the module children # as torch.nn.Module.__init__ is called from inner module to outer module, # the final value of self._model will be the outermost model # e.g. if the model is torchvision.models.resnet18, then the final value of self._model # will be the ``ResNet`` object. self._root_children = list(module.children()) self._model = module # store the children to keep the module hierarchy layer_spec = LayerSpec(module.__class__, *modified_args, **modified_kwargs) layer_spec.set_children(module.children()) # store the layer spec in this context module_id = id(module) self._layer_spec_dict[module_id] = layer_spec # convert all torch.nn.Parameter to colossalai.tensor.ColoParameter name_list = [] for name, param in module.named_parameters(): if isinstance(param, ColoParameter): continue name_list.append((name, param)) for name, param in name_list: if hasattr(module, name): delattr(module, name) setattr(module, name, ColoParameter.from_torch_tensor(tensor=param.data, requires_grad=param.requires_grad)) def to_layer_list(self, exec_seq=None): """ Create a layer spec list and func list with execution sequence given by user. If exec_seq is None, we will take the module initializing order as execution order. """ self._exec_seq = exec_seq if exec_seq is None: # if user do not provide the model executing sequence, we use the initialization order as the executing order. children_name = [] for child in self._root_children: layer_spec = self._layer_spec_dict[id(child)] if layer_spec.typename in ( torch.nn.modules.container.ModuleList, torch.nn.modules.container.Sequential, ): for child_in_container in layer_spec.children: self._layer_spec_list.append(self._layer_spec_dict[id(child_in_container)]) for name, module in self._model.named_modules(): if id(module) == id(child_in_container): children_name.append(name) break else: self._layer_spec_list.append(layer_spec) for name, module in self._model.named_modules(): if id(module) == id(child): children_name.append(name) break else: front_funcs_list = [] named_modules = dict(self._model.named_modules()) for index, element in enumerate(exec_seq): if isinstance(element, str): if element == "SPLIT_NODE": continue assert ( element in named_modules ), f"Found invalid module name {element}, please check if you spell the module name correctly." # get the layer spec based on the module ID module = named_modules[element] layer_spec = self._layer_spec_dict[id(module)] # check whether there are functions which should be executed before this module if len(front_funcs_list) != 0: func_key = (layer_spec, "front") if func_key not in self._func_dict: self._func_dict[func_key] = [] for f in front_funcs_list: self._func_dict[func_key].append(f) front_funcs_list = [] func_key = (layer_spec, "behind") self._layer_spec_list.append(layer_spec) elif isinstance(element, tuple) and element[1] == "front": front_funcs_list.append(element[0]) else: if func_key not in self._func_dict: self._func_dict[func_key] = [] if isinstance(element, tuple): self._func_dict[func_key].append(element[0]) else: self._func_dict[func_key].append(element) def partition(self, num_chunks, pipeline_size, rank): """ Partitioned model will be built respect to partition policy. The real module instance will be built in this method. """ if isinstance(self._policy, str): if self._policy == "uniform": parts = partition_uniform(len(self._layer_spec_list), pipeline_size, num_chunks)[rank] elif self._policy == "balanced": param_counts = [] for layer_spec in self._layer_spec_list: param_counts.append(layer_spec.count_params()) parts = partition_balanced(param_counts, pipeline_size, num_chunks)[rank] elif self._policy == "customized": assert ( self._exec_seq is not None ), f"An explicit exec_seq must be defined by user in customized policy mode." self.customized_parts = customized_partition(self._exec_seq) assert len(self.customized_parts) == gpc.get_world_size( ParallelMode.PIPELINE ), f"World size is {gpc.get_world_size(ParallelMode.PIPELINE)}, but the number of partitions is {len(self.customized_parts)}" parts = self.customized_parts[rank] else: raise ValueError("A string partition policy should be one of ['uniform', 'balanced', 'customized'].") elif isinstance(self._policy, dict): parts = self._policy[rank] else: raise ValueError("A partition policy should be either a string or a dictionary.") layers_to_build = [] for start, end in parts: layers_to_build += self._layer_spec_list[start:end] behind_func_dict_in_partition = {} front_func_dict_in_partition = {} module_list_in_partition = [] for layer in layers_to_build: module = layer.build() module_list_in_partition.append(module) if (layer, "front") in self._func_dict: front_func_dict_in_partition[id(module)] = self._func_dict[(layer, "front")] elif (layer, "behind") in self._func_dict: behind_func_dict_in_partition[id(module)] = self._func_dict[(layer, "behind")] module_list_in_partition = torch.nn.ModuleList(module_list_in_partition) pipeline_model = PipelinableModel( module_list_in_partition, front_func_dict_in_partition, behind_func_dict_in_partition ) return pipeline_model class PipelinableModel(torch.nn.Module): def __init__(self, module_list, front_func_dict, behind_func_dict): super().__init__() self._module_list = module_list self._front_func_dict = front_func_dict self._behind_func_dict = behind_func_dict def forward(self, *input_tensor, **kwargs): for module in self._module_list: if id(module) in self._front_func_dict: input_tensor = exec_funcs_with_kwargs(self._front_func_dict, id(module), input_tensor, kwargs) if isinstance(module, CheckpointModule): forward_func = module._forward else: forward_func = module.forward module_kwargs = build_kwargs_for_module(forward_func, input_tensor, kwargs) if input_tensor is None: input_tensor = call_module(module, kwargs=module_kwargs) elif isinstance(input_tensor, torch.Tensor): input_tensor = call_module(module, args=(input_tensor,), kwargs=module_kwargs) else: input_tensor = call_module(module, args=input_tensor, kwargs=module_kwargs) if id(module) in self._behind_func_dict: input_tensor = exec_funcs_with_kwargs(self._behind_func_dict, id(module), input_tensor, kwargs) return input_tensor
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/utils.py
colossalai/legacy/pipeline/utils.py
import heapq import inspect from collections import OrderedDict from typing import List import torch from colossalai.legacy.nn.layer.utils import CheckpointModule from colossalai.logging import get_dist_logger def _binary_partition(weights: List, start: int, end: int): """Returns the binary partition position of `weights`, given the start position `st` and the end position `ed`. Args: weights (list): A python list to be binary partitioned start (int): the start position of the binary partition end (int): the end position of the binary partition Returns: int: the binary partition position of `weights` """ w_sum = weights[end - 1] prefix = 0 if start > 0: w_sum -= weights[start - 1] prefix = weights[start - 1] minimum = float("inf") for idx in range(start + 1, end): front = weights[idx - 1] - prefix diff = abs(w_sum - 2 * front) if diff < minimum: pos = idx minimum = diff return start, pos, end def _heap_addition(weights: List, intervals: int, add_cnt: int): """ """ def _heap_push(heap, st, ed): value = weights[ed - 1] if st > 0: value -= weights[st - 1] heapq.heappush(heap, (-value, st, ed)) ret_intervals = [] heap = [] for st, ed in intervals: _heap_push(heap, st, ed) while add_cnt > 0: _, st, ed = heapq.heappop(heap) if ed - st == 1: ret_intervals.append((st, ed)) else: l, m, r = _binary_partition(weights, st, ed) _heap_push(heap, l, m) _heap_push(heap, m, r) add_cnt -= 1 while heap: _, st, ed = heapq.heappop(heap) ret_intervals.append((st, ed)) ret_intervals.sort() return ret_intervals def _calc_partitions(weights, value): prev = 0 prefix = 0 num_block = 0 intervals = [] for idx, w in enumerate(weights): if weights[idx] - prefix > value: intervals.append((prev, idx)) prev = idx prefix = weights[idx - 1] num_block += 1 intervals.append((prev, len(weights))) return num_block + 1, intervals def _binary_search(weights, num): length = len(weights) prefix = [1 if w == 0 else w for w in weights] for i in range(1, length): prefix[i] += prefix[i - 1] lower_bound = max(weights) upper_bound = prefix[length - 1] while upper_bound > lower_bound: mid = (upper_bound + lower_bound) // 2 number, _ = _calc_partitions(prefix, mid) if number <= num: upper_bound = mid else: lower_bound = mid + 1 num_block, intervals = _calc_partitions(prefix, upper_bound) if num_block < num: intervals = _heap_addition(prefix, intervals, num - num_block) return intervals def partition_uniform(num_items, pipeline_parallel_size, num_chunks): assert ( num_items % num_chunks == 0 ), "Layer length should be divided by the number of chunks, otherwise parameter method is recommended" logger = get_dist_logger() parts = [[] for _ in range(pipeline_parallel_size)] partition_items = num_items // num_chunks for idx in range(num_chunks): base_idx = idx * partition_items chunk_size = partition_items // pipeline_parallel_size left = pipeline_parallel_size - partition_items % pipeline_parallel_size if chunk_size == 0: logger.warning("Some nodes in Pipeline have no requests") for p in range(pipeline_parallel_size): st = base_idx base_idx += chunk_size + (p >= left) parts[p].append((st, base_idx)) return parts def partition_balanced(weights, pipeline_parallel_size, num_chunks): num_total = pipeline_parallel_size * num_chunks num_items = len(weights) if num_items <= num_total: return partition_uniform(num_items, pipeline_parallel_size, num_chunks) intervals = _binary_search(weights, num_total) current = 0 parts = [[] for _ in range(pipeline_parallel_size)] for inter in intervals: parts[current].append(inter) current = (current + 1) % pipeline_parallel_size return parts def build_kwargs_for_module(function, input_tensor, kw_dict): """ Generally, the first argument of module.forward is an input tensor come from the previous layer. Therefore, we just filter the kwargs from second element of the dictionary. """ sig = inspect.signature(function) if input_tensor is None: kwargs_offset = 0 elif isinstance(input_tensor, torch.Tensor): kwargs_offset = 1 elif isinstance(input_tensor, (tuple, OrderedDict)): # assert isinstance(input_tensor, tuple), f'input_tensor should be a torch.Tensor or a tuple object.' # Huggingface will take their own structures based on OrderedDict as the output # between layers so we've to close this check. kwargs_offset = len(input_tensor) args_name_list = list(sig.parameters.keys()) kw_dict = {k: v for k, v in kw_dict.items() if k in args_name_list[kwargs_offset:]} if len(kw_dict) == 0: return None return kw_dict def build_kwargs_for_function(function, kw_dict): sig = inspect.signature(function) kw_dict = {k: v for k, v in kw_dict.items() if k in sig.parameters} if len(kw_dict) == 0: return None return kw_dict def exec_func_with_kwargs(func, kw_dict, input_tensor, kwargs): """ We suppose the callable object passed to to_layer_list method in two purpose: a. use the callable object to modify input tensor, such as \ lambda x: torch.flatten(x, 1) b. use the callable object to modify kwargs value, such as \ def foo(attention_mask=None): if attention_mask is not None: batch_size = input_ids.shape[0] attention_mask = attention_mask.view(batch_size, -1) return attention_mask """ if kw_dict is not None: rst = func(**kw_dict) if isinstance(rst, tuple): for i, k in enumerate(kw_dict.keys()): kwargs[k] = rst[i] else: for k in kw_dict.keys(): kwargs[k] = rst return input_tensor if isinstance(input_tensor, tuple): assert len(input_tensor) > 0, f"input_tensor should not be empty, when kw_dict is None." sig = inspect.signature(func) func_args_num = len(sig.parameters) assert func_args_num <= len( input_tensor ), f"func requires {func_args_num} arguments, but input_tensors only have {len(input_tensor)}." if func_args_num < len(input_tensor): return func(*input_tensor[:func_args_num]) else: return func(*input_tensor) assert isinstance(input_tensor, torch.Tensor), "input_tensor should be a type of torch.Tensor or tuple." return func(input_tensor) def exec_funcs_with_kwargs(func_dict, func_key, input_tensor, kwargs): assert func_key in func_dict, f"{func_key} is not in the function_dict." funcs_to_exec = func_dict[func_key] if isinstance(funcs_to_exec, list): for f in funcs_to_exec: f_kwargs = build_kwargs_for_function(f, kwargs) input_tensor = exec_func_with_kwargs(f, f_kwargs, input_tensor, kwargs) else: f_kwargs = build_kwargs_for_function(funcs_to_exec, kwargs) input_tensor = exec_func_with_kwargs(funcs_to_exec, f_kwargs, input_tensor, kwargs) return input_tensor def call_module(module, args=None, kwargs=None): if args is None: args = () if kwargs is None: kwargs = {} if isinstance(module, CheckpointModule): forward_func = module._forward else: forward_func = module.forward sig = inspect.signature(forward_func) param_nums = len(sig.parameters) len(args) + len(kwargs) args_needed_nums = param_nums - len(kwargs) args_needed = args[:args_needed_nums] if isinstance(module, CheckpointModule): convert_kwargs_to_args = [] for v in kwargs.values(): convert_kwargs_to_args.append(v) return module(*args_needed, *convert_kwargs_to_args) else: return module(*args_needed, **kwargs) def customized_partition(exec_seq): """ This function will analyze the exec_seq. In the exec_seq, users will use 'SPLIT_NODE' as an annotation to note the partition point. """ customized_parts = {} start = 0 stop = 0 rank = 0 for element in exec_seq: if isinstance(element, str): if element == "SPLIT_NODE": customized_parts[rank] = [(start, stop)] start = stop rank += 1 else: stop += 1 customized_parts[rank] = [(start, stop)] return customized_parts
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/__init__.py
colossalai/legacy/pipeline/__init__.py
from .layer_spec import LayerSpec from .pipelinable import PipelinableContext, PipelinableModel __all__ = ["PipelinableModel", "PipelinableContext", "LayerSpec"]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/layer_spec.py
colossalai/legacy/pipeline/layer_spec.py
import torch from colossalai.utils.model.utils import call_to_str class LayerSpec: """ """ def __init__(self, typename, *module_args, **module_kwargs): self.typename = typename self.module_args = module_args self.module_kwargs = module_kwargs self.children = None self._param_count = 0 if not issubclass(typename, torch.nn.Module): raise RuntimeError("LayerSpec only supports torch.nn.Module types.") def __repr__(self): return call_to_str(self.typename.__name__, self.module_args, self.module_kwargs) @property def param_count(self): return self._param_count def build(self): """Build the stored specification.""" recovered_args = [] for obj in self.module_args: if isinstance(obj, LayerSpec): obj = obj.build() recovered_args.append(obj) recovered_args = tuple(recovered_args) recovered_kwargs = {} for k, v in self.module_kwargs.items(): if isinstance(v, LayerSpec): v = v.build() recovered_kwargs[k] = v return self.typename(*recovered_args, **recovered_kwargs) def set_children(self, children): self.children = children def count_params(self): self._param_count = 0 layer = self.build() for param in layer.parameters(): self._param_count += param.numel() return self._param_count def reset_param_count(self): self._param_count = 0
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/middleware/topo.py
colossalai/legacy/pipeline/middleware/topo.py
from dataclasses import dataclass from typing import Dict, List # This file includes data structure used by Pipeline Middleware. @dataclass class ValPosition: partition_id: int offset: int def __str__(self) -> str: res = f"[partition_id:{self.partition_id},offset:{self.offset}]" return res def __repr__(self) -> str: return self.__str__() class PartitionInputVal(object): def __init__(self, partition_id, offset) -> None: # every input from which partition_id and which offset val_pos = ValPosition(partition_id, offset) self._from_partition_and_offset: ValPosition = val_pos def get(self): return self._from_partition_and_offset def __str__(self) -> str: res = "" res += f"<-({self._from_partition_and_offset})" return res def __repr__(self) -> str: return self.__str__() class PartitionOutputVal(object): def __init__(self) -> None: # every output to which partition_id and which offset self._to_partition_and_offset: List[ValPosition] = [] def add(self, partition_id, offset): val_pos = ValPosition(partition_id, offset) self._to_partition_and_offset.append(val_pos) def get(self): return self._to_partition_and_offset def __str__(self) -> str: res = "" res += "->(" for val_pos in self._to_partition_and_offset: res += f"{val_pos}," res += ")" return res def __repr__(self) -> str: return self.__str__() class Partition(object): def __init__(self) -> None: self._input_vals: List[PartitionInputVal] = [] self._output_vals: List[PartitionOutputVal] = [] def add_input_val(self, input_val: PartitionInputVal): self._input_vals.append(input_val) def add_output_val(self, output_val: PartitionOutputVal): self._output_vals.append(output_val) def get_input_vals(self): return self._input_vals def get_output_vals(self): return self._output_vals # get the output offsets sent to dst_partition_id def get_output_offsets(self, dst_partition_id): res = [] for offset, output_val in enumerate(self._output_vals): outputs = output_val.get() for val_pos in outputs: if val_pos.partition_id == dst_partition_id: res.append(offset) return res # get all input dst partition_ids def get_input_partition_ids(self): res = [] for input_val in self._input_vals: val_pos = input_val.get() if val_pos.partition_id not in res: res.append(val_pos.partition_id) return res # get all output dst partition_ids def get_output_partition_ids(self): res = [] for output_val in self._output_vals: outputs = output_val.get() for val_pos in outputs: if val_pos.partition_id not in res: res.append(val_pos.partition_id) return res def __str__(self) -> str: res = "" res += f" input:\n" res += f" length:{len(self._input_vals)}\n" for i, input_val in enumerate(self._input_vals): res += f" offset={i}:{input_val}\n" res += f" output:\n" res += f" length:{len(self._output_vals)}\n" for i, output_val in enumerate(self._output_vals): res += f" offset={i}:{output_val}\n" return res def __repr__(self) -> str: return self.__str__() # This class is a middleware between partition splitter # and Pipeline Scheduler. It records the graph info about # partition input/output and provides it to scheduler. # There are three kinds of partition in Pipeline Middleware Design # which represents the whole process of a model execution: input-fwd-output # 1. input_partition: records the input of a model. # 2. mid_partition: record the splitted forwards execution of a model. # 3. output_partition: records the output of a model. # attributes: # _partitions: include all partitions # _input_partition_id: the key represents input_partition # _output_partition_id: the key represents output_partition class Topo(object): def __init__(self, input_partition_id=None, output_partition_id=None) -> None: self._partitions: Dict[int, Partition] = {} self._input_partition_id = input_partition_id self._output_partition_id = output_partition_id def set_input_partition_id(self, partition_id: int): self._input_partition_id = partition_id def set_output_partition_id(self, partition_id: int): self._output_partition_id = partition_id def get_input_partition_id(self): return self._input_partition_id def get_output_partition_id(self): return self._output_partition_id def set_partitions(self, partition_id: int, partition: Partition): self._partitions[partition_id] = partition def get_mid_partitions(self): res = {} # {partition_id: Partition} for partition_id, partition in self._partitions.items(): if self._input_partition_id == partition_id or self._output_partition_id == partition_id: continue res[partition_id] = partition return res def get_mid_partition_ids(self): return list(self.get_mid_partitions().keys()) def get_input_partition(self): if self._input_partition_id is not None: return self._partitions[self._input_partition_id] return None def get_output_partition(self): if self._output_partition_id is not None: return self._partitions[self._output_partition_id] return None def get_partition_by_id(self, partition_id): return self._partitions[partition_id] def __str__(self) -> str: res = "" if len(self._partitions) == 0: return "Empty Topo Graph." input_part = self.get_input_partition() if input_part is not None: res += "{\n" res += f"InputPartition:\n partition_id={self._input_partition_id}\n{input_part}" res += "}\n" mid_parts = self.get_mid_partitions() for i, (partition_id, part) in enumerate(mid_parts.items()): res += "{\n" res += f"SubPartition_{i}:\n partition_id={partition_id}\n {part}" res += "}\n" output_part = self.get_output_partition() if output_part is not None: res += "{\n" res += f"OutputPartition:\n partition_id={self._output_partition_id}\n{output_part}" res += "}\n" return res def __repr__(self) -> str: return self.__str__()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/middleware/__init__.py
colossalai/legacy/pipeline/middleware/__init__.py
from .topo import Partition, PartitionInputVal, PartitionOutputVal, Topo __all__ = ["Topo", "Partition", "PartitionOutputVal", "PartitionInputVal"]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/middleware/adaptor/fx.py
colossalai/legacy/pipeline/middleware/adaptor/fx.py
import torch from torch.fx.graph_module import GraphModule from colossalai.legacy.pipeline.middleware.topo import Partition, PartitionInputVal, PartitionOutputVal, Topo def partition_name_to_id(partition_name, is_input=False, is_output=False): if is_input: partition_id = 0 elif is_output: partition_id = 1 else: prefix = "submod_" partition_id = int(partition_name.split(prefix)[-1]) + 2 return partition_id # There are two kinds of def in fx.graph # 1. non direct_use & non direct_def, which means the output is used by next partition with a temporary mid value. # e.g. submod1 = call_module(...) # temporary_val = submod1[0] # submod2 = call_module(temporary_val, ...) # 2. direct_use & direct_def, which means the output is used by next partition directly. # e.g. submod1 = call_module(...) # submod2 = call_module(submod1, ...) def find_input_in_partition(node, partitions, input_partitions=None): p_input_val = None direct_def = not node.name.startswith("getitem") # search in input if direct_def and input_partitions is not None: partition_id = partition_name_to_id("", is_input=True) for i, input_node in enumerate(input_partitions): if input_node == node: p_input_val = PartitionInputVal(partition_id=partition_id, offset=i) return p_input_val # search submod in mid part if direct_def: for partition in partitions: if partition == node: partition_id = partition_name_to_id(partition.name) p_input_val = PartitionInputVal(partition_id=partition_id, offset=0) return p_input_val # search temporary value in graph else: for partition in partitions: for offset, mid_val in enumerate(partition.users): if mid_val == node: partition_id = partition_name_to_id(partition.name) p_input_val = PartitionInputVal(partition_id=partition_id, offset=offset) return p_input_val return p_input_val def find_output_in_partition(node, partitions, output_partitions=None): p_output_val = PartitionOutputVal() for user in node.users: direct_use = not user.name.startswith("getitem") # user is mid partition for partition in partitions: # direct call if direct_use: if user == partition: partition_id = partition_name_to_id(partition.name) for i, arg in enumerate(partition.args): if arg == node: p_output_val.add(partition_id=partition_id, offset=i) break # getitem call else: if user in partition.args: partition_id = partition_name_to_id(partition.name) for i, arg in enumerate(partition.args): if arg == user: p_output_val.add(partition_id=partition_id, offset=i) break # user is output if output_partitions is not None: output_node = output_partitions[0] if user.op == output_node.op: output_keys = {} partition_id = partition_name_to_id("", is_output=True) torch.fx.graph.map_arg(output_node.args[0], lambda n: output_keys.setdefault(n)) for i, arg in enumerate(output_keys): if arg == node: p_output_val.add(partition_id=partition_id, offset=i) break return p_output_val def get_topology(gm: GraphModule): topo = Topo() topo_output_partition = Partition() input_partitions = [] partitions = [] output_partitions = [] for node in gm.graph.nodes: if node.op == "placeholder": input_partitions.append(node) elif node.name.startswith("submod_"): partitions.append(node) elif node.op == "output": output_partitions.append(node) else: continue # set output for input_partition topo_input_partition = Partition() for partition in input_partitions: cur_node = partition p_output_val = find_output_in_partition(cur_node, partitions, output_partitions) topo_input_partition.add_output_val(p_output_val) topo.set_partitions(partition_id=0, partition=topo_input_partition) topo.set_input_partition_id(partition_id=0) for i, partition in enumerate(partitions): topo_mid_partition = Partition() # set input for submodule for arg in partition.args: cur_node = arg p_input_val = find_input_in_partition(cur_node, partitions, input_partitions) topo_mid_partition.add_input_val(p_input_val) # set output for submodule direct_use = True for user in partition.users: if user.name.startswith("getitem"): direct_use = False break if direct_use: cur_node = partition p_output_val = find_output_in_partition(cur_node, partitions, output_partitions) topo_mid_partition.add_output_val(p_output_val) else: for user in partition.users: cur_node = user p_output_val = find_output_in_partition(cur_node, partitions, output_partitions) topo_mid_partition.add_output_val(p_output_val) topo.set_partitions(partition_id=i + 2, partition=topo_mid_partition) # set input for output_partition for partition in output_partitions: topo_output_partition = Partition() torch.fx.graph.map_arg( partition.args[0], lambda n: topo_output_partition.add_input_val(find_input_in_partition(n, partitions, input_partitions)), ) topo.set_partitions(partition_id=1, partition=topo_output_partition) topo.set_output_partition_id(partition_id=1) return topo
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/middleware/adaptor/__init__.py
colossalai/legacy/pipeline/middleware/adaptor/__init__.py
from .fx import get_topology as get_fx_topology __all__ = ["get_fx_topology"]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/rpc/_pipeline_schedule.py
colossalai/legacy/pipeline/rpc/_pipeline_schedule.py
import threading from typing import Callable, Dict, List import torch from torch._C._distributed_rpc import PyRRef from torch.futures import Future from colossalai.legacy.pipeline.pipeline_process_group import ppg from colossalai.legacy.pipeline.rpc._pipeline_base import Phase, PipelineEngineBase, UniqueKey, WorkerBase, WorkItem # Implementation of different Pipeline schedule # <strategy>Worker defines the worker for each stage # <strategy>PipelineEngine is the class for use class FillDrainWorker(WorkerBase): def _get_work_item_key(self) -> UniqueKey: # execute backward first (if backward phase in work_list) num_microbatches = self.num_microbatches if self.forward_times < num_microbatches: target_phase = Phase.FORWARD target_microbatch_id = self.forward_times else: target_phase = Phase.BACKWARD target_microbatch_id = self.backward_times target_key = UniqueKey(target_microbatch_id, target_phase) return target_key class FillDrainPipelineEngine(PipelineEngineBase): def __init__( self, partition_fn: Callable, stage_num: int, num_microbatches: int, device: str, chunk: int = 1, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None, ) -> None: if chunk > 1: assert ( num_microbatches % stage_num == 0 ), "if you use interleaving strategy, make sure 'num_microbatches' is a multiple of stage_num!" use_1F1B = False super().__init__( FillDrainWorker, partition_fn, stage_num, num_microbatches, device, use_1F1B, chunk, criterion, metric, checkpoint, data_process_func, ) class OneFOneBWorker(WorkerBase): def _get_work_item_key(self) -> UniqueKey: # execute backward first (if backward phase in work_list) pp_rank = self.pp_rank actual_stage_num = self.actual_stage_num num_microbatches = self.num_microbatches is_last_stage = pp_rank == actual_stage_num - 1 if self.outstanding <= self.outstanding_range[0]: target_phase = Phase.FORWARD target_microbatch_id = self.forward_times elif self.outstanding >= self.outstanding_range[1]: target_phase = Phase.BACKWARD target_microbatch_id = self.backward_times else: raise ValueError("outstanding_range[1] - outstanding_range[0] must be in [0, 1]") target_key = UniqueKey(target_microbatch_id, target_phase) # change outstanding_range at: # 1. forward times reach actual_stage_num, this is the end of continuous forward # 2. forward times reach num_microbatches, this is the end of 1F1B mode if not is_last_stage and target_key.phase == Phase.FORWARD: if target_key.microbatch_id == actual_stage_num - 1 and num_microbatches > 2: # Why need num_microbatches > 2 ? Because there is no steady stage when num_microbatches <= 2 outstanding_min = actual_stage_num - pp_rank - 1 outstanding_max = actual_stage_num - pp_rank self.outstanding_range = (outstanding_min, outstanding_max) if target_key.microbatch_id == num_microbatches - 1: self.outstanding_range = (0, 0) return target_key class OneFOneBPipelineEngine(PipelineEngineBase): def __init__( self, partition_fn: Callable, stage_num: int, num_microbatches: int, device: str, chunk: int = 1, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None, ) -> None: if chunk > 1: assert ( num_microbatches % stage_num == 0 ), "if you use interleaving strategy, make sure 'num_microbatches' is a multiple of stage_num!" # assert num_microbatches > stage_num * chunk, "num_microbatches must be greater than stage_num * chunk" use_1F1B = True super().__init__( OneFOneBWorker, partition_fn, stage_num, num_microbatches, device, use_1F1B, chunk, criterion, metric, checkpoint, data_process_func, ) class ChimeraWorker(WorkerBase): def _get_producer_consumer(self) -> None: rank = self.pp_rank min_pp_rank = (rank // self.actual_stage_num) * self.actual_stage_num max_pp_rank = min_pp_rank + self.actual_stage_num - 1 assert self.producer_stage_ids is None, f"all the producers of rank {rank} has been subscribed" assert self.consumer_stage_ids is None, f"all the consumers of rank {rank} has been subscribed" # should be arranged in order, the order of the input of current forward self.producer_stage_ids = [] self.consumer_stage_ids = [] # Just for demo prev_rank = rank - 1 next_rank = rank + 1 if prev_rank >= min_pp_rank: self.producer_stage_ids.append(prev_rank) if next_rank <= max_pp_rank: self.consumer_stage_ids.append(next_rank) def _get_work_item_key(self) -> UniqueKey: pp_rank = self.pp_rank stage_num = self.actual_stage_num real_microbatch_num = self.num_microbatches // 2 forward_block_size = 1 if self.num_microbatches < stage_num else self.num_microbatches // stage_num forward_block_num = self.forward_times // forward_block_size if self.forward_times >= real_microbatch_num or ( (pp_rank + 1) % stage_num == 0 and forward_block_num > self.backward_times ): target_phase = Phase.BACKWARD target_microbatch_id = self.backward_times else: # others target_phase = Phase.FORWARD target_microbatch_id = self.forward_times # In up pipeline, microbatch_id to consume is 0, 2, 4 (2n) # In down pipeline, microbatch_id to consume is 1, 3, 5 (2n + 1) real_target_microbatch_id = target_microbatch_id * 2 if pp_rank >= stage_num: real_target_microbatch_id += 1 target_key = UniqueKey(real_target_microbatch_id, target_phase) with self.work_list_condition_lock: self.work_list_condition_lock.wait_for(lambda: target_key in self.work_list) return target_key def _initialize_partition(self): # In order to ensure the down pipeline share the same parameter # with the up pipeline, partition of down partition will be copied # from corresponding up stage pp_rank = self.pp_rank stage_num = self.actual_stage_num self.device if pp_rank < stage_num: super()._initialize_partition() else: # if it is down pipeline, create partition by origin method co_up_pp_worker_rref = self.pp_rank_to_worker_rref[pp_rank - stage_num] # get the corresponding model state dict and wait for its init state_dict = co_up_pp_worker_rref.rpc_sync().get_partition_state_dict() super()._initialize_partition() self.module_partition.load_state_dict(state_dict) # init group for chimera in ppg ppg.get_chimera_all_reduce_group(pp_rank) # lock for step sync self.step_sync_lock = threading.Lock() self.step_sync_lock.acquire() self.have_grad_lock = threading.Lock() self.have_grad_lock.acquire() def _get_lock_gradient(self): self.have_grad_lock.acquire() grads = self.get_parameter_gradients() self.step_sync_lock.release() return grads def is_first_stage(self): return (self.pp_rank % self.actual_stage_num) == 0 def is_last_stage(self): return (self.pp_rank % self.actual_stage_num) == self.actual_stage_num - 1 def _is_last_step(self, work_item: WorkItem) -> bool: if work_item.forward_only: last_phase = Phase.FORWARD else: last_phase = Phase.BACKWARD is_last_phase = work_item.phase == last_phase last_microbatch_id = self.num_microbatches - 1 if self.pp_rank < self.actual_stage_num: last_microbatch_id -= 1 is_last_microbatch = work_item.microbatch_id == last_microbatch_id return is_last_phase and is_last_microbatch def _get_step_order(self) -> List[int]: # TODO : If you want to extend it to multi head chimera, overwrite here stage_num = self.actual_stage_num pp_rank = self.pp_rank # pp_rank in the same device local_device_pp_ranks = [pp_rank, stage_num * 2 - pp_rank - 1] local_device_pp_ranks.sort(reverse=min(local_device_pp_ranks) < stage_num // 2) return local_device_pp_ranks def _hook_before_step(self): self.have_grad_lock.release() pp_rank = self.pp_rank stage_num = self.actual_stage_num co_pp_rank = (pp_rank + stage_num) % (2 * stage_num) # if current pp_rank is not the first to do step # wait its previous pp_rank finish step grads = self.get_parameter_gradients() # send co_worker = self.pp_rank_to_worker_rref[co_pp_rank] co_grads = co_worker.rpc_sync()._get_lock_gradient() # sync self.step_sync_lock.acquire() for i in range(len(grads)): grads[i] += co_grads[i] class ChimeraPipelineEngine(PipelineEngineBase): def __init__( self, partition_fn: Callable, stage_num: int, num_microbatches: int, device: str, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None, ) -> None: assert num_microbatches % stage_num == 0, "In Chimera, num_microbatches must be the multiply of stage_num!" use_1F1B = False chunk = 1 super().__init__( ChimeraWorker, partition_fn, stage_num, num_microbatches, device, use_1F1B, chunk, criterion, metric, checkpoint, data_process_func, ) def _consume_constraint( self, microbatch_id: int, forward_only: bool, input_pp_ranks: List[int], output_pp_ranks: List[int], ret_future ): pass def _create_pp_rank_to_rpc_worker_id(self) -> None: stage_num = self.stage_num self.pp_rank_to_rpc_worker_id = [0] * (stage_num * 2) for pp_rank in range(stage_num): self.pp_rank_to_rpc_worker_id[pp_rank] = pp_rank self.pp_rank_to_rpc_worker_id[pp_rank + stage_num] = stage_num - pp_rank - 1 def _create_pp_rank_to_module_partition_id(self) -> None: stage_num = self.stage_num self.pp_rank_to_module_partition_id = [0] * (stage_num * 2) for pp_rank in range(stage_num): self.pp_rank_to_module_partition_id[pp_rank] = pp_rank self.pp_rank_to_module_partition_id[pp_rank + stage_num] = pp_rank def _create_ret_future(self, output_pp_ranks: List[int]) -> Dict[int, List[Future]]: num_microbatches = self.num_microbatches stage_num = self.stage_num up_ret_future = {pp_rank: [None] * num_microbatches for pp_rank in output_pp_ranks} down_ret_future = {pp_rank + stage_num: [None] * num_microbatches for pp_rank in output_pp_ranks} # merge up and down return {**up_ret_future, **down_ret_future} def _set_input(self, input_pp_ranks: List[int], microbatch_id: int, microbatch, forward_only: bool): # offset is 0 for all the ranks in up pipeline # offset is stage_num for all the ranks in down pipeline offset = (microbatch_id % 2) * self.stage_num for pp_rank in input_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank + offset] worker_rref.remote().set_input(microbatch_id, microbatch, forward_only) def _set_labels(self, output_pp_ranks: List[int], microbatch_id: int, microlabels): # offset is 0 for all the ranks in up pipeline # offset is stage_num for all the ranks in down pipeline offset = (microbatch_id % 2) * self.stage_num for pp_rank in output_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank + offset] worker_rref.remote().set_labels(microbatch_id, microlabels) def _subscribe_forward(self, microbatch_id: int, output_pp_ranks: List[int], ret_future: Dict[int, List[Future]]): key = UniqueKey(microbatch_id, Phase.FORWARD) offset = (microbatch_id % 2) * self.stage_num for pp_rank in output_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank + offset] ret_future[pp_rank + offset][microbatch_id] = worker_rref.rpc_async().get_output_by_key(key) def _ensure_backward(self, forward_only: bool, input_pp_ranks: List[int]): stage_num = self.stage_num num_microbatches = self.num_microbatches if not forward_only: for pp_rank in input_pp_ranks: up_last_microbatch_id = num_microbatches - 2 down_last_microbatch_id = num_microbatches - 1 up_worker_rref = self.pp_rank_to_worker_rref[pp_rank] down_worker_rref = self.pp_rank_to_worker_rref[pp_rank + stage_num] up_key = UniqueKey(up_last_microbatch_id, Phase.BACKWARD) down_key = UniqueKey(down_last_microbatch_id, Phase.BACKWARD) up_worker_rref.rpc_sync().get_output_by_key(up_key) down_worker_rref.rpc_sync().get_output_by_key(down_key) def _collect_forward_result(self, output_pp_ranks: List[int], ret_future: Dict[PyRRef, List[Future]]): """Logic of collection of forward in Chimera. Currently, only one input one output model is supported """ stage_num = self.stage_num forward_result = [] for pp_rank in output_pp_ranks: worker_forward_result = [None] * self.num_microbatches for microbatch_id in range(self.num_microbatches): offset = (microbatch_id % 2) * stage_num ret = ret_future[pp_rank + offset][microbatch_id].wait() ret = [ret] if isinstance(ret, torch.Tensor) else ret worker_forward_result[microbatch_id] = ret worker_forward_result = list(zip(*worker_forward_result)) forward_result.extend(worker_forward_result) return forward_result
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/rpc/utils.py
colossalai/legacy/pipeline/rpc/utils.py
import argparse import os import warnings from typing import Any, Callable, Tuple, Type, Union import torch import torch.distributed.rpc as rpc import torch.multiprocessing as mp from torch._C._distributed_rpc import _is_current_rpc_agent_set from torch.futures import Future from colossalai.initialize import launch from colossalai.legacy.pipeline.pipeline_process_group import ppg def pyobj_map(obj: Any, fn: Callable, process_types: Union[Type, Tuple[Type]] = ()) -> Any: if isinstance(obj, process_types): return fn(obj) elif type(obj) is dict: return {k: pyobj_map(obj[k], fn, process_types) for k in obj} elif type(obj) is tuple: return tuple(pyobj_map(o, fn, process_types) for o in obj) elif type(obj) is list: return list(pyobj_map(o, fn, process_types) for o in obj) else: return obj def pytree_map(obj: Any, fn: Callable, process_types: Union[Type, Tuple[Type]] = (), map_all: bool = False) -> Any: """process object recursively, like pytree Args: obj (:class:`Any`): object to process fn (:class:`Callable`): a function to process subobject in obj process_types (:class: `type | tuple[type]`): types to determine the type to process map_all (:class: `bool`): if map_all is True, then any type of element will use fn Returns: :class:`Any`: returns have the same structure of `obj` and type in process_types after map of `fn` """ if isinstance(obj, dict): return {k: pytree_map(obj[k], fn, process_types, map_all) for k in obj} elif isinstance(obj, tuple): return tuple(pytree_map(o, fn, process_types, map_all) for o in obj) elif isinstance(obj, list): return list(pytree_map(o, fn, process_types, map_all) for o in obj) elif isinstance(obj, process_types): return fn(obj) else: return fn(obj) if map_all else obj def tensor_shape_list(obj): return pytree_map(obj, fn=lambda x: x.shape, process_types=torch.Tensor) def get_batch_lengths(batch): lengths = [] pytree_map(batch, fn=lambda x: lengths.append(len(x)), process_types=torch.Tensor) return lengths def split_batch(batch: Any, start, stop, device: str): if device == "cuda": fn = lambda x: x[start:stop].cuda() else: fn = lambda x: x[start:stop] return pytree_map(batch, fn=fn, process_types=torch.Tensor) def type_detail(obj): return pytree_map(obj, lambda x: type(x), map_all=True) def pytree_filter(fn, obj, process_types): if obj is None: return None filters = [] def condition_append(obj): if fn(obj): filters.append(obj) pytree_map(obj, fn=condition_append, process_types=process_types) return filters def get_real_args_kwargs(args_or_kwargs): args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) # TODO : combine producer and consumer # by default, merge all args in the output args or kwargs if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) args_or_kwargs = flatten_args return args_or_kwargs def run_worker(rank, args, master_func): os.environ["MASTER_ADDR"] = args.master_addr os.environ["MASTER_PORT"] = args.master_port device = args.device world_size = args.world_size dp_degree = args.dp_degree tp_degree = args.tp_degree num_worker_threads = args.num_worker_threads host = args.master_addr port = args.master_port backend = "nccl" if device == "cuda" else "gloo" launch(rank, world_size, host, int(port), backend, verbose=False) ppg.set_global_info( rank=rank, world_size=world_size, dp_degree=dp_degree, tp_degree=tp_degree, num_worker_threads=num_worker_threads, device=device, ) ppg.args = args # in rpc mode, only rank 0 is needed to be coded if rank == 0: master_func(args) # barrier here if _is_current_rpc_agent_set(): rpc.shutdown() else: warnings.warn("RPC has not been initialized") def rpc_run(args, master_func): world_size = args.world_size mp.spawn(run_worker, args=(args, master_func), nprocs=world_size) def parse_args(): parser = argparse.ArgumentParser() parser.add_argument("--epoch", type=int, default=1) parser.add_argument("--world_size", type=int, default=2) parser.add_argument("--batch_size", type=int, default=16) parser.add_argument("--dp_degree", type=int, default=1) parser.add_argument("--tp_degree", type=int, default=1) parser.add_argument("--num_microbatches", type=int, default=2) parser.add_argument("--chunk", type=int, default=1) parser.add_argument("--use_checkpoint", action="store_true") parser.add_argument("--optimizer", type=str, choices=["SGD", "Adam", "RMSprop"], default="SGD") parser.add_argument("--device", type=str, choices=["cpu", "cuda"], default="cuda") parser.add_argument("--master_addr", type=str, default="localhost") parser.add_argument("--master_port", type=str, default="29020") parser.add_argument("--num_worker_threads", type=int, default=128) return parser.parse_args()
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/rpc/_pipeline_base.py
colossalai/legacy/pipeline/rpc/_pipeline_base.py
import inspect import math import threading from abc import ABC, abstractmethod from enum import Enum from functools import partial from typing import Any, Callable, Dict, List, Tuple import torch import torch.distributed.rpc as rpc from torch import autograd, nn, optim from torch._C._distributed_rpc import PyRRef from torch.futures import Future from colossalai.legacy.pipeline.middleware import Partition, Topo from colossalai.legacy.pipeline.pipeline_process_group import ppg from colossalai.legacy.pipeline.rpc.utils import get_batch_lengths, pyobj_map, pytree_filter, pytree_map, split_batch class Phase(Enum): FORWARD = 0 BACKWARD = 1 UPDATE = 2 INPUT = 3 class UniqueKey: __slots__ = ("microbatch_id", "phase") microbatch_id: int phase: Phase def __init__(self, microbatch_id, phase) -> None: self.microbatch_id = microbatch_id self.phase = phase def __eq__(self, __o: object) -> bool: return (self.microbatch_id == __o.microbatch_id) and (self.phase == __o.phase) def __hash__(self) -> int: return tuple.__hash__((self.microbatch_id, self.phase)) def __repr__(self) -> str: return f"Key(microbatch_id={self.microbatch_id}, phase={self.phase})" class WorkItem: __slots__ = ( "stage_id", "phase", "args", "kwargs", "output", "refcount", "microbatch_id", "batch_id", "num_microbatches", "forward_only", ) stage_id: int phase: Phase args: Tuple[Any] kwargs: Dict[str, Any] output: Future microbatch_id: int refcount: int batch_id: int num_microbatches: int forward_only: bool def __init__( self, stage_id, phase, args, kwargs, output, microbatch_id, batch_id, num_microbatches, forward_only, refcount=0 ) -> None: for attr_name in self.__slots__: setattr(self, attr_name, locals()[attr_name]) class BackwardCache: __slots__ = ("checkpoint", "stage_input_args", "stage_input_kwargs", "stage_outputs") checkpoint: bool stage_input_args: Tuple[Any] stage_input_kwargs: Dict[Any, Any] stage_outputs: Tuple[Any] def __init__( self, stage_input_args: Tuple[Any], stage_input_kwargs: Dict[Any, Any] = None, stage_outputs: Tuple[Any] = None, checkpoint: bool = False, ) -> None: for arg_name in self.__slots__: setattr(self, arg_name, locals()[arg_name]) class WorkerBase(ABC): def __init__( self, partition_fn: Callable, partition_args: tuple, pp_rank: int, actual_stage_num: int, num_microbatches: int, device: str, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None, ) -> None: super().__init__() self.pp_rank = pp_rank self.actual_stage_num = actual_stage_num self.num_microbatches = num_microbatches self.checkpoint = checkpoint if data_process_func is not None: self.data_process_func = partial(data_process_func, pp_rank) self.device = device self._initialize_outstanding_range() # variable and const for context management self.outstanding = 0 self.forward_times = 0 self.backward_times = 0 self.reset_key = UniqueKey(0, Phase.FORWARD) # rref of other workers self.pp_rank_to_worker_rref: Dict[int, PyRRef] = None # lock for the list self._initialize_lock() # topology info self.producer_stage_ids: List[int] = None self.consumer_stage_ids: List[int] = None # module partitions self.partition_fn = partition_fn self.partition_args = partition_args self.criterion = criterion self.metric = metric self.reset = False # context to maintain loop self._initialize_context_container() # main loop self.main_loop_thread = threading.Thread(target=self._work_loop, name=f"rank_{pp_rank}", daemon=True) self.main_loop_thread.start() def _get_future_by_device(self): return torch.futures.Future(devices=None if self.device in (None, "cpu") else [self.device]) def _initialize_outstanding_range(self): outstanding_range = None if self.pp_rank == self.actual_stage_num - 1: outstanding_range = (0, 1) else: outstanding_range = (self.actual_stage_num, self.actual_stage_num) self.outstanding_range = outstanding_range def _initialize_context_container(self): self.microbatch_id_to_backward_cache: Dict[int, BackwardCache] = dict() self.microbatch_id_to_labels: Dict[int, Any] = dict() self.work_list: Dict[UniqueKey, WorkItem] = dict() self.output_list: Dict[UniqueKey, WorkItem] = dict() def _initialize_lock(self): self.partition_condition_lock = threading.Condition(threading.Lock()) self.work_list_condition_lock = threading.Condition(threading.Lock()) self.output_list_condition_lock = threading.Condition(threading.Lock()) self.label_lock = threading.Condition(threading.Lock()) self.reset_condition = threading.Condition(threading.Lock()) def _initialize_partition(self): partition_fn = self.partition_fn partition_args = self.partition_args device = self.device with self.partition_condition_lock: self.module_partition: nn.Module = partition_fn(*partition_args).to(device) self.partition_condition_lock.notify_all() def _get_output_all(self, key: UniqueKey, ref_use=False, rank=None): with self.output_list_condition_lock: self.output_list_condition_lock.wait_for(lambda: key in self.output_list) output_work_item = self.output_list[key] output = output_work_item.output if not ref_use and output_work_item.phase != Phase.INPUT: self.output_list.pop(key) if not ref_use and output_work_item.phase != Phase.INPUT: output_work_item.refcount += 1 refcount = output_work_item.refcount # lifecycle management for DAG scheduler if output_work_item.phase == Phase.FORWARD: lifecycle = len(self.get_consumer_stage_ids()) if self.is_model_output(): # an extra reference for scheduler collecting results lifecycle += 1 elif output_work_item.phase == Phase.BACKWARD: lifecycle = len(self.get_producer_stage_ids()) if self.is_model_input() and self._is_last_step( output_work_item ): # an extra reference for ensure_backward lifecycle += 1 else: lifecycle = 0 refcount = 0 with self.output_list_condition_lock: if refcount <= lifecycle: self.output_list[key] = output_work_item self.output_list_condition_lock.notify_all() if isinstance(output, Future): output = output.wait() return output def sync_global_worker_rrefs(self, pp_rank_to_worker_rref: Dict[int, PyRRef]) -> None: assert self.pp_rank_to_worker_rref is None, f"in rank {self.pp_rank}, worker has sync global workers rrefs" assert pp_rank_to_worker_rref is not None, "stage_to_workers must be a dict instead of None" self.pp_rank_to_worker_rref = pp_rank_to_worker_rref # for some schedule need the other worker's info to initialise partition (like Chimera) # construction of partition is executed after the registration of pp_rank_to_worker_rref self._initialize_partition() # res_use works for lifecycle counter, # if ref_use is True, lifecycle won't add. # offset supports get partial output to reduce comm costs. def get_output_by_key(self, key: UniqueKey, ref_use=False, rank=None, offsets=None) -> Any: output = self._get_output_all(key, ref_use, rank) if offsets is None: # get all for non iterable output return output else: # get part for iterable output output = [output[i] for i in offsets] return output def get_numels(self) -> int: numel = sum(param.numel() for param in self.module_partition.parameters()) return numel def get_parameters(self) -> List[torch.Tensor]: return [p for p in self.module_partition.parameters()] def get_parameter_gradients(self) -> List[torch.Tensor]: return [p.grad for p in self.module_partition.parameters()] def get_partition(self): with self.partition_condition_lock: self.partition_condition_lock.wait_for(lambda: hasattr(self, "module_partition")) return self.module_partition def get_partition_state_dict(self): with self.partition_condition_lock: self.partition_condition_lock.wait_for(lambda: hasattr(self, "module_partition")) return self.module_partition.state_dict() def _make_args_kwargs(self, microbatch, merge=False): if isinstance(microbatch, dict): if merge: return list(microbatch.values()), {} return [], microbatch elif isinstance(microbatch, torch.Tensor): return [microbatch], {} elif isinstance(microbatch, (tuple, list)): args = [] kwargs = {} for arg in microbatch: if isinstance(arg, dict): kwargs.update(arg) else: args.append(arg) if merge: arg_lst = args for arg in kwargs.values(): arg_lst.append(arg) return arg_lst, {} return args, kwargs else: raise TypeError(f"Input batch can be only dict, list, tuple or tensor, but receive {type(microbatch)}") # just for first pp_rank def set_input(self, microbatch_id: int, microbatch: Tuple[Any], forward_only: bool): key = UniqueKey(microbatch_id, Phase.FORWARD) output = self._get_future_by_device() if not self.use_middleware(): # make args and kwargs args, kwargs = self._make_args_kwargs(microbatch) work_item = WorkItem( self.pp_rank, Phase.FORWARD, args, kwargs, output, microbatch_id, None, self.num_microbatches, forward_only, ) with self.work_list_condition_lock: self.work_list[key] = work_item self.work_list_condition_lock.notify_all() else: # make args and kwargs arg_lst, _ = self._make_args_kwargs(microbatch, merge=True) # first stage assign correct input into other stages topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) input_partition = topo.get_input_partition() self_input_offsets = input_partition.get_output_offsets(self_partition_id) recv_input_key = UniqueKey(microbatch_id, Phase.INPUT) # set input for self rank self_arg_lst = [] for off in self_input_offsets: self_arg_lst.append(arg_lst[off]) work_item = WorkItem( self.pp_rank, Phase.FORWARD, self_arg_lst, {}, output, microbatch_id, None, self.num_microbatches, forward_only, ) with self.work_list_condition_lock: self.work_list[key] = work_item self.work_list_condition_lock.notify_all() # put input tensor which other nodes need into output_list as Phase.INPUT work_item_remote = WorkItem( self.pp_rank, Phase.INPUT, [], {}, arg_lst, microbatch_id, None, self.num_microbatches, forward_only ) with self.output_list_condition_lock: self.output_list[recv_input_key] = work_item_remote self.output_list_condition_lock.notify_all() # just for last pp_rank def set_labels(self, microbatch_id: int, microlabels: Any): with self.label_lock: self.microbatch_id_to_labels[microbatch_id] = microlabels self.label_lock.notify_all() # just for last pp_rank def _begin_backward(self, microbatch_id: int): with self.work_list_condition_lock: assert self.producer_stage_ids is not None key = UniqueKey(microbatch_id, Phase.BACKWARD) output = self._get_future_by_device() grad_wrt_loss = None work_item = WorkItem( self.pp_rank, Phase.BACKWARD, grad_wrt_loss, {}, output, microbatch_id, None, self.num_microbatches, False, ) self.work_list[key] = work_item self.work_list_condition_lock.notify_all() def _subscribe_producer(self, microbatch_id: int, forward_only: bool): """ You should call this function asynchronously """ stage_id = self.pp_rank output = self._get_future_by_device() if not self.use_middleware(): producer_num = len(self.producer_stage_ids) subscribe_forward_futures: List[Future] = [None] * producer_num for i in range(producer_num): producer_stage_id = self.producer_stage_ids[i] producer_output_key = UniqueKey(microbatch_id, Phase.FORWARD) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] subscribe_forward_futures[i] = producer_worker_rref.rpc_async().get_output_by_key(producer_output_key) else: producer_stage_ids = self.get_producer_stage_ids() producer_num = len(producer_stage_ids) if self.need_model_input(): producer_num += 1 # for input partition subscribe_forward_futures: List[Future] = [None] * producer_num # TODO(jiangziyue) get single value instead of the whole output if self.need_model_input(): producer_stage_id = 0 producer_output_key = UniqueKey(microbatch_id, Phase.INPUT) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] offsets = self._get_input_offsets_by_index(target_index=0) subscribe_forward_futures[0] = producer_worker_rref.rpc_async().get_output_by_key( producer_output_key, rank=self.pp_rank, offsets=offsets ) for i in range(0, producer_num - 1): producer_stage_id = producer_stage_ids[i] producer_output_key = UniqueKey(microbatch_id, Phase.FORWARD) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] target_index = i + 1 offsets = self._get_input_offsets_by_index(target_index=target_index) if offsets is not None and len(offsets) == 0: # no need to do rpc subscribe_forward_futures[target_index] = [] else: subscribe_forward_futures[target_index] = producer_worker_rref.rpc_async().get_output_by_key( producer_output_key, rank=self.pp_rank, offsets=offsets ) else: for i in range(producer_num): producer_stage_id = producer_stage_ids[i] producer_output_key = UniqueKey(microbatch_id, Phase.FORWARD) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] target_index = i offsets = self._get_input_offsets_by_index(target_index=target_index) if offsets is not None and len(offsets) == 0: # no need to do rpc subscribe_forward_futures[target_index] = [] else: subscribe_forward_futures[target_index] = producer_worker_rref.rpc_async().get_output_by_key( producer_output_key, rank=self.pp_rank, offsets=offsets ) work_item_from_producer = WorkItem( stage_id, Phase.FORWARD, subscribe_forward_futures, {}, output, microbatch_id, None, self.num_microbatches, forward_only, ) return work_item_from_producer # TODO(jiangziyue) Profile the side effect of the lock for lifecycle protection and consider a better one. def subscribe_producer(self, microbatch_id: int, forward_only: bool): key = UniqueKey(microbatch_id, Phase.FORWARD) with self.work_list_condition_lock: if key not in self.work_list: # On current PP middleware design for DAG, get_output_by_key used by _subscribe_producer # can only be executed once for every producer-consumer stage pair, which is necessary # to count the lifecycle of work_item. So, keeping the _subscribe_producer in the same # lock of work_item queue operation guarantees the consistency of lifecycle counter. work_item_from_producer = self._subscribe_producer(microbatch_id, forward_only) self.work_list[key] = work_item_from_producer self.work_list_condition_lock.notify_all() def _subscribe_consumer(self, microbatch_id: int): """ You should call this function asynchronously """ stage_id = self.pp_rank output = self._get_future_by_device() if not self.use_middleware(): consumer_stage_ids = self.consumer_stage_ids else: consumer_stage_ids = self.get_consumer_stage_ids() consumer_num = len(consumer_stage_ids) subscribe_backward_futures: List[Future] = [None] * consumer_num for i in range(consumer_num): consumer_stage_id = consumer_stage_ids[i] consumer_output_key = UniqueKey(microbatch_id, Phase.BACKWARD) consumer_worker_rref = self.pp_rank_to_worker_rref[consumer_stage_id] target_index = i offsets = self._get_output_offsets_by_index(target_index=target_index) if offsets is not None and len(offsets) == 0: # no need to do rpc subscribe_backward_futures[target_index] = [] else: subscribe_backward_futures[target_index] = consumer_worker_rref.rpc_async().get_output_by_key( consumer_output_key, rank=self.pp_rank, offsets=offsets ) # flatten args work_item_from_consumer = WorkItem( stage_id, Phase.BACKWARD, subscribe_backward_futures, {}, output, microbatch_id, None, self.num_microbatches, False, ) return work_item_from_consumer def subscribe_consumer(self, microbatch_id: int): key = UniqueKey(microbatch_id, Phase.BACKWARD) with self.work_list_condition_lock: if key not in self.work_list: # On current PP middleware design for DAG, get_output_by_key used by subscribe_consumer # can only be executed once for every producer-consumer stage pair, which is necessary # to count the lifecycle of work_item. So, keeping the subscribe_consumer in the same # lock of work_item queue operation guarantees the consistency of lifecycle counter. work_item_from_consumer = self._subscribe_consumer(microbatch_id) self.work_list[key] = work_item_from_consumer self.work_list_condition_lock.notify_all() def get_producer_stage_ids(self): producer_stage_ids = [] rank = self.pp_rank if not self.use_middleware(): prev_rank = rank - 1 if prev_rank >= 0: producer_stage_ids.append(prev_rank) else: topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) input_partition_ids = self_partition.get_input_partition_ids() model_input_partition_id = topo.get_input_partition_id() for partition_id in input_partition_ids: # ignore input partition in current implementation. # it will be specially tackled. if partition_id != model_input_partition_id: producer_stage_ids.append(self.partition_id_to_pp_rank(partition_id, topo)) return producer_stage_ids def get_consumer_stage_ids(self): consumer_stage_ids = [] rank = self.pp_rank if not self.use_middleware(): next_rank = rank + 1 if next_rank <= self.actual_stage_num - 1: consumer_stage_ids.append(next_rank) else: topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) output_partition_ids = self_partition.get_output_partition_ids() model_output_partition_id = topo.get_output_partition_id() for partition_id in output_partition_ids: if model_output_partition_id != partition_id: consumer_stage_ids.append(self.partition_id_to_pp_rank(partition_id, topo)) return consumer_stage_ids def _get_producer_consumer(self) -> None: rank = self.pp_rank assert self.producer_stage_ids is None, f"all the producers of rank {rank} has been subscribed" assert self.consumer_stage_ids is None, f"all the consumers of rank {rank} has been subscribed" # should be arranged in order, the order of the input of current forward self.producer_stage_ids = self.get_producer_stage_ids() self.consumer_stage_ids = self.get_consumer_stage_ids() def pp_rank_to_partition_id(self, pp_rank: int, topo: Topo): partition_ids = topo.get_mid_partition_ids() return partition_ids[pp_rank] def partition_id_to_pp_rank(self, partition_id: int, topo: Topo): partition_ids = topo.get_mid_partition_ids() for i, id in enumerate(partition_ids): if id == partition_id: return i def get_topo(self): with self.partition_condition_lock: self.partition_condition_lock.wait_for(lambda: hasattr(self, "module_partition")) if hasattr(self.module_partition, "_topo"): return self.module_partition._topo else: return None def use_middleware(self): topo = self.get_topo() return topo is not None def _get_input_offsets_by_index(self, target_index): res = [] topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) model_input_partition_id = topo.get_input_partition_id() input_vals = self_partition.get_input_vals() producer_stage_ids = self.get_producer_stage_ids() if self.need_model_input(): # 0 for data from input batch # >= 1 for data from prev stages base = 1 else: # data from prev stages base = 0 for val in input_vals: val_pos = val.get() src_partition_id = val_pos.partition_id src_offset = val_pos.offset src_index = base src_partition = topo.get_partition_by_id(src_partition_id) output_len = len(src_partition.get_output_vals()) # data from not-input partition if src_partition_id != model_input_partition_id: src_stage_id = self.partition_id_to_pp_rank(src_partition_id, topo) src_index = base for i, stage_id in enumerate(producer_stage_ids): if stage_id == src_stage_id: src_index += i break else: # data from input partition src_index = 0 # when output_len = 1, not iterable if target_index == src_index: if output_len == 1: res = None # offset = None to get all outputs return res else: res.append(src_offset) return res def _get_output_offsets_by_index(self, target_index): res = [] topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) output_vals = self_partition.get_output_vals() consumer_stage_ids = self.get_consumer_stage_ids() for val_list in output_vals: # An output may be passed to many down stages. for val_pos in val_list.get(): dst_partition_id = val_pos.partition_id dst_offset = val_pos.offset dst_partition = topo.get_partition_by_id(dst_partition_id) input_len = len(dst_partition.get_input_vals()) dst_stage_id = self.partition_id_to_pp_rank(dst_partition_id, topo) for i, stage_id in enumerate(consumer_stage_ids): if stage_id == dst_stage_id: dst_index = i break if target_index == dst_index: if input_len == 1: res = None # offset = None to get all outputs return res else: res.append(dst_offset) return res # TODO(jiangziyue) get single value instead of the whole output def _get_real_args_kwargs_fwd(self, args_or_kwargs): if not self.use_middleware(): args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) args_or_kwargs = flatten_args else: args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] if self.is_first_stage(): pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) else: # get by offset topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) model_input_partition_id = topo.get_input_partition_id() input_vals = self_partition.get_input_vals() producer_stage_ids = self.get_producer_stage_ids() if self.need_model_input(): # 0 for data from input batch # >= 1 for data from prev stages base = 1 else: # data from prev stages base = 0 for val in input_vals: val_pos = val.get() src_partition_id = val_pos.partition_id src_offset = val_pos.offset src_index = base src_partition = topo.get_partition_by_id(src_partition_id) output_len = len(src_partition.get_output_vals()) # data from not-input partition if src_partition_id != model_input_partition_id: src_stage_id = self.partition_id_to_pp_rank(src_partition_id, topo) src_index = base for i, stage_id in enumerate(producer_stage_ids): if stage_id == src_stage_id: src_index += i break else: # data from input partition src_index = 0 # when output_len = 1, not iterable if output_len == 1: target = args_or_kwargs[src_index] else: offsets = self._get_input_offsets_by_index(src_index) real_offset = offsets.index(src_offset) target = args_or_kwargs[src_index][real_offset] flatten_args.append(target) args_or_kwargs = flatten_args return args_or_kwargs # TODO(jiangziyue) get single value instead of the whole output def _get_real_args_kwargs_bwd(self, args_or_kwargs): if not self.use_middleware(): args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) args_or_kwargs = flatten_args else: for i, arg in enumerate(args_or_kwargs): args_or_kwargs[i] = arg.wait() if args_or_kwargs is not None: # get by offset flatten_args = [] topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) output_vals = self_partition.get_output_vals() consumer_stage_ids = self.get_consumer_stage_ids() for val_list in output_vals: # An output may be passed to many down stages. target = None for val_pos in val_list.get(): dst_partition_id = val_pos.partition_id dst_offset = val_pos.offset dst_partition = topo.get_partition_by_id(dst_partition_id) input_len = len(dst_partition.get_input_vals()) dst_stage_id = self.partition_id_to_pp_rank(dst_partition_id, topo) for i, stage_id in enumerate(consumer_stage_ids): if stage_id == dst_stage_id: dst_index = i break if input_len == 1: part_grad = args_or_kwargs[dst_index] else: offsets = self._get_output_offsets_by_index(dst_index) real_offsets = offsets.index(dst_offset) part_grad = args_or_kwargs[dst_index][real_offsets] if target is None: target = part_grad elif part_grad is not None: target += part_grad
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
true
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/legacy/pipeline/rpc/__init__.py
colossalai/legacy/pipeline/rpc/__init__.py
from ._pipeline_schedule import ChimeraPipelineEngine, FillDrainPipelineEngine, OneFOneBPipelineEngine from .utils import pytree_map __all__ = ["FillDrainPipelineEngine", "OneFOneBPipelineEngine", "ChimeraPipelineEngine", "pytree_map"]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/__init__.py
colossalai/nn/__init__.py
from .init import * from .layer import * from .loss import * from .lr_scheduler import * from .optimizer import *
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/init.py
colossalai/nn/init.py
import math import warnings import torch.nn as nn from torch import Tensor def zeros_(): """Return the initializer filling the input Tensor with the scalar zeros""" def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.zeros_(tensor) return initializer def ones_(): """Return the initializer filling the input Tensor with the scalar ones""" def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.ones_(tensor) return initializer def uniform_(a: float = 0.0, b: float = 1.0): r"""Return the initializer filling the input Tensor with values drawn from the uniform distribution :math:`\mathcal{U}(a, b)`. Args: a (float): the lower bound of the uniform distribution. Defaults 0.0. b (float): the upper bound of the uniform distribution. Defaults 1.0. """ def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.uniform_(tensor, a, b) return initializer def normal_(mean: float = 0.0, std: float = 1.0): r"""Return the initializer filling the input Tensor with values drawn from the normal distribution .. math:: \mathcal{N}(\text{mean}, \text{std}^2) Args: mean (float): the mean of the normal distribution. Defaults 0.0. std (float): the standard deviation of the normal distribution. Defaults 1.0. """ def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.normal_(tensor, mean, std) return initializer def trunc_normal_(mean: float = 0.0, std: float = 1.0, a: float = -2.0, b: float = 2.0): r"""Return the initializer filling the input Tensor with values drawn from a truncated normal distribution. The values are effectively drawn from the normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)` with values outside :math:`[a, b]` redrawn until they are within the bounds. The method used for generating the random values works best when :math:`a \leq \text{mean} \leq b`. Args: mean (float): the mean of the normal distribution. Defaults 0.0. std (float): the standard deviation of the normal distribution. Defaults 1.0. a (float): the minimum cutoff value. Defaults -2.0. b (float): the maximum cutoff value. Defaults 2.0. """ def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.trunc_normal_(tensor, mean, std, a, b) return initializer def kaiming_uniform_(a=0, mode="fan_in", nonlinearity="leaky_relu"): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification` - He, K. et al. (2015), using a uniform distribution. The resulting tensor will have values sampled from :math:`\mathcal{U}(-\text{bound}, \text{bound})` where .. math:: \text{bound} = \text{gain} \times \sqrt{\frac{3}{\text{fan_mode}}} Also known as 'He initialization'. Args: a (int): the negative slope of the rectifier used after this layer (only used with ``'leaky_relu'``). mode (str, optional): either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'`` preserves the magnitude of the variance of the weights in the forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the backwards pass. nonlinearity (str, optional): the non-linear function (`nn.functional` name), recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default). """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): if 0 in tensor.shape: warnings.warn("Initializing zero-element tensors is a no-op") return tensor if mode == "fan_in": assert fan_in is not None, "Fan_in is not provided." fan = fan_in elif mode == "fan_out": assert fan_out is not None, "Fan_out is not provided." fan = fan_out else: raise ValueError(f"Invalid initialization mode '{mode}'") std = nn.init.calculate_gain(nonlinearity, a) / math.sqrt(fan) bound = math.sqrt(3.0) * std return nn.init.uniform_(tensor, -bound, bound) return initializer def kaiming_normal_(a=0, mode="fan_in", nonlinearity="leaky_relu"): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification` - He, K. et al. (2015), using a normal distribution. The resulting tensor will have values sampled from :math:`\mathcal{N}(0, \text{std}^2)` where .. math:: \text{std} = \frac{\text{gain}}{\sqrt{\text{fan_mode}}} Also known as 'He initialization'. Args: a (int): the negative slope of the rectifier used after this layer (only used with ``'leaky_relu'``). mode (str, optional): either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'`` preserves the magnitude of the variance of the weights in the forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the backwards pass. nonlinearity (str, optional): the non-linear function (`nn.functional` name), recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default). """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): if 0 in tensor.shape: warnings.warn("Initializing zero-element tensors is a no-op") return tensor if mode == "fan_in": assert fan_in is not None, "Fan_in is not provided." fan = fan_in elif mode == "fan_out": assert fan_out is not None, "Fan_out is not provided." fan = fan_out else: raise ValueError(f"Invalid initialization mode '{mode}'") std = nn.init.calculate_gain(nonlinearity, a) / math.sqrt(fan) return nn.init.normal_(tensor, 0, std) return initializer def xavier_uniform_(a: float = math.sqrt(3.0), scale: float = 2.0, gain: float = 1.0): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Understanding the difficulty of training deep feedforward neural networks` - Glorot, X. & Bengio, Y. (2010), using a uniform distribution. The resulting tensor will have values sampled from :math:`\mathcal{U}(-a, a)` where .. math:: a = \text{gain} \times \sqrt{\frac{6}{\text{fan_in} + \text{fan_out}}} Also known as 'Glorot initialization'. Args: a (float, optional): an optional scaling factor used to calculate uniform bounds from standard deviation. Defaults ``math.sqrt(3.)``. scale (float, optional): an optional scaling factor used to calculate standard deviation. Defaults 2.0. gain (float, optional): an optional scaling factor. Defaults 1.0. """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, "Fan_in is not provided." fan = fan_in if fan_out is not None: fan += fan_out std = gain * math.sqrt(scale / float(fan)) bound = a * std return nn.init.uniform_(tensor, -bound, bound) return initializer def xavier_normal_(scale: float = 2.0, gain: float = 1.0): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Understanding the difficulty of training deep feedforward neural networks` - Glorot, X. & Bengio, Y. (2010), using a normal distribution. The resulting tensor will have values sampled from :math:`\mathcal{N}(0, \text{std}^2)` where .. math:: \text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan_in} + \text{fan_out}}} Also known as 'Glorot initialization'. Args: scale (float, optional): an optional scaling factor used to calculate standard deviation. Defaults 2.0. gain (float, optional): an optional scaling factor. Defaults 1.0. """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, "Fan_in is not provided." fan = fan_in if fan_out is not None: fan += fan_out std = gain * math.sqrt(scale / float(fan)) return nn.init.normal_(tensor, 0.0, std) return initializer def lecun_uniform_(): # adapted from jax.nn.initializers def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, "Fan_in is not provided." var = 1.0 / fan_in bound = math.sqrt(3 * var) return nn.init.uniform_(tensor, -bound, bound) return initializer def lecun_normal_(): # adapted from jax.nn.initializers def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, "Fan_in is not provided." std = math.sqrt(1.0 / fan_in) return nn.init.trunc_normal_(tensor, std=std / 0.87962566103423978) return initializer
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/loss/__init__.py
colossalai/nn/loss/__init__.py
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/layer/layernorm.py
colossalai/nn/layer/layernorm.py
"""This code is from NVIDIA apex: https://github.com/NVIDIA/apex with some changes. """ import numbers import torch from torch.cuda.amp import custom_bwd, custom_fwd from torch.nn import init from torch.nn.parameter import Parameter from colossalai.kernel.kernel_loader import LayerNormLoader try: from colossalai._C import layer_norm except ImportError: layer_norm = None class FusedLayerNormAffineFunction(torch.autograd.Function): @staticmethod @custom_fwd(cast_inputs=torch.float32) def forward(ctx, input, weight, bias, normalized_shape, eps): ctx.normalized_shape = normalized_shape ctx.eps = eps input_ = input.contiguous() weight_ = weight.contiguous() bias_ = bias.contiguous() global layer_norm if layer_norm is None: layer_norm = LayerNormLoader().load() output, mean, invvar = layer_norm.forward_affine(input_, ctx.normalized_shape, weight_, bias_, ctx.eps) ctx.layernorm_op = layer_norm ctx.save_for_backward(input_, weight_, bias_, mean, invvar) return output @staticmethod @custom_bwd def backward(ctx, grad_output): input_, weight_, bias_, mean, invvar = ctx.saved_tensors grad_input = grad_weight = grad_bias = None grad_input, grad_weight, grad_bias = layer_norm.backward_affine( grad_output.contiguous(), mean, invvar, input_, ctx.normalized_shape, weight_, bias_, ctx.eps ) return grad_input, grad_weight, grad_bias, None, None class MixedFusedLayerNorm(torch.nn.Module): def __init__(self, normalized_shape, eps=1e-5, device=None, dtype=None): super(MixedFusedLayerNorm, self).__init__() if isinstance(normalized_shape, numbers.Integral): normalized_shape = (normalized_shape,) self.normalized_shape = torch.Size(normalized_shape) self.eps = eps self.weight = Parameter(torch.empty(*normalized_shape, device=device, dtype=dtype)) self.bias = Parameter(torch.empty(*normalized_shape, device=device, dtype=dtype)) self.reset_parameters() def reset_parameters(self): init.ones_(self.weight) init.zeros_(self.bias) def forward(self, input): return FusedLayerNormAffineFunction.apply(input, self.weight, self.bias, self.normalized_shape, self.eps) def __repr__(self): return f"MixedFusedLayerNorm(normalized_shape={self.normalized_shape}, eps={self.eps})"
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/layer/utils.py
colossalai/nn/layer/utils.py
def divide(numerator, denominator): """Only allow exact division. Args: numerator (int): Numerator of the division. denominator (int): Denominator of the division. Returns: int: the result of exact division. """ assert denominator != 0, "denominator can not be zero" assert numerator % denominator == 0, "{} is not divisible by {}".format(numerator, denominator) return numerator // denominator
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/layer/__init__.py
colossalai/nn/layer/__init__.py
from .utils import *
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/layer/scaled_softmax.py
colossalai/nn/layer/scaled_softmax.py
# This code from NVIDIA Megatron: # with minor changes. import enum import torch import torch.nn as nn from colossalai.kernel.kernel_loader import ScaledMaskedSoftmaxLoader, ScaledUpperTriangleMaskedSoftmaxLoader # NOTE: These kernels are compiled on specific GPU arch and not widely applicable. # try: # from colossalai._C import scaled_masked_softmax as scaled_masked_softmax, scaled_upper_triangle_masked_softmax_cuda as scaled_upper_triang_masked_softmax # except ImportError: scaled_masked_softmax = None scaled_upper_triang_masked_softmax = None class AttnMaskType(enum.Enum): padding = 1 causal = 2 paddedcausal = 3 class ScaledUpperTriangMaskedSoftmax(torch.autograd.Function): """ Fused operation which performs following three operations in sequence 1. Scale the tensor. 2. Apply upper triangular mask (typically used in gpt models). 3. Perform softmax. """ @staticmethod def forward(ctx, inputs, scale): global scaled_upper_triang_masked_softmax if scaled_upper_triang_masked_softmax: scaled_upper_triang_masked_softmax = ScaledUpperTriangleMaskedSoftmaxLoader().load() scale_t = torch.tensor([scale]) softmax_results = scaled_upper_triang_masked_softmax.forward(inputs, scale_t[0]) ctx.save_for_backward(softmax_results, scale_t) return softmax_results @staticmethod def backward(ctx, output_grads): softmax_results, scale_t = ctx.saved_tensors input_grads = scaled_upper_triang_masked_softmax.backward(output_grads, softmax_results, scale_t[0]) return input_grads, None class ScaledMaskedSoftmax(torch.autograd.Function): """ Fused operation which performs following three operations in sequence 1. Scale the tensor. 2. Apply the mask. 3. Perform softmax. """ @staticmethod def forward(ctx, inputs, mask, scale): scale_t = torch.tensor([scale]) # build and load kernel if not pre-built global scaled_masked_softmax if scaled_masked_softmax is None: scaled_masked_softmax = ScaledMaskedSoftmaxLoader().load() softmax_results = scaled_masked_softmax.forward(inputs, mask, scale_t[0]) ctx.save_for_backward(softmax_results, scale_t) return softmax_results @staticmethod def backward(ctx, output_grads): softmax_results, scale_t = ctx.saved_tensors input_grads = scaled_masked_softmax.backward(output_grads, softmax_results, scale_t[0]) return input_grads, None, None, None class FusedScaleMaskSoftmax(nn.Module): """ Fused operation: scaling + mask + softmax Arguments: input_in_fp16: Flag to indicate if input in fp16 data format. input_in_bf16: Flag to indicate if input in bf16 data format. attn_mask_type: Attention mask type (pad or causal) scaled_masked_softmax_fusion: Flag to indicate user want to use softmax fusion mask_func: Mask function to be applied. softmax_in_fp32: If True, softmax in performed at fp32 precision. scale: Scaling factor used in input tensor scaling. """ def __init__( self, input_in_fp16, input_in_bf16, attn_mask_type, scaled_masked_softmax_fusion, mask_func, softmax_in_fp32, scale, ): super(FusedScaleMaskSoftmax, self).__init__() self.input_in_fp16 = input_in_fp16 self.input_in_bf16 = input_in_bf16 assert not ( self.input_in_fp16 and self.input_in_bf16 ), "both fp16 and bf16 flags cannot be active at the same time." self.input_in_float16 = self.input_in_fp16 or self.input_in_bf16 self.attn_mask_type = attn_mask_type self.scaled_masked_softmax_fusion = scaled_masked_softmax_fusion self.mask_func = mask_func self.softmax_in_fp32 = softmax_in_fp32 self.scale = scale assert self.scale is None or softmax_in_fp32, "softmax should be in fp32 when scaled" def forward(self, input, mask): # [b, np, sq, sk] assert input.dim() == 4 if self.is_kernel_available(mask, *input.size()): return self.forward_fused_softmax(input, mask) else: return self.forward_torch_softmax(input, mask) def is_kernel_available(self, mask, b, np, sq, sk): attn_batches = b * np if ( self.scaled_masked_softmax_fusion # user want to fuse and self.input_in_float16 # input must be fp16 and mask is not None # mask tensor must not be None and 16 < sk <= 2048 # sk must be 16 ~ 2048 and sq % 4 == 0 # sq must be divisor of 4 and attn_batches % 4 == 0 # np * b must be divisor of 4 ): if 0 <= sk <= 2048: batch_per_block = self.get_batch_per_block(sq, sk, b, np) if self.attn_mask_type.value > 1: if attn_batches % batch_per_block == 0: return True else: if sq % batch_per_block == 0: return True return False def forward_fused_softmax(self, input, mask): b, np, sq, sk = input.size() scale = self.scale if self.scale is not None else 1.0 if self.attn_mask_type.value > 1: assert sq == sk, "causal mask is only for self attention" # input is 3D tensor (attn_batches, sq, sk) input = input.view(-1, sq, sk) probs = ScaledUpperTriangMaskedSoftmax.apply(input, scale) return probs.view(b, np, sq, sk) else: # input is 4D tensor (b, np, sq, sk) return ScaledMaskedSoftmax.apply(input, mask, scale) def forward_torch_softmax(self, input, mask): if self.input_in_float16 and self.softmax_in_fp32: input = input.float() if self.scale is not None: input = input * self.scale mask_output = self.mask_func(input, mask) if mask is not None else input probs = torch.nn.Softmax(dim=-1)(mask_output) if self.input_in_float16 and self.softmax_in_fp32: if self.input_in_fp16: probs = probs.half() else: probs = probs.bfloat16() return probs def get_batch_per_block(self, sq, sk, b, np): # build and load kernel if not pre-built global scaled_masked_softmax if scaled_masked_softmax is None: scaled_masked_softmax = ScaledMaskedSoftmaxLoader().load() return scaled_masked_softmax.get_batch_per_block(sq, sk, b, np)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/onecycle.py
colossalai/nn/lr_scheduler/onecycle.py
from torch.optim.lr_scheduler import OneCycleLR as _OneCycleLR class OneCycleLR(_OneCycleLR): r"""Sets the learning rate of each parameter group according to the 1cycle learning rate policy. The 1cycle policy anneals the learning rate from an initial learning rate to some maximum learning rate and then from that maximum learning rate to some minimum learning rate much lower than the initial learning rate. This policy was initially described in the paper `Super-Convergence: Very Fast Training of Neural Networks Using Large Learning Rates`_. The 1cycle learning rate policy changes the learning rate after every batch. `step` should be called after a batch has been used for training. This scheduler is not chainable. Note also that the total number of steps in the cycle can be determined in one of two ways (listed in order of precedence): * A value for total_steps is explicitly provided. * A number of epochs (epochs) and a number of steps per epoch (steps_per_epoch) are provided. In this case, the number of total steps is inferred by total_steps = epochs * steps_per_epoch You must either provide a value for total_steps or provide a value for both epochs and steps_per_epoch. The default behaviour of this scheduler follows the fastai implementation of 1cycle, which claims that "unpublished work has shown even better results by using only two phases". To mimic the behaviour of the original paper instead, set ``three_phase=True``. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. pct_start (float, optional): The percentage of the cycle (in number of steps) spent increasing the learning rate, defaults to 0.3. anneal_strategy (str, optional): {'cos', 'linear'}, Specifies the annealing strategy: "cos" for cosine annealing, "linear" for linear annealing, defaults to 'cos'. cycle_momentum (bool, optional): If ``True``, momentum is cycled inversely to learning rate between 'base_momentum' and 'max_momentum', defaults to True. base_momentum (float, optional): Lower momentum boundaries in the cycle for each parameter group. Note that momentum is cycled inversely to learning rate; at the peak of a cycle, momentum is 'base_momentum' and learning rate is 'max_lr', defaults to 0.85. max_momentum (float, optional): Upper momentum boundaries in the cycle for each parameter group. Functionally, it defines the cycle amplitude (max_momentum - base_momentum). Note that momentum is cycled inversely to learning rate; at the start of a cycle, momentum is 'max_momentum' and learning rate is 'base_lr', defaults to 0.95. div_factor (float, optional): Determines the initial learning rate via initial_lr = max_lr/div_factor, defaults to 25.0. final_div_factor (float, optional): Determines the minimum learning rate via min_lr = initial_lr/final_div_factor, defaults to 10000.0. last_epoch (int, optional): The index of the last batch. This parameter is used when resuming a training job. Since `step()` should be invoked after each batch instead of after each epoch, this number represents the total number of *batches* computed, not the total number of epochs computed. When last_epoch=-1, the schedule is started from the beginning, defaults to -1 The ``kwargs`` for initializing torch.optim.lr_scheduler.OneCycleLR should include parameters below: :: epochs (int, optional, default=None) steps_per_epoch (int, optional, default=None) three_phase (bool, optional, default=False) verbose (bool, optional, default=False) More details about kwargs could be found in `OneCycleLR <https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.OneCycleLR.html#torch.optim.lr_scheduler.OneCycleLR>`_. .. _Super-Convergence\: Very Fast Training of Neural Networks Using Large Learning Rates: https://arxiv.org/abs/1708.07120 """ def __init__( self, optimizer, total_steps: int, pct_start=0.3, anneal_strategy="cos", cycle_momentum=True, base_momentum=0.85, max_momentum=0.95, div_factor=25.0, final_div_factor=10000.0, last_epoch=-1, **kwargs, ): max_lrs = list(map(lambda group: group["lr"], optimizer.param_groups)) super().__init__( optimizer, max_lrs, total_steps=total_steps, pct_start=pct_start, anneal_strategy=anneal_strategy, cycle_momentum=cycle_momentum, base_momentum=base_momentum, max_momentum=max_momentum, div_factor=div_factor, final_div_factor=final_div_factor, last_epoch=last_epoch, )
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/multistep.py
colossalai/nn/lr_scheduler/multistep.py
from typing import List from torch.optim.lr_scheduler import MultiStepLR as _MultiStepLR from .delayed import WarmupScheduler class MultiStepLR(_MultiStepLR): """Decays the learning rate of each parameter group by gamma once the number of epoch reaches one of the milestones. Notice that such decay can happen simultaneously with other changes to the learning rate from outside this scheduler. When last_epoch=-1, sets initial lr as lr. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. milestones (List[int], optional): List of epoch indices. Must be increasing, defaults to None. gamma (float, optional): Multiplicative factor of learning rate decay, defaults to 0.1. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__( self, optimizer, total_steps: int, milestones: List[int] = None, gamma: float = 0.1, last_epoch: int = -1, **kwargs, ): super().__init__(optimizer, milestones, gamma=gamma, last_epoch=last_epoch) class MultiStepWarmupLR(WarmupScheduler): """Multistep learning rate scheduler with warmup. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. warmup_steps (int, optional): Number of warmup steps, defaults to 0. milestones (List[int], optional): List of epoch indices. Must be increasing, defaults to None. gamma (float, optional): Multiplicative factor of learning rate decay, defaults to 0.1. num_steps_per_epoch (int, optional): Number of steps per epoch, defaults to -1. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__( self, optimizer, total_steps: int, warmup_steps: int = 0, milestones: List[int] = None, gamma: float = 0.1, last_epoch: int = -1, **kwargs, ): if len(milestones) == 0: raise ValueError("milestones cannot be empty") milestones = [v - warmup_steps for v in milestones if v >= warmup_steps] base_scheduler = _MultiStepLR(optimizer, milestones=milestones, gamma=gamma) super().__init__(optimizer, warmup_steps, base_scheduler, last_epoch=last_epoch)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/delayed.py
colossalai/nn/lr_scheduler/delayed.py
import torch from packaging.version import Version if Version(torch.__version__) >= Version("2.0.0"): from torch.optim.lr_scheduler import LRScheduler as _LRScheduler else: from torch.optim.lr_scheduler import _LRScheduler from colossalai.logging import get_dist_logger class _enable_get_lr_call: def __init__(self, o): self.o = o def __enter__(self): self.o._get_lr_called_within_step = True return self def __exit__(self, type, value, traceback): self.o._get_lr_called_within_step = False class TwoStageScheduler(_LRScheduler): def __init__(self, optimizer, after_scheduler: _LRScheduler, last_epoch=-1): self.after_scheduler = after_scheduler self.finished = False super().__init__(optimizer, last_epoch) def state_dict(self): state_dict = {key: value for key, value in self.__dict__.items() if key not in "optimizer"} if isinstance(state_dict["after_scheduler"], _LRScheduler): state_dict["after_scheduler_type"] = type(state_dict["after_scheduler"]).__name__ state_dict["after_scheduler_dict"] = state_dict["after_scheduler"].state_dict() del state_dict["after_scheduler"] else: raise NotImplementedError() return state_dict def load_state_dict(self, state_dict): if "after_scheduler_dict" not in state_dict: logger = get_dist_logger() logger.warning( "after_scheduler_dict is not found, skip loading after_scheduler. This may cause unexpected behavior." ) else: self.after_scheduler.load_state_dict(state_dict["after_scheduler_dict"]) state_dict = { key: value for key, value in state_dict.items() if key not in ("after_scheduler_type", "after_scheduler_dict") } super().load_state_dict(state_dict) class DelayerScheduler(TwoStageScheduler): """Starts with a flat lr schedule until it reaches N epochs then applies the specific scheduler (For example: ReduceLROnPlateau) Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. delay_epochs (int): Number of epochs to keep the initial lr until starting applying the scheduler. after_scheduler (:class:`torch.optim.lr_scheduler`): After target_epoch, use this scheduler. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__(self, optimizer, delay_epochs, after_scheduler, last_epoch=-1): if delay_epochs < 0: raise ValueError(f"delay_epochs must >= 0, got {delay_epochs}") self.delay_epochs = delay_epochs super().__init__(optimizer, after_scheduler, last_epoch) def get_lr(self): if self.last_epoch >= self.delay_epochs: if not self.finished: self.after_scheduler.base_lrs = self.base_lrs self.finished = True with _enable_get_lr_call(self.after_scheduler): return self.after_scheduler.get_lr() return self.base_lrs def step(self, epoch=None): if self.finished: if epoch is None: self.after_scheduler.step(None) self._last_lr = self.after_scheduler.get_last_lr() else: self.after_scheduler.step(epoch - self.delay_epochs) self._last_lr = self.after_scheduler.get_last_lr() else: return super(DelayerScheduler, self).step(epoch) class WarmupScheduler(TwoStageScheduler): """Starts with a linear warmup lr schedule until it reaches N epochs then applies the specific scheduler (For example: ReduceLROnPlateau). Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. warmup_epochs (int): Number of epochs to linearly warmup lr until starting applying the scheduler. after_scheduler (:class:`torch.optim.lr_scheduler`): After target_epoch, use this scheduler. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__(self, optimizer, warmup_epochs, after_scheduler, last_epoch=-1): self.warmup_epochs = int(warmup_epochs) super().__init__(optimizer, after_scheduler, last_epoch) def get_lr(self): if self.last_epoch >= self.warmup_epochs: if not self.finished: self.after_scheduler.base_lrs = self.base_lrs self.finished = True return self.after_scheduler.get_lr() return [(self.last_epoch + 1) / self.warmup_epochs * lr for lr in self.base_lrs] def step(self, epoch=None): if self.finished: if epoch is None: self.after_scheduler.step(None) self._last_lr = self.after_scheduler.get_last_lr() else: self.after_scheduler.step(epoch - self.warmup_epochs) self._last_lr = self.after_scheduler.get_last_lr() else: return super().step(epoch) class WarmupDelayerScheduler(TwoStageScheduler): """Starts with a linear warmup lr schedule until it reaches N epochs and a flat lr schedule until it reaches M epochs then applies the specific scheduler (For example: ReduceLROnPlateau). Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. warmup_epochs (int): Number of epochs to linearly warmup lr until starting applying the scheduler. delay_epochs (int): Number of epochs to keep the initial lr until starting applying the scheduler. after_scheduler (:class:`torch.optim.lr_scheduler`): After target_epoch, use this scheduler. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__(self, optimizer, warmup_epochs, delay_epochs, after_scheduler, last_epoch=-1): if delay_epochs < 0: raise ValueError(f"delay_epochs must >= 0, got {delay_epochs}") if warmup_epochs < 0: raise ValueError(f"warmup_epochs must >= 0, got {warmup_epochs}") self.warmup_epochs = warmup_epochs self.delay_epochs = delay_epochs super().__init__(optimizer, after_scheduler, last_epoch) def get_lr(self): if self.last_epoch >= self.warmup_epochs + self.delay_epochs: if not self.finished: self.after_scheduler.base_lrs = self.base_lrs # reset lr to base_lr for group, base_lr in zip(self.optimizer.param_groups, self.base_lrs): group["lr"] = base_lr self.finished = True with _enable_get_lr_call(self.after_scheduler): return self.after_scheduler.get_lr() elif self.last_epoch >= self.warmup_epochs: return self.base_lrs return [(self.last_epoch + 1) / self.warmup_epochs * lr for lr in self.base_lrs] def step(self, epoch=None): if self.finished: if epoch is None: self.after_scheduler.step(None) self._last_lr = self.after_scheduler.get_last_lr() else: self.after_scheduler.step(epoch - self.warmup_epochs) self._last_lr = self.after_scheduler.get_last_lr() else: return super().step(epoch)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/__init__.py
colossalai/nn/lr_scheduler/__init__.py
from .cosine import CosineAnnealingLR, CosineAnnealingWarmupLR, FlatAnnealingLR, FlatAnnealingWarmupLR from .linear import LinearWarmupLR from .multistep import MultiStepLR, MultiStepWarmupLR from .onecycle import OneCycleLR from .poly import PolynomialLR, PolynomialWarmupLR from .torch import ExponentialLR, LambdaLR, MultiplicativeLR, StepLR __all__ = [ "CosineAnnealingLR", "CosineAnnealingWarmupLR", "FlatAnnealingLR", "FlatAnnealingWarmupLR", "LinearWarmupLR", "MultiStepLR", "MultiStepWarmupLR", "OneCycleLR", "PolynomialLR", "PolynomialWarmupLR", "LambdaLR", "MultiplicativeLR", "StepLR", "ExponentialLR", ]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/cosine.py
colossalai/nn/lr_scheduler/cosine.py
from torch.optim.lr_scheduler import CosineAnnealingLR as _CosineAnnealingLR from .delayed import DelayerScheduler, WarmupDelayerScheduler, WarmupScheduler class CosineAnnealingLR(_CosineAnnealingLR): r"""Set the learning rate of each parameter group using a cosine annealing schedule, where :math:`\eta_{max}` is set to the initial lr and :math:`T_{cur}` is the number of epochs since the last restart in SGDR: .. math:: \begin{aligned} \eta_t & = \eta_{min} + \frac{1}{2}(\eta_{max} - \eta_{min})\left(1 + \cos\left(\frac{T_{cur}}{T_{max}}\pi\right)\right), & T_{cur} \neq (2k+1)T_{max}; \\ \eta_{t+1} & = \eta_{t} + \frac{1}{2}(\eta_{max} - \eta_{min}) \left(1 - \cos\left(\frac{1}{T_{max}}\pi\right)\right), & T_{cur} = (2k+1)T_{max}. \end{aligned} When last_epoch=-1, sets initial lr as lr. Notice that because the schedule is defined recursively, the learning rate can be simultaneously modified outside this scheduler by other operators. If the learning rate is set solely by this scheduler, the learning rate at each step becomes: .. math:: \eta_t = \eta_{min} + \frac{1}{2}(\eta_{max} - \eta_{min})\left(1 + \cos\left(\frac{T_{cur}}{T_{max}}\pi\right)\right) It has been proposed in `SGDR: Stochastic Gradient Descent with Warm Restarts`_. Note that this only implements the cosine annealing part of SGDR, and not the restarts. .. _SGDR\: Stochastic Gradient Descent with Warm Restarts: https://arxiv.org/abs/1608.03983 Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. eta_min (int, optional): Minimum learning rate, defaults to 0. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__(self, optimizer, total_steps: int, eta_min: int = 0, last_epoch: int = -1, **kwargs): super().__init__(optimizer, total_steps, eta_min=eta_min, last_epoch=last_epoch) class CosineAnnealingWarmupLR(WarmupScheduler): """Cosine annealing learning rate scheduler with learning rate warmup. A linear warmup schedule will be applied. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. warmup_steps (int, optional): Number of warmup steps, defaults to 0. eta_min (int, optional): Minimum learning rate, defaults to 0. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__(self, optimizer, total_steps: int, warmup_steps: int = 0, eta_min: float = 0.0, last_epoch: int = -1): base_scheduler = _CosineAnnealingLR( optimizer, total_steps - warmup_steps, eta_min=eta_min, last_epoch=last_epoch ) super().__init__(optimizer, warmup_steps, base_scheduler, last_epoch=last_epoch) class FlatAnnealingLR(DelayerScheduler): """Flat and cosine annealing learning rate scheduler. The learning rate will be a fixed value before starting decay. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. pct_start (float, optional): Percent of steps before starting learning rate decay, defaults to -0.72. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__(self, optimizer, total_steps: int, pct_start: float = 0.72, last_epoch: int = -1, **kwargs): if not (0.0 <= pct_start <= 1.0): raise ValueError(f"pct_start must >= 0.0 and <= 1.0, got {pct_start}") flat_steps = int(total_steps * pct_start) anneal_steps = total_steps - flat_steps base_scheduler = _CosineAnnealingLR(optimizer, anneal_steps) super().__init__(optimizer, flat_steps, base_scheduler, last_epoch=last_epoch) class FlatAnnealingWarmupLR(WarmupDelayerScheduler): """Flat and cosine annealing learning rate scheduler with learning rate warmup. A linear warmup schedule will be applied, and then the learning rate will be a fixed value before starting decay. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. warmup_steps (int, optional): Number of warmup steps, defaults to 0. pct_start (float, optional): Percent of steps before starting learning rate decay, defaults to -0.72. eta_min (int, optional): Minimum learning rate, defaults to 0. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__( self, optimizer, total_steps: int, warmup_steps: int = 0, pct_start: float = 0.72, eta_min: int = 0, last_epoch: int = -1, **kwargs, ): if not (0.0 <= pct_start <= 1.0): raise ValueError(f"pct_start must >= 0.0 and <= 1.0, got {pct_start}") flat_steps = int((total_steps - warmup_steps) * pct_start) anneal_steps = total_steps - warmup_steps - flat_steps base_scheduler = _CosineAnnealingLR(optimizer, anneal_steps, eta_min=eta_min) super().__init__(optimizer, warmup_steps, flat_steps, base_scheduler, last_epoch=last_epoch)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/poly.py
colossalai/nn/lr_scheduler/poly.py
from torch.optim.lr_scheduler import _LRScheduler from .delayed import WarmupScheduler class PolynomialLR(_LRScheduler): """Polynomial learning rate scheduler. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. end_lr (float, optional): Minimum learning rate, defaults to 0.0001. power (float, optional): The power of polynomial, defaults to 1.0. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__( self, optimizer, total_steps: int, end_lr: float = 0.0001, power: float = 1.0, last_epoch: int = -1, **kwargs ): if end_lr < 0: raise ValueError(f"end_lr must >= 0, got {end_lr}") self.total_steps = total_steps self.end_lr = end_lr self.power = power super().__init__(optimizer, last_epoch=last_epoch) def get_lr(self): return self._get_closed_form_lr() def _get_closed_form_lr(self): return [ (base_lr - self.end_lr) * ((1 - min(self.last_epoch, self.total_steps) / self.total_steps) ** self.power) + self.end_lr for base_lr in self.base_lrs ] class PolynomialWarmupLR(WarmupScheduler): """Polynomial learning rate scheduler with warmup. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. warmup_steps (int, optional): Number of warmup steps, defaults to 0. end_lr (float, optional): Minimum learning rate, defaults to 0.0001. power (float, optional): The power of polynomial, defaults to 1.0. last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__( self, optimizer, total_steps: int, warmup_steps: int = 0, end_lr: float = 0.0001, power: float = 1.0, last_epoch: int = -1, **kwargs, ): base_scheduler = PolynomialLR(optimizer, total_steps - warmup_steps, end_lr=end_lr, power=power) super().__init__(optimizer, warmup_steps, base_scheduler, last_epoch=last_epoch)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/torch.py
colossalai/nn/lr_scheduler/torch.py
from torch.optim.lr_scheduler import ExponentialLR as _ExponentialLR from torch.optim.lr_scheduler import LambdaLR as _LambdaLR from torch.optim.lr_scheduler import MultiplicativeLR as _MultiplicativeLR from torch.optim.lr_scheduler import StepLR as _StepLR class LambdaLR(_LambdaLR): """Sets the learning rate of each parameter group to the initial lr times a given function. When last_epoch=-1, sets initial lr as lr. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. lr_lambda (Union[``function``, ``list[function]``]): A function which computes a multiplicative factor given an integer parameter epoch, or a list of such functions, one for each group in optimizer.param_groups, defaults to None. last_epoch (int, optional): The index of last epoch, defaults to -1. """ def __init__(self, optimizer, total_steps, lr_lambda=None, last_epoch: int = -1) -> None: super().__init__(optimizer, lr_lambda, last_epoch=last_epoch) class MultiplicativeLR(_MultiplicativeLR): """Multiply the learning rate of each parameter group by the factor given in the specified function. When last_epoch=-1, sets initial lr as lr. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. lr_lambda (Union[``function``, ``list[function]``]): A function which computes a multiplicative factor given an integer parameter epoch, or a list of such functions, one for each group in optimizer.param_groups, defaults to None. last_epoch (int, optional): The index of last epoch, defaults to -1. """ def __init__(self, optimizer, total_steps, lr_lambda=None, last_epoch: int = -1) -> None: super().__init__(optimizer, lr_lambda, last_epoch=last_epoch) class StepLR(_StepLR): """Decays the learning rate of each parameter group by gamma every step_size epochs. Notice that such decay can happen simultaneously with other changes to the learning rate from outside this scheduler. When last_epoch=-1, sets initial lr as lr. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. step_size (int, optional): Period of learning rate decay, defaults to 1. gamma (float, optional): Multiplicative factor of learning rate decay, defaults to 0.1. last_epoch (int, optional): The index of last epoch, defaults to -1. """ def __init__(self, optimizer, total_steps, step_size: int = 1, gamma: float = 0.1, last_epoch: int = -1) -> None: super().__init__(optimizer, step_size, gamma=gamma, last_epoch=last_epoch) class ExponentialLR(_ExponentialLR): """Decays the learning rate of each parameter group by gamma every epoch. When last_epoch=-1, sets initial lr as lr Args: optimizer (Union[:class:`torch.optim.Optimizer`, :class:`colossalai.nn.optimizer`]): Wrapped optimizer. total_steps (int): Number of total training steps. gamma (float, optional): Multiplicative factor of learning rate decay, defaults to 1.0. last_epoch (int, optional): The index of last epoch, defaults to -1. """ def __init__(self, optimizer, total_steps, gamma: float = 1.0, last_epoch: int = -1) -> None: super().__init__(optimizer, gamma, last_epoch=last_epoch)
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/lr_scheduler/linear.py
colossalai/nn/lr_scheduler/linear.py
from torch.optim.lr_scheduler import _LRScheduler class LinearWarmupLR(_LRScheduler): """Linearly warmup learning rate and then linearly decay. Args: optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer. total_steps (int): Number of total training steps. warmup_steps (int, optional): Number of warmup steps, defaults to 0 last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1, the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr. """ def __init__(self, optimizer, total_steps: int, warmup_steps: int = 0, last_epoch: int = -1, **kwargs): self.warmup_steps = warmup_steps self.total_steps = total_steps super().__init__(optimizer, last_epoch=last_epoch) def get_lr(self): if self.last_epoch < self.warmup_steps: return [(self.last_epoch + 1) / (self.warmup_steps + 1) * lr for lr in self.base_lrs] else: return [ (self.total_steps - self.last_epoch) / (self.total_steps - self.warmup_steps) * lr for lr in self.base_lrs ]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/distributed_lamb.py
colossalai/nn/optimizer/distributed_lamb.py
# Disclaimer: Modified from https://github.com/NUS-HPC-AI-Lab/pytorch-lamb/blob/master/optim/lamb.py from typing import Dict, Optional import torch import torch.distributed as dist from colossalai.interface.optimizer import DistributedOptim from colossalai.tensor.d_tensor import is_distributed_tensor __all__ = ["DistributedLamb"] class DistributedLamb(DistributedOptim): r"""Implements the Lamb algorithm, with extra support for ZeRO 2 and Tensor Parallel. Proposed in `Large Batch Optimization for Deep Learning: Training BERT in 76 minutes`_. It's recommended to use this with HybridParallelPlugin/ZeRO plugin and booster, which will take care of setup_distributed. Example with 4 devices: >>> optim = DistributedLamb(model.parameters(), lr=1e-3) >>> proc_mesh = ProcessGroupMesh(tp_size, zero_size) >>> tp_group = proc_mesh.get_group_along_axis(0) >>> dp_group = proc_mesh.get_group_along_axis(1) >>> optim.setup_distributed(tp_group, dp_group) Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) .. _Large Batch Optimization for Deep Learning: Training BERT in 76 minutes: https://arxiv.org/abs/1904.00962 """ def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-6, weight_decay=0, bias_correction=True, ): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0])) if not 0.0 <= betas[1] < 1.0: raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1])) # self.setup_distributed(tp_group, dp_group) self.shard_to_working_param = {} self.tp_size = self.dp_size = 1 self.is_zero = False defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, bias_correction=bias_correction) super().__init__(params, defaults) def setup_distributed( self, tp_group: Optional[dist.ProcessGroup] = None, dp_group: Optional[dist.ProcessGroup] = None, shard_to_working_param: Optional[Dict] = {}, padding_map=None, is_zero: Optional[bool] = False, ): """Assign process groups for TP and ZeRO 2. Arguments: tp_group (dist.ProcessGroup): Tensor Parallel process group dp_group (dist.ProcessGroup): ZeRO 2 process group shard_to_working_param (Dict): ZeRO 2 feeds the optimizer a sharded param view as grads are sharded. This maps from id(view) to working params used in forward & backward. padding_map: An empty interface placeholder is_zero (bool): Whether to use ZeRO 2. """ self.tp_group = tp_group self.dp_group = dp_group if tp_group is not None: self.tp_size = dist.get_world_size(tp_group) if dp_group is not None: self.dp_size = dist.get_world_size(dp_group) self.shard_to_working_param = shard_to_working_param if shard_to_working_param is not None else {} self.is_zero = is_zero self.is_dist = {} # Cache parameter layout for group in self.param_groups: for p in group["params"]: # w/o ZeRO: master param = working param self.shard_to_working_param[id(p)] = self.shard_to_working_param.get(id(p), p) self.is_dist[p] = ( is_distributed_tensor(p) if self.dp_size <= 1 else is_distributed_tensor(self.shard_to_working_param.get(id(p), None)) ) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group["params"]: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError("Lamb does not support sparse gradients, consider SparseAdam instad.") state = self.state[p] # State initialization if len(state) == 0: state["step"] = 0 # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(p.data) # Exponential moving average of squared gradient values state["exp_avg_sq"] = torch.zeros_like(p.data) exp_avg, exp_avg_sq = state["exp_avg"], state["exp_avg_sq"] beta1, beta2 = group["betas"] state["step"] += 1 # Decay the first and second moment running average coefficient # m_t exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) # v_t exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) scaled_lr = group["lr"] if group["bias_correction"]: bias_correction1 = 1 - beta1 ** state["step"] bias_correction2 = 1 - beta2 ** state["step"] # Apply debiasing to lr to avoid broadcast scaled_lr *= (bias_correction2**0.5) / bias_correction1 # exp_avg.div_(bias_correction1) # exp_avg_sq.div_(bias_correction2) update = exp_avg / exp_avg_sq.sqrt().add(group["eps"]) if group["weight_decay"] != 0: update.add_(p.data, alpha=group["weight_decay"]) # Compute global layer-wise trust ratio if self.is_dist[p] or self.is_zero: p_local = p g_sum = (update**2).sum() if self.dp_size > 1 and self.is_zero: # ZeRO 2 doesn't shard param. Compute full param norm w/o communication. dist.all_reduce(g_sum, group=self.dp_group) p_local = self.shard_to_working_param[id(p)] w_sum = (p_local**2).sum() sums = torch.stack([w_sum, g_sum]) # Get global l2 norms if self.tp_size > 1: dist.all_reduce(sums, group=self.tp_group) w_norm, g_norm = sums.sqrt().chunk(2) else: # Fall back to vanilla Lamb w_norm = torch.norm(p) g_norm = torch.norm(update) trust_ratio = torch.where(w_norm > 0 and g_norm > 0, (w_norm / g_norm), 1.0).item() scaled_lr *= trust_ratio p.data.add_(update, alpha=-scaled_lr) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/fused_lamb.py
colossalai/nn/optimizer/fused_lamb.py
# modified from https://github.com/NVIDIA/apex/blob/master/apex/optimizers/fused_lamb.py import torch from colossalai.utils import multi_tensor_applier class FusedLAMB(torch.optim.Optimizer): """Implements LAMB algorithm. `FusedLAMB` requires CUDA extensions which can be built during installation or runtime. This version of fused LAMB implements 2 fusions. * Fusion of the LAMB update's elementwise operations * A multi-tensor apply launch that batches the elementwise updates applied to all the model's parameters into one or a few kernel launches. :class:`colossalai.nn.optimizer.FusedLAMB`'s usage is identical to any ordinary Pytorch optimizer :class:`colossalai.nn.optimizer.FusedLAMB` may be used with or without Amp. LAMB was proposed in `Large Batch Optimization for Deep Learning: Training BERT in 76 minutes`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its norm. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-6) weight_decay (float, optional): weight decay (L2 penalty) (default: 0.01) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ NOT SUPPORTED now! (default: False) adam_w_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) grad_averaging (bool, optional): whether apply (1-beta2) to grad when calculating running averages of gradient. (default: True) set_grad_none (bool, optional): whether set grad to None when zero_grad() method is called. (default: True) max_grad_norm (float, optional): value used to clip global grad norm (default: 1.0) use_nvlamb (boolean, optional): Apply adaptive learning rate to 0.0 weight decay parameter (default: False) .. _Large Batch Optimization for Deep Learning: Training BERT in 76 minutes: https://arxiv.org/abs/1904.00962 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__( self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-6, weight_decay=0.01, amsgrad=False, adam_w_mode=True, grad_averaging=True, set_grad_none=True, max_grad_norm=1.0, use_nvlamb=False, ): if amsgrad: raise RuntimeError("FusedLAMB does not support the AMSGrad variant.") defaults = dict( lr=lr, bias_correction=bias_correction, betas=betas, eps=eps, weight_decay=weight_decay, grad_averaging=grad_averaging, max_grad_norm=max_grad_norm, ) super(FusedLAMB, self).__init__(params, defaults) if multi_tensor_applier.available: from colossalai.kernel.kernel_loader import FusedOptimizerLoader fused_optim = FusedOptimizerLoader().load() self.multi_tensor_l2norm = fused_optim.multi_tensor_l2norm # Skip buffer self._dummy_overflow_buf = torch.tensor( [0], dtype=torch.int, device=self.param_groups[0]["params"][0].device ) self.multi_tensor_lamb = fused_optim.multi_tensor_lamb else: raise RuntimeError("FusedLAMB requires cuda extensions") self.adam_w_mode = 1 if adam_w_mode else 0 self.set_grad_none = set_grad_none self.use_nvlamb = use_nvlamb def zero_grad(self): if self.set_grad_none: for group in self.param_groups: for p in group["params"]: p.grad = None else: super(FusedLAMB, self).zero_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() # create separate grad lists for fp32 and fp16 params g_all_32, g_all_16 = [], [] for group in self.param_groups: for p in group["params"]: if p.grad is None: continue if p.dtype == torch.float32: g_all_32.append(p.grad.data) elif p.dtype == torch.float16: g_all_16.append(p.grad.data) else: raise RuntimeError("FusedLAMB only support fp16 and fp32.") device = self.param_groups[0]["params"][0].device g_norm_32, g_norm_16 = torch.zeros(1, device=device), torch.zeros(1, device=device) # compute grad norm for two lists if len(g_all_32) > 0: g_norm_32 = multi_tensor_applier(self.multi_tensor_l2norm, self._dummy_overflow_buf, [g_all_32], False)[0] if len(g_all_16) > 0: g_norm_16 = multi_tensor_applier(self.multi_tensor_l2norm, self._dummy_overflow_buf, [g_all_16], False)[0] # blend two grad norms to get global grad norm global_grad_norm = multi_tensor_applier( self.multi_tensor_l2norm, self._dummy_overflow_buf, [[g_norm_32, g_norm_16]], False )[0] max_grad_norm = self.defaults["max_grad_norm"] for group in self.param_groups: bias_correction = 1 if group["bias_correction"] else 0 beta1, beta2 = group["betas"] grad_averaging = 1 if group["grad_averaging"] else 0 # assume same step across group now to simplify things # per parameter step can be easily support by making it tensor, or pass list into kernel if "step" in group: group["step"] += 1 else: group["step"] = 1 # create lists for multi-tensor apply g_16, p_16, m_16, v_16 = [], [], [], [] g_32, p_32, m_32, v_32 = [], [], [], [] for p in group["params"]: if p.grad is None: continue if p.grad.data.is_sparse: raise RuntimeError( "FusedLAMB does not support sparse gradients, please consider SparseAdam instead" ) state = self.state[p] # State initialization if len(state) == 0: # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(p) # Exponential moving average of gradient values state["exp_avg_sq"] = torch.zeros_like(p) if p.dtype == torch.float16: g_16.append(p.grad.data) p_16.append(p.data) m_16.append(state["exp_avg"]) v_16.append(state["exp_avg_sq"]) elif p.dtype == torch.float32: g_32.append(p.grad.data) p_32.append(p.data) m_32.append(state["exp_avg"]) v_32.append(state["exp_avg_sq"]) else: raise RuntimeError("FusedLAMB only support fp16 and fp32.") if len(g_16) > 0: multi_tensor_applier( self.multi_tensor_lamb, self._dummy_overflow_buf, [g_16, p_16, m_16, v_16], group["lr"], beta1, beta2, group["eps"], group["step"], bias_correction, group["weight_decay"], grad_averaging, self.adam_w_mode, global_grad_norm, max_grad_norm, self.use_nvlamb, ) if len(g_32) > 0: multi_tensor_applier( self.multi_tensor_lamb, self._dummy_overflow_buf, [g_32, p_32, m_32, v_32], group["lr"], beta1, beta2, group["eps"], group["step"], bias_correction, group["weight_decay"], grad_averaging, self.adam_w_mode, global_grad_norm, max_grad_norm, self.use_nvlamb, ) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/hybrid_adam.py
colossalai/nn/optimizer/hybrid_adam.py
from typing import Any, Optional import torch from colossalai.kernel.kernel_loader import FusedOptimizerLoader from colossalai.utils import get_current_device, multi_tensor_applier from .cpu_adam import CPUAdam class HybridAdam(CPUAdam): """Implements Adam algorithm. Supports parameters updating on both GPU and CPU, depending on the device of parameters. But the parameters and gradients should on the same device: * Parameters on CPU and gradients on CPU is allowed. * Parameters on GPU and gradients on GPU is allowed. * Parameters on GPU and gradients on CPU is **not** allowed. `HybridAdam` requires CUDA extensions which can be built during installation or runtime. This version of Hybrid Adam is an hybrid of CPUAdam and FusedAdam. * For parameters updating on CPU, it uses CPUAdam. * For parameters updating on GPU, it uses FusedAdam. * Hybrid precision calculation of fp16 and fp32 is supported, eg fp32 parameters and fp16 gradients. :class:`colossalai.nn.optimizer.HybridAdam` may be used as a drop-in replacement for ``torch.optim.AdamW``, or ``torch.optim.Adam`` with ``adamw_mode=False`` Adam was been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: model_params (iterable): iterable of parameters of dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) NOT SUPPORTED yet in CPUAdam! adamw_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) simd_log (boolean, optional): whether to show if you are using SIMD to accelerate. (default: False) nvme_offload_fraction (float, optional): Fraction of optimizer states to be offloaded to NVMe. Defaults to 0.0. nvme_offload_dir (Optional[str], optional): Directory to save NVMe offload files. If it's ``None``, a random temporary directory will be used. Defaults to None. .. _Adam\: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ # Number of fp32 shards for per parameter # Param weight, grad, momentum and variance num_fp32_shards_per_param = 4 def __init__( self, model_params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, adamw_mode=True, nvme_offload_fraction: float = 0.0, nvme_offload_dir: Optional[str] = None, **defaults: Any, ): super().__init__( model_params, lr, bias_correction, betas, eps, weight_decay, adamw_mode, nvme_offload_fraction, nvme_offload_dir, ) if torch.cuda.is_available(): fused_optim = FusedOptimizerLoader().load() self.gpu_adam_op = fused_optim.multi_tensor_adam self._dummy_overflow_buf = torch.tensor([0], dtype=torch.int, device=get_current_device()) @torch.no_grad() def step(self, closure=None, div_scale: float = -1): loss = None if closure is not None: with torch.enable_grad(): loss = closure() self._pre_step("exp_avg", "exp_avg_sq") for _, group in enumerate(self.param_groups): g_l, p_l, m_l, v_l = [], [], [], [] group_step = 0 for _, p in enumerate(group["params"]): if p.grad is None: continue state = self.state[p] target_device = p.device if len(state) == 0: state["step"] = 0 # gradient momentums state["exp_avg"] = torch.zeros_like(p, device=target_device) # gradient variances state["exp_avg_sq"] = torch.zeros_like(p, device=target_device) self._post_state_init(p) state["step"] += 1 group_step = state["step"] beta1, beta2 = group["betas"] if target_device.type == "cpu" or target_device.type == "npu": assert state["exp_avg"].device.type in ("cpu", "npu"), "exp_avg should stay on cpu" assert state["exp_avg_sq"].device.type in ("cpu", "npu"), "exp_avg should stay on cpu" self._pre_update(p, "exp_avg", "exp_avg_sq") if p.grad.dtype is torch.bfloat16 or p.grad.device.type == "npu": # cpu adam kernel does not support bf16 now bias_correction1 = 1 - beta1 ** state["step"] bias_correction2 = 1 - beta2 ** state["step"] self.torch_adam_update( p.data, p.grad.data, state["exp_avg"], state["exp_avg_sq"], group["lr"], beta1, beta2, group["eps"], group["weight_decay"], bias_correction1, bias_correction2, self.adamw_mode, ) else: self.cpu_adam_op.step( state["step"], group["lr"], beta1, beta2, group["eps"], group["weight_decay"], group["bias_correction"], p.data, p.grad.data, state["exp_avg"], state["exp_avg_sq"], div_scale, ) self._post_update(p, "exp_avg", "exp_avg_sq") elif target_device.type == "cuda": assert state["exp_avg"].device.type == "cuda", "exp_avg should stay on cuda" assert state["exp_avg_sq"].device.type == "cuda", "exp_avg should stay on cuda" # record the state by group and update at once g_l.append(p.grad.data) p_l.append(p.data) m_l.append(state["exp_avg"]) v_l.append(state["exp_avg_sq"]) else: raise RuntimeError if len(g_l) > 0: adamw_mode = 1 if self.adamw_mode else 0 bias_correction = 1 if group["bias_correction"] else 0 multi_tensor_applier( self.gpu_adam_op, self._dummy_overflow_buf, [g_l, p_l, m_l, v_l], group["lr"], group["betas"][0], group["betas"][1], group["eps"], group_step, adamw_mode, bias_correction, group["weight_decay"], div_scale, ) self._post_step() return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/distributed_adafactor.py
colossalai/nn/optimizer/distributed_adafactor.py
import math from typing import Dict import torch import torch.distributed as dist from colossalai.interface.optimizer import DistributedOptim from colossalai.shardformer.layer._operation import _gather, _split from colossalai.tensor.d_tensor import get_sharding_spec, is_distributed_tensor # DistributedAdaFactor (with Tensor parallel and Zero stage 2) __all__ = ["DistributedAdaFactor"] class DistributedAdaFactor(DistributedOptim): def __init__( self, params, lr=None, eps=(1e-30, 1e-3), clip_threshold=1.0, decay_rate=-0.8, beta1=None, weight_decay=0.0, scale_parameter=True, relative_step=True, warmup_init=False, ): lr = None if lr is not None and relative_step: raise ValueError("Cannot combine manual `lr` and `relative_step=True` options") if warmup_init and not relative_step: raise ValueError("`warmup_init=True` requires `relative_step=True`") defaults = { "lr": lr, "eps": eps, "clip_threshold": clip_threshold, "decay_rate": decay_rate, "beta1": beta1, "weight_decay": weight_decay, "scale_parameter": scale_parameter, "relative_step": relative_step, "warmup_init": warmup_init, } self.tp_size = 1 self.tp_group = None self.dp_size = 1 self.dp_group = None self.shard_to_working_param = None # Dict{id:shape}, sample {id(param): torch.tensor} self.use_zero = True self.param_is_dtensor_dict = {} # {id(p): True/False} self.grad_shape_dict = {} # {id(p): master param shape} self.factored_dict = {} # {id(p): True/False} self.use_first_moment_dict = {} # {id(p): True/False} self.shard_spec_dict = {} # {id(p): ShardSpec} super().__init__(params, defaults) def setup_distributed( self, tp_group: dist.ProcessGroup = None, dp_group: dist.ProcessGroup = None, shard_to_working_param: Dict = {}, padding_map=None, use_zero: bool = True, ) -> None: """Setup process groups for TP and ZeRO 2. Inject features to the Optimizer Args: tp_group: The devices group for tensor parallel; dp_group: The devices group for data parallel; shard_to_working_param (Dict): ZeRO 2 feeds the optimizer a sharded param view as grads are sharded. This maps from id(view) to working params used in forward & backward. padding_map: An empty interface placeholder; use_zero: Whether or not to use zero; """ self.tp_group = tp_group # "Expected row process group" self.dp_group = dp_group if self.tp_group is not None: self.tp_size = dist.get_world_size(self.tp_group) if self.dp_group is not None: self.dp_size = dist.get_world_size(self.dp_group) self.use_zero = use_zero self.shard_to_working_param = shard_to_working_param if shard_to_working_param is not None else {} # grad is None, cause we dont setup now for group in self.param_groups: for p in group["params"]: self.shard_to_working_param[id(p)] = self.shard_to_working_param.get( id(p), p ) # If not ZeRO, working param is master param self.param_is_dtensor_dict[id(p)] = is_distributed_tensor(self.shard_to_working_param[id(p)]) self.grad_shape_dict[id(p)] = self.shard_to_working_param.get(id(p)).shape self.factored_dict[id(p)], self.use_first_moment_dict[id(p)] = self._get_options( group, self.grad_shape_dict[id(p)] ) if self.param_is_dtensor_dict[id(p)]: self.shard_spec_dict[id(p)] = get_sharding_spec(self.shard_to_working_param[id(p)]) else: self.shard_spec_dict[id(p)] = None @staticmethod def _get_lr(param_group, param_state): rel_step_sz = param_group["lr"] if param_group["relative_step"]: min_step = 1e-6 * param_state["step"] if param_group["warmup_init"] else 1e-2 rel_step_sz = min(min_step, 1.0 / math.sqrt(param_state["step"])) param_scale = 1.0 if param_group["scale_parameter"]: param_scale = max(param_group["eps"][1], param_state["RMS"]) return param_scale * rel_step_sz @staticmethod def _get_options(param_group, param_shape): """ Determines whether the current param is factored Args: param_group : param group param_shape : Original Shape of param """ factored = len(param_shape) >= 2 use_first_moment = param_group["beta1"] is not None return factored, use_first_moment @staticmethod def _rms(tensor, param_is_dtensor, use_zero, tp_size, dp_size, tp_group, dp_group): tensor_sum = tensor.pow(2).sum() num_of_element = tensor.numel() if param_is_dtensor: # reduce tensor_sum from tp_group dist.all_reduce(tensor_sum, group=tp_group) num_of_element = num_of_element * tp_size if use_zero: dist.all_reduce(tensor_sum, group=dp_group) num_of_element = num_of_element * dp_size rms = (tensor_sum / num_of_element).sqrt() return rms @staticmethod def _approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col): r_factor = (exp_avg_sq_row / exp_avg_sq_row.mean(dim=-1, keepdim=True)).rsqrt_().unsqueeze(-1) c_factor = exp_avg_sq_col.unsqueeze(-2).rsqrt() return torch.mul(r_factor, c_factor) # approx_sq_grad for row parallel weight @staticmethod def _approx_sq_grad_row_parallel(exp_avg_sq_row, exp_avg_sq_col, sq_row_meam): # row_meam = sq_row_meam r_factor = (exp_avg_sq_row / sq_row_meam).rsqrt_().unsqueeze(-1) c_factor = exp_avg_sq_col.unsqueeze(-2).rsqrt() return torch.mul(r_factor, c_factor) def _col_parallel_factor(self, update, grad, state, grad_shape, beta2t): if grad_shape[0] % self.dp_size != 0: # gather update[flatten] along dp group then reshape to [H, W/tp] update = _gather(input_=update, dim=-1, process_group=self.dp_group) update_reshape = update.view(-1, grad_shape[1]) # gather grad[flatten] along dp group then reshape to [H, W/tp] grad = _gather(input_=grad, dim=-1, process_group=self.dp_group) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state["exp_avg_sq_row"] # [H] exp_avg_sq_col = state["exp_avg_sq_col"] # [W/tp] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) else: update_reshape = update.view(-1, grad_shape[1]) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state["exp_avg_sq_row"] # [H/dp] exp_avg_sq_col = state["exp_avg_sq_col"] # [W/tp] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) dist.all_reduce(exp_avg_sq_row, group=self.tp_group) exp_avg_sq_row.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) if self.use_zero: update = update_reshape.view(-1) else: update = update_reshape return update def _row_parallel_factor(self, update, grad, state, grad_shape, beta2t): if grad_shape[0] % self.dp_size != 0: # gather update[flatten] along dp group then reshape to [H/tp, W] update = _gather(input_=update, dim=-1, process_group=self.dp_group) # view update to origin[tp] shape update_reshape = update.view(-1, grad_shape[1]) # gather grad[flatten] along dp group then reshape to [H/tp, W] grad = _gather(input_=grad, dim=-1, process_group=self.dp_group) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state["exp_avg_sq_row"] # [H/tp] exp_avg_sq_col = state["exp_avg_sq_col"] # [W] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) if self.use_zero: update = _split(input_=update_reshape.view(-1), dim=-1, process_group=self.dp_group) else: update = update_reshape else: update_reshape = update.view(-1, grad_shape[1]) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state["exp_avg_sq_row"] # [H/dp/tp] exp_avg_sq_col = state["exp_avg_sq_col"] # [W] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) # gather row exp_avg_sq_row_gather = _gather(input_=exp_avg_sq_row, dim=-1, process_group=self.tp_group) sq_row_meam = exp_avg_sq_row_gather.mean(dim=-1, keepdim=True) update_reshape = self._approx_sq_grad_row_parallel(exp_avg_sq_row, exp_avg_sq_col, sq_row_meam) update_reshape.mul_(grad_reshape) if self.use_zero: update = update_reshape.view(-1) else: update = update_reshape return update def _base_factor(self, update, grad, state, grad_shape, beta2t): if self.use_zero: # only zero if grad_shape[0] % self.dp_size != 0: # view update to origin shape update.view(grad_shape[0]//self.data_parallel_size , grad_shape[1]) # row mean no change # col mean need reduce and div # gather update[flatten] along dp group then reshape to [H, W] update = _gather(input_=update, dim=-1, process_group=self.dp_group) # view update to origin[tp] shape update_reshape = update.view(-1, grad_shape[1]) # gather grad[flatten] along dp group then reshape to [H, W] grad = _gather(input_=grad, dim=-1, process_group=self.dp_group) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state["exp_avg_sq_row"] # [H/dp] exp_avg_sq_col = state["exp_avg_sq_col"] # [W] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) update = _split(input_=update_reshape.view(-1), dim=-1, process_group=self.dp_group) else: # no residual row # view update to origin[tp] shape update_reshape = update.view(-1, grad_shape[1]) # [H/dp, W] grad_reshape = grad.view(-1, grad_shape[1]) # [H/dp, W] exp_avg_sq_row = state["exp_avg_sq_row"] # [H/tp] exp_avg_sq_col = state["exp_avg_sq_col"] # [W] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) update = update_reshape.view(-1) else: # base factor; no tp, no dp exp_avg_sq_row = state["exp_avg_sq_row"] exp_avg_sq_col = state["exp_avg_sq_col"] # Exponential average of row indexes exp_avg_sq_row.mul_(beta2t).add_(update.mean(dim=-1), alpha=(1.0 - beta2t)) # Exponential average of columns indexes exp_avg_sq_col.mul_(beta2t).add_(update.mean(dim=-2), alpha=(1.0 - beta2t)) # Approximation of exponential moving average of square of gradient update = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update.mul_(grad) return update @torch.no_grad() def step(self, closure=None): """ Performs a single optimization steps Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() """ param_groups: Dict { "params":[weight, bias] "lr" "eps" "clip_threshold" "decay_rate" "beta1" "weight_decay" "scale_parameter" "relative_step" "warmup_init" } """ for group in self.param_groups: # update weight & bias for p in group["params"]: if p.grad is None: continue grad = p.grad if grad.is_sparse: raise RuntimeError("Adafactor does not support sparse gradients.") state = self.state[p] grad_shape = self.grad_shape_dict[id(p)] param_is_dtensor = self.param_is_dtensor_dict[id(p)] if param_is_dtensor: grad_shape = self.shard_to_working_param.get(id(p)).shape # tp shape (2 dim) factored, use_first_moment = self.factored_dict[id(p)], self.use_first_moment_dict[id(p)] shard_spec = self.shard_spec_dict[id(p)] if len(state) == 0: state["step"] = 0 if use_first_moment: # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(p) if factored: if param_is_dtensor: if shard_spec.sharding_sequence[0] == "R": # Col Parallel if grad_shape[0] % self.dp_size != 0: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0], device=p.device, dtype=p.dtype ) # [H] else: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=p.device, dtype=p.dtype ) # [H/dp] state["exp_avg_sq_col"] = torch.zeros( grad_shape[1], device=p.device, dtype=p.dtype ) # [W/TP] if shard_spec.sharding_sequence[-1] == "R": # Row Parallel # Row indivisible shape situation if grad_shape[0] % self.dp_size != 0: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0], device=p.device, dtype=p.dtype ) # [H/tp] else: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=p.device, dtype=p.dtype ) # [H/dp/tp] state["exp_avg_sq_col"] = torch.zeros( grad_shape[1], device=p.device, dtype=p.dtype ) # [W] else: if self.use_zero: if grad_shape[0] % self.dp_size != 0: # save all exp_avg_sq_row [H] state["exp_avg_sq_row"] = torch.zeros( grad_shape[0], device=grad.device, dtype=p.dtype ) else: # exp_avg_sq_row [H // dp] state["exp_avg_sq_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=grad.device, dtype=p.dtype ) else: # exp_avg_sq_row [H] state["exp_avg_sq_row"] = torch.zeros(grad_shape[0], device=grad.device, dtype=p.dtype) # exp_avg_sq_col alaways [W] state["exp_avg_sq_col"] = torch.zeros(grad_shape[1], device=grad.device, dtype=p.dtype) else: state["exp_avg_sq"] = torch.zeros_like(p) state["RMS"] = 0 else: if use_first_moment: state["exp_avg"] = state["exp_avg"] if factored: state["exp_avg_sq_row"] = state["exp_avg_sq_row"] state["exp_avg_sq_col"] = state["exp_avg_sq_col"] else: state["exp_avg_sq"] = state["exp_avg_sq"] state["step"] += 1 lr = self._get_lr(group, state) beta2t = 1.0 - math.pow(state["step"], group["decay_rate"]) update = (grad**2) + group["eps"][0] if factored: if param_is_dtensor: # ============================== # First Dim is R, Last Dim is S{} means split dim -1 ---> # Coloum Parallel ---> sq_row need Do (col) Reduce # ============================== if shard_spec.sharding_sequence[0] == "R": update = self._col_parallel_factor(update, grad, state, grad_shape, beta2t) # ============================== # Last Dim is R, First Dim is S{} means split dim 0 ---> # Row Parallel ---> sq_col need Do (row) Reduce # ============================== elif shard_spec.sharding_sequence[-1] == "R": update = self._row_parallel_factor(update, grad, state, grad_shape, beta2t) else: update = self._base_factor(update, grad, state, grad_shape, beta2t) else: exp_avg_sq = state["exp_avg_sq"] exp_avg_sq.mul_(beta2t).add_(update, alpha=(1.0 - beta2t)) update = exp_avg_sq.rsqrt().mul_(grad) # # (Line No.8) RMS rms = self._rms( update, param_is_dtensor, self.use_zero, self.tp_size, self.dp_size, self.tp_group, self.dp_group, ) update.div_((rms / group["clip_threshold"]).clamp_(min=1.0)) update.mul_(lr) if use_first_moment: exp_avg = state["exp_avg"] exp_avg.mul_(group["beta1"]).add_(update, alpha=(1 - group["beta1"])) update = exp_avg if group["weight_decay"] != 0: p.add_(p, alpha=(-group["weight_decay"] * lr)) p.add_(-update) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/lars.py
colossalai/nn/optimizer/lars.py
"""Adapted from https://github.com/NUS-HPC-AI-Lab/LARS-ImageNet-PyTorch/blob/main/lars.py""" from typing import Iterable import torch from torch.optim import Optimizer class Lars(Optimizer): r"""Implements the LARS optimizer from `"Large batch training of convolutional networks" <https://arxiv.org/pdf/1708.03888.pdf>`_. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) momentum (float, optional): momentum factor (default: 0) eeta (float, optional): LARS coefficient as used in the paper (default: 1e-3) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) """ def __init__( self, params: Iterable[torch.nn.Parameter], lr=1e-3, momentum=0, eeta=1e-3, weight_decay=0, epsilon=0.0 ) -> None: if not isinstance(lr, float) or lr < 0.0: raise ValueError("Invalid learning rate: {}".format(lr)) if momentum < 0.0: raise ValueError("Invalid momentum value: {}".format(momentum)) if weight_decay < 0.0: raise ValueError("Invalid weight_decay value: {}".format(weight_decay)) if eeta <= 0 or eeta > 1: raise ValueError("Invalid eeta value: {}".format(eeta)) if epsilon < 0: raise ValueError("Invalid epsilon value: {}".format(epsilon)) defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay, eeta=eeta, epsilon=epsilon, lars=True) super().__init__(params, defaults) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure() for group in self.param_groups: weight_decay = group["weight_decay"] momentum = group["momentum"] eeta = group["eeta"] lr = group["lr"] lars = group["lars"] eps = group["epsilon"] for p in group["params"]: if p.grad is None: continue decayed_grad = p.grad scaled_lr = lr if lars: w_norm = torch.norm(p) g_norm = torch.norm(p.grad) trust_ratio = torch.where( w_norm > 0 and g_norm > 0, eeta * w_norm / (g_norm + weight_decay * w_norm + eps), torch.ones_like(w_norm), ) trust_ratio.clamp_(0.0, 50) scaled_lr *= trust_ratio.item() if weight_decay != 0: decayed_grad = decayed_grad.add(p, alpha=weight_decay) decayed_grad = torch.clamp(decayed_grad, -10.0, 10.0) if momentum != 0: param_state = self.state[p] if "momentum_buffer" not in param_state: buf = param_state["momentum_buffer"] = torch.clone(decayed_grad).detach() else: buf = param_state["momentum_buffer"] buf.mul_(momentum).add_(decayed_grad) decayed_grad = buf p.add_(decayed_grad, alpha=-scaled_lr) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/fused_adam.py
colossalai/nn/optimizer/fused_adam.py
# modified from https://github.com/NVIDIA/apex/blob/master/apex/optimizers/fused_adam.py """ Copyright 2020 The Microsoft DeepSpeed Team Copyright NVIDIA/apex This file is adapted from fused adam in NVIDIA/apex, commit a109f85 Licensed under the MIT License. """ import torch from colossalai.utils import get_current_device, multi_tensor_applier class FusedAdam(torch.optim.Optimizer): """Implements Adam algorithm. `FusedAdam` requires CUDA extensions which can be built during installation or runtime. This version of fused Adam implements 2 fusions. * Fusion of the Adam update's elementwise operations * A multi-tensor apply launch that batches the elementwise updates applied to all the model's parameters into one or a few kernel launches. :class:`colossalai.nn.optimizer.FusedAdam` may be used as a drop-in replacement for ``torch.optim.AdamW``, or ``torch.optim.Adam`` with ``adamw_mode=False`` :class:`colossalai.nn.optimizer.FusedAdam` may be used with or without Amp. Adam was been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) NOT SUPPORTED in FusedAdam! adamw_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) set_grad_none (bool, optional): whether set grad to None when zero_grad() method is called. (default: True) .. _Adam\: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__( self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, adamw_mode=True, weight_decay=0.0, amsgrad=False, set_grad_none=True, ): if amsgrad: raise RuntimeError("FusedAdam does not support the AMSGrad variant.") defaults = dict(lr=lr, bias_correction=bias_correction, betas=betas, eps=eps, weight_decay=weight_decay) super(FusedAdam, self).__init__(params, defaults) self.adamw_mode = 1 if adamw_mode else 0 self.set_grad_none = set_grad_none if multi_tensor_applier.available: from colossalai.kernel.kernel_loader import FusedOptimizerLoader fused_optim = FusedOptimizerLoader().load() # Skip buffer self._dummy_overflow_buf = torch.tensor([0], dtype=torch.int, device=get_current_device()) self.multi_tensor_adam = fused_optim.multi_tensor_adam else: raise RuntimeError("FusedAdam requires cuda extensions") def zero_grad(self, set_to_none=False): if set_to_none: for group in self.param_groups: for p in group["params"]: p.grad = None else: super(FusedAdam, self).zero_grad() def step(self, closure=None, grads=None, output_params=None, scale=None, grad_norms=None, div_scale: float = -1): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. The remaining arguments are deprecated, and are only retained (for the moment) for error-checking purposes. """ if any(p is not None for p in [grads, output_params, scale, grad_norms]): raise RuntimeError( "FusedAdam has been updated. Simply initialize it identically to torch.optim.Adam, and call step() with no arguments." ) loss = None if closure is not None: loss = closure() for group in self.param_groups: bias_correction = 1 if group["bias_correction"] else 0 beta1, beta2 = group["betas"] # assume same step across group now to simplify things # per parameter step can be easily support by making it tensor, or pass list into kernel if "step" in group: group["step"] += 1 else: group["step"] = 1 # create lists for multi-tensor apply g_l, p_l, m_l, v_l = [], [], [], [] for p in group["params"]: if p.grad is None: continue if p.grad.data.is_sparse: raise RuntimeError( "FusedAdam does not support sparse gradients, please consider SparseAdam instead" ) state = self.state[p] # State initialization if len(state) == 0: # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(p) # Exponential moving average of squared gradient values state["exp_avg_sq"] = torch.zeros_like(p) if p.dtype not in [torch.float16, torch.float32, torch.bfloat16]: raise RuntimeError("FusedAdam only support fp16, fp32 and bf16.") g_l.append(p.grad.data) p_l.append(p.data) m_l.append(state["exp_avg"]) v_l.append(state["exp_avg_sq"]) multi_tensor_applier( self.multi_tensor_adam, self._dummy_overflow_buf, [g_l, p_l, m_l, v_l], group["lr"], beta1, beta2, group["eps"], group["step"], self.adamw_mode, bias_correction, group["weight_decay"], div_scale, ) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/came.py
colossalai/nn/optimizer/came.py
# Copied from https://github.com/yangluo7/CAME/blob/master/came_pytorch/CAME.py import torch import torch.optim class CAME(torch.optim.Optimizer): """Implements CAME algorithm. This implementation is based on: `CAME: Confidence-guided Adaptive Memory Efficient Optimization` Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): external learning rate (default: None) eps (tuple[float, float]): regularization constants for square gradient and instability respectively (default: (1e-30, 1e-16)) clip_threshold (float): threshold of root-mean-square of final gradient update (default: 1.0) betas (tuple[float, float, float]): coefficient used for computing running averages of update, square gradient and instability (default: (0.9, 0.999, 0.9999))) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) """ def __init__( self, params, lr=None, eps=(1e-30, 1e-16), clip_threshold=1.0, betas=(0.9, 0.999, 0.9999), weight_decay=0.0, ): assert lr > 0.0 assert all([0.0 <= beta <= 1.0 for beta in betas]) defaults = dict( lr=lr, eps=eps, clip_threshold=clip_threshold, betas=betas, weight_decay=weight_decay, ) super(CAME, self).__init__(params, defaults) @property def supports_memory_efficient_fp16(self): return True @property def supports_flat_params(self): return False def _get_options(self, param_shape): factored = len(param_shape) >= 2 return factored def _rms(self, tensor): return tensor.norm(2) / (tensor.numel() ** 0.5) def _approx_sq_grad(self, exp_avg_sq_row, exp_avg_sq_col): r_factor = (exp_avg_sq_row / exp_avg_sq_row.mean(dim=-1, keepdim=True)).rsqrt_().unsqueeze(-1) c_factor = exp_avg_sq_col.unsqueeze(-2).rsqrt() return torch.mul(r_factor, c_factor) def step(self, closure=None): """Performs a single optimization step. Args: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group["params"]: if p.grad is None: continue grad = p.grad if grad.is_sparse: raise RuntimeError("CAME does not support sparse gradients.") state = self.state[p] grad_shape = grad.shape factored = self._get_options(grad_shape) # State Initialization if len(state) == 0: state["step"] = 0 state["exp_avg"] = torch.zeros_like(grad) if factored: state["exp_avg_sq_row"] = torch.zeros(grad_shape[:-1], dtype=p.dtype, device=p.device) state["exp_avg_sq_col"] = torch.zeros( grad_shape[:-2] + grad_shape[-1:], dtype=p.dtype, device=p.device ) state["exp_avg_res_row"] = torch.zeros(grad_shape[:-1], dtype=p.dtype, device=p.device) state["exp_avg_res_col"] = torch.zeros( grad_shape[:-2] + grad_shape[-1:], dtype=p.dtype, device=p.device ) else: state["exp_avg_sq"] = torch.zeros_like(p) state["step"] += 1 update = (grad**2) + group["eps"][0] if factored: exp_avg_sq_row = state["exp_avg_sq_row"] exp_avg_sq_col = state["exp_avg_sq_col"] exp_avg_sq_row.mul_(group["betas"][1]).add_(update.mean(dim=-1), alpha=1.0 - group["betas"][1]) exp_avg_sq_col.mul_(group["betas"][1]).add_(update.mean(dim=-2), alpha=1.0 - group["betas"][1]) # Approximation of exponential moving average of square of gradient update = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update.mul_(grad) else: exp_avg_sq = state["exp_avg_sq"] exp_avg_sq.mul_(group["betas"][1]).add_(update, alpha=1.0 - group["betas"][1]) update = exp_avg_sq.rsqrt().mul_(grad) update.div_((self._rms(update) / group["clip_threshold"]).clamp_(min=1.0)) exp_avg = state["exp_avg"] exp_avg.mul_(group["betas"][0]).add_(update, alpha=1 - group["betas"][0]) # Confidence-guided strategy # Calculation of instability res = (update - exp_avg) ** 2 + group["eps"][1] if factored: exp_avg_res_row = state["exp_avg_res_row"] exp_avg_res_col = state["exp_avg_res_col"] exp_avg_res_row.mul_(group["betas"][2]).add_(res.mean(dim=-1), alpha=1.0 - group["betas"][2]) exp_avg_res_col.mul_(group["betas"][2]).add_(res.mean(dim=-2), alpha=1.0 - group["betas"][2]) # Approximation of exponential moving average of instability res_approx = self._approx_sq_grad(exp_avg_res_row, exp_avg_res_col) update = res_approx.mul_(exp_avg) else: update = exp_avg.clone() if group["weight_decay"] != 0: p.data.add_(p.data, alpha=-group["weight_decay"] * group["lr"]) update.mul_(group["lr"]) p.data.add_(-update) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/galore.py
colossalai/nn/optimizer/galore.py
""" adapted from https://github.com/jiaweizzhao/GaLore/blob/master/galore_torch/adamw8bit.py""" import warnings from typing import List import torch from bitsandbytes.optim.optimizer import Optimizer2State from torch._C import _LinAlgError def get_galore_param_groups( model, weight_decay, rank=256, update_proj_gap=200, scale=0.25, proj_type="std" ) -> List[dict]: """ It's advised to use this instead of manually specifying which param groups to apply GaLore on. """ galore_params = [] non_galore = [] no_decay_params = [] no_decay = ["bias", "LayerNorm.weight"] for name, param in model.named_parameters(): # Only make sense to do SVD on 2d gradient matrices # e.g. nn.Linear, VocabEmbedding, etc. if any(nd in name for nd in no_decay): no_decay_params.append(param) elif param.dim() == 2: galore_params.append(param) else: non_galore.append(param) param_groups = [ { "params": galore_params, "rank": rank, "update_proj_gap": update_proj_gap, "scale": scale, "proj_type": proj_type, "weight_decay": weight_decay, }, {"params": non_galore, "weight_decay": weight_decay}, {"params": no_decay_params, "weight_decay": 0.0}, ] return param_groups def make_low_rank_buffer(p, grad): """For compatibility with bitsandbytes's update_step, we need an empty low-rank param update buffer to avoid mutating original params. TODO: optimize by reusing the memory for p.grad? Need to modify bitsandbytes? """ p.saved_data = p.data.clone() # p.data = grad.clone().to(p.data.dtype).to(p.data.device) p.data = torch.zeros_like(grad, device=grad.device, dtype=grad.dtype) # p.data.zero_() p.grad = grad class GaLoreProjector: def __init__(self, rank, verbose=False, update_proj_gap=200, scale=1.0, proj_type="std"): self.rank = rank self.verbose = verbose self.update_proj_gap = update_proj_gap self.scale = scale self.ortho_matrix = None self.proj_type = proj_type self.svd_type = None def project(self, full_rank_grad, iter): dim = full_rank_grad.dim() if dim != 2: warnings.warn( f"Warning: You shouldn't specify projection rank for {dim}D params in param_groups. Skipping SVD." ) return full_rank_grad m, n = full_rank_grad.shape # For ZeRO sharded grads if self.proj_type == "std": # Project the lower dim to minimize information loss if self.svd_type is None: self.svd_type = "right" if m >= n else "left" # SVD step if self.ortho_matrix is None or iter % self.update_proj_gap == 0: self.ortho_matrix = self.get_orthogonal_matrix(full_rank_grad, self.rank, type=self.svd_type) if self.svd_type == "right": low_rank_grad = torch.matmul(full_rank_grad, self.ortho_matrix.t()[:n]) else: low_rank_grad = torch.matmul(self.ortho_matrix.t()[:, :m], full_rank_grad) elif self.proj_type == "reverse_std": if self.svd_type is None: self.svd_type = "left" if m >= n else "right" # SVD step if self.ortho_matrix is None or iter % self.update_proj_gap == 0: self.ortho_matrix = self.get_orthogonal_matrix(full_rank_grad, self.rank, type=self.svd_type) if self.svd_type == "left": low_rank_grad = torch.matmul(self.ortho_matrix.t()[:, :m], full_rank_grad) else: low_rank_grad = torch.matmul(full_rank_grad, self.ortho_matrix.t()[:n]) return low_rank_grad def project_back(self, low_rank_grad): if low_rank_grad.dim() != 2: return m, n = low_rank_grad.shape if self.svd_type == "right": full_rank_grad = torch.matmul(low_rank_grad, self.ortho_matrix[:n]) else: full_rank_grad = torch.matmul(self.ortho_matrix[:, :m], low_rank_grad) return full_rank_grad * self.scale # svd decomposition def get_orthogonal_matrix(self, weights, rank, type): module_params = weights if module_params.data.dtype != torch.float: float_data = False original_type = module_params.data.dtype original_device = module_params.data.device matrix = module_params.data.float() else: float_data = True matrix = module_params.data # TODO: redo SVD in the next step. if matrix.isnan().any(): print(f"{__file__}: skipping SVD due to NaN matrix") return self.ortho_matrix try: U, s, Vh = torch.linalg.svd(matrix, full_matrices=False) except _LinAlgError as e: print(f"{__file__}: skipping SVD due to {e}") return self.ortho_matrix # make the smaller matrix always to be orthogonal matrix if type == "right": B = Vh[:rank, :] if not float_data: B = B.to(original_device).type(original_type) return B elif type == "left": A = U[:, :rank] if not float_data: A = A.to(original_device).type(original_type) return A elif type == "full": A = U[:, :rank] B = Vh[:rank, :] if not float_data: A = A.to(original_device).type(original_type) B = B.to(original_device).type(original_type) return [A, B] else: raise ValueError("type should be left, right or full") class GaLoreAdamW8bit(Optimizer2State): r"""Implements Galore, a optimizer-agonistic gradient compression technique on 8-bit AdamW. Proposed in `GaLore: Memory-Efficient LLM Training by Gradient Low-Rank Projection`. It compresses gradient via low-rank projection and is claimed to be insensitive to hyperparams like lr. https://arxiv.org/abs/2403.03507 Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its norm. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-6) weight_decay (float, optional): weight decay (L2 penalty) (default: 0.01) nbits (int): The number of bits of optim states. Only 32 and 8 are supported. min_8bit_size (`int`, defaults to 4096): The minimum number of elements of the parameter tensors for 8-bit optimization. percentile_clipping (`int`, defaults to 100): Adapts clipping threshold automatically by tracking the last 100 gradient norms and clipping the gradient at a certain percentile to improve stability. block_wise (`bool`, defaults to `True`): Whether to independently quantize each block of tensors to reduce outlier effects and improve stability. is_paged (`bool`, defaults to `False`): Whether the optimizer is a paged optimizer (handle memory spike via CPU-GPU transfer) or not. args (dict, optional): quantization-related arguments. If passed, will override all quantization args above. Example: """ def __init__( self, params, lr=1e-2, betas=(0.9, 0.999), eps=1e-8, weight_decay=1e-2, nbits=8, min_8bit_size=4096, percentile_clipping=100, block_wise=True, is_paged=False, args=None, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, optim_bits=nbits, args=args, min_8bit_size=min_8bit_size, percentile_clipping=percentile_clipping, block_wise=block_wise, is_paged=is_paged, ) proj_none = all(["rank" not in group for group in self.param_groups]) if proj_none: warnings.warn( "Will not apply GaLore as no rank is specified. Or did you forget to? Try get_galore_param_groups" ) # Defaults from the paper for group in self.param_groups: if "rank" in group: group["update_proj_gap"] = group.get("update_proj_gap", 200) group["proj_type"] = group.get("proj_type", "std") group["scale"] = group.get("scale", 0.25) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure() if not self.initialized: self.check_overrides() self.to_gpu() # needed for fairseq pure fp16 training self.initialized = True for gindex, group in enumerate(self.param_groups): for pindex, p in enumerate(group["params"]): if p.grad is None: continue if p is self.param_groups[0]["params"][0]: torch.save(p.grad, "grad.pt") state = self.state[p] if "step" not in state: state["step"] = 0 # GaLore Projection if "rank" in group: if "projector" not in state: state["projector"] = GaLoreProjector( group["rank"], scale=group["scale"], update_proj_gap=group["update_proj_gap"], proj_type=group["proj_type"], ) if "weight_decay" in group and group["weight_decay"] > 0: # ensure that the weight decay is not applied to the norm grad group["weight_decay_saved"] = group["weight_decay"] group["weight_decay"] = 0 grad = state["projector"].project(p.grad, state["step"]) make_low_rank_buffer(p, grad) if "state1" not in state: self.init_state(group, p, gindex, pindex) # p.grad = p.grad.contiguous() # avoid bitsandbytes update error # Prefetch if paged self.prefetch_state(p) # Adam update step using the buffer self.update_step(group, p, gindex, pindex) torch.cuda.synchronize() # GaLore Projection Back if "rank" in group: if p is self.param_groups[0]["params"][1]: pass update = state["projector"].project_back(p.data) p.data = p.saved_data.add_(update) # apply weight decay if "weight_decay_saved" in group: p.data.add_(p.data, alpha=-group["lr"] * group["weight_decay_saved"]) group["weight_decay"] = group["weight_decay_saved"] del group["weight_decay_saved"] if self.is_paged: # all paged operation are asynchronous, we need # to sync to make sure all tensors are in the right state torch.cuda.synchronize() return loss def __del__(self): """Avoid buffer memory leak""" for group in self.param_groups: for p in group["params"]: if hasattr(p, "saved_data"): del p.saved_data
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/__init__.py
colossalai/nn/optimizer/__init__.py
from galore_torch import GaLoreAdafactor, GaLoreAdamW from colossalai.logging import get_dist_logger from .came import CAME from .cpu_adam import CPUAdam from .distributed_adafactor import DistributedAdaFactor from .distributed_came import DistributedCAME from .distributed_galore import DistGaloreAwamW from .distributed_lamb import DistributedLamb from .fused_adam import FusedAdam from .fused_lamb import FusedLAMB from .fused_sgd import FusedSGD from .galore import GaLoreAdamW8bit from .hybrid_adam import HybridAdam from .lamb import Lamb from .lars import Lars from .adafactor import Adafactor # noqa __all__ = [ "FusedLAMB", "FusedAdam", "FusedSGD", "Lamb", "Lars", "CPUAdam", "HybridAdam", "DistributedLamb", "DistGaloreAwamW", "GaLoreAdamW", "GaLoreAdafactor", "GaLoreAdamW8bit", "CAME", "DistributedCAME", "Adafactor", "DistributedAdaFactor", ] optim2DistOptim = { GaLoreAdamW8bit: DistGaloreAwamW, Lamb: DistributedLamb, CAME: DistributedCAME, Adafactor: DistributedAdaFactor, } def cast_to_distributed(optim): if optim.__class__ in optim2DistOptim: _logger = get_dist_logger() _logger.info(f"Converting optimizer {optim.__class__.__name__} to its distributed version.", ranks=[0]) if isinstance(optim, GaLoreAdamW8bit): return optim2DistOptim[GaLoreAdamW8bit](optim.param_groups, args=optim.args) return optim2DistOptim[optim.__class__](optim.param_groups) return optim
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/adafactor.py
colossalai/nn/optimizer/adafactor.py
# coding=utf-8 # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math import torch from torch.optim import Optimizer __all__ = ["Adafactor"] # Adafactor class Adafactor(Optimizer): def __init__( self, params, lr=None, eps=(1e-30, 1e-3), clip_threshold=1.0, decay_rate=-0.8, beta1=None, weight_decay=0.0, scale_parameter=True, relative_step=True, warmup_init=False, ): lr = None if lr is not None and relative_step: raise ValueError("Cannot combine manual `lr` and `relative_step=True` options") if warmup_init and not relative_step: raise ValueError("`warmup_init=True` requires `relative_step=True`") defaults = { "lr": lr, "eps": eps, "clip_threshold": clip_threshold, "decay_rate": decay_rate, "beta1": beta1, "weight_decay": weight_decay, "scale_parameter": scale_parameter, "relative_step": relative_step, "warmup_init": warmup_init, } super().__init__(params, defaults) @staticmethod def _get_lr(param_group, param_state): rel_step_sz = param_group["lr"] if param_group["relative_step"]: min_step = 1e-6 * param_state["step"] if param_group["warmup_init"] else 1e-2 rel_step_sz = min(min_step, 1.0 / math.sqrt(param_state["step"])) param_scale = 1.0 if param_group["scale_parameter"]: param_scale = max(param_group["eps"][1], param_state["RMS"]) return param_scale * rel_step_sz @staticmethod def _get_options(param_group, param_shape): factored = len(param_shape) >= 2 use_first_moment = param_group["beta1"] is not None return factored, use_first_moment @staticmethod def _rms(tensor): return tensor.norm(2) / (tensor.numel() ** 0.5) @staticmethod def _approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col): r_factor = (exp_avg_sq_row / exp_avg_sq_row.mean(dim=-1, keepdim=True)).rsqrt_().unsqueeze(-1) c_factor = exp_avg_sq_col.unsqueeze(-2).rsqrt() return torch.mul(r_factor, c_factor) @torch.no_grad() def step(self, closure=None): """ Performs a single optimization step Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() """ param_groups: Dict { "params":[weight, bias] "lr" "eps" "clip_threshold" "decay_rate" "beta1" "weight_decay" "scale_parameter" "relative_step" "warmup_init" } """ for group in self.param_groups: # update weight & bias for p in group["params"]: if p.grad is None: continue """ # grad shape is same as weigh / bias """ grad = p.grad if grad.is_sparse: raise RuntimeError("Adafactor does not support sparse gradients.") """ p is weight state {'step', 'exp_avg_sq_row', 'exp_avg_sq_col', 'RMS' } p is bias state {'step', 'exp_avg_sq', 'RMS' } """ state = self.state[p] grad_shape = grad.shape factored, use_first_moment = self._get_options(group, grad_shape) # State Initialization if len(state) == 0: state["step"] = 0 if use_first_moment: # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(grad) if factored: state["exp_avg_sq_row"] = torch.zeros(grad_shape[:-1], device=grad.device) state["exp_avg_sq_col"] = torch.zeros(grad_shape[:-2] + grad_shape[-1:], device=grad.device) else: state["exp_avg_sq"] = torch.zeros_like(grad) state["RMS"] = 0 else: if use_first_moment: state["exp_avg"] = state["exp_avg"] if factored: state["exp_avg_sq_row"] = state["exp_avg_sq_row"] state["exp_avg_sq_col"] = state["exp_avg_sq_col"] else: state["exp_avg_sq"] = state["exp_avg_sq"] state["step"] += 1 # state["RMS"] = self._rms(p_data_fp32) lr = self._get_lr(group, state) beta2t = 1.0 - math.pow(state["step"], group["decay_rate"]) update = (grad**2) + group["eps"][0] if factored: exp_avg_sq_row = state["exp_avg_sq_row"] exp_avg_sq_col = state["exp_avg_sq_col"] # Exponential average of row indexes exp_avg_sq_row.mul_(beta2t).add_(update.mean(dim=-1), alpha=(1.0 - beta2t)) # Exponential average of columns indexes exp_avg_sq_col.mul_(beta2t).add_(update.mean(dim=-2), alpha=(1.0 - beta2t)) # Approximation of exponential moving average of square of gradient update = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update.mul_(grad) else: exp_avg_sq = state["exp_avg_sq"] exp_avg_sq.mul_(beta2t).add_(update, alpha=(1.0 - beta2t)) update = exp_avg_sq.rsqrt().mul_(grad) # RMS update.div_((self._rms(update) / group["clip_threshold"]).clamp_(min=1.0)) update.mul_(lr) if use_first_moment: exp_avg = state["exp_avg"] exp_avg.mul_(group["beta1"]).add_(update, alpha=(1 - group["beta1"])) update = exp_avg if group["weight_decay"] != 0: p.add_(p, alpha=(-group["weight_decay"] * lr)) p.add_(-update) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/distributed_came.py
colossalai/nn/optimizer/distributed_came.py
from typing import Dict import torch import torch.distributed as dist from colossalai.interface.optimizer import DistributedOptim from colossalai.shardformer.layer._operation import _gather, _split from colossalai.tensor.d_tensor import get_sharding_spec, is_distributed_tensor class DistributedCAME(DistributedOptim): """Implements CAME algorithm. This implementation is based on: `CAME: Confidence-guided Adaptive Memory Efficient Optimization` Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): external learning rate (default: None) eps (tuple[float, float]): regularization constants for square gradient and instability respectively (default: (1e-30, 1e-16)) clip_threshold (float): threshold of root-mean-square of final gradient update (default: 1.0) betas (tuple[float, float, float]): coefficient used for computing running averages of update, square gradient and instability (default: (0.9, 0.999, 0.9999))) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) """ def __init__( self, params, lr=None, eps=(1e-30, 1e-16), clip_threshold=1.0, betas=(0.9, 0.999, 0.9999), weight_decay=0.0, ): defaults = dict( lr=lr, eps=eps, clip_threshold=clip_threshold, betas=betas, weight_decay=weight_decay, ) self.tp_size = 1 self.tp_group = None self.dp_size = 1 self.dp_group = None self.shard_to_working_param = None # Dict{id:shape}, sample {id(param): torch.tensor} self.use_zero = True self.param_is_dtensor_dict = {} # {id(p): True/False} self.grad_shape_dict = {} # {id(p): master param shape} self.factored_dict = {} # {id(p): True/False} self.use_first_moment_dict = {} # {id(p): True/False} self.shard_spec_dict = {} # {id(p): ShardSpec} super(DistributedCAME, self).__init__(params, defaults) @property def supports_memory_efficient_fp16(self): return True @property def supports_flat_params(self): return False def setup_distributed( self, tp_group: dist.ProcessGroup = None, dp_group: dist.ProcessGroup = None, shard_to_working_param: Dict = {}, padding_map=None, use_zero: bool = True, ) -> None: """ Inject features to the Optimizer Args: tp_group: The devices group for tensor parallel; dp_group: The devices group for data parallel; shard_to_working_param (Dict): ZeRO 2 feeds the optimizer a sharded param view as grads are sharded. This maps from id(view) to working params used in forward & backward. padding_map: Interface placeholder use_zero: Whether or not to use zero; """ self.tp_group = tp_group # "Expected row process group" self.dp_group = dp_group if self.tp_group is not None: self.tp_size = dist.get_world_size(self.tp_group) if self.dp_group is not None: self.dp_size = dist.get_world_size(self.dp_group) self.use_zero = use_zero self.shard_to_working_param = shard_to_working_param if shard_to_working_param is not None else {} # grad is None, cause we dont setup now for group in self.param_groups: for p in group["params"]: # w/o ZeRO: master param = working param self.shard_to_working_param[id(p)] = self.shard_to_working_param.get(id(p), p) self.param_is_dtensor_dict[id(p)] = is_distributed_tensor(self.shard_to_working_param[id(p)]) self.grad_shape_dict[id(p)] = self.shard_to_working_param[id(p)].shape # Avoid row parallel lead H=1, then factored param is determined as not factored; if self.param_is_dtensor_dict[id(p)]: self.shard_spec_dict[id(p)] = get_sharding_spec(self.shard_to_working_param[id(p)]) if self.shard_spec_dict[id(p)].sharding_sequence[0] == "R": self.factored_dict[id(p)] = True elif self.shard_spec_dict[id(p)].sharding_sequence[-1] == "R": self.factored_dict[id(p)] = True else: self.factored_dict[id(p)] = self._get_options(self.grad_shape_dict[id(p)]) else: self.shard_spec_dict[id(p)] = None self.factored_dict[id(p)] = self._get_options(self.grad_shape_dict[id(p)]) @staticmethod def _get_options(param_shape): factored = len(param_shape) >= 2 return factored @staticmethod def _rms(tensor, param_is_dtensor, use_zero, tp_size, dp_size, tp_group, dp_group): tensor_sum = tensor.pow(2).sum() num_of_element = tensor.numel() if param_is_dtensor: # reduce tensor_sum from tp_group dist.all_reduce(tensor_sum, group=tp_group) num_of_element = num_of_element * tp_size if use_zero: dist.all_reduce(tensor_sum, group=dp_group) num_of_element = num_of_element * dp_size rms = (tensor_sum / num_of_element).sqrt() return rms @staticmethod def _approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col): r_factor = (exp_avg_sq_row / exp_avg_sq_row.mean(dim=-1, keepdim=True)).rsqrt_().unsqueeze(-1) c_factor = exp_avg_sq_col.unsqueeze(-2).rsqrt() return torch.mul(r_factor, c_factor) # approx_sq_grad for row parallel weight @staticmethod def _approx_sq_grad_row_parallel(exp_avg_sq_row, exp_avg_sq_col, sq_row_meam): r_factor = (exp_avg_sq_row / sq_row_meam).rsqrt_().unsqueeze(-1) c_factor = exp_avg_sq_col.unsqueeze(-2).rsqrt() return torch.mul(r_factor, c_factor) def _col_parallel_factor(self, update, grad, state_row, state_col, grad_shape, beta2t): if grad_shape[0] % self.dp_size != 0: # gather update[flatten] along dp group then reshape to [H, W/tp] update = _gather(input_=update, dim=-1, process_group=self.dp_group) update_reshape = update.view(-1, grad_shape[1]) # gather grad[flatten] along dp group then reshape to [H, W/tp] grad = _gather(input_=grad, dim=-1, process_group=self.dp_group) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state_row # [H] exp_avg_sq_col = state_col # [W/tp] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) else: update_reshape = update.view(-1, grad_shape[1]) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state_row # [H] exp_avg_sq_col = state_col # [W/tp] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) dist.all_reduce(exp_avg_sq_row, group=self.tp_group) exp_avg_sq_row.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) if self.use_zero: update = update_reshape.view(-1) else: update = update_reshape return update def _row_parallel_factor(self, update, grad, state_row, state_col, grad_shape, beta2t): if grad_shape[0] % self.dp_size != 0: # gather update[flatten] along dp group then reshape to [H/tp, W] update = _gather(input_=update, dim=-1, process_group=self.dp_group) # view update to origin[tp] shape update_reshape = update.view(-1, grad_shape[1]) # gather grad[flatten] along dp group then reshape to [H/tp, W] grad = _gather(input_=grad, dim=-1, process_group=self.dp_group) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state_row # [H] exp_avg_sq_col = state_col # [W/tp] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) if self.use_zero: update = _split(input_=update_reshape.view(-1), dim=-1, process_group=self.dp_group) else: update = update_reshape else: update_reshape = update.view(-1, grad_shape[1]) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state_row # [H] exp_avg_sq_col = state_col # [W/tp] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) # gather row exp_avg_sq_row_gather = _gather(input_=exp_avg_sq_row, dim=-1, process_group=self.tp_group) sq_row_meam = exp_avg_sq_row_gather.mean(dim=-1, keepdim=True) update_reshape = self._approx_sq_grad_row_parallel(exp_avg_sq_row, exp_avg_sq_col, sq_row_meam) update_reshape.mul_(grad_reshape) if self.use_zero: update = update_reshape.view(-1) else: update = update_reshape return update def _base_factor(self, update, grad, state_row, state_col, grad_shape, beta2t): if self.use_zero: # only zero # [30522, 128], [2, 128] if grad_shape[0] % self.dp_size != 0: # view update to origin shape update.view(grad_shape[0]//self.data_parallel_size , grad_shape[1]) # row mean no change # col mean need reduce and div # gather update[flatten] along dp group then reshape to [H, W] update = _gather(input_=update, dim=-1, process_group=self.dp_group) # view update to origin[tp] shape update_reshape = update.view(-1, grad_shape[1]) # gather grad[flatten] along dp group then reshape to [H, W] grad = _gather(input_=grad, dim=-1, process_group=self.dp_group) grad_reshape = grad.view(-1, grad_shape[1]) exp_avg_sq_row = state_row # [H/dp] exp_avg_sq_col = state_col # [W] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) update = _split(input_=update_reshape.view(-1), dim=-1, process_group=self.dp_group) else: # no residual row # view update to origin[tp] shape update_reshape = update.view(-1, grad_shape[1]) # [H/dp, W] grad_reshape = grad.view(-1, grad_shape[1]) # [H/dp, W] exp_avg_sq_row = state_row # [H/dp] exp_avg_sq_col = state_col # [W] exp_avg_sq_row.mul_(beta2t).add_(update_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(update_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) update_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update_reshape.mul_(grad_reshape) update = update_reshape.view(-1) else: # # base factor; no tp, no dp exp_avg_sq_row = state_row # [H/dp] exp_avg_sq_col = state_col # [W] # Exponential average of row indexes exp_avg_sq_row.mul_(beta2t).add_(update.mean(dim=-1), alpha=(1.0 - beta2t)) # Exponential average of columns indexes exp_avg_sq_col.mul_(beta2t).add_(update.mean(dim=-2), alpha=(1.0 - beta2t)) # Approximation of exponential moving average of square of gradient update = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) update.mul_(grad) return update # factor def _base_res_factor(self, res, exp_avg, state_row, state_col, grad_shape, beta2t): if self.use_zero: # only zero if grad_shape[0] % self.dp_size != 0: # view res to origin shape res.view(grad_shape[0]//self.data_parallel_size , grad_shape[1]) # row mean no change # col mean need reduce and div # gather res[flatten] along dp group then reshape to [H, W] res = _gather(input_=res, dim=-1, process_group=self.dp_group) # view res to origin[tp] shape res_reshape = res.view(-1, grad_shape[1]) # gather exp_avg[flatten] along dp group then reshape to [H, W] exp_avg = _gather(input_=exp_avg, dim=-1, process_group=self.dp_group) exp_avg_reshape = exp_avg.view(-1, grad_shape[1]) exp_avg_sq_row = state_row # [H/dp] exp_avg_sq_col = state_col # [W] exp_avg_sq_row.mul_(beta2t).add_(res_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(res_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) res_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) res_reshape.mul_(exp_avg_reshape) res = _split(input_=res_reshape.view(-1), dim=-1, process_group=self.dp_group) else: # no residual row # view res to origin[tp] shape res_reshape = res.view(-1, grad_shape[1]) # [H/dp, W] exp_avg_reshape = exp_avg.view(-1, grad_shape[1]) # [H/dp, W] exp_avg_sq_row = state_row # [H/dp] exp_avg_sq_col = state_col # [W] exp_avg_sq_row.mul_(beta2t).add_(res_reshape.mean(dim=-1), alpha=(1.0 - beta2t)) exp_avg_sq_col.mul_(beta2t).add_(res_reshape.mean(dim=-2), alpha=(1.0 - beta2t)) # reduce col dist.all_reduce(exp_avg_sq_col, group=self.tp_group) exp_avg_sq_col.div_(self.tp_size) res_reshape = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) res_reshape.mul_(exp_avg_reshape) res = res_reshape.view(-1) else: # # base factor; no tp, no dp exp_avg_sq_row = state_row # [H/dp] exp_avg_sq_col = state_col # [W] # Exponential average of row indexes exp_avg_sq_row.mul_(beta2t).add_(res.mean(dim=-1), alpha=(1.0 - beta2t)) # Exponential average of columns indexes exp_avg_sq_col.mul_(beta2t).add_(res.mean(dim=-2), alpha=(1.0 - beta2t)) # Approximation of exponential moving average of square of gradient res = self._approx_sq_grad(exp_avg_sq_row, exp_avg_sq_col) res.mul_(exp_avg) return res @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Args: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group["params"]: if p.grad is None: continue grad = p.grad if grad.is_sparse: raise RuntimeError("CAME does not support sparse gradients.") state = self.state[p] # Under zero the grad_shape is the original grad that is flattened and then cut (only one dimension) grad_shape = grad.shape grad_shape = self.grad_shape_dict[id(p)] param_is_dtensor = self.param_is_dtensor_dict[id(p)] if param_is_dtensor: grad_shape = self.shard_to_working_param.get(id(p)).shape # tp shape (2 dim) factored = self.factored_dict[id(p)] shard_spec = self.shard_spec_dict[id(p)] # State Initialization if len(state) == 0: state["step"] = 0 state["exp_avg"] = torch.zeros_like(p) if factored: if param_is_dtensor: if shard_spec.sharding_sequence[0] == "R": # Col Parallel if grad_shape[0] % self.dp_size != 0: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0], device=p.device, dtype=p.dtype ) # [H] state["exp_avg_res_row"] = torch.zeros( grad_shape[0], device=p.device, dtype=p.dtype ) # [H] else: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=p.device, dtype=p.dtype ) # [H/dp] state["exp_avg_res_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=p.device, dtype=p.dtype ) # [H/dp] state["exp_avg_sq_col"] = torch.zeros( grad_shape[1], device=p.device, dtype=p.dtype ) # [W/TP] state["exp_avg_res_col"] = torch.zeros( grad_shape[1], device=p.device, dtype=p.dtype ) # [W/TP] if shard_spec.sharding_sequence[-1] == "R": # Row Parallel # Row indivisible shape situation if grad_shape[0] % self.dp_size != 0: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0], device=p.device, dtype=p.dtype ) # [H/tp] state["exp_avg_res_row"] = torch.zeros( grad_shape[0], device=p.device, dtype=p.dtype ) # [H/tp] else: state["exp_avg_sq_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=p.device, dtype=p.dtype ) # [H/dp/tp] state["exp_avg_res_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=p.device, dtype=p.dtype ) # [H/dp/tp] state["exp_avg_sq_col"] = torch.zeros( grad_shape[1], device=p.device, dtype=p.dtype ) # [W] state["exp_avg_res_col"] = torch.zeros( grad_shape[1], device=p.device, dtype=p.dtype ) # [W] else: if self.use_zero: if grad_shape[0] % self.dp_size != 0: # save all exp_avg_sq_row [H] state["exp_avg_sq_row"] = torch.zeros( grad_shape[0], device=grad.device, dtype=p.dtype ) state["exp_avg_res_row"] = torch.zeros( grad_shape[0], device=grad.device, dtype=p.dtype ) else: # exp_avg_sq_row [H // dp] state["exp_avg_sq_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=grad.device, dtype=p.dtype ) state["exp_avg_res_row"] = torch.zeros( grad_shape[0] // self.dp_size, device=grad.device, dtype=p.dtype ) else: # exp_avg_sq_row [H] state["exp_avg_sq_row"] = torch.zeros(grad_shape[0], device=grad.device, dtype=p.dtype) state["exp_avg_res_row"] = torch.zeros(grad_shape[0], device=grad.device, dtype=p.dtype) # exp_avg_sq_col alaways [W] state["exp_avg_sq_col"] = torch.zeros(grad_shape[1], device=grad.device, dtype=p.dtype) state["exp_avg_res_col"] = torch.zeros(grad_shape[1], device=grad.device, dtype=p.dtype) else: state["exp_avg_sq"] = torch.zeros_like(p) state["RMS"] = 0 else: if factored: state["exp_avg_sq_row"] = state["exp_avg_sq_row"] state["exp_avg_sq_col"] = state["exp_avg_sq_col"] state["exp_avg_res_row"] = state["exp_avg_sq_row"] state["exp_avg_res_col"] = state["exp_avg_sq_col"] else: state["exp_avg_sq"] = state["exp_avg_sq"] state["step"] += 1 update = (grad**2) + group["eps"][0] if factored: if param_is_dtensor: # ============================== # First Dim is R, Last Dim is S{} means split dim -1 ---> # Coloum Parallel ---> sq_row need Do (col) Reduce # ============================== if shard_spec.sharding_sequence[0] == "R": update = self._col_parallel_factor( update, grad, state["exp_avg_sq_row"], state["exp_avg_sq_col"], grad_shape, group["betas"][1], ) # ============================== # Last Dim is R, First Dim is S{} means split dim 0 ---> # Row Parallel ---> sq_col need Do (row) Reduce # ============================== elif shard_spec.sharding_sequence[-1] == "R": update = self._row_parallel_factor( update, grad, state["exp_avg_sq_row"], state["exp_avg_sq_col"], grad_shape, group["betas"][1], ) else: update = self._base_factor( update, grad, state["exp_avg_sq_row"], state["exp_avg_sq_col"], grad_shape, group["betas"][1], ) else: exp_avg_sq = state["exp_avg_sq"] exp_avg_sq.mul_(group["betas"][1]).add_(update, alpha=(1.0 - group["betas"][1])) update = exp_avg_sq.rsqrt().mul_(grad) rms = self._rms( update, param_is_dtensor, self.use_zero, self.tp_size, self.dp_size, self.tp_group, self.dp_group, ) update.div_((rms / group["clip_threshold"]).clamp_(min=1.0)) exp_avg = state["exp_avg"] exp_avg.mul_(group["betas"][0]).add_(update, alpha=1 - group["betas"][0]) # Confidence-guided strategy # Calculation of instability res = (update - exp_avg) ** 2 + group["eps"][1] if factored: if param_is_dtensor: # ============================== # First Dim is R, Last Dim is S{} means split dim -1 ---> # Coloum Parallel ---> sq_row need Do (col) Reduce # ============================== if shard_spec.sharding_sequence[0] == "R": update = self._col_parallel_factor( res, exp_avg, state["exp_avg_res_row"], state["exp_avg_res_col"], grad_shape, group["betas"][2], ) # ============================== # Last Dim is R, First Dim is S{} means split dim 0 ---> # Row Parallel ---> sq_col need Do (row) Reduce # ============================== elif shard_spec.sharding_sequence[-1] == "R": update = self._row_parallel_factor( res, exp_avg, state["exp_avg_res_row"], state["exp_avg_res_col"], grad_shape, group["betas"][2], ) else: update = self._base_res_factor( res, exp_avg, state["exp_avg_res_row"], state["exp_avg_res_col"], grad_shape, group["betas"][2], ) else: update = exp_avg if group["weight_decay"] != 0: p.add_(p, alpha=-group["weight_decay"] * group["lr"]) update.mul_(group["lr"]) p.add_(-update) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/lamb.py
colossalai/nn/optimizer/lamb.py
""" Adapted from the pytorch-lamb library at https://github.com/cybertronai/pytorch-lamb """ import torch from torch.optim import Optimizer class Lamb(Optimizer): r"""Implements Lamb algorithm. It has been proposed in `Large Batch Optimization for Deep Learning: Training BERT in 76 minutes`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-6) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) adam (bool, optional): always use trust ratio = 1, which turns this into Adam. Useful for comparison purposes. .. _Large Batch Optimization for Deep Learning\: Training BERT in 76 minutes: https://arxiv.org/abs/1904.00962 """ def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-6, weight_decay=0, adam=False, bias_correction=False ): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0])) if not 0.0 <= betas[1] < 1.0: raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1])) defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, bias_correction=bias_correction) self.adam = adam super(Lamb, self).__init__(params, defaults) def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group["params"]: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError("Lamb does not support sparse gradients, consider SparseAdam instead.") state = self.state[p] # State initialization if len(state) == 0: state["step"] = 0 # Exponential moving average of gradient values state["exp_avg"] = torch.zeros_like(p) # Exponential moving average of squared gradient values state["exp_avg_sq"] = torch.zeros_like(p) exp_avg, exp_avg_sq = state["exp_avg"], state["exp_avg_sq"] beta1, beta2 = group["betas"] state["step"] += 1 # Decay the first and second moment running average coefficient # m_t exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) # v_t exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) # NOTE: Paper v3 does not use debiasing. scaled_lr = group["lr"] if group["bias_correction"]: bias_correction1 = 1 - beta1 ** state["step"] bias_correction2 = 1 - beta2 ** state["step"] # Apply debiasing to lr to avoid broadcast scaled_lr *= (bias_correction2**0.5) / bias_correction1 # exp_avg.div_(bias_correction1) # exp_avg_sq.div_(bias_correction2) weight_norm = p.data.pow(2).sum().sqrt() adam_step = exp_avg / exp_avg_sq.sqrt().add(group["eps"]) if group["weight_decay"] != 0: adam_step.add_(p.data, alpha=group["weight_decay"]) adam_norm = adam_step.pow(2).sum().sqrt() if weight_norm == 0 or adam_norm == 0: trust_ratio = 1 else: trust_ratio = weight_norm / adam_norm if self.adam: trust_ratio = 1 p.data.add_(adam_step, alpha=-scaled_lr * trust_ratio) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/nvme_optimizer.py
colossalai/nn/optimizer/nvme_optimizer.py
import math import os import tempfile from typing import Callable, Dict, List, Optional import torch from torch.nn.parameter import Parameter class NVMeOptimizer(torch.optim.Optimizer): """A base class for offloading optimizer states. Args: params: parameters defaults (dict): default dict nvme_offload_fraction (float, optional): Fraction of params to be offloaded to NVMe. Defaults to 0.0. offload_dir (Optional[str], optional): Directory to save NVMe offload files. If it's ``None``, a random temporary directory will be used. Defaults to None. Raises: ImportError: Raise if ``tensornvme`` is not installed. """ def __init__( self, params, defaults: dict, nvme_offload_fraction: float = 0.0, offload_dir: Optional[str] = None ) -> None: assert 0.0 <= nvme_offload_fraction <= 1.0 super().__init__(params, defaults) self.nvme_offload_fraction = float(nvme_offload_fraction) if self.nvme_offload_fraction > 0.0: try: from tensornvme import DiskOffloader from tensornvme._C import get_backends except ModuleNotFoundError: raise ModuleNotFoundError("Please install tensornvme to use NVMeOptimizer") self.offload_dir = offload_dir or tempfile.mkdtemp() backend = "uring" if "uring" in get_backends() else "aio" self.offloader = DiskOffloader(self.offload_dir, 8, backend=backend) else: self.offload_dir = None self.offloader = None self.is_on_nvme: Dict[Parameter, bool] = {} self.offloaded_numel: int = 0 # As param may be not materialized here, these attributes are initialized when the first step self.total_numel: Optional[int] = None self.can_offload_numel: Optional[int] = None self.prefetch_params: List[Parameter] = [] self.param_to_prefetch_idx: Dict[Parameter, int] = {} def _get_numel(self) -> int: numel = 0 for group in self.param_groups: for p in group["params"]: numel += p.storage().size() return numel def _post_state_init(self, param: Parameter) -> None: numel = param.storage().size() if ( self.offloader is not None and param.device.type == "cpu" and numel + self.offloaded_numel <= self.can_offload_numel ): self.is_on_nvme[param] = True self.offloaded_numel += numel else: self.is_on_nvme[param] = False def _setup_prefetch_params(self) -> List[Parameter]: if self.offloader is None: return assert len(self.prefetch_params) == 0 and len(self.param_to_prefetch_idx) == 0 for group in self.param_groups: for p in group["params"]: if p.grad is None: continue if len(self.state[p]) > 0 and self.is_on_nvme[p]: assert p.device.type == "cpu" self.param_to_prefetch_idx[p] = len(self.prefetch_params) self.prefetch_params.append(p) def _pre_step(self, *state_keys: str) -> None: if self.total_numel is None: self.total_numel = self._get_numel() self.can_offload_numel = math.floor(self.total_numel * self.nvme_offload_fraction) self._setup_prefetch_params() if self.offloader is None or len(self.prefetch_params) == 0: return state = self.state[self.prefetch_params[0]] for key in state_keys: self.offloader.async_read(state[key]) def _pre_update(self, param: Parameter, *state_keys: str) -> None: if self.offloader is None or param not in self.param_to_prefetch_idx: return self.offloader.sync_read_events() idx = self.param_to_prefetch_idx[param] if idx + 1 < len(self.prefetch_params): state = self.state[self.prefetch_params[idx + 1]] for key in state_keys: self.offloader.async_read(state[key]) def _post_update(self, param: Parameter, *state_keys: str) -> None: if self.offloader is None: return self.offloader.sync_write_events() if self.is_on_nvme[param]: state = self.state[param] for key in state_keys: self.offloader.async_write(state[key]) def _post_step(self) -> None: if self.offloader is not None: self.offloader.synchronize() self.prefetch_params.clear() self.param_to_prefetch_idx.clear() def step(self, closure: Optional[Callable[[], float]] = ...) -> Optional[float]: """Performs a single optimization step (parameter update). Example: >>> self._pre_step('exp_avg', 'exp_avg_sq') >>> for group in self.param_groups: >>> for p in group['params']: >>> if p.grad is None: >>> continue >>> state = self.state[p] >>> if len(state) == 0: >>> state['exp_avg'] = ... >>> state['exp_avg_sq'] = ... >>> self._post_state_init(p) >>> if p.device.type == 'cpu': >>> self._pre_update(p, 'exp_avg', 'exp_avg_sq') >>> adam() >>> self._post_update(p, 'exp_avg', 'exp_avg_sq') >>> else: >>> ... >>> self._post_step() Args: closure (Optional[Callable[[], float]], optional): A closure that reevaluates the model and returns the loss. Optional for most optimizers. """ raise NotImplementedError def state_dict(self) -> dict: # TODO(ver217): design a new method to save state_dict. When using NVMe offload, this method may lead to OOM. if self.offloader is not None: raise NotImplementedError return super().state_dict() def load_state_dict(self, state_dict: dict) -> None: # TODO(ver217): design a new method to load state_dict. When using NVMe offload, whole state_dict may not be able to fit in memory. if self.offloader is not None: raise NotImplementedError super().load_state_dict(state_dict) def __del__(self) -> None: if getattr(self, "offloader", None) is not None: del self.offloader if os.path.exists(self.offload_dir): try: os.rmdir(self.offload_dir) except OSError: pass
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/distributed_galore.py
colossalai/nn/optimizer/distributed_galore.py
""" adapted from https://github.com/jiaweizzhao/GaLore/blob/master/galore_torch/adamw8bit.py""" import warnings from collections import defaultdict from typing import Dict, Optional import torch import torch.distributed as dist import torch.nn.functional as F from bitsandbytes.optim.optimizer import Optimizer2State from colossalai.interface.optimizer import DistributedOptim from colossalai.tensor.d_tensor import get_shard_dim_1d, is_distributed_tensor from .galore import GaLoreProjector, make_low_rank_buffer __all__ = ["DistributedGalore"] # Mark sharded dimension class DistGaloreAwamW(DistributedOptim, Optimizer2State): r"""Implements Galore, a optimizer-agonistic gradient compression technique on 8-bit AdamW. It largely compresses gradient via low-rank projection and is claimed to be insensitive to hyperparams like lr. Supports Tensor Parallel and ZeRO stage 1 and 2 via booster and plugin. Proposed in `GaLore: Memory-Efficient LLM Training by Gradient Low-Rank Projection` https://arxiv.org/abs/2403.03507 Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its norm. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-6) weight_decay (float, optional): weight decay (L2 penalty) (default: 0.01) nbits: Number of bits for quantization optim states. Only 32 and 8 are supported. min_8bit_size (`int`, defaults to 4096): The minimum number of elements of the parameter tensors for 8-bit optimization. percentile_clipping (`int`, defaults to 100): Adapts clipping threshold automatically by tracking the last 100 gradient norms and clipping the gradient at a certain percentile to improve stability. block_wise (`bool`, defaults to `True`): Whether to independently quantize each block of tensors to reduce outlier effects and improve stability. is_paged (`bool`, defaults to `False`): Whether the optimizer is a paged optimizer (handle memory spike via CPU-GPU transfer) or not. args (dict, optional): quantization-related arguments. If passed, will override all quantization args above. """ def __init__( self, params, lr=1e-2, betas=(0.9, 0.999), eps=1e-8, weight_decay=1e-2, nbits=8, min_8bit_size=4096, percentile_clipping=100, block_wise=True, is_paged=False, args=None, ): super().__init__( "adam", params, lr, betas, eps, weight_decay, optim_bits=nbits, args=args, min_8bit_size=min_8bit_size, percentile_clipping=percentile_clipping, block_wise=block_wise, is_paged=is_paged, ) self.tp_size = 1 self.dp_size = 1 self.is_dist = {} proj_none = all(["rank" not in group for group in self.param_groups]) if proj_none: warnings.warn( "Will not apply GaLore as rank isn't in any param group. If you forgot to, try get_galore_param_groups" ) # Default from the paper for group in self.param_groups: if "rank" in group: group["update_proj_gap"] = group.get("update_proj_gap", 200) group["proj_type"] = group.get("proj_type", "std") group["scale"] = group.get("scale", 0.25) def setup_distributed( self, tp_group: Optional[dist.ProcessGroup] = None, dp_group: Optional[dist.ProcessGroup] = None, shard_to_working_param: Optional[Dict] = {}, padding_map: Optional[Dict] = defaultdict(int), is_zero: Optional[bool] = False, ): """Setup process groups for TP and ZeRO 2. Arguments: tp_group (dist.ProcessGroup): Tensor Parallel process group dp_group (dist.ProcessGroup): ZeRO 2 process group shard_to_working_param (Dict): ZeRO 2 feeds the optimizer a sharded param view as grads are sharded. This maps from id(view) to working params used in forward & backward. padding_map (Dict): Padding size of each param from ZeRO's param store. Required if ZeRO is used. is_zero (bool): Whether to use ZeRO 2. """ assert dist.is_initialized(), "You forgot to initialized distributed backend..." self.tp_group = tp_group self.dp_group = dp_group if tp_group is not None: self.tp_size = dist.get_world_size(tp_group) if dp_group is not None: self.dp_size = dist.get_world_size(dp_group) self.shard_to_working_param = shard_to_working_param if shard_to_working_param is not None else {} self.is_zero = is_zero and self.dp_size > 1 self.padding_map = padding_map if padding_map is not None else defaultdict(int) if is_zero: assert self.padding_map is not defaultdict( int ), "We can't do SVD without knowing ZeRO's per-param padding size" self.distributed_on = self.tp_size > 0 or self.dp_size > 0 # Cache working param layout self.shard_dim = {} for group in self.param_groups: for p in group["params"]: # w/o ZeRO: master param = working param self.shard_to_working_param[id(p)] = self.shard_to_working_param.get(id(p), p) if id(p) not in self.padding_map: self.padding_map[id(p)] = 0 self.is_dist[id(p)] = is_distributed_tensor(self.shard_to_working_param[id(p)]) if is_distributed_tensor(self.shard_to_working_param[id(p)]): self.shard_dim[id(p)] = get_shard_dim_1d(self.shard_to_working_param[id(p)]) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure() if not self.initialized: self.check_overrides() self.to_gpu() self.initialized = True for gindex, group in enumerate(self.param_groups): for pindex, p in enumerate(group["params"]): if p.grad is None: continue state = self.state[p] if "step" not in state: state["step"] = 0 # GaLore Projection if "rank" in group: if "projector" not in state: state["projector"] = GaLoreProjector( group["rank"], scale=group["scale"], update_proj_gap=group["update_proj_gap"], proj_type=group["proj_type"], ) # decoupled weight decay if "weight_decay" in group and group["weight_decay"] > 0: group["weight_decay_saved"] = group["weight_decay"] group["weight_decay"] = 0 grad = p.grad working_shape = list(self.shard_to_working_param[id(p)].shape) padding = self.padding_map[id(p)] # All-gather grads for projection step if self.distributed_on: # Gather for ZeRO 1 & 2 implementation don't retain full grads if self.is_zero: # (m, n).flatten().chunk(dp_size) equals to (m / dp_size, n).flatten() working_shape[0] //= self.dp_size # Gather grads for projection if state["step"] % group["update_proj_gap"] == 0: all_grads = [ torch.empty_like(grad, dtype=p.grad.dtype, device=p.grad.device) for _ in range(self.dp_size) ] dist.all_gather(all_grads, grad, self.dp_group) grad = torch.cat(all_grads) # To working param shape if padding > 0: grad = grad[:-padding] working_shape[0] *= self.dp_size grad = grad.reshape(working_shape) # unflatten # Gather TP grads if self.is_dist[id(p)] and state["step"] % group["update_proj_gap"] == 0: all_grads = [ torch.empty_like(grad, dtype=p.grad.dtype, device=p.grad.device) for _ in range(self.tp_size) ] dist.all_gather(all_grads, grad.contiguous(), self.tp_group) grad = torch.cat(all_grads, dim=self.shard_dim[id(p)]) # Compute SVD. Will use a subset of singular vectors when grads are sharded. grad = state["projector"].project(grad, state["step"]) # Re-shard gathered grads after SVD if self.distributed_on and state["step"] % group["update_proj_gap"] == 0: # TP if self.is_dist[id(p)]: grad = grad.chunk(self.tp_size, dim=self.shard_dim[id(p)])[dist.get_rank(self.tp_group)] # ZeRO # TODO: this might not work with padding, e.g. (3, 3) with dp size 2 # Need extra logic in ZeRO to pad nRows/nCols to be divisible by dp_size if self.is_zero: grad = grad.chunk(self.dp_size)[dist.get_rank(self.dp_group)] grad = grad.contiguous() # avoid bitsandbytes update error working_shape = grad.shape # To flattended master param shape grad = self.to_master_shape(grad, padding) make_low_rank_buffer(p, grad) if "state1" not in state: self.init_state(group, p, gindex, pindex) self.prefetch_state(p) self.update_step(group, p, gindex, pindex) torch.cuda.synchronize() # Project Back to working param shape if "rank" in group: # Unpad if self.is_zero: if padding > 0: p.data = p.data[:-padding] p.data = p.data.reshape(working_shape) p.data = state["projector"].project_back(p.data) # Re-flatten grads for ZeRO p.data = self.to_master_shape(p.data, padding) p.data = p.saved_data.add_(p.data) # apply decoupled weight decay if "weight_decay_saved" in group: p.data.add_(p.data, alpha=-group["lr"] * group["weight_decay_saved"]) group["weight_decay"] = group["weight_decay_saved"] del group["weight_decay_saved"] if self.is_paged: # all paged operation are asynchronous, we need # to sync to make sure all tensors are in the right state torch.cuda.synchronize() return loss def to_master_shape(self, data, padding): """Pad to master (optimizer) param shape""" if not self.is_zero: return data data = data.view(-1) if padding > 0: data = F.pad(data, [0, padding]) return data def __del__(self): """Avoid buffer memory leak""" for group in self.param_groups: for p in group["params"]: if hasattr(p, "saved_data"): del p.saved_data
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/cpu_adam.py
colossalai/nn/optimizer/cpu_adam.py
import math from typing import Optional import torch from colossalai.kernel.kernel_loader import CPUAdamLoader from .nvme_optimizer import NVMeOptimizer class CPUAdam(NVMeOptimizer): """ Implements Adam algorithm. Supports parameters updating on both GPU and CPU, depending on the device of parameters. But the parameters and gradients should on the same device: * Parameters on CPU and gradients on CPU is allowed. * Parameters on GPU and gradients on GPU is allowed. * Parameters on GPU and gradients on CPU is **not** allowed. `CPUAdam` requires CUDA extensions which can be built during installation or runtime. This version of CPU Adam accelerates parameters updating on CPU with SIMD. Support of AVX2 or AVX512 is required. The GPU part is implemented in an naive way. CPU Adam also supports the hybrid precision calculation, eg. fp32 parameters and fp16 gradients. :class:`colossalai.nn.optimizer.CPUAdam` may be used as a drop-in replacement for ``torch.optim.AdamW``, or ``torch.optim.Adam`` with ``adamw_mode=False`` Adam was been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: model_params (iterable): iterable of parameters of dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) NOT SUPPORTED yet in CPUAdam! adamw_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) simd_log (boolean, optional): whether to show if you are using SIMD to accelerate. (default: False) nvme_offload_fraction (float, optional): Fraction of optimizer states to be offloaded to NVMe. Defaults to 0.0. nvme_offload_dir (Optional[str], optional): Directory to save NVMe offload files. If it's ``None``, a random temporary directory will be used. Defaults to None. .. _Adam\: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ # Number of fp32 shards for per parameter # Param weight, grad, momentum and variance num_fp32_shards_per_param = 4 def __init__( self, model_params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, adamw_mode=True, nvme_offload_fraction: float = 0.0, nvme_offload_dir: Optional[str] = None, ): default_args = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, bias_correction=bias_correction) super(CPUAdam, self).__init__(model_params, default_args, nvme_offload_fraction, nvme_offload_dir) self.adamw_mode = adamw_mode cpu_adam = CPUAdamLoader().load() # if you find yourself stuck here, make sure that you install colossalai with BUILD_EXT=1 specification self.cpu_adam_op = cpu_adam.CPUAdamOptimizer(lr, betas[0], betas[1], eps, weight_decay, adamw_mode) def load_state_dict(self, state_dict): super().load_state_dict(state_dict) for group in self.param_groups: for p in group["params"]: state = self.state[p] if "step" in state and isinstance(state["step"], torch.Tensor): state["step"] = int(state["step"].item()) def torch_adam_update( self, data, grad, exp_avg, exp_avg_sq, lr, beta1, beta2, eps, weight_decay, bias_correction1, bias_correction2, use_adamw=False, ): grad = grad.to(data.dtype) if weight_decay != 0: if use_adamw: data.mul_(1 - lr * weight_decay) else: grad = grad.add(data, alpha=weight_decay) # Decay the first and second moment running average coefficient exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) # TODO(jiaruifang) dose not support amsgrad denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(eps) step_size = lr / bias_correction1 data.addcdiv_(exp_avg, denom, value=-step_size) @torch.no_grad() def step(self, closure=None, div_scale: float = -1): loss = None if closure is not None: with torch.enable_grad(): loss = closure() self._pre_step("exp_avg", "exp_avg_sq") for _, group in enumerate(self.param_groups): for _, p in enumerate(group["params"]): if p.grad is None: continue state = self.state[p] target_device = p.device if len(state) == 0: state["step"] = 0 # gradient momentums state["exp_avg"] = torch.zeros_like(p, device=target_device) # gradient variances state["exp_avg_sq"] = torch.zeros_like(p, device=target_device) self._post_state_init(p) state["step"] += 1 beta1, beta2 = group["betas"] if target_device.type == "cpu": assert p.data.numel() == p.grad.data.numel(), "parameter and gradient should have the same size" assert state["exp_avg"].device.type == "cpu", "exp_avg should stay on cpu" assert state["exp_avg_sq"].device.type == "cpu", "exp_avg should stay on cpu" self._pre_update(p, "exp_avg", "exp_avg_sq") if p.grad.dtype is torch.bfloat16: # cpu adam kernel does not support bf16 now bias_correction1 = 1 - beta1 ** state["step"] bias_correction2 = 1 - beta2 ** state["step"] self.torch_adam_update( p.data, p.grad.data, state["exp_avg"], state["exp_avg_sq"], group["lr"], beta1, beta2, group["eps"], group["weight_decay"], bias_correction1, bias_correction2, self.adamw_mode, ) else: self.cpu_adam_op.step( state["step"], group["lr"], beta1, beta2, group["eps"], group["weight_decay"], group["bias_correction"], p.data, p.grad.data, state["exp_avg"], state["exp_avg_sq"], div_scale, ) self._post_update(p, "exp_avg", "exp_avg_sq") elif target_device.type == "cuda": assert div_scale == -1, "div_scale should remain default" assert state["exp_avg"].device.type == "cuda", "exp_avg should stay on cuda" assert state["exp_avg_sq"].device.type == "cuda", "exp_avg should stay on cuda" bias_correction1 = 1 - beta1 ** state["step"] bias_correction2 = 1 - beta2 ** state["step"] # adam on cuda self.torch_adam_update( p.data, p.grad.data, state["exp_avg"], state["exp_avg_sq"], group["lr"], beta1, beta2, group["eps"], group["weight_decay"], bias_correction1, bias_correction2, self.adamw_mode, ) else: raise RuntimeError self._post_step() return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/nn/optimizer/fused_sgd.py
colossalai/nn/optimizer/fused_sgd.py
# modified from https://github.com/NVIDIA/apex/blob/master/apex/optimizers/fused_sgd.py import torch from torch.optim.optimizer import Optimizer, required from colossalai.utils import multi_tensor_applier class FusedSGD(Optimizer): r"""Implements stochastic gradient descent (optionally with momentum). `FusedSGD` requires CUDA extensions which can be built during installation or runtime. This version of fused SGD implements 2 fusions. * Fusion of the SGD update's elementwise operations * A multi-tensor apply launch that batches the elementwise updates applied to all the model's parameters into one or a few kernel launches. :class:`colossalai.nn.optimizer.FusedSGD` may be used as a drop-in replacement for ``torch.optim.SGD`` :class:`colossalai.nn.optimizer.FusedSGD` may be used with or without Amp. Nesterov momentum is based on the formula from `On the importance of initialization and momentum in deep learning`__. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float): learning rate momentum (float, optional): momentum factor (default: 0) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) dampening (float, optional): dampening for momentum (default: 0) nesterov (bool, optional): enables Nesterov momentum (default: False) __ http://www.cs.toronto.edu/%7Ehinton/absps/momentum.pdf .. note:: The implementation of SGD with Momentum/Nesterov subtly differs from Sutskever et. al. and implementations in some other frameworks. Considering the specific case of Momentum, the update can be written as .. math:: v = \rho * v + g \\ p = p - lr * v where p, g, v and :math:`\rho` denote the parameters, gradient, velocity, and momentum respectively. This is in contrast to Sutskever et. al. and other frameworks which employ an update of the form .. math:: v = \rho * v + lr * g \\ p = p - v The Nesterov version is analogously modified. """ def __init__( self, params, lr=required, momentum=0, dampening=0, weight_decay=0, nesterov=False, wd_after_momentum=False ): if lr is not required and lr < 0.0: raise ValueError("Invalid learning rate: {}".format(lr)) if momentum < 0.0: raise ValueError("Invalid momentum value: {}".format(momentum)) if weight_decay < 0.0: raise ValueError("Invalid weight_decay value: {}".format(weight_decay)) defaults = dict(lr=lr, momentum=momentum, dampening=dampening, weight_decay=weight_decay, nesterov=nesterov) if nesterov and (momentum <= 0 or dampening != 0): raise ValueError("Nesterov momentum requires a momentum and zero dampening") super(FusedSGD, self).__init__(params, defaults) self.wd_after_momentum = wd_after_momentum if multi_tensor_applier.available: from colossalai.kernel.kernel_loader import FusedOptimizerLoader fused_optim = FusedOptimizerLoader().load() # Skip buffer self._dummy_overflow_buf = torch.tensor( [0], dtype=torch.int, device=self.param_groups[0]["params"][0].device ) self.multi_tensor_sgd = fused_optim.multi_tensor_sgd else: raise RuntimeError("FusedSGD requires cuda extensions") def __setstate__(self, state): super(FusedSGD, self).__setstate__(state) for group in self.param_groups: group.setdefault("nesterov", False) def get_momentums(self, params): momentums = [] first_run = True for p in params: param_state = self.state[p] # torch.optim.SGD initializes momentum in the main loop, we have # to do it here, and track whether or not we've done so, so that # momentum application can be skipped in the main kernel. if "momentum_buffer" not in param_state: first_run = True buf = param_state["momentum_buffer"] = torch.zeros_like(p) momentums.append(buf) else: first_run = False momentums.append(param_state["momentum_buffer"]) return momentums, first_run def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: weight_decay = group["weight_decay"] momentum = group["momentum"] dampening = group["dampening"] nesterov = group["nesterov"] # For each group, there are 3 possible combinations we need to consider: # grad_type, param_to_update_type, momentum_type # 1. fp16, fp16, fp16 # 2. fp32, fp32, fp32 # 3. fp16, fp32, fp32 g_l, p_l = [], [] for p in group["params"]: if p.grad is None: continue if p.grad.data.is_sparse: raise RuntimeError("FusedSGD does not support sparse gradients") g_l.append(p.grad) p_l.append(p) m_l, first_run = self.get_momentums(p_l) multi_tensor_applier( self.multi_tensor_sgd, self._dummy_overflow_buf, [g_l, p_l, m_l], weight_decay, momentum, dampening, group["lr"], nesterov, first_run, self.wd_after_momentum, 1.0, ) return loss
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/__init__.py
colossalai/auto_parallel/__init__.py
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/shard_metainfo.py
colossalai/auto_parallel/meta_profiler/shard_metainfo.py
from typing import Callable, List import torch from colossalai.auto_parallel.tensor_shard.sharding_strategy import OperationData, ShardingStrategy, TrainCycleItem from colossalai.tensor.sharding_spec import ShardingSpec from .constants import INPLACE_MODULE, INPLACE_OPS, NO_SAVE_ACTIVATION from .registry import meta_register __all__ = ["ShardMetaInfo"] class ShardMetaInfo: """ShardMetaInfo class This class is used to store meta info based on sharding strategy and the given target function. """ def __init__(self, strategy: ShardingStrategy = None, target: Callable = None) -> None: # compute cost of forward and backward computation self.compute_cost: TrainCycleItem # compute memory cost of forward and backward phase self.memory_cost: TrainCycleItem # list of input tensors self.fwd_in: List[torch.Tensor] # list of buffer tensors self.fwd_buffer: List[torch.Tensor] # list of output tensors self.fwd_out: List[torch.Tensor] # sharding strategy self._strategy = strategy # target function self._target = target # compute shard_metainfo if possible if self._strategy is not None and self._target is not None: self.compute_shard_metainfo() @property def strategy(self) -> ShardingStrategy: return self._strategy @property def target(self) -> Callable: return self._target @strategy.setter def strategy(self, strategy: ShardingStrategy) -> None: self._strategy = strategy if self._strategy is not None and self._target is not None: self.compute_shard_metainfo() @target.setter def target(self, target: Callable) -> None: self._target = target if self._strategy is not None and self._target is not None: self.compute_shard_metainfo() def compute_sharded_opdata(self, operation_data: OperationData, sharding_spec: ShardingSpec): """ Compute sharded opdata based on the given data and sharding spec. """ if isinstance(sharding_spec, ShardingSpec): op_data = OperationData( name=operation_data.name, data=torch.zeros(sharding_spec.get_sharded_shape_per_device(), device="meta"), type=operation_data.type, logical_shape=operation_data.logical_shape, ) elif isinstance(sharding_spec, (list, tuple)): data = operation_data.data assert isinstance(data, (list, tuple)), f"Data Should be list or tuple, but got {type(data)}." assert len(data) == len(sharding_spec), f"Length of data and sharding spec should be the same." sharded_data = [] for d, s in zip(data, sharding_spec): sharded_data.append(torch.zeros(s.get_sharded_shape_per_device(), device="meta")) op_data = OperationData(name=operation_data.name, data=sharded_data, type=operation_data.type) else: raise ValueError(f"Sharding spec should be ShardingSpec or list, but got {type(sharding_spec)}.") return op_data def compute_shard_metainfo(self): """ Compute meta info based on sharding strategy and the given target function. """ assert meta_register.has(self._target.__class__) or meta_register.has( self._target ), f"Meta info for {self._target} is not registered." if meta_register.has(self._target.__class__): # module meta_func = meta_register.get(self._target.__class__) # check whether the target in the list that we don't need to save activation save_fwd_in = self._target.__class__ not in NO_SAVE_ACTIVATION else: # function meta_func = meta_register.get(self._target) # check whether the target in the list that we don't need to save activation save_fwd_in = self._target.__class__ not in NO_SAVE_ACTIVATION # construct args for meta_func args = [self.compute_sharded_opdata(k, v) for k, v in self._strategy.sharding_specs.items()] # construct kwargs if self.target in INPLACE_MODULE: kwargs = {"inplace": self.target.inplace} elif self.target in INPLACE_OPS: kwargs = {"inplace": True} else: kwargs = {"inplace": False} # compute metainfo with meta_func self.compute_cost, self.memory_cost, self.fwd_in, self.fwd_buffer, self.fwd_out = meta_func(*args, **kwargs) # process corner case for NO_SAVE_ACTIVATION if not save_fwd_in: self.fwd_in = []
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/registry.py
colossalai/auto_parallel/meta_profiler/registry.py
__all__ = ["Registry"] class Registry: def __init__(self, name): self.name = name self.store = {} def register(self, source): def wrapper(func): if isinstance(source, (list, tuple)): # support register a list of items for this func for element in source: self.store[element] = func else: self.store[source] = func return func return wrapper def get(self, source): assert source in self.store, f"{source} not found in the {self.name} registry" target = self.store[source] return target def has(self, source): return source in self.store meta_register = Registry("meta")
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/constants.py
colossalai/auto_parallel/meta_profiler/constants.py
import operator import torch import torch.nn as nn # list of inplace module INPLACE_MODULE = [nn.ReLU] # list of inplace operations INPLACE_OPS = [torch.flatten] # list of operations that do not save forward activations NO_SAVE_ACTIVATION = [torch.add, torch.sub, operator.add, operator.sub]
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/__init__.py
colossalai/auto_parallel/meta_profiler/__init__.py
from .meta_registry import * from .registry import meta_register from .shard_metainfo import *
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/activation.py
colossalai/auto_parallel/meta_profiler/meta_registry/activation.py
from typing import Callable, List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import ewise_flop_counter as elementwise_flop_counter from colossalai._analyzer.fx.node_util import compute_size_in_bytes as activation_size from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, OperationDataType, TrainCycleItem from ..registry import meta_register __all__ = ["elementwise_meta_info"] def elementwise_meta_info(temp_mem_scale: float = 0, buffer_mem_scale: float = 0) -> Callable: """This is a function to create the meta information generator for elementwise operations Args: temp_mem_scale (float, optional): temp memory scaling factor for backward. Defaults to 0. buffer_mem_scale (float, optional): buffer memory scaling factor for forward. Defaults to 0. Returns: Callable: meta information generator """ def meta_func(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: input_tensor = next( filter( lambda x: (x.type == OperationDataType.ARG or x.type == OperationDataType.PARAM) and x.name != "softmax_dim", args, ) ).data output_tensor = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data is_inplace = 1 if kwargs.get("inplace", False) else 0 flop_counter = elementwise_flop_counter(1, 0) # calculate compute cost fwd_compute_cost = flop_counter([input_tensor], [output_tensor]) bwd_compute_cost = flop_counter([output_tensor], [input_tensor]) compute_cost = TrainCycleItem( fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost ) # calculate memory cost # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward # NOTE: if in_place is True, we will not create a new tensor in forward fwd_memory_cost = MemoryCost( activation=activation_size(input_tensor) * (2 - is_inplace), parameter=0, temp=0, buffer=activation_size(input_tensor) * buffer_mem_scale, ) # temp_mem_scale is for situation like softmax backward # the buffer will be removed during backward phase bwd_memory_cost = MemoryCost( activation=activation_size(input_tensor) - activation_size(input_tensor) * buffer_mem_scale, parameter=0, temp=activation_size(input_tensor) * temp_mem_scale + activation_size(input_tensor) * buffer_mem_scale, buffer=0, ) # total cost is the sum of forward and backward cost total_cost = MemoryCost( activation=fwd_memory_cost.activation + bwd_memory_cost.activation, parameter=fwd_memory_cost.parameter + bwd_memory_cost.parameter, temp=fwd_memory_cost.temp + bwd_memory_cost.temp, buffer=fwd_memory_cost.buffer + bwd_memory_cost.buffer, ) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [] fwd_buffer = [torch.zeros_like(output_tensor, device="meta")] fwd_out = [torch.zeros_like(output_tensor, device="meta")] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out return meta_func # register meta information # (0, 0) meta_register.register([torch.nn.ReLU, torch.nn.functional.relu, torch.tanh])(elementwise_meta_info(0, 0)) # (1, 0) meta_register.register([torch.nn.Softmax, torch.nn.functional.softmax])(elementwise_meta_info(1, 0)) # (0, 0.25) for dropout, the buffer is in bool type so that the buffer memory cost is 0.25 times of input tensor meta_register.register([torch.nn.Dropout, torch.nn.functional.dropout])(elementwise_meta_info(0, 0.25))
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/where.py
colossalai/auto_parallel/meta_profiler/meta_registry/where.py
from typing import List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import flop_mapping from colossalai._analyzer.fx.node_util import compute_size_in_bytes as activation_size from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, TrainCycleItem from ..registry import meta_register __all__ = ["where_meta_info"] @meta_register.register(torch.where) def where_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """torch.where meta information generator Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ condition_tensor, x_tensor, y_tensor, output_tensor = [arg.data for arg in args] # compute cost fwd_compute_cost = 0 # if we need to broadcast the condition tensor, during backward we need to do a reduce_sum bwd_compute_cost = 0 if x_tensor.shape != output_tensor.shape: bwd_compute_cost += flop_mapping[torch.ops.aten.sum.dim_IntList]([output_tensor], [x_tensor]) if y_tensor.shape != output_tensor.shape: bwd_compute_cost += flop_mapping[torch.ops.aten.sum.dim_IntList]([output_tensor], [y_tensor]) compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # memory cost # during the forward phase, torch.where will allocate memory for output tensor and condition tensor # during the backward phase, torch.where will allocate temp memory which is 3 times as output tensor, then generate # gradient matrix for input x and input y, remove the temp memory and condition tensor generated in forward phase # NOTE: currently in SPMD solver we always believe that there will be a new input tensor created in forward fwd_mem_cost = MemoryCost(activation=activation_size([condition_tensor, x_tensor, y_tensor, output_tensor])) bwd_mem_cost = MemoryCost( activation=activation_size([x_tensor, y_tensor]) - activation_size([condition_tensor]), parameter=0, temp=activation_size([output_tensor]) * 3 + activation_size([condition_tensor]) - activation_size([x_tensor, y_tensor]), buffer=0, ) total_mem_cost = MemoryCost( activation=fwd_mem_cost.activation + bwd_mem_cost.activation, parameter=fwd_mem_cost.parameter + bwd_mem_cost.parameter, temp=fwd_mem_cost.temp + bwd_mem_cost.temp, buffer=fwd_mem_cost.buffer + bwd_mem_cost.buffer, ) memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [condition_tensor] fwd_buffer = [] fwd_out = [output_tensor] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/binary_elementwise_ops.py
colossalai/auto_parallel/meta_profiler/meta_registry/binary_elementwise_ops.py
from typing import List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import flop_mapping from colossalai._analyzer.fx.node_util import compute_size_in_bytes as activation_size from colossalai.auto_parallel.tensor_shard.constants import BCAST_FUNC_OP from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, OperationDataType, TrainCycleItem from ..registry import meta_register __all__ = ["binary_elementwise_meta_info"] @meta_register.register(BCAST_FUNC_OP) def binary_elementwise_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """Meta information generator for binary elementwise operations NOTE: Some of the binary elementwise operations will discard the input activation after computation, as they don't need those tensors for back propagation, for example, if there are two tensors being sent for `torch.add`, they will be discarded right after add operation is done. We create a simple API in `ShardMetaInfo` class to identify this behavior, it is critical for better memory estimation. Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ input_op_data = [arg for arg in args if arg.type != OperationDataType.OUTPUT] output_op_data = next(filter(lambda arg: arg.type == OperationDataType.OUTPUT, args)) # construct forward args for flop mapping fwd_in_args = [opdata.data for opdata in input_op_data] fwd_out_args = [output_op_data.data] # calculate cost # calculate compute cost # NOTE: we set bwd_compute_cost two times of fwd_compute_cost in this case fwd_compute_cost = flop_mapping[torch.ops.aten.add.Tensor](fwd_in_args, fwd_out_args) bwd_compute_cost = fwd_compute_cost * 2 compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # calculate memory cost param_mem_cost = activation_size([arg.data for arg in input_op_data if arg.type == OperationDataType.PARAM]) fwd_mem_cost = MemoryCost( activation=activation_size(output_op_data.data), parameter=param_mem_cost, ) bwd_mem_cost = MemoryCost( activation=activation_size(fwd_in_args), parameter=param_mem_cost, ) # total cost total_mem_cost = MemoryCost( activation=fwd_mem_cost.activation + bwd_mem_cost.activation, parameter=fwd_mem_cost.parameter + bwd_mem_cost.parameter, ) memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [] fwd_buffer = [] fwd_out = [torch.zeros_like(output_op_data.data, device="meta")] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/conv.py
colossalai/auto_parallel/meta_profiler/meta_registry/conv.py
from typing import List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import flop_mapping from colossalai._analyzer.fx.node_util import compute_size_in_bytes from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, OperationDataType, TrainCycleItem from ..registry import meta_register __all__ = ["convnd_meta_info"] @meta_register.register(torch.nn.Conv1d) @meta_register.register(torch.nn.Conv2d) @meta_register.register(torch.nn.Conv3d) @meta_register.register(torch.nn.functional.conv1d) @meta_register.register(torch.nn.functional.conv2d) @meta_register.register(torch.nn.functional.conv3d) def convnd_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """torch.nn.Conv1d, torch.nn.Conv2d, torch.nn.Conv3d meta info generator The atens graph of torch.nn.Convnd with bias is graph(): %input_2 : [#users=2] = placeholder[target=placeholder](default=) %convolution_default : [#users=1] = call_function[target=torch.ops.aten.convolution.default](args = (%input_2, None, None, [None, None, None], [None, None, None], [None, None, None], None, [None, None, None], None), kwargs = {}) %zeros_like_default : [#users=1] = call_function[target=torch.ops.aten.zeros_like.default](args = (%convolution_default,), kwargs = {dtype: None, layout: None, device: None, pin_memory: None}) %detach_default : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%input_2,), kwargs = {}) %convolution_backward_default : [#users=3] = call_function[target=torch.ops.aten.convolution_backward.default](args = (%zeros_like_default, %detach_default, None, [None], [None, None, None], [None, None, None], [None, None, None], None, [None, None, None], None, [None, None, None]), kwargs = {}) %detach_default_1 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%convolution_backward_default,), kwargs = {}) %detach_default_2 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_1,), kwargs = {}) %detach_default_3 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%convolution_backward_default,), kwargs = {}) %detach_default_4 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_3,), kwargs = {}) %detach_default_5 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%convolution_backward_default,), kwargs = {}) %detach_default_6 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_5,), kwargs = {}) The atens graph of torch.nn.Convnd without bias is graph(): %input_2 : [#users=2] = placeholder[target=placeholder](default=) %convolution_default : [#users=1] = call_function[target=torch.ops.aten.convolution.default](args = (%input_2, None, None, [None, None], [None, None], [None, None], None, [None, None], None), kwargs = {}) %zeros_like_default : [#users=1] = call_function[target=torch.ops.aten.zeros_like.default](args = (%convolution_default,), kwargs = {dtype: None, layout: None, device: None, pin_memory: None}) %detach_default : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%input_2,), kwargs = {}) %convolution_backward_default : [#users=2] = call_function[target=torch.ops.aten.convolution_backward.default](args = (%zeros_like_default, %detach_default, None, [None], [None, None], [None, None], [None, None], None, [None, None], None, [None, None, None]), kwargs = {}) %detach_default_1 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%convolution_backward_default,), kwargs = {}) %detach_default_2 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_1,), kwargs = {}) %detach_default_3 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%convolution_backward_default,), kwargs = {}) %detach_default_4 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_3,), kwargs = {}) Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ has_bias: bool = False input_tensor = args[0].data output_tensor = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data if len(args) == 4: weight_tensors = [args[1].data, args[3].data] else: weight_tensors = [args[1].data] # check if conv has bias if len(weight_tensors) > 1: has_bias = True # bias tensor's shape only has one dimension if len(weight_tensors[0].shape) == 1: bias_tensor, weight_tensor = weight_tensors else: weight_tensor, bias_tensor = weight_tensors else: weight_tensor = weight_tensors[0] # construct input args for forward fwd_args = [None] * 9 # weight and input fwd_args[0] = input_tensor fwd_args[1] = weight_tensor fwd_args[2] = bias_tensor if has_bias else None # transpose indicator should be set to False fwd_args[6] = False # construct input args for backward bwd_args = [None] * 11 # weight and input bwd_args[0] = output_tensor bwd_args[1] = input_tensor bwd_args[2] = weight_tensor bwd_args[-1] = [True, True, True] if has_bias else [True, True, False] # calculate cost # the fwd op with compute cost is convolution.default # the bwd op with compute cost is convolution_backward.default # calculate compute cost fwd_compute_cost = flop_mapping[torch.ops.aten.convolution.default](fwd_args, (output_tensor,)) bwd_compute_cost = ( flop_mapping[torch.ops.aten.convolution_backward.default](bwd_args, (input_tensor, weight_tensor, bias_tensor)) if has_bias else flop_mapping[torch.ops.aten.convolution_backward.default](bwd_args, (input_tensor, weight_tensor)) ) compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # calculate memory cost # TODO: use profiler to check conv temp memory # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, output_tensor]), parameter=( compute_size_in_bytes([weight_tensor, bias_tensor]) if has_bias else compute_size_in_bytes(weight_tensor) ), temp=0, buffer=0, ) bwd_memory_cost = MemoryCost( activation=( compute_size_in_bytes([input_tensor, weight_tensor, bias_tensor]) if has_bias else compute_size_in_bytes([input_tensor, weight_tensor]) ), parameter=( compute_size_in_bytes([weight_tensor, bias_tensor]) if has_bias else compute_size_in_bytes(weight_tensor) ), temp=0, buffer=0, ) # total cost is the sum of forward and backward cost total_cost = MemoryCost( activation=fwd_memory_cost.activation + bwd_memory_cost.activation, parameter=fwd_memory_cost.parameter + bwd_memory_cost.parameter, ) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [torch.zeros_like(input_tensor, device="meta")] fwd_buffer = [] fwd_out = [torch.zeros_like(output_tensor, device="meta")] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/pooling.py
colossalai/auto_parallel/meta_profiler/meta_registry/pooling.py
from typing import List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import flop_mapping from colossalai._analyzer.fx.node_util import compute_size_in_bytes from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, OperationDataType, TrainCycleItem from ..registry import meta_register __all__ = ["avgpool_meta_info", "maxpool_meta_info"] @meta_register.register(torch.nn.AdaptiveAvgPool1d) @meta_register.register(torch.nn.AdaptiveAvgPool2d) @meta_register.register(torch.nn.AdaptiveAvgPool3d) def avgpool_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """Meta info for AdaptiveAvgPool The aten graph of AdaptiveAvgPool is graph(): %input_2 : [#users=2] = placeholder[target=placeholder](default=) %_adaptive_avg_pool2d_default : [#users=1] = call_function[target=torch.ops.aten._adaptive_avg_pool2d.default](args = (%input_2, [None, None]), kwargs = {}) %zeros_like_default : [#users=1] = call_function[target=torch.ops.aten.zeros_like.default](args = (%_adaptive_avg_pool2d_default,), kwargs = {dtype: None, layout: None, device: None, pin_memory: None}) %detach_default : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%input_2,), kwargs = {}) %_adaptive_avg_pool2d_backward_default : [#users=1] = call_function[target=torch.ops.aten._adaptive_avg_pool2d_backward.default](args = (%zeros_like_default, %detach_default), kwargs = {}) %detach_default_1 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%_adaptive_avg_pool2d_backward_default,), kwargs = {}) %detach_default_2 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_1,), kwargs = {}) Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ input_tensor = args[0].data output_tensor = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data is_inplace = kwargs.get("inplace", False) # construct forward args for flop mapping fwd_in_args = [input_tensor] fwd_out_args = [output_tensor] # construct backward args for flop mapping bwd_in_args = [output_tensor] bwd_out_args = [input_tensor] # calculate cost # the fwd op with compute cost is _adaptive_avg_pool2d.default # the bwd op with compute cost is _adaptive_avg_pool2d_backward.default # calculate compute cost fwd_compute_cost = flop_mapping[torch.ops.aten._adaptive_avg_pool2d.default](fwd_in_args, fwd_out_args) bwd_compute_cost = flop_mapping[torch.ops.aten._adaptive_avg_pool2d_backward.default](bwd_in_args, bwd_out_args) compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # calculate memory cost fwd_mem_cost = MemoryCost() if is_inplace else MemoryCost(activation=compute_size_in_bytes(output_tensor)) bwd_mem_cost = MemoryCost() if is_inplace else MemoryCost(activation=compute_size_in_bytes(input_tensor)) # total cost total_mem_cost = MemoryCost(activation=fwd_mem_cost.activation + bwd_mem_cost.activation) mem_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [] fwd_buffer = [] fwd_out = [torch.zeros_like(output_tensor, device="meta")] return compute_cost, mem_cost, fwd_in, fwd_buffer, fwd_out @meta_register.register(torch.nn.MaxPool1d) @meta_register.register(torch.nn.MaxPool2d) @meta_register.register(torch.nn.MaxPool3d) def maxpool_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """Meta info for MaxPool The aten graph of MaxPool is graph(): %input_2 : [#users=2] = placeholder[target=placeholder](default=) %max_pool2d_with_indices_default : [#users=2] = call_function[target=torch.ops.aten.max_pool2d_with_indices.default](args = (%input_2, [None, None], [None, None]), kwargs = {}) %zeros_like_default : [#users=1] = call_function[target=torch.ops.aten.zeros_like.default](args = (%max_pool2d_with_indices_default,), kwargs = {dtype: None, layout: None, device: None, pin_memory: None}) %detach_default : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%input_2,), kwargs = {}) %detach_default_1 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%max_pool2d_with_indices_default,), kwargs = {}) %max_pool2d_with_indices_backward_default : [#users=1] = call_function[target=torch.ops.aten.max_pool2d_with_indices_backward.default](args = (%zeros_like_default, %detach_default, [None, None], [None, None], [None, None], [None, None], None, %detach_default_1), kwargs = {}) %detach_default_2 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%max_pool2d_with_indices_backward_default,), kwargs = {}) %detach_default_3 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_2,), kwargs = {}) Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ input_tensor = next(filter(lambda x: x.type == OperationDataType.ARG, args)).data output_tensor = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data # construct forward args for flop mapping fwd_in_args = [input_tensor] fwd_out_args = [output_tensor] # construct backward args for flop mapping bwd_in_args = [output_tensor] bwd_out_args = [input_tensor] # construct index matrix index_matrix = torch.zeros_like(output_tensor, device="meta", dtype=torch.int64) # calculate cost # the fwd op with compute cost is max_pool2d_with_indices.default # the bwd op with compute cost is max_pool2d_with_indices_backward.default # calculate compute cost fwd_compute_cost = flop_mapping[torch.ops.aten.max_pool2d_with_indices.default](fwd_in_args, fwd_out_args) bwd_compute_cost = flop_mapping[torch.ops.aten.max_pool2d_with_indices_backward.default](bwd_in_args, bwd_out_args) compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # calculate memory cost # NOTE: the index matrix will be discarded in backward phase # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes([input_tensor, output_tensor, index_matrix])) # temp memory for backward is the index matrix to be discarded bwd_mem_cost = MemoryCost( activation=compute_size_in_bytes(input_tensor) - compute_size_in_bytes(index_matrix), temp=compute_size_in_bytes(index_matrix), ) # total cost total_mem_cost = MemoryCost(activation=fwd_mem_cost.activation + bwd_mem_cost.activation, temp=bwd_mem_cost.temp) mem_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [torch.zeros_like(input_tensor, device="meta")] fwd_buffer = [torch.zeros_like(index_matrix, device="meta")] fwd_out = [torch.zeros_like(output_tensor, device="meta")] return compute_cost, mem_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/non_spmd.py
colossalai/auto_parallel/meta_profiler/meta_registry/non_spmd.py
import operator from typing import List, Tuple import torch from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, TrainCycleItem from ..registry import meta_register __all__ = ["non_spmd_meta_info"] @meta_register.register(torch.Size) @meta_register.register(torch.Tensor.size) @meta_register.register(torch.finfo) @meta_register.register(operator.le) def non_spmd_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """Non-SPMD node meta information generator Those nodes will not be handled by SPMD solver, so we just return all zero meta information for it Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ compute_cost = TrainCycleItem(fwd=0, bwd=0, total=0) memory_cost = TrainCycleItem(fwd=MemoryCost(), bwd=MemoryCost(), total=MemoryCost()) fwd_in, fwd_buffer, fwd_out = [], [], [] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/norm.py
colossalai/auto_parallel/meta_profiler/meta_registry/norm.py
from typing import List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import flop_mapping from colossalai._analyzer.fx.node_util import compute_size_in_bytes from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, OperationDataType, TrainCycleItem from ..registry import meta_register __all__ = ["batchnormnd_meta_info", "layernorm_meta_info"] @meta_register.register(torch.nn.BatchNorm1d) @meta_register.register(torch.nn.BatchNorm2d) @meta_register.register(torch.nn.BatchNorm3d) def batchnormnd_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """BatchNorm1d, BatchNorm2d, BatchNorm3d, meta info generator The aten graph of BatchNorm2d is like graph(): %input_2 : [#users=2] = placeholder[target=placeholder](default=) %cudnn_batch_norm_default : [#users=4] = call_function[target=torch.ops.aten.cudnn_batch_norm.default](args = (%input_2, None, None, None, None, None, None, None), kwargs = {}) %zeros_like_default : [#users=1] = call_function[target=torch.ops.aten.zeros_like.default](args = (%cudnn_batch_norm_default,), kwargs = {dtype: None, layout: None, device: None, pin_memory: None}) %detach_default : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%input_2,), kwargs = {}) %detach_default_1 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%cudnn_batch_norm_default,), kwargs = {}) %detach_default_2 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%cudnn_batch_norm_default,), kwargs = {}) %detach_default_3 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%cudnn_batch_norm_default,), kwargs = {}) %cudnn_batch_norm_backward_default : [#users=3] = call_function[target=torch.ops.aten.cudnn_batch_norm_backward.default](args = (%detach_default, %zeros_like_default, None, None, None, %detach_default_1, %detach_default_2, None, %detach_default_3), kwargs = {}) %detach_default_4 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%cudnn_batch_norm_backward_default,), kwargs = {}) %detach_default_5 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_4,), kwargs = {}) %detach_default_6 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%cudnn_batch_norm_backward_default,), kwargs = {}) %detach_default_7 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_6,), kwargs = {}) %detach_default_8 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%cudnn_batch_norm_backward_default,), kwargs = {}) %detach_default_9 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_8,), kwargs = {}) Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ input_tensor = args[0].data output_tensor = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data weight_tensor = next(filter(lambda x: x.name == "weight", args)).data bias_tensor = next(filter(lambda x: x.name == "bias", args)).data mean_tensor = next(filter(lambda x: x.name == "running_mean", args)).data var_tensor = next(filter(lambda x: x.name == "running_var", args)).data num_batch = next(filter(lambda x: x.name == "num_batches_tracked", args)).data # construct fwd args # the fwd inputs are input, weight, bias, running_mean, running_var and some other args # indicating the status of the module # the fwd outputs are output, saved mean, saved inv std and num batches tracked fwd_in_args = [input_tensor, weight_tensor, bias_tensor, mean_tensor, var_tensor, True, 0.1, 1e-5] fwd_out_args = [output_tensor, mean_tensor, var_tensor, num_batch] # construct bwd args # the bwd inputs are upstream grad, input, weight, running_mean, running_var, saved mean, # saved inv std and some other args indicating the status of the module # the bwd outputs are input grad, weight grad and bias grad bwd_in_args = [ output_tensor, output_tensor, weight_tensor, mean_tensor, var_tensor, mean_tensor, var_tensor, 1e-5, num_batch, ] bwd_out_args = [input_tensor, weight_tensor, bias_tensor] # calculate cost fwd_compute_cost = flop_mapping[torch.ops.aten.cudnn_batch_norm.default](fwd_in_args, fwd_out_args) bwd_compute_cost = flop_mapping[torch.ops.aten.cudnn_batch_norm_backward.default](bwd_in_args, bwd_out_args) compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # calculate memory cost # the fwd activation cost is output plus saved mean and saved inv std # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, output_tensor, mean_tensor, var_tensor]), parameter=compute_size_in_bytes([weight_tensor, bias_tensor]), temp=0, buffer=compute_size_in_bytes([mean_tensor, var_tensor]), ) # the bwd memory cost is quite tricky here, BatchNorm will remove saved mean # and saved inv std during backward phase bwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor]), parameter=compute_size_in_bytes([weight_tensor, bias_tensor]), temp=compute_size_in_bytes([mean_tensor, var_tensor]), buffer=compute_size_in_bytes([mean_tensor, var_tensor]), ) # total cost is the sum of forward and backward cost total_cost = MemoryCost( activation=fwd_memory_cost.activation + bwd_memory_cost.activation, parameter=fwd_memory_cost.parameter + bwd_memory_cost.parameter, ) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [torch.zeros_like(input_tensor, device="meta")] fwd_buffer = [torch.zeros_like(mean_tensor, device="meta"), torch.zeros_like(var_tensor, device="meta")] fwd_out = [torch.zeros_like(output_tensor, device="meta")] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out @meta_register.register(torch.nn.LayerNorm) def layernorm_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """LayerNorm meta information Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ # construct needed tensors input_tensor = next(filter(lambda x: x.type == OperationDataType.ARG, args)).data output_tensor = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data weight_tensor = next(filter(lambda x: x.name == "weight", args)).data bias_tensor = next(filter(lambda x: x.name == "bias", args)).data running_mean = torch.rand(input_tensor.shape[0], 1, device="meta") running_var = torch.rand(input_tensor.shape[0], 1, device="meta") # construct args fwd_in_args = [input_tensor, [input_tensor.shape[0]], weight_tensor] fwd_out_args = [output_tensor] bwd_in_args = [input_tensor, output_tensor, [input_tensor.shape[0]]] bwd_out_args = [weight_tensor, bias_tensor] # compute cost fwd_compute_cost = flop_mapping[torch.ops.aten.native_layer_norm.default](fwd_in_args, fwd_out_args) bwd_compute_cost = flop_mapping[torch.ops.aten.native_layer_norm_backward.default](bwd_in_args, bwd_out_args) compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # memory cost # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, output_tensor, weight_tensor, bias_tensor]), parameter=compute_size_in_bytes([weight_tensor, bias_tensor]), temp=0, buffer=compute_size_in_bytes([running_mean, running_var]), ) bwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, weight_tensor, bias_tensor]), parameter=compute_size_in_bytes([weight_tensor, bias_tensor]), temp=compute_size_in_bytes([running_mean, running_var]), buffer=compute_size_in_bytes([running_mean, running_var]), ) total_cost = MemoryCost( activation=fwd_memory_cost.activation + bwd_memory_cost.activation, parameter=fwd_memory_cost.parameter + bwd_memory_cost.parameter, temp=fwd_memory_cost.temp + bwd_memory_cost.temp, buffer=fwd_memory_cost.buffer + bwd_memory_cost.buffer, ) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [torch.zeros_like(input_tensor, device="meta")] fwd_buffer = [torch.zeros_like(running_mean, device="meta"), torch.zeros_like(running_var, device="meta")] fwd_out = [torch.zeros_like(output_tensor, device="meta")] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/__init__.py
colossalai/auto_parallel/meta_profiler/meta_registry/__init__.py
from .activation import * from .binary_elementwise_ops import * from .conv import * from .embedding import * from .linear import * from .non_spmd import * from .norm import * from .pooling import * from .tensor import * from .where import *
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/embedding.py
colossalai/auto_parallel/meta_profiler/meta_registry/embedding.py
from typing import List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import flop_mapping from colossalai._analyzer.fx.node_util import compute_size_in_bytes from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, OperationDataType, TrainCycleItem from ..registry import meta_register __all__ = ["embedding_meta_info"] @meta_register.register(torch.nn.Embedding) def embedding_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """torch.nn.Embedding metainfo generator Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ input_tensor = next(filter(lambda x: x.type == OperationDataType.ARG, args)).data weight_tensor = next(filter(lambda x: x.type == OperationDataType.PARAM, args)).data output_tensor = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data # compute cost fwd_compute_cost = flop_mapping[torch.ops.aten.embedding.default]([weight_tensor, input_tensor], [output_tensor]) bwd_compute_cost = flop_mapping[torch.ops.aten.embedding_dense_backward.default]( [output_tensor, weight_tensor], [weight_tensor] ) compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # memory cost # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward # NOTE: during the backward phase of torch.nn.Embedding, it seems when the input is large enough, it will # have a temp memory which is kind of weird and we don't know the reason yet, so currently we just assume # that there will be no temp memory, as the temp memory is significantly smaller than the gradient memory fwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, output_tensor]), parameter=0, temp=0, buffer=0 ) bwd_memory_cost = MemoryCost(activation=compute_size_in_bytes([weight_tensor]), parameter=0, temp=0, buffer=0) total_memory_cost = MemoryCost(activation=fwd_memory_cost.activation + bwd_memory_cost.activation) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_memory_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [torch.zeros_like(input_tensor)] fwd_buffer = [] fwd_out = [torch.zeros_like(output_tensor)] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/tensor.py
colossalai/auto_parallel/meta_profiler/meta_registry/tensor.py
from typing import Callable, List, Tuple import torch from colossalai._analyzer.fx.node_util import compute_size_in_bytes from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, OperationDataType, TrainCycleItem from ..registry import meta_register __all__ = ["tensor_related_metainfo"] def tensor_related_metainfo(bwd_mem_out_factor: float = 1, bwd_mem_tmp_factor: float = 0) -> Callable: """torch.Tensor related metainfo generator template Args: bwd_mem_out_factor (float, optional): backward activation memory cost factor. Defaults to 1. bwd_mem_tmp_factor (float, optional): backward temp memory cost factor. Defaults to 0. Returns: Callable: torch.Tensor related metainfo generator """ def meta_func(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """torch.Tensor related metainfo generator Returns: Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: compute cost, memory cost and forward inputs """ outputs = next(filter(lambda x: x.type == OperationDataType.OUTPUT, args)).data # compute costs are all zero compute_cost = TrainCycleItem(fwd=0, bwd=0, total=0) # memory costs # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(outputs) * 2, parameter=0, temp=0, buffer=0) bwd_mem_cost = MemoryCost( activation=compute_size_in_bytes(outputs) * bwd_mem_out_factor, parameter=0, temp=compute_size_in_bytes(outputs) * bwd_mem_tmp_factor, buffer=0, ) total_mem_cost = MemoryCost( activation=fwd_mem_cost.activation + bwd_mem_cost.activation, parameter=fwd_mem_cost.parameter + bwd_mem_cost.parameter, temp=fwd_mem_cost.temp + bwd_mem_cost.temp, buffer=fwd_mem_cost.buffer + bwd_mem_cost.buffer, ) memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [] fwd_buffer = [] if isinstance(outputs, tuple) or isinstance(outputs, list) or isinstance(outputs, dict): # tuple of tensors fwd_out = [torch.zeros_like(tensor) for tensor in outputs] else: # enaged_tensors is a single tensor fwd_out = [torch.zeros_like(outputs)] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out return meta_func # register torch.Tensor related metainfo # (0, 0) meta_register.register([torch.tensor, torch.Tensor.to, torch.Tensor.unsqueeze, torch.unsqueeze, torch.arange])( tensor_related_metainfo(0, 0) ) # (1, 0) meta_register.register( [ torch.Tensor.flatten, torch.flatten, torch.Tensor.transpose, torch.transpose, torch.Tensor.permute, torch.permute, torch.Tensor.split, torch.split, torch.Tensor.view, ] )(tensor_related_metainfo(1, 0)) # (1, 1) meta_register.register([torch.Tensor.type, torch.Tensor.contiguous])(tensor_related_metainfo(1, 1))
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false
hpcaitech/ColossalAI
https://github.com/hpcaitech/ColossalAI/blob/b1915d2889543949eb5b610241f1515c73df5059/colossalai/auto_parallel/meta_profiler/meta_registry/linear.py
colossalai/auto_parallel/meta_profiler/meta_registry/linear.py
from functools import reduce from typing import List, Tuple import torch from colossalai._analyzer._subclasses.flop_tensor import flop_mapping from colossalai._analyzer.fx.node_util import compute_size_in_bytes from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, TrainCycleItem from ..registry import meta_register __all__ = ["linear_meta_info", "matmul_meta_info"] @meta_register.register(torch.nn.functional.linear) @meta_register.register(torch.nn.Linear) def linear_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """torch.nn.Linear & torch.nn.functional.linear meta info generator NOTE: currently we separate the bias part from the biased linear ops, we will consider the memory consumption in add metainfo generator, but we will hold the bias mechanism in the linear metainfo generator for future use. graph(): %input_2 : [#users=2] = placeholder[target=placeholder](default=) %addmm_default : [#users=1] = call_function[target=torch.ops.aten.addmm.default](args = (None, %input_2, None), kwargs = {}) %zeros_like_default : [#users=3] = call_function[target=torch.ops.aten.zeros_like.default](args = (%addmm_default,), kwargs = {dtype: None, layout: None, device: None, pin_memory: None}) %detach_default : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%input_2,), kwargs = {}) %mm_default : [#users=1] = call_function[target=torch.ops.aten.mm.default](args = (%zeros_like_default, None), kwargs = {}) %t_default : [#users=1] = call_function[target=torch.ops.aten.t.default](args = (%zeros_like_default,), kwargs = {}) %mm_default_1 : [#users=1] = call_function[target=torch.ops.aten.mm.default](args = (%t_default, %detach_default), kwargs = {}) %t_default_1 : [#users=1] = call_function[target=torch.ops.aten.t.default](args = (%mm_default_1,), kwargs = {}) %sum_dim_int_list : [#users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%zeros_like_default, [None], None), kwargs = {}) %view_default : [#users=1] = call_function[target=torch.ops.aten.view.default](args = (%sum_dim_int_list, [None]), kwargs = {}) %detach_default_1 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%view_default,), kwargs = {}) %detach_default_2 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_1,), kwargs = {}) %detach_default_3 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%mm_default,), kwargs = {}) %detach_default_4 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_3,), kwargs = {}) %t_default_2 : [#users=1] = call_function[target=torch.ops.aten.t.default](args = (%t_default_1,), kwargs = {}) %detach_default_5 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%t_default_2,), kwargs = {}) %detach_default_6 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_5,), kwargs = {}) The one without bias is graph(): %input_2 : [#users=2] = placeholder[target=placeholder](default=) %mm_default : [#users=1] = call_function[target=torch.ops.aten.mm.default](args = (%input_2, None), kwargs = {}) %zeros_like_default : [#users=2] = call_function[target=torch.ops.aten.zeros_like.default](args = (%mm_default,), kwargs = {dtype: None, layout: None, device: None, pin_memory: None}) %detach_default : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%input_2,), kwargs = {}) %t_default : [#users=1] = call_function[target=torch.ops.aten.t.default](args = (%zeros_like_default,), kwargs = {}) %mm_default_1 : [#users=1] = call_function[target=torch.ops.aten.mm.default](args = (%t_default, %detach_default), kwargs = {}) %t_default_1 : [#users=1] = call_function[target=torch.ops.aten.t.default](args = (%mm_default_1,), kwargs = {}) %mm_default_2 : [#users=1] = call_function[target=torch.ops.aten.mm.default](args = (%zeros_like_default, None), kwargs = {}) %detach_default_1 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%mm_default_2,), kwargs = {}) %detach_default_2 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_1,), kwargs = {}) %t_default_2 : [#users=1] = call_function[target=torch.ops.aten.t.default](args = (%t_default_1,), kwargs = {}) %detach_default_3 : [#users=1] = call_function[target=torch.ops.aten.detach.default](args = (%t_default_2,), kwargs = {}) %detach_default_4 : [#users=0] = call_function[target=torch.ops.aten.detach.default](args = (%detach_default_3,), kwargs = {}) Returns: Tuple[TrainCycleItem, TrainCycleItem, bool]: compute cost, memory cost and forward inputs """ has_bias: bool = False input_tensor = args[0].data output_tensor = args[2].data if len(args) == 4: weight_tensors = [args[1].data, args[3].data] else: weight_tensors = [args[1].data] # process the dimension of input and output if len(input_tensor.shape) > 2: input_tensor: torch.Tensor input_tensor = input_tensor.view(-1, input_tensor.shape[-1]) if len(output_tensor.shape) > 2: output_tensor: torch.Tensor output_tensor = output_tensor.view(-1, output_tensor.shape[-1]) if len(weight_tensors) > 1: has_bias = True if len(weight_tensors[0].shape) == 2: weight_tensor, bias_tensor = weight_tensors else: bias_tensor, weight_tensor = weight_tensors else: weight_tensor = weight_tensors[0] if has_bias: # calculate cost with bias # the fwd op with compute cost is addmm # the bwd op with compute cost is mm * 2 and sum.dim_IntList # calculate compute cost fwd_compute_cost = flop_mapping[torch.ops.aten.addmm.default]( [bias_tensor, input_tensor, torch.transpose(weight_tensor, 0, 1)], (output_tensor,) ) bwd_compute_cost = ( flop_mapping[torch.ops.aten.mm.default]([output_tensor, weight_tensor], (input_tensor,)) + flop_mapping[torch.ops.aten.mm.default]( [torch.transpose(output_tensor, 0, 1), input_tensor], (weight_tensor,) ) + flop_mapping[torch.ops.aten.sum.dim_IntList]([output_tensor], (bias_tensor,)) ) compute_cost = TrainCycleItem( fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost ) # calculate memory cost # NOTE: Linear don't have buffer and temp in forward and backward phase # the forward activation cost is the size of output_tensor, parameter cost is the size of weight_tensor and bias_tensor # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, output_tensor]), parameter=compute_size_in_bytes([weight_tensor, bias_tensor]), temp=0, buffer=0, ) # the backward activation cost is the size of input_tensor, weight_tensor and bias_tensor, parameter cost is 0 bwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, weight_tensor, bias_tensor]), parameter=compute_size_in_bytes([weight_tensor, bias_tensor]), temp=0, buffer=0, ) # total cost is to sum the forward and backward cost total_cost = MemoryCost( activation=fwd_memory_cost.activation + bwd_memory_cost.activation, parameter=fwd_memory_cost.parameter + bwd_memory_cost.parameter, ) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_cost) else: # calculate cost without bias # the fwd op with compute cost is mm # the bwd op with compute cost is mm * 2 # calculate compute cost fwd_compute_cost = flop_mapping[torch.ops.aten.mm.default]( [input_tensor, torch.transpose(weight_tensor, 0, 1)], (output_tensor,) ) bwd_compute_cost = flop_mapping[torch.ops.aten.mm.default]( [output_tensor, weight_tensor], (input_tensor,) ) + flop_mapping[torch.ops.aten.mm.default]( [torch.transpose(output_tensor, 0, 1), input_tensor], (weight_tensor,) ) compute_cost = TrainCycleItem( fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost ) # calculate memory cost # NOTE: Linear don't have buffer and temp in forward and backward phase # the forward activation cost is the size of output_tensor, parameter cost is the size of weight_tensor # NOTE: currently in SPMD solver we always believe that there will be a new tensor created in forward fwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, output_tensor]), parameter=compute_size_in_bytes(weight_tensor), temp=0, buffer=0, ) # the backward activation cost is the size of input_tensor and weight_tensor, parameter cost is 0 bwd_memory_cost = MemoryCost( activation=compute_size_in_bytes([input_tensor, weight_tensor]), parameter=compute_size_in_bytes(weight_tensor), temp=0, buffer=0, ) # total cost is to sum the forward and backward cost total_cost = MemoryCost( activation=fwd_memory_cost.activation + bwd_memory_cost.activation, parameter=fwd_memory_cost.parameter + bwd_memory_cost.parameter, ) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = [torch.zeros_like(input_tensor, device="meta")] fwd_buffer = [] fwd_out = [torch.zeros_like(output_tensor, device="meta")] return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out @meta_register.register(torch.matmul) def matmul_meta_info(*args, **kwargs) -> Tuple[TrainCycleItem, TrainCycleItem, List[torch.Tensor]]: """torch.matmul meta info generator There are several cases for torch.matmul: 1. Vector-vector multiplication => no temp memory, forward memory cost is 1 element (could be neglected), backward memory cost is the same as two input vectors. 2. Matrix-vector multiplication => if the first input is matrix, no temp memory is needed, otherwise, there is a temp memory in the backward phase for the transpose of the matrix. The forward memory cost is the size of output tensor, backward memory cost is the size of the two inputs; if the first input is vector, the forward memory cost is the size of the output tensor, and during the backward phase, it will allocate a temp memory the same size as the input matrix, and allocate memory for the gradient of two inputs. 3. Batched Matrix-vector multiplication => if the first input is the batched matrix, no temp memory, the forward memory cost is the size of output tensor, backward memory cost is the size of the two inputs; if the second input is the batched matrix, the matmul will allocate memory for the gradient of the batched matrix in the forward phase (as they create a new tensor without the former batches), so the forward memory cost is the output tensor and the newly created matrix (take the same amount of memory of the input batched matrix). During the backward phase, it will allocate a temp memory the same size as input batched matrix, and allocate a tensor for the gradient of the input vector. The gradient of the batched matrix will be stored in the memory allocated during the forward phase. 3. Matrix-matrix multiplication => no temp memory, forward memory is the size of output tensor, backward memory is the size of the two inputs 4. Batched matrix-matrix multiplication => if the first input is the batched matrix, no temp memory, the forward memory cost is the size of two inputs and backward memory cost is the size of the output tensor; if the second input is the batched matrix, during the forward phase it will allocate memory for the output and gradient of the second input, and has a temp memory the same size as the output, during the backward phase, it will allocate memory for the gradient of the first input and has a temp memory which is as big as output and the second input. 5. Batched matrix-batched matrix multiplication => if the two inputs have the same batch dimensions, no temp memory, the forward memory cost is the size of output, backward memory cost is the size of the two inputs; it the two inputs have different batch dimensions, during the forward phase it will allocate memory of the expanded inputs (so that the batch dimensions could match) and the output, and during the backward phase, it has a temp memory of the size of two expanded inputs, and it will allocate memory for the gradient of the two inputs and discard the expanded inputs allocated during the forward phase. Returns: Tuple[TrainCycleItem, TrainCycleItem, bool]: compute cost, memory cost and forward inputs """ # Get input and output tensors input_tensors = [args[0].data, args[1].data] output_tensors = [args[-1].data] # Check dimension if all(len(tensor.shape) == 1 for tensor in input_tensors): # Dot fwd_compute_cost = flop_mapping[torch.ops.aten.matmul.default](input_tensors, output_tensors) bwd_compute_cost = flop_mapping[torch.ops.aten.mul.Tensor](input_tensors[0], output_tensors) * 2 fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(output_tensors), parameter=0, temp=0, buffer=0) bwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(input_tensors), parameter=0, temp=0, buffer=0) elif len(input_tensors[0].shape) >= 2 and len(input_tensors[1].shape) == 1: # gemv case 1: matrix-vector multiplication # & # batched gemv case 1: batched matrix-vector multiplication fwd_compute_cost = flop_mapping[torch.ops.aten.matmul.default]( [input_tensors[0].reshape(-1, input_tensors[0].shape[-1]), input_tensors[1]], output_tensors ) # combine the dimensions of output bwd_compute_cost = flop_mapping[torch.ops.aten.mul.Tensor]( [output_tensors[0].reshape(-1), input_tensors[1]], output_tensors ) + flop_mapping[torch.ops.aten.matmul.default]( [input_tensors[0].reshape(-1, input_tensors[0].shape[-1]).transpose(0, 1), output_tensors[0].reshape(-1)], output_tensors, ) fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(output_tensors), parameter=0, temp=0, buffer=0) bwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(input_tensors), parameter=0, temp=0, buffer=0) elif len(input_tensors[0].shape) == 1 and len(input_tensors[1].shape) == 2: # gemv case 2: vector-matrix multiplication fwd_compute_cost = flop_mapping[torch.ops.aten.matmul.default](input_tensors, output_tensors) bwd_compute_cost = flop_mapping[torch.ops.aten.mul.Tensor]( [output_tensors[0], input_tensors[0]], output_tensors ) + flop_mapping[torch.ops.aten.matmul.default]([input_tensors[1], output_tensors[0]], output_tensors) fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(output_tensors), parameter=0, temp=0, buffer=0) bwd_mem_cost = MemoryCost( activation=compute_size_in_bytes(input_tensors), parameter=0, temp=compute_size_in_bytes(input_tensors[1]), buffer=0, ) elif len(input_tensors[0].shape) == 1 and len(input_tensors[1].shape) >= 3: # batched gemv case 2: vector-batched matrix multiplication fwd_compute_cost = flop_mapping[torch.ops.aten.matmul.default]( [input_tensors[1].transpose(-2, -1).reshape(-1, input_tensors[1].shape[-2]), input_tensors[0]], [output_tensors[0].reshape(-1)], ) # combine the dimensions of output bwd_compute_cost = flop_mapping[torch.ops.aten.mul.Tensor]( [output_tensors[0].reshape(-1), input_tensors[0]], output_tensors ) + flop_mapping[torch.ops.aten.matmul.default]( [ input_tensors[1].transpose(-2, -1).reshape(-1, input_tensors[1].shape[-2]).transpose(0, 1), output_tensors[0].reshape(-1), ], output_tensors, ) fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(output_tensors + [input_tensors[1]])) bwd_mem_cost = MemoryCost( activation=compute_size_in_bytes(input_tensors[0]), parameter=0, temp=compute_size_in_bytes(input_tensors[1]), buffer=0, ) elif len(input_tensors[0].shape) >= 2 and len(input_tensors[1].shape) == 2: # gemm & batched gemm case 1: batched matrix-matrix multiplication fwd_compute_cost = flop_mapping[torch.ops.aten.mm.default]( [input_tensors[0].reshape(-1, input_tensors[0].shape[-1]), input_tensors[1]], [output_tensors[0].reshape(-1, output_tensors[0].shape[-1])], ) bwd_compute_cost = flop_mapping[torch.ops.aten.mm.default]( [ input_tensors[0].reshape(-1, input_tensors[0].shape[-1]).transpose(0, 1), output_tensors[0].reshape(-1, output_tensors[0].shape[-1]), ], [input_tensors[1]], ) + flop_mapping[torch.ops.aten.mm.default]( [output_tensors[0].reshape(-1, output_tensors[0].shape[-1]), input_tensors[1].transpose(0, 1)], [input_tensors[0].reshape(-1, input_tensors[0].shape[-1])], ) fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(output_tensors), parameter=0, temp=0, buffer=0) bwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(input_tensors), parameter=0, temp=0, buffer=0) elif len(input_tensors[0].shape) == 2 and len(input_tensors[1].shape) >= 3: # batched gemm case 2: matrix-batched matrix multiplication fwd_compute_cost = flop_mapping[torch.ops.aten.mm.default]( [ input_tensors[1].transpose(-2, -1).reshape(-1, input_tensors[1].shape[-2]), input_tensors[0].transpose(0, 1), ], [output_tensors[0].transpose(-2, -1)], ) bwd_compute_cost = flop_mapping[torch.ops.aten.mm.default]( [ output_tensors[0].transpose(-2, -1).reshape(-1, output_tensors[0].shape[-2]).transpose(0, 1), input_tensors[1].transpose(-2, -1).reshape(-1, input_tensors[1].shape[-2]), ], [input_tensors[0]], ) + flop_mapping[torch.ops.aten.mm.default]( [output_tensors[0].transpose(-2, -1).reshape(-1, output_tensors[0].shape[-2]), input_tensors[0]], [input_tensors[1].transpose(-2, -1).reshape(-1, input_tensors[1].shape[-2])], ) fwd_mem_cost = MemoryCost( activation=compute_size_in_bytes(output_tensors) + compute_size_in_bytes(input_tensors[1]), temp=compute_size_in_bytes(output_tensors), ) bwd_mem_cost = MemoryCost( activation=compute_size_in_bytes(input_tensors[0]), parameter=0, temp=compute_size_in_bytes(input_tensors[1]) + compute_size_in_bytes(output_tensors), ) elif all(len(tensor.shape) >= 3 for tensor in input_tensors): # Batched matrix-batched matrix multiplication # Fetch shape of the two inputs and see if the batch dimensions are the same _is_batch_dims_same = True if len(input_tensors[0].shape) == len(input_tensors[1].shape): for shape_0, shape_1 in zip(input_tensors[0].shape[:-2], input_tensors[1].shape[:-2]): if shape_0 != shape_1: _is_batch_dims_same = False break else: _is_batch_dims_same = False # retrieve dimensions input_dim_00 = input_tensors[0].shape[-2] input_dim_01 = input_tensors[0].shape[-1] input_dim_10 = input_tensors[1].shape[-2] input_dim_11 = input_tensors[1].shape[-1] output_dim_0 = output_tensors[0].shape[-2] output_dim_1 = output_tensors[0].shape[-1] if _is_batch_dims_same: # Case 1: batch dimensions are the same # Forward compute cost: C = A * B fwd_compute_cost = flop_mapping[torch.ops.aten.bmm.default]( [ input_tensors[0].reshape(-1, input_dim_00, input_dim_01), input_tensors[1].reshape(-1, input_dim_10, input_dim_11), ], [output_tensors[0].reshape(-1, output_dim_0, output_dim_1)], ) # Backward compute cost: dB = A^T * dC, dA = dC * B^T bwd_compute_cost = flop_mapping[torch.ops.aten.bmm.default]( [ input_tensors[0].transpose(-2, -1).reshape(-1, input_dim_01, input_dim_00), output_tensors[0].reshape(-1, output_dim_0, output_dim_1), ], [input_tensors[1].reshape(-1, input_dim_11, input_dim_10)], ) + flop_mapping[torch.ops.aten.bmm.default]( [ output_tensors[0].reshape(-1, output_dim_0, output_dim_1), input_tensors[1].transpose(-2, -1).reshape(-1, input_dim_11, input_dim_10), ], [input_tensors[0].reshape(-1, input_dim_00, input_dim_01)], ) fwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(output_tensors)) bwd_mem_cost = MemoryCost(activation=compute_size_in_bytes(input_tensors)) else: # Case 2: batch dimensions are different batch_dims = output_tensors[0].shape[:-2] extended_input_0 = torch.rand( reduce(lambda x, y: x * y, batch_dims), input_dim_00, input_dim_01, device="meta" ) extended_input_1 = torch.rand( reduce(lambda x, y: x * y, batch_dims), input_dim_10, input_dim_11, device="meta" ) # Forward compute cost: C = A * B fwd_compute_cost = flop_mapping[torch.ops.aten.bmm.default]( [extended_input_0, extended_input_1], [output_tensors[0].reshape(-1, output_dim_0, output_dim_1)] ) # Backward compute cost: dB = A^T * dC, dA = dC * B^T bwd_compute_cost = flop_mapping[torch.ops.aten.bmm.default]( [extended_input_0.transpose(-2, -1), output_tensors[0].reshape(-1, output_dim_0, output_dim_1)], [extended_input_1], ) + flop_mapping[torch.ops.aten.bmm.default]( [output_tensors[0].reshape(-1, output_dim_0, output_dim_1), extended_input_1.transpose(-2, -1)], [extended_input_0], ) fwd_mem_cost = MemoryCost( activation=compute_size_in_bytes([output_tensors[0], extended_input_0, extended_input_1]) ) bwd_mem_cost = MemoryCost( activation=compute_size_in_bytes(input_tensors) - compute_size_in_bytes([extended_input_0, extended_input_1]), temp=compute_size_in_bytes([extended_input_0, extended_input_1]), ) # compute cost compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) # memory cost total_cost = MemoryCost( activation=fwd_mem_cost.activation + bwd_mem_cost.activation, parameter=fwd_mem_cost.parameter + bwd_mem_cost.parameter, temp=fwd_mem_cost.temp + bwd_mem_cost.temp, buffer=fwd_mem_cost.buffer + bwd_mem_cost.buffer, ) memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_cost) # store fwd_in, fwd_buffer, fwd_out fwd_in = input_tensors fwd_buffer = [] fwd_out = output_tensors return compute_cost, memory_cost, fwd_in, fwd_buffer, fwd_out
python
Apache-2.0
b1915d2889543949eb5b610241f1515c73df5059
2026-01-04T14:40:19.002665Z
false