feat(ml): fuse two embedding models and protect the ML service with a token
This commit is contained in:
1 parent
bc00d6901f
commit
b24e5ff906
63 files changed
+2287
-276
No files matched your search
@@ -117,3 +117,15 @@ func DemoProductPolicyV5() ProductPolicy {
|
||||
policy.Matching = &matching
|
||||
return policy
|
||||
}
|
||||
|
||||
// DemoProductPolicyV6 returns DemoProductPolicyV5 with the two-model
|
||||
// semantic section (DemoSemanticEnsembleMatchingPolicy): Sem fuses Giga and
|
||||
// Qwen3-Embedding. V5 stays the previous version with Giga alone. It is
|
||||
// published explicitly with the policy-publish command; startup seeding
|
||||
// publishes V1 only.
|
||||
func DemoProductPolicyV6() ProductPolicy {
|
||||
policy := DemoProductPolicyV5()
|
||||
matching := DemoSemanticEnsembleMatchingPolicy()
|
||||
policy.Matching = &matching
|
||||
return policy
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package domain
|
||||
|
||||
// Qwen3-Embedding-0.6B at the pinned revision with the service prompt recipe
|
||||
// (the model's own query prompt, last-token pooling); the ML service reports
|
||||
// the same identity.
|
||||
const (
|
||||
Qwen3EmbeddingModelID = "Qwen/Qwen3-Embedding-0.6B"
|
||||
Qwen3EmbeddingModelRevision = "97b0c614be4d77ee51c0cef4e5f07c00f9eb65b3"
|
||||
Qwen3EmbeddingPromptVersion = "qwen3-card-last-v1"
|
||||
)
|
||||
|
||||
// DemoMatchingSemanticsEnsemblePolicy returns the semantic section of the
|
||||
// two-model ensemble S5-DE of the LincodexMatch research: Sem is the
|
||||
// calibrated fused score 0.4 * z(Giga) + 0.6 * z(Qwen3-Embedding) of the
|
||||
// cosines of the need view and the card view.
|
||||
//
|
||||
// - Weights: the research step 5 choice, made by 5-fold CV over the DEV
|
||||
// needs on the research views (w_giga = 0.4 in four folds of five).
|
||||
// - Mean and standard deviation: the research profiles and needs rendered
|
||||
// with the product view builder (needs exactly as the research; a card
|
||||
// with the catalog roles and declared grades, the card stack codes, and
|
||||
// one published project per research experience entry), encoded by the
|
||||
// ML service in float16 on CUDA. The mean is over all DEV need-profile
|
||||
// pairs; the deviation is the pooled within-need deviation, so the fixed
|
||||
// z-score reproduces the per-need z-score of the research (DEV nDCG@10
|
||||
// 0.809 / indirect 0.518 against 0.809 / 0.518; Giga alone 0.738 / 0.403).
|
||||
// - Calibration: the isotonic fit of P(consensus >= 2) on the fused score
|
||||
// of the 3220 judged DEV pairs (994 positive), computed with the rounded
|
||||
// mean and deviation below; 5-fold grouped CV ECE 0.025.
|
||||
//
|
||||
// The explanation thresholds stay those of Giga, the first member, which
|
||||
// alone provides the item vectors. The values are a calibrated starting
|
||||
// point, not a product measurement: real cards carry fewer projects than the
|
||||
// research profiles carry experience entries.
|
||||
func DemoMatchingSemanticsEnsemblePolicy() MatchingSemanticsPolicy {
|
||||
policy := DemoMatchingSemanticsPolicy()
|
||||
policy.Members = []MatchingSemanticMember{
|
||||
{
|
||||
Model: SemanticModel{
|
||||
ID: GigaEmbeddingsModelID, Revision: GigaEmbeddingsModelRevision, PromptVersion: GigaEmbeddingsPromptVersion,
|
||||
},
|
||||
Weight: 4000, Mean: 2497, StandardDeviation: 741,
|
||||
},
|
||||
{
|
||||
Model: SemanticModel{
|
||||
ID: Qwen3EmbeddingModelID, Revision: Qwen3EmbeddingModelRevision, PromptVersion: Qwen3EmbeddingPromptVersion,
|
||||
},
|
||||
Weight: 6000, Mean: 3636, StandardDeviation: 731,
|
||||
},
|
||||
}
|
||||
policy.Calibration = demoEnsembleCalibration()
|
||||
policy.Source = "LincodexMatch DEV split 2026-10-11, product card views, giga+qwen3 float16; weights 0.4/0.6 S5-DE (reports/step5), mean/std and isotonic fit on 3220 judged pairs"
|
||||
return policy
|
||||
}
|
||||
|
||||
// DemoSemanticEnsembleMatchingPolicy returns DemoSemanticMatchingPolicy with
|
||||
// the ensemble semantic section; the Fit weights stay those of the single
|
||||
// model section.
|
||||
func DemoSemanticEnsembleMatchingPolicy() MatchingPolicy {
|
||||
policy := DemoSemanticMatchingPolicy()
|
||||
semantics := DemoMatchingSemanticsEnsemblePolicy()
|
||||
policy.Semantics = &semantics
|
||||
return policy
|
||||
}
|
||||
|
||||
// demoEnsembleCalibration is the isotonic fit of the fused score in basis
|
||||
// points (a fused score of 1.0 is 10000).
|
||||
func demoEnsembleCalibration() []MatchingCalibrationPoint {
|
||||
return []MatchingCalibrationPoint{
|
||||
{-22961, 0}, {610, 0}, {619, 101}, {3279, 101}, {3279, 263}, {5629, 263}, {5682, 267}, {7140, 267},
|
||||
{7155, 559}, {9628, 559}, {9636, 985}, {12855, 985}, {12869, 1379}, {14934, 1379}, {14954, 1690}, {16934, 1690},
|
||||
{16971, 1940}, {17749, 1940}, {17751, 2283}, {18641, 2283}, {18642, 2632}, {18955, 2632}, {18960, 2667}, {19764, 2667},
|
||||
{19767, 3105}, {21601, 3105}, {21614, 3436}, {23072, 3436}, {23075, 4062}, {23840, 4062}, {23843, 4286}, {23906, 4286},
|
||||
{23933, 4444}, {24162, 4444}, {24179, 5059}, {27070, 5059}, {27076, 5766}, {28697, 5766}, {28701, 6706}, {29902, 6706},
|
||||
{29916, 7059}, {30450, 7059}, {30460, 7500}, {33107, 7500}, {33109, 7692}, {33320, 7692}, {33354, 8182}, {33738, 8182},
|
||||
{33753, 8391}, {35898, 8391}, {35939, 9244}, {40768, 9244}, {40885, 9792}, {44357, 9792}, {44390, 10000}, {52584, 10000},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnsembleSemanticsPolicyIsValidAndKeepsTheGigaExplanation(t *testing.T) {
|
||||
ensemble := DemoMatchingSemanticsEnsemblePolicy()
|
||||
if err := ensemble.Validate(); err != nil {
|
||||
t.Fatalf("ensemble Validate() = %v", err)
|
||||
}
|
||||
single := DemoMatchingSemanticsPolicy()
|
||||
if ensemble.ExplanationModel() != single.ExplanationModel() || ensemble.ExplanationModel().ID != GigaEmbeddingsModelID {
|
||||
t.Fatalf("explanation model = %+v, want Giga", ensemble.ExplanationModel())
|
||||
}
|
||||
if ensemble.SkillMedium != single.SkillMedium || ensemble.SkillHigh != single.SkillHigh ||
|
||||
ensemble.TaskMedium != single.TaskMedium || ensemble.TaskHigh != single.TaskHigh {
|
||||
t.Fatal("the explanation thresholds must stay those of Giga")
|
||||
}
|
||||
if ensemble.Members[1].Model != (SemanticModel{ID: Qwen3EmbeddingModelID, Revision: Qwen3EmbeddingModelRevision, PromptVersion: Qwen3EmbeddingPromptVersion}) {
|
||||
t.Fatalf("second member = %+v, want the pinned Qwen3-Embedding", ensemble.Members[1].Model)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsembleSemanticsMapsTheFusedScoreMonotonically(t *testing.T) {
|
||||
ensemble := DemoMatchingSemanticsEnsemblePolicy()
|
||||
// At the member means every standardized cosine, and so the fused score, is 0.
|
||||
score, ok := ensemble.FusedScore([]float64{0.2497, 0.3636})
|
||||
if !ok || math.Abs(score) > 1e-12 {
|
||||
t.Fatalf("score at the means = %v, %v, want 0", score, ok)
|
||||
}
|
||||
if low, high := ensemble.Probability(-1), ensemble.Probability(10); low != 0 || high != 1_000_000 {
|
||||
t.Fatalf("clipped ends = %d and %d, want 0 and 1", low, high)
|
||||
}
|
||||
previous := int64(-1)
|
||||
for giga := 0.0; giga <= 0.8; giga += 0.02 {
|
||||
score, ok := ensemble.FusedScore([]float64{giga, giga + 0.1})
|
||||
if !ok {
|
||||
t.Fatal("the fused score needs both members")
|
||||
}
|
||||
value := ensemble.Probability(score)
|
||||
if value < previous {
|
||||
t.Fatalf("Sem decreased at cosine %v: %d after %d", giga, value, previous)
|
||||
}
|
||||
previous = value
|
||||
}
|
||||
}
|
||||
|
||||
func TestProductPolicyV6CarriesTheEnsembleAndRoundTrips(t *testing.T) {
|
||||
v6 := DemoProductPolicyV6()
|
||||
if err := v6.Validate(); err != nil {
|
||||
t.Fatalf("V6 Validate() = %v", err)
|
||||
}
|
||||
v5 := DemoProductPolicyV5()
|
||||
if v6.Matching.FitSemanticsWeight != v5.Matching.FitSemanticsWeight || len(v5.Matching.Semantics.Members) != 1 {
|
||||
t.Fatal("V6 must keep the Fit weights of V5, and V5 must stay the single-model version")
|
||||
}
|
||||
encoded, _, err := encodeProductPolicy(v6)
|
||||
if err != nil {
|
||||
t.Fatalf("encodeProductPolicy(V6) error = %v", err)
|
||||
}
|
||||
decoded, err := decodeProductPolicy(encoded)
|
||||
if err != nil {
|
||||
t.Fatalf("decodeProductPolicy(V6) error = %v", err)
|
||||
}
|
||||
if !slices.Equal(decoded.Matching.Semantics.Members, v6.Matching.Semantics.Members) ||
|
||||
!slices.Equal(decoded.Matching.Semantics.Calibration, v6.Matching.Semantics.Calibration) {
|
||||
t.Fatal("the ensemble section did not survive the document round trip")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user