bridge.models.qwen.modeling_qwen4_exp.ep_ngram_embedding#

Frozen EP-sharded lookup for Qwen3.8’s concatenated padded n-gram table.

The owner/request/return all-to-all protocol and forked shard initialization are adapted from Megatron-LM PR #7553, commit 1ce7e86a073c18bcb6ea2ca5542c91e270d06a12, megatron/core/models/engram/distributed_embedding.py: https://github.com/NVIDIA/Megatron-LM/pull/7553 https://github.com/NVIDIA/Megatron-LM/blob/1ce7e86a073c18bcb6ea2ca5542c91e270d06a12/megatron/core/models/engram/distributed_embedding.py. Unlike that trainable, multitable implementation, this module preserves one logical weight and has no embedding backward or tensor-parallel reduction.

Module Contents#

Classes#

FrozenEPNgramEmbedding

Shard one frozen padded table by rows over an explicit expert group.

API#

class bridge.models.qwen.modeling_qwen4_exp.ep_ngram_embedding.FrozenEPNgramEmbedding(
config: megatron.core.transformer.transformer_config.TransformerConfig,
num_embeddings: int,
embedding_dim: int,
*,
init_method: collections.abc.Callable[[torch.Tensor], None],
ep_group: torch.distributed.ProcessGroup,
expt_dp_group: torch.distributed.ProcessGroup,
)#

Bases: torch.nn.Module

Shard one frozen padded table by rows over an explicit expert group.

The caller computes hashes on full sequences, then slices sequence-parallel token positions before lookup. Every EP member must call forward in the same order, including members with no local requests. Expert-DP groups replicate a row shard; dense TP ranks are not additional checkpoint replicas.

Initialization

_initialize_weight(
init_method: collections.abc.Callable[[torch.Tensor], None],
) → None#

Initialize without advancing the caller’s CPU or local CUDA RNG stream.

forward(row_ids: torch.Tensor) → torch.Tensor#

Return exact table rows in the input order, with shape [*ids.shape, D].

A validation bit travels with each peer’s request count, so an invalid rank fails the whole EP group before any request/value exchange. This shares the existing count exchange rather than adding an all-reduce. Duplicate IDs and zero counts are retained; no reduction changes values.

sharded_state_dict(
prefix: str = '',
sharded_offsets: tuple = (),
metadata: dict | None = None,
) → megatron.core.dist_checkpointing.mapping.ShardedStateDict#

Keep the original single-table logical key with EP rows and EDP replicas.