-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathodla_ops.cc
434 lines (371 loc) · 17.8 KB
/
odla_ops.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
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
//===- odla_ops.cc --------------------------------------------------------===//
//
// Copyright (C) 2019-2020 Alibaba Group Holding Limited.
// Copyright (c) 2020 Graphcore Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// =============================================================================
#include <ODLA/odla.h>
#include <popart/builder.hpp>
#include <popart/tensorinfo.hpp>
#include <regex>
#include <string>
#include <vector>
#include "common.h"
#include "odla_popart.h"
#include "popart_config.h"
#if !defined(ODLA_VERSION_NUMBER) || (ODLA_VERSION_NUMBER < 50)
#error This library requires minimum ODLA version 0.5
#endif
/* Ops */
/* Binary Ops */
odla_value odla_Add(odla_value lhs, const odla_value rhs,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result = g_comp->builder->aiOnnxOpset10().add(
{lhs->tensor_id, rhs->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Sub(odla_value lhs, odla_value rhs, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result = g_comp->builder->aiOnnxOpset10().sub(
{lhs->tensor_id, rhs->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Mul(odla_value lhs, odla_value rhs, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result = g_comp->builder->aiOnnxOpset10().mul(
{lhs->tensor_id, rhs->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Div(odla_value lhs, odla_value rhs, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result = g_comp->builder->aiOnnxOpset10().div(
{lhs->tensor_id, rhs->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
/* Unary Ops */
// Erf is declared, but not implemented in onnx namespace,
// so we call the custom version temporialy
odla_value odla_Erf(odla_value input, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
auto result = g_comp->builder->aiOnnxOpset10().erf({input->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Floor(odla_value input, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result =
g_comp->builder->aiOnnxOpset10().floor({input->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Sqrt(odla_value input, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result =
g_comp->builder->aiOnnxOpset10().sqrt({input->tensor_id});
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Rsqrt(odla_value input, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
const popart::OperatorIdentifier rsqrt(popart::Domain::ai_graphcore, "Rsqrt",
1, 1, 1);
auto result =
g_comp->builder->customOp(rsqrt, 1, {input->tensor_id}, 1, {}, name)[0];
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Dropout(odla_value input, odla_float32 dropout_prob,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result = g_comp->builder->aiOnnxOpset10().dropout(
{input->tensor_id}, 1, dropout_prob, name)[0];
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Gather(odla_value input, odla_value indices, odla_int32 axis,
odla_value_shape output_dims, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
axis = axis >= 0 ? axis : input->tensor_info.rank() + axis;
popart::TensorId result = g_comp->builder->aiOnnxOpset10().gather(
{input->tensor_id, indices->tensor_id}, axis, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_BatchMatmul(odla_value lhs, odla_bool lhs_trans, odla_value rhs,
odla_bool rhs_trans, odla_value_shape output_dims,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result = g_comp->builder->aiOnnxOpset10().matmul(
{lhs->tensor_id, rhs->tensor_id}, name);
// set the AMP
float amp = PopartConfig::instance()->amp();
g_comp->builder->setAvailableMemoryProportion(result, amp);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
// Y = alpha*A*B + beta*C, popart::Gemm only support A and B rank == 2,
// popart::Matmul support rank range from 1 to 4, but it does not support
// bias and transpose
odla_value odla_Gemm(odla_value lhs, odla_bool transpose_lhs, odla_value rhs,
odla_bool transpose_rhs, odla_float32 alpha,
odla_float32 beta, odla_value bias,
odla_value_shape output_dims, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
int rank = lhs->tensor_info.rank();
/*
if (rank == 2) {
std::vector<popart::TensorId> inputs{lhs->tensor_id, rhs->tensor_id};
if (bias == nullptr || beta == 0) {
static const int64_t zero = 0;
auto dummy_bias = odla_CreateConstant(
{GetOdlaType(lhs->tensor_info.dataType()), {.size = 1, .dims = {1}}},
&zero, (const odla_value_id)((name + "_bias_zero").c_str()));
inputs.push_back(dummy_bias->tensor_id);
} else {
inputs.push_back(bias->tensor_id);
}
popart::TensorId result = g_comp->builder->aiOnnxOpset10().gemm(
inputs, alpha, beta, transpose_lhs, transpose_rhs);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}*/
popart::TensorId lhs_trans = lhs->tensor_id;
std::string transpose_name = std::string(name) + "_transpose";
if (rank >= 2 && transpose_lhs) {
if (rank == 4) {
lhs_trans = g_comp->builder->aiOnnxOpset10().transpose(
{lhs->tensor_id}, std::vector<int64_t>{0, 1, 3, 2}, transpose_name);
} else if (rank == 3) {
lhs_trans = g_comp->builder->aiOnnxOpset10().transpose(
{lhs->tensor_id}, std::vector<int64_t>{0, 2, 1}, transpose_name);
} else if (rank == 2) {
lhs_trans = g_comp->builder->aiOnnxOpset10().transpose(
{lhs->tensor_id}, std::vector<int64_t>{1, 0}, transpose_name);
}
}
popart::TensorId rhs_trans = rhs->tensor_id;
rank = rhs->tensor_info.rank();
if (rank >= 2 && transpose_rhs) {
if (rank == 4) {
rhs_trans = g_comp->builder->aiOnnxOpset10().transpose(
{rhs->tensor_id}, std::vector<int64_t>{0, 1, 3, 2}, transpose_name);
} else if (rank == 3) {
rhs_trans = g_comp->builder->aiOnnxOpset10().transpose(
{rhs->tensor_id}, std::vector<int64_t>{0, 2, 1}, transpose_name);
} else if (rank == 2) {
rhs_trans = g_comp->builder->aiOnnxOpset10().transpose(
{rhs->tensor_id}, std::vector<int64_t>{1, 0}, transpose_name);
}
}
// USE_BATCHED_MATMUL
popart::TensorId result =
g_comp->builder->aiOnnxOpset10().matmul({lhs_trans, rhs_trans}, name);
float amp = PopartConfig::instance()->amp();
g_comp->builder->setAvailableMemoryProportion(result, amp);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Reshape(odla_value input, odla_value_shape output_dims,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
// TODO(unknown) memory leak ?
auto new_shape = odla_CreateConstant(
{ODLA_INT64, {.size = 1, .dims = {output_dims.size}}},
static_cast<odla_void*>(GetPopartShape(output_dims).data()),
(const odla_value_id)((name + "_new_shape").c_str()));
popart::TensorId result = g_comp->builder->aiOnnxOpset10().reshape(
{input->tensor_id, new_shape->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_OneHot(odla_value indices, odla_int32 depth, odla_value values,
odla_int32 axis, odla_value_shape output_dims,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
std::vector<int64_t> depth_data = {depth};
auto depth_v = odla_CreateConstant(
{ODLA_INT64, {.size = 1, .dims = {1}}},
static_cast<odla_void*>(depth_data.data()),
(const odla_value_id)((name + "_depth_value").c_str()));
popart::TensorId result = g_comp->builder->aiOnnxOpset10().onehot(
{indices->tensor_id, depth_v->tensor_id, values->tensor_id}, -1L, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Relu(odla_value input, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
popart::TensorId result =
g_comp->builder->aiOnnxOpset10().relu({input->tensor_id}, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_Softmax(odla_value input, odla_int32 axis,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
axis = axis >= 0 ? axis : input->tensor_info.rank() + axis;
popart::TensorId result =
g_comp->builder->aiOnnxOpset10().softmax({input->tensor_id}, axis, name);
return new _odla_value(result,
{g_comp->builder->getTensorDataType(result),
g_comp->builder->getTensorShape(result)},
name);
}
odla_value odla_GroupNormalization(odla_value input,
odla_memory_layout input_layout,
odla_int32 group, odla_float32 epsilon,
odla_value scale, odla_value offset,
odla_float32 scalar_scale,
odla_float32 scalar_offset,
const odla_value_id id) {
// TODO(unknown) mean var not in use, check group_norm/batch_norm
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
auto outs = g_comp->builder->aiGraphcoreOpset1().groupnormalization(
{input->tensor_id, scale->tensor_id, offset->tensor_id}, group, epsilon,
name);
return new _odla_value(outs[0],
{g_comp->builder->getTensorDataType(outs[0]),
g_comp->builder->getTensorShape(outs[0])},
name);
}
odla_value odla_ArgMax(odla_value input, odla_int32 axis, odla_bool keep_dims,
odla_bool return_last_index,
odla_value_type output_value_type,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
axis = axis >= 0 ? axis : input->tensor_info.rank() + axis;
popart::TensorId reduced = g_comp->builder->aiOnnxOpset10().argmax(
{input->tensor_id}, axis, keep_dims, name);
auto builder = g_comp->builder.get();
odla_value result = new _odla_value(
reduced,
{builder->getTensorDataType(reduced), builder->getTensorShape(reduced)},
name);
return result;
}
odla_value odla_ReduceMean(odla_value input, odla_size_t num_of_axes,
const odla_uint32* axes, odla_bool keep_dims,
odla_value_shape output_dims,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
std::vector<int64_t> axes_vec;
for (int64_t i = 0; i < num_of_axes; i++) {
axes_vec.push_back(axes[i]);
}
popart::TensorId reduced = g_comp->builder->aiOnnxOpset10().reducemean(
{input->tensor_id}, axes_vec, keep_dims, name);
auto builder = g_comp->builder.get();
odla_value result = new _odla_value(
reduced,
{builder->getTensorDataType(reduced), builder->getTensorShape(reduced)},
name);
return result;
}
odla_value odla_Slice(odla_value input, const odla_int32* start,
const odla_int32* end, const odla_int32* stride,
odla_value_shape output_dims, const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
std::vector<int64_t> starts;
std::vector<int64_t> ends;
std::vector<int64_t> axes;
std::vector<int64_t> strides;
int64_t rank = input->tensor_info.rank();
for (int64_t i = 0; i < rank; i++) {
starts.push_back(start[i]);
ends.push_back(end[i]);
axes.push_back(i);
strides.push_back(stride[i]);
}
auto start_tensor =
odla_CreateConstant({ODLA_INT64, {.size = 1, .dims = {rank}}},
static_cast<odla_void*>(starts.data()),
(const odla_value_id)((name + "_starts").c_str()));
auto end_tensor =
odla_CreateConstant({ODLA_INT64, {.size = 1, .dims = {rank}}},
static_cast<odla_void*>(ends.data()),
(const odla_value_id)((name + "_ends").c_str()));
auto axes_tensor =
odla_CreateConstant({ODLA_INT64, {.size = 1, .dims = {rank}}},
static_cast<odla_void*>(axes.data()),
(const odla_value_id)((name + "_axes").c_str()));
auto strides_tensor =
odla_CreateConstant({ODLA_INT64, {.size = 1, .dims = {rank}}},
static_cast<odla_void*>(strides.data()),
(const odla_value_id)((name + "_strides").c_str()));
auto builder = g_comp->builder.get();
popart::TensorId sliced = builder->aiOnnxOpset10().slice(
{input->tensor_id, start_tensor->tensor_id, end_tensor->tensor_id,
axes_tensor->tensor_id, strides_tensor->tensor_id},
name);
odla_value result = new _odla_value(
sliced,
{builder->getTensorDataType(sliced), builder->getTensorShape(sliced)},
name);
return result;
}
odla_value odla_Transpose(odla_value input, odla_value_shape permutations,
odla_value_shape output_dims,
const odla_value_id id) {
const auto& name = id ? std::string(reinterpret_cast<const char*>(id)) : "";
// If permutations.size is 0, perm is empty.
// popart reverses the dimensions by default.
std::vector<int64_t> perm;
for (int64_t i = 0; i < permutations.size; i++) {
perm.push_back(permutations.dims[i]);
}
popart::TensorId transposed = g_comp->builder->aiOnnxOpset10().transpose(
{input->tensor_id}, perm, name);
auto builder = g_comp->builder.get();
odla_value result = new _odla_value(transposed,
{builder->getTensorDataType(transposed),
builder->getTensorShape(transposed)},
name);
return result;
}