Skip to content

fix: materialize host views for AutoMooncake (CTBaseMooncake extension) - #632

Merged
ocots merged 3 commits into
mainfrom
fix/mooncake-host-views
Oct 21, 2026
Merged

ocots merged 3 commits into
mainfrom
fix/mooncake-host-views

Conversation

@ocots

@ocots ocots commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Gradients of CTBase.Differentiation taken with AutoMooncake at a view of a host array are now arrays. Mooncake returns a structural Mooncake.Tangent (fields parent, indices, …) for a SubArray, even with friendly_tangents=true (chalk-lab/Mooncake.jl#1294), which made Flow(h; ad_backend=AutoMooncake()) fail on CPU in CTFlows (control-toolbox/CTFlows.jl#448, plan and probes there).

What changes

  • src/Differentiation/materialize.jl (new): the hook _materialize(ad::ADTypes.AbstractADType, x), a pass-through by default. It dispatches on the ADTypes backend the :ad_backend option already holds: no tag type is needed, ADTypes being a hard dependency.
  • ext/CTBaseMooncake.jl (new extension, weak dependency Mooncake): _materialize(::ADTypes.AutoMooncake, x::SubArray{<:Any,<:Any,<:Array}) = copy(x).
  • ext/CTBaseDifferentiationInterface.jl: every entry point (hamiltonian_gradient, variable_gradient, the three pseudo_*, gradient, differentiate, pushforward) adjusts its inputs with _ad_inputs before building the closures, so the captured arguments are dense too. Copying only the differentiated argument was measured 30–40 % slower.
  • Other backends keep their views (pass-through), and device arrays are not matched (SubArray of an Array only).
  • Tests: test/suite/extensions/test_mooncake_extension.jl (57 tests: the hook, every entry point on vector and matrix views against ForwardDiff). Docs wiring, changelog.

Checks run locally

  • Full suite: 5433/5433, including Aqua, JET and test_extensions_quality (TST8).
  • Docs: built twice locally, no error.
  • CTFlows against this branch: full suite green (8532 pass, 2 broken = GPU skips).

No breaking change. Next: release 0.30.13 (stable), then the compat bump in CTFlows.

🤖 Generated with Claude Code

Mooncake returns a structural Mooncake.Tangent for a SubArray, even with
friendly_tangents=true (chalk-lab/Mooncake.jl#1294), so the gradients of
CTBase.Differentiation taken at a view of a host array could not be used
as arrays (CTFlows.jl#448).

- Differentiation: tags MooncakeTag / GenericADTag, _ad_tag(ad), and the
  _materialize(tag, x) hook (pass-through by default).
- New CTBaseMooncake extension (weakdep Mooncake): _materialize copies a
  view of a host Array for MooncakeTag.
- CTBaseDifferentiationInterface: every entry point adjusts its inputs
  (_ad_inputs) before building the closures, so the captured arguments are
  dense too (copying only the differentiated argument is 30-40% slower).
  Other backends and device arrays are unchanged.
- Tests, docs wiring, changelog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots ocots added run ci github-runner Trigger the CI workflow on GitHub-hosted runners run breakage Trigger the Breakage workflow on this PR run documentation Trigger the Documentation workflow on this PR documentation push preview With 'run documentation': also deploy a preview of the docs labels Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Breakage — packages

Tested commit: 16f3937e4beec4e9a98b2f5ca51976173ef2a02d. Each package is tested at its default branch (Latest) and its newest tag (Stable).

Package Check Latest Stable
CTDirect test ✅ main, compat v0.30.12 ✅ v1.1.8-beta, compat v0.30.12
CTFlows test ✅ main, compat v0.30.12 ✅ v0.18.1, compat v0.30.12
CTLie test ✅ main, compat v0.30.12 ✅ v0.3.1, compat v0.30.12
CTModels test ✅ main, compat v0.30.12 ✅ v0.20.0, compat v0.30.12
CTParser test ❌ main, compat v0.28.9-beta ❌ v0.9.5-beta, compat v0.30.12
CTSolvers test ✅ main, compat v0.30.12 ✅ v0.6.2, compat v0.30.12
OptimalControl test ❌ main, compat v0.30.12 ❌ v2.2.1-beta, compat v0.30.12
OptimalControlProblems test ❌ main, compat v0.16.4 ❌ v0.5.0, compat v0.16.4

ocots and others added 2 commits October 21, 2026 09:50
`using ..Core: AbstractTag` is not resolved by JET's top-level analysis on
Julia 1.10; use `using ..Core: Core` and `Core.AbstractTag`, like the other
submodules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `:ad_backend` option already holds an ADTypes backend, and ADTypes is a
hard dependency: CTBaseMooncake overloads
`_materialize(::ADTypes.AutoMooncake, ::SubArray{<:Any,<:Any,<:Array})`
directly. MooncakeTag, GenericADTag and _ad_tag are removed (ad_tags.jl ->
materialize.jl), and with them the import of Core.AbstractTag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ocots
ocots merged commit 5e3d0e4 into main Oct 21, 2026
33 checks passed
@ocots
ocots deleted the fix/mooncake-host-views branch October 21, 2026 08:50
@ocots ocots mentioned this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation push preview With 'run documentation': also deploy a preview of the docs run breakage Trigger the Breakage workflow on this PR run ci github-runner Trigger the CI workflow on GitHub-hosted runners run documentation Trigger the Documentation workflow on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant