Skip to content

Add a precision benchmark: float32 against float64 across CPU, CUDA and Apple Silicon - #2

Merged
mmcky merged 3 commits into
mainfrom
precision-harness
Oct 25, 2026
Merged

mmcky merged 3 commits into
mainfrom
precision-harness

Conversation

@mmcky

@mmcky mmcky commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Part of #1: the Phase 0 harness, built so the matrix can run on qe-mini1, the RunsOn T4 and then qe-wharton1-gpu without changes.

What it adds

  • precision/, which measures what float64 costs against float32 on each device, by kernel type:

    Workload Bound by
    matmul compute
    bellman_stored: one Bellman step over a stored m × m reward array memory
    bellman_recompute: the same step, recomputing log-utility rewards in blocks of 512 rows transcendental arithmetic
    kalman: a 10-state Kalman filter under lax.scan launch latency

    Both Bellman workloads shard by rows across GPUs (gpu:2), for the capacity question on the Wharton machine's two Titans.

  • One fresh process per cell, with JAX_ENABLE_X64, JAX_PLATFORMS and CUDA_VISIBLE_DEVICES set before JAX imports. Each cell records compile and first-run time apart from five warm runs. It asserts the result dtype, keeps 64 fixed-position result values for float32/float64 fidelity, and keeps XLA's memory analysis. On a GPU it also reads clocks, temperature and power just after timing.

  • Memory-aware planning: cells that would exceed --memory-fraction of a device's memory are recorded as skipped rather than run. A skipped cell is a data point for the capacity question.

  • Output: one JSON line per cell in results/<host>/<date>.jsonl. Each line carries an environment block that is a superset of hardware/benchmark_hardware.py's system block. summarise.py turns the lines into markdown tables: times, the float64 penalty, rates, fidelity, and where each GPU first beats its own host CPU, warm and cold.

  • benchmark.yml gains run_precision and precision_quick inputs. They run the matrix on the RunsOn GPU instance and its host CPU, and write the summary to the job summary. run_precision defaults to false, so nothing runs, and nothing costs anything, until it is dispatched.

How it was tested

  • The full CPU matrix ran end to end on an M1 Pro (JAX 0.12.0.1). The results are not committed: the laptop was in use and short of memory, and the 20k float64 stored-Bellman cell (3 GB) went into swap at 1.7 GB/s, against ~80 GB/s at every other size. A clean machine should supply the first reference results. The pattern is the CPU one expected: the float64 penalty is about 2× for matmul and the Bellman steps and about 1× for the Kalman filter.
  • The sharded path was run on CPU with forced host devices (cpu:2). Sharded and unsharded results agree exactly, stored and recomputed results agree exactly, and float32 sits about 4e-7 from float64.
  • The crossover table was exercised with synthetic GPU rows. The workflow file parses.
  • The GPU path has not run, because there is no NVIDIA card here: the CUDA environment, the nvidia-smi telemetry and gpu:2 sharding over real cards. The first run_precision dispatch on the T4 is its test.

One finding along the way

Written as one broadcast, the recomputing Bellman step saves no memory on CPU. XLA materialises the full m × m array, and its memory analysis reports a temp of exactly m² × itemsize. Hence the blocks, which bring the temp down to about 512 × m. A lecture written the naive way would get the same surprise.

Not yet

🤖 Generated with Claude Code

mmcky and others added 2 commits October 24, 2026 18:40
…nd Apple Silicon

A harness under precision/ that measures what float64 costs on each kind of
device, by kernel type: a dense matmul (compute-bound), a Bellman step over
stored rewards (memory-bound), the same step recomputing rewards in blocks
(transcendental), and a Kalman filter under lax.scan (launch-bound). Each
cell runs in a fresh process with precision and device set before JAX
imports, keeps cold and warm times apart, asserts the result dtype, and
appends one JSON line with an environment block to results/<host>/.

The Bellman workloads shard by rows across GPUs, for the capacity question
on qe-wharton1-gpu. The workflow gains a run_precision input that runs the
matrix on the RunsOn GPU instance and its host CPU.

Part of #1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 24, 2026 07:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It introduces a sizable new benchmarking subsystem whose GPU/CUDA and multi-card sharding paths are, by the author's own account, not yet executed on real hardware and incur CI cost, so final human verification is warranted.

No open issues 🎉

What changed in this PR

This PR adds a new precision/ subsystem that measures the cost of float64 relative to float32 across CPU, CUDA, and Apple Silicon, broken down by kernel type (compute-, memory-, transcendental-, and launch-bound). It is Phase 0 of issue #1 and is designed to run unchanged on qe-mini1, the RunsOn T4, and qe-wharton1-gpu. Each benchmark cell runs in a fresh process with precision/device flags set before JAX imports, records cold and warm timings, asserts the result dtype, captures fidelity samples and XLA memory analysis, and (on GPU) reads telemetry. A memory-aware planner skips cells that would exceed a device-memory fraction, and summarise.py renders markdown tables including the float64 penalty and GPU/CPU crossover.

Changes:

  • New precision/ harness: spec.py (matrix + memory formulas), kernels.py (matmul, two Bellman steps, Kalman filter, with row-sharding), run_cell.py (single-cell timing), run_matrix.py (per-cell process orchestration + memory planning), summarise.py (markdown tables), env_info.py (environment capture).
  • benchmark.yml gains run_precision/precision_quick dispatch inputs and a GPU job that runs the matrix on the RunsOn GPU and its host CPU, writing to the job summary and uploading results/.
  • README updates documenting the new precision/ and results/ directories.
File Description
precision/​spec.py Declares workloads, sizes, shardability, rate units, and per-device memory estimates used for skip planning.
precision/​kernels.py JAX kernel builders with row-sharding; stored and recomputed Bellman steps compute the same answer.
precision/​run_cell.py Times one cell in a fresh process, asserts result dtype, captures samples/telemetry/memory analysis.
precision/​run_matrix.py Orchestrates one process per cell, sets precision/device env, enforces memory-fraction skipping, writes JSONL.
precision/​summarise.py Aggregates JSONL into markdown tables: times, penalty, rates, fidelity, GPU/CPU crossover.
precision/​env_info.py Collects a machine/environment block that is a superset of benchmark_hardware.py's system block.
precision/​README.md Documents workloads, running instructions, method, output, and caveats.
.github/​workflows/​benchmark.yml Adds dispatch inputs and a precision GPU job (install JAX, run matrix, summarise, upload results).
README.md Adds precision/ and results/ entries and a Precision Benchmarks section.

Review effort: Balanced

- run_cell.py records backend_init_s, the time jax.devices() takes to
  start the backend. It fell into no field before. On qe-wharton1-gpu
  it is about 0.2 s on the CPU and 0.6 s on a TITAN RTX, as much as
  the compile and first run of most quick cells. summarise.py counts
  it in the cold time and the cold crossover.
- env_info.py ignores untracked files for git_dirty, so earlier results
  in results/ don't mark every later run dirty.
- JAX is pinned in precision/constraints.txt (0.12.0.1), used by the
  workflow and the README, so that every platform runs the same release.
- The precision job uses the lectures' RunsOn label form, with the
  image defined in .github/runs-on.yml as lecture-jax defines it. The
  old labels (runner=gpu, image=quantecon-gpu-ubuntu2404) are not
  defined in this repository.
- Two dispatch choices: precision_instance (g4dn T4, g5 A10G, g6 L4)
  and precision_cuda (cuda13, cuda12; cuda12 runs get --label cuda12).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mmcky

mmcky commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Smoke-tested on real hardware on 2026-10-25, with fixes and two dispatch choices pushed in 91baa22.

What ran. The quick matrix on qe-mini1 (M6, CPU) and on qe-wharton1-gpu: its CPU, one TITAN RTX (gpu, which PCI order maps to 0b:00.0), and both cards (gpu:2). JAX 0.12.0.1 throughout, under both jax[cuda13] and jax[cuda12] on the Titans. Every cell came back ok, the sharded and unsharded Bellman results agree exactly with the CPU's, and the telemetry reads. These were checks of the harness, not results, so nothing went into results/.

What 91baa22 changes.

  • backend_init_s. import_s was recorded before jax.devices(), so starting the backend fell into no field. On the Wharton box it is about 0.2 s on the CPU and 0.6 s on a Titan, as much as the compile and first run of most quick cells. summarise.py now counts it in the cold time and in the cold crossover. The rest of the gap between process_s and the timed fields is process teardown after the result is printed (about 0.3 s on CPU, 0.55 s on CUDA), which a lecture doesn't wait for.
  • git_dirty ignores untracked files. results/ isn't gitignored, so every run after the first in a checkout was marked dirty.
  • JAX is pinned in precision/constraints.txt (0.12.0.1). The workflow and the README install with -c, so every platform runs the same release, as Precision and platform: float64 against float32 on CPU, CUDA and Apple Silicon #1 requires.
  • The precision job's RunsOn labels. runner=gpu and image=quantecon-gpu-ubuntu2404 aren't defined in this repository, so the job would never have found a runner. It now uses the lectures' label form (family=…/image=quantecon_ubuntu2404/…), with the image defined in .github/runs-on.yml as lecture-jax defines it, and installs Python with setup-python rather than relying on conda in the image.
  • Two dispatch choices: precision_instance (g4dn.2xlarge: T4, g5.2xlarge: A10G, g6.2xlarge: L4) and precision_cuda (cuda13 or cuda12; a cuda12 run is labelled cuda12).

Still open, outside this PR.

  • The two older GPU jobs (benchmark-gpu, benchmark-gpu-nsight) and .github/workflows/README.md still use the undefined labels.
  • Whether the RunsOn app (runson-v3-quantecon, installed on selected repositories) includes this repository.
  • Dispatch only after merging: as far as I know, RunsOn reads .github/runs-on.yml from the default branch.
  • Merge before the Phase 0 and Phase 1 runs, so that every row's git_commit is on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants