{"id":23413354,"url":"https://github.com/shreyansh26/llm-sampling","last_synced_at":"2025-04-12T05:25:57.437Z","repository":{"id":267138140,"uuid":"864472135","full_name":"shreyansh26/LLM-Sampling","owner":"shreyansh26","description":"A collection of various LLM sampling methods implemented in pure Pytorch","archived":false,"fork":false,"pushed_at":"2024-12-09T09:40:09.000Z","size":130,"stargazers_count":21,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T00:51:17.194Z","etag":null,"topics":["llm","llm-inference","sampling-methods","torch","transformers"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shreyansh26.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-28T10:04:39.000Z","updated_at":"2025-03-16T20:21:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"e8c3f024-415d-43bb-87e5-70f6ecb2ede6","html_url":"https://github.com/shreyansh26/LLM-Sampling","commit_stats":null,"previous_names":["shreyansh26/llm-sampling"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyansh26%2FLLM-Sampling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyansh26%2FLLM-Sampling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyansh26%2FLLM-Sampling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyansh26%2FLLM-Sampling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shreyansh26","download_url":"https://codeload.github.com/shreyansh26/LLM-Sampling/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248521165,"owners_count":21118012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["llm","llm-inference","sampling-methods","torch","transformers"],"created_at":"2024-12-22T19:33:45.810Z","updated_at":"2025-04-12T05:25:57.429Z","avatar_url":"https://github.com/shreyansh26.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LLM Sampling Library\n\n## Overview\n\nThe LLM Sampling Library is a Python package designed to facilitate text generation using various sampling methods with large language models (LLMs). This library provides a simple command-line interface (CLI) for users to generate text based on input prompts, utilizing models from the Hugging Face Transformers library.\n\n## Features\n\n- **Multiple Sampling Methods**: Implements various sampling techniques including:\n  - Unconstrained sampling\n  - Top-k sampling\n  - Top-p (nucleus) sampling \n  - [Min-p sampling](https://arxiv.org/abs/2407.01082)\n  - [Typical sampling](https://arxiv.org/abs/2202.00666)\n  - [Epsilon sampling](https://arxiv.org/abs/2210.15191)\n  - [Eta sampling](https://arxiv.org/abs/2210.15191)\n  - Beam search\n  - [Chain-of-Thought (CoT) decoding](https://arxiv.org/abs/2402.10200)\n  - Constrained JSON decoding\n  - [Speculative sampling](https://arxiv.org/abs/2302.01318)\n  - [Medusa decoding](https://arxiv.org/abs/2401.10774)\n\n- **Chat Template Support**: Optionally apply chat templates for Instruct models\n- **Memory Efficient**: Uses KV-cache for better memory usage\n\nIn all implementations, temperature scaling is applied to the logits before any sampling methods, following the [GPT-2 implementation](https://github.com/openai/gpt-2/blob/9b63575ef42771a015060c964af2c3da4cf7c8ab/src/sample.py#L61C5-L72C14) and [HuggingFace's implementation](https://github.com/huggingface/transformers/blob/acc394c4f5e1283c19783581790b3dc3105a3697/src/transformers/generation/utils.py#L825C9-L847C23).\n\n## Installation\n\n```bash\npip install -r requirements.txt\n```\n\n## Usage\n\nBasic usage:\n\n```bash\npython generate.py --model \u003cmodel_name\u003e --prompt \"\u003cinput_prompt\u003e\" --apply-chat-template --temperature \u003ctemperature\u003e --method \u003csampling_method\u003e --max_new_tokens \u003cmax_new_tokens\u003e --hf-token \u003chugging_face_token\u003e --dtype \u003cdata_type\u003e\n```\n\n### Parameters\n\n- `--model`: The path or name of the Hugging Face model to use\n- `--prompt`: The input sequence for the model\n- `--prompt_file`: Alternative to --prompt, load prompt from a file\n- `--temperature`: Sampling temperature (default: 1.0)\n- `--method`: Sampling method to use (see list below)\n- `--max_new_tokens`: Maximum number of new tokens to generate (default: 500)\n- `--hf-token`: Your Hugging Face token for model access\n- `--dtype`: Data type for model (bfloat16, float16, float32)\n- `--seed`: Random seed for reproducibility\n\nMethod-specific parameters:\n- `--top_k`: K value for top-k sampling\n- `--top_p`: P value for nucleus sampling\n- `--min_p`: Threshold for min-p sampling\n- `--epsilon`: Epsilon value for epsilon/eta sampling\n- `--beam_width`: Beam width for beam search\n- `--typical_p_mass`: Mass parameter for typical sampling\n- `--json_schema`: Schema file path for constrained JSON sampling\n- `--draft-model`: Path to draft model for speculative sampling\n- `--medusa-model-heads`: Path to Medusa model heads\n- `--lookahead`: Lookahead parameter for speculative sampling\n\n## Examples\n\nTop-k sampling:\n```bash\npython generate.py --model meta-llama/Llama-3.1-8B-Instruct --prompt \"Tell me a story\" --method top_k --top_k 50 --temperature 0.7\n```\n\nNucleus sampling:\n```bash\npython generate.py --model meta-llama/Llama-3.1-8B-Instruct --prompt \"Write a poem\" --method top_p --top_p 0.9 --temperature 0.8\n```\n\nSpeculative sampling:\n```bash\npython generate.py --model meta-llama/Llama-3.1-8B-Instruct --prompt \"Explain quantum physics\" --method speculative --draft-model meta-llama/Llama-3.2-1B-Instruct --lookahead 4\n```\n\nMedusa decoding:\n```bash\npython generate.py --model meta-llama/Llama-3.1-8B-Instruct --prompt \"Write code for merge sort\" --method medusa --medusa-model-heads \u003cpath_to_heads\u003e\n```\n\n### Acknowledgements\n\n- [Medusa](https://github.com/FasterDecoding/Medusa) for the Medusa decoding implementation\n- [vLLM](https://github.com/vllm-project/vllm) used as the reference for the Medusa heads architecture\n- [Jsonformer](https://github.com/1rgs/jsonformer) for the constrained JSON decoding implementation\n- [HuggingFace Transformers](https://github.com/huggingface/transformers) for correctness evaluation\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyansh26%2Fllm-sampling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshreyansh26%2Fllm-sampling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyansh26%2Fllm-sampling/lists"}