1 from __future__
import absolute_import
2 from __future__
import division
3 from __future__
import print_function
4 from __future__
import unicode_literals
14 'uint8_fused': np.uint8,
16 'float16': np.float16,
20 def benchmark_sparse_lengths_sum(
27 print(
'Preparing lookup table. ' + str(datetime.datetime.now()))
31 data = np.ones([categorical_limit, embedding_size], dtype=np.float32)
34 if dtype_str ==
'uint8':
35 scale_bias = np.random.rand(categorical_limit, 2).astype(np.float32)
36 workspace.FeedBlob(
"scale_bias", scale_bias.astype(np.float32))
37 elif dtype_str ==
'uint8_fused':
38 scale_bias = np.random.randint(255, size=(categorical_limit, 8))
39 data = np.concatenate([data, scale_bias], axis=1)
41 print(
'Data has shape {} {}'.format(data.shape, datetime.datetime.now()))
42 workspace.FeedBlob(
"X", data.astype(DTYPES[dtype_str]))
47 lengths = np.random.randint(
48 int(average_len * 0.75),
49 int(average_len * 1.25),
50 batch_size).astype(np.int32)
51 indices = np.random.randint(
52 0, categorical_limit, np.sum(lengths)).astype(np.int64)
53 outputs[0].feed(indices)
54 outputs[1].feed(lengths)
56 net = core.Net(
"mynet")
57 net.Python(f)([], [
"indices",
"lengths", ])
58 if dtype_str ==
"uint8":
59 net.SparseLengthsSum8BitsRowwise([
"X",
"indices",
"lengths",
"scale_bias"],
"Y")
60 elif dtype_str ==
"uint8_fused":
61 net.SparseLengthsSumFused8BitRowwise([
"X",
"indices",
"lengths"],
"Y")
63 net.SparseLengthsSum([
"X",
"indices",
"lengths"],
"Y")
64 workspace.CreateNet(net)
70 print(
'Preparation finished. ' + str(datetime.datetime.now()))
72 workspace.BenchmarkNet(net.Name(), 1, iterations,
True)
75 if __name__ ==
"__main__":
76 parser = argparse.ArgumentParser(
77 description=
"minimal benchmark for sparse lengths sum.")
79 '-d',
"--dtype", choices=list(DTYPES.keys()), default=
"float",
80 help=
"The data type for the input lookup table.")
82 '-e',
"--embedding-size", type=int, default=6000000,
83 help=
"Lookup table size.")
85 "--embedding-dim", type=int, default=128,
86 help=
"Embedding dimension.")
88 "--average_len", type=int, default=27,
89 help=
"Sparse feature average lengths, default is 27")
91 "--batch_size", type=int, default=100,
92 help=
"The batch size.")
94 '-i',
"--iteration", type=int, default=100000,
95 help=
"The number of iterations.")
96 args, extra_args = parser.parse_known_args()
97 core.GlobalInit([
'python'] + extra_args)
98 benchmark_sparse_lengths_sum(