Skip to navigation

nemo_automodel.components.models.gpt_oss.layers

View as MarkdownOpen in Claude

Module Contents

Classes

NameDescription
GptOssAttention-

Functions

NameDescription
_has_thd_metadata-
_use_physical_fa4_layoutRestrict physical THD dispatch to the measured SM100 kernel geometry.

API

class nemo_automodel.components.models.gpt_oss.layers.GptOssAttention(
config: transformers.models.gpt_oss.configuration_gpt_oss.GptOssConfig,
use_sliding_attention: bool = False
)

Bases: Module

head_dim
= config.head_dim
hidden_size
= config.hidden_size
k_proj
num_attention_heads
= config.num_attention_heads
num_key_value_heads
= config.num_key_value_heads
o_proj
q_proj
sinks
sliding_window
softmax_scale
= self.head_dim ** -0.5
v_proj
yarn_concentration
= yarn_get_mscale(config.rope_scaling['factor'])
nemo_automodel.components.models.gpt_oss.layers.GptOssAttention.forward(
x: torch.Tensor,
freqs_cis: torch.Tensor,
attention_mask: torch.Tensor | None = None,
attn_kwargs: typing.Any = {}
) -> torch.Tensor

Attend with the configured backend and the original learned sink parameter.

Parameters:

x
torch.Tensor

Hidden states [batch, sequence, hidden] or [physical_tokens, hidden].

freqs_cis
torch.Tensor

Non-fused cosine/sine tensor [batch, sequence, rotary_dim] or [physical_tokens, rotary_dim]; fused angles have shape [sequence, 1, 1, rotary_dim]. rotary_dim is even and at most head_dim.

attention_mask
torch.Tensor | NoneDefaults to None

Optional BSHD padding mask [batch, sequence].

**attn_kwargs
AnyDefaults to {}

Logical/physical offsets [documents + 1] and optional private compact FA4 indices [valid_tokens], prepared at model entry.

Returns: torch.Tensor

Projected states with the same layout as x. Physical padding remains present.

nemo_automodel.components.models.gpt_oss.layers.GptOssAttention.init_weights(
buffer_device: torch.device,
init_std: float = 0.02
)
nemo_automodel.components.models.gpt_oss.layers._has_thd_metadata(
attn_kwargs: dict[str, typing.Any]
) -> bool
nemo_automodel.components.models.gpt_oss.layers._use_physical_fa4_layout(
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
kwargs: dict[str, typing.Any]
) -> bool

Restrict physical THD dispatch to the measured SM100 kernel geometry.

Parameters:

q
torch.Tensor

Query tensor of shape [physical_tokens, heads, head_dim].

k
torch.Tensor

Key tensor of shape [physical_tokens, kv_heads, head_dim].

v
torch.Tensor

Value tensor of shape [physical_tokens, kv_heads, head_dim].

kwargs
dict[str, Any]

Prepared metadata: _fa4_seqused is int32 [documents], _fa4_physical_cu_seqlens is int32 [documents + 1], packed_token_indices is int64 [valid_tokens], and learnable_sink is BF16 [heads]. No input is mutated.

Returns: bool

Whether the shape, dtype, device, window and prefix metadata fall in