3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
8 from caffe2.proto
import caffe2_pb2
18 log = logging.getLogger(
"embedding_generation_benchmark")
19 log.setLevel(logging.DEBUG)
22 def generate_data(T, batch_size, max_seq_length):
24 Fill a queue with input data 26 log.info(
"Generating T={} batches".format(T))
28 generate_input_init_net = core.Net(
'generate_input_init')
29 queue = generate_input_init_net.CreateBlobsQueue(
30 [],
"inputqueue", num_blobs=1, capacity=T,
32 workspace.RunNetOnce(generate_input_init_net)
34 generate_input_net = core.Net(
'generate_input')
35 generate_input_net.EnqueueBlobs([queue,
"scratch"], [
"scratch"])
39 if (t % (max(10, T // 10)) == 0):
40 log.info(
"Generating data {}/{}".format(t, T))
41 X = np.tile(np.arange(max_seq_length), [batch_size, 1]).transpose()
42 workspace.FeedBlob(
"scratch", X)
43 workspace.RunNetOnce(generate_input_net.Proto())
45 log.info(
"Finished data generation")
49 def generate_embedding_table(vocab_size, embedding_size):
50 log.info(
"Generating embedding table with dimensions {}" 51 .format([vocab_size, embedding_size]))
53 generate_table_net = core.Net(
'generate_table')
54 table = generate_table_net.GaussianFill(
57 shape=[vocab_size, embedding_size],
60 workspace.RunNetOnce(generate_table_net)
64 def create_model(args, queue, embedding_table, embedding_size):
65 model = model_helper.ModelHelper(name=
'embedding_generation_bench')
66 input_blob = model.net.DequeueBlobs(queue,
'input_data')
68 if args.implementation ==
'sinusoid':
69 model.net.SinusoidPositionEncoding(
72 embedding_size=embedding_size
76 [embedding_table, input_blob],
83 def Caffe2EmbeddingGeneration(args):
84 T = args.data_size // args.batch_size
86 queue = generate_data(T, args.batch_size, args.seq_length)
88 embedding_table =
None 89 if args.implementation ==
'table':
90 embedding_table = generate_embedding_table(
95 model = create_model(args, queue, embedding_table, args.embedding_size)
97 workspace.RunNetOnce(model.param_init_net)
98 workspace.CreateNet(model.net)
100 start_time = time.time()
105 log.info(
"------ Warming up ------")
106 workspace.RunNet(model.net.Proto().name)
108 log.info(
"------ Starting benchmark ------")
109 start_time = time.time()
110 last_time = time.time()
111 for iteration
in range(1, num_iters, args.iters_to_report):
112 iters_once = min(args.iters_to_report, num_iters - iteration)
113 total_iters += iters_once
114 workspace.RunNet(model.net.Proto().name, iters_once)
116 new_time = time.time()
118 "Iter: {} / {}. Embeddings Generated Per Second: {}k.".format(
121 (iters_once * args.batch_size * args.seq_length) /
122 (new_time - last_time) // 100 / 10,
127 total_per_sec = (num_iters - 1) * args.batch_size * args.seq_length
128 total_per_sec = total_per_sec / (time.time() - start_time) // 100 / 10
130 log.info(
"Done. Total embeddings generated per second " +
131 "excluding 1st iteration: {}k".format(total_per_sec))
133 return time.time() - start_time
138 return Caffe2EmbeddingGeneration(args)
141 def GetArgumentParser():
142 parser = argparse.ArgumentParser(
143 description=
"Embedding generation benchmark." 150 help=
"Embedding size",
156 help=
"The batch size." 162 help=
"Number of sequences to generate" 168 help=
"Max sequence length" 174 help=
"Number of iterations to report progress" 180 help=
"'table' or 'sinusoid'",
185 if __name__ ==
'__main__':
186 args, extra_args = GetArgumentParser().parse_known_args()
188 workspace.GlobalInit([
190 '--caffe2_log_level=0',
191 '--caffe2_print_blob_sizes_at_exit=0'] + extra_args)
193 device = core.DeviceOption(caffe2_pb2.CPU)
195 with core.DeviceScope(device):