forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfloat16.cc
More file actions
685 lines (609 loc) · 21.4 KB
/
bfloat16.cc
File metadata and controls
685 lines (609 loc) · 21.4 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/* Copyright 2017 The TensorFlow Authors. 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 <array>
#include "tensorflow/python/lib/core/bfloat16.h"
#include "tensorflow/core/framework/numeric_types.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/python/lib/core/numpy.h"
#include "tensorflow/python/lib/core/safe_ptr.h"
namespace tensorflow {
namespace {
// Workarounds for Python 2 vs 3 API differences.
#if PY_MAJOR_VERSION < 3
PyObject* MakePyString(const string& s) {
return PyString_FromString(s.c_str());
}
typedef long HashType; // NOLINT
bool TfPyInt_Check(PyObject* object) { return PyInt_Check(object); }
PyObject* TfPyInt_FromLong(long x) { // NOLINT
return PyInt_FromLong(x);
}
long TfPyInt_AsLong(PyObject* x) { // NOLINT
return PyInt_AsLong(x);
}
#else // PY_MAJOR_VERSION < 3
PyObject* MakePyString(const string& s) {
return PyUnicode_FromString(s.c_str());
}
bool TfPyInt_Check(PyObject* object) {
if (!PyLong_Check(object)) {
return 0;
}
int overflow = 0;
PyLong_AsLongAndOverflow(object, &overflow);
return (overflow == 0);
}
PyObject* TfPyInt_FromLong(long x) { // NOLINT
return PyLong_FromLong(x);
}
long TfPyInt_AsLong(PyObject* x) { // NOLINT
return PyLong_AsLong(x);
}
typedef Py_hash_t HashType;
#endif // PY_MAJOR_VERSION < 3
// Forward declaration.
extern PyTypeObject PyBfloat16_Type;
// Representation of a Python bfloat16 object.
struct PyBfloat16 {
PyObject_HEAD; // Python object header
bfloat16 value;
};
// Returns true if 'object' is a PyBfloat16.
bool PyBfloat16_Check(PyObject* object) {
return PyObject_IsInstance(object,
reinterpret_cast<PyObject*>(&PyBfloat16_Type));
}
// Extracts the value of a PyBfloat16 object.
bfloat16 PyBfloat16_Bfloat16(PyObject* object) {
return reinterpret_cast<PyBfloat16*>(object)->value;
}
// Constructs a PyBfloat16 object from a bfloat16.
Safe_PyObjectPtr PyBfloat16_FromBfloat16(bfloat16 x) {
Safe_PyObjectPtr ref =
make_safe(PyBfloat16_Type.tp_alloc(&PyBfloat16_Type, 0));
PyBfloat16* p = reinterpret_cast<PyBfloat16*>(ref.get());
if (p) {
p->value = x;
}
return ref;
}
// Converts a Python object to a bfloat16 value. Returns true on success,
// returns false and reports a Python error on failure.
bool AsBfloat16(PyObject* arg, bfloat16* output) {
if (PyBfloat16_Check(arg)) {
*output = PyBfloat16_Bfloat16(arg);
return true;
}
if (PyFloat_Check(arg)) {
double d = PyFloat_AsDouble(arg);
if (PyErr_Occurred()) {
return false;
}
// TODO(phawkins): check for overflow
*output = bfloat16(d);
return true;
}
if (TfPyInt_Check(arg)) {
long l = TfPyInt_AsLong(arg); // NOLINT
if (PyErr_Occurred()) {
return false;
}
// TODO(phawkins): check for overflow
*output = bfloat16(static_cast<float>(l));
return true;
}
if (PyArray_IsScalar(arg, Float)) {
float f;
PyArray_ScalarAsCtype(arg, &f);
*output = bfloat16(f);
return true;
}
PyErr_Format(PyExc_TypeError, "expected number, got %s",
arg->ob_type->tp_name);
return false;
}
// Converts a PyBfloat16 into a PyFloat.
PyObject* PyBfloat16_Float(PyObject* self) {
bfloat16 x = PyBfloat16_Bfloat16(self);
return PyFloat_FromDouble(static_cast<double>(x));
}
// Converts a PyBfloat16 into a PyInt.
PyObject* PyBfloat16_Int(PyObject* self) {
bfloat16 x = PyBfloat16_Bfloat16(self);
long y = static_cast<long>(x); // NOLINT
return TfPyInt_FromLong(y);
}
// Negates a PyBfloat16.
PyObject* PyBfloat16_Negative(PyObject* self) {
bfloat16 x = PyBfloat16_Bfloat16(self);
return PyBfloat16_FromBfloat16(-x).release();
}
// Binary arithmetic operators on PyBfloat16 values.
#define BFLOAT16_BINOP(name, op) \
PyObject* PyBfloat16_##name(PyObject* a, PyObject* b) { \
bfloat16 x, y; \
if (!AsBfloat16(a, &x) || !AsBfloat16(b, &y)) return nullptr; \
bfloat16 z = x op y; \
return PyBfloat16_FromBfloat16(z).release(); \
}
BFLOAT16_BINOP(Add, +)
BFLOAT16_BINOP(Subtract, -)
BFLOAT16_BINOP(Multiply, *)
BFLOAT16_BINOP(Divide, /)
#undef BFLOAT16_BINOP
// Python number methods for PyBfloat16 objects.
PyNumberMethods PyBfloat16_AsNumber = {
PyBfloat16_Add, // nb_add
PyBfloat16_Subtract, // nb_subtract
PyBfloat16_Multiply, // nb_multiply
#if PY_MAJOR_VERSION < 3
PyBfloat16_Divide, // nb_divide
#endif
nullptr, // nb_remainder
nullptr, // nb_divmod
nullptr, // nb_power
PyBfloat16_Negative, // nb_negative
nullptr, // nb_positive
nullptr, // nb_absolute
nullptr, // nb_nonzero
nullptr, // nb_invert
nullptr, // nb_lshift
nullptr, // nb_rshift
nullptr, // nb_and
nullptr, // nb_xor
nullptr, // nb_or
#if PY_MAJOR_VERSION < 3
nullptr, // nb_coerce
#endif
PyBfloat16_Int, // nb_int
#if PY_MAJOR_VERSION < 3
PyBfloat16_Int, // nb_long
#else
nullptr, // reserved
#endif
PyBfloat16_Float, // nb_float
#if PY_MAJOR_VERSION < 3
nullptr, // nb_oct
nullptr, // nb_hex
#endif
nullptr, // nb_inplace_add
nullptr, // nb_inplace_subtract
nullptr, // nb_inplace_multiply
#if PY_MAJOR_VERSION < 3
nullptr, // nb_inplace_divide
#endif
nullptr, // nb_inplace_remainder
nullptr, // nb_inplace_power
nullptr, // nb_inplace_lshift
nullptr, // nb_inplace_rshift
nullptr, // nb_inplace_and
nullptr, // nb_inplace_xor
nullptr, // nb_inplace_or
nullptr, // nb_floor_divide
PyBfloat16_Divide, // nb_true_divide
nullptr, // nb_inplace_floor_divide
nullptr, // nb_inplace_true_divide
nullptr, // nb_index
};
// Constructs a new PyBfloat16.
PyObject* PyBfloat16_New(PyTypeObject* type, PyObject* args, PyObject* kwds) {
if (kwds && PyDict_Size(kwds)) {
PyErr_SetString(PyExc_TypeError, "constructor takes no keyword arguments");
return nullptr;
}
Py_ssize_t size = PyTuple_Size(args);
if (size != 1) {
PyErr_SetString(PyExc_TypeError,
"expected number as argument to bfloat16 constructor");
return nullptr;
}
PyObject* arg = PyTuple_GetItem(args, 0);
if (PyBfloat16_Check(arg)) {
Py_INCREF(arg);
return arg;
} else {
bfloat16 value;
if (!AsBfloat16(arg, &value)) {
return nullptr;
}
return PyBfloat16_FromBfloat16(value).release();
}
}
// Comparisons on PyBfloat16s.
PyObject* PyBfloat16_RichCompare(PyObject* a, PyObject* b, int op) {
bfloat16 x, y;
if (!AsBfloat16(a, &x) || !AsBfloat16(b, &y)) return nullptr;
bool result;
switch (op) {
case Py_LT:
result = x < y;
break;
case Py_LE:
result = x <= y;
break;
case Py_EQ:
result = x == y;
break;
case Py_NE:
result = x != y;
break;
case Py_GT:
result = x > y;
break;
case Py_GE:
result = x >= y;
break;
default:
LOG(FATAL) << "Invalid op type " << op;
}
return PyBool_FromLong(result);
}
// Implementation of repr() for PyBfloat16.
PyObject* PyBfloat16_Repr(PyObject* self) {
bfloat16 x = reinterpret_cast<PyBfloat16*>(self)->value;
string v = strings::StrCat("bfloat16(", static_cast<float>(x), ")");
return MakePyString(v);
}
// Implementation of str() for PyBfloat16.
PyObject* PyBfloat16_Str(PyObject* self) {
bfloat16 x = reinterpret_cast<PyBfloat16*>(self)->value;
string v = strings::StrCat(static_cast<float>(x));
return MakePyString(v);
}
// Hash function for PyBfloat16. We use the identity function, which is a weak
// hash function.
HashType PyBfloat16_Hash(PyObject* self) {
bfloat16 x = reinterpret_cast<PyBfloat16*>(self)->value;
return x.value;
}
// Python type for PyBfloat16 objects.
PyTypeObject PyBfloat16_Type = {
#if PY_MAJOR_VERSION < 3
PyObject_HEAD_INIT(nullptr) 0, // ob_size
#else
PyVarObject_HEAD_INIT(nullptr, 0)
#endif
"bfloat16", // tp_name
sizeof(PyBfloat16), // tp_basicsize
0, // tp_itemsize
nullptr, // tp_dealloc
nullptr, // tp_print
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare / tp_reserved
PyBfloat16_Repr, // tp_repr
&PyBfloat16_AsNumber, // tp_as_number
nullptr, // tp_as_sequence
nullptr, // tp_as_mapping
PyBfloat16_Hash, // tp_hash
nullptr, // tp_call
PyBfloat16_Str, // tp_str
nullptr, // tp_getattro
nullptr, // tp_setattro
nullptr, // tp_as_buffer
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags
"bfloat16 floating-point values", // tp_doc
nullptr, // tp_traverse
nullptr, // tp_clear
PyBfloat16_RichCompare, // tp_richcompare
0, // tp_weaklistoffset
nullptr, // tp_iter
nullptr, // tp_iternext
nullptr, // tp_methods
nullptr, // tp_members
nullptr, // tp_getset
nullptr, // tp_base
nullptr, // tp_dict
nullptr, // tp_descr_get
nullptr, // tp_descr_set
0, // tp_dictoffset
nullptr, // tp_init
nullptr, // tp_alloc
PyBfloat16_New, // tp_new
nullptr, // tp_free
nullptr, // tp_is_gc
nullptr, // tp_bases
nullptr, // tp_mro
nullptr, // tp_cache
nullptr, // tp_subclasses
nullptr, // tp_weaklist
nullptr, // tp_del
0, // tp_version_tag
};
// Numpy support
PyArray_ArrFuncs NPyBfloat16_ArrFuncs;
PyArray_Descr NPyBfloat16_Descr = {
PyObject_HEAD_INIT(nullptr) & PyBfloat16_Type, // typeobj
// We must register bfloat16 with a kind other than "f", because numpy
// considers two types with the same kind and size to be equal, but
// float16 != bfloat16.
'V', // kind
// TODO(phawkins): there doesn't seem to be a way of guaranteeing a type
// character is unique.
'E', // type
'=', // byteorder
NPY_NEEDS_PYAPI | NPY_USE_GETITEM | NPY_USE_SETITEM, // hasobject
0, // type_num
sizeof(bfloat16), // elsize
alignof(bfloat16), // alignment
nullptr, // subarray
nullptr, // fields
nullptr, // names
&NPyBfloat16_ArrFuncs, // f
};
// Registered numpy type ID. Global variable populated by the registration code.
int npy_bfloat16_ = -1;
// Implementations of NumPy array methods.
PyObject* NPyBfloat16_GetItem(void* data, void* arr) {
bfloat16 x;
memcpy(&x, data, sizeof(bfloat16));
return PyBfloat16_FromBfloat16(x).release();
}
int NPyBfloat16_SetItem(PyObject* item, void* data, void* arr) {
bfloat16 x;
if (!AsBfloat16(item, &x)) return -1;
memcpy(data, &x, sizeof(bfloat16));
return 0;
}
void ByteSwap16(void* value) {
char* p = reinterpret_cast<char*>(value);
std::swap(p[0], p[1]);
}
void NPyBfloat16_CopySwapN(void* dstv, npy_intp dstride, void* srcv,
npy_intp sstride, npy_intp n, int swap, void* arr) {
char* dst = reinterpret_cast<char*>(dstv);
char* src = reinterpret_cast<char*>(srcv);
if (!src) {
return;
}
if (swap) {
for (npy_intp i = 0; i < n; i++) {
char* r = dst + dstride * i;
memcpy(r, src + sstride * i, sizeof(uint16_t));
ByteSwap16(r);
}
} else if (dstride == sizeof(uint16_t) && sstride == sizeof(uint16_t)) {
memcpy(dst, src, n * sizeof(uint16_t));
} else {
for (npy_intp i = 0; i < n; i++) {
memcpy(dst + dstride * i, src + sstride * i, sizeof(uint16_t));
}
}
}
void NPyBfloat16_CopySwap(void* dst, void* src, int swap, void* arr) {
if (!src) {
return;
}
memcpy(dst, src, sizeof(uint16_t));
if (swap) {
ByteSwap16(dst);
}
}
npy_bool NPyBfloat16_NonZero(void* data, void* arr) {
bfloat16 x;
memcpy(&x, data, sizeof(x));
return x != static_cast<bfloat16>(0);
}
int NPyBfloat16_Fill(void* buffer_raw, npy_intp length, void* ignored) {
bfloat16* const buffer = reinterpret_cast<bfloat16*>(buffer_raw);
const float start(buffer[0]);
const float delta = static_cast<float>(buffer[1]) - start;
for (npy_intp i = 2; i < length; ++i) {
buffer[i] = static_cast<bfloat16>(start + i * delta);
}
return 0;
}
// NumPy casts
// Performs a NumPy array cast from type 'From' to 'To'.
template <typename From, typename To>
void NPyCast(void* from_void, void* to_void, npy_intp n, void* fromarr,
void* toarr) {
const From* from = reinterpret_cast<From*>(from_void);
To* to = reinterpret_cast<To*>(to_void);
for (npy_intp i = 0; i < n; ++i) {
to[i] = static_cast<To>(from[i]);
}
}
// Registers a cast between bfloat16 and type 'T'. 'numpy_type' is the NumPy
// type corresponding to 'T'. If 'cast_is_safe', registers that bfloat16 can be
// safely coerced to T.
template <typename T>
bool RegisterBfloat16Cast(int numpy_type, bool cast_is_safe) {
if (PyArray_RegisterCastFunc(PyArray_DescrFromType(numpy_type), npy_bfloat16_,
NPyCast<T, bfloat16>) < 0) {
return false;
}
if (PyArray_RegisterCastFunc(&NPyBfloat16_Descr, numpy_type,
NPyCast<bfloat16, T>) < 0) {
return false;
}
if (cast_is_safe && PyArray_RegisterCanCast(&NPyBfloat16_Descr, numpy_type,
NPY_NOSCALAR) < 0) {
return false;
}
return true;
}
template <typename InType, typename OutType, typename Functor>
void BinaryUFunc(char** args, npy_intp* dimensions, npy_intp* steps,
void* data) {
const char* i0 = args[0];
const char* i1 = args[1];
char* o = args[2];
for (npy_intp k = 0; k < *dimensions; k++) {
InType x = *reinterpret_cast<const InType*>(i0);
InType y = *reinterpret_cast<const InType*>(i1);
*reinterpret_cast<OutType*>(o) = Functor()(x, y);
i0 += steps[0];
i1 += steps[1];
o += steps[2];
}
}
template <typename Functor>
void CompareUFunc(char** args, npy_intp* dimensions, npy_intp* steps,
void* data) {
BinaryUFunc<bfloat16, npy_bool, Functor>(args, dimensions, steps, data);
}
struct Bfloat16EqFunctor {
npy_bool operator()(bfloat16 a, bfloat16 b) { return a == b; }
};
struct Bfloat16NeFunctor {
npy_bool operator()(bfloat16 a, bfloat16 b) { return a != b; }
};
struct Bfloat16LtFunctor {
npy_bool operator()(bfloat16 a, bfloat16 b) { return a < b; }
};
struct Bfloat16GtFunctor {
npy_bool operator()(bfloat16 a, bfloat16 b) { return a > b; }
};
struct Bfloat16LeFunctor {
npy_bool operator()(bfloat16 a, bfloat16 b) { return a <= b; }
};
struct Bfloat16GeFunctor {
npy_bool operator()(bfloat16 a, bfloat16 b) { return a >= b; }
};
// Initializes the module.
bool Initialize() {
// It's critical to import umath to avoid crash in open source build.
import_umath1(false);
Safe_PyObjectPtr numpy_str = make_safe(MakePyString("numpy"));
if (!numpy_str) {
return false;
}
Safe_PyObjectPtr numpy = make_safe(PyImport_Import(numpy_str.get()));
if (!numpy) {
return false;
}
// We hit a mysterious crash if we haven't initialized numpy before this:
PyBfloat16_Type.tp_base = &PyGenericArrType_Type;
if (PyType_Ready(&PyBfloat16_Type) < 0) {
return false;
}
// Initializes the NumPy descriptor.
PyArray_InitArrFuncs(&NPyBfloat16_ArrFuncs);
NPyBfloat16_ArrFuncs.getitem = NPyBfloat16_GetItem;
NPyBfloat16_ArrFuncs.setitem = NPyBfloat16_SetItem;
NPyBfloat16_ArrFuncs.copyswapn = NPyBfloat16_CopySwapN;
NPyBfloat16_ArrFuncs.copyswap = NPyBfloat16_CopySwap;
NPyBfloat16_ArrFuncs.nonzero = NPyBfloat16_NonZero;
NPyBfloat16_ArrFuncs.fill = NPyBfloat16_Fill;
Py_TYPE(&NPyBfloat16_Descr) = &PyArrayDescr_Type;
npy_bfloat16_ = PyArray_RegisterDataType(&NPyBfloat16_Descr);
if (npy_bfloat16_ < 0) return false;
// Support dtype(bfloat16)
if (PyDict_SetItemString(PyBfloat16_Type.tp_dict, "dtype",
reinterpret_cast<PyObject*>(&NPyBfloat16_Descr)) <
0) {
return false;
}
// Register casts
// We lie shamelessly and say that a cast from half to bfloat16 is safe.
// Numpy frequently uses the smallest legal representation type for small
// float constants (e.g., 1.0), which is often float16. Things break if these
// cannot be converted transparently to bfloat16.
if (!RegisterBfloat16Cast<Eigen::half>(NPY_HALF, /*cast_is_safe=*/true)) {
return false;
}
if (!RegisterBfloat16Cast<float>(NPY_FLOAT, /*cast_is_safe=*/true)) {
return false;
}
if (!RegisterBfloat16Cast<double>(NPY_DOUBLE, /*cast_is_safe=*/true)) {
return false;
}
if (!RegisterBfloat16Cast<int32>(NPY_INT32, /*cast_is_safe=*/false)) {
return false;
}
if (!RegisterBfloat16Cast<int64>(NPY_INT64, /*cast_is_safe=*/false)) {
return false;
}
// Following the numpy convention. imag part is dropped when converting to
// float.
if (!RegisterBfloat16Cast<complex64>(NPY_COMPLEX64, /*cast_is_safe=*/true)) {
return false;
}
if (!RegisterBfloat16Cast<complex128>(NPY_COMPLEX128,
/*cast_is_safe=*/true)) {
return false;
}
// Register ufuncs
auto register_ufunc = [&](const char* name, PyUFuncGenericFunction fn,
const std::array<int, 3>& types) {
Safe_PyObjectPtr ufunc_obj =
make_safe(PyObject_GetAttrString(numpy.get(), name));
if (!ufunc_obj) {
return false;
}
PyUFuncObject* ufunc = reinterpret_cast<PyUFuncObject*>(ufunc_obj.get());
if (types.size() != ufunc->nargs) {
PyErr_Format(PyExc_AssertionError,
"ufunc %s takes %d arguments, loop takes %lu", name,
ufunc->nargs, types.size());
return false;
}
if (PyUFunc_RegisterLoopForType(ufunc, npy_bfloat16_, fn,
const_cast<int*>(types.data()),
nullptr) < 0) {
return false;
}
return true;
};
// Comparisons
const std::array<int, 3> compare_types = {
{npy_bfloat16_, npy_bfloat16_, NPY_BOOL}};
if (!register_ufunc("equal", CompareUFunc<Bfloat16EqFunctor>,
compare_types)) {
return false;
}
if (!register_ufunc("not_equal", CompareUFunc<Bfloat16NeFunctor>,
compare_types)) {
return false;
}
if (!register_ufunc("less", CompareUFunc<Bfloat16LtFunctor>, compare_types)) {
return false;
}
if (!register_ufunc("greater", CompareUFunc<Bfloat16GtFunctor>,
compare_types)) {
return false;
}
if (!register_ufunc("less_equal", CompareUFunc<Bfloat16LeFunctor>,
compare_types)) {
return false;
}
if (!register_ufunc("greater_equal", CompareUFunc<Bfloat16GeFunctor>,
compare_types)) {
return false;
}
return true;
}
} // namespace
void RegisterNumpyBfloat16() {
if (npy_bfloat16_ >= 0) {
// Already initialized.
return;
}
if (!Initialize()) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError, "cannot load bfloat16 module.");
}
PyErr_Print();
}
}
PyObject* Bfloat16PyType() {
CHECK(PyBfloat16_Type.tp_base != nullptr);
Py_INCREF(&PyBfloat16_Type);
return reinterpret_cast<PyObject*>(&PyBfloat16_Type);
}
int Bfloat16NumpyType() {
CHECK_GE(npy_bfloat16_, 0);
return npy_bfloat16_;
}
} // namespace tensorflow