Repository navigation
[CMake 6/7] Initialize CVODES adjoint quadrature derivative buffers - #3508
Conversation
bab8171 to
1c3b5cc
Compare
005153a to
77455ad
Compare
|
Thanks for tracking this down. Some observations, in case they help: It also hits the current CI. In #3446, which does not touch ODE code, On
With the The callback has been like this since v4.1.0. Example programSaved as #include <stan/math/rev.hpp>
#include <gtest/gtest.h>
#include <cmath>
#include <cstdio>
#include <vector>
// dy/dt = -a t y => y(T) = y0 exp(-a (T^2 - t0^2) / 2)
struct decay_rhs {
template <typename T_t, typename T_y, typename T_a>
Eigen::Matrix<stan::return_type_t<T_y, T_a>, -1, 1> operator()(
const T_t& t, const T_y& y, std::ostream* msgs, const T_a& a) const {
return -a * y * t;
}
};
TEST(cvodesAdjoint, quadrature_buffer) {
const double t0 = 0.5, T = 1.25, tol = 1e-10;
const Eigen::VectorXd y0 = Eigen::VectorXd::Constant(1, 0.75);
const Eigen::VectorXd atol = Eigen::VectorXd::Constant(1, tol);
stan::math::var a = 0.2;
auto y = stan::math::ode_adjoint_tol_ctl(
decay_rhs(), y0, t0, std::vector<double>{T}, tol, atol, tol, atol, tol,
tol, 1000000, 150, CV_HERMITE, CV_BDF, CV_ADAMS, nullptr, a);
y[0](0).grad();
const double c = -0.5 * (T * T - t0 * t0);
const double exact = c * y0(0) * std::exp(c * a.val());
std::printf("dy/da = %.17g, relative error %.2g\n", a.adj(),
std::abs(a.adj() / exact - 1));
} |
77455ad to
01c1ec9
Compare
01c1ec9 to
87c42dd
Compare
87c42dd to
3caa4e0
Compare
Clear the quadrature derivative output before accumulating adjoints. Cover analytic values and gradients across repeated solves and reverse passes.
3caa4e0 to
9bbd900
Compare
WardBrian
left a comment
There was a problem hiding this comment.
For the record, to get the test to actually fail without the patch I needed to run
for i in {1..100}; do MALLOC_PERTURB_=12 ./runTests.py -j12 test/unit/math/rev/functor/cos_ode_typed_test.cpp ; done
Jenkins Console Log Machine informationDistributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian Address sizes: 43 bits physical, 48 bits virtual CPU(s): 256 On-line CPU(s) list: 0-255 Thread(s) per core: 2 Core(s) per socket: 64 Socket(s): 2 NUMA node(s): 2 Vendor ID: AuthenticAMD CPU family: 23 Model: 49 Model name: AMD EPYC 7742 64-Core Processor Stepping: 0 Frequency boost: enabled CPU MHz: 1496.790 CPU max MHz: 3416.0681 CPU min MHz: 1500.0000 BogoMIPS: 4491.80 Virtualization: AMD-V L1d cache: 4 MiB L1i cache: 4 MiB L2 cache: 64 MiB L3 cache: 512 MiB NUMA node0 CPU(s): 0-63,128-191 NUMA node1 CPU(s): 64-127,192-255 Vulnerability Gather data sampling: Not affected Vulnerability Indirect target selection: Not affected Vulnerability Itlb multihit: Not affected Vulnerability L1tf: Not affected Vulnerability Mds: Not affected Vulnerability Meltdown: Not affected Vulnerability Mmio stale data: Not affected Vulnerability Old microcode: Not affected Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Mitigation; untrained return thunk; SMT enabled with STIBP protection Vulnerability Spec rstack overflow: Mitigation; Safe RET Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Retpolines; IBPB conditional; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Not affected Vulnerability Tsa: Not affected Vulnerability Tsx async abort: Not affected Vulnerability Vmscape: Mitigation; IBPB before exit to userspace Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sev sev_es G++: g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Clang: clang version 10.0.0-4ubuntu1 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin |
Summary
The adjoint cosine ODE tests fail on Windows x64 with incorrect gradients and on Windows ARM64 with CVodeB error -3 in the CMake CI run. CVODES reuses its quadrature derivative output buffer, but
accumulate_adjointsadds to that buffer with+=, retaining stale or uninitialized values between callbacks.Clear the output before accumulating derivatives. Add an analytic value/gradient regression covering scalar and vector parameters, repeated solves, and repeated reverse passes. This is a solver correctness fix reviewed separately from the CMake infrastructure; existing tolerances stay unchanged.
Tests
MALLOC_PERTURB_).-Wall -Werror; native Windows runtime validation remains for CI.git diff --checkpass.Side Effects
Adjoint quadrature derivatives no longer depend on previous output-buffer contents. Public APIs and solver tolerances are unchanged.
Release notes
Fix incorrect adjoint ODE parameter gradients and intermittent CVODES backward-integration failures caused by stale quadrature output.
Checklist
Related PRs
Native stack: https://github.com/stan-dev/math/stack/3509