Implementation:Predibase Lorax Flash Phi Modeling
| Knowledge Sources | |
|---|---|
| Domains | Model_Architecture, Inference |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Optimized Phi (1.5/2) transformer implementation for LoRax inference serving with flash attention, partial rotary embeddings, and LoRA adapter support.
Description
This module implements the Microsoft Phi architecture (Phi-1.5 and Phi-2) adapted for high-throughput inference in the LoRax serving framework. Phi models use a parallel residual connection pattern where attention and MLP outputs are computed from the same normalized input and summed. The main components are:
- FlashPhiAttention -- Multi-head attention with partial rotary embeddings (controlled by config.partial_rotary_factor), grouped-query attention support via n_head_kv, and separate Q/K/V projections loaded via TensorParallelMultiAdapterLinear. Supports LoRA adapters on q_proj, k_proj, v_proj, and dense (output projection).
- PhiMLP -- Feed-forward network with fc1 and fc2 projections, configurable activation function (supporting various GELU approximations), and LoRA adapter support on both layers.
- FlashPhiLayer -- A single transformer layer using a parallel residual pattern: both attention and MLP are applied to the same layer-normalized input, then their outputs are summed. Uses FastLayerNorm with an all-reduce for tensor parallelism.
- FlashPhiModel -- The full transformer model with token embedding (embed_tokens), stacked FlashPhiLayer instances, and final layer normalization (final_layernorm). Computes rotary cos/sin once and passes them to all layers.
- FlashPhiForCausalLM -- The top-level causal language model wrapping FlashPhiModel with a MultiAdapterHead LM head supporting LoRA on the output projection.
Usage
Used internally by the LoRax server when serving Phi-based models (Phi-1.5, Phi-2). 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_phi_modeling.py
- Lines: 1-411
Signature
class FlashPhiForCausalLM(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_phi_modeling import FlashPhiForCausalLM
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 partial 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_phi_modeling import FlashPhiForCausalLM
# Model instantiated by model registry, not directly by users