-
Notifications
You must be signed in to change notification settings - Fork 180
feat: Support generic categories and MMLR-Pro mapping #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Xunzhuo
merged 2 commits into
vllm-project:main
from
tao12345666333:feat-mmlu-pro-mapping
Sep 23, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Example: Using generic categories with MMLU-Pro mapping | ||
# This file demonstrates how to declare free-style categories and map them to | ||
# MMLU-Pro categories expected by the classifier model. | ||
|
||
bert_model: | ||
model_id: sentence-transformers/all-MiniLM-L12-v2 | ||
threshold: 0.6 | ||
use_cpu: true | ||
|
||
classifier: | ||
category_model: | ||
model_id: "models/category_classifier_modernbert-base_model" | ||
use_modernbert: true | ||
threshold: 0.6 | ||
use_cpu: true | ||
category_mapping_path: "models/category_classifier_modernbert-base_model/category_mapping.json" | ||
|
||
# Define your generic categories and map them to MMLU-Pro categories. | ||
# The classifier will translate predicted MMLU categories into these generic names. | ||
categories: | ||
- name: tech | ||
mmlu_categories: ["computer science", "engineering"] | ||
model_scores: | ||
- model: phi4 | ||
score: 0.9 | ||
- model: mistral-small3.1 | ||
score: 0.7 | ||
- name: finance | ||
mmlu_categories: ["economics"] | ||
model_scores: | ||
- model: gemma3:27b | ||
score: 0.8 | ||
- name: politics | ||
# If omitted, identity mapping applies when this name matches MMLU | ||
model_scores: | ||
- model: gemma3:27b | ||
score: 0.6 | ||
|
||
# A default model is recommended for fallback | ||
default_model: mistral-small3.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/vllm-project/semantic-router/src/semantic-router/pkg/config" | ||
) | ||
|
||
var _ = Describe("MMLU categories in config YAML", func() { | ||
It("should unmarshal mmlu_categories into Category struct", func() { | ||
yamlContent := ` | ||
categories: | ||
- name: "tech" | ||
mmlu_categories: ["computer science", "engineering"] | ||
model_scores: | ||
- model: "phi4" | ||
score: 0.9 | ||
use_reasoning: false | ||
- name: "finance" | ||
mmlu_categories: ["economics"] | ||
model_scores: | ||
- model: "gemma3:27b" | ||
score: 0.8 | ||
use_reasoning: true | ||
- name: "politics" | ||
model_scores: | ||
- model: "gemma3:27b" | ||
score: 0.6 | ||
use_reasoning: false | ||
` | ||
|
||
var cfg config.RouterConfig | ||
Expect(yaml.Unmarshal([]byte(yamlContent), &cfg)).To(Succeed()) | ||
|
||
Expect(cfg.Categories).To(HaveLen(3)) | ||
|
||
Expect(cfg.Categories[0].Name).To(Equal("tech")) | ||
Expect(cfg.Categories[0].MMLUCategories).To(ConsistOf("computer science", "engineering")) | ||
Expect(cfg.Categories[0].ModelScores).ToNot(BeEmpty()) | ||
|
||
Expect(cfg.Categories[1].Name).To(Equal("finance")) | ||
Expect(cfg.Categories[1].MMLUCategories).To(ConsistOf("economics")) | ||
|
||
Expect(cfg.Categories[2].Name).To(Equal("politics")) | ||
Expect(cfg.Categories[2].MMLUCategories).To(BeEmpty()) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/semantic-router/pkg/utils/classification/generic_category_mapping_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package classification | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
candle_binding "github.com/vllm-project/semantic-router/candle-binding" | ||
"github.com/vllm-project/semantic-router/src/semantic-router/pkg/config" | ||
) | ||
|
||
var _ = Describe("generic category mapping (MMLU-Pro -> generic)", func() { | ||
var ( | ||
classifier *Classifier | ||
mockCategoryInitializer *MockCategoryInitializer | ||
mockCategoryModel *MockCategoryInference | ||
) | ||
|
||
BeforeEach(func() { | ||
mockCategoryInitializer = &MockCategoryInitializer{InitError: nil} | ||
mockCategoryModel = &MockCategoryInference{} | ||
|
||
cfg := &config.RouterConfig{} | ||
cfg.Classifier.CategoryModel.ModelID = "model-id" | ||
cfg.Classifier.CategoryModel.CategoryMappingPath = "category-mapping-path" | ||
cfg.Classifier.CategoryModel.Threshold = 0.5 | ||
|
||
// Define generic categories with MMLU-Pro mappings | ||
cfg.Categories = []config.Category{ | ||
{ | ||
Name: "tech", | ||
MMLUCategories: []string{"computer science", "engineering"}, | ||
ModelScores: []config.ModelScore{{Model: "phi4", Score: 0.9, UseReasoning: config.BoolPtr(false)}}, | ||
ReasoningEffort: "low", | ||
}, | ||
{ | ||
Name: "finance", | ||
MMLUCategories: []string{"economics"}, | ||
ModelScores: []config.ModelScore{{Model: "gemma3:27b", Score: 0.8, UseReasoning: config.BoolPtr(true)}}, | ||
}, | ||
{ | ||
Name: "politics", | ||
// No explicit mmlu_categories -> identity fallback when label exists in mapping | ||
ModelScores: []config.ModelScore{{Model: "gemma3:27b", Score: 0.6, UseReasoning: config.BoolPtr(false)}}, | ||
}, | ||
} | ||
|
||
// Category mapping represents labels coming from the MMLU-Pro model | ||
categoryMapping := &CategoryMapping{ | ||
CategoryToIdx: map[string]int{ | ||
"computer science": 0, | ||
"economics": 1, | ||
"politics": 2, | ||
}, | ||
IdxToCategory: map[string]string{ | ||
"0": "Computer Science", // different case to assert case-insensitive mapping | ||
"1": "economics", | ||
"2": "politics", | ||
}, | ||
} | ||
|
||
var err error | ||
classifier, err = newClassifierWithOptions( | ||
cfg, | ||
withCategory(categoryMapping, mockCategoryInitializer, mockCategoryModel), | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
It("builds expected MMLU<->generic maps", func() { | ||
Expect(classifier.MMLUToGeneric).To(HaveKeyWithValue("computer science", "tech")) | ||
Expect(classifier.MMLUToGeneric).To(HaveKeyWithValue("engineering", "tech")) | ||
Expect(classifier.MMLUToGeneric).To(HaveKeyWithValue("economics", "finance")) | ||
// identity fallback for a generic name that exists as an MMLU label | ||
Expect(classifier.MMLUToGeneric).To(HaveKeyWithValue("politics", "politics")) | ||
|
||
Expect(classifier.GenericToMMLU).To(HaveKey("tech")) | ||
Expect(classifier.GenericToMMLU["tech"]).To(ConsistOf("computer science", "engineering")) | ||
Expect(classifier.GenericToMMLU).To(HaveKeyWithValue("finance", ConsistOf("economics"))) | ||
Expect(classifier.GenericToMMLU).To(HaveKeyWithValue("politics", ConsistOf("politics"))) | ||
}) | ||
|
||
It("translates ClassifyCategory result to generic category", func() { | ||
// Model returns class index 0 -> "Computer Science" (MMLU) which maps to generic "tech" | ||
mockCategoryModel.classifyResult = candle_binding.ClassResult{Class: 0, Confidence: 0.92} | ||
|
||
category, score, err := classifier.ClassifyCategory("This text is about GPUs and compilers") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(category).To(Equal("tech")) | ||
Expect(score).To(BeNumerically("~", 0.92, 0.001)) | ||
}) | ||
|
||
It("translates names in entropy flow and returns generic top category", func() { | ||
// Probabilities favor index 0 -> generic should be "tech" | ||
mockCategoryModel.classifyWithProbsResult = candle_binding.ClassResultWithProbs{ | ||
Class: 0, | ||
Confidence: 0.88, | ||
Probabilities: []float32{0.7, 0.2, 0.1}, | ||
NumClasses: 3, | ||
} | ||
|
||
category, confidence, decision, err := classifier.ClassifyCategoryWithEntropy("Economic policies in computer science education") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(category).To(Equal("tech")) | ||
Expect(confidence).To(BeNumerically("~", 0.88, 0.001)) | ||
Expect(decision.TopCategories).ToNot(BeEmpty()) | ||
Expect(decision.TopCategories[0].Category).To(Equal("tech")) | ||
}) | ||
|
||
It("falls back to identity when no mapping exists for an MMLU label", func() { | ||
// index 2 -> "politics" (no explicit mapping provided, but present in MMLU set) | ||
mockCategoryModel.classifyResult = candle_binding.ClassResult{Class: 2, Confidence: 0.91} | ||
|
||
category, score, err := classifier.ClassifyCategory("This is a political debate") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(category).To(Equal("politics")) | ||
Expect(score).To(BeNumerically("~", 0.91, 0.001)) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.