Repository navigation
fix: pass the request seed to the sampler, and survive a negative one - #220
Merged
Merged
Conversation
The samplers draw from their own RandomState, which only GenerateParameters.seed seeds. Seeding MLX's global generator alone left the request's `seed` without effect: the same seeded request sampled differently every time. Both handlers now set GenerateParameters.seed; the global seed call stays for the paths that use MLXRandom directly. UInt64(seed) trapped on a negative value, so `"seed": -1` took the whole server down. samplingSeed(_:) takes the bit pattern instead. Closes SharpAI#219.
This was referenced Oct 9, 2026
HamedShams
added a commit
to HamedShams/SwiftLM
that referenced
this pull request
Oct 10, 2026
Both SharpAI#220 and this branch added a block before the results footer of tests/test-contract.sh. The seed assertions keep their numbers (9, 9b); the request-shape assertions move to 10 and 10b.
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 #219.
What
seednow reaches the sampler. Both handlers setGenerateParameters.seed, which is whatTopPSampler/CategoricalSampleractually use; the globalMLXRandom.seedcall stays for the paths that callMLXRandomdirectly.UInt64(seed)trapped on it ("Negative value is not representable",Server.swift:2387); the newsamplingSeed(_:)takes the bit pattern instead, so every distinctIntis still a distinct seed.Behaviour without a
seedis unchanged: the sampler keeps its entropy-seededRandomState.Why
The samplers draw from their own
RandomState, seeded only throughGenerateParameters.seed. Seeding MLX's global generator alone meantseedwas accepted and silently ignored: the same request withseed: 42three times gave three different answers. And since OpenAI'sseedis a plain integer, a client sending-1took the whole server down with one request (crash report in #219).Verified
SamplingSeedTests(new): theInt → UInt64mapping, and that two samplers built with the sameGenerateParameters.seeddraw the same tokens while different seeds differ. Red onmainfor the negative-seed case (trap) and green here.tests/test-contract.shgained two assertions: the same seeded request twice returns the identical message, and"seed": -1answers 200 with/healthstill responding. Both fail onmain(the second one by killing the server).swift test --filter SwiftLMTests: 280 tests green locally (M1 Max, Xcode 27 / Swift 6.4).tests/test-contract.shagainst the CI model (mlx-community/Qwen2.5-0.5B-Instruct-4bit): 12 passed, 0 failed, 2 skipped (the script's own tool-call / JSON capability skips).