"""GPT-OSS decoder layer: attention + MoE with RMSNorm residual connections. Uses the shared ``LlamaAttention`` with ``use_sinks=True`` and ``sliding_window`` to implement GPT-OSS attention sinks and per-layer sliding window. Rotary embedding is passed through forward (created once at the model level and shared across layers). """ from __future__ import annotations import torch import torch.nn as nn from fastkernels.infra.cuda_ext import lazy_op from ..L1.rms_norm import RMSNorm from ..L2.attention import LlamaAttention from ..L2.gpt_oss_moe import GptOssMoE, _router_small_batch_kernel _C = lazy_op("candidate_gpt_oss_decoder_v3", "gpt_oss_decoder.cu") class GptOssDecoderLayer(nn.Module): def __init__(self, config, layer_idx: int): super().__init__() self.self_attn = LlamaAttention( config.hidden_size, config.num_attention_heads, config.num_key_value_heads, config.head_dim, bias=True, o_proj_bias=True, use_sinks=True, sliding_window=config.sliding_window, layer_idx=layer_idx, ) self.mlp = GptOssMoE(config) self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.post_attention_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps) @staticmethod def _rms_norm(hidden_states, residual, layernorm): if ( hidden_states.is_cuda and hidden_states.dtype == torch.bfloat16 and hidden_states.is_contiguous() and hidden_states.shape[-1] == 2880 and ( 1 < hidden_states.shape[0] <= 32 or 128 < hidden_states.shape[0] <= 512 ) ): if residual is None: out = torch.empty_like(hidden_states) _C.rms_norm_2880( out, hidden_states, layernorm.weight, layernorm.eps ) return out, hidden_states if residual.is_contiguous(): _C.add_rms_norm_2880( hidden_states, residual, layernorm.weight, layernorm.eps ) return hidden_states, residual if residual is None: return layernorm(hidden_states), hidden_states return layernorm(hidden_states, residual) def _mlp(self, hidden_states): mlp = self.mlp if not ( hidden_states.shape[0] == 1 and hidden_states.is_cuda and mlp.use_trtllm and mlp._H_pad != mlp.hidden_size and not mlp._use_custom_op ): return mlp(hidden_states) if not mlp._processed: mlp.process_weights_after_loading() padded_shape = (1, mlp._H_pad) if ( mlp._padded_hidden is None or mlp._padded_hidden.shape != padded_shape or mlp._padded_hidden.device != hidden_states.device ): mlp._padded_hidden = hidden_states.new_empty(padded_shape) router_logits = torch.empty( (1, mlp.num_experts), dtype=hidden_states.dtype, device=hidden_states.device, ) _router_small_batch_kernel[(mlp.num_experts, 1)]( hidden_states, mlp.router.weight, mlp.router.bias, router_logits, mlp._padded_hidden, K=mlp.hidden_size, E=mlp.num_experts, PAD_K=mlp._H_pad, BLOCK_K=4096, STORE_PAD=True, num_warps=4, ) output = mlp.trtllm_moe( mlp._padded_hidden, router_logits, mlp._w13_shuffled, mlp._w13_scale, mlp._w13_bias_f32, mlp._w2_shuffled, mlp._w2_scale, mlp._w2_bias_f32, ) if mlp.tp_size > 1: output = mlp.allreduce(output) return output def forward(self, positions, hidden_states, residual, rotary_emb): hidden_states, residual = self._rms_norm( hidden_states, residual, self.input_layernorm ) hidden_states = self.self_attn(positions, hidden_states, rotary_emb=rotary_emb) hidden_states, residual = self._rms_norm( hidden_states, residual, self.post_attention_layernorm ) hidden_states = self._mlp(hidden_states) return hidden_states, residual