Skip to content

Giles method to differentiate solvers and minor edits to docs - #11

Merged
sriharikrishna merged 7 commits into
Reference-LAPACK:mainfrom
Shreyas911:main
Sep 1, 2026
Merged

Giles method to differentiate solvers and minor edits to docs#11
sriharikrishna merged 7 commits into
Reference-LAPACK:mainfrom
Shreyas911:main

Conversation

@Shreyas911

@Shreyas911 Shreyas911 commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

What

  1. README fix: CMake-built Reference LAPACK produces libblas.a, not librefblas.a, which the Make/Meson builds expect by default. Documents the naming fix (symlink, BLAS_LIB override, or -Dlibblas=blas) and adds a -j parallel-build note.
  2. Black-box (Giles) differentiation for all eight triangular-solve BLAS routines, real and complex: DTRSM, STRSM, CTRSM, ZTRSM, DTRSV, STRSV, CTRSV, ZTRSV. Differentiates the defining relation (op(A)X=alpha*B / op(A)x=b) rather than the substitution algorithm, so each _d/_b/_dv/_bv is just calls to the routine's own undifferentiated BLAS.

Fix

Real case: standard Giles adjoint, _d via perturb-and-resolve, _b via transpose-solve on the seed + triangle-restricted outer product (DIAG='U' diagonal zeroed). _dv/_bv gather/scatter around Tapenade's direction-first array layout.

Complex case (Wirtinger/CR-calculus convention):

  • Adjoint solve uses A^H, not A^T. 'N'/'C' map directly; 'T' needs conj(A)Y=S ⟺ A·conj(Y)=conj(S).
  • Outer product conjugates the other operand, not the seed — 6 branches for TRSM (2 side × 3 transa), 2 for TRSV.
  • Bb = CONJG(ALPHA)*Yb (complex ALPHA in TRSM).
  • _d/_dv unaffected — passing the same trans flag to CTRMM/CTRMV handles conjugation for free.

Also fixes the pre-existing duplicate seed_array declaration in every scalar-mode test file (run_tapenade_blas.py output, declared after executable statements — illegal Fortran). Vector-mode tests unaffected.

Routine Precision BLAS calls Notes
DTRSM double DTRSM, DTRMM baseline
STRSM single STRSM, STRMM precision swap
CTRSM complex CTRSM, CTRMM complex ALPHA, 3-way transa
ZTRSM dcomplex ZTRSM, ZTRMM precision swap
DTRSV double DTRSV, DTRMV no alpha, vector outer product, arbitrary INCX
STRSV single STRSV, STRMV precision swap
CTRSV complex CTRSV, CTRMV 3-way trans, arbitrary INCX
ZTRSV dcomplex ZTRSV, ZTRMV precision swap

Testing

README/library naming:

  • symlink + make
  • make BLAS_LIB=...
  • Meson (-Dlibblas=blas)

Giles routines, FD/VJP (complex checks perturb Re+Im, use conj(·)·(·) inner product):

Routine _d _b _dv _bv
DTRSM/STRSM/CTRSM/ZTRSM
DTRSV/STRSV/CTRSV/ZTRSV

All 32 combinations pass.

Notes for reviewers

  • run_tapenade_blas.py rewrites top-level build files on every invocation regardless of --file/--mode — separate issue if useful.

Reference LAPACK's CMake build produces libblas.a/liblapack.a rather
than librefblas.a, which the Make and Meson builds expect by default.
Document the symlink/override workarounds.

Tested (Make path only; Meson not installed locally):
- Make + symlink (ln -s libblas.a librefblas.a): build succeeds
- Make + BLAS_LIB override (BLAS_LIB="-L$LAPACKDIR -lblas"): build succeeds
- Meson + -Dlibblas=blas -Dlibblas_path=$LAPACKDIR: build succeeds
@Shreyas911 Shreyas911 changed the title docs: clarify BLAS library naming for CMake-built LAPACK Giles method to differentiate solvers and minor edits to docs Aug 2, 2026
@sriharikrishna sriharikrishna self-assigned this Aug 31, 2026
@sriharikrishna
sriharikrishna self-requested a review August 31, 2026 18:56
Correct and harden the black-box (Giles) derivatives of the eight
triangular-solve routines (d/s/c/z × trsm/trsv) across all four modes.

Source (BLAS/src):
- Wrap fixed-form lines exceeding column 72 that broke compilation with
  gfortran defaults (strsm_b.f and the *trsm_bv.f IF/expression lines).
- Forward/vector-forward: pass DIAG='N' (not the caller's DIAG) to the
  inner TRMM/TRMV. For a unit-triangular A the diagonal is constant, so
  its derivative is zero; forwarding DIAG='U' added a spurious +X and
  gave wrong tangents for every DIAG='U' case (adjoint already skipped
  the diagonal, so this restores forward/reverse consistency).
- Vector modes (_dv/_bv): dimension the direction arrays by the runtime
  nbdirs and drop the DIFFSIZES.inc/nbdirsmax cap, matching the
  hand-modified dgemv_dv/dgemv_bv convention (nbdirs no longer limited
  to 4).
- Remove the commented-out Tapenade-generated code from the hand-written
  Giles sources.

Tests (BLAS/test):
- Rewrite the trs test drivers following the gemv/gemv_vector structure:
  multi-size, sweep DIAG in {N,U}. Forward uses full-complex central
  differences (holomorphic for TRANS='N'); reverse uses the real-part
  Hermitian VJP identity. Verified against finite differences for all
  eight routines in all four modes.

Build:
- BLAS/meson.build: fold the trsm/trsv sources into the per-mode file
  lists in alphabetical order instead of a trailing catch-all block.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@sriharikrishna
sriharikrishna merged commit fcb3afa into Reference-LAPACK:main Sep 1, 2026
10 checks passed
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