Upload folder using huggingface_hub
Browse files- README.md +128 -0
- SmolLM2-135M/.gitattributes +35 -0
- config.json +29 -0
- custom_generate/generate.py +192 -0
- generation_config.json +6 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- special_tokens_map.json +42 -0
- tokenizer.json +0 -0
- tokenizer_config.json +167 -0
- vocab.json +0 -0
README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags:
|
| 4 |
+
- custom_generate
|
| 5 |
+
- ancestral_sampling
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
# Multinomial (Ancestral) Sampling simple implementation
|
| 9 |
+
|
| 10 |
+
## Description
|
| 11 |
+
A clean, hackable implementation of ancestral sampling (multinomial sampling) with full KV cache support. This is a simplified alternative to the complex generation mixin in transformers, designed for readability and ease of modification while maintaining full performance.
|
| 12 |
+
|
| 13 |
+
The implementation supports both sampling and greedy decoding modes, with optional temperature scaling and top-k/top-p filtering.
|
| 14 |
+
|
| 15 |
+
## Base model
|
| 16 |
+
- [HuggingFaceTB/SmolLM2-135M-Instruct](https://huggingface.co/HuggingFaceTB/SmolLM2-135M-Instruct)
|
| 17 |
+
|
| 18 |
+
## Model compatibility
|
| 19 |
+
Most transformer LLM/VLM models trained for causal language modeling.
|
| 20 |
+
|
| 21 |
+
## Additional Arguments
|
| 22 |
+
- `temperature` (float): Sampling temperature (default: 1.0, higher = more random)
|
| 23 |
+
- `top_k` (int): Only consider top-k most probable tokens (default: None)
|
| 24 |
+
- `top_p` (float): Only consider tokens with cumulative probability <= top_p (default: None)
|
| 25 |
+
- `do_sample` (bool): Whether to use sampling (True, default) or greedy decoding (False)
|
| 26 |
+
|
| 27 |
+
## Output Type changes
|
| 28 |
+
When `return_dict_in_generate=True`, returns a dictionary with:
|
| 29 |
+
- `sequences`: Generated token IDs
|
| 30 |
+
- `scores`: Log probabilities of sampled tokens (with temperature/sampling modifications)
|
| 31 |
+
- `logps`: Original model log probabilities (T=1, no modifications)
|
| 32 |
+
- `prompt_lens`: Length of input prompts
|
| 33 |
+
- `lens`: Final sequence lengths
|
| 34 |
+
|
| 35 |
+
## Example usage
|
| 36 |
+
|
| 37 |
+
```py
|
| 38 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 39 |
+
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
|
| 41 |
+
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct", device_map="auto")
|
| 42 |
+
|
| 43 |
+
inputs = tokenizer(["The quick brown"], return_tensors="pt").to(model.device)
|
| 44 |
+
|
| 45 |
+
# Basic sampling
|
| 46 |
+
gen_out = model.generate(**inputs, custom_generate="manueldeprada/ancestral_sampling", trust_remote_code=True)
|
| 47 |
+
|
| 48 |
+
# With temperature
|
| 49 |
+
gen_out = model.generate(**inputs, custom_generate="manueldeprada/ancestral_sampling", temperature=0.8, trust_remote_code=True)
|
| 50 |
+
|
| 51 |
+
# With top-k
|
| 52 |
+
gen_out = model.generate(**inputs, custom_generate="manueldeprada/ancestral_sampling", top_k=50, trust_remote_code=True)
|
| 53 |
+
|
| 54 |
+
# With top-p (nucleus sampling)
|
| 55 |
+
gen_out = model.generate(**inputs, custom_generate="manueldeprada/ancestral_sampling", top_p=0.9, trust_remote_code=True)
|
| 56 |
+
|
| 57 |
+
# Greedy decoding (no sampling)
|
| 58 |
+
gen_out = model.generate(**inputs, custom_generate="manueldeprada/ancestral_sampling", do_sample=False, trust_remote_code=True)
|
| 59 |
+
|
| 60 |
+
# Get detailed output with probabilities
|
| 61 |
+
gen_out = model.generate(
|
| 62 |
+
**inputs,
|
| 63 |
+
custom_generate="manueldeprada/ancestral_sampling",
|
| 64 |
+
return_dict_in_generate=True,
|
| 65 |
+
trust_remote_code=True
|
| 66 |
+
)
|
| 67 |
+
print(f"Generated text: {tokenizer.batch_decode(gen_out.sequences, skip_special_tokens=True)}")
|
| 68 |
+
print(f"Sampling scores: {gen_out.scores}")
|
| 69 |
+
print(f"Model log probabilities: {gen_out.logps}")
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
## Algorithm
|
| 73 |
+
1. Initialize KV cache and prepare input sequences
|
| 74 |
+
2. For each generation step:
|
| 75 |
+
- Get logits from the model for the current sequence
|
| 76 |
+
- Apply temperature scaling to logits
|
| 77 |
+
- Optionally apply top-k filtering (keep only top-k tokens)
|
| 78 |
+
- Optionally apply top-p filtering (nucleus sampling)
|
| 79 |
+
- Convert to probabilities using softmax
|
| 80 |
+
- Sample from the probability distribution (or take argmax for greedy)
|
| 81 |
+
- Append the selected token to the sequence
|
| 82 |
+
- Update KV cache and track sequence completion
|
| 83 |
+
3. Return generated sequences and probability information
|
| 84 |
+
|
| 85 |
+
## Helper Functions for Custom Generation
|
| 86 |
+
|
| 87 |
+
The implementation provides two key helper functions that you can use to build your own generation strategies:
|
| 88 |
+
|
| 89 |
+
### `init_gen(model_kwargs, model, max_new_tokens, bos_token_id)`
|
| 90 |
+
Initializes the generation process and prepares the KV cache:
|
| 91 |
+
- Sets up input sequences and model inputs
|
| 92 |
+
- Prepares the KV cache for generation
|
| 93 |
+
- Returns updated `model_kwargs` and `input_ids`
|
| 94 |
+
|
| 95 |
+
### `ps_next(model, model_kwargs, input_ids)`
|
| 96 |
+
Gets the next token logits and updates the KV cache:
|
| 97 |
+
- Runs the model forward pass
|
| 98 |
+
- Extracts logits for the last token
|
| 99 |
+
- Updates the KV cache
|
| 100 |
+
- Returns updated `model_kwargs` and `logits`
|
| 101 |
+
|
| 102 |
+
### Example: Custom Generation Loop
|
| 103 |
+
|
| 104 |
+
```py
|
| 105 |
+
from ancestral_sampling.generate import init_gen, ps_next
|
| 106 |
+
|
| 107 |
+
def custom_generation(model, model_kwargs, max_new_tokens=20, temperature=1.0):
|
| 108 |
+
# Initialize generation
|
| 109 |
+
model_kwargs, input_ids = init_gen(model_kwargs, model, max_new_tokens, bos_token_id)
|
| 110 |
+
|
| 111 |
+
for i in range(max_new_tokens):
|
| 112 |
+
# Get next token logits
|
| 113 |
+
model_kwargs, logits = ps_next(model, model_kwargs, input_ids)
|
| 114 |
+
|
| 115 |
+
# Your custom logic here
|
| 116 |
+
probs = (logits / temperature).softmax(-1)
|
| 117 |
+
next_token = torch.multinomial(probs, 1)
|
| 118 |
+
|
| 119 |
+
# Append token and continue
|
| 120 |
+
input_ids = torch.cat([input_ids, next_token], dim=-1)
|
| 121 |
+
|
| 122 |
+
# Add your stopping conditions
|
| 123 |
+
if next_token.item() == eos_token_id:
|
| 124 |
+
break
|
| 125 |
+
|
| 126 |
+
return input_ids
|
| 127 |
+
```
|
| 128 |
+
|
SmolLM2-135M/.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
config.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LlamaForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 0,
|
| 8 |
+
"eos_token_id": 0,
|
| 9 |
+
"hidden_act": "silu",
|
| 10 |
+
"hidden_size": 576,
|
| 11 |
+
"initializer_range": 0.041666666666666664,
|
| 12 |
+
"intermediate_size": 1536,
|
| 13 |
+
"is_llama_config": true,
|
| 14 |
+
"max_position_embeddings": 8192,
|
| 15 |
+
"model_type": "llama",
|
| 16 |
+
"num_attention_heads": 9,
|
| 17 |
+
"num_hidden_layers": 30,
|
| 18 |
+
"num_key_value_heads": 3,
|
| 19 |
+
"pretraining_tp": 1,
|
| 20 |
+
"rms_norm_eps": 1e-05,
|
| 21 |
+
"rope_interleaved": false,
|
| 22 |
+
"rope_scaling": null,
|
| 23 |
+
"rope_theta": 100000,
|
| 24 |
+
"tie_word_embeddings": true,
|
| 25 |
+
"torch_dtype": "bfloat16",
|
| 26 |
+
"transformers_version": "4.40.1",
|
| 27 |
+
"use_cache": true,
|
| 28 |
+
"vocab_size": 49152
|
| 29 |
+
}
|
custom_generate/generate.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import GenerationConfig
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def ps_next(model, model_kwargs, input_ids):
|
| 6 |
+
"""
|
| 7 |
+
Auxiliary function to get the next token probabilities and update the KV cache.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
model: The language model
|
| 11 |
+
model_kwargs: Model keyword arguments including KV cache
|
| 12 |
+
input_ids: Current input token IDs
|
| 13 |
+
T: Temperature for sampling
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
Updated model_kwargs, probabilities at temperature T, probabilities at T=1
|
| 17 |
+
"""
|
| 18 |
+
model_inputs = model.prepare_inputs_for_generation(input_ids, **model_kwargs)
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**model_inputs, return_dict=True)
|
| 21 |
+
|
| 22 |
+
logits = outputs.logits[:, -1].detach()
|
| 23 |
+
model_kwargs = model._update_model_kwargs_for_generation(
|
| 24 |
+
outputs, model_kwargs, is_encoder_decoder=model.config.is_encoder_decoder
|
| 25 |
+
)
|
| 26 |
+
del outputs
|
| 27 |
+
return model_kwargs, logits
|
| 28 |
+
|
| 29 |
+
def init_gen(model_kwargs, model, max_new_tokens, bos_token_id):
|
| 30 |
+
"""
|
| 31 |
+
Auxiliary function to initialize the generation process and prepare the KV cache.
|
| 32 |
+
|
| 33 |
+
Args:
|
| 34 |
+
model_kwargs: Model keyword arguments
|
| 35 |
+
model: The language model
|
| 36 |
+
max_new_tokens: Maximum number of new tokens to generate
|
| 37 |
+
|
| 38 |
+
Returns:
|
| 39 |
+
Model keyword arguments and input token IDs
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
input_ids, model_input_name, model_kwargs = model._prepare_model_inputs(
|
| 43 |
+
None, bos_token_id, model_kwargs
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
batch_size = input_ids.shape[0]
|
| 47 |
+
model._prepare_cache_for_generation(
|
| 48 |
+
model.generation_config, model_kwargs, None, batch_size,
|
| 49 |
+
max_cache_length=max_new_tokens, device=input_ids.device
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Get initial cache position
|
| 53 |
+
model_kwargs = model._get_initial_cache_position(input_ids.shape[1], input_ids.device, model_kwargs)
|
| 54 |
+
return model_kwargs, input_ids
|
| 55 |
+
|
| 56 |
+
def _apply_top_k_top_p(ps, model):
|
| 57 |
+
if hasattr(model, 'generation_config') and hasattr(model.generation_config, 'top_k') and model.generation_config.top_k is not None:
|
| 58 |
+
top_k = model.generation_config.top_k
|
| 59 |
+
top_k = min(top_k, ps.size(-1))
|
| 60 |
+
indices_to_remove = ps < torch.topk(ps, top_k)[0][..., -1, None]
|
| 61 |
+
ps[indices_to_remove] = 0.0
|
| 62 |
+
ps = ps / ps.sum(dim=-1, keepdim=True)
|
| 63 |
+
|
| 64 |
+
# Apply top-p filtering if specified
|
| 65 |
+
if hasattr(model, 'generation_config') and hasattr(model.generation_config, 'top_p') and model.generation_config.top_p is not None:
|
| 66 |
+
top_p = model.generation_config.top_p
|
| 67 |
+
if top_p < 1.0:
|
| 68 |
+
sorted_probs, sorted_indices = torch.sort(ps, descending=True)
|
| 69 |
+
cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
|
| 70 |
+
|
| 71 |
+
# Remove tokens with cumulative probability above the threshold
|
| 72 |
+
sorted_indices_to_remove = cumulative_probs > top_p
|
| 73 |
+
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
|
| 74 |
+
sorted_indices_to_remove[..., 0] = 0
|
| 75 |
+
|
| 76 |
+
indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
|
| 77 |
+
ps[indices_to_remove] = 0.0
|
| 78 |
+
ps = ps / ps.sum(dim=-1, keepdim=True)
|
| 79 |
+
return ps
|
| 80 |
+
|
| 81 |
+
def ancestral_sampling(model_kwargs, model, eos_token_ids, pad_token_id, bos_token_id, do_sample=True, max_new_tokens=20, T=1.0):
|
| 82 |
+
"""
|
| 83 |
+
Ancestral sampling implementation with proper KV caching.
|
| 84 |
+
|
| 85 |
+
Args:
|
| 86 |
+
prompts: List of input prompts
|
| 87 |
+
model: The language model
|
| 88 |
+
max_new_tokens: Maximum number of new tokens to generate
|
| 89 |
+
eos_token_ids: List of end-of-sequence token IDs
|
| 90 |
+
pad_token_id: Padding token ID
|
| 91 |
+
bos_token_id: Beginning-of-sequence token ID
|
| 92 |
+
max_new_tokens: Maximum number of new tokens to generate
|
| 93 |
+
|
| 94 |
+
Returns:
|
| 95 |
+
Generated sequences, log probabilities, and metadata
|
| 96 |
+
"""
|
| 97 |
+
# Initialize the generation process and prepare the KV cache
|
| 98 |
+
model_kwargs, input_ids = init_gen(model_kwargs, model, max_new_tokens, bos_token_id)
|
| 99 |
+
batch_size, max_prompts_len = input_ids.shape
|
| 100 |
+
prompts_len = (input_ids != pad_token_id).sum(dim=-1)
|
| 101 |
+
|
| 102 |
+
# Keeps track of which sequences are finished and their lengths
|
| 103 |
+
active_seqs = input_ids.new_ones((batch_size, 1), dtype=torch.bool)
|
| 104 |
+
lens = torch.full((batch_size,), max_prompts_len, dtype=torch.long, device=input_ids.device)
|
| 105 |
+
# Modified log probabilities of the sequences
|
| 106 |
+
scores = torch.zeros((batch_size, max_new_tokens), dtype=torch.float32)
|
| 107 |
+
# Unfiltered sequence log probabilities (T=1, no sampling modifications)
|
| 108 |
+
logps = torch.zeros((batch_size, max_new_tokens), dtype=torch.float32)
|
| 109 |
+
|
| 110 |
+
for i in range(max_new_tokens):
|
| 111 |
+
# Get the next token probabilities and update the KV cache
|
| 112 |
+
model_kwargs, logits = ps_next(model, model_kwargs, input_ids)
|
| 113 |
+
# Original model probabilities (T=1, no sampling modifications)
|
| 114 |
+
model_ps = logits.softmax(-1)
|
| 115 |
+
# Sampling probabilities (T, with sampling modifications)
|
| 116 |
+
ps = (logits/T).softmax(-1)
|
| 117 |
+
ps = _apply_top_k_top_p(ps, model)
|
| 118 |
+
|
| 119 |
+
# Sample the next token and gather the log probabilities
|
| 120 |
+
if do_sample:
|
| 121 |
+
next_token_ids = torch.multinomial(ps, 1) * active_seqs + pad_token_id * ~active_seqs
|
| 122 |
+
else:
|
| 123 |
+
next_token_ids = torch.argmax(ps, dim=-1).unsqueeze(-1) * active_seqs + pad_token_id * ~active_seqs
|
| 124 |
+
next_token_logps = ps.gather(-1, next_token_ids).log()
|
| 125 |
+
next_token_model_logps = model_ps.gather(-1, next_token_ids).log()
|
| 126 |
+
|
| 127 |
+
input_ids = torch.cat([input_ids, next_token_ids], dim=-1)
|
| 128 |
+
scores[:, i] = (next_token_logps * active_seqs).squeeze()
|
| 129 |
+
logps[:, i] = (next_token_model_logps * active_seqs).squeeze()
|
| 130 |
+
|
| 131 |
+
lens += active_seqs.squeeze(-1).long()
|
| 132 |
+
active_seqs &= ~torch.isin(next_token_ids, eos_token_ids)
|
| 133 |
+
if active_seqs.sum() == 0:
|
| 134 |
+
break
|
| 135 |
+
return input_ids.detach().cpu(), scores[:,:i+1], logps[:,:i+1], prompts_len, lens.tolist()
|
| 136 |
+
|
| 137 |
+
def generate(model, **kwargs):
|
| 138 |
+
"""
|
| 139 |
+
Ancestral sampling strategy - multinomial sampling with temperature and optional top-k/top-p filtering.
|
| 140 |
+
Simple implementation with proper KV caching support.
|
| 141 |
+
|
| 142 |
+
Args:
|
| 143 |
+
model: The language model
|
| 144 |
+
model_kwargs: Model keyword arguments from the tokenizer
|
| 145 |
+
generation_config: Generation configuration
|
| 146 |
+
temperature: Sampling temperature (higher = more random)
|
| 147 |
+
top_k: Only consider top-k most probable tokens
|
| 148 |
+
top_p: Only consider tokens with cumulative probability <= top_p
|
| 149 |
+
**kwargs: Additional arguments
|
| 150 |
+
|
| 151 |
+
Returns:
|
| 152 |
+
Generated token IDs
|
| 153 |
+
"""
|
| 154 |
+
generation_config = model.generation_config
|
| 155 |
+
max_new_tokens = kwargs.get('max_new_tokens', generation_config.max_new_tokens)
|
| 156 |
+
do_sample = kwargs.get('do_sample', True)
|
| 157 |
+
eos_token_ids = kwargs.get('eos_token_ids', generation_config.eos_token_id)
|
| 158 |
+
if eos_token_ids is None:
|
| 159 |
+
raise ValueError("Model generation config does not have an EOS token id. You must provide it to generate() with the eos_token_ids argument.")
|
| 160 |
+
eos_token_ids = torch.as_tensor(eos_token_ids, device=model.device)
|
| 161 |
+
if eos_token_ids is not None and eos_token_ids.ndim == 0:
|
| 162 |
+
eos_token_ids = eos_token_ids.unsqueeze(0)
|
| 163 |
+
|
| 164 |
+
pad_token_id = kwargs.get('pad_token_id', generation_config.pad_token_id if generation_config.pad_token_id is not None else eos_token_ids[0])
|
| 165 |
+
bos_token_id = kwargs.get('bos_token_id', generation_config.bos_token_id)
|
| 166 |
+
if bos_token_id is None:
|
| 167 |
+
raise ValueError("Model generation config does not have a BOS token id. You must provide it to generate() with the bos_token_id argument.")
|
| 168 |
+
T = kwargs.get('temperature', 1.0)
|
| 169 |
+
return_dict = kwargs.get('return_dict_in_generate', False)
|
| 170 |
+
|
| 171 |
+
generated_ids, scores, logps, prompt_lens, lens = ancestral_sampling(
|
| 172 |
+
model_kwargs=kwargs,
|
| 173 |
+
model=model,
|
| 174 |
+
eos_token_ids=eos_token_ids,
|
| 175 |
+
pad_token_id=pad_token_id,
|
| 176 |
+
bos_token_id=bos_token_id,
|
| 177 |
+
do_sample=do_sample,
|
| 178 |
+
max_new_tokens=max_new_tokens,
|
| 179 |
+
T=T,
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
if return_dict:
|
| 183 |
+
return {
|
| 184 |
+
"sequences": generated_ids,
|
| 185 |
+
"scores": scores,
|
| 186 |
+
"logps": logps,
|
| 187 |
+
"prompt_lens": prompt_lens,
|
| 188 |
+
"lens": lens,
|
| 189 |
+
}
|
| 190 |
+
else:
|
| 191 |
+
return generated_ids
|
| 192 |
+
|
generation_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 0,
|
| 4 |
+
"eos_token_id": 0,
|
| 5 |
+
"transformers_version": "4.40.1"
|
| 6 |
+
}
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:80521b40281d6ce74e35c9282c22539e75aa0ac8578892b2a59955ef78d55da1
|
| 3 |
+
size 269060552
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
"<|endoftext|>",
|
| 4 |
+
"<|im_start|>",
|
| 5 |
+
"<|im_end|>",
|
| 6 |
+
"<repo_name>",
|
| 7 |
+
"<reponame>",
|
| 8 |
+
"<file_sep>",
|
| 9 |
+
"<filename>",
|
| 10 |
+
"<gh_stars>",
|
| 11 |
+
"<issue_start>",
|
| 12 |
+
"<issue_comment>",
|
| 13 |
+
"<issue_closed>",
|
| 14 |
+
"<jupyter_start>",
|
| 15 |
+
"<jupyter_text>",
|
| 16 |
+
"<jupyter_code>",
|
| 17 |
+
"<jupyter_output>",
|
| 18 |
+
"<jupyter_script>",
|
| 19 |
+
"<empty_output>"
|
| 20 |
+
],
|
| 21 |
+
"bos_token": {
|
| 22 |
+
"content": "<|endoftext|>",
|
| 23 |
+
"lstrip": false,
|
| 24 |
+
"normalized": false,
|
| 25 |
+
"rstrip": false,
|
| 26 |
+
"single_word": false
|
| 27 |
+
},
|
| 28 |
+
"eos_token": {
|
| 29 |
+
"content": "<|endoftext|>",
|
| 30 |
+
"lstrip": false,
|
| 31 |
+
"normalized": false,
|
| 32 |
+
"rstrip": false,
|
| 33 |
+
"single_word": false
|
| 34 |
+
},
|
| 35 |
+
"unk_token": {
|
| 36 |
+
"content": "<|endoftext|>",
|
| 37 |
+
"lstrip": false,
|
| 38 |
+
"normalized": false,
|
| 39 |
+
"rstrip": false,
|
| 40 |
+
"single_word": false
|
| 41 |
+
}
|
| 42 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"0": {
|
| 5 |
+
"content": "<|endoftext|>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": false,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
},
|
| 12 |
+
"1": {
|
| 13 |
+
"content": "<|im_start|>",
|
| 14 |
+
"lstrip": false,
|
| 15 |
+
"normalized": false,
|
| 16 |
+
"rstrip": false,
|
| 17 |
+
"single_word": false,
|
| 18 |
+
"special": true
|
| 19 |
+
},
|
| 20 |
+
"2": {
|
| 21 |
+
"content": "<|im_end|>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": false,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false,
|
| 26 |
+
"special": true
|
| 27 |
+
},
|
| 28 |
+
"3": {
|
| 29 |
+
"content": "<repo_name>",
|
| 30 |
+
"lstrip": false,
|
| 31 |
+
"normalized": false,
|
| 32 |
+
"rstrip": false,
|
| 33 |
+
"single_word": false,
|
| 34 |
+
"special": true
|
| 35 |
+
},
|
| 36 |
+
"4": {
|
| 37 |
+
"content": "<reponame>",
|
| 38 |
+
"lstrip": false,
|
| 39 |
+
"normalized": false,
|
| 40 |
+
"rstrip": false,
|
| 41 |
+
"single_word": false,
|
| 42 |
+
"special": true
|
| 43 |
+
},
|
| 44 |
+
"5": {
|
| 45 |
+
"content": "<file_sep>",
|
| 46 |
+
"lstrip": false,
|
| 47 |
+
"normalized": false,
|
| 48 |
+
"rstrip": false,
|
| 49 |
+
"single_word": false,
|
| 50 |
+
"special": true
|
| 51 |
+
},
|
| 52 |
+
"6": {
|
| 53 |
+
"content": "<filename>",
|
| 54 |
+
"lstrip": false,
|
| 55 |
+
"normalized": false,
|
| 56 |
+
"rstrip": false,
|
| 57 |
+
"single_word": false,
|
| 58 |
+
"special": true
|
| 59 |
+
},
|
| 60 |
+
"7": {
|
| 61 |
+
"content": "<gh_stars>",
|
| 62 |
+
"lstrip": false,
|
| 63 |
+
"normalized": false,
|
| 64 |
+
"rstrip": false,
|
| 65 |
+
"single_word": false,
|
| 66 |
+
"special": true
|
| 67 |
+
},
|
| 68 |
+
"8": {
|
| 69 |
+
"content": "<issue_start>",
|
| 70 |
+
"lstrip": false,
|
| 71 |
+
"normalized": false,
|
| 72 |
+
"rstrip": false,
|
| 73 |
+
"single_word": false,
|
| 74 |
+
"special": true
|
| 75 |
+
},
|
| 76 |
+
"9": {
|
| 77 |
+
"content": "<issue_comment>",
|
| 78 |
+
"lstrip": false,
|
| 79 |
+
"normalized": false,
|
| 80 |
+
"rstrip": false,
|
| 81 |
+
"single_word": false,
|
| 82 |
+
"special": true
|
| 83 |
+
},
|
| 84 |
+
"10": {
|
| 85 |
+
"content": "<issue_closed>",
|
| 86 |
+
"lstrip": false,
|
| 87 |
+
"normalized": false,
|
| 88 |
+
"rstrip": false,
|
| 89 |
+
"single_word": false,
|
| 90 |
+
"special": true
|
| 91 |
+
},
|
| 92 |
+
"11": {
|
| 93 |
+
"content": "<jupyter_start>",
|
| 94 |
+
"lstrip": false,
|
| 95 |
+
"normalized": false,
|
| 96 |
+
"rstrip": false,
|
| 97 |
+
"single_word": false,
|
| 98 |
+
"special": true
|
| 99 |
+
},
|
| 100 |
+
"12": {
|
| 101 |
+
"content": "<jupyter_text>",
|
| 102 |
+
"lstrip": false,
|
| 103 |
+
"normalized": false,
|
| 104 |
+
"rstrip": false,
|
| 105 |
+
"single_word": false,
|
| 106 |
+
"special": true
|
| 107 |
+
},
|
| 108 |
+
"13": {
|
| 109 |
+
"content": "<jupyter_code>",
|
| 110 |
+
"lstrip": false,
|
| 111 |
+
"normalized": false,
|
| 112 |
+
"rstrip": false,
|
| 113 |
+
"single_word": false,
|
| 114 |
+
"special": true
|
| 115 |
+
},
|
| 116 |
+
"14": {
|
| 117 |
+
"content": "<jupyter_output>",
|
| 118 |
+
"lstrip": false,
|
| 119 |
+
"normalized": false,
|
| 120 |
+
"rstrip": false,
|
| 121 |
+
"single_word": false,
|
| 122 |
+
"special": true
|
| 123 |
+
},
|
| 124 |
+
"15": {
|
| 125 |
+
"content": "<jupyter_script>",
|
| 126 |
+
"lstrip": false,
|
| 127 |
+
"normalized": false,
|
| 128 |
+
"rstrip": false,
|
| 129 |
+
"single_word": false,
|
| 130 |
+
"special": true
|
| 131 |
+
},
|
| 132 |
+
"16": {
|
| 133 |
+
"content": "<empty_output>",
|
| 134 |
+
"lstrip": false,
|
| 135 |
+
"normalized": false,
|
| 136 |
+
"rstrip": false,
|
| 137 |
+
"single_word": false,
|
| 138 |
+
"special": true
|
| 139 |
+
}
|
| 140 |
+
},
|
| 141 |
+
"additional_special_tokens": [
|
| 142 |
+
"<|endoftext|>",
|
| 143 |
+
"<|im_start|>",
|
| 144 |
+
"<|im_end|>",
|
| 145 |
+
"<repo_name>",
|
| 146 |
+
"<reponame>",
|
| 147 |
+
"<file_sep>",
|
| 148 |
+
"<filename>",
|
| 149 |
+
"<gh_stars>",
|
| 150 |
+
"<issue_start>",
|
| 151 |
+
"<issue_comment>",
|
| 152 |
+
"<issue_closed>",
|
| 153 |
+
"<jupyter_start>",
|
| 154 |
+
"<jupyter_text>",
|
| 155 |
+
"<jupyter_code>",
|
| 156 |
+
"<jupyter_output>",
|
| 157 |
+
"<jupyter_script>",
|
| 158 |
+
"<empty_output>"
|
| 159 |
+
],
|
| 160 |
+
"bos_token": "<|endoftext|>",
|
| 161 |
+
"clean_up_tokenization_spaces": false,
|
| 162 |
+
"eos_token": "<|endoftext|>",
|
| 163 |
+
"model_max_length": 8192,
|
| 164 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 165 |
+
"unk_token": "<|endoftext|>",
|
| 166 |
+
"vocab_size": 49152
|
| 167 |
+
}
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|