flatten_bench.py 3.3 KB
Newer Older
aiss's avatar
aiss committed
1
2
'''Copyright The Microsoft DeepSpeed Team'''

aiss's avatar
aiss committed
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
# run the benchmark under timeit (-t), cProfile (-c), line_profiler (-l)
#
# usage:
# ./flatten_bench.py -t
# ./flatten_bench.py -c
# kernprof -l flatten_bench.py -l; python -m line_profiler  flatten_bench.py.lprof

import argparse

import gc

import torch
aiss's avatar
aiss committed
16
17
from torch._utils import _flatten_dense_tensors
from deepspeed.accelerator import get_accelerator
aiss's avatar
aiss committed
18
19
20
21
22
23
24
25
26
27
28
29
from deepspeed.ops.op_builder import UtilsBuilder

from apex_C import flatten as flatten_apex

util_ops = UtilsBuilder().load()
flatten = util_ops.flatten
unflatten = util_ops.unflatten

torch.manual_seed(0)
# emulate a small typical model weights
x = [
    torch.rand((512,
aiss's avatar
aiss committed
30
                512)).to(get_accelerator().device_name()),
aiss's avatar
aiss committed
31
    torch.rand((512,
aiss's avatar
aiss committed
32
                1024)).to(get_accelerator().device_name()),
aiss's avatar
aiss committed
33
    torch.rand((512,
aiss's avatar
aiss committed
34
                30000)).to(get_accelerator().device_name())
aiss's avatar
aiss committed
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
]
t = x * 30

# warm up and check that the same output is produced
flat_py = _flatten_dense_tensors(t)
flat_cpp = flatten(t)
flat_apex = flatten_apex(t)
#numel = flat_cpp.numel()
assert torch.eq(flat_py, flat_cpp).all(), "both produce the same tensor"
assert torch.eq(flat_py, flat_apex).all(), "both produce the same tensor"

TIMES = 1000


# the programs being tested
def py():
    for i in range(TIMES):
        flat = _flatten_dense_tensors(t)


def cpp():
    for i in range(TIMES):
        flat = flatten(t)


def apex():
    for i in range(TIMES):
        flat = flatten_apex(t)


#### cProfile ####

import cProfile


def cprofileme():
    print("--------------- cProfile -----------------")
    print("py")
    cProfile.run("py()", sort=-1)
    gc.collect()
aiss's avatar
aiss committed
75
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
76
77
78
    print("cpp")
    cProfile.run("cpp()", sort=-1)
    gc.collect()
aiss's avatar
aiss committed
79
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
80
81
82
    print("apex")
    cProfile.run("apex()", sort=-1)
    gc.collect()
aiss's avatar
aiss committed
83
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
84
85
86
87
88
89
90
91
92
93
94


#### timeit ####

import timeit


def timeme():
    print("--------------- timeit -----------------")
    print(f'py  ={timeit.Timer("py()", globals=globals()).timeit(number=1)}')
    gc.collect()
aiss's avatar
aiss committed
95
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
96
97
    print(f'cpp ={timeit.Timer("cpp()", globals=globals()).timeit(number=1)}')
    gc.collect()
aiss's avatar
aiss committed
98
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
99
100
    print(f'apex={timeit.Timer("apex()", globals=globals()).timeit(number=1)}')
    gc.collect()
aiss's avatar
aiss committed
101
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
102
103
104
105
106
107
108
109
110
111
112


#### line_profiler ####
# this one requires a special way to be called
# pip install line_profiler
# kernprof -l flatten_bench.py -l; python -m line_profiler  flatten_bench.py.lprof


def line_profileme():
    print("--------------- line_profiler -----------------")
    print("py")
aiss's avatar
aiss committed
113
    profile(py)()  # noqa: F821
aiss's avatar
aiss committed
114
    gc.collect()
aiss's avatar
aiss committed
115
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
116
    print("cpp")
aiss's avatar
aiss committed
117
    profile(cpp)()  # noqa: F821
aiss's avatar
aiss committed
118
    gc.collect()
aiss's avatar
aiss committed
119
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
120
    print("apex")
aiss's avatar
aiss committed
121
    profile(apex)()  # noqa: F821
aiss's avatar
aiss committed
122
    gc.collect()
aiss's avatar
aiss committed
123
    get_accelerator().empty_cache()
aiss's avatar
aiss committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-l", action='store_true')
    parser.add_argument("-c", action='store_true')
    parser.add_argument("-t", action='store_true')
    args = parser.parse_args()
    if args.l:
        line_profileme()
    elif args.c:
        cprofileme()
    elif args.t:
        timeme()