Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,10 @@ struct Vision_Element : JSON::Element {
v_.config_filename = JSON::Get<std::string_view>(value);
} else if (name == "adapter_filename") {
v_.adapter_filename = JSON::Get<std::string_view>(value);
} else if (name == "spatial_merge_size") {
v_.spatial_merge_size = static_cast<int>(JSON::Get<double>(value));
} else if (name == "tokens_per_second") {
v_.tokens_per_second = static_cast<float>(JSON::Get<double>(value));
} else {
throw JSON::unknown_value_error{};
}
Expand Down Expand Up @@ -949,6 +953,12 @@ struct Model_Element : JSON::Element {
v_.decoder_start_token_id = static_cast<int>(JSON::Get<double>(value));
} else if (name == "sep_token_id") {
v_.sep_token_id = static_cast<int>(JSON::Get<double>(value));
} else if (name == "image_token_id") {
v_.image_token_id = static_cast<int>(JSON::Get<double>(value));
} else if (name == "video_token_id") {
v_.video_token_id = static_cast<int>(JSON::Get<double>(value));
} else if (name == "vision_start_token_id") {
v_.vision_start_token_id = static_cast<int>(JSON::Get<double>(value));
} else {
throw JSON::unknown_value_error{};
}
Expand Down
11 changes: 11 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct Config {
// Vision encoder names
static constexpr std::string_view PixelValuesName = "pixel_values";
static constexpr std::string_view ImageSizesName = "image_sizes";
static constexpr std::string_view ImageGridThwName = "image_grid_thw";
static constexpr std::string_view ImageAttentionMaskName = "image_attention_mask";
static constexpr std::string_view ImageFeaturesName = "image_features";
static constexpr std::string_view NumImageTokens = "num_image_tokens";
Expand Down Expand Up @@ -106,6 +107,12 @@ struct Config {
int bos_token_id{}; // The id of the beginning-of-stream token.
int sep_token_id{}; // The id of the separation token.
int decoder_start_token_id{}; // If an encoder-decoder model starts decoding with a different token than bos, the id of that token.

// Qwen2.5-VL specific token IDs
int image_token_id{};
int video_token_id{};
int vision_start_token_id{};

int vocab_size{};
int context_length{};

Expand Down Expand Up @@ -156,6 +163,10 @@ struct Config {
std::optional<SessionOptions> session_options;
std::optional<RunOptions> run_options;

// Qwen2.5-VL specific vision config values
int spatial_merge_size{2};
float tokens_per_second{2.0f};

std::string config_filename{"processor_config.json"};
std::optional<std::string> adapter_filename{};

Expand Down
7 changes: 4 additions & 3 deletions src/models/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,8 @@ std::shared_ptr<Model> CreateModel(OrtEnv& ort_env, const char* config_path, con
}

std::shared_ptr<Model> CreateModel(OrtEnv& ort_env, std::unique_ptr<Config> config) {
if (config->model.type == "fara" || config->model.type == "qwen2_5_vl")
// Check if it's a pipeline model by checking if decoder.pipeline is configured
if ((config->model.type == "fara" || config->model.type == "qwen2_5_vl") && !config->model.decoder.pipeline.empty())
Comment thread
apsonawane marked this conversation as resolved.
return std::make_shared<Qwen2_5_VL_PipelineModel>(std::move(config), ort_env);
if (config->model.type == "gpt2")
return std::make_shared<Gpt_Model>(std::move(config), ort_env);
Expand Down Expand Up @@ -1250,8 +1251,8 @@ MultiModalProcessor::MultiModalProcessor(Config& config, const SessionInfo& sess
{"whisper", Processor::Create<WhisperProcessor>},
{"phi4mm", Processor::Create<PhiMultiModalProcessor>},
{"gemma3", Processor::Create<GemmaImageProcessor>},
{"fara", Processor::Create<Qwen2_5VLImageProcessor>},
{"qwen2_5_vl", Processor::Create<Qwen2_5VLImageProcessor>}} {
{"fara", Processor::Create<QwenImageProcessor>},
{"qwen2_5_vl", Processor::Create<QwenImageProcessor>}} {
auto processor = processor_factory_.find(config.model.type);
if (processor != processor_factory_.end()) {
processor_ = processor->second(config, session_info);
Expand Down
5 changes: 5 additions & 0 deletions src/models/model_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ struct ModelType {
return std::find(VLM.begin(), VLM.end(), model_type) != VLM.end();
}

inline static bool IsQwen25VL(const std::string& model_type) {
// Qwen25-VL specific check for 3D position IDs
return model_type == "fara" || model_type == "qwen2_5_vl";
}

inline static bool IsALM(const std::string& model_type) {
// Audio-language model (ALM)
static constexpr std::array<std::string_view, 1> ALM = {"whisper"};
Expand Down
36 changes: 32 additions & 4 deletions src/models/multi_modal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../generators.h"
#include "multi_modal.h"
#include <numeric>

namespace Generators {

Expand Down Expand Up @@ -181,9 +182,9 @@ DeviceSpan<float> EmbeddingState::Run(int current_length, DeviceSpan<int32_t>& n
DecoderState::DecoderState(const MultiModalLanguageModel& model, DeviceSpan<int32_t> sequence_lengths, const GeneratorParams& params)
: State{params, model},
model_{model},
position_inputs_{model, *this, sequence_lengths, model_.config_->model.decoder.inputs.attention_mask} {
position_inputs_{CreatePositionInputs(*this, sequence_lengths, model_.config_->model.decoder.inputs.attention_mask)} {
inputs_embeds_.Add();
position_inputs_.Add();
position_inputs_->Add();
logits_.Add();
kv_cache_.Add();
}
Expand All @@ -201,7 +202,14 @@ DeviceSpan<float> DecoderState::Run(int current_length, DeviceSpan<int32_t>& nex
void DecoderState::UpdateInputsOutputs(DeviceSpan<int32_t>& next_tokens, int total_length, DeviceSpan<int32_t> beam_indices) {
int batch_size = static_cast<int>(inputs_embeds_.GetShape()[0]);
size_t new_length = next_tokens.size() / batch_size;
position_inputs_.Update(next_tokens, total_length, static_cast<int>(new_length));
position_inputs_->Update(next_tokens, total_length, static_cast<int>(new_length));
kv_cache_.Update(beam_indices, total_length);
logits_.Update(next_tokens, new_length);
inputs_embeds_.UpdateSequenceLength(new_length);
}

// Overload for pipeline to call
void DecoderState::UpdateInputsOutputs(DeviceSpan<int32_t>& next_tokens, int total_length, DeviceSpan<int32_t> beam_indices, size_t new_length) {
kv_cache_.Update(beam_indices, total_length);
logits_.Update(next_tokens, new_length);
inputs_embeds_.UpdateSequenceLength(new_length);
Expand Down Expand Up @@ -243,6 +251,24 @@ void MultiModalPipelineState::SetExtraInputs(const std::vector<ExtraInput>& extr
speech_state_->SetExtraInputs(extra_inputs, num_audio_tokens_);
}
embedding_state_->SetExtraInputs(num_images_, num_image_tokens_, num_audio_tokens_);
// Set the grid tensors for Qwen2-VL if present
if (auto* qwen_pos_inputs = dynamic_cast<Qwen2VLPositionInputs*>(decoder_state_->position_inputs_.get())) {
std::shared_ptr<Tensor> img_grid, vid_grid, sec_grid;

for (const auto& input : extra_inputs) {
if (input.name == Config::Defaults::ImageGridThwName) {
img_grid = input.tensor;
} else if (input.name == "video_grid_thw") {
vid_grid = input.tensor;
} else if (input.name == "second_per_grid_ts") {
sec_grid = input.tensor;
}
}

if (img_grid || vid_grid) {
qwen_pos_inputs->SetGridTensors(img_grid, vid_grid, sec_grid);
}
}
}

DeviceSpan<float> MultiModalPipelineState::Run(int current_length, DeviceSpan<int32_t>& next_tokens, DeviceSpan<int32_t> next_indices) {
Expand All @@ -266,7 +292,9 @@ DeviceSpan<float> MultiModalPipelineState::Run(int current_length, DeviceSpan<in
if (num_audio_tokens_ > 0 && speech_state_) {
speech_state_->Run(current_length, next_tokens, next_indices);
}
if (vision_state_) embedding_state_->image_features_->ReuseFeaturesBuffer(*vision_state_->image_features_);
if (vision_state_) {
embedding_state_->image_features_->ReuseFeaturesBuffer(*vision_state_->image_features_);
}
if (speech_state_) embedding_state_->audio_features_->ReuseFeaturesBuffer(*speech_state_->audio_features_);
embedding_state_->inputs_embeds_.ReuseEmbeddingsBuffer(decoder_state_->inputs_embeds_);
embedding_state_->Run(current_length, next_tokens, next_indices);
Expand Down
11 changes: 6 additions & 5 deletions src/models/multi_modal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct MultiModalLanguageModel : Model {
MultiModalLanguageModel(const MultiModalLanguageModel&) = delete;
MultiModalLanguageModel& operator=(const MultiModalLanguageModel&) = delete;

std::unique_ptr<State> CreateState(DeviceSpan<int32_t> sequence_lengths, const GeneratorParams& params) const;
std::unique_ptr<State> CreateState(DeviceSpan<int32_t> sequence_lengths, const GeneratorParams& params) const override;

std::unique_ptr<OrtSession> vision_session_; // pixel_values, [image_attention_mask], image_sizes -> image_features
std::unique_ptr<OrtSession> speech_session_; // audio_embeds, audio_sizes, audio_projection_mode -> audio_features
Expand Down Expand Up @@ -96,18 +96,19 @@ struct DecoderState : State {
DecoderState& operator=(const DecoderState&) = delete;

DeviceSpan<float> Run(int current_length, DeviceSpan<int32_t>& next_tokens, DeviceSpan<int32_t> next_indices) override;
void UpdateInputsOutputs(DeviceSpan<int32_t>& next_tokens, int current_length, DeviceSpan<int32_t> beam_indices);

private:
friend struct MultiModalPipelineState;

void UpdateInputsOutputs(DeviceSpan<int32_t>& next_tokens, int current_length, DeviceSpan<int32_t> beam_indices);
void UpdateInputsOutputs(DeviceSpan<int32_t>& next_tokens, int current_length, DeviceSpan<int32_t> beam_indices, size_t new_length);

const MultiModalLanguageModel& model_;
Embeddings inputs_embeds_{*this, Embeddings::Mode::Input, // Model input
model_.config_->model.decoder.inputs.embeddings};
DefaultPositionInputs position_inputs_; // Model input
DefaultKeyValueCache kv_cache_{*this}; // Model input
Logits logits_{*this}; // Model output
std::unique_ptr<PositionInputs> position_inputs_; // Model input
DefaultKeyValueCache kv_cache_{*this}; // Model input
Logits logits_{*this}; // Model output
};

struct MultiModalPipelineState : State {
Expand Down
Loading
Loading