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
25 changes: 19 additions & 6 deletions src/llm/servable_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,20 @@ Status parseModelsPath(std::string& outPath, std::string modelsPath, std::string
return StatusCode::LLM_NODE_PATH_DOES_NOT_EXIST_AND_NOT_GGUFFILE;
}

static std::optional<uint32_t> findMaxLengthField(const rapidjson::Value& config) {
if (!config.IsObject()) {
return std::nullopt;
}
static const std::vector<std::string> maxLengthFields = {"max_position_embeddings", "n_positions", "seq_len", "seq_length", "n_ctx", "sliding_window"};
for (const auto& field : maxLengthFields) {
auto it = config.FindMember(field.c_str());
if (it != config.MemberEnd() && it->value.IsUint()) {
return it->value.GetUint();
}
}
return std::nullopt;
}
Comment thread
Copilot marked this conversation as resolved.

std::optional<uint32_t> parseMaxModelLength(std::string& modelsPath) {
std::string configPath = FileSystem::appendSlash(modelsPath) + "config.json";
std::optional<uint32_t> maxModelLength;
Expand All @@ -546,12 +560,11 @@ std::optional<uint32_t> parseMaxModelLength(std::string& modelsPath) {
if (parseResult.Code()) {
return maxModelLength;
}
std::vector<std::string> maxLengthFields = {"max_position_embeddings", "n_positions", "seq_len", "seq_length", "n_ctx", "sliding_window"};
for (auto field : maxLengthFields) {
if (modelConfig.HasMember(field.c_str()) && modelConfig[field.c_str()].IsUint()) {
maxModelLength = modelConfig[field.c_str()].GetUint();
break;
}
maxModelLength = findMaxLengthField(modelConfig);
// Composite VLM/omni configs (e.g. Qwen3.5-Omni) nest the language model's
// parameters under "text_config" instead of the top level.
if (!maxModelLength.has_value() && modelConfig.HasMember("text_config") && modelConfig["text_config"].IsObject()) {
maxModelLength = findMaxLengthField(modelConfig["text_config"]);
}
}
return maxModelLength;
Expand Down
24 changes: 24 additions & 0 deletions src/test/llm/max_model_length_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,27 @@ TEST_F(MaxModelLengthTest, maxModelLength_parsingOrder) {
ASSERT_TRUE(maxModelLength.has_value());
EXPECT_EQ(maxModelLength.value(), 5);
}

TEST_F(MaxModelLengthTest, maxModelLength_textConfig_VALID) {
// Composite VLM/omni configs (e.g. Qwen3.5-Omni) nest the field under "text_config".
std::string modelConfigContent = R"({"model_type" : "qwen3_5", "text_config" : {"max_position_embeddings" : 262144}})";
createConfigFileWithContent(modelConfigContent, configFilePath);
auto maxModelLength = parseMaxModelLength(directoryPath);
ASSERT_TRUE(maxModelLength.has_value());
EXPECT_EQ(maxModelLength.value(), 262144);
}

TEST_F(MaxModelLengthTest, maxModelLength_topLevelTakesPriorityOverTextConfig) {
std::string modelConfigContent = R"({"max_position_embeddings" : 5, "text_config" : {"max_position_embeddings" : 262144}})";
createConfigFileWithContent(modelConfigContent, configFilePath);
auto maxModelLength = parseMaxModelLength(directoryPath);
ASSERT_TRUE(maxModelLength.has_value());
EXPECT_EQ(maxModelLength.value(), 5);
}

TEST_F(MaxModelLengthTest, maxModelLength_textConfig_notAnObject) {
std::string modelConfigContent = R"({"text_config" : "INVALID"})";
createConfigFileWithContent(modelConfigContent, configFilePath);
auto maxModelLength = parseMaxModelLength(directoryPath);
EXPECT_FALSE(maxModelLength.has_value());
}