Skip to content

ciflow/trunk/207361

@mlazos mlazos tagged this 03 Dec 00:13
Strict numerics tests compile a function and require the result to match eager CUDA. Each existing test in `test/inductor/test_strict_numerics.py` hand-rolled its own compile, bit-view comparison and code inspection. This PR adds one harness that the rest of the stack builds on and moves the existing tests onto it. Every later PR in the stack adds its own tests to this structure.

How the tests are structured:

- `torch/testing/_internal/inductor_strict_numerics.py` holds the comparator (`assert_strict_equal`, plus `strict_equal` / `diff_kind` for sweeps), so other Inductor test files can share them. The comparator is bitwise except that all NaN encodings compare equal and `-0 == +0`: NaN payload/sign and the sign of zero are out of scope for strict numerics. NaN positions, infinities and finite values are compared exactly.
- `StrictNumericsTestCase.check_strict(fn, *calls, ...)` is the single compile-and-compare helper. It compiles `fn` once, compares every call's outputs and input mutations against eager, and optionally checks gradients, strides, lazy conj/neg flags, generated-code patterns, fallback kernels and Dynamo frame counts.
- `Case` rows in `STRICT_CASES` are the declarative form: a row names `fn`, an input builder, dtypes and a parameter grid. `StrictCaseTest.test_case` expands each row into ids of the form `test_case_<name>_<axes>_<dtype>_cuda`. Later PRs add rows next to the input helpers they need.
- One-off tests (`StrictNumericsCompileTest`, `StrictNumericsTest`, `StrictNumericsConfigTest`, ...) cover assertions a row cannot express, and also go through `check_strict`.
- Input generators (`bit_patterns`: exhaustive 16-bit / sampled fp32 patterns, `specials`, `boundary_values`, ...) are shared module-level helpers.
- `StrictHarnessTest` tests the comparator and every `check_strict` option, using small custom backends that inject each failure kind (wrong value, dropped mutation, detached gradient, materialized lazy view, ...).
- The OpInfo sweep (`PointwiseStrictNumericsTest`) keeps its structure. Its byte comparator `_outputs_equal` now treats all NaNs as equal and -0 as +0, matching the shared comparator's scope, so the ledger entries that differed only in NaN encoding or the sign of zero (min/max/fmin/fmax, `entr`, `xlogy`/`xlog1py`, `double` and a few clamp/activation and backward cases) are removed here. `acosh` float32 differs from eager on CUDA 12.9 builds but matches on CI's CUDA 13.2 build, so it is listed in `ENV_DEPENDENT_XFAIL` and not checked in either direction. Later PRs shrink the ledger as ops are fixed. The last PR replaces the sweep with an all-dtype sweep built on the shared comparator.

Existing tests are kept with the same ids, except `test_softshrink_strict_symbolic_lambd` and `test_compile_options_enable_eager_division`, which become `STRICT_CASES` rows. `test_relu_shared_predicate` becomes `test_clamp_shared_predicate`, which checks relu through `check_strict` instead of asserting the sign of `relu(-0.0)` (out of scope); later PRs extend it to the clamp family. `test_unary_bit_patterns` is new: it runs `abs`, `neg`, `angle`, `frexp` and `signbit` over every fp16/bf16 bit pattern.

`check_strict(no_fallback=True)` needs a count of generated fallback kernels, so this PR adds `torch._inductor.metrics.generated_fallback_kernel_count`, incremented in `FallbackKernel.codegen` (not for `ComplexView`). That is the only non-test change. It adds a metric and does not change compiled code.

## Test Plan

```
python test/inductor/test_strict_numerics.py -k "not PointwiseStrictNumericsTest"
```

`StrictNumericsTest` keeps its existing gate (CUDA with `tl.ReductionOrdering`); its comparisons against eager sums need the eager inner-tree sum implementation (CuTeDSL) and are skipped without it, while the Inductor-side checks still run.

## Agent details

This PR was written with an AI assistant (Claude).

[ghstack-poisoned]
Assets 2
Loading