Honor seed=0 in iterate_batches - #1661
Merged
michalk8 merged 1 commit intoAug 5, 2026
Merged
Conversation
iterate_batches gated its seeding on `if seed:`, so an explicit seed of 0 was silently ignored and batch order fell back to whatever state global numpy happened to be in. 0 is the default seed in mlx_lm.lora's CONFIG_DEFAULTS, which made the most common value the one that did not work. Use `if seed is not None:` instead. Add a regression test asserting a given seed produces the same batch order regardless of other numpy use in between, for both 0 and a non-zero seed. The test fails before this change.
michalk8
self-requested a review
August 5, 2026 12:47
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 #1660.
Summary
iterate_batchesgated its seeding onif seed:, so an explicitseed=0was silently ignored and batch order fell back to whatever state global numpy happened to be in.0is the default inmlx_lm.lora'sCONFIG_DEFAULTS, which made the most common value the one that did not work.Evaluation
Measured before the change, passing the seed explicitly and drawing from numpy in between to see whether the seed actually isolates the sequence:
After the change both are
True, and different seeds still produce different orders.The new test asserts exactly that, for
seed=0andseed=42. It fails onmain:and passes with the fix. Full suite:
Scope
Deliberately just the bug fix. Worth being clear about what this does not change, since #1660 could be read as a larger claim:
Batch order for a normal
mlx_lm.lorarun is already reproducible today.run()inlora.pycallsnp.random.seed(args.seed)before training,train()callsiterate_batcheswithout aseedargument, and nothing in the CLI training path draws from numpy in between, so the permutations come from that seeded global state. This fix does not alter that path at all.What it fixes is the case where
seedis passed explicitly and happens to be0, which is what you hit callingtrain()oriterate_batchesdirectly from Python, and which is the documented parameter for this.A second, larger question is whether
train()should passargs.seedthrough toiterate_batchesso batch order depends on an explicit seed rather than ambient global state. That would change batch order for a given seed relative to today, so I left it out. Happy to send it separately if you want it.Risks
Low. The only behavior change is that
seed=0now seeds instead of being ignored. Anyone previously passing0was getting unseeded behavior by accident; they now get deterministic ordering, which is what the argument promises. No change forseed=Noneor any non-zero seed, and no change to the CLI path.Pre-merge
Nothing. One-line change, no new dependencies, no config or docs updates needed.
Post-merge
Nothing required. If you'd like the
train()passthrough discussed above, I can open it as a follow-up.Environment: mlx-lm at e5baded, mlx 0.32.0, M2 Pro, macOS 26.5.2.