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
25 changes: 15 additions & 10 deletions .github/workflows/Benchmarking.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ jobs:
if: ${{ !cancelled() && needs.benchmark-pr.result == 'success' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: julia-actions/setup-julia@v3
with:
version: '1'
- uses: actions/download-artifact@v8
with:
name: benchmark-pr
Expand All @@ -102,24 +108,23 @@ jobs:
head_sha='${{ github.event.pull_request.head.sha }}'
main_sha='${{ needs.benchmark-main.outputs.sha }}'
main_status='${{ needs.benchmark-main.result }}'
main_table=()
if [[ "$main_status" == "success" ]]; then
main_table=(main/results.md)
fi
julia benchmarks/report.jl '${{ github.repository }}' "$head_sha" "$main_sha" \
head/results.md "${main_table[@]}" > table.html
{
echo "<!-- benchmark-report -->"
echo "## Benchmarks @ ${head_sha}"
echo "## Benchmarks"
echo ""
echo "**Performance Ratio:** gradient time divided by log-density time."
echo ""
echo "For very small models these ratios are noisy across runs and machines; raw primal and gradient timings are more reliable. The benchmarks are aimed at DynamicPPL developers and mainly catch obvious allocation or type-stability regressions. See [benchmark notes](https://github.com/TuringLang/DynamicPPL.jl/tree/main/benchmarks#interpreting-results) for details."
echo ""
cat head/results.md
cat table.html
echo ""
if [[ "$main_status" == "success" ]]; then
echo "<details><summary>Main @ ${main_sha}</summary>"
echo ""
cat main/results.md
echo ""
echo "</details>"
echo ""
else
if [[ "$main_status" != "success" ]]; then
echo "Main benchmark job did not succeed (\`${main_status}\`) — see workflow logs."
echo ""
fi
Expand Down
7 changes: 4 additions & 3 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ dominated by timer floor, fixed overhead, and run-to-run variation. For those
rows, raw `t(grad)` is more meaningful than `t(grad)/t(logdensity)`. These
microbenchmarks can also vary noticeably across runs and machines.

The CI comment shows the PR head table first and, when available, includes a
collapsed `main` table for comparison. Treat the numbers as approximate and use
the `main` table to spot likely regressions.
The CI comment shows one fixed-width table in a code fence with `main` and `PR`
primal times side by side, with commit links above the table. Gradient ratios
are from the PR only. Missing `main` results appear as `—`. Treat the numbers as
approximate and use the primal columns to spot likely regressions.

## PosteriorDB comparison

Expand Down
134 changes: 134 additions & 0 deletions benchmarks/report.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Usage: julia benchmarks/report.jl REPO PR_SHA MAIN_SHA PR_TABLE [MAIN_TABLE]
# Uses only Base so post-comment does not need the benchmark environment.
function read_table(path)
lines = strip.(readlines(path))
if length(lines) >= 2 && first(lines) == last(lines) == "```"
lines = lines[2:(end - 1)]
end
malformed() = error("Malformed benchmark table: $path")
length(lines) >= 7 || malformed()
columns(line) = split(line, r" {2,}")
header = [
"Model", "dim", "linked", "primal", "FwdDiff", "RvsDiff", "Mooncake", "Enzyme"
]
occursin(r"^=+$", lines[1]) && lines[end] == lines[1] || malformed()
columns(lines[2]) == ["eval", "gradient"] || malformed()
occursin(r"^-+ -+$", lines[3]) || malformed()
columns(lines[4]) == header || malformed()
lines[5] == repeat("-", length(lines[1])) || malformed()

seen = Set{Tuple{String,String,String}}()
return map(lines[6:(end - 1)]) do line
# print_results uses a two-space gap; model names contain only single
# spaces, as does the separator between a primal number and its unit.
cells = columns(line)
length(cells) == length(header) || malformed()
name, dim, linked, primal = cells[1:4]
isempty(name) && malformed()
occursin(r"^(?:[0-9]+|err)$", dim) || malformed()
linked in ("true", "false") || malformed()
if primal != "err"
time = split(primal, ' ')
length(time) == 2 || malformed()
tryparse(Float64, time[1]) !== nothing || malformed()
time[2] in ("ns", "μs", "ms", "s") || malformed()
end
ratios = cells[5:8]
all(x -> x == "err" || tryparse(Float64, x) !== nothing, ratios) || malformed()
# The tiny-primal marker can differ between runs; it is not part of
# the model's identity. Preserve the PR marker in the displayed name.
key = (chopsuffix(name, "*"), dim, linked)
key in seen && malformed()
push!(seen, key)
(; key, name, dim, linked, primal, ratios)
end
end

function commit_header(repo, sha, label)
isempty(sha) && return "`$label`"
return "`$label` [$(first(sha, 7))](https://github.com/$repo/commit/$sha)"
end

function report(args)
length(args) in (4, 5) || error(
"Usage: julia benchmarks/report.jl REPO PR_SHA MAIN_SHA PR_TABLE [MAIN_TABLE]"
)
repo, pr_sha, main_sha, pr_path = args[1:4]
pr_rows = read_table(pr_path)
main_times = Dict{Tuple{String,String,String},String}()
main_note = ""
if length(args) == 5
# Main runs main's printer, which may predate a format change in this PR.
main_rows = try
read_table(args[5])
catch err
err isa ErrorException || rethrow()
@warn "Could not parse the main benchmark table" exception = err
main_note = "Main benchmark table could not be parsed; see workflow logs."
()
end
for row in main_rows
main_times[row.key] = row.primal
end
end
rows = map(pr_rows) do row
(
row.name,
row.dim,
row.linked,
get(main_times, row.key, "—"),
row.primal,
row.ratios...,
)
end
header = (
"Model", "dim", "linked", "main", "PR", "FwdDiff", "RvsDiff", "Mooncake", "Enzyme"
)
# Match print_results: one extra space for names, two for every other
# column, and a two-space gap between columns.
widths = [
max(length(label), maximum(textwidth(row[i]) for row in rows)) + (i == 1 ? 1 : 2)
for (i, label) in enumerate(header)
]
gap = " "
gap_w = textwidth(gap)
stub_w = sum(widths[1:3]) + 2 * gap_w
primal_w = sum(widths[4:5]) + gap_w
grad_w = sum(widths[6:9]) + 3 * gap_w
total_w = stub_w + gap_w + primal_w + gap_w + grad_w
center(s, w) = lpad(rpad(s, div(w + textwidth(s), 2)), w)
function format_row(row)
return join(
(
i == 1 ? rpad(cell, w) : lpad(cell, w) for
(i, (cell, w)) in enumerate(zip(row, widths))
),
gap,
)
end

println(
"Primal times: $(commit_header(repo, main_sha, "main")), $(commit_header(repo, pr_sha, "PR"))",
)
println("\n```")
println(repeat("=", total_w))
println(
rpad("", stub_w) *
gap *
center("primal", primal_w) *
gap *
center("gradient", grad_w),
)
println(rpad("", stub_w) * gap * repeat("-", primal_w) * gap * repeat("-", grad_w))
println(format_row(header))
println(repeat("-", total_w))
foreach(row -> println(format_row(row)), rows)
println(repeat("=", total_w))
println("```")
isempty(main_note) || println("\n", main_note)
return nothing
end

if abspath(PROGRAM_FILE) == @__FILE__
report(ARGS)
end
Loading