8000 modeltests: add -warmup flag for steady-state inference benchmarking by janpfeifer · Pull Request #97 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Unreleased

- 2026/12/01:
- `modeltests`, `docs/ModelTests.md`:
- Added optional `-warmup` flag to run an untimed warmup pass before each timed inference execution (embeddings, prefill logits, teacher-forced KV generation, and time-series) and display a `Warmup` column alongside warmed-up `Infer` timings in the summary table for fair cross-backend comparisons (e.g., `xla:cuda` vs. `cuda`).
- `examples/dolly/browse`:
- Added `-quiet` flag to suppress per-example output and print only the final summary averages.

Expand Down
1 change: 1 addition & 0 deletions docs/ModelTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Set -modeltests to enable testing model tests, which are slow, and may require d
| `-strict` | `false` | Fail instead of skipping when a gated model lacks `HF_TOKEN` or a download fails (useful for nightly CI). |
| `-debug_layers` | `false` | If local layer-dump files exist in `testdata/<model>/debug_layers/`, run layer-by-layer activation checks. |
| `-set` | `""` | GoMLX store hyperparameter overrides (via `commandline.CreateSettingsFlag`, e.g., `-set="quantized_matmul=false"`). |
| `-warmup` | `false` | Run an untimed warmup execution pass before each timed execution (and show a `Warmup` column in the summary table) so compiled graphs and caches are warm when comparing steady-state timings across backends (e.g., `xla:cuda` vs. `cuda`). |

### 3.3 Common Invocation Examples

Expand Down
2 changes: 2 additions & 0 deletions modeltests/modeltests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ var (
"Run layer-by-layer activation checks if debug_layers/ files exist under the model's testdata directory.")
flagSettings = flag.String("set", "",
`Semicolon-separated GoMLX store hyperparameter overrides (e.g. "quantized_matmul=false").`)
flagWarmup = flag.Bool("warmup", false,
"Run an untimed warmup execution pass before each timed execution so compiled graphs and caches are warm.")
)

const skipModelTestsMsg = "Set -modeltests to enable testing model tests, which are slow, and may require downloading of large models to run."
Expand Down
47 changes: 36 additions & 11 deletions modeltests/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,31 +362,48 @@ func mergeModelOptions(base ModelOptions, override *ModelOptions) ModelOptions {

// runReportRow records timing and peak memory for one (model, variant, backend) execution.
type runReportRow struct {
Model string
Variant string
Backend string
Status string
LoadTime time.Duration
InferTime time.Duration
Mem MemoryStats
Notes string
Model string
Variant string
Backend string
Status string
LoadTime time.Duration
WarmupTime time.Duration
InferTime time.Duration
Mem MemoryStats
Notes string
}

func printSummaryTable(rows []runReportRow) {
if len(rows) == 0 {
return
}
showWarmup := *flagWarmup
for _, r := range rows {
if r.WarmupTime > 0 {
showWarmup = true
break
}
}
baseStyle := lipgloss.NewStyle().Padding(0, 1)
headers := []string{"Model", "Variant", "Backend", "Status", "Load"}
if showWarmup {
headers = append(headers, "Warmup")
}
headers = append(headers, "Infer", "Peak RSS", "Delta RSS", "Peak Heap", "Notes")
rightAlignEnd := 8
if showWarmup {
rightAlignEnd = 9
}
tbl := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("238"))).
Headers("Model", "Variant", "Backend", "Status", "Load", "Infer", "Peak RSS", "Delta RSS", "Peak Heap", "Notes").
Headers(headers...).
StyleFunc(func(row, col int) lipgloss.Style {
st := baseStyle
if row == table.HeaderRow {
return st.Foreground(lipgloss.Color("252")).Bold(true)
}
if col >= 4 && col <= 8 {
if col >= 4 && col <= rightAlignEnd {
st = st.Align(lipgloss.Right)
}
return st
Expand All @@ -396,6 +413,10 @@ func printSummaryTable(rows []runReportRow) {
if r.LoadTime > 0 {
loadStr = humanize.Duration(r.LoadTime)
}
warmupStr := "-"
if r.WarmupTime > 0 {
warmupStr = humanize.Duration(r.WarmupTime)
}
inferStr := "-"
if r.InferTime > 0 {
inferStr = humanize.Duration(r.InferTime)
Expand All @@ -412,7 +433,11 @@ func printSummaryTable(rows []runReportRow) {
if r.Mem.PeakHeapAllocBytes > 0 {
peakHeap = humanize.Bytes(r.Mem.PeakHeapAllocBytes)
}
tbl.Row(r.Model, r.Variant, r.Backend, r.Status, loadStr, inferStr, peakRSS, deltaRSS, peakHeap, r.Notes)
if showWarmup {
tbl.Row(r.Model, r.Variant, r.Backend, r.Status, loadStr, warmupStr, inferStr, peakRSS, deltaRSS, peakHeap, r.Notes)
} else {
tbl.Row(r.Model, r.Variant, r.Backend, r.Status, loadStr, inferStr, peakRSS, deltaRSS, peakHeap, r.Notes)
}
}
fmt.Printf("\n=== Model Tests Summary ===\n%s\n", tbl)
}
21 changes: 18 additions & 3 deletions modeltests/timeseries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func runTimeSeriesModel(t *testing.T, loaded loadedSpec, backendConfigs []string
}

store := mlmodel.NewStore().WithVariableAsConst(true)
var warmupTime time.Duration
var inferTime time.Duration
var notes string

Expand All @@ -119,13 +120,20 @@ func runTimeSeriesModel(t *testing.T, loaded loadedSpec, backendConfigs []string
mem := memTracker.Stop()
cleanupMemory()
row.Mem = mem
row.WarmupTime = warmupTime
row.InferTime = inferTime
row.Notes = notes
row.Status = testStatus(t)
recordReport(row)
t.Logf("[%s/%s/%s] load=%s infer=%s %s",
spec.Name, variant.Name, resolvedLabel,
humanize.Duration(row.LoadTime), humanize.Duration(inferTime), mem)
if warmupTime > 0 {
t.Logf("[%s/%s/%s] load=%s warmup=%s infer=%s %s",
spec.Name, variant.Name, resolvedLabel,
humanize.Duration(row.LoadTime), humanize.Duration(warmupTime), humanize.Duration(inferTime), mem)
} else {
t.Logf("[%s/%s/%s] load=%s infer=%s %s",
spec.Name, variant.Name, resolvedLabel,
humanize.Duration(row.LoadTime), humanize.Duration(inferTime), mem)
}
}()

tLoad := time.Now()
Expand All @@ -142,6 +150,13 @@ func runTimeSeriesModel(t *testing.T, loaded loadedSpec, backendConfigs []string
defer exec.Finalize()

x := tensors.FromFlatDataAndDimensions(input, batch, ts.ContextSteps)
if *flagWarmup {
tw := time.Now()
yWarm, wErr := exec.Call1(x)
warmupTime = time.Since(tw)
require.NoError(t, wErr)
yWarm.FinalizeAll()
}
t0 := time.Now()
y, err := exec.Call1(x)
inferTime = time.Since(t0)
Expand Down
67 changes: 58 additions & 9 deletions modeltests/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func runTransformerModel(
}

store := createStoreWithSettings(t, opts)
var totalWarmupTime time.Duration
var totalInferTime t 8 ime.Dura 10BC8 tion
var notes string

Expand All @@ -122,13 +123,20 @@ func runTransformerModel(
mem := memTracker.Stop()
cleanupMemory()
row.Mem = mem
row.WarmupTime = totalWarmupTime
row.InferTime = totalInferTime
row.Notes = notes
row.Status = testStatus(t)
recordReport(row)
t.Logf("[%s/%s/%s] load=%s infer=%s %s",
spec.Name, variant.Name, resolvedLabel,
humanize.Duration(row.LoadTime), humanize.Duration(totalInferTime), mem)
if totalWarmupTime > 0 {
t.Logf("[%s/%s/%s] load=%s warmup=%s infer=%s %s",
spec.Name, variant.Name, resolvedLabel,
humanize.Duration(row.LoadTime), humanize.Duration(totalWarmupTime), humanize.Duration(totalInferTime), mem)
} else {
t.Logf("[%s/%s/%s] load=%s infer=%s %s",
spec.Name, variant.Name, resolvedLabel,
humanize.Duration(row.LoadTime), humanize.Duration(totalInferTime), mem)
}
}()

tLoad := time.Now()
Expand All @@ -141,16 +149,18 @@ func runTransformerModel(
loadedPaths := snapshotVariablePaths(store)

if variant.Embedding != nil {
inferDur, embNotes := runTransformerEmbeddingChecks(
inferDur, warmupDur, embNotes := runTransformerEmbeddingChecks(
t, backend, store, hfModel, tokenizer, loaded.Dir, variant.Embedding, sharedSentences, loadedPaths)
totalInferTime += inferDur
totalWarmupTime += warmupDur
notes = embNotes
}

if variant.Generate != nil {
inferDur, genNotes := runTransformerGenerateChecks(
inferDur, warmupDur, genNotes := runTransformerGenerateChecks(
t, backend, store, hfModel, tokenizer, variant.Generate, sharedPrompts, loadedPaths)
totalInferTime += inferDur
totalWarmupTime += warmupDur
if notes != "" && genNotes != "" {
notes += "; " + genNotes
} else if genNotes != "" {
Expand Down Expand Up @@ -233,7 +243,7 @@ func runTransformerEmbeddingChecks(
embSpec *EmbeddingSpec,
sharedSentences *SharedSentences,
loadedPaths map[string]bool,
) (time.Duration, string) {
) (time.Duration, time.Duration, string) {
sentences := resolveSentences(embSpec, sharedSentences)
require.NotEmpty(t, sentences, "embedding spec resolved to zero sentences")

Expand Down Expand Up @@ -267,6 +277,7 @@ func runTransformerEmbeddingChecks(
padID = int32(id)
}

var totalWarmup time.Duration
var totalInfer time.Duration
worstCos := 1.0
worstMaxAbs := 0.0
Expand All @@ -288,6 +299,15 @@ func runTransformerEmbeddingChecks(
tokens[j] = int32(id)
}
inTensor := tensors.FromValue([][]int32{tokens})
if *flagWarmup {
tw := time.Now()
wRes, wErr := singleExec.Exec(inTensor)
totalWarmup += time.Since(tw)
require.NoError(t, wErr, "warmup singleExec.Exec sentence %d", i)
for _, wt := range wRes {
wt.FinalizeAll()
}
}
t0 := time.Now()
results, err := singleExec.Exec(inTensor)
totalInfer += time.Since(t0)
Expand Down Expand Up @@ -364,6 +384,15 @@ func runTransformerEmbeddingChecks(
copy(batchFlat[i*paddedLen:i*paddedLen+len(row)], row)
}
batchTensor := tensors.FromFlatDataAndDimensions(batchFlat, len(sentences), paddedLen)
if *flagWarmup {
tw := time.Now()
wRes, wErr := batchExec.Exec(batchTensor)
totalWarmup += time.Since(tw)
require.NoError(t, wErr, "warmup batchExec.Exec")
for _, wt := range wRes {
wt.FinalizeAll()
}
}
t0 := time.Now()
results, err := batchExec.Exec(batchTensor)
totalInfer += time.Since(t0)
Expand All @@ -388,7 +417,7 @@ func runTransformerEmbeddingChecks(
})
}

return totalInfer, fmt.Sprintf("min_cos=%.5f max_diff=%.2e", worstCos, worstMaxAbs)
return totalInfer, totalWarmup, fmt.Sprintf("min_cos=%.5f max_diff=%.2e", worstCos, worstMaxAbs)
}

func runTransformerGenerateChecks(
Expand All @@ -400,9 +429,10 @@ func runTransformerGenerateChecks(
genSpec *GenerateSpec,
_ *SharedPrompts,
loadedPaths map[string]bool,
) (time.Duration, string) {
) (time.Duration, time.Duration, string) {
require.NotEmpty(t, genSpec.Cases, "generate spec has no test cases")

var totalWarmup time.Duration
var totalInfer time.Duration
totalMatched := 0
totalExpected := 0
Expand Down Expand Up @@ -434,6 +464,15 @@ func runTransformerGenerateChecks(
}
tokens := xslices.Map(promptIDs, func(v int) int32 { return int32(v) })
inTensor := tensors.FromValue([][]int32{tokens})
if *flagWarmup {
tw := time.Now()
wRes, wErr := prefillExec.Exec(inTensor)
totalWarmup += time.Since(tw)
require.NoError(t, wErr, "warmup prefillExec.Exec %s", gc.Name)
for _, wt := range wRes {
wt.FinalizeAll()
}
}
t0 := time.Now()
results, err := prefillExec.Exec(inTensor)
totalInfer += time.Since(t0)
Expand Down Expand Up @@ -617,10 +656,20 @@ func runTransformerGenerateChecks(
var stepLogits [][]float32
var dt time.Duration
if genSpec.UseNaiveOnly {
if *flagWarmup {
tw := time.Now()
_ = runTeacherForcedNaive(t, backend, store, hfModel, promptIDs, wantIDs)
totalWarmup += time.Since(tw)
}
t0 := time.Now()
stepLogits = runTeacherForcedNaive(t, backend, store, hfModel, promptIDs, wantIDs)
dt = time.Since(t0)
} else {
if *flagWarmup {
// Warm up both prefillExec (for len(promptIDs)) and stepExec (1 decode step).
_, _, wdt := kvRunner.Run(t, promptIDs, wantIDs, min(2, len(wantIDs)))
totalWarmup += wdt
}
stepLogits, _, dt = kvRunner.Run(t, promptIDs, wantIDs, len(wantIDs))
}
totalInfer += dt
Expand Down Expand Up @@ -683,7 +732,7 @@ func runTransformerGenerateChecks(
if totalExpected > 0 {
notes = fmt.Sprintf("tf=%d/%d toks", totalMatched, totalExpected)
}
return totalInfer, notes
return totalInfer, totalWarmup, notes
}

// teacherForcedKVRunner runs prefill + teacher-forced single-token KV-cache decode steps,
Expand Down
Loading
0