Skip to main content
TensorRT-LLM provides a flexible framework for adding custom model architectures. This guide walks through the process of implementing, registering, and using custom models.

Overview

Adding a custom model involves four main steps:
  1. Model Configuration - Define the configuration class
  2. Model Definition - Implement the model architecture
  3. Weight Loading - Handle checkpoint loading and conversion
  4. Model Registration - Register the model for auto-discovery
If your model is already supported in HuggingFace Transformers, you can reuse the HuggingFace configuration and adapt the modeling code.

Prerequisites

  • Working TensorRT-LLM installation
  • Understanding of PyTorch and Transformer architectures
  • Familiarity with your target model architecture

Step 1: Model Configuration

Create a configuration class that defines all model hyperparameters.
If your model exists in HuggingFace Transformers, reuse their config:
configuration_mymodel.py
Or extend it with custom parameters:

Step 2: Model Definition

Implement your model architecture using TensorRT-LLM’s base classes and modules.

Model Structure

A typical decoder model consists of:
  1. Attention Layer - Inherits from Attention
  2. Decoder Layer - Inherits from DecoderLayer
  3. Model - Inherits from DecoderModel
  4. Model for Causal LM - Inherits from DecoderModelForCausalLM

Implementation Example

modeling_mymodel.py

Key Components

The Attention module handles all attention computation:
Features:
  • Automatic KV cache management
  • Support for GQA, MQA, MHA
  • RoPE, ALiBi position embeddings
  • Flash Attention integration
  • Tensor parallelism
Use TensorRT-LLM’s optimized modules for best performance:
Benefits:
  • Automatic tensor parallelism
  • Quantization support
  • Optimized CUDA kernels
  • Reduced memory usage
Packed Mode: Input tensors are in packed format where the first dimension is the total number of tokens in the batch:
AttentionMetadata: Contains batching and KV cache metadata:
  • Sequence lengths
  • Cumulative sequence lengths
  • KV cache locations
  • Attention mask information
Pass attn_metadata to all attention modules.

Step 3: Weight Loading

Implement weight loading to convert checkpoint weights to your model format.

Default Weight Loading

The base DecoderModelForCausalLM provides automatic weight loading that works for most models:

Custom Weight Loading

If you need custom weight mapping, override load_weights:

Weight Loading Examples

Step 4: Model Registration

Register your model so it can be auto-discovered by TensorRT-LLM.

Using Your Custom Model

Once registered, use your model like any other TensorRT-LLM model:

Complete Example

See the full out-of-tree model example:
This example implements a complete OPT model as an out-of-tree model.

Testing Your Model

1

Unit Tests

Test individual components:
2

Generation Test

Test end-to-end generation:
3

Accuracy Test

Compare outputs with reference implementation:

Best Practices

  • Inherit from base classes: Use DecoderModel, DecoderLayer, Attention for compatibility
  • Use optimized modules: Prefer TensorRT-LLM modules over PyTorch for better performance
  • Handle packed input: Ensure your model handles packed token sequences correctly
  • Pass attn_metadata: Always pass AttentionMetadata to attention modules
  • Use module-level load_weights: Call Linear.load_weights(), Embedding.load_weights() for TP/quantization
  • Handle weight fusion: Fuse QKV, gate/up projections for better performance
  • Test with different TP sizes: Verify weight loading works with tensor parallelism
  • Support quantization: Ensure weight loader handles quantized checkpoints
  • Fuse operations: Combine QKV, gate/up projections into single Linear layers
  • Use RMSNorm: Faster than LayerNorm when applicable
  • Enable tensor parallelism: Use Linear(..., tp_group=...) for automatic TP
  • Optimize memory: Use torch.utils.checkpoint for gradient checkpointing during training
  • Unit test components: Test attention, layer, model separately
  • Compare with reference: Validate outputs against HuggingFace/original implementation
  • Test all TP configurations: Test with TP=1, 2, 4, 8
  • Benchmark performance: Measure throughput and latency vs baselines

Common Issues

Problem: Tensor shape errors in forward passSolution: Remember that inputs are in packed mode:
Problem: Weights don’t load correctlySolution: Check weight names and shapes:
Problem: Tensor parallelism produces wrong resultsSolution: Ensure all Linear layers have TP config:

Example Models

Study these reference implementations:

LLaMA

Standard decoder-only model with RoPE and GQA

GPT

Classic GPT architecture with learned positions

Gemma

Model with custom normalization and sliding window

OPT (Out-of-tree)

Complete out-of-tree model example

Next Steps

Model Configuration

Configure your custom model

Quantization

Add quantization support

Deployment

Deploy your model to production