Skip to content

Zeros-init + well-conditioned Gram-Schmidt; add dace_canonicalize_{cpu,gpu} frameworks#47

Open
ThrudPrimrose wants to merge 2 commits into
mainfrom
fix/well-conditioned-gramschmidt-and-zeros-init
Open

Zeros-init + well-conditioned Gram-Schmidt; add dace_canonicalize_{cpu,gpu} frameworks#47
ThrudPrimrose wants to merge 2 commits into
mainfrom
fix/well-conditioned-gramschmidt-and-zeros-init

Conversation

@ThrudPrimrose

@ThrudPrimrose ThrudPrimrose commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Three changes.

1. np.empty / np.empty_like -> np.zeros / np.zeros_like

256 sites across 132 files, all backends (numpy, dace, numba, cupy, dpnp, pythran, legate, jax).

An uninitialized empty buffer that a kernel writes only partially leaves stale/garbage memory in the result. Those kernels are therefore read-nondeterministic: the same input can yield different outputs on different runs, and the comparison against the reference flakes. Zero-initializing makes every run reproducible. For buffers that are fully overwritten before use the change is equivalent, so all sites are converted uniformly. Commented-out code is left untouched.

2. Well-conditioned Gram-Schmidt input

gramschmidt's initialize now builds a deterministically full-column-rank, well-conditioned matrix via a diagonal-dominance term, replacing the reject-sampling while np.linalg.matrix_rank(A) < N loop:

A = rng.random((M, N), dtype=datatype)
A[:N, :N] += N * np.eye(N, dtype=datatype)

A plain random matrix is only full-rank in expectation and can still be poorly conditioned, which makes the QR numerically unstable and sensitive to harmless FMA-contraction reassociation. The added diagonal gives cond(A) ~= 1.5 deterministically. All framework implementations validate against the same-input NumPy reference, so the change is self-consistent.

3. New frameworks: dace_canonicalize_cpu / dace_canonicalize_gpu

Two frameworks that run DaCe's canonicalize pipeline instead of auto_optimize, to contrast the WCR (write-conflict-resolution) reduction lowering:

  • The new frameworks build the SDFG, run canonicalize(target=cpu|gpu, peel_limit=4, break_anti_dependence=True, interchange_carry_with_map=True, scatter_to_guarded_maps=True) + finalize_for_target, leaving sdfg.openmp_array_reductions = True (WCR-config ON) so a whole-buffer WCR accumulator of a parallel map lowers to an OpenMP reduction(op:A[0:n]) array-section clause instead of per-element atomics.
  • The existing dace_cpu / dace_gpu now set openmp_array_reductions = False explicitly before compile (WCR-config OFF, the default per-element atomic path).

Registered exactly like dace_cpu/dace_gpu: framework_info/dace_canonicalize_{cpu,gpu}.json + a DaceCanonicalizeFramework class (a DaceFramework subclass shared by both arches, selected by the JSON arch field). Reuses the existing *_dace.py implementations and every DaceFramework calling convention.

Verified: the CPU variant builds, runs and validates against NumPy on atax (a reduction kernel). The GPU variant is defined/registered analogously and builds + canonicalizes; fully running it needs the GPU device-schedule assignment that DaCe's canonicalize(target='gpu') path does not yet emit, so it is definition-only for now.

Two fixes for run-to-run reproducibility and numerical stability.

1. np.empty / np.empty_like -> np.zeros / np.zeros_like for output and work
   buffers across the whole suite (256 sites in 132 files, all backends:
   numpy, dace, numba, cupy, dpnp, pythran, legate, jax). An uninitialized
   `empty` buffer that a kernel writes only partially leaves stale/garbage
   memory in the result, so those kernels are read-nondeterministic and their
   comparison against the reference flakes across runs. Zeroing makes every run
   reproducible; for buffers that are fully overwritten before use it is
   equivalent, so all sites are converted uniformly. Commented-out code is left
   untouched.

2. gramschmidt: make the input matrix deterministically full column rank and
   well-conditioned via a diagonal-dominance term, replacing the reject-sampling
   `while matrix_rank(A) < N` loop. A plain random matrix is only full-rank in
   expectation and can be poorly conditioned, which makes the Gram-Schmidt QR
   numerically unstable and sensitive to harmless FMA-contraction reassociation.
   The added diagonal gives cond(A) ~= 1.5 with no nondeterministic resampling.
Two new frameworks that run DaCe's canonicalize pipeline instead of auto_optimize,
to contrast the WCR (write-conflict-resolution) reduction lowering.

- dace_canonicalize_cpu / dace_canonicalize_gpu build the SDFG, then run
  canonicalize(target=cpu|gpu, peel_limit=4, break_anti_dependence=True,
  interchange_carry_with_map=True, scatter_to_guarded_maps=True) followed by
  finalize_for_target, leaving sdfg.openmp_array_reductions = True (WCR-config ON):
  a whole-buffer WCR accumulator of a parallel map lowers to an OpenMP
  reduction(op:A[0:n]) array-section clause instead of per-element atomics.
- The existing dace_cpu / dace_gpu now set openmp_array_reductions = False
  explicitly before compile (WCR-config OFF, the default per-element atomic path),
  so the contrast between the two families is explicit.

Registered exactly like dace_cpu / dace_gpu: two framework_info/*.json files plus a
DaceCanonicalizeFramework class (a DaceFramework subclass shared by both arches,
selected by the JSON "arch" field). Reuses the existing *_dace.py benchmark
implementations and every DaceFramework calling convention.

Verified: the CPU variant builds, runs and validates against NumPy on atax (a
reduction kernel). The GPU variant is defined/registered analogously and builds +
canonicalizes; fully running it needs the GPU device-schedule assignment that DaCe's
canonicalize(target='gpu') path does not yet emit, so it is definition-only for now.
@ThrudPrimrose ThrudPrimrose changed the title Zero-initialize output buffers; make Gram-Schmidt input well-conditioned Zeros-init + well-conditioned Gram-Schmidt; add dace_canonicalize_{cpu,gpu} frameworks Jul 11, 2026
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.

1 participant