eggqq007 commited on
Commit
c25e25e
1 Parent(s): f3a511c

Upload benchmark_generation_mamba_simple.py

Browse files
benchmark_generation_mamba_simple.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Tri Dao, Albert Gu.
2
+
3
+ import argparse
4
+ import time
5
+ import json
6
+
7
+ import torch
8
+ import torch.nn.functional as F
9
+
10
+ from einops import rearrange
11
+
12
+ from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaTokenizer
13
+
14
+ from mamba_ssm.models.mixer_seq_simple import MambaLMHeadModel
15
+
16
+
17
+ parser = argparse.ArgumentParser(description="Generation benchmarking")
18
+ parser.add_argument("--model-name", type=str, default="state-spaces/mamba-130m")
19
+ parser.add_argument("--prompt", type=str, default=None)
20
+ parser.add_argument("--promptlen", type=int, default=100)
21
+ parser.add_argument("--genlen", type=int, default=100)
22
+ parser.add_argument("--temperature", type=float, default=1.0)
23
+ parser.add_argument("--topk", type=int, default=1)
24
+ parser.add_argument("--topp", type=float, default=1.0)
25
+ parser.add_argument("--repetition-penalty", type=float, default=1.0)
26
+ parser.add_argument("--batch", type=int, default=1)
27
+ args = parser.parse_args()
28
+
29
+ repeats = 3
30
+ device = "cuda"
31
+ dtype = torch.float16
32
+
33
+ print(f"Loading model {args.model_name}")
34
+ tokenizer = LlamaTokenizer.from_pretrained(args.model_name)
35
+ model = MambaLMHeadModel.from_pretrained(args.model_name, device=device, dtype=dtype)
36
+ model.eval()
37
+ print(f"Number of parameters: {sum(p.numel() for p in model.parameters() if p.requires_grad)}")
38
+
39
+ torch.random.manual_seed(0)
40
+ if args.prompt is None:
41
+ input_ids = torch.randint(1, 1000, (args.batch, args.promptlen), dtype=torch.long, device="cuda")
42
+ attn_mask = torch.ones_like(input_ids, dtype=torch.long, device="cuda")
43
+ else:
44
+ args.prompt = args.prompt.replace('\\n', '\n')
45
+ tokens = tokenizer(args.prompt, return_tensors="pt")
46
+ input_ids = tokens.input_ids.to(device=device)
47
+ attn_mask = tokens.attention_mask.to(device=device)
48
+ max_length = input_ids.shape[1] + args.genlen
49
+
50
+ fn = lambda: model.generate(
51
+ input_ids=input_ids,
52
+ max_length=max_length,
53
+ cg=True,
54
+ return_dict_in_generate=True,
55
+ output_scores=True,
56
+ enable_timing=False,
57
+ temperature=args.temperature,
58
+ top_k=args.topk,
59
+ top_p=args.topp,
60
+ repetition_penalty=args.repetition_penalty,
61
+ )
62
+ out = fn()
63
+ if args.prompt is not None:
64
+ l = tokenizer.batch_decode(out.sequences.tolist())
65
+ text = ''.join(l)
66
+ print(text)
67
+
68
+ torch.cuda.synchronize()
69
+ start = time.time()
70
+ for _ in range(repeats):
71
+ fn()
72
+ torch.cuda.synchronize()
73
+ print(f"Prompt length: {len(input_ids[0])}, generation length: {len(out.sequences[0]) - len(input_ids[0])}")
74
+ print(f"{args.model_name} prompt processing + decoding time: {(time.time() - start) / repeats * 1000:.0f}ms")