Skip to content

Fix NaN and over/underflow in the HMM functions for long sequences and large log densities (#2677) - #3440

Open
atarutin wants to merge 5 commits into
stan-dev:developfrom
atarutin:bugfix/issue-2677-hmm-hidden-state-prob-underflow
Open

atarutin wants to merge 5 commits into
stan-dev:developfrom
atarutin:bugfix/issue-2677-hmm-hidden-state-prob-underflow

Conversation

@atarutin

@atarutin atarutin commented Oct 9, 2026 •

Copy link
Copy Markdown

Fixes #2677.

The fix covers the two causes described below. It has been checked with the tests in this PR. The CmdStan reproducer in the issue comment was last run at 6a70a96, by the review bot, and gave no NaN. It has not been run on the model and data attached in this comment or on the forum thread linked from the issue.

This was assisted by Claude. @SteveBronder wrote the changes to hmm_latent_rng, hmm_marginal and hmm_check.

Summary

hmm_hidden_state_prob returns NaN in two situations, both described in #2677:

  1. Long sequences. The forward pass rescaled only alphas.col(0). Every later column is the product of omegas and the previous column, so it shrinks like the joint density p(y_1, ..., y_n). Once that falls below the smallest double (a log density of about -745), the column is exactly 0, and the normalization alphas.col(n) /= alphas.col(n).sum() computes 0/0. In the reproducer (2 states, Gaussian observations) the first NaN appears at t = 472, for every T beyond that.
  2. Log densities of large magnitude. omegas = exp(log_omegas) underflows to 0 for entries below about -745 and overflows to inf above about 709. This is what @charlesm93 found in 2022 with log_omegas at 1000 or -1000.

hmm_latent_rng and hmm_marginal already rescale their forward passes, so they don't have the first problem, but they do exponentiate log_omegas directly and have the second.

The fix:

  • Forward pass in hmm_hidden_state_prob: each column is now divided by its maximum, as the backward pass in this function already does and as the forward passes of hmm_latent_rng and hmm_marginal already do. The hidden state probabilities are invariant to a positive factor on each column of alphas, so unlike hmm_marginal the scale does not need to be kept.
  • All three functions: each column of log_omegas is shifted by its maximum before it is exponentiated.
    • In hmm_hidden_state_prob and hmm_latent_rng the probabilities are invariant to the shift.
    • hmm_marginal adds the shifts back to the log density. Its gradients are unchanged because the shift cancels in the adjoint.
  • hmm_check: it now rejects a log_omegas with no columns, which otherwise indexes alphas.col(0) out of bounds. hmm_hidden_state_prob now calls hmm_check before taking the column maxima.
  • Docs: the docblocks of hmm_hidden_state_prob and hmm_latent_rng described log_omegas transposed and are corrected.

The backward pass is unchanged. The cost stays at O(K^2) per step, with no transcendental functions in the loop. A recursion fully on the log scale (with log_sum_exp) would also work, but it needs K^2 exp/log calls per step, and the rescaled form matches the existing HMM functions.

@charlesm93 started bugfix/issue-2677-hmm_hidden_state in 2022. It has one commit, e7a5efa, which adds the test hidden_state_prob1 (log_omegas = 1000, Gamma sending every state to state 1) and no fix. The branch is 3,572 commits behind develop, so this PR is opened from a fresh branch, and that test is included here with its original name and assertions.

Tests

New tests, plus a reference implementation in hmm_hidden_state_prob_test.cpp: hmm_hidden_state_prob_log_space, which runs forward-backward entirely on the log scale with log_sum_exp.

hmm_hidden_state_prob_test.cpp:

  • hidden_state_long_sequence: simulates a 2-state Gaussian HMM with 5000 observations (Gamma = [[0.95, 0.05], [0.10, 0.90]], means +1/-1, sd 1, boost::random::mt19937 seeded at 2677). It checks that hmm_marginal is below -2000, so the old forward pass must underflow. It then asserts that every output is finite, that every column sums to 1 within 1e-12, and that the output matches the log scale reference within 1e-10.
  • hidden_state_very_negative_log_omegas: subtracts 1000 + 10 n from column n of the fixture's log_omegas, so every entry is below -1000. It asserts that the output is finite, equals the output for the unshifted log_omegas within 1e-12, and matches the log scale reference within 1e-10. The reference tolerance is looser because a log scale computation loses about machine epsilon times the magnitude of the log densities.
  • hidden_state_prob1: taken from @charlesm93's branch (log_omegas = 1000, overflow).
  • hidden_state_neg_inf_log_omega: a single -inf entry gives probability 0 for that state, with a finite result that matches the log scale reference within 1e-12.
  • hidden_state_no_states_throws and hidden_state_no_observations_throws: empty log_omegas throw.

hmm_latent_rng_test.cpp:

  • latent_rng_shifted_log_omegas: with shifts of -1000 and 1000 per column (plus 10 n) and the same seed, 100 draws match the draws for the unshifted log_omegas.
  • latent_rng_no_observations_throws: a log_omegas with no columns throws.

hmm_marginal_test.cpp:

  • shifted_log_omegas: with the same shifts, the log density changes by the total shift and the gradients match the unshifted ones within 1e-10.
  • exceptions: gains a case for a log_omegas with no columns.

hmm_check_test.cpp:

  • hmm_check: gains the same case.

Results of the four test files at 2a468d6, and at the merge base e4745c8 with the same test files copied in (g++ 16.2.1, O=0):

  • At 2a468d6: all 19 tests pass.
  • At the merge base, failing: hidden_state_long_sequence, hidden_state_very_negative_log_omegas, hidden_state_prob1, latent_rng_shifted_log_omegas, shifted_log_omegas and hmm_check.
  • At the merge base, aborting on an Eigen assert: hidden_state_no_observations_throws, latent_rng_no_observations_throws and exceptions, all on a log_omegas with no columns.
  • At the merge base, passing: the rest, including the new hidden_state_neg_inf_log_omega and hidden_state_no_states_throws, which guard against regressions from the shift.

CI is green at 2a468d6.

Side Effects

Long sequences and log densities of large magnitude, which used to give NaN from hmm_hidden_state_prob, now give finite probabilities. The large-magnitude case is also handled in hmm_latent_rng and hmm_marginal.

The result of hmm_hidden_state_prob can still be NaN in two cases:

  • A column of log_omegas is entirely -inf.
  • The largest density in a column belongs to a state the chain cannot reach, and the log densities of the reachable states are more than about 745 below it.

For inputs that already gave finite results, the shift and the rescaling do not change the output in exact arithmetic, so any difference is rounding. The existing tests pass unchanged, but no test compares the output directly with develop's.

A log_omegas with no columns now throws std::invalid_argument from all three functions, through hmm_check. Before, it hit an Eigen assert, or read out of bounds with asserts disabled.

Release notes

hmm_hidden_state_prob no longer returns NaN for long sequences or for log densities of large magnitude. hmm_latent_rng and hmm_marginal no longer underflow or overflow for log densities of large magnitude. All three now throw std::invalid_argument if log_omegas has no columns.

Checklist

  • Copyright holder: Aleksandr Tarutin

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit): the four HMM test files were run locally at 2a468d6, not all of test/unit
    • header checks pass, (make test-headers): not re-run locally since the latest commits
    • dependencies checks pass, (make test-math-dependencies): not run locally
    • docs build, (make doxygen): not run locally
    • code passes the built in C++ standards checks (make cpplint): not re-run locally since the latest commits
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

🤖 Generated with Claude Code

The forward pass rescaled only its first column, so on a long sequence
the forward probabilities underflowed to 0 and the final normalization
computed 0/0. Rescale every forward step, as hmm_latent_rng and
hmm_marginal already do. The hidden state probabilities are invariant to
a positive factor on each column, so the scale does not need to be kept.

log_omegas was also exponentiated directly, so log densities below about
-745 underflowed to 0 and large ones overflowed to inf. Subtract the
maximum of each column before exponentiating; the hidden state
probabilities are invariant to that shift as well.

The test hidden_state_prob1 is taken from Charles Margossian's branch
bugfix/issue-2677-hmm_hidden_state (e7a5efa).

@atarutin atarutin left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable.

@atarutin
atarutin marked this pull request as ready for review October 9, 2026 21:39
@stan-buildbot

Copy link
Copy Markdown
Contributor
Name Old Result New Result Ratio Performance change( 1 - new / old )
stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan 0.04 0.04 0.98 -2.4% slower
stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan 0.16 0.16 1.01 0.7% faster
stat_comp_benchmarks/benchmarks/garch/garch.stan 0.85 0.86 0.99 -0.61% slower
stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.98 -2.37% slower
stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan 0.11 0.11 1.02 1.49% faster
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan 8.07 8.03 1.0 0.43% faster
stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan 3.74 3.72 1.0 0.35% faster
stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan 32.41 33.31 0.97 -2.76% slower
stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan 0.38 0.38 1.0 0.07% faster
stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan 6.93 6.93 1.0 0.01% faster
stat_comp_benchmarks/benchmarks/sir/sir.stan 137.32 135.94 1.01 1.01% faster
stat_comp_benchmarks/benchmarks/arK/arK.stan 3.23 3.2 1.01 0.87% faster
stat_comp_benchmarks/benchmarks/arma/arma.stan 0.6 0.61 1.0 -0.4% slower
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 25.71 25.84 0.99 -0.51% slower
performance.compilation 401.03 383.09 1.05 4.47% faster
Mean result: 1.0005338929591159

Jenkins Console Log
Jenkins Build Stages
Commit hash: 44a50912ff3f2e71023ea29a6f91adf7c6b1cf87

Machine information
Distributor 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:                           52 bits physical, 57 bits virtual
CPU(s):                                  192
On-line CPU(s) list:                     0-191
Thread(s) per core:                      2
Core(s) per socket:                      48
Socket(s):                               2
NUMA node(s):                            2
Vendor ID:                               AuthenticAMD
CPU family:                              25
Model:                                   17
Model name:                              AMD EPYC 9474F 48-Core Processor
Stepping:                                1
Frequency boost:                         enabled
CPU MHz:                                 1496.778
CPU max MHz:                             4114.4229
CPU min MHz:                             1500.0000
BogoMIPS:                                7189.39
Virtualization:                          AMD-V
L1d cache:                               3 MiB
L1i cache:                               3 MiB
L2 cache:                                96 MiB
L3 cache:                                512 MiB
NUMA node0 CPU(s):                       0-47,96-143
NUMA node1 CPU(s):                       48-95,144-191
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:                  Not affected
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; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Mitigation; Clear CPU buffers
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 amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid 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 perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap

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

@stan-buildbot

Copy link
Copy Markdown
Contributor
Name Old Result New Result Ratio Performance change( 1 - new / old )
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.4 8.11 1.04 3.42% faster
stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan 2.18 2.14 1.02 1.88% faster
stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan 0.06 0.06 1.05 4.46% faster
stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan 4.26 4.15 1.03 2.53% faster
stat_comp_benchmarks/benchmarks/garch/garch.stan 0.43 0.42 1.04 3.45% faster
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan 2.71 2.63 1.03 2.75% faster
stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan 0.24 0.24 1.02 2.2% faster
stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan 19.09 18.58 1.03 2.66% faster
stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan 0.02 0.02 1.01 1.2% faster
stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan 0.1 0.1 1.0 0.05% faster
stat_comp_benchmarks/benchmarks/sir/sir.stan 69.86 66.52 1.05 4.79% faster
stat_comp_benchmarks/benchmarks/arK/arK.stan 1.57 1.53 1.02 2.28% faster
stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.98 -2.19% slower
stat_comp_benchmarks/benchmarks/arma/arma.stan 0.28 0.28 1.02 2.21% faster
performance.compilation 249.13 228.44 1.09 8.3% faster
Mean result: 1.027929827856697

Jenkins Console Log
Jenkins Build Stages
Commit hash: 017f091b63e5f89b01de871d2a7ead2d78127331

Machine information
Distributor 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:                           46 bits physical, 48 bits virtual
CPU(s):                                  80
On-line CPU(s) list:                     0-79
Thread(s) per core:                      2
Core(s) per socket:                      20
Socket(s):                               2
NUMA node(s):                            2
Vendor ID:                               GenuineIntel
CPU family:                              6
Model:                                   85
Model name:                              Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz
Stepping:                                7
CPU MHz:                                 999.859
CPU max MHz:                             3900.0000
CPU min MHz:                             1000.0000
BogoMIPS:                                5000.00
Virtualization:                          VT-x
L1d cache:                               1.3 MiB
L1i cache:                               1.3 MiB
L2 cache:                                40 MiB
L3 cache:                                55 MiB
NUMA node0 CPU(s):                       0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78
NUMA node1 CPU(s):                       1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79
Vulnerability Gather data sampling:      Mitigation; Microcode
Vulnerability Indirect target selection: Mitigation; Aligned branch/return thunks
Vulnerability Itlb multihit:             KVM: Mitigation: Split huge pages
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Old microcode:             Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow:      Not affected
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; Enhanced / Automatic IBRS; IBPB conditional; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Mitigation; TSX disabled
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 dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req vnmi pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

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

@avehtari avehtari added the numerics Numerical issues label Oct 16, 2026
@stan-buildbot

Copy link
Copy Markdown
Contributor
Name Old Result New Result Ratio Performance change( 1 - new / old )
stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan 0.04 0.04 0.99 -1.21% slower
stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan 0.17 0.16 1.03 2.52% faster
stat_comp_benchmarks/benchmarks/garch/garch.stan 0.86 0.87 0.99 -0.95% slower
stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 1.02 2.24% faster
stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan 0.1 0.11 0.98 -2.18% slower
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan 8.2 8.03 1.02 2.05% faster
stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan 3.71 3.77 0.98 -1.68% slower
stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan 32.96 32.84 1.0 0.35% faster
stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan 0.38 0.39 0.99 -0.61% slower
stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan 6.85 6.82 1.01 0.52% faster
stat_comp_benchmarks/benchmarks/sir/sir.stan 167.1 167.01 1.0 0.06% faster
stat_comp_benchmarks/benchmarks/arK/arK.stan 3.21 3.23 0.99 -0.6% slower
stat_comp_benchmarks/benchmarks/arma/arma.stan 0.6 0.61 0.99 -0.93% slower
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 25.75 25.94 0.99 -0.77% slower
performance.compilation 393.64 382.2 1.03 2.91% faster
Mean result: 1.00139093156814

Jenkins Console Log
Jenkins Build Stages
Commit hash: a2236819d562837e97a9bfe97dc9ebd6d8613729

Machine information
Distributor 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:                           52 bits physical, 57 bits virtual
CPU(s):                                  192
On-line CPU(s) list:                     0-191
Thread(s) per core:                      2
Core(s) per socket:                      48
Socket(s):                               2
NUMA node(s):                            2
Vendor ID:                               AuthenticAMD
CPU family:                              25
Model:                                   17
Model name:                              AMD EPYC 9474F 48-Core Processor
Stepping:                                1
Frequency boost:                         enabled
CPU MHz:                                 1496.910
CPU max MHz:                             4114.4229
CPU min MHz:                             1500.0000
BogoMIPS:                                7189.39
Virtualization:                          AMD-V
L1d cache:                               3 MiB
L1i cache:                               3 MiB
L2 cache:                                96 MiB
L3 cache:                                512 MiB
NUMA node0 CPU(s):                       0-47,96-143
NUMA node1 CPU(s):                       48-95,144-191
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:                  Not affected
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; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Mitigation; Clear CPU buffers
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 amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid 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 perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap

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

@stevieb-bot

This comment was marked as resolved.

hmm_latent_rng and hmm_marginal exponentiated log_omegas directly, so
log densities below about -745 underflowed to 0 and large ones overflowed
to inf, as in hmm_hidden_state_prob before. Subtract the maximum of each
column before exponentiating. The sampling probabilities are invariant to
the shift. hmm_marginal adds the shifts back to the log density, and its
gradients are unchanged because the shift cancels in the adjoint.

In hmm_hidden_state_prob, call hmm_check before taking the column maxima,
so that a log_omegas with no rows throws instead of hitting an Eigen
assert.

Fix the docblocks of hmm_hidden_state_prob and hmm_latent_rng, which
described log_omegas transposed, and state that the result can still be
NaN when the largest density in a column belongs to a state the chain
cannot reach.

The loops in four hidden state tests skipped the last column.
hmm_check did not check that log_omegas has at least one column. With no
columns, n_transitions is -1 and hmm_hidden_state_prob, hmm_latent_rng
and hmm_marginal index alphas.col(0) out of bounds. Throw
std::invalid_argument instead.
@stan-buildbot

Copy link
Copy Markdown
Contributor
Name Old Result New Result Ratio Performance change( 1 - new / old )
stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan 0.04 0.04 0.98 -1.65% slower
stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan 0.17 0.16 1.01 1.41% faster
stat_comp_benchmarks/benchmarks/garch/garch.stan 0.88 0.89 0.99 -0.85% slower
stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan 0.01 0.01 0.95 -5.72% slower
stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan 0.1 0.1 1.02 1.87% faster
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan 8.07 7.99 1.01 1.01% faster
stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan 3.68 3.68 1.0 -0.03% slower
stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan 32.92 33.67 0.98 -2.29% slower
stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan 0.38 0.38 1.0 -0.04% slower
stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan 6.83 6.8 1.0 0.36% faster
stat_comp_benchmarks/benchmarks/sir/sir.stan 165.77 166.76 0.99 -0.6% slower
stat_comp_benchmarks/benchmarks/arK/arK.stan 3.23 3.21 1.0 0.43% faster
stat_comp_benchmarks/benchmarks/arma/arma.stan 0.61 0.61 1.0 0.2% faster
stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 25.73 25.8 1.0 -0.27% slower
performance.compilation 390.79 378.36 1.03 3.18% faster
Mean result: 0.9983887889556122

Jenkins Console Log
Jenkins Build Stages
Commit hash: 1dfac7ebd63e7747b7c9fe5984b0252ad312ebea

Machine information
Distributor 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:                           52 bits physical, 57 bits virtual
CPU(s):                                  192
On-line CPU(s) list:                     0-191
Thread(s) per core:                      2
Core(s) per socket:                      48
Socket(s):                               2
NUMA node(s):                            2
Vendor ID:                               AuthenticAMD
CPU family:                              25
Model:                                   17
Model name:                              AMD EPYC 9474F 48-Core Processor
Stepping:                                1
Frequency boost:                         enabled
CPU MHz:                                 1497.657
CPU max MHz:                             4114.4229
CPU min MHz:                             1500.0000
BogoMIPS:                                7189.39
Virtualization:                          AMD-V
L1d cache:                               3 MiB
L1i cache:                               3 MiB
L2 cache:                                96 MiB
L3 cache:                                512 MiB
NUMA node0 CPU(s):                       0-47,96-143
NUMA node1 CPU(s):                       48-95,144-191
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:                  Not affected
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; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Mitigation; Clear CPU buffers
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 amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid 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 perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap

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

@atarutin atarutin changed the title Fix NaN in hmm_hidden_state_prob for long sequences (#2677) Fix NaN and over/underflow in the HMM functions for long sequences and large log densities (#2677) Nov 3, 2026
@atarutin

atarutin commented Nov 3, 2026

Copy link
Copy Markdown
Author

I've updated the title and description to match the current head, 2a468d6, including @SteveBronder's changes to hmm_latent_rng, hmm_marginal and hmm_check. The bot's review above was of 6a70a96, before those commits.

On the bot's requests:

  • "Fixes hmm_hidden_state returns NaN #2677": the description now says what the fix was checked against, and that the model attached to the issue and the forum thread weren't run. I've kept the closing keyword because the PR addresses both causes identified in the issue. If you'd rather keep hmm_hidden_state returns NaN #2677 open until the attached model has been run, I can change it to "Refs".
  • "Inputs that used to give NaN": narrowed to long sequences and large-magnitude log densities, and the description now lists the cases that can still give NaN.
  • Measured values (-7900.6, 2.5e-14 and so on): removed. The description now gives only the bounds the tests assert, and says that no test compares the output directly with develop.

At 2a468d6, the 19 tests in the four HMM test files pass locally. At the merge base with the same test files, six fail and three abort on an Eigen assert. The description lists them by name.

@SteveBronder, is there anything else you need from me before review?

@SteveBronder SteveBronder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@atarutin

Copy link
Copy Markdown
Author

@SteveBronder The only failing check here is continuous-integration/jenkins/pr-merge, and I don't think either failure came from this PR:

What would you prefer: a Jenkins rerun on the current head (2a468d6), like #3504 got, or should I merge develop into the branch? It's 63 commits behind, merges cleanly, and a push would start a new build. I've held off pushing so that your approval stays on the commit you reviewed.

@atarutin

Copy link
Copy Markdown
Author

@SteveBronder Thanks for replaying it. Build 8 never got to the tests, though. Lint and docs passed, and then both "Quick tests" branches (headers and unit) failed cloning stan-dev/math. The fetch was running at about 400 KiB/s when it dropped (fetch-pack: unexpected disconnect while reading sideband packet, early EOF), and Jenkins gave up after its retry limit. The PRs from the same 11-06 abort have since passed on reruns, so could you replay it once more when you get a chance?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

numerics Numerical issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hmm_hidden_state returns NaN

5 participants