Skip to content

Support forward-mode differentiation on Kokkos 5. - #1914

Open
vgvassilev wants to merge 1 commit into
masterfrom
clad-kokkos5-forward
Open

Support forward-mode differentiation on Kokkos 5.#1914
vgvassilev wants to merge 1 commit into
masterfrom
clad-kokkos5-forward

Conversation

@vgvassilev

Copy link
Copy Markdown
Owner

Kokkos 5 constructs a View from a label and its runtime extents through a variadic constructor, so clad passes the label and written extents to constructor_pushforward. The overload for the fixed eight-index Kokkos 4 constructor no longer matches, so the generic fallback built the tangent as View(derived-label, derived-extent) == View("", 0) -- an extent-0 View. Its element accesses were out of bounds, and a function constructing two Views corrupted the host allocator and aborted at the second allocation. Add constructor_pushforward overloads for the rank-1 and rank-2 Kokkos 5 forms so the tangent is an independent, identically shaped View; the Kokkos 4 overload is kept, and higher runtime ranks would need analogous overloads.

Two supporting changes: Derive read FunctionDecl::getName() to special-case a few builtins on a declaration-only callee, which asserts on the non-identifier name of an operator -- reached via a KOKKOS_LAMBDA's operator(); take the name only when it is an identifier. And link the imported Kokkos::kokkos target in the unit tests so its cxx_std_20 usage requirement, which Kokkos 5 needs, propagates.

The rank-1, rank-2 and ViewAccess::f (compound-assignment) forward gradients match central finite differences. Tests: test/Regressions/DeclOnlyOperator.cpp, unittests/Kokkos/ViewCtorForward.cpp -- the latter runs once the KokkosTests target builds, which reverse-mode View gaps still block.

@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@vgvassilev
vgvassilev force-pushed the clad-kokkos5-forward branch from 506839f to b8ac4c3 Compare July 21, 2026 01:23
@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Kokkos 5 constructs a View from a label and its runtime extents through a
variadic constructor, so clad passes the label and written extents to
constructor_pushforward. The overload for the fixed eight-index Kokkos 4
constructor no longer matches, so the generic fallback built the tangent as
View(derived-label, derived-extent) == View("", 0) -- an extent-0 View. Its
element accesses were out of bounds, and a function constructing two Views
corrupted the host allocator and aborted at the second allocation. Add
constructor_pushforward overloads for the rank-1 and rank-2 Kokkos 5 forms so
the tangent is an independent, identically shaped View; the Kokkos 4 overload
is kept, and higher runtime ranks would need analogous overloads.

An all-static-extent View (every extent a compile-time template argument, e.g.
View<double[5]> or View<double[2][2][2][2][2][2][2]>) is constructed from a
label alone, so clad passes only the label -- which none of the extent-carrying
overloads accept. clad then fell back to a direct tangent construction that
flattens the label's implicit std::string temporary into a View braced-init,
View{"name", allocator}, for which no constructor exists. Add a label-only
constructor_pushforward so clad emits a proper pushforward call instead.

Two supporting changes: Derive read FunctionDecl::getName() to special-case a
few builtins on a declaration-only callee, which asserts on the non-identifier
name of an operator -- reached via a KOKKOS_LAMBDA's operator(); take the name
only when it is an identifier. And link the imported Kokkos::kokkos target in
the unit tests so its cxx_std_20 usage requirement, which Kokkos 5 needs,
propagates.

Provision the *-kokkos CI rows with Kokkos 5.1.1 from the ci-workflows
setup-kokkos action instead of libtrilinos-kokkos-dev (an unusable Trilinos 3.4
bundle). Reverse mode over a View is not yet supported on Kokkos 5 -- TBR
analysis asserts on a View access, and reverse over a View construction hits an
upstream clang cast<TagType> assertion -- so the reverse checks in ViewAccess
and ViewBasics are disabled with a note; the forward tangents stay and are
checked against finite differences (ViewCtorForward.cpp: rank-1, rank-2 and the
compound-assignment ViewAccess::f shape). DeclOnlyOperator.cpp covers the
getName change without Kokkos.

With the forward TUs compiling, the KokkosTests target now links as a whole,
which exposed an existing one-definition-rule violation: TestUtils.h defined the
non-inline free function parallel_polynomial_true_derivative, so every including
TU emitted a separate definition. Mark it inline.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vgvassilev
vgvassilev force-pushed the clad-kokkos5-forward branch from b8ac4c3 to aa4253b Compare July 21, 2026 12:33
@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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