11import { isDeepStrictEqual } from 'node:util' ;
22
33import { confirm , hasNoSslFlag , hasYesFlag } from './cli-utils.js' ;
4- import { configCacheKey } from './etl/config-cache.js' ;
4+ import { bulkUpsertAvailability } from './etl/benchmark-ingest.js' ;
5+ import { configCacheKey , createConfigCache } from './etl/config-cache.js' ;
56import { type Sql , createAdminSql } from './etl/db-utils.js' ;
67import { jsonbParam } from './lib/backfill-runner.js' ;
78import { planBenchmarkPointBackfill } from './lib/benchmark-point-backfill.js' ;
@@ -10,6 +11,7 @@ import {
1011 type BenchmarkPointBackfill ,
1112 type ChangelogBackfill ,
1213 type PurgedBenchmarkPoint ,
14+ backfillDesiredConfig ,
1315 validateRunBackfills ,
1416} from './etl/run-overrides.js' ;
1517
@@ -168,6 +170,8 @@ interface BenchmarkPointBackfillTarget {
168170 backfill : BenchmarkPointBackfill ;
169171 resultId : number ;
170172 metrics : Record < string , unknown > ;
173+ sourceSpecMethod : string ;
174+ date : string ;
171175}
172176
173177function benchmarkPointDescription ( backfill : BenchmarkPointBackfill ) : string {
@@ -192,8 +196,12 @@ async function previewBenchmarkPointBackfill(
192196 const sourceRecipeFingerprint = backfill . recipeFingerprint ?? null ;
193197 const desiredRecipeFingerprint = backfill . set . recipeFingerprint ?? sourceRecipeFingerprint ;
194198 const { config } = backfill ;
199+ const specMethods = [
200+ ...new Set ( [ config . specMethod , backfill . set . specMethod ?? config . specMethod ] ) ,
201+ ] ;
195202 const rows = await sql `
196- SELECT br.id, br.offload_mode, br.recipe_fingerprint, br.metrics
203+ SELECT br.id, br.offload_mode, br.recipe_fingerprint, br.metrics, br.date::text AS date,
204+ c.spec_method
197205 FROM benchmark_results br
198206 JOIN workflow_runs wr ON wr.id = br.workflow_run_id
199207 JOIN configs c ON c.id = br.config_id
@@ -203,7 +211,7 @@ async function previewBenchmarkPointBackfill(
203211 AND c.framework = ${ config . framework }
204212 AND c.model = ${ config . model }
205213 AND c.precision = ${ config . precision }
206- AND c.spec_method = ${ config . specMethod }
214+ AND c.spec_method = ANY( ${ specMethods } )
207215 AND c.disagg = ${ config . disagg }
208216 AND c.is_multinode = ${ config . isMultinode }
209217 AND c.prefill_tp = ${ config . prefillTp }
@@ -239,6 +247,7 @@ async function previewBenchmarkPointBackfill(
239247 {
240248 offload_mode : row . offload_mode ,
241249 recipe_fingerprint : row . recipe_fingerprint ,
250+ spec_method : row . spec_method ,
242251 metrics : row . metrics ,
243252 } ,
244253 backfill ,
@@ -256,25 +265,55 @@ async function previewBenchmarkPointBackfill(
256265 backfill,
257266 resultId : row . id as number ,
258267 metrics,
268+ sourceSpecMethod : row . spec_method as string ,
269+ date : row . date as string ,
259270 } ;
260271}
261272
262273async function applyBenchmarkPointBackfillToDatabase (
263274 target : BenchmarkPointBackfillTarget ,
264275) : Promise < void > {
265- const { backfill, resultId, metrics } = target ;
276+ const { backfill, resultId, metrics, sourceSpecMethod , date } = target ;
266277 const desiredOffloadMode = backfill . set . offloadMode ?? backfill . offloadMode ;
267278 const desiredRecipeFingerprint =
268279 backfill . set . recipeFingerprint ?? backfill . recipeFingerprint ?? null ;
269- const [ updated ] = await sql `
270- UPDATE benchmark_results
271- SET offload_mode = ${ desiredOffloadMode } ,
272- recipe_fingerprint = ${ desiredRecipeFingerprint } ,
273- metrics = ${ jsonbParam ( sql , metrics ) }
274- WHERE id = ${ resultId }
275- RETURNING id
276- ` ;
277- if ( ! updated ) throw new Error ( `${ backfill . id } : benchmark row disappeared before update` ) ;
280+ const desiredConfig = backfillDesiredConfig ( backfill ) ;
281+ await sql . begin ( async ( _tx ) => {
282+ const tx = _tx as unknown as Sql ;
283+ const configId = await createConfigCache ( tx ) . getOrCreateConfig ( desiredConfig ) ;
284+ const [ updated ] = await tx `
285+ UPDATE benchmark_results
286+ SET config_id = ${ configId } ,
287+ offload_mode = ${ desiredOffloadMode } ,
288+ recipe_fingerprint = ${ desiredRecipeFingerprint } ,
289+ metrics = ${ jsonbParam ( tx , metrics ) }
290+ WHERE id = ${ resultId }
291+ RETURNING id
292+ ` ;
293+ if ( ! updated ) throw new Error ( `${ backfill . id } : benchmark row disappeared before update` ) ;
294+ if ( sourceSpecMethod === desiredConfig . specMethod ) return ;
295+ const availability = {
296+ model : desiredConfig . model ,
297+ isl : backfill . isl ,
298+ osl : backfill . osl ,
299+ precision : desiredConfig . precision ,
300+ hardware : desiredConfig . hardware ,
301+ framework : desiredConfig . framework ,
302+ disagg : desiredConfig . disagg ,
303+ } ;
304+ await bulkUpsertAvailability (
305+ tx ,
306+ [
307+ {
308+ ...availability ,
309+ specMethod : desiredConfig . specMethod ,
310+ benchmarkType : backfill . benchmarkType ,
311+ } ,
312+ ] ,
313+ date ,
314+ ) ;
315+ await deleteStaleAvailability ( tx , [ { ...availability , spec_method : sourceSpecMethod , date } ] ) ;
316+ } ) ;
278317 console . log ( ` ${ backfill . id } patched.` ) ;
279318}
280319
@@ -401,6 +440,13 @@ async function purgeBenchmarkResults(tx: Sql, resultIds: number[]): Promise<void
401440 ` ;
402441 }
403442
443+ await deleteStaleAvailability ( tx , availKeys ) ;
444+ }
445+
446+ async function deleteStaleAvailability (
447+ tx : Sql ,
448+ availKeys : readonly Record < string , unknown > [ ] ,
449+ ) : Promise < void > {
404450 if ( availKeys . length > 0 ) {
405451 const models = availKeys . map ( ( r ) => r . model as string ) ;
406452 const isls = availKeys . map ( ( r ) => r . isl as number | null ) ;
0 commit comments