forked from mlc-ai/web-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
132 lines (112 loc) · 4.76 KB
/
evaluate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from typing import List
import argparse
import os
import time
import tvm
from tvm import relax
from tvm.runtime import ShapeTuple
from tvm.relax.testing.lib_comparator import LibCompareVMInstrument
import numpy as np
import torch
from transformers import AutoTokenizer
from web_llm import utils
def _parse_args():
args = argparse.ArgumentParser()
args.add_argument("--device-name", type=str, default="auto")
args.add_argument("--debug-dump", action="store_true", default=False)
args.add_argument("--artifact-path", type=str, default="dist")
args.add_argument("--prompt", type=str, default="The capital of Canada is")
args.add_argument("--model", type=str, default="vicuna-7b-v1")
args.add_argument("--profile", action="store_true", default=False)
parsed = args.parse_args()
parsed.model_path = os.path.join(parsed.artifact_path, "models", parsed.model)
parsed.artifact_path = os.path.join(parsed.artifact_path, parsed.model)
if parsed.device_name == "auto":
if tvm.cuda().exist:
parsed.device_name = "cuda"
elif tvm.metal().exist:
parsed.device_name = "metal"
else:
raise ValueError("Cannot auto deduce device-name, please set it")
return parsed
class LibCompare(LibCompareVMInstrument):
def __init__(self, mod, device):
super().__init__(mod, device, True)
self.time_eval_results = {}
def compare(
self,
name: str,
ref_args: List[tvm.nd.NDArray],
new_args: List[tvm.nd.NDArray],
ret_indices: List[int],
):
super().compare(name, ref_args, new_args, ret_indices)
if name not in self.time_eval_results:
res = self.mod.time_evaluator(name, dev=self.device)(*new_args)
self.time_eval_results[name] = res
print(f"Time-eval result {name} on {self.device}: {res}")
def create_kv_caches(device):
kv_caches = []
fcreate_cache = tvm.get_global_func("vm.builtin.attention_kv_cache_create")
for i in range(64):
kv_cache = fcreate_cache(
tvm.nd.empty((1, 32, 128), device=device, dtype="float32"),
tvm.runtime.ShapeTuple([6, 32, 128]),
0,
)
kv_caches.append(kv_cache)
return kv_caches
def deploy_to_pipeline(args) -> None:
device = tvm.device(args.device_name)
const_params = utils.load_params(args.artifact_path, device)
ex = tvm.runtime.load_module(
os.path.join(args.artifact_path, f"{args.model}_{args.device_name}.so")
)
vm = relax.VirtualMachine(ex, device)
tokenizer = AutoTokenizer.from_pretrained(args.model_path)
print("Tokenizing...")
inputs = tvm.nd.array(
tokenizer(args.prompt, return_tensors="pt").input_ids.to(torch.int32).numpy(),
device,
)
first_sampled_token = tvm.nd.array(np.array([[6234]]).astype("int32"), device)
seq_len_shape = tvm.runtime.ShapeTuple([inputs.shape[1]])
second_seq_len_shape = tvm.runtime.ShapeTuple([inputs.shape[1] + 1])
kv_caches = create_kv_caches(device)
print("Running inference...")
start = time.time()
logits, kv_caches = vm["encoding"](inputs, seq_len_shape, kv_caches, const_params)
device.sync()
encoding_end = time.time()
logits, kv_caches = vm["decoding"](
first_sampled_token, second_seq_len_shape, kv_caches, const_params
)
device.sync()
end = time.time()
fcache_view = tvm.get_global_func("vm.builtin.attention_kv_cache_view")
first_k_cache = fcache_view(kv_caches[0], ShapeTuple([7, 32, 128]))
if args.debug_dump:
print(f"output kv_cache[0]:\n{first_k_cache.numpy().transpose(1, 0, 2)}")
print(f"output logits:\n{logits.numpy()}")
print(
f"Time elapsed: encoding {(encoding_end - start)} seconds, decoding {end - encoding_end} secs"
)
if args.profile:
from contextlib import redirect_stdout
cmp_instrument = LibCompare(ex, device)
vm.set_instrument(cmp_instrument)
print("Profiling...")
profile_file_path = os.path.join(args.artifact_path, "debug", "evaluate_profile.log")
with open(profile_file_path, "w") as file:
with redirect_stdout(file):
kv_caches = create_kv_caches(device)
print("======================= Starts Encoding =======================")
logits, kv_caches = vm["encoding"](inputs, seq_len_shape, kv_caches, const_params)
print("======================= Starts Decoding =======================")
logits, kv_caches = vm["decoding"](
first_sampled_token, second_seq_len_shape, kv_caches, const_params
)
print(f"Save the profiling results to {profile_file_path}")
if __name__ == "__main__":
ARGS = _parse_args()
deploy_to_pipeline(ARGS)