forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbeddingIndexRemappingBenchmark.cc
159 lines (142 loc) · 4.21 KB
/
EmbeddingIndexRemappingBenchmark.cc
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <algorithm>
#include <array>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
#include "./BenchUtils.h"
#include "fbgemm/Fbgemm.h"
#include "src/RefImplementations.h"
#include "test/EmbeddingSpMDMTestUtils.h"
using namespace std;
using namespace fbgemm;
static vector<vector<int>> GetInputs_() {
vector<vector<int>> input_dims = {
// batch size, number of rows of table, avg lengthl
{10, 4000000, 100},
{20, 4000000, 100},
{40, 4000000, 100},
{50, 4000000, 100},
{10, 10000000, 100},
{20, 10000000, 100},
{40, 10000000, 100},
{50, 10000000, 100},
};
return input_dims;
}
int run_benchmark(
int batch_size,
int num_rows,
int average_len,
bool use_32_bit_indices = false) {
constexpr int NWARMUP = 4;
constexpr int NITER = 100;
int offset_numel = batch_size + 1;
constexpr float sparsity = 0.5;
vector<int64_t> lengths, offsets, indices;
vector<int32_t> lengths_32, offsets_32, indices_32;
vector<float> weights;
GenerateLengthsIndicesWeights(
lengths,
lengths_32,
offsets,
offsets_32,
indices,
indices_32,
weights,
batch_size,
num_rows,
average_len, // average number of indices in a batch
EmbeddingSpMDMCornerCase::NONE);
// Create mapping table for rowwise sparsity
vector<int32_t> mapping_table;
CreateMappingTableForRowWiseSparsity(mapping_table, num_rows, sparsity);
vector<int32_t> out_indices_32(indices_32.size(), 0);
vector<int32_t> out_offsets_32(offsets_32.size(), 0);
vector<float> out_weights(weights.size(), 0);
vector<int64_t> out_indices(indices.size(), 0);
vector<int64_t> out_offsets(offsets.size(), 0);
double duration_ref = measureWithWarmup(
[&]() {
if (use_32_bit_indices) {
compressed_indices_remap_ref<int32_t>(
offset_numel,
indices_32.data(),
mapping_table.data(),
offsets_32.data(),
weights.data(),
out_indices_32.data(),
out_offsets_32.data(),
out_weights.data());
} else {
compressed_indices_remap_ref<int64_t>(
offset_numel,
indices.data(),
mapping_table.data(),
offsets.data(),
weights.data(),
out_indices.data(),
out_offsets.data(),
out_weights.data());
}
},
NWARMUP,
NITER);
double duration = measureWithWarmup(
[&]() {
if (use_32_bit_indices) {
compressed_indices_remap<int32_t>(
offset_numel,
indices_32.data(),
mapping_table.data(),
offsets_32.data(),
weights.data(),
out_indices_32.data(),
out_offsets_32.data(),
out_weights.data());
} else {
compressed_indices_remap<int64_t>(
offset_numel,
indices.data(),
mapping_table.data(),
offsets.data(),
weights.data(),
out_indices.data(),
out_offsets.data(),
out_weights.data());
}
},
NWARMUP,
NITER);
cout << "reference:" << duration_ref * 1e6 << " (us), ";
cout << "Opt:" << duration * 1e6 << " (us) " << endl;
return 0;
}
int main() {
int batch_size;
int num_rows;
int average_len;
vector<vector<int>> inputs(GetInputs_());
for (auto& input : inputs) {
assert(input.size() == 3);
batch_size = input[0];
num_rows = input[1];
average_len = input[2];
cout << "batch size" << setw(6) << batch_size << setw(10) << "num rows"
<< setw(14) << num_rows << setw(16) << "avg length" << setw(6)
<< average_len << endl;
cout << "64 bit indices, ";
run_benchmark(batch_size, num_rows, average_len);
cout << "32 bit indices, ";
run_benchmark(batch_size, num_rows, average_len, true);
cout << endl;
}
return 0;
}