import torch import torch.nn as nn import triton import triton.language as tl @triton.jit def fused_moe_block_ptr_kernel( x_ptr, w_gate_ptr, w_up_ptr, out_ptr, expert_offsets_ptr, stride_x_m, stride_x_k, stride_w_e, stride_w_k, stride_w_n, stride_out_m, stride_out_n, H, I, BLOCK_SIZE_M: tl.constexpr, BLOCK_SIZE_N: tl.constexpr, BLOCK_SIZE_K: tl.constexpr, ): expert_id = tl.program_id(2) start_offset = tl.load(expert_offsets_ptr + expert_id) end_offset = tl.load(expert_offsets_ptr + expert_id + 1) M_e = end_offset - start_offset block_m = tl.program_id(0) block_n = tl.program_id(1) if block_m * BLOCK_SIZE_M >= M_e: return # Initialize block pointers x_block_ptr = tl.make_block_ptr( base=x_ptr + start_offset * stride_x_m, shape=(M_e, H), strides=(stride_x_m, stride_x_k), offsets=(block_m * BLOCK_SIZE_M, 0), block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_K), order=(1, 0), ) w_gate_block_ptr = tl.make_block_ptr( base=w_gate_ptr + expert_id * stride_w_e, shape=(H, I), strides=(stride_w_k, stride_w_n), offsets=(0, block_n * BLOCK_SIZE_N), block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_N), order=(1, 0), ) w_up_block_ptr = tl.make_block_ptr( base=w_up_ptr + expert_id * stride_w_e, shape=(H, I), strides=(stride_w_k, stride_w_n), offsets=(0, block_n * BLOCK_SIZE_N), block_shape=(BLOCK_SIZE_K, BLOCK_SIZE_N), order=(1, 0), ) out_block_ptr = tl.make_block_ptr( base=out_ptr + start_offset * stride_out_m, shape=(M_e, I), strides=(stride_out_m, stride_out_n), offsets=(block_m * BLOCK_SIZE_M, block_n * BLOCK_SIZE_N), block_shape=(BLOCK_SIZE_M, BLOCK_SIZE_N), order=(1, 0), ) accumulator_gate = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) accumulator_up = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) for k in range(0, H, BLOCK_SIZE_K): x_block = tl.load(x_block_ptr, boundary_check=(0, 1)) w_gate_block = tl.load(w_gate_block_ptr, boundary_check=(0, 1)) w_up_block = tl.load(w_up_block_ptr, boundary_check=(0, 1)) accumulator_gate += tl.dot(x_block, w_gate_block) accumulator_up += tl.dot(x_block, w_up_block) x_block_ptr = tl.advance(x_block_ptr, [0, BLOCK_SIZE_K]) w_gate_block_ptr = tl.advance(w_gate_block_ptr, [BLOCK_SIZE_K, 0]) w_up_block_ptr = tl.advance(w_up_block_ptr, [BLOCK_SIZE_K, 0]) gate_sig = tl.sigmoid(accumulator_gate.to(tl.float32)) gated_up_f32 = gate_sig * accumulator_gate * accumulator_up gated_up = gated_up_f32.to(tl.bfloat16) tl.store(out_block_ptr, gated_up, boundary_check=(0, 1)) class Model(nn.Module): def __init__(self, T_total: int, H: int, I: int, E: int, K: int): super().__init__() self.T_total = T_total self.H = H self.I = I self.E = E self.K = K self.W_gate = nn.Parameter(torch.empty(E, H, I, dtype=torch.bfloat16)) self.W_up = nn.Parameter(torch.empty(E, H, I, dtype=torch.bfloat16)) nn.init.normal_(self.W_gate, std=0.02) nn.init.normal_(self.W_up, std=0.02) def forward( self, hidden_states: torch.Tensor, # (T_perm, H) bf16 expert_offsets: torch.Tensor, # (E+1,) int32 ) -> torch.Tensor: T_perm, H = hidden_states.shape out = torch.empty(T_perm, self.I, dtype=torch.bfloat16, device=hidden_states.device) M_e = expert_offsets[1:] - expert_offsets[:-1] max_M = int(torch.max(M_e).item()) BLOCK_SIZE_M = 128 BLOCK_SIZE_N = 128 BLOCK_SIZE_K = 64 num_blocks_m = triton.cdiv(max_M, BLOCK_SIZE_M) num_blocks_n = triton.cdiv(self.I, BLOCK_SIZE_N) grid = (num_blocks_m, num_blocks_n, self.E) fused_moe_block_ptr_kernel[grid]( hidden_states, self.W_gate, self.W_up, out, expert_offsets, hidden_states.stride(0), hidden_states.stride(1), self.W_gate.stride(0), self.W_gate.stride(1), self.W_gate.stride(2), out.stride(0), out.stride(1), H, self.I, BLOCK_SIZE_M=BLOCK_SIZE_M, BLOCK_SIZE_N=BLOCK_SIZE_N, BLOCK_SIZE_K=BLOCK_SIZE_K, num_warps=8, num_stages=4, ) return out