初始化项目,由ModelHub XC社区提供模型
Model: transformers-community/dola Source: Original Platform
This commit is contained in:
36
.gitattributes
vendored
Normal file
36
.gitattributes
vendored
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
*.7z filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.gz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.model filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npy filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.parquet filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pickle filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pkl filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.rar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||||
|
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tgz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.wasm filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.xz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zst filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
||||||
130
README.md
Normal file
130
README.md
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
---
|
||||||
|
|
||||||
|
library_name: transformers
|
||||||
|
tags:
|
||||||
|
- custom_generate
|
||||||
|
---
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
Implementation of [Decoding by Contrasting Layers (DoLa)](https://huggingface.co/papers/2309.03883),
|
||||||
|
a contrastive decoding strategy for improving factuality and reducing hallucinations in language model outputs.
|
||||||
|
|
||||||
|
DoLa works by **contrasting the logits** from the final layer with those from earlier layers of the model,
|
||||||
|
amplifying factual knowledge localized in specific layers and suppressing spurious information.
|
||||||
|
|
||||||
|
This can be useful for:
|
||||||
|
|
||||||
|
* **Short-answer tasks** (e.g., TruthfulQA) — using higher layers (`dola_layers="high"`)
|
||||||
|
* **Long-answer reasoning tasks** (e.g., GSM8K, StrategyQA, FACTOR, VicunaQA) — using lower layers (`dola_layers="low"`)
|
||||||
|
|
||||||
|
DoLa is **not recommended for smaller models** such as GPT-2, as the improvement may be negligible.
|
||||||
|
|
||||||
|
This implementation matches the `DoLa` functionality present in `transformers<4.53.0`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Base model
|
||||||
|
|
||||||
|
* [Qwen/Qwen3-0.6B](https://huggingface.co/Qwen/Qwen3-0.6B)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Model compatibility
|
||||||
|
|
||||||
|
* Decoder-only transformer models
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Additional Arguments
|
||||||
|
|
||||||
|
* **`dola_layers`** (*str* or *List\[int]*, optional):
|
||||||
|
Which earlier layers to contrast with the final layer. Can be:
|
||||||
|
|
||||||
|
* `"low"` — lower half of layers (recommended for long answers)
|
||||||
|
* `"high"` — upper half of layers (recommended for short answers)
|
||||||
|
* List of integer indices (e.g., `[18, 20]`)
|
||||||
|
|
||||||
|
**Note:**
|
||||||
|
|
||||||
|
* Layer 0 is the word embedding; layer 1 is the first transformer block.
|
||||||
|
* If the model has tied word embeddings, layer 0 is skipped and counting starts at layer 2.
|
||||||
|
* Typical defaults:
|
||||||
|
|
||||||
|
| # Layers | `"low"` range | `"high"` range |
|
||||||
|
| -------- | ------------------- | ------------------- |
|
||||||
|
| > 40 | `(0, 20, 2)` | `(N - 20, N, 2)` |
|
||||||
|
| ≤ 40 | `range(0, N//2, 2)` | `range(N//2, N, 2)` |
|
||||||
|
|
||||||
|
* **`repetition_penalty`** (*float*, optional, defaults to `None`):
|
||||||
|
Helps reduce repetition. A value of `1.2` is recommended.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Output Type changes
|
||||||
|
|
||||||
|
* The `generate` method output remains the same as default `transformers` generation,
|
||||||
|
but logits are post-processed using the DoLa contrastive scoring before token selection.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Example usage
|
||||||
|
|
||||||
|
### Using higher layers (short-answer tasks)
|
||||||
|
|
||||||
|
```python
|
||||||
|
# requires `transformers>=4.56.0`, previously, it was part of the library
|
||||||
|
import torch
|
||||||
|
from transformers import AutoModelForCausalLM, AutoTokenizer, infer_device
|
||||||
|
|
||||||
|
device = infer_device()
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
"Qwen/Qwen3-0.6B", torch_dtype=torch.float16
|
||||||
|
).to(device)
|
||||||
|
|
||||||
|
inputs = tokenizer("What is the highest peak in the world?", return_tensors="pt").to(device)
|
||||||
|
|
||||||
|
outputs = model.generate(
|
||||||
|
**inputs,
|
||||||
|
max_new_tokens=50,
|
||||||
|
do_sample=False,
|
||||||
|
custom_generate="transformers-community/dola",
|
||||||
|
trust_remote_code=True,
|
||||||
|
dola_layers="high"
|
||||||
|
)
|
||||||
|
|
||||||
|
print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Contrasting specific layers
|
||||||
|
|
||||||
|
```python
|
||||||
|
import torch
|
||||||
|
from transformers import AutoModelForCausalLM, AutoTokenizer, infer_device
|
||||||
|
|
||||||
|
device = infer_device()
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
"Qwen/Qwen3-0.6B", torch_dtype=torch.float16
|
||||||
|
).to(device)
|
||||||
|
|
||||||
|
inputs = tokenizer("What is the highest peak in the world?", return_tensors="pt").to(device)
|
||||||
|
|
||||||
|
outputs = model.generate(
|
||||||
|
**inputs,
|
||||||
|
max_new_tokens=50,
|
||||||
|
do_sample=False,
|
||||||
|
repetition_penalty=1.2,
|
||||||
|
custom_generate="transformers-community/dola",
|
||||||
|
trust_remote_code=True,
|
||||||
|
dola_layers=[18, 20]
|
||||||
|
)
|
||||||
|
|
||||||
|
# Only decode the newly generated tokens
|
||||||
|
print(tokenizer.batch_decode(outputs[:, inputs.input_ids.shape[-1]:], skip_special_tokens=True))
|
||||||
|
```
|
||||||
30
config.json
Normal file
30
config.json
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"architectures": [
|
||||||
|
"Qwen3ForCausalLM"
|
||||||
|
],
|
||||||
|
"attention_bias": false,
|
||||||
|
"attention_dropout": 0.0,
|
||||||
|
"bos_token_id": 151643,
|
||||||
|
"eos_token_id": 151645,
|
||||||
|
"head_dim": 128,
|
||||||
|
"hidden_act": "silu",
|
||||||
|
"hidden_size": 1024,
|
||||||
|
"initializer_range": 0.02,
|
||||||
|
"intermediate_size": 3072,
|
||||||
|
"max_position_embeddings": 40960,
|
||||||
|
"max_window_layers": 28,
|
||||||
|
"model_type": "qwen3",
|
||||||
|
"num_attention_heads": 16,
|
||||||
|
"num_hidden_layers": 28,
|
||||||
|
"num_key_value_heads": 8,
|
||||||
|
"rms_norm_eps": 1e-06,
|
||||||
|
"rope_scaling": null,
|
||||||
|
"rope_theta": 1000000,
|
||||||
|
"sliding_window": null,
|
||||||
|
"tie_word_embeddings": true,
|
||||||
|
"torch_dtype": "bfloat16",
|
||||||
|
"transformers_version": "4.56.0",
|
||||||
|
"use_cache": true,
|
||||||
|
"use_sliding_window": false,
|
||||||
|
"vocab_size": 151936
|
||||||
|
}
|
||||||
350
custom_generate/generate.py
Normal file
350
custom_generate/generate.py
Normal file
@@ -0,0 +1,350 @@
|
|||||||
|
from typing import Union
|
||||||
|
import torch
|
||||||
|
from transformers import LogitsProcessorList, StoppingCriteriaList, GenerationConfig
|
||||||
|
from transformers.generation.utils import GenerationMixin, GenerateNonBeamOutput, GenerateDecoderOnlyOutput
|
||||||
|
import torch.nn as nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
import numpy as np
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _relative_top_filter(
|
||||||
|
scores: torch.FloatTensor,
|
||||||
|
baseline_scores: torch.FloatTensor,
|
||||||
|
relative_top: float = 0.1,
|
||||||
|
filter_value: float = -float("Inf"),
|
||||||
|
base_filter_value=-1e-3,
|
||||||
|
min_tokens_to_keep: int = 1,
|
||||||
|
) -> tuple[torch.FloatTensor, torch.FloatTensor]:
|
||||||
|
"""
|
||||||
|
Reference: https://github.com/XiangLi1999/ContrastiveDecoding/blob/170e9142e92159c1237d731e240f5eb14aabf428/transformers/src/transformers/generation_logits_process.py#L235
|
||||||
|
Apply filtering to only keep tokens with a probability above a certain threshold. The threshold is defined as `relative_top` * max probability in the distribution.
|
||||||
|
"""
|
||||||
|
scores_normalized = scores.log_softmax(dim=-1)
|
||||||
|
baseline_scores_normalized = baseline_scores.log_softmax(dim=-1)
|
||||||
|
sorted_logits, sorted_indices = torch.sort(scores_normalized, descending=True)
|
||||||
|
min_thresh = sorted_logits[..., min_tokens_to_keep - 1]
|
||||||
|
probs_max = torch.max(scores_normalized, dim=-1).values
|
||||||
|
probs_thresh = probs_max + np.log(relative_top)
|
||||||
|
probs_thresh = torch.min(min_thresh, probs_thresh)
|
||||||
|
probs_thresh = probs_thresh.unsqueeze(-1)
|
||||||
|
baseline_scores_normalized[scores_normalized < probs_thresh] = base_filter_value
|
||||||
|
scores_normalized[scores_normalized < probs_thresh] = filter_value
|
||||||
|
return scores_normalized, baseline_scores_normalized
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def _dola_select_contrast(
|
||||||
|
candidate_premature_layers: list[int],
|
||||||
|
candidate_premature_logits: dict[int, torch.FloatTensor],
|
||||||
|
final_logits: torch.FloatTensor,
|
||||||
|
) -> torch.FloatTensor:
|
||||||
|
if len(candidate_premature_layers) == 1:
|
||||||
|
base_logits = candidate_premature_logits[candidate_premature_layers[0]]
|
||||||
|
final_logits, base_logits = _relative_top_filter(final_logits, base_logits)
|
||||||
|
logits = final_logits - base_logits
|
||||||
|
return logits
|
||||||
|
|
||||||
|
# 1. Stacking all premature_layers into a new dimension
|
||||||
|
stacked_premature_layers = torch.stack([candidate_premature_logits[i] for i in candidate_premature_layers], dim=0)
|
||||||
|
|
||||||
|
# 2. Calculate the softmax values for mature_layer and all premature_layers
|
||||||
|
# shape: (batch_size, vocab_size)
|
||||||
|
softmax_mature_layer = F.softmax(final_logits, dim=-1)
|
||||||
|
# shape: (num_premature_layers, batch_size, vocab_size)
|
||||||
|
softmax_premature_layers = F.softmax(stacked_premature_layers, dim=-1)
|
||||||
|
|
||||||
|
# 3. Calculate the average distribution
|
||||||
|
# shape: (num_premature_layers, batch_size, vocab_size)
|
||||||
|
avg_dist = 0.5 * (softmax_mature_layer[None, :, :] + softmax_premature_layers)
|
||||||
|
|
||||||
|
# 4. Calculate log-softmax for the KL divergence
|
||||||
|
# shape: (batch_size, vocab_size)
|
||||||
|
log_softmax_mature_layer = F.log_softmax(final_logits, dim=-1)
|
||||||
|
# shape: (num_premature_layers, batch_size, vocab_size)
|
||||||
|
log_softmax_premature_layers = F.log_softmax(stacked_premature_layers, dim=-1)
|
||||||
|
|
||||||
|
# 5. Calculate the KL divergences and then the JS divergences
|
||||||
|
# shape: (num_premature_layers, batch_size)
|
||||||
|
kl1 = F.kl_div(log_softmax_mature_layer[None, :, :], avg_dist, reduction="none").mean(-1)
|
||||||
|
# shape: (num_premature_layers, batch_size)
|
||||||
|
kl2 = F.kl_div(log_softmax_premature_layers, avg_dist, reduction="none").mean(-1)
|
||||||
|
js_divs = 0.5 * (kl1 + kl2) # shape: (num_premature_layers, batch_size)
|
||||||
|
|
||||||
|
# 6. Reduce the batchmean
|
||||||
|
js_divs = js_divs.mean(-1) # shape: (num_premature_layers,)
|
||||||
|
premature_layer = candidate_premature_layers[int(js_divs.argmax().item())]
|
||||||
|
|
||||||
|
base_logits = candidate_premature_logits[premature_layer]
|
||||||
|
final_logits, base_logits = _relative_top_filter(final_logits, base_logits)
|
||||||
|
logits = final_logits - base_logits
|
||||||
|
return logits
|
||||||
|
|
||||||
|
def _dola_decoding(
|
||||||
|
model,
|
||||||
|
input_ids: torch.LongTensor,
|
||||||
|
logits_processor: LogitsProcessorList,
|
||||||
|
stopping_criteria: StoppingCriteriaList,
|
||||||
|
generation_config: GenerationConfig,
|
||||||
|
synced_gpus: bool = False,
|
||||||
|
streamer: "BaseStreamer" = None,
|
||||||
|
**model_kwargs,
|
||||||
|
) -> Union[GenerateNonBeamOutput, torch.LongTensor]:
|
||||||
|
r"""
|
||||||
|
Generates sequences of token ids for models with a language modeling head using **dola decoding** and can be
|
||||||
|
used for decoder-only text models.
|
||||||
|
The method is based on the paper "DoLa: Decoding by Contrasting Layers Improves Factuality in Large Language
|
||||||
|
Models" (https://huggingface.co/papers/2309.03883) in ICLR 2024.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
||||||
|
The sequence used as a prompt for the generation.
|
||||||
|
dola_layers (`Union[str, list[int]]`):
|
||||||
|
The candidate layers used in contrasting layers of DoLa. It can be either 1) 'low' or 'high', which
|
||||||
|
means the lower part or higher part of the model layers, respectively, or 2) a list of layer indices
|
||||||
|
to be used for candidate layers. The 0-th layer is the word embedding layer of the model.
|
||||||
|
logits_processor (`LogitsProcessorList`):
|
||||||
|
An instance of [`LogitsProcessorList`]. List of instances of class derived from [`LogitsProcessor`]
|
||||||
|
used to modify the prediction scores of the language modeling head applied at each generation step.
|
||||||
|
stopping_criteria (`StoppingCriteriaList`, *optional*):
|
||||||
|
An instance of [`StoppingCriteriaList`]. List of instances of class derived from [`StoppingCriteria`]
|
||||||
|
used to tell if the generation loop should stop.
|
||||||
|
generation_config ([`~generation.GenerationConfig`]):
|
||||||
|
The generation configuration to be used as parametrization of the decoding method.
|
||||||
|
synced_gpus (`bool`, *optional*, defaults to `False`):
|
||||||
|
Whether to continue running the while loop until max_length (needed to avoid deadlocking with
|
||||||
|
`FullyShardedDataParallel` and DeepSpeed ZeRO Stage 3).
|
||||||
|
streamer (`BaseStreamer`, *optional*):
|
||||||
|
Streamer object that will be used to stream the generated sequences. Generated tokens are passed
|
||||||
|
through `streamer.put(token_ids)` and the streamer is responsible for any further processing.
|
||||||
|
model_kwargs:
|
||||||
|
Additional model specific keyword arguments will be forwarded to the `forward` function of the model.
|
||||||
|
If model is an encoder-decoder model the kwargs should include `encoder_outputs`.
|
||||||
|
|
||||||
|
Return:
|
||||||
|
[`~generation.GenerateDecoderOnlyOutput`], [`~generation.GenerateEncoderDecoderOutput`]
|
||||||
|
or `torch.LongTensor`: A `torch.LongTensor` containing the generated tokens (default behaviour) or a
|
||||||
|
[`~generation.GenerateDecoderOnlyOutput`] if `model.config.is_encoder_decoder=False` and
|
||||||
|
`return_dict_in_generate=True` or a [`~generation.GenerateEncoderDecoderOutput`] if
|
||||||
|
`model.config.is_encoder_decoder=True`.
|
||||||
|
"""
|
||||||
|
dola_layers: Union[str, list[int]] = generation_config.dola_layers
|
||||||
|
|
||||||
|
|
||||||
|
# 1. General sanity checks
|
||||||
|
# A few arguments are not allowed, especially arguments that control caches.
|
||||||
|
assert dola_layers is not None, "dola_layers must be set to use DoLa decoding"
|
||||||
|
|
||||||
|
# DoLa generation needs num_beams == 1
|
||||||
|
if getattr(generation_config, "num_beams", 1) != 1:
|
||||||
|
raise ValueError("DoLa generation needs num_beams == 1")
|
||||||
|
|
||||||
|
if model.config.is_encoder_decoder:
|
||||||
|
raise ValueError("DoLa decoding is only available for decoder-only models.")
|
||||||
|
|
||||||
|
if generation_config.repetition_penalty < 1.2:
|
||||||
|
logger.warning(
|
||||||
|
f"`repetition_penalty` is set to a value of {generation_config.repetition_penalty}, which could induce unwanted repetition. "
|
||||||
|
"The recommended value for DoLa decoding is `repetition_penalty>=1.2`.",
|
||||||
|
)
|
||||||
|
|
||||||
|
if getattr(model, "_is_stateful", False):
|
||||||
|
# DoLa decoding was not designed for stateful models, and would require some changes
|
||||||
|
raise ValueError(
|
||||||
|
f"DoLa decoding is not supported with stateful models, such as {model.__class__.__name__}"
|
||||||
|
)
|
||||||
|
|
||||||
|
if model.config.is_encoder_decoder:
|
||||||
|
raise ValueError("DoLa decoding is only available for decoder-only models.")
|
||||||
|
|
||||||
|
# init values
|
||||||
|
pad_token_id = generation_config._pad_token_tensor
|
||||||
|
output_attentions = generation_config.output_attentions
|
||||||
|
output_hidden_states = generation_config.output_hidden_states
|
||||||
|
output_scores = generation_config.output_scores
|
||||||
|
output_logits = generation_config.output_logits
|
||||||
|
return_dict_in_generate = generation_config.return_dict_in_generate
|
||||||
|
has_eos_stopping_criteria = any(hasattr(criteria, "eos_token_id") for criteria in stopping_criteria)
|
||||||
|
do_sample = generation_config.do_sample
|
||||||
|
|
||||||
|
# init attention / hidden states / scores tuples
|
||||||
|
scores = () if (return_dict_in_generate and output_scores) else None
|
||||||
|
raw_logits = () if (return_dict_in_generate and output_logits) else None
|
||||||
|
decoder_attentions = () if (return_dict_in_generate and output_attentions) else None
|
||||||
|
cross_attentions = () if (return_dict_in_generate and output_attentions) else None
|
||||||
|
decoder_hidden_states = () if (return_dict_in_generate and output_hidden_states) else None
|
||||||
|
|
||||||
|
# keep track of which sequences are already finished
|
||||||
|
batch_size, cur_length = input_ids.shape[:2]
|
||||||
|
unfinished_sequences = torch.ones(batch_size, dtype=torch.long, device=input_ids.device)
|
||||||
|
# Does not exist anymore in recent versions!
|
||||||
|
if hasattr(model, "_get_initial_cache_position"):
|
||||||
|
model_kwargs = model._get_initial_cache_position(cur_length, input_ids.device, model_kwargs)
|
||||||
|
|
||||||
|
this_peer_finished = False
|
||||||
|
|
||||||
|
# prepare layers for DoLa decoding
|
||||||
|
final_layer = model.config.get_text_config().num_hidden_layers
|
||||||
|
# if the model has tied word embeddings, we skip the word embeddings (0-th) layer and start from the 2nd layer,
|
||||||
|
# as the early exit from word embeddings will become identity function
|
||||||
|
# if the model is really shallow (<=2 layers), we use the 1st layer if it's not the final layer and the 0-th
|
||||||
|
# layer otherwise. Notice that DoLa does not help shallow models much.
|
||||||
|
if not model.config.tie_word_embeddings:
|
||||||
|
start_layer = 0
|
||||||
|
elif final_layer > 2:
|
||||||
|
start_layer = 2
|
||||||
|
elif final_layer == 2:
|
||||||
|
start_layer = 1
|
||||||
|
else:
|
||||||
|
start_layer = 0
|
||||||
|
|
||||||
|
# For `N`-layer models with `N <= 40` layers, the layers of `range(0, N // 2, 2)` and `range(N // 2, N, 2)`
|
||||||
|
# are used for `'low'` and `'high'` layers, respectively.
|
||||||
|
# For models with `N > 40` layers, the layers of `range(0, 20, 2)` and `range(N - 20, N, 2)` are used for
|
||||||
|
# `'low'` and `'high'` layers, respectively.
|
||||||
|
if isinstance(dola_layers, str) and dola_layers == "low":
|
||||||
|
if start_layer == final_layer // 2:
|
||||||
|
candidate_premature_layers = [start_layer]
|
||||||
|
else:
|
||||||
|
candidate_premature_layers = (
|
||||||
|
list(range(start_layer, final_layer // 2, 2))
|
||||||
|
if final_layer <= 40
|
||||||
|
else list(range(start_layer, 20, 2))
|
||||||
|
)
|
||||||
|
elif isinstance(dola_layers, str) and dola_layers == "high":
|
||||||
|
candidate_premature_layers = (
|
||||||
|
list(range(final_layer // 2, final_layer, 2))
|
||||||
|
if final_layer <= 40
|
||||||
|
else list(range(final_layer - 20, final_layer, 2))
|
||||||
|
)
|
||||||
|
# Set the `dola_layers` to a list of integers for layer indices to contrast manually specified layers.
|
||||||
|
elif isinstance(dola_layers, list):
|
||||||
|
candidate_premature_layers = [i for i in dola_layers if i < final_layer]
|
||||||
|
else:
|
||||||
|
raise ValueError("dola_layers must be either 'low', 'high' or a list of integers.")
|
||||||
|
|
||||||
|
lm_head = model.get_output_embeddings()
|
||||||
|
if lm_head is None:
|
||||||
|
raise ValueError("DoLa is not supported for models that don't have output embeddings.")
|
||||||
|
|
||||||
|
is_first_iteration = True
|
||||||
|
while model._has_unfinished_sequences(this_peer_finished, synced_gpus, device=input_ids.device):
|
||||||
|
# Transformers v5 cache protocol: prefill uses the full prompt; later cached
|
||||||
|
# steps use only the newest token. Uncached decoding keeps the full prefix.
|
||||||
|
next_sequence_length = (
|
||||||
|
None
|
||||||
|
if is_first_iteration or not model_kwargs.get("use_cache", True)
|
||||||
|
else 1
|
||||||
|
)
|
||||||
|
|
||||||
|
model_inputs = model.prepare_inputs_for_generation(
|
||||||
|
input_ids,
|
||||||
|
next_sequence_length=next_sequence_length,
|
||||||
|
is_first_iteration=is_first_iteration,
|
||||||
|
**model_kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
is_first_iteration = False
|
||||||
|
|
||||||
|
# forward pass to get next token
|
||||||
|
outputs = model(**model_inputs, return_dict=True)
|
||||||
|
|
||||||
|
# .float() is needed to retain precision for later logits manipulations
|
||||||
|
final_layer_next_token_logits = outputs.logits[:, -1, :].detach().to(copy=True, dtype=torch.float32)
|
||||||
|
final_logits = outputs.logits[:, -1, :].float()
|
||||||
|
candidate_premature_logits = {}
|
||||||
|
for candidate_premature_layer in candidate_premature_layers:
|
||||||
|
candidate_premature_logits[candidate_premature_layer] = lm_head(
|
||||||
|
outputs.hidden_states[candidate_premature_layer][:, -1, :]
|
||||||
|
).to(final_logits.device)
|
||||||
|
|
||||||
|
# synced_gpus: don't waste resources running the code we don't need; kwargs must be updated before skipping
|
||||||
|
model_kwargs = model._update_model_kwargs_for_generation(
|
||||||
|
outputs,
|
||||||
|
model_kwargs,
|
||||||
|
is_encoder_decoder=model.config.is_encoder_decoder,
|
||||||
|
)
|
||||||
|
if synced_gpus and this_peer_finished:
|
||||||
|
continue
|
||||||
|
|
||||||
|
next_token_logits = _dola_select_contrast(
|
||||||
|
candidate_premature_layers, candidate_premature_logits, final_logits
|
||||||
|
)
|
||||||
|
next_token_logits = next_token_logits.to(input_ids.device)
|
||||||
|
# pre-process distribution
|
||||||
|
next_token_scores = logits_processor(input_ids, next_token_logits)
|
||||||
|
|
||||||
|
# Store scores, attentions and hidden_states when required
|
||||||
|
if return_dict_in_generate:
|
||||||
|
if output_scores:
|
||||||
|
scores += (next_token_scores,)
|
||||||
|
if output_logits:
|
||||||
|
raw_logits += (final_layer_next_token_logits,)
|
||||||
|
if output_attentions:
|
||||||
|
decoder_attentions += (
|
||||||
|
(outputs.decoder_attentions,) if model.config.is_encoder_decoder else (outputs.attentions,)
|
||||||
|
)
|
||||||
|
if model.config.is_encoder_decoder:
|
||||||
|
cross_attentions += (outputs.cross_attentions,)
|
||||||
|
|
||||||
|
if output_hidden_states:
|
||||||
|
decoder_hidden_states += (
|
||||||
|
(outputs.decoder_hidden_states,)
|
||||||
|
if model.config.is_encoder_decoder
|
||||||
|
else (outputs.hidden_states,)
|
||||||
|
)
|
||||||
|
|
||||||
|
if do_sample: # sample
|
||||||
|
probs = nn.functional.softmax(next_token_scores, dim=-1)
|
||||||
|
next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1)
|
||||||
|
else: # argmax
|
||||||
|
next_tokens = torch.argmax(next_token_scores, dim=-1)
|
||||||
|
|
||||||
|
# finished sentences should have their next token be a padding token
|
||||||
|
if has_eos_stopping_criteria:
|
||||||
|
next_tokens = next_tokens * unfinished_sequences + pad_token_id * (1 - unfinished_sequences)
|
||||||
|
|
||||||
|
# update generated ids, model inputs, and length for next step
|
||||||
|
input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1)
|
||||||
|
if streamer is not None:
|
||||||
|
streamer.put(next_tokens.cpu())
|
||||||
|
|
||||||
|
# stop when each sentence is finished
|
||||||
|
unfinished_sequences = unfinished_sequences & ~stopping_criteria(input_ids, scores)
|
||||||
|
this_peer_finished = unfinished_sequences.max() == 0
|
||||||
|
|
||||||
|
if streamer is not None:
|
||||||
|
streamer.end()
|
||||||
|
|
||||||
|
if return_dict_in_generate:
|
||||||
|
return GenerateDecoderOnlyOutput(
|
||||||
|
sequences=input_ids,
|
||||||
|
scores=scores,
|
||||||
|
logits=raw_logits,
|
||||||
|
attentions=decoder_attentions,
|
||||||
|
hidden_states=decoder_hidden_states,
|
||||||
|
past_key_values=model_kwargs.get("past_key_values"),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return input_ids
|
||||||
|
|
||||||
|
def generate(model, *args, **kwargs):
|
||||||
|
"""Custom generate function for DoLa decoding.
|
||||||
|
Args:
|
||||||
|
model (`PreTrainedModel`):
|
||||||
|
The model to generate from.
|
||||||
|
dola_layers (`Union[str, list[int]]`): The layers to use for DoLa decoding. If `None`, DoLa decoding is not used. If a string, it must
|
||||||
|
be one of "low" or "high", which means using the lower part or higher part of the model layers, respectively.
|
||||||
|
"low" means the first half of the layers up to the first 20 layers, and "high" means the last half of the
|
||||||
|
layers up to the last 20 layers.
|
||||||
|
If a list of integers, it must contain the indices of the layers to use for candidate premature layers in DoLa.
|
||||||
|
The 0-th layer is the word embedding layer of the model. Set to `'low'` to improve long-answer reasoning tasks,
|
||||||
|
`'high'` to improve short-answer tasks. Check the [documentation](https://huggingface.co/transformers-community/dola)
|
||||||
|
or [the paper](https://huggingface.co/papers/2309.03883) for more details.
|
||||||
|
"""
|
||||||
|
generation_outputs = GenerationMixin.generate(
|
||||||
|
model, *args, custom_generate=_dola_decoding, **kwargs
|
||||||
|
)
|
||||||
|
return generation_outputs
|
||||||
13
generation_config.json
Normal file
13
generation_config.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"bos_token_id": 151643,
|
||||||
|
"do_sample": true,
|
||||||
|
"eos_token_id": [
|
||||||
|
151645,
|
||||||
|
151643
|
||||||
|
],
|
||||||
|
"pad_token_id": 151643,
|
||||||
|
"temperature": 0.6,
|
||||||
|
"top_k": 20,
|
||||||
|
"top_p": 0.95,
|
||||||
|
"transformers_version": "4.56.0"
|
||||||
|
}
|
||||||
151388
merges.txt
Normal file
151388
merges.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
model.safetensors
Normal file
3
model.safetensors
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:f47f71177f32bcd101b7573ec9171e6a57f4f4d31148d38e382306f42996874b
|
||||||
|
size 1503300328
|
||||||
BIN
tokenizer.json
(Stored with Git LFS)
Normal file
BIN
tokenizer.json
(Stored with Git LFS)
Normal file
Binary file not shown.
239
tokenizer_config.json
Normal file
239
tokenizer_config.json
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
{
|
||||||
|
"add_bos_token": false,
|
||||||
|
"add_prefix_space": false,
|
||||||
|
"added_tokens_decoder": {
|
||||||
|
"151643": {
|
||||||
|
"content": "<|endoftext|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151644": {
|
||||||
|
"content": "<|im_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151645": {
|
||||||
|
"content": "<|im_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151646": {
|
||||||
|
"content": "<|object_ref_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151647": {
|
||||||
|
"content": "<|object_ref_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151648": {
|
||||||
|
"content": "<|box_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151649": {
|
||||||
|
"content": "<|box_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151650": {
|
||||||
|
"content": "<|quad_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151651": {
|
||||||
|
"content": "<|quad_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151652": {
|
||||||
|
"content": "<|vision_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151653": {
|
||||||
|
"content": "<|vision_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151654": {
|
||||||
|
"content": "<|vision_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151655": {
|
||||||
|
"content": "<|image_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151656": {
|
||||||
|
"content": "<|video_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"151657": {
|
||||||
|
"content": "<tool_call>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151658": {
|
||||||
|
"content": "</tool_call>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151659": {
|
||||||
|
"content": "<|fim_prefix|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151660": {
|
||||||
|
"content": "<|fim_middle|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151661": {
|
||||||
|
"content": "<|fim_suffix|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151662": {
|
||||||
|
"content": "<|fim_pad|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151663": {
|
||||||
|
"content": "<|repo_name|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151664": {
|
||||||
|
"content": "<|file_sep|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151665": {
|
||||||
|
"content": "<tool_response>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151666": {
|
||||||
|
"content": "</tool_response>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151667": {
|
||||||
|
"content": "<think>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
},
|
||||||
|
"151668": {
|
||||||
|
"content": "</think>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additional_special_tokens": [
|
||||||
|
"<|im_start|>",
|
||||||
|
"<|im_end|>",
|
||||||
|
"<|object_ref_start|>",
|
||||||
|
"<|object_ref_end|>",
|
||||||
|
"<|box_start|>",
|
||||||
|
"<|box_end|>",
|
||||||
|
"<|quad_start|>",
|
||||||
|
"<|quad_end|>",
|
||||||
|
"<|vision_start|>",
|
||||||
|
"<|vision_end|>",
|
||||||
|
"<|vision_pad|>",
|
||||||
|
"<|image_pad|>",
|
||||||
|
"<|video_pad|>"
|
||||||
|
],
|
||||||
|
"bos_token": null,
|
||||||
|
"chat_template": "{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0].role == 'system' %}\n {{- messages[0].content + '\\n\\n' }}\n {%- endif %}\n {{- \"# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\n</tools>\\n\\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\\n<tool_call>\\n{\\\"name\\\": <function-name>, \\\"arguments\\\": <args-json-object>}\\n</tool_call><|im_end|>\\n\" }}\n{%- else %}\n {%- if messages[0].role == 'system' %}\n {{- '<|im_start|>system\\n' + messages[0].content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}\n{%- for message in messages[::-1] %}\n {%- set index = (messages|length - 1) - loop.index0 %}\n {%- if ns.multi_step_tool and message.role == \"user\" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}\n {%- set ns.multi_step_tool = false %}\n {%- set ns.last_query_index = index %}\n {%- endif %}\n{%- endfor %}\n{%- for message in messages %}\n {%- if message.content is string %}\n {%- set content = message.content %}\n {%- else %}\n {%- set content = '' %}\n {%- endif %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) %}\n {{- '<|im_start|>' + message.role + '\\n' + content + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {%- set reasoning_content = '' %}\n {%- if message.reasoning_content is string %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- if '</think>' in content %}\n {%- set reasoning_content = content.split('</think>')[0].rstrip('\\n').split('<think>')[-1].lstrip('\\n') %}\n {%- set content = content.split('</think>')[-1].lstrip('\\n') %}\n {%- endif %}\n {%- endif %}\n {%- if loop.index0 > ns.last_query_index %}\n {%- if loop.last or (not loop.last and reasoning_content) %}\n {{- '<|im_start|>' + message.role + '\\n<think>\\n' + reasoning_content.strip('\\n') + '\\n</think>\\n\\n' + content.lstrip('\\n') }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- if message.tool_calls %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.first or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- content }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '<think>\\n\\n</think>\\n\\n' }}\n {%- endif %}\n{%- endif %}",
|
||||||
|
"clean_up_tokenization_spaces": false,
|
||||||
|
"eos_token": "<|im_end|>",
|
||||||
|
"errors": "replace",
|
||||||
|
"model_max_length": 131072,
|
||||||
|
"pad_token": "<|endoftext|>",
|
||||||
|
"split_special_tokens": false,
|
||||||
|
"tokenizer_class": "Qwen2Tokenizer",
|
||||||
|
"unk_token": null
|
||||||
|
}
|
||||||
1
vocab.json
Normal file
1
vocab.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user