Skip to main content

Model Configuration

This page documents the PretrainedConfig class, which defines model architecture parameters and configuration for TensorRT-LLM models.

Overview

The PretrainedConfig class is the base configuration class for all TensorRT-LLM models. It contains architecture-specific parameters that define the model structure, such as layer counts, hidden dimensions, attention configuration, and more. Model-specific config classes (like LlamaConfig, GPTConfig, etc.) inherit from PretrainedConfig and add model-specific parameters.

Location

Source file: tensorrt_llm/models/modeling_utils.py:346-548

Constructor Parameters

Core Architecture Parameters

str
required
The model architecture type.Examples:
  • "LlamaForCausalLM"
  • "GPTForCausalLM"
  • "MixtralForCausalLM"
Note: This is automatically set when using AutoConfig.from_hugging_face().
str
required
The data type for model weights and activations.Supported values:
  • "float16": FP16
  • "bfloat16": BF16
  • "float32": FP32
int
required
Dimensionality of the model’s hidden layers.Example: 4096 for Llama-2-7B
int
required
Number of transformer layers in the model.Example: 32 for Llama-2-7B
int
required
Number of attention heads in each layer.Example: 32 for Llama-2-7B
Optional[int]
Size of the vocabulary.Example: 32000 for Llama-2

Activation and Normalization

str
default:"gelu"
The activation function used in the feed-forward layers.Common values:
  • "gelu": Gaussian Error Linear Unit
  • "silu": Sigmoid Linear Unit (used in Llama)
  • "relu": Rectified Linear Unit
float
The epsilon value for layer normalization.
str
default:"float32"
The data type for output logits.

Position Embeddings

Union[PositionEmbeddingType, str]
default:"learned_absolute"
The type of position embedding to use.Options:
  • "learned_absolute": Learned absolute position embeddings
  • "rope_gpt_neox": Rotary Position Embeddings (GPT-NeoX style)
  • "rope_gptj": Rotary Position Embeddings (GPT-J style)
  • "alibi": Attention with Linear Biases
  • "alibi_with_scale": ALiBi with learned scale
  • "relative": Relative position embeddings
Optional[int]
The maximum sequence length that the model can handle.Example: 4096 for Llama-2
Optional[int]
The dimensionality of rotary position embeddings.Default calculation: If not specified, computed as head_size * rotary_pct where rotary_pct defaults to 1.0.

Attention Configuration

Optional[int]
Number of key-value heads for Grouped Query Attention (GQA).Default: If not specified, equals num_attention_heads (Multi-Head Attention).Examples:
  • num_key_value_heads == num_attention_heads: Multi-Head Attention (MHA)
  • num_key_value_heads < num_attention_heads: Grouped Query Attention (GQA)
  • num_key_value_heads == 1: Multi-Query Attention (MQA)
Optional[int]
The dimension of each attention head.Default calculation: If not specified, computed as hidden_size // num_attention_heads.
bool
default:false
Whether to apply layer normalization to queries and keys in attention.

Feed-Forward Network

Optional[int]
The dimensionality of the feed-forward network’s intermediate layer.Default calculation: If not specified, computed as hidden_size * 4.Example: 11008 for Llama-2-7B

Parallel Configuration

Optional[Union[Mapping, dict]]
The parallel mapping configuration.Mapping fields:
  • world_size: Total number of GPUs
  • rank: Current GPU rank
  • tp_size: Tensor parallel size
  • pp_size: Pipeline parallel size
  • cp_size: Context parallel size
  • gpus_per_node: Number of GPUs per node
Default: If not specified, creates a single-GPU mapping.

Quantization Configuration

Optional[Union[QuantConfig, dict]]
Quantization configuration for the model.QuantConfig fields:
  • quant_algo: Quantization algorithm
  • kv_cache_quant_algo: KV cache quantization algorithm
  • group_size: Group size for group-wise quantization
  • smoothquant_val: Smoothing parameter
  • exclude_modules: Modules to exclude from quantization
See Quantization Configuration for full details.

Embedding Configuration

bool
default:false
Whether to use parallel embedding tables (sharded across GPUs).
int
default:0
The dimension along which to shard the embedding table.Options:
  • 0: Shard along vocabulary dimension
  • 1: Shard along hidden dimension

Runtime Defaults

Optional[Union[RuntimeDefaults, dict]]
Default runtime configuration values.RuntimeDefaults fields:
  • KV cache defaults
  • Scheduling defaults
  • Performance knob defaults
These are typically loaded from saved engine configurations.

Properties

Quantization Mode

QuantMode
The quantization mode derived from the quantization configuration.Accessed via:
Optional[QuantAlgo]
The quantization algorithm.Accessed via:

KV Cache Data Type

str
The data type for KV cache.Returns:
  • "int8": If using INT8 KV cache quantization
  • "fp8": If using FP8 KV cache quantization
  • "fp4": If using FP4 KV cache quantization
  • config.dtype: Otherwise (same as model dtype)
Accessed via:

Methods

Loading and Saving

classmethod
Create a PretrainedConfig from a dictionary.Parameters:
  • config (dict): Configuration dictionary
Returns: PretrainedConfig instanceExample:
classmethod
Load a PretrainedConfig from a JSON file.Parameters:
  • config_file (str): Path to the config.json file
Returns: PretrainedConfig instanceExample:
classmethod
Load a PretrainedConfig from a checkpoint directory.Parameters:
  • ckpt_dir (str): Path to checkpoint directory
Returns: PretrainedConfig instanceExample:
method
Convert the config to a dictionary.Returns: dict representation of the configExample:
method
Save the config to a JSON file.Parameters:
  • config_file (str): Path to save the config
Example:

Rank Management

method
Set the rank for this config instance.Parameters:
  • rank (int): The GPU rank
Example:
method
Iterate over all ranks, yielding a config copy for each rank.Returns: Generator yielding config instances for each rankExample:

Model-Specific Configurations

Model-specific config classes extend PretrainedConfig with additional parameters:

Llama Configuration

Mixtral Configuration

GPT Configuration


Quantization Configuration

The quantization field accepts a QuantConfig object:

QuantConfig Fields

Optional[QuantAlgo]
Quantization algorithm for weights.Options:
  • QuantAlgo.W8A16: INT8 weight-only
  • QuantAlgo.W4A16: INT4 weight-only
  • QuantAlgo.FP8: FP8 quantization
  • QuantAlgo.NVFP4: NVFP4 quantization
  • QuantAlgo.W4A16_AWQ: AWQ INT4 quantization
  • QuantAlgo.W8A8_SQ_PER_CHANNEL: SmoothQuant per-channel
Optional[QuantAlgo]
Quantization algorithm for KV cache.Options:
  • QuantAlgo.INT8: INT8 KV cache
  • QuantAlgo.FP8: FP8 KV cache
  • QuantAlgo.NVFP4: NVFP4 KV cache
int
default:128
Group size for group-wise quantization.
float
Smoothing parameter alpha used in SmoothQuant.
Optional[List[str]]
Module name patterns that are skipped in quantization.Example:

Example Configurations

Llama-2-7B Configuration

Mixtral-8x7B Configuration

Tensor Parallel Configuration

With Quantization


Loading from HuggingFace

Most commonly, you’ll load configurations from HuggingFace models:
This automatically:
  • Downloads the model configuration
  • Converts HuggingFace config to TensorRT-LLM format
  • Sets appropriate defaults for the model architecture

See Also