forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseAdagradBenchmark.cc
218 lines (199 loc) · 7.01 KB
/
SparseAdagradBenchmark.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* 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 <cassert>
#include <chrono>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <vector>
#include "./BenchUtils.h"
#include "fbgemm/Fbgemm.h"
#include "src/RefImplementations.h"
using namespace std;
using namespace fbgemm;
static vector<vector<int>> GetInputs_() {
vector<vector<int>> input_dims = {
// num_rows, emb dim
{1500000, 24},
{1500000, 32},
{1500000, 40},
{1500000, 64},
{1500000, 80},
{1500000, 128},
{1500000, 144},
{1500000, 192},
{1500000, 384},
};
return input_dims;
}
void run_benchmark(
const int num_rows, // number of rows reading
const int block_size, // number of parameters per row
const uint64_t param_size, // total number of parameters
const bool isIndex64b,
const bool adjust_weight_decay) {
vector<char> llc(64L * 1024L * 1024L, 1.0);
vector<float> g(num_rows * block_size); // gradients
vector<float> h(param_size); // input momentums
vector<float> w(param_size); // input params
vector<float> h_ref(param_size);
vector<float> w_ref(param_size);
default_random_engine generator;
// normal_distribution<float> h_w_distribution;
// TODO: check appropriate vals for g,h,w
for (size_t i = 0; i < g.size(); ++i) {
g[i] = 4 + i; // h_w_distribution(generator);
}
for (size_t i = 0; i < h.size(); ++i) {
h_ref[i] = h[i] = 2 + i; // h_w_distribution(generator);
}
for (size_t i = 0; i < w.size(); ++i) {
w_ref[i] = w[i] = 3 + i; // h_w_distribution(generator);
}
vector<int64_t> indices(num_rows);
vector<int32_t> indices_32(num_rows);
vector<double> counter(num_rows);
float epsilon = 1e-5;
float lr = 0.5;
float weight_decay = adjust_weight_decay ? 1e-7 : 0.0f;
constexpr int64_t counter_halflife = 1e6;
uniform_int_distribution<int64_t> length_distribution(0, num_rows - 1);
uniform_int_distribution<int64_t> counter_distribution(
0, 2 * counter_halflife);
for (int i = 0; i < num_rows; ++i) {
indices_32[i] = indices[i] = length_distribution(generator);
counter[i] = counter_distribution(generator);
}
constexpr int NUM_WARMUP = 4;
constexpr int NUM_ITER = 10;
double data_moved = 5 * sizeof(float) * num_rows * block_size;
double t = 0.0;
constexpr bool rowwise = false;
constexpr int prefetch = 16;
if (isIndex64b) {
auto fn_indices_64 = GenerateSparseAdaGrad<int64_t>(
block_size, rowwise, prefetch, adjust_weight_decay);
t = measureWithWarmup(
[&]() {
fn_indices_64(
num_rows, // number of rows reading
param_size, // total number of parameters
w.data(), // input parameters
g.data(), // input gradients
h.data(), // input momentums
indices.data(), // indices of each row
epsilon,
lr,
weight_decay, // weight decay (lambda)
adjust_weight_decay
? counter.data()
: nullptr, // feature ID frequency counter data
counter_halflife); // counter halflife value for adjustments
},
NUM_WARMUP,
NUM_ITER,
[&]() { llc_flush(llc); });
for (int i = 0; i < NUM_WARMUP + NUM_ITER; ++i) {
sparse_adagrad_ref(
num_rows, // number of rows reading
block_size, // number of parameters per row
param_size, // total number of parameters
w_ref.data(), // input parameters
g.data(), // input gradients
h_ref.data(), // input momentums
indices.data(), // indices of each row
epsilon,
lr,
weight_decay, // weight decay (lambda)
adjust_weight_decay ? counter.data()
: nullptr, // feature ID frequency counter data
counter_halflife); // counter halflife value for adjustments
}
} else {
auto fn_indices_32 = GenerateSparseAdaGrad<int32_t>(
block_size, rowwise, prefetch, adjust_weight_decay);
t = measureWithWarmup(
[&]() {
fn_indices_32(
num_rows, // number of rows reading
param_size, // total number of parameters
w.data(), // input parameters
g.data(), // input gradients
h.data(), // input momentums
indices_32.data(), // indices of each row
epsilon,
lr,
weight_decay, // weight decay (lambda)
adjust_weight_decay
? counter.data()
: nullptr, // feature ID frequency counter data
counter_halflife); // counter halflife value for adjustments
},
NUM_WARMUP,
NUM_ITER,
[&]() { llc_flush(llc); });
for (int i = 0; i < NUM_WARMUP + NUM_ITER; ++i) {
sparse_adagrad_ref(
num_rows, // number of rows reading
block_size, // number of parameters per row
param_size, // total number of parameters
w_ref.data(), // input parameters
g.data(), // input gradients
h_ref.data(), // input momentums
indices_32.data(), // indices of each row
epsilon,
lr,
weight_decay, // weight decay (lambda)
adjust_weight_decay ? counter.data()
: nullptr, // feature ID frequency counter data
counter_halflife); // counter halflife value for adjustments
}
}
for (size_t i = 0; i < w.size(); ++i) {
assert(fabs(w[i] - w_ref[i]) < 1e-5);
if (fabs(w[i] - w_ref[i]) >= 1e-5) {
fprintf(stderr, "%ld %f %f\n", i, w[i], w_ref[i]);
}
}
for (size_t i = 0; i < h.size(); ++i) {
assert(fabs(h[i] - h_ref[i]) < 1e-5);
if (fabs(h[i] - h_ref[i]) >= 1e-5) {
fprintf(stderr, "%ld %f %f\n", i, h[i], h_ref[i]);
}
}
cout << "indices: " << (isIndex64b ? " 64bits " : " 32bits ") << " | ";
cout << "weight_decay: " << setw(1) << adjust_weight_decay << " | ";
cout << "num_rows: " << setw(8) << num_rows << " block_size: " << setw(4)
<< block_size << " | ";
cout << "time taken by jit code(secs): " << setw(10) << fixed
<< setprecision(6) << t << " | ";
cout << "bandwidth fbgemm (GB/s) " << setw(10) << fixed << setprecision(6)
<< data_moved / t / 1e9 << endl;
}
int main() {
int num_rows;
int block_size;
uint64_t param_size;
vector<vector<int>> inputs(GetInputs_());
for (auto isIndex64b : vector<bool>{true, false}) {
for (auto adjust_weight_decay : vector<bool>{true, false}) {
for (auto& input : inputs) {
assert(input.size() >= 2);
num_rows = input[0];
block_size = input[1];
param_size = num_rows * block_size;
run_benchmark(
num_rows, block_size, param_size, isIndex64b, adjust_weight_decay);
}
}
}
return 0;
}