Views
No views yet
meta-llama/Meta-Llama-3-8B-Instruct and is trained to dynamically skip layers during inference, enabling significant speedups.
1import transformers
2from transformers import TextStreamer
3import torch
4from transformers.generation.streamers import BaseStreamer
5
6
7class TokenStreamer(BaseStreamer):
8 """
9 Simple token streamer that prints each token with its corresponding layers used.
10
11 Parameters:
12 tokenizer (`AutoTokenizer`):
13 The tokenizer used to decode the tokens.
14 skip_prompt (`bool`, *optional*, defaults to `False`):
15 Whether to skip the prompt tokens in the output. Useful for chatbots.
16 """
17
18 def __init__(self, tokenizer, skip_prompt=True):
19 self.tokenizer = tokenizer
20 self.skip_prompt = skip_prompt
21 self.next_tokens_are_prompt = True
22
23 def put(self, value):
24 """
25 Receives tokens and prints each one surrounded by brackets.
26 """
27 if len(value.shape) > 1 and value.shape[0] > 1:
28 raise ValueError("TokenStreamer only supports batch size 1")
29 elif len(value.shape) > 1:
30 value = value[0]
31
32 if self.skip_prompt and self.next_tokens_are_prompt:
33 self.next_tokens_are_prompt = False
34 return
35
36 # Process each token in the received tensor
37 for token_id in value.tolist():
38 token_text = self.tokenizer.decode([token_id])
39 print(f"={repr(token_text)}", end="\n", flush=True)
40
41 def end(self):
42 """Prints a newline at the end of generation."""
43 self.next_tokens_are_prompt = True
44 print() # Print a newline at the end
45
46
47
48# model path
49model_id = "xuan-luo/FlexiDepth-Llama-3-8B-Instruct"
50# tokenizer
51tokenizer = transformers.AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
52model = transformers.AutoModelForCausalLM.from_pretrained(
53 model_id,
54 torch_dtype=torch.bfloat16,
55 device_map="auto",
56 trust_remote_code=True
57)
58
59pipeline = transformers.pipeline(
60 "text-generation",
61 model=model,
62 tokenizer=tokenizer,
63 model_kwargs={"torch_dtype": torch.bfloat16},
64 device_map="auto",
65 trust_remote_code=True
66)
67
68messages = [
69 {"role": "user", "content": \
70"""
71Please calcualte the sum of the eight numbers in the list: [99, 45, 12, 78, 33, 66, 21, 54]. Please solve this problem step by step.
72"""},
73]
74
75terminators = [
76 pipeline.tokenizer.eos_token_id,
77 pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
78]
79
80
81streamer = TokenStreamer(tokenizer)
82outputs = pipeline(
83 messages,
84 max_new_tokens=512,
85 eos_token_id=terminators,
86 do_sample=True,
87 temperature=0.6,
88 top_p=1.0,
89 streamer=streamer,
90)lm-eval-harness framework (version 0.4.9.1). This is an update from the version used in our original paper (v0.4.8). A key change in the newer framework is the introduction of the new humaneval_instruct benchmark, which is more suitable for instruction-tuned models. We have therefore included its results below.evals folder of this repository.FlexiDepth-Llama-3-8B-Instruct against the baseline Llama-3-8B-Instruct. For our model, we report both the performance score and the average number of layers used per task, demonstrating its efficiency.| Benchmark | Shots | Metric | FlexiDepth Score | FlexiDepth Avg. Layers | Llama-3 Score | Llama-3 Layers |
|---|---|---|---|---|---|---|
| MMLU | 5 | acc | 0.6642 | 28.31 | 0.6732 | 32 |
| Hellaswag | 5 | acc_norm | 0.7451 | 30.15 | 0.7066 | 32 |
| Winogrande | 5 | acc | 0.7545 | 27.65 | 0.7380 | 32 |
| GSM8K | 5 | strict-match | 0.7013 | 22.39 | 0.6687 | 32 |
| HumanEval | 0 | pass@1 | 0.3476 | 22.97 | 0.2927 | 32 |
| HumanEval-Instruct | 0 | pass@1 | 0.6098 | 22.18 | 0.5976 | 32 |
| CoQA | 0 | f1 | 0.7878 | 25.17 | 0.7816 | 32 |