Skip to content

Commit e5167c1

Browse files
feat: stream maia top-4 mid-depth refresh before multipv
1 parent f351e10 commit e5167c1

1 file changed

Lines changed: 74 additions & 1 deletion

File tree

src/lib/engine/stockfish.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type RootProbePlan = {
3939

4040
type RootMovePhase = 'multipv' | 'screen' | 'deep' | 'ground-truth'
4141
type CandidateSelectionSource = 'sf-top' | 'maia-95' | 'both'
42+
const MAX_MAIA_DEEPEN_MOVES = 4
43+
const MAIA_MID_DEPTH = 14
4244

4345
type AnalysisRunContext = {
4446
positionId: string
@@ -755,6 +757,77 @@ class Engine {
755757
runContext.kSf > 0 ? runContext.kSf : 4,
756758
),
757759
)
760+
const maiaTopMoves = runContext.maiaCandidateMoves.slice(
761+
0,
762+
MAX_MAIA_DEEPEN_MOVES,
763+
)
764+
const maiaMidDepth = Math.min(MAIA_MID_DEPTH, targetDepth)
765+
766+
if (maiaMidDepth > plan.screeningDepth) {
767+
for (const move of maiaTopMoves) {
768+
if (!this.isActiveRun(runContext.positionId)) {
769+
return
770+
}
771+
772+
if (!rootScores[move]) {
773+
continue
774+
}
775+
776+
markSelectionSource(move, 'maia-95')
777+
const current = rootScores[move]
778+
if (current && current.depth >= maiaMidDepth) {
779+
continue
780+
}
781+
782+
const t0 = Date.now()
783+
const deepProbe = await this.runRootSearchWithRetry(
784+
runContext,
785+
maiaMidDepth,
786+
move,
787+
)
788+
phaseTimesMs.deepening += Date.now() - t0
789+
if (!deepProbe) {
790+
continue
791+
}
792+
793+
rootScores[move] = {
794+
cp: deepProbe.cp,
795+
depth: Math.max(1, Math.min(maiaMidDepth, deepProbe.depth)),
796+
mateIn: deepProbe.mateIn,
797+
}
798+
movePhases[move] = 'deep'
799+
deepenedMoves.add(move)
800+
801+
const maiaMidEval = this.buildEvaluationFromRootScores(
802+
maiaMidDepth,
803+
rootScores,
804+
runContext.fen,
805+
)
806+
this.attachDiagnostics(maiaMidEval, {
807+
runContext,
808+
strategy: 'staged-root-probe',
809+
targetDepth,
810+
legalMoveCount: runContext.legalMoveCount,
811+
phaseTimesMs,
812+
rootScores,
813+
movePhases,
814+
moveSelectionSources,
815+
moveCounts: {
816+
screened: runContext.legalMoveCount,
817+
deepened: deepenedMoves.size,
818+
finalAtTargetDepth: Object.values(rootScores).filter(
819+
(score) => score.depth >= targetDepth,
820+
).length,
821+
},
822+
})
823+
this.maybePublishDiagnostics(
824+
maiaMidEval,
825+
false,
826+
runContext.diagnosticsToConsole,
827+
)
828+
yield maiaMidEval
829+
}
830+
}
758831

759832
for await (const multipvSnapshot of this.streamMultiPvSnapshots(
760833
runContext,
@@ -808,7 +881,7 @@ class Engine {
808881
yield streamingEval
809882
}
810883

811-
for (const move of runContext.maiaCandidateMoves) {
884+
for (const move of maiaTopMoves) {
812885
if (!this.isActiveRun(runContext.positionId)) {
813886
return
814887
}

0 commit comments

Comments
 (0)