-
Notifications
You must be signed in to change notification settings - Fork 19
ET backend: GPU evaluation with forces, virial, basis, and batching #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8c28e0a
Add tests for forces and virial via Zygote through ET backend
jameskermode d3642fe
Add ETCalculator with AtomsCalculators interface for ET backend
jameskermode 64b480f
Add ETBasisCalculator for GPU basis evaluation (Phase 2)
jameskermode 4892ad2
Add GPU-compatible cubic splines via KernelAbstractions (Phase 3)
jameskermode 013668a
Add batched evaluation for multi-structure GPU training (Phase 4)
jameskermode 15253d0
Refactor: Unified ETCalculator architecture
jameskermode a12a225
Remove GPUCubicSpline - P4ML already provides this
jameskermode a25ffaa
Fix ETGraph constructor calls with graph_data argument
jameskermode 0279e5e
CI: Use EquivariantTensors branch with Zygote rrule fixes
jameskermode c87b7de
Optimize evaluate_basis_ed with ForwardDiff.jacobian (500x speedup)
jameskermode 78f8f35
CI: Fix Pkg.develop -> Pkg.add for branch dependency
jameskermode 240dc09
CI: Add General registry before EquivariantTensors branch
jameskermode f60a730
Refactor: Extract shared utilities and fix ETGraph constructor bug
jameskermode 0d9603e
Trigger CI with updated EquivariantTensors vector pullback support
jameskermode 91ec036
Restore deleted debugging code per reviewer request
jameskermode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| #!/usr/bin/env julia | ||
| # | ||
| # Benchmark script for evaluate_basis_ed | ||
| # Compares current Zygote pullback approach vs ForwardDiff.jacobian | ||
| # | ||
|
|
||
| using Pkg | ||
| Pkg.activate(joinpath(@__DIR__, "..")) | ||
|
|
||
| using ACEpotentials, AtomsBase, Unitful, BenchmarkTools, Random | ||
| using Lux, StaticArrays | ||
| using AtomsBuilder | ||
|
|
||
| # Access functions from internal Models module | ||
| const M = ACEpotentials.Models | ||
| const build_et_calculator = M.build_et_calculator | ||
| const evaluate_basis = M.evaluate_basis | ||
| const evaluate_basis_ed = M.evaluate_basis_ed | ||
| const length_basis = M.length_basis | ||
|
|
||
| # Build a small test system (Si crystal) | ||
| function make_si_system(supercell=(2,1,1)) | ||
| sys = AtomsBuilder.bulk(:Si) * supercell | ||
| AtomsBuilder.rattle!(sys, 0.1u"Å") | ||
| return sys | ||
| end | ||
|
|
||
| # Build model and calculator (following test_et_calculator.jl pattern) | ||
| println("Building ACE model...") | ||
|
|
||
| elements = (:Si,) | ||
| level = M.TotalDegree() | ||
| max_level = 8 | ||
| order = 2 | ||
| maxl = 4 | ||
|
|
||
| rin0cuts = M._default_rin0cuts(elements) | ||
| rin0cuts = (x -> (rin = x.rin, r0 = x.r0, rcut = 5.5)).(rin0cuts) | ||
|
|
||
| model = M.ace_model(; elements = elements, order = order, | ||
| Ytype = :solid, level = level, max_level = max_level, | ||
| maxl = maxl, pair_maxn = max_level, | ||
| rin0cuts = rin0cuts, | ||
| init_WB = :glorot_normal, init_Wpair = :glorot_normal) | ||
|
|
||
| rng = Random.MersenneTwister(1234) | ||
| ps, st = Lux.setup(rng, model) | ||
|
|
||
| # Zero out pair basis (not implemented in ET backend) | ||
| for s in model.pairbasis.splines | ||
| s.itp.itp.coefs[:] *= 0 | ||
| end | ||
|
|
||
| println("Building ET calculator...") | ||
| calc = build_et_calculator(model, ps, st) | ||
|
|
||
| # Test with different system sizes | ||
| for supercell in [(2,1,1)] # Can add larger systems | ||
| sys = make_si_system(supercell) | ||
| n_atoms = length(sys) | ||
|
|
||
| println("\n" * "="^60) | ||
| println("System: $(n_atoms) atoms (supercell $(supercell))") | ||
| println("Basis length per species: $(calc.len_Bi)") | ||
| println("Total basis length: $(length_basis(calc))") | ||
| println("="^60) | ||
|
|
||
| # Warmup | ||
| println("\nWarmup...") | ||
| B = evaluate_basis(calc, sys) | ||
| println("B shape: $(size(B))") | ||
|
|
||
| # Benchmark evaluate_basis (forward only) | ||
| println("\n--- evaluate_basis (forward only) ---") | ||
| b1 = @benchmark evaluate_basis($calc, $sys) samples=10 evals=1 | ||
| display(b1) | ||
|
|
||
| # Benchmark evaluate_basis_ed (forward + Jacobian) | ||
| println("\n--- evaluate_basis_ed (current: Zygote pullback) ---") | ||
| println("This may take a while for larger systems...") | ||
|
|
||
| # First just time it once to see how long it takes | ||
| t0 = time() | ||
| B_ed, dB = evaluate_basis_ed(calc, sys) | ||
| t1 = time() | ||
| println("Single call time: $(round(t1-t0, digits=2)) seconds") | ||
| println("B_ed shape: $(size(B_ed)), dB shape: $(size(dB))") | ||
|
|
||
| # Only do proper benchmark if it's not too slow | ||
| if t1 - t0 < 30.0 | ||
| b2 = @benchmark evaluate_basis_ed($calc, $sys) samples=5 evals=1 | ||
| display(b2) | ||
| else | ||
| println("Skipping full benchmark - single call took > 30 seconds") | ||
| end | ||
|
|
||
| # Calculate theoretical efficiency | ||
| n_outputs = n_atoms * calc.len_Bi | ||
| println("\n--- Analysis ---") | ||
| println("Number of outputs (n_atoms × len_Bi): $n_outputs") | ||
| println("With Zygote pullback: $n_outputs backward passes") | ||
| println("With ForwardDiff: ~$(3 * n_atoms) forward passes (rough estimate)") | ||
| println("Theoretical speedup: $(round(n_outputs / (3 * n_atoms), digits=1))×") | ||
| end | ||
|
|
||
| println("\n\nBenchmark complete.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,14 +39,6 @@ function _convert_Rnl_learnable(basis; zlist = ChemicalSpecies.(basis._i2z), | |
| # named-tuple inputs | ||
| # | ||
| et_trans = _convert_agnesi(basis) | ||
|
|
||
| # OLD VERSION - KEEP FOR DEBUGGING then remove | ||
| # et_trans = let transforms = basis.transforms | ||
| # ET.NTtransform( xij -> begin | ||
| # trans_ij = transforms[__z2i(xij.s0), __z2i(xij.s1)] | ||
| # return trans_ij(rfun(xij)) | ||
| # end ) | ||
| # end | ||
|
|
||
| # the envelope is always a simple quartic (1 -x^2)^2 | ||
| # otherwise make this transform fail. | ||
|
|
@@ -76,16 +68,6 @@ function _convert_Rnl_learnable(basis; zlist = ChemicalSpecies.(basis._i2z), | |
| NZ^2, # num (Zi,Zj) pairs | ||
| selector) | ||
|
|
||
| # et_rbasis = SkipConnection( # input is (rij, zi, zj) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| # Chain(y = et_trans, # transforms yij | ||
| # P = SkipConnection( | ||
| # et_polys, | ||
| # WrappedFunction( Py -> et_env.(Py[2]) .* Py[1] ) | ||
| # ) | ||
| # ), # r -> y -> P = e(y) * polys(y) | ||
| # et_linl # P -> W(Zi, Zj) * P | ||
| # ) | ||
|
|
||
| et_rbasis = SkipConnection( # input is (rij, zi, zj) | ||
| Chain(y = et_trans, # transforms yij | ||
| Pe = BranchLayer( | ||
|
|
@@ -114,14 +96,7 @@ function _agnesi_et_params(trans) | |
| rcut = trans.rcut | ||
|
|
||
| params = ET.agnesi_params(pcut, pin, rin, req, rcut) | ||
| @assert params.a ≈ a | ||
|
|
||
| # ----- for debugging ----------- | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| # r = rin + rand() * (rcut - rin) | ||
| # y1 = trans(r) | ||
| # y2 = ET.eval_agnesi(r, params) | ||
| # @assert y1 ≈ y2 | ||
| # ------------------------------- | ||
| @assert params.a ≈ a | ||
|
|
||
| return params | ||
| end | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you ask Claude to not remove my commented out old code? I always keep it for a reason and want to remove it myself.