Implementation:Predibase Lorax Flash NeoX Modeling
| Knowledge Sources | |
|---|---|
| Domains | Model_Architecture, Inference |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Optimized GPT-NeoX transformer implementation for LoRax inference serving with flash attention and rotary positional embeddings.
Description
This module implements the GPT-NeoX architecture adapted for high-throughput inference in the LoRax serving framework. GPT-NeoX features rotary positional embeddings and supports both parallel and sequential residual connections. The main components are:
- FlashNeoxAttention -- Multi-head self-attention with rotary positional embeddings (RoPE) applied to queries and keys. Uses a fused query_key_value projection that is permuted to separate Q, K, V heads. Supports flash attention for prefill and paged attention for decode.
- FlashMLP -- Feed-forward network with dense_h_to_4h and dense_4h_to_h projections, supporting configurable activation functions including various GELU approximations.
- FlashNeoXLayer -- A single transformer layer that supports two residual connection modes: parallel residual (attention and MLP computed in parallel from the same normalized input, then summed) and sequential residual (standard pre-norm architecture). Includes input_layernorm and post_attention_layernorm.
- FlashGPTNeoXModel -- The full transformer model with token embedding (embed_in), stacked FlashNeoXLayer instances, and final layer normalization. Computes rotary cos/sin once and passes them to all layers.
- FlashGPTNeoXForCausalLM -- The top-level causal language model wrapping FlashGPTNeoXModel with an embed_out head via TensorParallelHead.
Note: This implementation does not include LoRA adapter support, unlike some other flash model implementations in the LoRax codebase.
Usage
Used internally by the LoRax server when serving GPT-NeoX-based models (such as Pythia). Loaded via the model registry when the model config type matches.
Code Reference
Source Location
- Repository: Predibase_Lorax
- File: server/lorax_server/models/custom_modeling/flash_neox_modeling.py
- Lines: 1-379
Signature
class FlashGPTNeoXForCausalLM(FlashGPTNeoXPreTrainedModel):
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,
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_neox_modeling import FlashGPTNeoXForCausalLM
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 |
| 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] | Always None for GPT-NeoX |
Usage Examples
# Internal usage within LoRax server
from lorax_server.models.custom_modeling.flash_neox_modeling import FlashGPTNeoXForCausalLM
# Model instantiated by model registry, not directly by users