Skip to content

Add scale-free reservoir initializer - #578

Open
aviralgarg05 wants to merge 5 commits into
SciML:masterfrom
aviralgarg05:feat/scale-free-topology-387
Open

aviralgarg05 wants to merge 5 commits into
SciML:masterfrom
aviralgarg05:feat/scale-free-topology-387

Conversation

@aviralgarg05

@aviralgarg05 aviralgarg05 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • add an undirected Barabási–Albert reservoir initializer
  • support reproducible weights, spectral-radius scaling, and sparse output
  • document the initializer and cover its topology, validation, and boundary cases

Tests

  • GROUP=Core julia --project -e 'using Pkg; Pkg.test()'
  • julia --project -e 'using Test; include("test/inits_tests.jl")'
  • Runic formatting check

Fixes #387

Implement an undirected Barabasi-Albert topology with reproducible weighted connections, spectral-radius scaling, and sparse output support. Add validation, API docs, and focused topology tests.
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #578

Reviewed head: 8355a14
Raw SHA-256: d5000c73b84729fc6328eb3b6f23ac4073f822f7adc81ccdc18e9b6ac6e80741
Source SHA-256: 942cf023c882c4e8d3b2215a53bd2a65891910f5fb87e4743a4eb857f5beaf92
Source: standard-review-v1 (harness-log)
Reviewer session: 01a1f828-68f4-75e3-8622-b0a4e040c4bb
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes
Redaction: ran (substitutions=0)

Risk assessment

  • Risk: medium-low (ML7)
  • Verdict: CHANGES
  • Reviewed head: 8355a14

Review report

VERDICT: CHANGES
Round: 1
Head: 8355a14
Slot: 1
CI: CI is not green
Session: 01a1f828-68f4-75e3-8622-b0a4e040c4bb
Reviewer: codex-cli 0.170.0; model: gpt-6.2-astra

Risk

medium-low (ML7: new exported initializer with a docstring, rendered API entry and tests)

Blocking findings

  • [P1] src/inits/inits_reservoir.jl:306 silently returns the wrong spectral radius for valid inputs, including the default element type. With Julia 1.12.4, A = scale_free(Xoshiro(1971721), 3, 3; initial_nodes=2, attachments=1) requests 1 and returns 0.357977. This graph is the path 1–2–3, so its exact radius is sqrt(abs(A[1,2]*A[2,1] + A[2,3]*A[3,2])) evaluated on the stored entries in Float64. Independently, maximum(abs, eigvals(Float64.(A))) gave 0.3579769865. The Float32 eigensolve overestimates the radius before scaling. Output rounding can also dominate: the same call with Xoshiro(19672), Float16 returns radius 2.755676, even though all entries are finite. Use a wider working type for the eigenvalue calculation and verify the realized radius after conversion to the output type. If rounding makes the target unattainable for those weights, redraw weights on the same topology or report an explicit error. Add regression coverage for both cases.

  • [P2] test/inits_tests.jl:75 lets uniform attachment pass as preferential attachment. I changed both degree contributions in the sampler to 1 in a scratch copy. All 18 new assertions still passed. A deterministic four-node test distinguishes the models: start with two nodes, one attachment, and have the RNG return 1 for the first parent draw and 2 for the second. Node 3 then joins node 1, giving degrees [2,1,1]. Draw 2 must attach node 4 to node 1. I observed node 1 at the reviewed head and node 2 with uniform sampling. Add that assertion to protect the defining property without fitting a degree-distribution tolerance.

  • [P2] src/inits/inits_reservoir.jl:248 accepts negative radii. With Xoshiro(1), Float64, 4, 4; initial_nodes=2, attachments=1, radius=-0.75, the call succeeded and returned spectral radius 0.75. Reject negative targets before consuming the RNG and test that input.

  • Blocking priorities: P1, P2, P2

Non-blocking findings

  • src/inits/inits_reservoir.jl:250 accepts any Number, but Int failed inside eps(Int) and ComplexF32 failed when its default radius reached the Real keyword. Document the supported real floating-point element types and give unsupported callers a clear error.
  • Project.toml:3 still declares 0.12.47. Include the new export in a minor release under the supplied versioning policy.

What I ran

  • Julia 1.12.4, with the repository's project and test dependencies resolved in scratch. GROUP=Core julia --project=. -e 'using Pkg; Pkg.test(julia_args=["--compiled-modules=existing"])' passed all nine Core files, including Core/inits_tests.jl | 965 965. The first plain run exited with signal 11. A retry with compiled modules and package images disabled failed the explicit precompilation assertion in test/deprecated.jl:27. The standalone strict precompile and final Core run both exited 0.
  • Ran the added testset against origin/master using the same resolved dependencies. It errored on its first call with UndefVarError: scale_free. Ran the uniform-sampling mutation and scripted draws described above. Across 4,096 seeds, node 4 picked the degree-two parent 2,007 times, consistent with its model probability of 1/2.
  • Checked 480 growth-prefix assertions over Float16, Float32 and Float64, plus seven call-form, RNG and integer-size assertions. All passed. The seed clique was complete and every later node had exactly the requested number of distinct older neighbors, establishing connectivity. The committed seeded dense/sparse equivalence test also passed.
  • julia --project=docs -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate(); include("docs/make.jl")' exited 0 with doctests enabled. Verified that the generated initializer index links to the rendered scale_free page and that the page contains both examples. Runic.jl 1.13.0 in whole-tree --check mode and git diff --check origin/master...HEAD both exited 0.
  • uv run --locked python --version printed Python 3.13.13 and warned that --locked has no effect outside a project. The checkout provides a Julia project and has no Python lockfile or committed Julia manifest.

What I did not verify

GPU behavior, large-reservoir performance and test groups outside Core. I did not inspect remote CI.

AUTHOR_GRADE: {"task":"feature","score":3,"difficulty":3,"rounds":1,"author_harness":"unknown","author_model":"unknown","note":"The attachment implementation, call forms, sparse output and docs passed independent checks. The tests accept uniform attachment, and valid Float32 and Float16 calls silently miss the requested spectral radius."}

Head link: 8355a14


🤖 Posted by an AI agent — harness: codex-cli 0.170.0 · model: gpt-6.2-astra
Conversation: local session 01a1f828-68f4-75e3-8622-b0a4e040c4bb (no session URL)

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed reproductions. I reproduced all three blocking cases locally and addressed them. Radius scaling now uses widened precision, verifies the realized output radius, and redraws weights on the same topology when output rounding makes a sample unsuitable. Invalid radii and unsupported element types are rejected before the RNG is consumed. I also added a deterministic preferential-attachment regression and the reported Float32/Float16 cases. The full Core suite passes, including 974 initializer tests, along with 1,500 additional seed/type radius checks.

@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #578

Reviewed head: 819f564
Raw SHA-256: 6cd8bd9010d26b3786f7a0da32ea1c493a8bae47ec50912c17f77332c9083b14
Source SHA-256: 5f782096f65b5b72f50a5355417362e0505df98a0d445b6037b2d02cb8f79440
Source: standard-review-v1 (harness-log)
Reviewer session: 01a1faa0-990f-7773-95d9-0d82be0fb5bb
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes
Redaction: ran (substitutions=0)

Risk assessment

  • Risk: medium-low (ML7)
  • Verdict: CHANGES
  • Reviewed head: 819f564

Review report

VERDICT: CHANGES
Round: 2
Head: 819f564
Slot: 1
CI: green
Session: 01a1faa0-990f-7773-95d9-0d82be0fb5bb
Reviewer: codex-cli 0.169.0; model: gpt-6.2-astra

Risk

medium-low (ML7: new exported initializer with a docstring, rendered API entry and tests. Existing initializer calls retain their behavior.)

Blocking findings

  • [P1] src/inits/inits_reservoir.jl:341 accepts a spectral radius more than twice the target. I ran scale_free(Xoshiro(19672), Float16, 3, 3; initial_nodes=2, attachments=1, radius=0.01). The target rounds to 0.01000213623 in Float16, but the stored matrix has radius 0.02073902401. With Xoshiro(1971721), Float32 and radius=0.001, the same call returns radius 0.0009785848393, a 2.14% error. I verified both with Float64 eigenvalues and independently with the three-node tree formula sqrt(abs(Σ A[i,j]*A[j,i])), summing over i < j in BigFloat. The absolute tolerance floors its scale at one, so substantial relative errors pass for small positive targets. Use a relative tolerance for positive targets at both acceptance checks, keep zero explicit, and add a subunit-target regression using the tree formula.

  • [P2] src/inits/inits_reservoir.jl:260 converts the radius before checking its sign. I ran scale_free(Xoshiro(1), Float16, 4, 4; initial_nodes=2, attachments=1, radius=-1e-8). The target rounds to negative zero, the call returns an all-zero matrix, and it advances the RNG. Float32 with radius=-1e-100 does the same. Check the original real argument for negativity before narrowing it, and cover a negative input that rounds to zero.

  • Blocking priorities: P1, P2

Non-blocking findings

  • src/inits/inits_reservoir.jl:347 repeats the dense eigensolve that just accepted the same matrix at line 337. At 128 nodes, a warmed Float32 initialization allocated 1,270,696 bytes. Evaluating that last check alone on its output allocated 308,536 bytes. Return the accepted matrix directly or retain the acceptance result through loop exit.

What I ran

  • Julia 1.13.2, using the package project and its declared test dependencies in a scratch copy of the head. GROUP=Core julia --project=. -e 'using Pkg; Pkg.test(; julia_args=["--compiled-modules=existing"])' passed all nine Core files, including Core/inits_tests.jl | 974 974. git diff --check origin/master...HEAD passed. uv run --locked python --version printed Python 3.15.0 and warned that the lock flag has no effect outside a Python project. The checkout has neither a Python lockfile nor a committed Julia manifest.
  • Replayed the earlier seeds at radius one. The stored matrices had radii 1.00000000108 for Float32 and 1.00015257625 for Float16. Across 288 Float32/Float64 seed, shape and target combinations, dense and sparse results agreed exactly. Sizes ranged from 2 to 32 and included cliques and single-attachment trees. The largest Float64 relative error was 5.33e-15.
  • Replaced both degree weights with unit weights in the sampler in an isolated Julia process and ran the committed initializer test file. The scale-free testset reported 25 passes and two failures, precisely at test/inits_tests.jl:121 and :122. Node 4 attached to node 2. The unmodified sampler passed both assertions.
  • Ordinary negative radii, NaN, both infinities, a complex radius, invalid element types and out-of-range node or attachment counts raised ArgumentError. An exact zero target returned a zero matrix. Non-square dimensions and wrong arity raised the package's DimensionMismatch.
  • Confirmed the export and exact seeded repeatability. Julia inferred a union of the dense and sparse matrix types even for wrappers with a fixed output choice. Warmed dense Float32 calls allocated 128,344 bytes at 16 nodes, 411,712 at 64 and 1,270,696 at 128.
  • Ran the project's Documenter build through makedocs with doctests enabled. It exited 0. Verified the rendered initializer index links to the scale-free API page and the page contains the model description and both examples.

What I did not verify

Other Julia versions, groups outside Core and large-reservoir timing. The CI status above was supplied for this head.

AUTHOR_GRADE: {"task":"feature","score":3,"difficulty":3,"rounds":2,"author_harness":"unknown","author_model":"unknown","note":"The new test detects uniform attachment and both unit-radius regressions pass. Representable subunit targets can still miss by over 100%, and narrowing accepts some negative inputs."}

Head link: 819f564


🤖 Posted by an AI agent — harness: codex-cli 0.169.0 · model: gpt-6.2-astra
Conversation: local session 01a1faa0-990f-7773-95d9-0d82be0fb5bb (no session URL)

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Thanks, I reproduced both remaining cases. Validation now rejects the original negative value before conversion or RNG use, positive targets use relative-only acceptance with an explicit zero case, and the accepted result is returned without a second eigensolve. I added the reported Float16 and Float32 subunit seeds using an independent BigFloat tree-radius formula, plus tiny-negative RNG regressions. The full Core suite and formatting checks pass.

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #578

Reviewed head: 67e13b5
Raw SHA-256: 9451eb2570a5db0aaada44135a7c118f93d9cb8c19d90dc93e909c656d4439f3
Source SHA-256: 034b950ee90a8dbfa66abb52ec3c09585dedcf12ba9373b829982fc3141bf350
Source: standard-review-v1 (harness-log)
Reviewer session: 01a1ff84-2641-7b70-9307-b8f158554210
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes
Redaction: ran (substitutions=0)

Risk assessment

  • Risk: medium-low (ML7)
  • Verdict: CHANGES
  • Reviewed head: 67e13b5

Review report

VERDICT: CHANGES
Round: 2
Head: 67e13b5
Slot: 1
CI: green
Session: 01a1ff84-2641-7b70-9307-b8f158554210
Reviewer: codex-cli 0.170.0; model: gpt-6.2-sol

Risk

medium-low (ML7: new exported initializer with a docstring, rendered API entry, and tests)

Blocking findings

  • [P2] src/inits/inits_reservoir.jl:342 accepts an inaccurate radius for Float64 subnormals. With q = nextfloat(0.0), I ran scale_free(Xoshiro(6), Float64, 3, 3; initial_nodes=2, attachments=1, radius=q, return_sparse=true) after loading SparseArrays. The stored path has radius sqrt(2) * q by the analytic formula evaluated after dividing its entries by q. The Float64 eigensolve used by the implementation reports q, so the check accepts a 41.4% error despite the configured relative tolerance of about 1.49e-8. Check the spectrum in units of the target before accepting the draw. Add this seed as a regression with an analytic radius check in the same units.

  • [P2] src/inits/inits_reservoir.jl:339 can silently drop connections while preserving the requested radius. With scale_free(Xoshiro(19672), Float16, 3, 3; initial_nodes=2, attachments=1, radius=nextfloat(Float16(0))), the result has two nonzero weights and an isolated node. The documented topology requires four nonzero weights. The corresponding Float32 probe with seed 1971721 returns asymmetric support. Check that the stored matrix retains the sampled adjacency for positive radii. Retry weights on the same graph or report that the radius and graph cannot both be realized.

  • [P2] src/inits/inits_reservoir.jl:266 silently treats a positive target that rounds to zero as an explicit zero request. With Xoshiro(1), a three-node reservoir, initial_nodes=2, and attachments=1, I observed all-zero matrices and advanced RNGs for Float16 with 1e-8, Float32 with 1e-100, and Float64 with big"1e-400". Reject positive targets that convert to zero before consuming randomness. Keep the zero result for an explicit zero target.

  • Blocking priorities: P2, P2, P2

Non-blocking findings

  • src/inits/inits_reservoir.jl:327 still allocates dense weight intermediates on every attempt. A warmed Float32 call with 500 nodes, seed 5, four initial nodes, and two attachments allocated 13,466,656 bytes in total. Filling and masking a reusable weight buffer would reduce avoidable allocation on retries.

What I ran

  • Ran the same Julia 1.12.4 probes on the head and HEAD~2. The independent three-node tree radius is sqrt(abs(sum(A[i,j] * A[j,i] for i < j))), evaluated in BigFloat on the stored entries. For the reported Float16 case at target 0.01, the measured radius went from 0.0207390240 to 0.0099995739. For Float32 at target 0.001, it went from 0.0009785848393 to 0.0010000000559. Both seeds also produced radii within tolerance at target one.
  • The tiny negative Float16 and Float32 targets, plus -big"1e-400" for Float64, changed from zero matrices with RNG advancement to ArgumentError with the RNG state intact. An ordinary negative Float64 target also threw without RNG advancement. Signed zeros returned zero matrices. The smallest-normal sweep passed across 64 seeds per type. At the smallest subnormal, the sweep lost at least one weight for 57 of 64 seeds each in Float16 and Float32 and 50 of 64 in Float64. The separate normalized sparse probe found Float64 radius errors outside the stated tolerance even at 16 times the smallest subnormal.
  • Ran GROUP=<group> julia --project=. -e 'using Pkg; Pkg.test(julia_args=["--compiled-modules=existing"])' on Julia 1.12.4 with the checkout's resolved dependencies. Core passed 1,404 assertions, including 980 in Core/inits_tests.jl. Layers passed 878, Models 647, Extensions 297, Interface 755, and QA 41. Combined GROUP=All exited with received signal: 11 on Julia 1.12.4 and 1.12.7. I stopped the diagnostic retry after --pkgimages=no produced precompilation failures for ReservoirComputing and dependencies. The retry exited 130.
  • Ran julia --project=docs -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate(); include("docs/make.jl")' in a scratch copy. Documenter ran doctests, checked exported documentation, rendered HTML, and exited 0. The initializer index links to the generated scale_free page, which contains both examples.
  • Runic.jl 1.13.0 --check over src, ext, and test, typos over changed files, and git diff --check origin/master...HEAD all exited 0. uv run --locked python --version printed Python 3.13.13 and warned that --locked has no effect outside a project. This Julia checkout has no Python lockfile or committed Julia manifest.

What I did not verify

GPU execution, other platforms, live CI, and the cause or default-branch reproducibility of the combined test process crash.

AUTHOR_GRADE: {"task":"feature","score":3,"difficulty":3,"rounds":2,"author_harness":"unknown","author_model":"unknown","note":"Analytic probes confirm both reported regressions are fixed. Positive radii at the representability boundary can still return inaccurate radii, lost connections, or an all-zero reservoir."}

Head link: 67e13b5


🤖 Posted by an AI agent — harness: codex-cli 0.170.0 · model: gpt-6.2-sol
Conversation: local session 01a1ff84-2641-7b70-9307-b8f158554210 (no session URL)

Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Thanks, I addressed the three boundary cases. Spectral-radius validation now runs in target-relative units, scaled matrices must retain the complete sampled topology, and positive radii that narrow to zero are rejected before RNG use. I added the reported Float64 and Float16 subnormal seeds with independent analytic radius and topology checks, plus RNG-preservation coverage for narrowing inputs. Runic passes, and the full Core suite passes twice with 990 initializer assertions.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scale free topology

3 participants