Repository navigation
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #172 +/- ##
==========================================
+ Coverage 97.08% 97.91% +0.82%
==========================================
Files 4 4
Lines 309 335 +26
Branches 62 66 +4
==========================================
+ Hits 300 328 +28
+ Misses 4 3 -1
+ Partials 5 4 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ormsbee
force-pushed
the
ormsbee/miller-drop-numpy
branch
from
November 20, 2026 20:30
baaf288 to
76c93ae
Compare
miller.py was the only module that imported numpy. It used it for arithmetic on 3D vectors, which is now written out in pure Python, so chem no longer requires numpy. That's one step towards running LMS and Studio web processes without numpy, to reduce the memory they need. numpy didn't compute the same Miller indices on every CPU. np.dot calls OpenBLAS, which picks a kernel to suit the CPU. For a 3D vector, the SkylakeX and Cooperlake kernels, for CPUs with AVX-512, use fused multiply-add. The Haswell, Zen and Sandybridge kernels multiply and add each term separately, which rounds differently. miller() tests dot products for exact zeros, so the two graded some answers differently. The new code does the same thing on every CPU. It computes the indices with fused arithmetic first, so every answer that got indices on a CPU with AVX-512 still gets the same ones. If that raises ZeroDivisionError, it tries again with plain arithmetic, which is better at noticing that a plane goes through the origin. See #171 for the alternatives. math.fma needs Python 3.13, and raises an exception where numpy returned inf or nan. So _fma() does the fused multiply-add with fractions instead. Behavior changes, from running the new code and chem 2.0.1 on 1,000,000 random answers: * On hosts with AVX-512, 2,236 answers that raised ZeroDivisionError get Miller indices. Nothing else changes. * On hosts that used the Haswell, Zen or Sandybridge kernels, 2,313 answers get different indices, and 120 that raised ZeroDivisionError get indices. No answer starts raising. * A point without exactly three coordinates still raises ValueError, but with a different message. * miller() takes points as any sequences, not only numpy arrays. Tests: * miller_baseline.json records the Miller indices chem 2.0.1 computed for about 1,480 answers. test_miller_baseline checks the current code against it. * 150 of those are answers that chem 2.0.1 computed differently with the two kinds of kernel. They record both results, and are checked separately. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ormsbee
force-pushed
the
ormsbee/miller-drop-numpy
branch
from
November 23, 2026 03:55
76c93ae to
1fba852
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #171.
Summary
miller.pywas the only module inchemthat imported numpy. It used it for arithmetic on 3D vectors when grading crystallography problems. This writes that arithmetic out in pure Python, and dropsnumpyas a dependency.The reason is in #171: the long-term goal is to run LMS and Studio web processes without numpy, to reduce the memory they need. openedx-platform still gets numpy from other packages, so this doesn't save that memory by itself.
Which arithmetic
numpy didn't compute the same Miller indices on every CPU.
np.dotcalls OpenBLAS, which picks a kernel to suit the CPU:miller()tests dot products for exact zeros, so the two graded some answers differently. #171 has the details and a way to reproduce it.miller()now does the same thing on every CPU. This is option 5 in #171:ZeroDivisionError, it computes them again with plain arithmetic. Fused arithmetic is worse at noticing that a plane goes through the origin, and when it doesn't notice,sub_miller()divides by zero._milleris the old body ofmiller(), with the dot product passed in. The algorithm itself, the rounding of coordinates ingrade(), andsub_millerare unchanged._dot_fusedneeds a fused multiply-add, andmath.fmadoesn't fit:chemsupports 3.12, as openedx-platform does.infornan.So
_fma()works out the exact product and sum withfractions, and rounds once.Behavior changes
These counts are from the differential test below, on 1,000,000 random answers:
ZeroDivisionErrorto Miller indices.ZeroDivisionErrorto Miller indices.(3,-4,-5)example in Remove numpy dependency #171.ZeroDivisionErroris now rarer than it was with either kind of kernel. Of 85,480 such planes with points on the cube's edges, the fused kernels raised it on 4,506, the plain kernels on 2,956, and this branch on 2,946.ValueError, but the message is different. numpy raised it fromnp.crossor from array broadcasting.miller()now checks for it.miller()takes any sequences as points, not only numpy arrays.grade()takes about 38 microseconds on the machine I tested on, up from about 22 with numpy. Thefractionsarithmetic is the reason: with plain arithmetic alone it took about 8.Evidence
Differential test: I ran chem 2.0.1 and this branch's
miller.pyon 1,000,000 random answers of three points each, and compared the Miller indicesgrade()computed, or the exception it raised:I also checked the rule itself on each of the 1,000,000 answers. This branch always gave what chem 2.0.1 gave with a fused kernel, unless that was
ZeroDivisionError. Then it gave what chem 2.0.1 gave with a plain kernel.numpy 1.26.4 is the version openedx-platform pins. OpenBLAS chose Cooperlake by default with numpy 1.26.4 on the test machine, and SkylakeX with numpy 2.5.5. The other kernels were forced with
OPENBLAS_CORETYPE. The answers were:grade()_fmaagainstmath.fma: on Python 3.13, the two agreed on all 999,954 of 1,000,000 random inputs thatmath.fmadidn't raise an exception for, including the sign of zero.Baseline from chem 2.0.1: The differential test needs numpy, so it can't live in this repo. Instead,
miller_baseline.jsonrecords what chem 2.0.1 computed for 1,483 answers, andtest_miller_baseline.pychecks the current code against it. This follows the pattern ofchemcalc_baseline.jsonin #169.With 8 of the first 1,341, that makes 150 answers that depended on the kernel. Each records both results, as
fusedandplain. They're checked in two classes:TestMillerBaselineFused: 40 answers where the fused kernel's result wasn'tZeroDivisionError. The code must give the fused result.TestMillerBaselineRetry: 110 answers where the fused kernel raisedZeroDivisionError. The code must give the plain kernel's indices.generate_miller_baseline.pymade the file. It runs chem 2.0.1 with both kinds of kernel, and checks which arithmetic each one really does before trusting it. It refuses to run unless it's importing chem 2.0.1. It produced the same file with numpy 1.26.4 on Python 3.12 and with numpy 2.5.5 on Python 3.13.Mutation checks: To see what the tests catch, I made each of these changes in a scratch copy:
miller.pyTestCrystallographyMillerTestMillerBaseline…Fused…Retry_dot_fuseddoes plain arithmetic_dot_fusedtakes its terms in reverse order_dot_fusedusesmath.sumprod_dot_plaindoes fused arithmetic_dot_plainadds its terms in reverse order_dot_plainusesmath.sumprod_fmareturnsx * y + z_fmacallsmath.fmamiller()doesn't retrymiller()tries plain arithmetic first, then fusedmiller()retries after anyArithmeticError_crossis negatedRemoving any one branch of
_fmafailstest_fma, and no other test.Tests:
toxpasses, bothpyandquality, and the tests pass on Python 3.12 and 3.13. The 32 existingTestCrystallographyMillertests pass unchanged. Seven new tests intest_all.pycover:(1,1,1)and(10,10,10)forZeroDivisionErroron hosts with AVX-512OverflowErrorfrom fused arithmetic, which isn't retried_dot_fusedand_dot_plain, on inputs where they differ andmath.sumprodmatches neither_fma, against results frommath.fmaand for inputs thatmath.fmaraises onmiller()taking tuples and listsNot in this PR
Dockerfilestill installs numpy and scipy. Nothing in the repo uses it, and it was already out of date (it pins pyparsing 2.2.0), so I left it alone.🤖 Generated with Claude Code