Implementation:Predibase Lorax Flash DBRX Modeling
| Knowledge Sources | |
|---|---|
| Domains | Model_Architecture, Inference |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Optimized DBRX transformer implementation for LoRax inference serving with flash attention, LoRA adapter support, and Mixture of Experts (MoE) feed-forward layers using the Megablocks library for block-sparse operations.
Description
This module implements the DBRX Mixture of Experts architecture adapted for high-throughput inference in the LoRax serving framework. The main components are:
- DbrxConfig / DbrxAttentionConfig / DbrxFFNConfig -- Configuration classes that define model hyperparameters including attention settings (clip_qkv, kv_n_heads, rope_theta) and FFN settings (moe_num_experts, moe_top_k, ffn_hidden_size).
- DbrxAttention -- Multi-head attention with grouped-query attention (GQA), rotary positional embeddings (RoPE), QKV clipping, and paged attention for efficient KV cache management. Supports LoRA adapters via TensorParallelMultiAdapterLinear.
- DbrxNormAttentionNorm -- Wraps attention with pre- and post-attention layer normalization, providing residual connections.
- BlockSparseMoE -- The primary MoE feed-forward layer that uses Megablocks for block-sparse matrix operations, enabling efficient routing of tokens to experts without dropping tokens. Falls back to dense computation for small batch sizes (256 tokens or fewer).
- DenseMoE -- A fallback dense MoE implementation used when quantization is enabled, iterating over experts individually.
- DbrxLayer -- A single transformer layer combining DbrxNormAttentionNorm and the MoE feed-forward module.
- DbrxModel -- The full transformer model stacking embedding, multiple DbrxLayer instances, and final layer normalization.
- FlashDbrxForCausalLM -- The top-level causal language model wrapping DbrxModel with an LM head supporting multi-adapter inference.
Usage
Used internally by the LoRax server when serving DBRX-based models. Loaded via the model registry when the model config type matches. Requires the megablocks and stk libraries for block-sparse MoE computation.
Code Reference
Source Location
- Repository: Predibase_Lorax
- File: server/lorax_server/models/custom_modeling/flash_dbrx_modeling.py
- Lines: 1-1032
Signature
class FlashDbrxForCausalLM(torch.nn.Module):
def __init__(self, prefix: str, config, weights):
...
def forward(
self,
input_ids: torch.Tensor,
position_ids: torch.Tensor,
cu_seqlen_prefill: Optional[torch.Tensor],
kv_cache: List[Tuple[torch.Tensor, torch.Tensor]],
block_tables: torch.Tensor,
slots: torch.Tensor,
seqlen: Seqlen,
max_s: int,
adapter_data: AdapterBatchData,
prefill_cache_indices: Optional[torch.Tensor] = None,
lm_head_indices: Optional[torch.Tensor] = None,
skip_lm_head: bool = False,
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
...
Import
from lorax_server.models.custom_modeling.flash_dbrx_modeling import FlashDbrxForCausalLM
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input_ids | torch.Tensor | Yes | Token IDs for the input sequence |
| position_ids | torch.Tensor | Yes | Position indices for rotary embeddings |
| cu_seqlen_prefill | Optional[torch.Tensor] | Yes | Cumulative sequence lengths for flash attention prefill; None during decode |
| kv_cache | List[Tuple[torch.Tensor, torch.Tensor]] | Yes | Key-value cache tensors for each layer |
| block_tables | torch.Tensor | Yes | Block tables for paged attention |
| slots | torch.Tensor | Yes | Slot indices for KV cache storage |
| seqlen | Seqlen | Yes | Sequence length information for the batch |
| max_s | int | Yes | Maximum sequence length in the batch |
| adapter_data | AdapterBatchData | Yes | LoRA adapter configuration for the batch |
| prefill_cache_indices | Optional[torch.Tensor] | No | Indices for selective cache prefilling |
| lm_head_indices | Optional[torch.Tensor] | No | Indices to select specific positions for LM head |
| skip_lm_head | bool | No | If True, return hidden states without applying the LM head |
Outputs
| Name | Type | Description |
|---|---|---|
| logits | torch.Tensor | Next-token logits over the vocabulary (or hidden states if skip_lm_head is True) |
| speculative_logits | Optional[torch.Tensor] | Speculative decoding logits, or None |
Usage Examples
# Internal usage within LoRax server
from lorax_server.models.custom_modeling.flash_dbrx_modeling import FlashDbrxForCausalLM
# Model instantiated by model registry, not directly by users