Skip to content

hash: keep hash consistent with isequal - #1543

Merged
yebai merged 1 commit into
mainfrom
bs04-transform-hash
Nov 19, 2026
Merged

yebai merged 1 commit into
mainfrom
bs04-transform-hash

Conversation

@yebai

@yebai yebai commented Nov 19, 2026

Copy link
Copy Markdown
Member

Split of #1529 (part 3 of 12).

FixedTransform, TransformedValue, WithTransforms, LinkSome, UnlinkSome and ParamsWithStats define isequal but used the default hash, which depends on the identity of the arrays and sets they hold. Equal values holding separate arrays or sets therefore hashed differently, and a Set or Dict kept both: Set([TransformedValue([1.0], NoTransform()), TransformedValue([1.0], NoTransform())]) had two elements and now has one. Each type now hashes the fields its isequal compares.

Three existing methods also hashed what isequal ignores, so equal values could hash differently:

  • PartialArray hashed its storage type, and keyed assigned entries by CartesianIndex when its mask was a GrowableArray; it now hashes its size and the linear positions and values of assigned entries.
  • GrowableArray hashed its wrapper type, although it compares equal to a plain Array.
  • LogProbAccumulator hashed its numeric type, so LogPriorAccumulator(1) and LogPriorAccumulator(1.0) were equal but hashed differently.

Part 4 hashes a model's arguments and bindings, which may contain these values, so their hashes must agree with isequal. Each type gains a small test, and HISTORY gains a one-line bugfix entry.

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.12%. Comparing base (fd8f8a6) to head (c74dedf).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1543      +/-   ##
==========================================
+ Coverage   85.07%   85.12%   +0.04%     
==========================================
  Files          52       52              
  Lines        4663     4671       +8     
==========================================
+ Hits         3967     3976       +9     
+ Misses        696      695       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

DynamicPPL.jl documentation for PR #1543 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR1543/

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Benchmarks

Performance Ratio: gradient time divided by log-density time.

For very small models these ratios are noisy across runs and machines; raw primal and gradient timings are more reliable. The benchmarks are aimed at DynamicPPL developers and mainly catch obvious allocation or type-stability regressions. See benchmark notes for details.

Primal times: main 5b9b55f, PR c74dedf

================================================================================================================================
                                                                     primal                            gradient                 
                                                             ----------------------  -------------------------------------------
Model                                         dim    linked        main          PR     FwdDiff    RvsDiff    Mooncake    Enzyme
--------------------------------------------------------------------------------------------------------------------------------
Simple assume observe*                          1     false     5.25 ns     5.25 ns        9.82    1212.23        9.63      6.47
Simple assume observe*                          1      true     5.25 ns     5.25 ns        9.19    1342.05       10.49      6.55
Smorgasbord                                   201     false     5.41 μs     5.38 μs       77.28     137.83        5.32      1.34
Smorgasbord                                   201      true     6.39 μs     6.45 μs       75.78     158.82        5.61      1.45
Smorgasbord fixed                             100     false     2.56 μs     2.59 μs       52.35     169.34        6.62      1.26
Smorgasbord fixed                             100      true     3.03 μs     3.05 μs       62.17     164.21        6.64      1.43
NamedTuple-field LHS variable*                  1     false     5.25 ns     5.26 ns        9.79    1465.87        9.79      6.33
NamedTuple-field LHS variable*                  1      true     5.26 ns     5.26 ns        9.82    1361.41        9.83      6.34
MvNormal argument-supplied observation*         1     false     73.0 ns     73.8 ns        2.14     590.55        3.08      2.36
MvNormal argument-supplied observation*         1      true     71.3 ns     73.5 ns        2.22     636.62        3.31      2.28
Loop univariate 1k                           1000     false     17.1 μs     17.0 μs      868.74     316.30        2.96      0.83
Loop univariate 1k                           1000      true     18.9 μs     18.3 μs     1261.68     297.11        2.69      0.72
Multivariate 1k                              1000     false     1.01 μs     1.09 μs     1489.40     501.53        1.67      3.33
Multivariate 1k                              1000      true    956.0 ns     1.06 μs     1413.29     664.94        1.85      2.92
Loop univariate 10k                         10000     false    165.0 μs    165.0 μs    12995.53     365.88        2.98      0.71
Loop univariate 10k                         10000      true    178.0 μs    176.0 μs    13350.30     342.63        2.86      0.66
Multivariate 10k                            10000     false     10.8 μs     10.2 μs    24220.02     701.49        1.67      2.74
Multivariate 10k                            10000      true     9.87 μs     9.93 μs    25183.27     717.05        1.82      3.06
Dynamic                                       err     false     1.44 μs     1.36 μs         err      43.94       16.06      4.99
Dynamic                                        10      true     1.85 μs     1.85 μs        1.75      60.19       13.28      6.85
Submodel*                                       1     false     5.26 ns     5.27 ns       10.00    1562.02       10.07      6.52
Submodel*                                       1      true     5.26 ns     5.27 ns        9.78    1667.52       10.23      6.14
LDA                                             6      true     3.83 μs     3.76 μs        1.57      12.50       30.60     32.93
================================================================================================================================
Environment
Julia Version 1.13.4
Commit c1ac32ab733 (2026-11-13 17:30 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-20.1.8 (ORCJIT, znver3)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 4 virtual cores)

`FixedTransform`, `TransformedValue`, `WithTransforms`, `LinkSome`,
`UnlinkSome` and `ParamsWithStats` define `isequal` but used the
`objectid`-based default `hash`, so equal values holding separate
arrays or sets hashed differently and a `Set` or `Dict` kept both.

Other methods hashed what `isequal` ignores: `PartialArray` its storage
type and, with a `GrowableArray` mask, `CartesianIndex` positions;
`GrowableArray` its wrapper type, although it equals a plain `Array`;
`LogProbAccumulator` the type of its log-probability. For example,
`LogPriorAccumulator(1)` and `LogPriorAccumulator(1.0)` were equal but
hashed differently.

Assisted-by: Claude Code <noreply@anthropic.com>

@shravanngoswamii shravanngoswamii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Equal copies of FixedTransform(Shift([1.0, 2.0])) still hash differently because Shift itself has the same issue.

Should we fix the Bijectors hashes before part 4?

@yebai

yebai commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Should we fix the Bijectors hashes before part 4?

@shravanngoswamii can you try that?

@yebai
yebai added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 702b8c0 Nov 19, 2026
23 checks passed
@yebai
yebai deleted the bs04-transform-hash branch November 19, 2026 21:20
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.

3 participants