Triangular helpers without type-preserving conversion

upper_triangular and lower_triangular now use triu and tril, so Adjoint Cholesky factors on Julia 1.13 no longer raise a MethodError.

Fix triangular helpers for adjoint Cholesky factors on Julia 1.13
Revisions
Base
2d1580a926b902146a8a5e6b508cabba28eb5c76
Head
f4f30f5b0b35ca4069f349804fe7624caa2868f3
PR checked
2026-11-11T19:57:27.808Z
Agent
Claude Code cloud
Requested model
sonnet
Requested effort
medium
Cloud session
Open in Claude
Reported models
Unreported

Reviewed snapshot. Validation checks structure, not call paths.

Overview

Recommendation: Approve

No blocking issue found in the reviewed source.

Bijectors.jl provides bijections between constrained and unconstrained spaces. CorrBijector and VecCorrBijector map correlation matrices using a Cholesky factor.

On Julia 1.13 the upper factor of a Cholesky object stored as the lower triangle is UpperTriangular(C.factors'), so its parent is an Adjoint. The old upper_triangular tried to convert that matrix back to an Adjoint and threw a MethodError. Mooncake forward mode always stores the lower triangle, so correlation bijectors fail under it. This PR changes the two helpers to triu and tril.1, 8

Affected users: Julia 1.13 users of CorrBijector, VecCorrBijector or cholesky_upper, mostly with Mooncake. The helpers are internal functions, not documented API.

Example

Input from the PR description

A = [2.0 1; 1 2]
CL = cholesky(Hermitian(A, :L))
Bijectors.upper_triangular(parent(CL.U))

Result on Julia 1.13 (from the PR description)

Before

MethodError: the Adjoint parent cannot be produced by convert(typeof(A), UpperTriangular(A)).

After

A Matrix with the upper triangle of the factor. The new test expects it to equal Matrix(CL.U).

This comparison comes from the PR description and source reading. It was not run here.1, 7

Changes

Files in the PR

FileChange
src/utils.jllower_triangular is tril(A) and upper_triangular is triu(A).
test/bijectors/pd.jlNew testset compares each helper on an Adjoint factor with the Matrix of the factor.
HISTORY.mdEntry for the MethodError fix under 0.16.4.

Helper definitions

The PR's change to src/utils.jl.

PR change | source-checked
src/utils.jl | snippet lines

Removed lines start with minus. Added lines start with plus.
@@ -1,2 +1,2 @@
1-lower_triangular(A::AbstractMatrix) = convert(typeof(A), LowerTriangular(A))
2-upper_triangular(A::AbstractMatrix) = convert(typeof(A), UpperTriangular(A))
\ No newline at end of file
1+lower_triangular(A::AbstractMatrix) = tril(A)
2+upper_triangular(A::AbstractMatrix) = triu(A)
\ No newline at end of file
Copyable text

Check file context before applying this snippet patch.

2, 3

Design

Who calls the triangular helpers

Read left to right: functions in src/utils.jl and the ReverseDiff extension call the two helpers, which now call tril and triu. The scope is src/utils.jl and the ReverseDiff extension. Other callers were not searched in other extensions.

Select a node for its meaning and source.

Added (+) | Changed (~) | Removed (-) | Existing
Changes describe this scoped graph and its linked declarations. Scope: Same helpers at base. The two helpers call convert with LowerTriangular or UpperTriangular instead of tril and triu.

Loading diagram. Text details are below.

Labels identify types, functions, methods, macros, values and groups. Dashed arrows are uncertain.
Drag to pan. Ctrl + scroll to zoom. Keyboard: arrows, +, -, 0.

Nodes and relationships

function: cholesky_lower

Returns the lower Cholesky factor as a Matrix.

cholesky_lower(X::AbstractMatrix)
Sources (1)

function: cholesky_upper

Returns the upper Cholesky factor as a Matrix. Fails on Julia 1.13 at base when the factor is stored lower.

cholesky_upper(X::AbstractMatrix)
Sources (1)

group: vec_to_triu and vec_to_triu1

Group of two functions that fill an upper triangle from a vector and then call upper_triangular. The group implies no call between its members.

Sources (1)

group: pd_from_lower and pd_from_upper

Group of two helpers that call lower_triangular and upper_triangular before forming a product.

Sources (1)

method: lower_triangular

Changed: now tril(A).

lower_triangular(A::AbstractMatrix)
Sources (2)

method: upper_triangular

Changed: now triu(A).

upper_triangular(A::AbstractMatrix)
Sources (2)

function: tril and triu

Standard library functions. They return a new Matrix for an Adjoint input.

Sources (1)

group: ReverseDiff overrides

Overrides for TrackedMatrix that call the helpers on the underlying values.

Sources (1)

cholesky-lower calls lower-triangular (source). Passes parent(cholesky(...).L).

Sources (1)

cholesky-upper calls upper-triangular (source). Passes parent(cholesky(...).U).

Sources (1)

vec-to-triu calls upper-triangular (source). Passes a Matrix built by update_triu_from_vec.

Sources (1)

pd-from calls lower-triangular (source). pd_from_lower calls lower_triangular. pd_from_upper calls upper_triangular.

Sources (1)

lower-triangular calls tril-triu (source). Calls tril at head. At base it called convert(typeof(A), LowerTriangular(A)).

Sources (2)

upper-triangular calls tril-triu (source). Calls triu at head. At base it called convert(typeof(A), UpperTriangular(A)).

Sources (2)

rd-ext calls lower-triangular (source). The @grad bodies call the helpers on plain values, so they reach tril and triu. Not executed here.

Sources (1)
Generated Mermaid

Benefits and costs

AspectAssessment
Benefittril and triu return a plain Matrix for any AbstractMatrix input, including an Adjoint. This removes the failing conversion.
CostThe result type is no longer typeof(A). For a Matrix input nothing changes. For other input types the output may differ. The callers read here pass Matrix values.
ADReverseDiff overrides still intercept TrackedMatrix. Other backends now differentiate through tril and triu instead of convert and LowerTriangular. This was not checked.

Findings

unverified

Runtime behavior is not reproduced

The MethodError and the fix rest on the PR description and on source reading. No Julia 1.13 run was done in this review, per the source-only rule.

1, 8
suggestion

The regression test passes trivially before Julia 1.13

On earlier Julia versions parent(CL.U) is already a Matrix, so the test exercises the old code path without failing. It guards the bug only on 1.13 and later. The test also calls the helpers directly and does not call cholesky_upper or cholesky_lower on a lower-stored factor. A direct call of cholesky_upper with a Hermitian lower input would cover the reported path. This is a nonblocking suggestion.

7, 4
question

AD through tril and triu with Mooncake and others

The failure was reported under Mooncake forward mode. After the change those backends differentiate tril and triu. The PR says the Mooncake integration cases fail on 1.13 in CI. Does CI on this head show those cases passing? The check was not made here.

1, 6
question

Output type of the helpers

Dropping typeof(A) changes the output type for inputs that are not Matrix. Only Matrix inputs were found at the callers read here, so no change is expected. A note in the comment above the helpers would record that the result is a Matrix.

3, 5
unverified

Review scope

Cloud source review. Package-test results were not verified.

Linked work: public repositories only, depth 2, at most 15 items. Read status describes collection, not semantic review.

Checks

passed

Source reading of the changed helpers and callers

Read src/utils.jl, the ReverseDiff overrides and the new test at the head commit. The change matches the description.

3, 4, 5, 6, 7
unverified

Package tests and Julia 1.13 behavior

Not run. This review inspected source only.

Definitions and linked work

Definitions

Two helper methods changed. Test code lives in unexpanded macros.

2 changed declarations | 2 in diagrams | 0 omitted with reasons | 0 unassigned
0 added | 2 changed | 0 removed
Scope: . | 2 changed files

Collection notes (15)

Parser: Julia 1.10.13 Meta.parseall.

Written declarations are compared without loading the project. Macros, generated code and quoted code need separate checks. Signature edits and moves may appear as removal and addition.

test/bijectors/pd.jl:4 Macro @testset was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:16 Macro @testset was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:30 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:31 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:34 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:35 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:4 Macro @testset was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:16 Macro @testset was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:30 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:31 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:34 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:35 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:39 Macro @testset was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:42 Macro @test was not expanded. Only written declarations are listed.

test/bijectors/pd.jl:44 Macro @test was not expanded. Only written declarations are listed.

lower_triangularmethod | changed | in diagrams

Base

lower_triangular(A::AbstractMatrix)

Head

lower_triangular(A::AbstractMatrix)

Who calls the triangular helpers / lower_triangular

upper_triangularmethod | changed | in diagrams

Base

upper_triangular(A::AbstractMatrix)

Head

upper_triangular(A::AbstractMatrix)

Who calls the triangular helpers / upper_triangular

Linked issues and pull requests

Discovery paths. Explanations need review.

11 of 35 items read | Incomplete | Explanations need review
Checked 2026-11-11T19:57:27.806Z

Scope and limits

Bodies, paginated comments, submitted/inline reviews and incoming timeline references. Mentions may be quoted or incidental.

Depth: 2. Read budget: 15.

Only three items have explanations. The rest show collection status only and were not reviewed.

All items and links

Fix triangular helpers for adjoint Cholesky factors on Julia 1.13

pr | open | read

The PR under review.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`)

pr | merged | read

Upstream change that makes Cholesky factors non-copying. The PR cites it as the cause. Read through the PR description only.

Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later

pr | merged | read

Merged base PR that this PR is stacked on. Not reviewed here.

Avoid allocation when accessing `cholesky` factors (.L, .U)

pr | closed | read

Needs an explanation after review.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves

pr | merged | read

Needs an explanation after review.

Eventually retire `chol_lower` and `chol_upper`

issue | open | read

Needs an explanation after review.

Fix doctest

pr | closed | read

Needs an explanation after review.

Backports release 1.12

pr | merged | read

Needs an explanation after review.

Improve Cholesky type inference without copying

issue | closed | read

Needs an explanation after review.

Fix argument handling in `@grad_from_chainrules`

pr | merged | read

Needs an explanation after review.

Request a PR review

issue | open | read

Needs an explanation after review.

JuliaLang/julia#38389

unknown | unknown | unread

Not read.

Depth limit.

JuliaStats/PDMats.jl#143

issue | unknown | unread

Not read.

Depth limit.

JuliaGaussianProcesses/ParameterHandling.jl#39

pr | unknown | unread

Not read.

Depth limit.

FluxML/Zygote.jl#1117

pr | unknown | unread

Not read.

Depth limit.

JuliaLang/julia#57509

pr | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1239

pr | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1405

issue | unknown | unread

Not read.

Depth limit.

EnzymeAD/Enzyme.jl#2813

issue | unknown | unread

Not read.

Depth limit.

JuliaLang/julia#57347

pr | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1207

unknown | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1196

unknown | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1202

unknown | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1203

unknown | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1210

unknown | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1214

unknown | unknown | unread

Not read.

Depth limit.

JuliaLang/LinearAlgebra.jl#1237

unknown | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#314

unknown | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#315

unknown | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#319

unknown | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#320

unknown | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#316

unknown | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#318

unknown | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#331

pr | unknown | unread

Not read.

Depth limit.

JuliaDiff/ReverseDiff.jl#330

pr | unknown | unread

Not read.

Depth limit.

Fix triangular helpers for adjoint Cholesky factors on Julia 1.13 mentions remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Reference in body.

Fix triangular helpers for adjoint Cholesky factors on Julia 1.13 mentions Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later. Collected reference.

Reference in body.

Fix triangular helpers for adjoint Cholesky factors on Julia 1.13 mentions Fix triangular helpers for adjoint Cholesky factors on Julia 1.13. Collected reference.

Reference in comments.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) mentions Avoid allocation when accessing `cholesky` factors (.L, .U). Collected reference.

Reference in body.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) mentions remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Reference in comments.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) mentions Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves. Collected reference.

Reference in comments.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) referenced by Eventually retire `chol_lower` and `chol_upper`. Collected reference.

Timeline cross-reference.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) referenced by Fix doctest. Collected reference.

Timeline cross-reference.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) referenced by Backports release 1.12. Collected reference.

Timeline cross-reference.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) referenced by Improve Cholesky type inference without copying. Collected reference.

Timeline cross-reference.

remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) referenced by Fix triangular helpers for adjoint Cholesky factors on Julia 1.13. Collected reference.

Timeline cross-reference.

Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later mentions Fix argument handling in `@grad_from_chainrules`. Collected reference.

Reference in body.

Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later mentions Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later. Collected reference.

Reference in comments.

Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later referenced by Fix triangular helpers for adjoint Cholesky factors on Julia 1.13. Collected reference.

Timeline cross-reference.

Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later referenced by Request a PR review. Collected reference.

Timeline cross-reference.

Avoid allocation when accessing `cholesky` factors (.L, .U) mentions JuliaLang/julia#38389. Collected reference.

Reference in comments.

Avoid allocation when accessing `cholesky` factors (.L, .U) mentions JuliaStats/PDMats.jl#143. Collected reference.

Reference in comments.

Avoid allocation when accessing `cholesky` factors (.L, .U) referenced by JuliaStats/PDMats.jl#143. Collected reference.

Timeline cross-reference.

Avoid allocation when accessing `cholesky` factors (.L, .U) referenced by JuliaGaussianProcesses/ParameterHandling.jl#39. Collected reference.

Timeline cross-reference.

Avoid allocation when accessing `cholesky` factors (.L, .U) referenced by FluxML/Zygote.jl#1117. Collected reference.

Timeline cross-reference.

Avoid allocation when accessing `cholesky` factors (.L, .U) referenced by remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Timeline cross-reference.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves mentions Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves. Collected reference.

Reference in comments.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves referenced by remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Timeline cross-reference.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves referenced by Backports release 1.12. Collected reference.

Timeline cross-reference.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves referenced by JuliaLang/julia#57509. Collected reference.

Timeline cross-reference.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves referenced by JuliaLang/LinearAlgebra.jl#1239. Collected reference.

Timeline cross-reference.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves referenced by JuliaLang/LinearAlgebra.jl#1405. Collected reference.

Timeline cross-reference.

Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves referenced by EnzymeAD/Enzyme.jl#2813. Collected reference.

Timeline cross-reference.

Eventually retire `chol_lower` and `chol_upper` mentions remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Reference in body.

Fix doctest mentions Fix doctest. Collected reference.

Reference in comments.

Fix doctest mentions remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Reference in comments.

Reference in comments.

Fix doctest referenced by JuliaLang/julia#57347. Collected reference.

Timeline cross-reference.

Backports release 1.12 mentions Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves. Collected reference.

Reference in body.

Reference in comments.

Backports release 1.12 mentions JuliaLang/LinearAlgebra.jl#1207. Collected reference.

Reference in body.

Reference in comments.

Backports release 1.12 mentions JuliaLang/LinearAlgebra.jl#1196. Collected reference.

Reference in body.

Backports release 1.12 mentions JuliaLang/LinearAlgebra.jl#1202. Collected reference.

Reference in body.

Backports release 1.12 mentions JuliaLang/LinearAlgebra.jl#1203. Collected reference.

Reference in body.

Backports release 1.12 mentions JuliaLang/LinearAlgebra.jl#1210. Collected reference.

Reference in body.

Backports release 1.12 mentions JuliaLang/LinearAlgebra.jl#1214. Collected reference.

Reference in body.

Reference in comments.

Backports release 1.12 mentions remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Reference in comments.

Reference in comments.

Backports release 1.12 mentions Backports release 1.12. Collected reference.

Reference in comments.

Backports release 1.12 mentions JuliaLang/LinearAlgebra.jl#1237. Collected reference.

Reference in comments.

Improve Cholesky type inference without copying mentions remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`). Collected reference.

Reference in comments.

Fix argument handling in `@grad_from_chainrules` mentions JuliaDiff/ReverseDiff.jl#314. Collected reference.

Reference in body.

Fix argument handling in `@grad_from_chainrules` mentions JuliaDiff/ReverseDiff.jl#315. Collected reference.

Reference in body.

Fix argument handling in `@grad_from_chainrules` mentions JuliaDiff/ReverseDiff.jl#319. Collected reference.

Reference in body.

Fix argument handling in `@grad_from_chainrules` mentions JuliaDiff/ReverseDiff.jl#320. Collected reference.

Reference in body.

Fix argument handling in `@grad_from_chainrules` mentions JuliaDiff/ReverseDiff.jl#316. Collected reference.

Reference in body.

Fix argument handling in `@grad_from_chainrules` mentions JuliaDiff/ReverseDiff.jl#318. Collected reference.

Reference in body.

Fix argument handling in `@grad_from_chainrules` mentions Fix argument handling in `@grad_from_chainrules`. Collected reference.

Reference in comments.

Fix argument handling in `@grad_from_chainrules` referenced by JuliaDiff/ReverseDiff.jl#331. Collected reference.

Timeline cross-reference.

Fix argument handling in `@grad_from_chainrules` referenced by Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later. Collected reference.

Timeline cross-reference.

Fix argument handling in `@grad_from_chainrules` referenced by JuliaDiff/ReverseDiff.jl#330. Collected reference.

Timeline cross-reference.

Request a PR review mentions Fix the ReverseDiff extension for ReverseDiff 1.18.4 and later. Collected reference.

Reference in comments.

Sources