Skip to content

Use generic CPU normalization when the kernel backend is unavailable - #930

Draft
ChrisRackauckas wants to merge 1 commit into
FluxML:masterfrom
ChrisRackauckas:fix/check-cpu-normalization-backend
Draft

ChrisRackauckas wants to merge 1 commit into
FluxML:masterfrom
ChrisRackauckas:fix/check-cpu-normalization-backend

Conversation

@ChrisRackauckas

@ChrisRackauckas ChrisRackauckas commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

🚧 UNREVIEWED — awaiting review by @ChrisRackauckas. Filed by an AI agent running as @ChrisRackauckas; Chris has not seen this.
Harness: Codex Desktop 0.171.0-alpha.13.2 · Model: gpt-6.2-astra
Conversation: local session 01a21e73-ebbf-7f80-9b25-d5646b56b858 (no session URL)

Use NNlib's generic normalization and pullback when the kernel backend for a Julia Array reports that it cannot run. Supported CPU backends use the fused path, including the native CPU in KernelAbstractions 0.9. The test compares the BatchNorm value and the input, scale, and bias gradients with their analytic expressions.

Reproduction and verification

An ordinary CPU Float32 BatchNorm call with a pullback is sufficient to reproduce the failure on native i686 Julia 1.13.4. With NNlib 0.9.61 and KernelAbstractions 0.10.9, execution reached the SPIR-V compiler and errored before the first numerical assertion with Invalid return type for runtime function 'unbox_int64': expected LLVM.IntegerType(i64), got LLVM.VoidType(void). With the companion capability fix and this change, the value and all three gradients passed (4/4).

using NNlib
using ChainRulesCore: rrule
x = Float32[0.1 1 2 -0.5; 0.3 1.2 0.7 2.1]
g, b = Float32[0.7, 1.25], Float32[-0.4, 0.6]
dy = Float32[1.1 -0.4 0.7 0.2; -0.3 0.2 -0.2 1.1]
y, back = rrule(NNlib.batchnorm, g, b, x, nothing, nothing, 0.1f0; eps=1f-5)
_, dg, db, dx, _, _, _ = back(dy)

The hosted Surrogates Core run originally aborted during PoCL thread creation in the Flux precompile workload. The numerical reproduction above used two PoCL workers so that the separate kernel capability failure could be observed.

With the corrected backend on Linux arm64, setting POCL_DEVICES=nonexistent made the released NNlib fail all three dispatch assertions and throw during BatchNorm. The candidate passed the three dispatch checks and four analytic comparisons on the unavailable device and on the working device. The 32-bit Surrogates Core/AD_compatibility.jl file passed all 289 assertions with the paired changes.

I also tested the oldest supported KernelAbstractions, 0.9.34, on Julia 1.13.4 arm64. The released NNlib 0.9.61 BatchNorm kernel threw MethodError: no method matching __index_Group_Linear(::CompilerMetadata) while evaluating a group index inside a subtraction. Assigning @index(Group, Linear) before subtracting one lets that backend lower the two affected kernels. All seven numerical and dispatch assertions passed on the real 0.9.34 CPU backend. They also passed in the isolated unavailable-backend process. An independent LayerNorm pullback on the same supported version failed in the other group-index expression on the release and passed all four analytical comparisons with the candidate.

In separate processes on native Linux x86_64, setting POCL_DEVICES=nonexistent on the corrected KernelAbstractions also reproduced all three dispatch failures and the BatchNorm exception on released NNlib. With this patch the parent passed seven assertions, the child passed seven assertions, and its exit check passed. On Linux arm64 the two normalization files selected with Pkg.test(; test_args=["--jobs=1", "cpu/normalization", "common_testsuite/normalization (CPU)"]) passed 245/245.

The hosted Surrogates rerun with POCL_CPU_MAX_CU_COUNT=2 subsequently reproduced this compiler error in Flux’s CPU BatchNorm precompile on released NNlib 0.9.61 and KernelAbstractions 0.10.9. Its prior run had aborted while PoCL created its threads.

I also forced KernelAbstractions 0.9.34 in a separate complete test run. NNlib’s library supports 0.9.34, while its test project sets a floor of 0.9.42. The extra run completed all 34 test files in 137m50s with 30,452 passes, two failures, four errors and two existing broken tests. The failures and errors were confined to upsampling gradients. An independent run of that upsampling group on the unchanged NNlib 0.9.61 release reproduced it: 241 passes, two failures and four errors. The four-line bilinear-gradient control failed identically on released and patched NNlib with 0.9.34. It passed on the test floor 0.9.42, on current 0.10.9, and on 0.9.34 with the upstream alias-scope fix applied. The normalization checks on 0.9.34 passed as described above.

I ran the complete default suite on macOS arm64 with Julia 1.13.4, registered KernelAbstractions 0.10.9 and Enzyme 0.13.221 using julia --startup-file=no --project=. -e 'using Pkg; Pkg.test(; test_args=["--jobs=2"])'. All 34 test files finished, with 30,458 passes, two existing broken tests and zero failures or errors in 58m54.9s. The run included Enzyme and Mooncake. Both source files matched the submitted diff byte-for-byte. Diff spelling and whitespace checks also passed. With the same Julia and registered dependency versions on Linux arm64, all 34 test files also finished with 30,458 passes and two existing broken tests in 227m43s using one worker.

On the submitted commit, hosted Julia 1.10 Linux passed 29,698 assertions in 94m32s, stable Linux passed 30,409 in 54m02s, and stable Windows passed 27,638 in 35m07s. Each reported two existing broken tests. Threaded Linux passed 16,992/16,992 and the Reactant group passed 28/28. Documentation and Buildkite also passed. Hosted macOS ran both normalization files successfully. It finished 13 of 34 files before GitHub cancelled the job at its two-hour limit. The log shows the new normalization file taking 21.5 seconds and the fused-attention file taking 23m42s. GitHub requires a repository maintainer to rerun the macOS job. The advisory nightly job stopped during resolution because its Julia development version permits Mooncake through 0.5.54 while the test environment requires at least 0.5.64. The same conflict occurred on the unchanged default branch.

The unchanged CI workflow completed successfully on the contributor fork at the exact submitted commit, 44cf20b4. On the hosted macOS 26 ARM64 image, its one-worker test run passed 27,638 assertions across all 34 files, with two existing broken assertions and zero failures or errors, in 96m54.6s. It used the workflow’s macOS setting NNLIB_TEST_ENZYME=false. All 182 package versions printed in the test manifest matched the timed-out upstream job. On the same fork, the direct parent had passed 27,630 assertions across 33 files with two existing broken assertions. The additional file on this PR supplied eight passing assertions.

With both upstream fixes and the two GEKPLS changes applied, the complete Surrogates Core suite passed 3,233/3,233 on native Linux i686 and x86_64. The i686 run used the hosted set of 258 package versions, coverage, bounds checks and the caller’s two-worker PoCL setting.

The work was produced in Codex Desktop 0.171.0-alpha.13.2 with gpt-6.2-astra, local session 01a221ab-aa0b-74d0-9a86-159c89e87d2c (no session URL). This draft awaits review by @ChrisRackauckas.

Links


🤖 Posted by an AI agent — harness: Codex Desktop 0.171.0-alpha.13.2 · model: gpt-6.2-astra
Conversation: local session 01a21e73-ebbf-7f80-9b25-d5646b56b858 (no session URL)

Honor the backend capability when selecting fused normalization for arrays.
Test the batch normalization value and all three pullback results.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Codex <noreply@openai.com>
Agent-Harness: Codex Desktop 0.171.0-alpha.13.2
Agent-Model: gpt-6.2-astra
Agent-Session: local session 01a221ab-aa0b-74d0-9a86-159c89e87d2c (no session URL)
@ChrisRackauckas

Copy link
Copy Markdown
Member Author

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

The native i686 regression failed during the released CPU BatchNorm pullback. With the backend capability fix and this change, the forward value and gradients for the input, scale and bias matched their analytic references. The complete NNlib default suite on macOS arm64 passed 30,458 assertions with two existing broken tests in 58m54.9s. The two Linux arm64 normalization files passed 245/245.

Links


🤖 Posted by an AI agent — harness: Codex Desktop 0.171.0-alpha.13.2 · model: gpt-6.2-astra
Conversation: local session 01a21e73-ebbf-7f80-9b25-d5646b56b858 (no session URL)

Comment on lines +256 to +257
group = @index(Group, Linear)
w = group - 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why this spurious change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's not spurious, there seems to be an issue with KernelAbstractions.jl I'll have it share the full assessment but it doesn't compile without the change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

The split assignment is required for the fused CPU LayerNorm pullback on supported KernelAbstractions versions. I ran an A/B check on Julia 1.13.4 (macOS arm64) with KernelAbstractions 0.9.34 and 0.9.42. I reverted only these two lines to w = @index(Group, Linear) - 1 and kept the rest of this PR applied. On both versions, the backend reported functional(CPU()) == true, the forward pass returned a fused pullback, and calling it failed in cpu_fused_norm_params_kernel!.

using NNlib
using ChainRulesCore: rrule

x = Float32[0.1 1 2 -0.5; 0.3 1.2 0.7 2.1]
g, b = Float32[0.7, 1.25], Float32[-0.4, 0.6]
dy = Float32[1.1 -0.4 0.7 0.2; -0.3 0.2 -0.2 1.1]
y, back = rrule(NNlib.layernorm, g, b, x; dims=1, eps=1f-5)
_, dg, db, dx = back(dy)

Both versions produced the same error. The excerpt below omits file paths and the rest of the stack trace.

ERROR: LoadError: MethodError: no method matching __index_Group_Linear(::KernelAbstractions.CompilerMetadata{KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.NoDynamicCheck, CartesianIndex{1}, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, KernelAbstractions.NDIteration.NDRange{1, KernelAbstractions.NDIteration.DynamicSize, KernelAbstractions.NDIteration.StaticSize{(1,)}, CartesianIndices{1, Tuple{Base.OneTo{Int64}}}, Nothing}})
The function `__index_Group_Linear` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  __index_Group_Linear(::Any, !Matched::CartesianIndex)

On these versions, KernelAbstractions collects assignments whose entire right-hand side is an @index call. Its CPU emitter adds the work-item CartesianIndex argument to the collected calls. With the subtraction on the same right-hand side, the collector misses the call, so @index expands to __index_Group_Linear(__ctx__) and dispatch fails. Assigning the group index first lets the emitter supply the second argument. The subtraction still converts the group index to zero-based indexing.

With the submitted two-line form restored, the forward value and the scale, bias, and input gradients passed all four analytic comparisons on each version. The capability check handles unavailable backends. This edit keeps the fused kernel working on the available CPU backend.


🤖 Posted by an AI agent — harness: Codex Desktop 0.171.0-alpha.13.2 · model: gpt-6.2-sol
Conversation: local session 01a22c2b-d4dc-79f2-b89b-f3b0037d916d (no session URL)

@CarloLucibello

Copy link
Copy Markdown
Member

@ChrisRackauckas, these AI posts are very unpleasant. Can they be made more concise and readable? Also, why is this a draft? Is it supposed to become ready for review by itself at some point?

This branch has not been deployed

No deployments
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