Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Predibase Lorax Flash GPT2 Modeling

From Leeroopedia


Knowledge Sources
Domains Model_Architecture, Inference
Last Updated 2026-02-08 00:00 GMT

Overview

Optimized GPT-2 transformer implementation for LoRax inference serving with flash attention and LoRA adapter support.

Description

This module implements the GPT-2 architecture adapted for high-throughput inference in the LoRax serving framework. GPT-2 uses absolute positional embeddings (learned) rather than rotary embeddings, and employs pre-norm layer normalization. The main components are:

  • FlashGPT2Attention -- Multi-head self-attention using the fused c_attn projection (combined QKV), flash attention for prefill and paged attention for decode. Supports attention scaling options including scale_attn_by_inverse_layer_idx and reorder_and_upcast_attn. Supports LoRA adapters on c_attn and c_proj via TensorParallelMultiAdapterLinear. Uses fan_in_fan_out weight layout consistent with GPT-2 conventions.
  • GPT2MLP -- Feed-forward network with configurable activation function (from GPT2Config.activation_function), c_fc and c_proj projections with adapter support. Uses fan_in_fan_out weight layout.
  • GPT2Block -- A single transformer block combining layer normalization (ln_1, ln_2), attention, and MLP with residual connections.
  • FlashGPT2Model -- The full transformer model with token embeddings (wte), positional embeddings (wpe), stacked GPT2Block layers, and final layer normalization (ln_f).
  • FlashGPT2ForCausalLM -- The top-level causal language model that wraps FlashGPT2Model and reuses the embedding weights (wte) as the LM head via weight tying.

Usage

Used internally by the LoRax server when serving GPT-2-based models. 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_gpt2_modeling.py
  • Lines: 1-390

Signature

class FlashGPT2ForCausalLM(FlashGPT2PreTrainedModel):
    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_gpt2_modeling import FlashGPT2ForCausalLM

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 learned positional 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, computed via weight-tied embedding
speculative_logits Optional[torch.Tensor] Always None for GPT-2 (no speculative decoding support)

Usage Examples

# Internal usage within LoRax server
from lorax_server.models.custom_modeling.flash_gpt2_modeling import FlashGPT2ForCausalLM
# Model instantiated by model registry, not directly by users

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment