mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-01 09:07:51 +00:00
[setup] support pre-build and jit-build of cuda kernels (#2374)
* [setup] support pre-build and jit-build of cuda kernels * polish code * polish code * polish code * polish code * polish code * polish code
This commit is contained in:
@@ -6,13 +6,32 @@ from torch import Tensor
|
||||
from torch.distributed import ProcessGroup
|
||||
|
||||
COL_MOE_KERNEL_FLAG = False
|
||||
from colossalai.kernel import moe
|
||||
|
||||
try:
|
||||
from colossalai._C import moe
|
||||
except:
|
||||
moe = None
|
||||
|
||||
|
||||
def build_moe_if_not_prebuilt():
|
||||
# load moe kernel during runtime if not pre-built
|
||||
global moe
|
||||
if moe is None:
|
||||
from colossalai.kernel.op_builder import MOEBuilder
|
||||
moe = MOEBuilder().load()
|
||||
|
||||
|
||||
class AllGather(torch.autograd.Function):
|
||||
|
||||
@staticmethod
|
||||
def forward(ctx: Any, inputs: Tensor, group: Optional[ProcessGroup] = None) -> Tensor:
|
||||
|
||||
global moe
|
||||
|
||||
if moe is None:
|
||||
from colossalai.kernel.op_builder import MOEBuilder
|
||||
moe = MOEBuilder().load()
|
||||
|
||||
if ctx is not None:
|
||||
ctx.comm_grp = group
|
||||
|
||||
@@ -85,6 +104,9 @@ class MoeDispatch(torch.autograd.Function):
|
||||
s = tokens.size(0)
|
||||
h = tokens.size(1)
|
||||
|
||||
# load moe kernel during runtime if not pre-built
|
||||
build_moe_if_not_prebuilt()
|
||||
|
||||
expert_input = moe.dispatch_forward(s, ec, h, tokens, mask, dest_idx)
|
||||
|
||||
ctx.save_for_backward(mask, dest_idx)
|
||||
@@ -112,6 +134,9 @@ class MoeCombine(torch.autograd.Function):
|
||||
c = ec // e
|
||||
h = expert_tokens.size(-1)
|
||||
|
||||
# load moe kernel during runtime if not pre-built
|
||||
build_moe_if_not_prebuilt()
|
||||
|
||||
fp16_flag = (expert_tokens.dtype == torch.float16)
|
||||
cb_input = expert_tokens.to(torch.float32) if fp16_flag else expert_tokens
|
||||
ctokens = moe.combine_forward(s, e, c, h, cb_input, logits, mask, dest_idx)
|
||||
@@ -143,6 +168,8 @@ def moe_cumsum(inputs: Tensor):
|
||||
dim0 = inputs.size(0)
|
||||
flag = (dim0 <= 1024) or (dim0 <= 2048 and dim0 % 2 == 0) or (dim0 % 4 == 0)
|
||||
if flag and COL_MOE_KERNEL_FLAG:
|
||||
# load moe kernel during runtime if not pre-built
|
||||
build_moe_if_not_prebuilt()
|
||||
return moe.cumsum_sub_one(inputs)
|
||||
else:
|
||||
return torch.cumsum(inputs, dim=0) - 1
|
||||
|
@@ -3,6 +3,7 @@ from typing import Optional
|
||||
|
||||
import torch
|
||||
|
||||
from colossalai.kernel.op_builder import CPUAdamBuilder
|
||||
from colossalai.registry import OPTIMIZERS
|
||||
|
||||
from .nvme_optimizer import NVMeOptimizer
|
||||
@@ -76,12 +77,8 @@ class CPUAdam(NVMeOptimizer):
|
||||
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
|
||||
try:
|
||||
import colossalai._C.cpu_optim
|
||||
except ImportError:
|
||||
raise ImportError('Please install colossalai from source code to use CPUAdam')
|
||||
self.cpu_adam_op = colossalai._C.cpu_optim.CPUAdamOptimizer(lr, betas[0], betas[1], eps, weight_decay,
|
||||
adamw_mode)
|
||||
cpu_adam = CPUAdamBuilder().load()
|
||||
self.cpu_adam_op = cpu_adam.CPUAdamOptimizer(lr, betas[0], betas[1], eps, weight_decay, adamw_mode)
|
||||
|
||||
def torch_adam_update(self,
|
||||
data,
|
||||
|
@@ -65,7 +65,8 @@ class FusedAdam(torch.optim.Optimizer):
|
||||
self.adamw_mode = 1 if adamw_mode else 0
|
||||
self.set_grad_none = set_grad_none
|
||||
if multi_tensor_applier.available:
|
||||
from colossalai.kernel import fused_optim
|
||||
from colossalai.kernel.op_builder import FusedOptimBuilder
|
||||
fused_optim = FusedOptimBuilder().load()
|
||||
|
||||
# Skip buffer
|
||||
self._dummy_overflow_buf = torch.cuda.IntTensor([0])
|
||||
|
@@ -76,7 +76,8 @@ class FusedLAMB(torch.optim.Optimizer):
|
||||
max_grad_norm=max_grad_norm)
|
||||
super(FusedLAMB, self).__init__(params, defaults)
|
||||
if multi_tensor_applier.available:
|
||||
from colossalai.kernel import fused_optim
|
||||
from colossalai.kernel.op_builder import FusedOptimBuilder
|
||||
fused_optim = FusedOptimBuilder().load()
|
||||
|
||||
self.multi_tensor_l2norm = fused_optim.multi_tensor_l2norm
|
||||
# Skip buffer
|
||||
|
@@ -80,7 +80,8 @@ class FusedSGD(Optimizer):
|
||||
self.wd_after_momentum = wd_after_momentum
|
||||
|
||||
if multi_tensor_applier.available:
|
||||
from colossalai.kernel import fused_optim
|
||||
from colossalai.kernel.op_builder import FusedOptimBuilder
|
||||
fused_optim = FusedOptimBuilder().load()
|
||||
|
||||
# Skip buffer
|
||||
self._dummy_overflow_buf = torch.tensor([0],
|
||||
|
@@ -2,6 +2,7 @@ from typing import Any, Optional
|
||||
|
||||
import torch
|
||||
|
||||
from colossalai.kernel.op_builder import CPUAdamBuilder, FusedOptimBuilder
|
||||
from colossalai.registry import OPTIMIZERS
|
||||
from colossalai.utils import multi_tensor_applier
|
||||
|
||||
@@ -77,7 +78,9 @@ class HybridAdam(NVMeOptimizer):
|
||||
super(HybridAdam, self).__init__(model_params, default_args, nvme_offload_fraction, nvme_offload_dir)
|
||||
self.adamw_mode = adamw_mode
|
||||
|
||||
from colossalai.kernel import cpu_optim, fused_optim
|
||||
# build during runtime if not found
|
||||
cpu_optim = CPUAdamBuilder().load()
|
||||
fused_optim = FusedOptimBuilder().load()
|
||||
self.cpu_adam_op = cpu_optim.CPUAdamOptimizer(lr, betas[0], betas[1], eps, weight_decay, adamw_mode)
|
||||
|
||||
self.gpu_adam_op = fused_optim.multi_tensor_adam
|
||||
|
Reference in New Issue
Block a user