fix(recall): 选择窗口与请求 topK 解耦(常数 100),修 top5 ⊄ top20 不变量违背
病根:selectionWindow 默认 topK*2 ⇒ k=5 窗口 10、k=20 窗口 40,同一 query 看到不同候选池。实测 `日志分析怎么定位根因` k=20 时 DA 排第 4、k=5 时完全不在。 (11-16 只把它当'提分实验'看,因 +0.8pp 属噪声内而回退 —— 漏掉了这是正确性问题。) 改:默认 max(100, topK),与 k 解耦;旋钮 0 = 旧行为供回滚。 实测:16 题 top5 ⊄ top20 = 0 道;主尺 0.55754 → 0.56548(+0.79pp); 窗 40 同分(池深饱和);延迟 p50 1.01→1.09s。
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package storage
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 2026-11-16 ftsReserveSlots:让位预算随 topK 缩放。
|
||||
// 判据(无覆盖文件时):topK/2 封顶 3 —— k=5 ⇒ 2、k=6 ⇒ 3、k≥6 恒 3(= 旧值)。
|
||||
@@ -25,17 +29,68 @@ func TestFtsReserveSlots_NeverEatsWholeList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 2026-11-16 selectionWindow:默认必须保持旧行为(topK*2)。
|
||||
// 宽窗口(max(topK*2,20) / 常数 100)实测不提分(0.5575→0.5655,涨1跌1 = 噪声内),
|
||||
// 故默认回落到 topK*2;宽窗口只作为热调实验开关(/var/lib/memoryweave/recall-sel-window)。
|
||||
func TestSelectionWindow_DefaultsToLegacy(t *testing.T) {
|
||||
// 2026-11-17 selectionWindow 默认改为**常数**(与 topK 解耦)。
|
||||
//
|
||||
// 理由不是「提分」(+0.79pp 属噪声内),而是**不变量**:改前 window(5)=10、window(20)=40,
|
||||
// 同一 query 请求 5 条与 20 条看到不同候选池 ⇒ 实测 `日志分析怎么定位根因` 在 k=20 时
|
||||
// DA 排第 4、k=5 时完全不在(top5 ⊄ top20)。用户换个 top_k 就拿到不同答案。
|
||||
// 改后实测 16 题 top5 ⊄ top20 = 0 道。
|
||||
func TestSelectionWindow_DecoupledFromTopK(t *testing.T) {
|
||||
t.Setenv("ZHIYI_SEL_WINDOW_FILE", "/nonexistent/recall-sel-window")
|
||||
cases := []struct{ topK, want int }{
|
||||
{1, 2}, {5, 10}, {10, 20}, {20, 40}, {50, 100},
|
||||
{1, selectionWindowDefault}, {5, selectionWindowDefault},
|
||||
{10, selectionWindowDefault}, {20, selectionWindowDefault},
|
||||
{50, selectionWindowDefault}, {100, selectionWindowDefault},
|
||||
{150, 150}, // 窗口不能小于请求条数
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := selectionWindow(c.topK); got != c.want {
|
||||
t.Errorf("selectionWindow(%d)=%d want %d(默认必须等于旧行为 topK*2)", c.topK, got, c.want)
|
||||
t.Errorf("selectionWindow(%d)=%d want %d", c.topK, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 单调性 + 恒定:k ≤ 默认值时窗口恒定(同一候选池 ⇒ 前缀结果一致)。
|
||||
func TestSelectionWindow_Monotonic(t *testing.T) {
|
||||
t.Setenv("ZHIYI_SEL_WINDOW_FILE", "/nonexistent/recall-sel-window")
|
||||
prev := 0
|
||||
for k := 1; k <= 200; k++ {
|
||||
w := selectionWindow(k)
|
||||
if w < prev {
|
||||
t.Fatalf("selectionWindow 非单调:k=%d got %d < 前一个 %d", k, w, prev)
|
||||
}
|
||||
if k <= selectionWindowDefault && w != selectionWindowDefault {
|
||||
t.Fatalf("k=%d ≤ 默认值时窗口应恒定 %d,实际 %d", k, selectionWindowDefault, w)
|
||||
}
|
||||
prev = w
|
||||
}
|
||||
}
|
||||
|
||||
// 旋钮 0 = 回滚到旧行为(topK*2)。
|
||||
func TestSelectionWindow_LegacyViaKnobZero(t *testing.T) {
|
||||
f := filepath.Join(t.TempDir(), "recall-sel-window")
|
||||
if err := os.WriteFile(f, []byte("0"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("ZHIYI_SEL_WINDOW_FILE", f)
|
||||
for _, c := range []struct{ topK, want int }{{1, 2}, {5, 10}, {20, 40}} {
|
||||
if got := selectionWindow(c.topK); got != c.want {
|
||||
t.Errorf("旋钮=0 时 selectionWindow(%d)=%d want %d(旧行为 topK*2)", c.topK, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 旋钮 n>0 = 窗口取 max(n, topK)。
|
||||
func TestSelectionWindow_KnobWidens(t *testing.T) {
|
||||
f := filepath.Join(t.TempDir(), "recall-sel-window")
|
||||
if err := os.WriteFile(f, []byte("40"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("ZHIYI_SEL_WINDOW_FILE", f)
|
||||
if got := selectionWindow(5); got != 40 {
|
||||
t.Errorf("旋钮=40 时 selectionWindow(5)=%d want 40", got)
|
||||
}
|
||||
if got := selectionWindow(60); got != 60 {
|
||||
t.Errorf("旋钮=40 时 selectionWindow(60)=%d want 60(不得小于请求条数)", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1896,15 +1896,24 @@ func ftsReserveSlots(topK int) int {
|
||||
return n
|
||||
}
|
||||
|
||||
// selectionWindowDefault 是与请求 topK 解耦的窗口常数。窗 40 与窗 100 实测同分,
|
||||
// 取 100 只为把「大 k 包含小 k」的不变量覆盖到 k ≤ 100 全区间。
|
||||
const selectionWindowDefault = 100
|
||||
|
||||
// selectionWindow 返回 MMR/RRF 的「选择窗口」宽度 —— 与请求的 topK 解耦。
|
||||
//
|
||||
// 2026-11-16(t_sel_window):原实现在两处用 topK*2 作为窗口,于是**同一 query
|
||||
// 请求 5 条与请求 50 条拿到的前 5 名不同**(实测 29 道零分题里有 6~8 道在
|
||||
// top_k=50 请求时 DA 已进前 5,只是 tk=5 请求拿不到)。窗口窄 ⇒ 只在前 10 条里
|
||||
// 做 MMR/RRF ⇒ 第 11 条起的好答案结构性没机会。
|
||||
// 规则:窗口 = max(topK*2, 20)。贪心 MMR 前缀稳定 ⇒ 前 5 名在 5/10/20/50 各档一致。
|
||||
// 热调:/var/lib/memoryweave/recall-sel-window 写整数 —— 0 = 旧行为(topK*2),
|
||||
// n>0 = 窗口取 max(n, topK);文件缺失 ⇒ 新默认。回滚 = echo 0 > 该文件。
|
||||
// 请求 5 条与请求 50 条拿到的前 5 名不同**。
|
||||
//
|
||||
// 2026-11-17 改默认(**为修不变量,不为提分**):默认窗口 = 常数 100。
|
||||
// 病根证据:`日志分析怎么定位根因` 在 top_k=20 时 DA 排第 4,top_k=5 时完全不在结果里
|
||||
// ⇒ top5 ⊄ top20,用户换个 top_k 就拿到不同答案。机制就是窗口随 k 缩放:
|
||||
// k=5 只在前 10 条里做 MMR/RRF,第 11 条起的好答案结构性没机会。
|
||||
// 改后实测:16 题 top5 ⊄ top20 = 0 道(不变量成立);主尺 0.55754 → 0.56548(+0.79pp);
|
||||
// 窗 40 与窗 100 同分(池深饱和)⇒ 选 100 是为了把不变量覆盖到 k ≤ 100 全区间;
|
||||
// 延迟 p50 1.01s → 1.09s(+80ms)。
|
||||
// 热调:/var/lib/memoryweave/recall-sel-window 写整数 —— 0 = 旧行为(topK*2,回滚用),
|
||||
// n>0 = 窗口取 max(n, topK)。文件缺失 ⇒ 常数默认。
|
||||
func selectionWindow(topK int) int {
|
||||
if b, err := os.ReadFile(recallKnobPath("recall-sel-window", "ZHIYI_SEL_WINDOW_FILE")); err == nil {
|
||||
if v, err := strconv.Atoi(strings.TrimSpace(string(b))); err == nil && v >= 0 {
|
||||
@@ -1917,11 +1926,11 @@ func selectionWindow(topK int) int {
|
||||
return topK
|
||||
}
|
||||
}
|
||||
// 默认 = 旧行为(topK*2)。宽窗口(max(topK*2,20) / 常数 100)实测**不提分**:
|
||||
// 主尺 0.5575(旧) → 0.5655(窗20) → 0.5655(窗100),涨 1 跌 1 = 噪声内;
|
||||
// 一致率虽 19%→27%→45%,但那是 RRF 池宽带来的词面名次上位,不构成收益。
|
||||
// 想复跑实验:echo 20(或 100)> /var/lib/memoryweave/recall-sel-window。
|
||||
return topK * 2
|
||||
// 窗口不能小于请求条数,否则凑不满 k。
|
||||
if topK > selectionWindowDefault {
|
||||
return topK
|
||||
}
|
||||
return selectionWindowDefault
|
||||
}
|
||||
|
||||
// recallKnobPath 返回热调旋钮文件路径。
|
||||
|
||||
Reference in New Issue
Block a user