-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_experimental.py
76 lines (67 loc) · 2.71 KB
/
api_experimental.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
import tvm
import tvm.relay as relay
import datetime
import time
from tvm.relay.testing import layers
from tvm.relay.testing import create_workload
import numpy as np
import tvm.runtime.vm as vm_rt
dtype = "float32"
target = 'llvm'
batch_size = 1
level=2
warm_iterations=1000
measurements=100
def evaluate_vm_runtime_style1(batch_size):
mod, params = build_net(batch_size)
with tvm.transform.PassContext(opt_level=level):
print(mod)
exe = relay.vm.compile(mod, target=target, params=params)
print(exe.bytecode)
vm = vm_rt.VirtualMachine(exe, tvm.cpu(0))
input_shape = (batch_size, 1024)
data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
input_list = [data_tvm]
for i in range(warm_iterations): # warm up
vm.run(input_list)
vm.run(input_list)
start_time = time.time()
for i in range(measurements):
vm.run(input_list)
end_time = time.time()
tvm_time = end_time - start_time
print("*** VM style1 runtime time elapsed", tvm_time)
print("\n")
return tvm_time
def evaluate_vm_runtime_style2(batch_size):
mod, params = build_net(batch_size)
with tvm.transform.PassContext(opt_level=level):
print(mod)
# mod = tvm.IRModule.from_expr(relay.build_module.bind_params_by_name(mod["main"], params))
# executor = relay.build_module.create_executor("vm", mod, tvm.cpu(0), target, params)
executor = relay.build_module.create_executor("vm", mod, tvm.cpu(0), target)
input_shape = (batch_size, 1024)
data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
input_list = [data_tvm]
for i in range(warm_iterations): # warm up
executor.evaluate()(data_tvm, **params)
start_time = time.time()
for i in range(measurements):
executor.evaluate()(data_tvm, **params)
end_time = time.time()
tvm_time = end_time - start_time
print("*** VM style2 runtime time elapsed", tvm_time)
print("\n")
return tvm_time
def build_net(batch_size):
input_shape = (batch_size, 1024)
data = relay.var("data", shape=input_shape, dtype=dtype)
dense0 = layers.dense_add_bias(data=data, units=512, name='fc0')
dense1 = layers.dense_add_bias(data=dense0, units=256, name='fc1')
dense2 = layers.dense_add_bias(data=dense1, units=128, name='fc2')
func = relay.Function(relay.analysis.free_vars(dense2), dense2)
mod, params = create_workload(func)
return mod, params
vm_1_time = evaluate_vm_runtime_style1(1)
vm_2_time = evaluate_vm_runtime_style2(1)
print("API 2 is {} times slower than API 1".format(vm_2_time/vm_1_time))