-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance_matrix.cpp
498 lines (437 loc) · 20.4 KB
/
distance_matrix.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright (c) 2022 Graphcore Ltd. All rights reserved.
#include <cmath>
#include <sstream>
#include <popops/Cast.hpp>
#include <popops/ElementWise.hpp>
#include <poputil/TileMapping.hpp>
#include <poputil/Util.hpp>
#include <poputil/VertexTemplates.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <popart/op.hpp>
#include <popart/opmanager.hpp>
#include <popart/opserialiser.hpp>
#include <popart/popx/opx.hpp>
#include <popart/popx/opxmanager.hpp>
#pragma GCC diagnostic pop
namespace {
void mapTensor2Dblocks(poplar::Graph& graph, poplar::Tensor& t) {
assert(t.rank() == 2 && "only 2D tensors can use mapTensor2Dblocks");
auto nTiles = graph.getTarget().getNumTiles();
auto blockSize0 =
std::max<unsigned>(std::ceil(t.dim(0) / nTiles),
std::ceil(std::sqrt(static_cast<float>(t.numElements()) / nTiles)));
auto nBlocks0 = (t.dim(0) + blockSize0 - 1) / blockSize0;
auto nBlocks1 = std::max<unsigned>(1u, nTiles / nBlocks0);
auto blockSize1 = (t.dim(1) + nBlocks1 - 1) / nBlocks1;
for (auto i = 0u; i < nBlocks0; ++i) {
for (auto j = 0u; j < nBlocks1; ++j) {
auto tile = nBlocks1 * i + j;
graph.setTileMapping(t.slice({std::min<unsigned>(i * blockSize0, t.dim(0)),
std::min<unsigned>(j * blockSize1, t.dim(1))},
{std::min<unsigned>((i + 1) * blockSize0, t.dim(0)),
std::min<unsigned>((j + 1) * blockSize1, t.dim(1))}),
tile);
}
}
}
poplar::Tensor getCachedCopy(std::map<std::pair<size_t, size_t>, poplar::Tensor>& cache,
poplar::Graph& graph,
size_t tile,
size_t index,
const poplar::Tensor& t,
poplar::program::Sequence& prog) {
std::pair<size_t, size_t> key = {tile, index};
auto iter = cache.find(key);
if (iter != cache.end()) {
return iter->second;
}
poplar::Tensor copy = graph.addVariable(t.elementType(), t.shape());
graph.setTileMapping(copy, tile);
cache[key] = copy;
prog.add(poplar::program::Copy(t, copy));
return copy;
}
// Like popops::cast, but alias rather than copying when the type already matches
poplar::Tensor castMaybe(poplar::Graph& graph,
const poplar::Tensor& tensor,
poplar::Type type,
poplar::program::Sequence& prog) {
if (type == tensor.elementType()) {
return tensor;
}
auto casted = graph.clone(type, tensor);
prog.add(popops::cast(graph, tensor, casted));
return casted;
}
poplar::Tensor l1distance(poplar::Graph& graph,
const poplar::Tensor& a,
const poplar::Tensor& b,
poplar::program::Sequence& prog,
const poplar::DebugContext& debugContext) {
if (a.rank() != 2 || b.rank() != 2 || a.dim(1) != b.dim(1)) {
std::ostringstream msg;
msg << "Bad arguments to l1distance, expected a.shape (M, K), b.shape (N, K), actual"
<< " a.shape = " << a.shapeToString() << ", b.shape = " << b.shapeToString() << ".";
throw std::invalid_argument(msg.str());
}
const size_t n = b.dim(0);
poplar::Tensor out =
graph.addVariable(poplar::FLOAT, {a.dim(0), b.dim(0)}, {debugContext, "l1dist_out"});
mapTensor2Dblocks(graph, out);
const auto& mapping = graph.getTileMapping(out);
poplar::ComputeSet cs = graph.addComputeSet({debugContext, "l1dist"});
const auto vertexName = poputil::templateVertex("L1DistanceSingleVertex", a.elementType());
for (size_t i = 0; i < mapping.size(); ++i) {
for (const auto& interval : mapping[i]) {
for (size_t j = interval.begin(); j != interval.end(); ++j) {
size_t a_index = j / n;
size_t b_index = j % n;
auto v = graph.addVertex(cs, vertexName);
graph.connect(v["a"], a[a_index]);
graph.connect(v["b"], b[b_index]);
graph.connect(v["out"], out[a_index][b_index]);
graph.setTileMapping(v, i);
graph.setPerfEstimate(v, 0); // placeholder
}
}
}
prog.add(poplar::program::Execute(cs));
return castMaybe(graph, out, a.elementType(), prog);
}
poplar::Tensor l1distancegrad(poplar::Graph& graph,
const poplar::Tensor& a,
const poplar::Tensor& b,
const poplar::Tensor& gradOutput,
poplar::program::Sequence& prog,
const poplar::DebugContext& debugContext) {
if (a.rank() != 2 || b.rank() != 2 || gradOutput.rank() != 2 || a.dim(1) != b.dim(1) ||
gradOutput.dim(0) != a.dim(0) || gradOutput.dim(1) != b.dim(0)) {
std::ostringstream msg;
msg << "Bad arguments to l1distancegrad, expected"
<< " a.shape (M, K), b.shape (N, K), gradOutput.shape (M, N), actual"
<< " a.shape = " << a.shapeToString() << ", b.shape = " << b.shapeToString()
<< ", gradOutput.shape = " << gradOutput.shapeToString() << ".";
throw std::invalid_argument(msg.str());
}
const size_t k = a.dim(1);
poplar::Tensor grad =
graph.addVariable(poplar::FLOAT, a.shape(), {debugContext, "l1dist_grad"});
mapTensor2Dblocks(graph, grad);
const auto& mapping = graph.getTileMapping(grad);
poplar::ComputeSet cs = graph.addComputeSet({debugContext, "l1dist_grad"});
const auto vertexName = poputil::templateVertex("L1DistanceGradSingleVertex", a.elementType());
std::map<std::pair<size_t, size_t>, poplar::Tensor> bCache, gradCache;
for (size_t i = 0; i < mapping.size(); ++i) {
for (const auto& interval : mapping[i]) {
for (size_t j = interval.begin(); j != interval.end(); ++j) {
size_t a1_index = j / k;
size_t a2_index = j % k;
auto v = graph.addVertex(cs, vertexName);
graph.connect(v["a"], a[a1_index][a2_index]);
graph.connect(
v["b"], getCachedCopy(bCache, graph, i, a2_index,
b.slice({a2_index, a2_index + 1}, 1).squeeze({1}), prog));
graph.connect(v["gradOutput"], getCachedCopy(gradCache, graph, i, a1_index,
gradOutput[a1_index], prog));
graph.connect(v["grad"], grad[a1_index][a2_index]);
graph.setTileMapping(v, i);
graph.setPerfEstimate(v, 0); // placeholder
}
}
}
prog.add(poplar::program::Execute(cs));
return castMaybe(graph, grad, a.elementType(), prog);
}
poplar::Tensor l2distance(poplar::Graph& graph,
const poplar::Tensor& a,
const poplar::Tensor& b,
poplar::program::Sequence& prog,
const poplar::DebugContext& debugContext) {
if (a.rank() != 2 || b.rank() != 2 || a.dim(1) != b.dim(1)) {
std::ostringstream msg;
msg << "Bad arguments to l2distance, expected a.shape (M, K), b.shape (N, K), actual"
<< " a.shape = " << a.shapeToString() << ", b.shape = " << b.shapeToString() << ".";
throw std::invalid_argument(msg.str());
}
const size_t n = b.dim(0);
poplar::Tensor out =
graph.addVariable(poplar::FLOAT, {a.dim(0), b.dim(0)}, {debugContext, "l2dist_out"});
mapTensor2Dblocks(graph, out);
const auto& mapping = graph.getTileMapping(out);
poplar::ComputeSet cs = graph.addComputeSet({debugContext, "l2dist"});
const auto vertexName = poputil::templateVertex("L2DistanceSingleVertex", a.elementType());
for (size_t i = 0; i < mapping.size(); ++i) {
for (const auto& interval : mapping[i]) {
for (size_t j = interval.begin(); j != interval.end(); ++j) {
size_t a_index = j / n;
size_t b_index = j % n;
auto v = graph.addVertex(cs, vertexName);
graph.connect(v["a"], a[a_index]);
graph.connect(v["b"], b[b_index]);
graph.connect(v["out"], out[a_index][b_index]);
graph.setTileMapping(v, i);
graph.setPerfEstimate(v, 0); // placeholder
}
}
}
prog.add(poplar::program::Execute(cs));
return castMaybe(graph, out, a.elementType(), prog);
}
poplar::Tensor l2distancegrad(poplar::Graph& graph,
const poplar::Tensor& a,
const poplar::Tensor& b,
const poplar::Tensor& dist,
const poplar::Tensor& gradOutput,
poplar::program::Sequence& prog,
const poplar::DebugContext& debugContext) {
if (a.rank() != 2 || b.rank() != 2 || dist.rank() != 2 || gradOutput.rank() != 2 ||
a.dim(1) != b.dim(1) || gradOutput.dim(0) != a.dim(0) || gradOutput.dim(1) != b.dim(0) ||
dist.dim(0) != a.dim(0) || dist.dim(1) != b.dim(0)) {
std::ostringstream msg;
msg << "Bad arguments to l2distancegrad, expected"
<< " a.shape (M, K), b.shape (N, K), gradOutput.shape (M, N), actual"
<< " a.shape = " << a.shapeToString() << ", b.shape = " << b.shapeToString()
<< ", gradOutput.shape = " << gradOutput.shapeToString() << ".";
throw std::invalid_argument(msg.str());
}
const size_t k = a.dim(1);
poplar::Tensor grad =
graph.addVariable(poplar::FLOAT, a.shape(), {debugContext, "l2dist_grad"});
mapTensor2Dblocks(graph, grad);
const auto& mapping = graph.getTileMapping(grad);
poplar::ComputeSet cs = graph.addComputeSet({debugContext, "l2dist_grad"});
const auto vertexName = poputil::templateVertex("L2DistanceGradSingleVertex", a.elementType());
for (size_t i = 0; i < mapping.size(); ++i) {
for (const auto& interval : mapping[i]) {
for (size_t j = interval.begin(); j != interval.end(); ++j) {
size_t a1_index = j / k;
size_t a2_index = j % k;
auto v = graph.addVertex(cs, vertexName);
graph.connect(v["a"], a[a1_index][a2_index]);
graph.connect(v["b"], b.slice({a2_index, a2_index + 1}, 1).squeeze({1}));
graph.connect(v["dist"], dist[a1_index]);
graph.connect(v["gradOutput"], gradOutput[a1_index]);
graph.connect(v["grad"], grad[a1_index][a2_index]);
graph.setTileMapping(v, i);
graph.setPerfEstimate(v, 0); // placeholder
}
}
}
prog.add(poplar::program::Execute(cs));
return castMaybe(graph, grad, a.elementType(), prog);
}
const popart::OperatorIdentifier L1DistanceId = {"ai.graphcore", "L1Distance", 1};
const popart::OperatorIdentifier L2DistanceId = {"ai.graphcore", "L2Distance", 1};
const popart::OperatorIdentifier L1DistanceGradId = {"ai.graphcore", "L1DistanceGrad", 1};
const popart::OperatorIdentifier L2DistanceGradId = {"ai.graphcore", "L2DistanceGrad", 1};
class L1DistanceOp;
class L1DistanceGradOpx;
class L2DistanceOp;
class L2DistanceGradOpx;
class L1DistanceGradOp : public popart::Op {
public:
L1DistanceGradOp(const L1DistanceOp& fwdOp);
std::unique_ptr<popart::Op> clone() const final {
return std::make_unique<L1DistanceGradOp>(*this);
}
void setup() final {
auto AInfo = inInfo(1);
auto BInfo = inInfo(2);
outInfo(0) = AInfo;
outInfo(1) = BInfo;
};
const std::vector<popart::GradInOutMapper>& gradInputInfo() const {
static const std::vector<popart::GradInOutMapper> inInfo = {
{0, 0, popart::GradOpInType::GradOut},
{1, 0, popart::GradOpInType::In},
{2, 1, popart::GradOpInType::In}};
return inInfo;
}
// The Grad Op has 1 output, which is the gradient of the only input
const std::map<int, int>& gradOutToNonGradIn() const {
static const std::map<int, int> outInfo = {{0, 0}, {1, 1}};
return outInfo;
}
bool requiresRandomSeed() const override { return false; }
// an estimate of how valuable sub-graph matching will be
float getSubgraphValue() const final { return getHighSubgraphValue(); }
};
class L1DistanceOp : public popart::Op {
public:
std::string rootPath;
public:
L1DistanceOp(const popart::OperatorIdentifier& _opid,
const popart::Op::Settings& settings_,
const std::string& rootPath_)
: popart::Op(_opid, settings_), rootPath(rootPath_) {}
std::unique_ptr<Op> clone() const final { return std::make_unique<L1DistanceOp>(*this); }
void setup() final {
auto AInfo = inInfo(0);
auto BInfo = inInfo(1);
assert(AInfo.rank() == 2);
assert(BInfo.rank() == 2);
assert(AInfo.dim(1) == BInfo.dim(1));
outInfo(0).set(AInfo.dataType(), {AInfo.dim(0), BInfo.dim(0)});
}
std::vector<std::unique_ptr<popart::Op>> getGradOps() {
std::vector<std::unique_ptr<Op>> upops;
upops.emplace_back(new L1DistanceGradOp(*this));
return upops;
}
float getSubgraphValue() const final { return getHighSubgraphValue(); }
bool requiresRandomSeed() const override { return false; }
};
class L2DistanceGradOp : public popart::Op {
public:
L2DistanceGradOp(const L2DistanceOp& fwdOp);
std::unique_ptr<popart::Op> clone() const final {
return std::make_unique<L2DistanceGradOp>(*this);
}
void setup() final {
auto AInfo = inInfo(1);
auto BInfo = inInfo(2);
outInfo(0) = AInfo;
outInfo(1) = BInfo;
};
const std::vector<popart::GradInOutMapper>& gradInputInfo() const {
static const std::vector<popart::GradInOutMapper> inInfo = {
{0, 0, popart::GradOpInType::GradOut},
{1, 0, popart::GradOpInType::In},
{2, 1, popart::GradOpInType::In},
{3, 0, popart::GradOpInType::Out}};
return inInfo;
}
// The Grad Op has 1 output, which is the gradient of the only input
const std::map<int, int>& gradOutToNonGradIn() const {
static const std::map<int, int> outInfo = {{0, 0}, {1, 1}};
return outInfo;
}
bool requiresRandomSeed() const override { return false; }
// an estimate of how valuable sub-graph matching will be
float getSubgraphValue() const final { return getHighSubgraphValue(); }
};
class L2DistanceOp : public popart::Op {
public:
std::string rootPath;
public:
L2DistanceOp(const popart::OperatorIdentifier& _opid,
const popart::Op::Settings& settings_,
const std::string& rootPath_)
: popart::Op(_opid, settings_), rootPath(rootPath_) {}
std::unique_ptr<Op> clone() const final { return std::make_unique<L2DistanceOp>(*this); }
void setup() final {
auto AInfo = inInfo(0);
auto BInfo = inInfo(1);
assert(AInfo.rank() == 2);
assert(BInfo.rank() == 2);
assert(AInfo.dim(1) == BInfo.dim(1));
outInfo(0).set(AInfo.dataType(), {AInfo.dim(0), BInfo.dim(0)});
}
std::vector<std::unique_ptr<popart::Op>> getGradOps() {
std::vector<std::unique_ptr<Op>> upops;
upops.emplace_back(new L2DistanceGradOp(*this));
return upops;
}
float getSubgraphValue() const final { return getHighSubgraphValue(); }
bool requiresRandomSeed() const override { return false; }
};
using popart::DataType;
using popart::OpDefinition;
static OpDefinition::DataTypes T = {DataType::FLOAT16, DataType::FLOAT};
static OpDefinition l1DistanceOpDef({OpDefinition::Inputs({{"a", T}, {"b", T}}),
OpDefinition::Outputs({{"output", T}}),
OpDefinition::Attributes({{"root_path", {"string"}}})});
static popart::OpCreator<L1DistanceOp> l1DistanceOpCreator(
popart::OpDefinitions({{L1DistanceId, l1DistanceOpDef}}),
[](const popart::OpCreatorInfo& info) {
auto rootPath = info.attributes.getAttribute<popart::Attributes::String>("root_path");
return std::make_unique<L1DistanceOp>(info.opid, info.settings, rootPath);
},
true);
static OpDefinition l2DistanceOpDef({OpDefinition::Inputs({{"a", T}, {"b", T}}),
OpDefinition::Outputs({{"output", T}}),
OpDefinition::Attributes({{"root_path", {"string"}}})});
static popart::OpCreator<L2DistanceOp> l2DistanceOpCreator(
popart::OpDefinitions({{L2DistanceId, l2DistanceOpDef}}),
[](const popart::OpCreatorInfo& info) {
auto rootPath = info.attributes.getAttribute<popart::Attributes::String>("root_path");
return std::make_unique<L2DistanceOp>(info.opid, info.settings, rootPath);
},
true);
class L1DistanceOpx : public popart::popx::Opx {
public:
L1DistanceOpx(popart::Op* op, popart::popx::Devicex* devicex) : popart::popx::Opx(op, devicex) {
verifyOp<L1DistanceOp>(op, {L1DistanceId});
// add codelets to graph
graph().addCodelets(getOp<L1DistanceOp>().rootPath + "/cpp/distance_matrix_codelet.cpp");
}
void grow(poplar::program::Sequence& prog) const final {
auto op = getOp<L1DistanceOp>();
poplar::Tensor A = getInTensor(0);
poplar::Tensor B = getInTensor(1);
poplar::Tensor out = l1distance(graph(), A, B, prog, "l1distance");
setOutTensor(0, out);
}
};
class L1DistanceGradOpx : public popart::popx::Opx {
public:
L1DistanceGradOpx(popart::Op* op, popart::popx::Devicex* devicex)
: popart::popx::Opx(op, devicex) {
verifyOp<L1DistanceGradOp>(op, {L1DistanceGradId});
}
void grow(poplar::program::Sequence& prog) const final {
auto op = getOp<L1DistanceGradOp>();
poplar::Tensor grad = getInTensor(0);
poplar::Tensor A = getInTensor(1);
poplar::Tensor B = getInTensor(2);
auto gradA = l1distancegrad(graph(), A, B, grad, prog, "l1distanceGrad");
auto gradB = l1distancegrad(graph(), B, A, grad.transpose(), prog, "l1distanceGrad");
setOutTensor(0, gradA);
setOutTensor(1, gradB);
}
};
class L2DistanceOpx : public popart::popx::Opx {
public:
L2DistanceOpx(popart::Op* op, popart::popx::Devicex* devicex) : popart::popx::Opx(op, devicex) {
verifyOp<L2DistanceOp>(op, {L2DistanceId});
// add codelets to graph
graph().addCodelets(getOp<L2DistanceOp>().rootPath + "/cpp/distance_matrix_codelet.cpp");
}
void grow(poplar::program::Sequence& prog) const final {
auto op = getOp<L2DistanceOp>();
poplar::Tensor A = getInTensor(0);
poplar::Tensor B = getInTensor(1);
poplar::Tensor out = l2distance(graph(), A, B, prog, "l2distance");
setOutTensor(0, out);
}
};
class L2DistanceGradOpx : public popart::popx::Opx {
public:
L2DistanceGradOpx(popart::Op* op, popart::popx::Devicex* devicex)
: popart::popx::Opx(op, devicex) {
verifyOp<L2DistanceGradOp>(op, {L2DistanceGradId});
}
void grow(poplar::program::Sequence& prog) const final {
auto op = getOp<L2DistanceGradOp>();
poplar::Tensor grad = getInTensor(0);
poplar::Tensor A = getInTensor(1);
poplar::Tensor B = getInTensor(2);
poplar::Tensor dist = getInTensor(3);
auto gradA = l2distancegrad(graph(), A, B, dist, grad, prog, "l2distanceGrad");
auto gradB = l2distancegrad(graph(), B, A, dist.transpose(), grad.transpose(), prog,
"l2distanceGrad");
setOutTensor(0, gradA);
setOutTensor(1, gradB);
}
};
L1DistanceGradOp::L1DistanceGradOp(const L1DistanceOp& fwdOp)
: popart::Op(L1DistanceGradId, fwdOp.settings) {}
L2DistanceGradOp::L2DistanceGradOp(const L2DistanceOp& fwdOp)
: popart::Op(L2DistanceGradId, fwdOp.settings) {}
static popart::popx::OpxCreator<L1DistanceOpx> L1DistanceOpxCreator({L1DistanceId});
static popart::popx::OpxCreator<L1DistanceGradOpx> L1DistanceGradOpxCreator({L1DistanceGradId});
static popart::popx::OpxCreator<L2DistanceOpx> L2DistanceOpxCreator({L2DistanceId});
static popart::popx::OpxCreator<L2DistanceGradOpx> L2DistanceGradOpxCreator({L2DistanceGradId});
} // namespace