Skip to content

rand=-1 to use a random seed, not rand=0 as documented #386

Closed

Description

@rmngllnn

Hello,

I use this library to use llama-cpp in Python.

It's working very well but the generation is always the same (no seed in the script). I use this function

from llama_cpp import Llama

llama = Llama("./path/models/7B/ggml-model-7b.bin")

gen = llama.create_completion("Cite the best movie citation you know.", 
                         max_tokens=50, 
                         temperature=0.95, 
                         top_p=0.95)

I ran it several times and it's always the same text generated. Is there like HuggingFace a do_sample parameter to trigger ? I didn't find anything in the documentation or the issues of this github (but maybe I missed it)

Thank you, whoever you are who will help me :)

Activity

  1. gjmulder commented on Jun 16, 2023

    @gjmulder
    Contributor

    Try setting seed=0 when you init the model:

    llama_cpp.llama.Llama.init

  2. rmngllnn commented on Jun 16, 2023

    @rmngllnn
    Author

    Same thing happens. Got the same generation with three runs.

  3. added
    qualityQuality of model output
    bugSomething isn't working
    llama.cppProblem with llama.cpp shared lib
    and removed
    qualityQuality of model output
    on Jun 16, 2023
  4. gjmulder commented on Jun 16, 2023

    @gjmulder
    Contributor

    That's strange. Can you try -1?

    It looks like seed is passed to libllama.so:

    llama.cpp$ ./main --help 2>&1 | grep seed
      -s SEED, --seed SEED  RNG seed (default: -1, use random seed for < 0)
    
  5. rmngllnn commented on Jun 16, 2023

    @rmngllnn
    Author

    It's working by passing -1 to seed

    According to documentation , default is to 1337 and 0 value for random seed.

    -1 does allow to have different generation for each run.

    Thank you.

  6. added
    documentationImprovements or additions to documentation
    and removed
    llama.cppProblem with llama.cpp shared lib
    on Jun 16, 2023
  7. gjmulder commented on Jun 16, 2023

    @gjmulder
    Contributor

    Bug in the docs then 😄

  8. rmngllnn commented on Jun 16, 2023

    @rmngllnn
    Author

    Apparently ... which is unfortunate ...

    Thank you for your (fast) response and help :)

  9. changed the title [-]Always the same generation[/-] [+]rand=-1 to use a random seed, not rand=0 as documented[/+] on Jun 17, 2023
  10. batrasrishti commented on Oct 9, 2023

    @batrasrishti

    It's working by passing -1 to seed

    According to documentation , default is to 1337 and 0 value for random seed.

    -1 does allow to have different generation for each run.

    Thank you.

    How to get same generated answer every single time?

  11. added a commit that references this issue on Feb 5, 2024
    d410f12
  12. gerrynjenny commented on Jul 25, 2024

    @gerrynjenny

    The above pointers were not permalinks so the urls are now 404. So I'm sharing this for others looking for the seed code:

    seed: int = llama_cpp.LLAMA_DEFAULT_SEED,

    seed: RNG seed, -1 for random

    Screenshot 2024-07-25 at 09 23 14
  13. dentropy commented on Nov 6, 2024

    @dentropy
    top_p=0.95)

    Seems like this was the magic variable we need to set, setting seed and temerature by themselves does not work

  14. sswam commented on May 26, 2025

    @sswam

    Setting seed=-1 still does not give a random seed. The LLM behaves identically at each start up. I'm working around this by setting seed=random.randint(0, 2**32).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocumentationImprovements or additions to documentation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions