Skip to content

Support global sparsity tracing through distribution constructors - #2064

Merged
devmotion merged 1 commit into
masterfrom
dmw/sparseconnectivitytracer
Jun 15, 2026
Merged

devmotion merged 1 commit into
masterfrom
dmw/sparseconnectivitytracer

Conversation

@devmotion

Copy link
Copy Markdown
Member

Motivation

@check_args validates a distribution's constructor arguments with comparisons such as σ ≥ 0. Under SparseConnectivityTracer's global TracerSparsityDetector, those comparisons run on primal-free tracers and return a tracer rather than a Bool, so they cannot be used in the boolean context the checks require. As a result, the global sparsity pattern cannot currently be traced through any distribution construction — jacobian_sparsity(x -> mean(Normal(x[1], x[2])), x, TracerSparsityDetector()) errors inside check_args.

Change

Introduce an overloadable per-argument predicate _arg_checkable(x) (default true). @check_args now forwards the explicitly-named checked arguments to check_args, which skips the checks when any argument is not checkable:

_arg_checkable(@nospecialize(_)) = true

function check_args(f::F, check::Bool, args...) where {F}
    (check && all(_arg_checkable, args)) && f()
    nothing
end

A new weak-dependency extension DistributionsSparseConnectivityTracerExt provides the single overload:

Distributions._arg_checkable(::AbstractTracer) = false

AbstractTracer covers exactly the primal-free global tracers (GradientTracer/HessianTracer). Local sparsity detection (TracerLocalSparsityDetector) passes primal-carrying Duals, which are <: Real (not <: AbstractTracer), so their comparisons evaluate to Bool as usual and the checks run unchanged — invalid arguments are still rejected under local tracing.

Ordinary numeric construction is unaffected (args are plain numbers, _arg_checkable is true).

Tests

test/sparseconnectivitytracer.jl checks that global gradient and (block-off-diagonal) Hessian sparsity trace through Normal construction, and that local detection still enforces argument validity.


This pull request was prepared with the assistance of generative AI (Claude Code).

`@check_args` performs validity comparisons on the constructor arguments (e.g. `σ ≥ 0`). Under
SparseConnectivityTracer's global `TracerSparsityDetector`, those comparisons run on primal-free
tracers and return a tracer rather than a `Bool`, so they cannot be used in the boolean context the
checks require — which prevents tracing the global sparsity pattern through any distribution
construction.

Introduce an overloadable per-argument predicate `_arg_checkable(x)` (default `true`). `@check_args`
now forwards the explicitly-named checked arguments to `check_args`, which skips the checks when any
argument is not checkable. A new `DistributionsSparseConnectivityTracerExt` extension defines
`_arg_checkable(::AbstractTracer) = false`, covering the primal-free `GradientTracer`/`HessianTracer`
used by global detection. Local detection (`TracerLocalSparsityDetector`) passes primal-carrying
`Dual`s (`<: Real`, not `<: AbstractTracer`), so its checks run unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@devmotion
devmotion force-pushed the dmw/sparseconnectivitytracer branch from 18ad218 to 17a40a1 Compare June 8, 2026 15:42
@codecov-commenter

codecov-commenter commented Jun 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.65%. Comparing base (a7ed7b6) to head (17a40a1).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/utils.jl 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2064      +/-   ##
==========================================
- Coverage   86.65%   86.65%   -0.01%     
==========================================
  Files         148      149       +1     
  Lines        8919     8925       +6     
==========================================
+ Hits         7729     7734       +5     
- Misses       1190     1191       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@andreasnoack andreasnoack left a comment

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.

Looks good. Based on Claude's review assistance, I've filed #2067 to keep track of possible follow-up work needed to support all distributions.

@devmotion
devmotion merged commit 5e315e2 into master Jun 15, 2026
15 checks passed
@devmotion
devmotion deleted the dmw/sparseconnectivitytracer branch June 15, 2026 23:00
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.

3 participants