diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa7e917b..d5be8e345 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix: honor zero-valued constructor seeds by @remote-controlled-man in #2444 - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds diff --git a/llama_cpp/llama.py b/llama_cpp/llama.py index 1b4daf6b9..f9dfd660d 100644 --- a/llama_cpp/llama.py +++ b/llama_cpp/llama.py @@ -313,7 +313,7 @@ def __init__( self.n_threads_batch = n_threads_batch or multiprocessing.cpu_count() # Used by the sampler - self._seed = seed or llama_cpp.LLAMA_DEFAULT_SEED + self._seed = seed if seed is not None else llama_cpp.LLAMA_DEFAULT_SEED # Context Params self.context_params = llama_cpp.llama_context_default_params() diff --git a/tests/test_llama.py b/tests/test_llama.py index 8c1512d49..338995cfb 100644 --- a/tests/test_llama.py +++ b/tests/test_llama.py @@ -267,6 +267,32 @@ def logit_processor_func(input_ids, logits): assert number_1 == number_3 +@pytest.mark.parametrize("seed", [0, 1337]) +def test_constructor_seed_matches_set_seed(llama_cpp_transformer_model_path, seed): + model = llama_cpp.Llama( + llama_cpp_transformer_model_path, + seed=seed, + n_ctx=128, + n_batch=128, + n_ubatch=128, + n_threads=1, + n_threads_batch=1, + verbose=False, + ) + try: + tokens = model.tokenize(b"Once upon a time") + model.eval(tokens) + assert model.save_state().seed == seed + + actual = list(itertools.islice(model.generate(tokens, temp=1.0), 16)) + model.set_seed(seed) + expected = list(itertools.islice(model.generate(tokens, temp=1.0), 16)) + + assert actual == expected + finally: + model.close() + + def test_real_llama_repeated_prompt_cache(llama_cpp_model_path): model = llama_cpp.Llama( llama_cpp_model_path,