Python module
max.pipelines.architectures.idefics3
Idefics3 vision-language architecture for multimodal text generation.
Idefics3Configβ
class max.pipelines.architectures.idefics3.Idefics3Config(*, devices, scale_factor, image_token_id, vision_config, text_config)
Bases: ArchConfigWithKVCache
Configuration for Idefics3 models.
-
Parameters:
-
- devices (list[DeviceRef])
- scale_factor (int)
- image_token_id (int)
- vision_config (Idefics3VisionConfig)
- text_config (Llama3Config)
calculate_max_seq_len()β
static calculate_max_seq_len(pipeline_config, huggingface_config)
Calculate maximum sequence length for Idefics3.
-
Parameters:
-
- pipeline_config (PipelineConfig)
- huggingface_config (AutoConfig)
-
Return type:
construct_kv_params()β
static construct_kv_params(huggingface_config, pipeline_config, devices, kv_cache_config, cache_dtype)
Get KV cache parameters for the language model.
-
Parameters:
-
- huggingface_config (AutoConfig)
- pipeline_config (PipelineConfig)
- devices (list[DeviceRef])
- kv_cache_config (KVCacheConfig)
- cache_dtype (DType)
-
Return type:
devicesβ
Devices that the Idefics3 model is parallelized over.
finalize()β
finalize(huggingface_config, llm_state_dict, return_logits, norm_method='rms_norm')
Finalize the Idefics3Config instance with state_dict dependent fields.
-
Parameters:
-
- huggingface_config (AutoConfig) β HuggingFace model configuration.
- llm_state_dict (dict[str, WeightData]) β Language model weights dictionary.
- dtype β Data type for model parameters.
- return_logits (ReturnLogits) β Return logits configuration.
- norm_method (Literal['rms_norm', 'layer_norm']) β Normalization method.
-
Return type:
-
None
get_kv_params()β
get_kv_params()
Returns the KV cache parameters from the embedded text config.
-
Return type:
get_max_seq_len()β
get_max_seq_len()
Returns the maximum sequence length from the embedded text config.
-
Return type:
get_num_layers()β
static get_num_layers(huggingface_config)
Get number of layers in the language model.
-
Parameters:
-
huggingface_config (AutoConfig)
-
Return type:
image_seq_lenβ
property image_seq_len: int
Calculate the number of image tokens after connector processing.
image_token_idβ
image_token_id: int
Token ID used to represent image tokens in the text sequence.
initialize()β
classmethod initialize(pipeline_config, model_config=None)
Initializes an Idefics3Config instance from pipeline configuration.
-
Parameters:
-
- pipeline_config (PipelineConfig) β The MAX Engine pipeline configuration.
- model_config (MAXModelConfig | None)
-
Returns:
-
An Idefics3Config instance with fields initialized from config.
-
Return type:
scale_factorβ
scale_factor: int
Scale factor for pixel shuffle operation in the connector.
text_configβ
text_config: Llama3Config
Text model configuration (Llama3-based).
vision_configβ
vision_config: Idefics3VisionConfig
Vision encoder configuration (SigLIP-based).
Idefics3Inputsβ
class max.pipelines.architectures.idefics3.Idefics3Inputs(tokens, input_row_offsets, return_n_logits, pixel_values=None, image_token_indices=None, *, kv_cache_inputs=None, lora_ids=None, lora_ranks=None, hidden_states=None)
Bases: ModelInputs
A class representing inputs for the Idefics3 model.
-
Parameters:
has_vision_inputsβ
property has_vision_inputs: bool
Check if this input contains vision data.
image_token_indicesβ
Pre-computed indices of image tokens in the input sequence.
input_row_offsetsβ
input_row_offsets: Buffer
Tensor containing the offsets for each row in the ragged input sequence.
pixel_valuesβ
Pixel values for vision inputs.
return_n_logitsβ
return_n_logits: Buffer
Number of logits to return, used by speculative decoding for example.
tokensβ
tokens: Buffer
Tensor containing the input token IDs.
Idefics3Modelβ
class max.pipelines.architectures.idefics3.Idefics3Model(pipeline_config, session, devices, kv_cache_config, weights, adapter=None, return_logits=ReturnLogits.LAST_TOKEN)
Bases: PipelineModelWithKVCache[TextAndVisionContext]
An Idefics3 pipeline model for multimodal text generation.
-
Parameters:
-
- pipeline_config (PipelineConfig)
- session (InferenceSession)
- devices (list[Device])
- kv_cache_config (KVCacheConfig)
- weights (Weights)
- adapter (WeightsAdapter | None)
- return_logits (ReturnLogits)
calculate_max_seq_len()β
static calculate_max_seq_len(pipeline_config, huggingface_config)
Calculates the maximum sequence length for the Idefics3 model.
-
Parameters:
-
- pipeline_config (PipelineConfig)
- huggingface_config (AutoConfig)
-
Return type:
execute()β
execute(model_inputs)
Executes the Idefics3 model with the prepared inputs.
-
Parameters:
-
model_inputs (ModelInputs)
-
Return type:
get_kv_params()β
classmethod get_kv_params(huggingface_config, pipeline_config, devices, kv_cache_config, cache_dtype)
Gets the parameters required to configure the KV cache for Idefics3.
-
Parameters:
-
- huggingface_config (AutoConfig)
- pipeline_config (PipelineConfig)
- devices (list[DeviceRef])
- kv_cache_config (KVCacheConfig)
- cache_dtype (DType)
-
Return type:
language_modelβ
language_model: Model
The compiled language model for text generation.
load_model()β
load_model(session)
Loads the compiled Idefics3 models into the MAX Engine session.
-
Returns:
-
A tuple of (vision_model, language_model).
-
Parameters:
-
session (InferenceSession)
-
Return type:
prepare_initial_token_inputs()β
prepare_initial_token_inputs(replica_batches, kv_cache_inputs=None, return_n_logits=1)
Prepares the initial inputs for the first execution pass of the Idefics3 model.
-
Parameters:
-
- replica_batches (Sequence[Sequence[TextAndVisionContext]])
- kv_cache_inputs (KVCacheInputs[Buffer, Buffer] | None)
- return_n_logits (int)
-
Return type:
prepare_next_token_inputs()β
prepare_next_token_inputs(next_tokens, prev_model_inputs)
Prepares the secondary inputs to be passed to execute().
While prepare_initial_token_inputs is responsible for managing the initial inputs.
This function is responsible for updating the inputs, for each step in a multi-step execution pattern.
-
Parameters:
-
- next_tokens (Buffer)
- prev_model_inputs (ModelInputs)
-
Return type:
vision_modelβ
vision_model: Model
The compiled vision model for processing images.
Idefics3VisionConfigβ
class max.pipelines.architectures.idefics3.Idefics3VisionConfig(dtype, hidden_size, intermediate_size, image_size, patch_size, num_channels, num_attention_heads, head_dim, layer_norm_eps, hidden_act, num_hidden_layers, initializer_range, scale_factor, text_config_hidden_size)
Bases: object
Configuration for Idefics3 Vision Model (SigLIP-based).
-
Parameters:
dtypeβ
dtype: DType
DType of the Idefics3 vision model weights.
head_dimβ
head_dim: int
Dimension of each attention head.
hidden_actβ
hidden_act: str
Activation function used in the vision encoder.
hidden_sizeβ
hidden_size: int
Hidden size of the vision encoder.
image_sizeβ
image_size: int
Input image size.
initialize_from_config()β
classmethod initialize_from_config(pipeline_config, huggingface_config, text_config_hidden_size)
Initialize Idefics3VisionConfig from HuggingFace config.
-
Parameters:
-
- pipeline_config (PipelineConfig)
- huggingface_config (AutoConfig)
- text_config_hidden_size (int)
-
Return type:
initializer_rangeβ
initializer_range: float
Standard deviation for weight initialization.
intermediate_sizeβ
intermediate_size: int
Intermediate size in the vision encoderβs feed-forward layers.
layer_norm_epsβ
layer_norm_eps: float
Epsilon for layer normalization.
num_attention_headsβ
num_attention_heads: int
Number of attention heads in the vision encoder.
num_channelsβ
num_channels: int
Number of input channels (typically 3 for RGB).
num_hidden_layersβ
num_hidden_layers: int
Number of hidden layers in the vision encoder.
patch_sizeβ
patch_size: int
Vision transformer patch size.
scale_factorβ
scale_factor: int
Scale factor for pixel shuffle operation in the connector.
text_config_hidden_sizeβ
text_config_hidden_size: int
Hidden size from the text config for modality projection.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!