Repository navigation
Expand file tree
/
Copy pathpy_seq_tensor.cc
More file actions
902 lines (796 loc) · 30.3 KB
/
Copy pathpy_seq_tensor.cc
File metadata and controls
902 lines (796 loc) · 30.3 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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/* 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 "tensorflow/python/lib/core/py_seq_tensor.h"
#include "tensorflow/c/eager/tfe_context_internal.h"
#include "tensorflow/c/eager/tfe_tensorhandle_internal.h"
#include "tensorflow/c/tensor_interface.h"
#include "tensorflow/c/tf_tensor_internal.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/platform/errors.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/tstring.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/python/lib/core/ndarray_tensor.h"
#include "tensorflow/python/lib/core/ndarray_tensor_bridge.h"
#include "tensorflow/python/lib/core/numpy.h"
#include "tensorflow/python/lib/core/py_util.h"
#include "tensorflow/python/lib/core/safe_ptr.h"
namespace tensorflow {
namespace {
inline bool PyIsInstance(PyObject* obj, PyTypeObject* t) {
return PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(t));
}
inline PyObject* PyType(PyObject* obj) {
return reinterpret_cast<PyObject*>(obj->ob_type);
}
bool IsPyString(PyObject* obj) {
return PyBytes_Check(obj) || PyUnicode_Check(obj);
}
bool IsPyInt(PyObject* obj) {
#if PY_MAJOR_VERSION >= 3
return PyLong_Check(obj) ||
PyIsInstance(obj, &PyIntegerArrType_Type); // NumPy integers
#else
return PyInt_Check(obj) || PyLong_Check(obj) ||
PyIsInstance(obj, &PyIntegerArrType_Type); // NumPy integers
#endif
}
bool IsPyDouble(PyObject* obj) {
return PyIsInstance(obj, &PyDoubleArrType_Type); // NumPy double type.
}
bool IsNumpyHalf(PyObject* obj) {
return PyIsInstance(obj, &PyHalfArrType_Type);
}
bool IsPyFloat(PyObject* obj) {
return PyFloat_Check(obj) ||
PyIsInstance(obj, &PyFloatingArrType_Type); // NumPy float types
}
struct ConverterState {
// The inferred tensor shape.
gtl::InlinedVector<int64, 4> inferred_shape;
// The inferred tensor data type.
DataType inferred_dtype;
// The following fields are used by ZeroDimArrayToScalar.
// We cache the last result of the check for a zero dimensional array: the
// function is called many times in a conversion, and most of the time is
// to check for the same type. This cache can reduce the conversion time by
// about 25%.
PyTypeObject* last_zerodim_type;
bool last_zerodim_check;
ConverterState() : inferred_dtype(DT_INVALID), last_zerodim_type(nullptr) {}
};
// If the input is a zero dimensional PyArray return it converted to a scalar.
// Otherwise return the input and increment its reference count.
// Users must Py_DECREF the output of this method.
PyObject* ZeroDimArrayToScalar(PyObject* obj, ConverterState* state) {
auto type = Py_TYPE(obj);
auto pyarray_obj = reinterpret_cast<PyArrayObject*>(obj);
if (type != state->last_zerodim_type) {
state->last_zerodim_type = type;
state->last_zerodim_check =
PyObject_TypeCheck(obj, &PyArray_Type) &&
!PyObject_TypeCheck(obj, &PyGenericArrType_Type);
}
if (state->last_zerodim_check && PyArray_NDIM(pyarray_obj) == 0) {
obj = PyArray_ToScalar(PyArray_DATA(pyarray_obj), pyarray_obj);
} else {
Py_INCREF(obj);
}
return obj;
}
// Sets *elem to a NEW reference to an element in seq on success.
// REQUIRES: PySequence_Check(seq) && PySequence_Length(seq) > 0.
Status SampleElementFromSequence(PyObject* seq, PyObject** elem) {
*elem = PySequence_GetItem(seq, 0);
if (*elem != nullptr) return Status::OK();
// seq may implement the sequence protocol (i.e., implement __getitem__)
// but may legitimately not have a 0-th element (__getitem__(self, 0)
// raises a KeyError). For example:
// seq = pandas.Series([0, 1, 2], index=[2, 4, 6])
//
// We don't actually care for the element at key 0, any element will do
// for inferring the element types. All elements are expected to
// have the same type, and this will be validated when converting
// to an EagerTensor.
PyErr_Clear();
Safe_PyObjectPtr iter(PyObject_GetIter(seq));
if (PyErr_Occurred()) {
return errors::InvalidArgument("Cannot infer dtype of a ",
Py_TYPE(seq)->tp_name,
" object: ", PyExceptionFetch());
}
*elem = PyIter_Next(iter.get());
if (PyErr_Occurred()) {
return errors::InvalidArgument(
"Cannot infer dtype of a ", Py_TYPE(seq)->tp_name,
" object, as iter(<object>).next() failed: ", PyExceptionFetch());
}
if (*elem == nullptr) {
return errors::InvalidArgument("Cannot infer dtype of a ",
Py_TYPE(seq)->tp_name,
" object since it is an empty sequence");
}
return Status::OK();
}
tstring PyRepr(PyObject* obj);
bool IsPyDimension(PyObject* obj);
Status InferShapeAndType(PyObject* obj, ConverterState* state) {
std::vector<Safe_PyObjectPtr> refs_to_clean;
while (true) {
// Convert any zero dimensional numpy arrays to scalars first of all.
// We also have to make sure a reference to the safe_obj is kept.
obj = ZeroDimArrayToScalar(obj, state);
refs_to_clean.push_back(make_safe(obj));
// We test strings first, in case a string is considered a sequence.
if (IsPyString(obj)) {
state->inferred_dtype = DT_STRING;
} else if (PySequence_Check(obj)) {
auto length = PySequence_Length(obj);
if (length > 0) {
state->inferred_shape.push_back(length);
PyObject* elem = nullptr;
TF_RETURN_IF_ERROR(SampleElementFromSequence(obj, &elem));
obj = elem;
refs_to_clean.push_back(make_safe(obj));
continue;
} else if (length == 0) {
state->inferred_shape.push_back(length);
state->inferred_dtype = DT_INVALID; // Invalid dtype for empty tensors.
} else {
// The sequence does not have a valid length (PySequence_Length < 0).
if (PyErr_Occurred()) {
// PySequence_Length failed and set an exception. Fetch the message
// and convert it to a failed status.
return errors::InvalidArgument(PyExceptionFetch());
} else {
// This is almost certainly dead code: PySequence_Length failed but
// did not set an exception.
return errors::InvalidArgument(
"Attempted to convert an invalid sequence to a Tensor.");
}
}
} else if (IsPyDouble(obj)) {
state->inferred_dtype = DT_DOUBLE;
} else if (IsNumpyHalf(obj)) {
state->inferred_dtype = DT_HALF;
} else if (IsPyFloat(obj)) {
state->inferred_dtype = DT_FLOAT;
} else if (PyBool_Check(obj) || PyIsInstance(obj, &PyBoolArrType_Type)) {
// Have to test for bool before int, since IsInt(True/False) == true.
state->inferred_dtype = DT_BOOL;
} else if (IsPyInt(obj)) {
state->inferred_dtype = DT_INT64;
} else if (IsPyDimension(obj)) {
state->inferred_dtype = DT_INT64;
} else if (PyComplex_Check(obj) ||
PyIsInstance(obj, &PyComplexFloatingArrType_Type)) { // NumPy
state->inferred_dtype = DT_COMPLEX128;
} else {
return errors::InvalidArgument("Attempt to convert a value (",
PyRepr(obj),
") with an unsupported type (",
PyRepr(PyType(obj)), ") to a Tensor.");
}
return Status::OK();
}
}
// Error messages
const char ErrorConverting[] =
"Error while converting Python sequence to Tensor.";
const char ErrorRectangular[] =
"Can't convert non-rectangular Python sequence to Tensor.";
const char ErrorMixedTypes[] =
"Can't convert Python sequence with mixed types to Tensor.";
const char ErrorOutOfRange[] =
"Can't convert Python sequence with out-of-range integer to Tensor.";
const char ErrorOutOfRangeDouble[] =
"Can't convert Python sequence with a value out of range for a "
"double-precision float.";
const char ErrorConvertingUnicodeString[] =
"Error converting unicode string while converting Python sequence to "
"Tensor.";
const char ErrorFoundInt64[] =
"Can't convert Python sequence with out-of-range integer to int32 Tensor.";
const char ErrorFoundFloat[] =
"Can't convert Python sequence with floating point values to integer "
"Tensor.";
// Defines a converter that recursively converts an object into
// an array of type T using the conversion function defined by the
// traits class in a ConvertScalar function.
//
// Note that these helper functions require shape.dims() >= 1.
template <class T>
struct ConverterTraits {
static const tensorflow::DataType kTypeEnum;
static const char* ConvertScalar(PyObject* v, T* out);
};
template <class T>
struct Converter {
static const char* Helper(PyObject* obj, int depth, ConverterState* state,
T** buf) {
if (TF_PREDICT_FALSE(obj == nullptr)) {
return ErrorConverting;
}
Safe_PyObjectPtr seq = make_safe(PySequence_Fast(obj, ""));
if (TF_PREDICT_FALSE(seq == nullptr)) return ErrorRectangular;
const int64 s = state->inferred_shape[depth];
if (TF_PREDICT_FALSE(s != PySequence_Fast_GET_SIZE(seq.get()))) {
return ErrorRectangular;
}
if (state->inferred_shape.size() - depth > 1) {
/* Iterate over outer dim, and recursively convert each element. */
for (int64 i = 0; i < s; ++i) {
const char* error = Helper(PySequence_Fast_GET_ITEM(seq.get(), i),
depth + 1, state, buf);
if (TF_PREDICT_FALSE(error != nullptr)) return error;
}
} else {
PyObject** l = PySequence_Fast_ITEMS(seq.get());
for (int64 i = 0; i < s; ++i) {
auto scalar = ZeroDimArrayToScalar(l[i], state);
const char* error = ConverterTraits<T>::ConvertScalar(scalar, *buf);
Py_DECREF(scalar);
if (TF_PREDICT_FALSE(error != nullptr)) return error;
++*buf;
}
}
return nullptr;
}
static Status Convert(TFE_Context* ctx, PyObject* obj, ConverterState* state,
TFE_TensorHandle** h, const char** error) {
// TODO(josh11b): Allocator & attributes
AbstractTensorInterface* t;
if (state->inferred_shape.empty()) { /* Scalar case */
T value;
auto scalar = ZeroDimArrayToScalar(obj, state);
*error = ConverterTraits<T>::ConvertScalar(scalar, &value);
Py_DECREF(scalar);
if (*error != nullptr) return errors::InvalidArgument(*error);
t = ConverterTraits<T>::CreateScalar(ctx, value);
if (t == nullptr) {
return errors::Internal("Cannot create tensor.");
}
} else {
t = ConverterTraits<T>::CreateTensor(ctx, state->inferred_shape);
if (t == nullptr) {
return errors::Internal("Cannot create tensor.");
}
if (t->NumElements() > 0) {
T* buf = static_cast<T*>(t->Data());
*error = Helper(obj, 0, state, &buf);
if (*error != nullptr) {
t->Release();
return errors::InvalidArgument(*error);
}
}
}
*h = tensorflow::wrap(tensorflow::unwrap(ctx)->CreateLocalHandle(t));
t->Release();
return Status::OK();
}
};
// Int support
template <>
struct ConverterTraits<int64> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx, int64 value) {
return tensorflow::unwrap(ctx)->CreateInt64Scalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_INT64, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, int64* out) {
#if PY_MAJOR_VERSION < 3
if (TF_PREDICT_TRUE(PyInt_Check(v))) {
*out = PyInt_AS_LONG(v);
return nullptr;
}
#endif
if (TF_PREDICT_TRUE(PyLong_Check(v) || IsPyDimension(v))) {
int overflow = 0;
// Have to use LongLong for 64 bits, since long is 32 bits on Windows.
*out = PyLong_AsLongLongAndOverflow(v, &overflow);
if (TF_PREDICT_FALSE(overflow)) return ErrorOutOfRange;
return nullptr;
}
if (PyIsInstance(v, &PyIntegerArrType_Type)) { // NumPy integers
#if PY_MAJOR_VERSION < 3
Safe_PyObjectPtr as_int = make_safe(PyNumber_Int(v));
#else
Safe_PyObjectPtr as_int = make_safe(PyNumber_Long(v));
#endif
return ConvertScalar(as_int.get(), out);
}
if (IsPyFloat(v)) return ErrorFoundFloat;
return ErrorMixedTypes;
}
};
typedef Converter<int64> Int64Converter;
template <>
struct ConverterTraits<uint64> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx, uint64 value) {
return tensorflow::unwrap(ctx)->CreateUint64Scalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_UINT64, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, uint64* out) {
#if PY_MAJOR_VERSION < 3
if (TF_PREDICT_TRUE(PyInt_Check(v))) {
*out = PyInt_AsUnsignedLongLongMask(v);
return nullptr;
}
#endif
if (TF_PREDICT_TRUE(PyLong_Check(v) || IsPyDimension(v))) {
*out = PyLong_AsUnsignedLongLong(v);
return nullptr;
}
if (PyIsInstance(v, &PyIntegerArrType_Type)) { // NumPy integers
#if PY_MAJOR_VERSION < 3
Safe_PyObjectPtr as_int = make_safe(PyNumber_Int(v));
#else
Safe_PyObjectPtr as_int = make_safe(PyNumber_Long(v));
#endif
return ConvertScalar(as_int.get(), out);
}
if (IsPyFloat(v)) return ErrorFoundFloat;
return ErrorMixedTypes;
}
};
typedef Converter<uint64> UInt64Converter;
template <>
struct ConverterTraits<int32> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx, int32 value) {
return tensorflow::unwrap(ctx)->CreateInt32Scalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_INT32, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, int32* out) {
int64 i;
#if PY_MAJOR_VERSION < 3
if (TF_PREDICT_TRUE(PyInt_Check(v))) {
i = PyInt_AS_LONG(v);
} else
#endif
if (PyLong_Check(v) || IsPyDimension(v)) {
int overflow = 0;
// Have to use LongLong for 64 bits, since long is 32 bits on Windows.
i = PyLong_AsLongLongAndOverflow(v, &overflow);
if (TF_PREDICT_FALSE(overflow)) return ErrorOutOfRange;
} else if (PyIsInstance(v, &PyIntegerArrType_Type)) { // NumPy integers
#if PY_MAJOR_VERSION < 3
Safe_PyObjectPtr as_int = make_safe(PyNumber_Int(v));
#else
Safe_PyObjectPtr as_int = make_safe(PyNumber_Long(v));
#endif
return ConvertScalar(as_int.get(), out);
} else if (IsPyFloat(v)) {
return ErrorFoundFloat;
} else {
return ErrorMixedTypes;
}
*out = static_cast<uint32>(static_cast<uint64>(i));
// Check for 32-bit overflow.
if (TF_PREDICT_FALSE(i != *out)) return ErrorFoundInt64;
return nullptr;
}
};
typedef Converter<int32> Int32Converter;
// Floating-point support
// Returns `true` if `out` overflows when converted from `as_double`.
template <class T>
static inline bool CheckForOverflow(double as_double, T* out) {
return (sizeof(T) < sizeof(double) && std::isinf(*out) &&
std::isfinite(as_double));
}
// There is no `std::isinf` that takes `Eigen::half` as argument but Eigen
// provides `Eigen::half_impl::isinf` instead.
template <>
inline bool CheckForOverflow<Eigen::half>(double as_double, Eigen::half* out) {
return (sizeof(Eigen::half) < sizeof(double) &&
Eigen::half_impl::isinf(*out) && std::isfinite(as_double));
}
template <class T>
static const char* ConvertOneFloat(PyObject* v, T* out) {
if (PyErr_Occurred()) {
return nullptr;
}
if (TF_PREDICT_TRUE(PyFloat_Check(v))) {
const double as_double = PyFloat_AS_DOUBLE(v);
*out = static_cast<T>(as_double);
// Check for overflow
if (TF_PREDICT_FALSE(CheckForOverflow<T>(as_double, out))) {
return ErrorOutOfRangeDouble;
}
return nullptr;
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(v)) {
*out = static_cast<T>(PyInt_AS_LONG(v));
return nullptr;
}
#endif
if (PyLong_Check(v)) {
*out = static_cast<T>(PyLong_AsDouble(v));
if (PyErr_Occurred()) return ErrorOutOfRangeDouble;
return nullptr;
}
if (PyIsInstance(v, &PyFloatingArrType_Type)) { // NumPy float types
Safe_PyObjectPtr as_float = make_safe(PyNumber_Float(v));
if (PyErr_Occurred()) {
return nullptr;
}
return ConvertOneFloat<T>(as_float.get(), out);
}
if (PyIsInstance(v, &PyIntegerArrType_Type)) { // NumPy integers
#if PY_MAJOR_VERSION < 3
Safe_PyObjectPtr as_int = make_safe(PyNumber_Int(v));
#else
Safe_PyObjectPtr as_int = make_safe(PyNumber_Long(v));
#endif
if (PyErr_Occurred()) {
return nullptr;
}
return ConvertOneFloat<T>(as_int.get(), out);
}
return ErrorMixedTypes;
}
template <>
struct ConverterTraits<float> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx, float value) {
return tensorflow::unwrap(ctx)->CreateFloatScalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_FLOAT, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, float* out) {
return ConvertOneFloat<float>(v, out);
}
};
template <>
struct ConverterTraits<double> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx, double value) {
return tensorflow::unwrap(ctx)->CreateDoubleScalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_DOUBLE, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, double* out) {
return ConvertOneFloat<double>(v, out);
}
};
typedef Converter<double> DoubleConverter;
typedef Converter<float> FloatConverter;
template <>
struct ConverterTraits<Eigen::half> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx,
Eigen::half value) {
return tensorflow::unwrap(ctx)->CreateHalfScalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_HALF, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, Eigen::half* out) {
return ConvertOneFloat<Eigen::half>(v, out);
}
};
typedef Converter<Eigen::half> NumpyHalfConverter;
// String support
template <>
struct ConverterTraits<tstring> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx,
tstring value) {
return tensorflow::unwrap(ctx)->CreateStringScalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_STRING, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, tstring* out) {
if (PyBytes_Check(v)) {
out->assign(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v));
return nullptr;
}
if (PyUnicode_Check(v)) {
#if PY_MAJOR_VERSION >= 3
Py_ssize_t size;
const char* str = PyUnicode_AsUTF8AndSize(v, &size);
if (str == nullptr) return ErrorConvertingUnicodeString;
out->assign(str, size);
return nullptr;
#else
PyObject* py_str = PyUnicode_AsUTF8String(v);
if (py_str == nullptr) return ErrorConvertingUnicodeString;
out->assign(PyBytes_AS_STRING(py_str), PyBytes_GET_SIZE(py_str));
Py_DECREF(py_str);
return nullptr;
#endif
}
return ErrorMixedTypes;
}
};
typedef Converter<tstring> StringConverter;
// Converts Python object `c` that should hold a Python string into a
// C++ string in *out. Returns nullptr on success, or a message on error.
// Defined below, but forward declared here for use in PyRepr.
tstring PyRepr(PyObject* obj) {
if (obj == nullptr) {
return "<null>";
}
Safe_PyObjectPtr repr_obj = make_safe(PyObject_Repr(obj));
if (repr_obj) {
tstring repr_str;
if (ConverterTraits<tstring>::ConvertScalar(repr_obj.get(), &repr_str) ==
nullptr) {
return repr_str;
}
}
return "<error computing repr()>";
}
bool IsPyDimension(PyObject* obj) {
const char* tp_name = obj->ob_type->tp_name;
if (strcmp(tp_name, "Dimension") != 0) return false;
bool ret = str_util::EndsWith(
PyRepr(PyType(obj)),
"tensorflow.python.framework.tensor_shape.Dimension'>");
return ret;
}
// Complex support
template <>
struct ConverterTraits<complex128> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx,
complex128 value) {
return tensorflow::unwrap(ctx)->CreateComplex128Scalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_COMPLEX128, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, complex128* out) {
if (PyComplex_Check(v)) {
*out = complex128(PyComplex_RealAsDouble(v), PyComplex_ImagAsDouble(v));
return nullptr;
} else if (PyIsInstance(v, &PyComplexFloatingArrType_Type)) { // NumPy
auto as_complex = PyComplex_AsCComplex(v);
*out = complex128(as_complex.real, as_complex.imag);
return nullptr;
}
double as_double;
auto error = ConvertOneFloat<double>(v, &as_double);
if (error != nullptr) return error;
*out = complex128(as_double, 0.0);
return nullptr;
}
};
typedef Converter<complex128> Complex128Converter;
// Bool support
template <>
struct ConverterTraits<bool> {
static AbstractTensorInterface* CreateScalar(TFE_Context* ctx, bool value) {
return tensorflow::unwrap(ctx)->CreateBoolScalar(value);
}
static AbstractTensorInterface* CreateTensor(
TFE_Context* ctx, absl::Span<const int64> dim_sizes) {
return tensorflow::unwrap(ctx)->CreateTensor(DT_BOOL, dim_sizes);
}
static const char* ConvertScalar(PyObject* v, bool* out) {
if (v == Py_True) {
*out = true;
} else if (v == Py_False) {
*out = false;
} else if (PyIsInstance(v, &PyBoolArrType_Type)) { // NumPy
*out = PyObject_IsTrue(v);
} else {
return ErrorMixedTypes;
}
return nullptr;
}
};
typedef Converter<bool> BoolConverter;
// Convert a Python numpy.ndarray object to a TFE_TensorHandle.
// The two may share underlying storage so changes to one may reflect in the
// other.
TFE_TensorHandle* NumpyToTFE_TensorHandle(TFE_Context* ctx, PyObject* obj) {
Safe_TF_TensorPtr tf_tensor = make_safe(static_cast<TF_Tensor*>(nullptr));
Status status = tensorflow::NdarrayToTensor(ctx, obj, &tf_tensor,
true /*convert_string*/);
if (TF_PREDICT_FALSE(!status.ok())) {
PyErr_SetString(PyExc_ValueError,
tensorflow::strings::StrCat(
"Failed to convert a NumPy array to a Tensor (",
status.error_message(), ").")
.c_str());
return nullptr;
}
return tensorflow::wrap(
tensorflow::unwrap(ctx)->CreateLocalHandle(tf_tensor->tensor));
}
} // namespace
// TODO(b/147743551): This function handles enough conversions to justify
// promoting to something like PyObjectToTensorHandle.
// TODO(b/147828820): Handle Tensors properly.
TFE_TensorHandle* PySeqToTFE_TensorHandle(TFE_Context* ctx, PyObject* obj,
DataType dtype) {
// Shortcut: __array__ objects (such as Pandas data frames).
// These objects are efficiently handled by Numpy. We transform them into
// Numpy arrays and handle them in the Numpy case below. Note that Tensors
// implement the __array__ function, and will be handled in this shortcut.
Safe_PyObjectPtr array =
make_safe(PyArray_FromArrayAttr(obj, nullptr, nullptr));
if (array == nullptr) {
return nullptr;
}
if (array.get() == Py_NotImplemented) {
// The Py_NotImplemented returned from PyArray_FromArrayAttr is not
// Py_INCREF'ed, so we don't want the Safe_PyObjectPtr to Py_DECREF it.
array.release();
} else {
// PyArray_FromArrayAttr ensures that `array` is a PyArrayObject, so all
// we have to do is replace `obj` with it and continue.
obj = array.get();
}
// Shortcut: Numpy arrays.
if (PyArray_Check(obj)) {
int desired_np_dtype = -1;
if (dtype != tensorflow::DT_INVALID) {
if (!tensorflow::TF_DataType_to_PyArray_TYPE(
static_cast<TF_DataType>(dtype), &desired_np_dtype)
.ok()) {
PyErr_SetString(
PyExc_TypeError,
tensorflow::strings::StrCat("Invalid dtype argument value ", dtype)
.c_str());
return nullptr;
}
}
PyArrayObject* array = reinterpret_cast<PyArrayObject*>(obj);
int array_dtype = PyArray_TYPE(array);
Safe_PyObjectPtr safe_value(nullptr);
// Use Numpy to convert between types if needed.
if ((desired_np_dtype >= 0 && desired_np_dtype != array_dtype) ||
!PyArray_ISCARRAY(array)) {
int new_dtype = desired_np_dtype >= 0 ? desired_np_dtype : array_dtype;
safe_value = tensorflow::make_safe(
PyArray_FromAny(obj, PyArray_DescrFromType(new_dtype), 0, 0,
NPY_ARRAY_CARRAY_RO | NPY_ARRAY_FORCECAST, nullptr));
if (PyErr_Occurred()) return nullptr;
if (safe_value == nullptr) {
PyErr_SetString(PyExc_ValueError, "Error while casting a numpy value");
}
obj = safe_value.get();
}
return NumpyToTFE_TensorHandle(ctx, obj);
}
ConverterState state;
Status status = InferShapeAndType(obj, &state);
if (!status.ok()) {
PyErr_SetString(PyExc_ValueError, status.error_message().c_str());
return nullptr;
}
DataType requested_dtype = DT_INVALID;
if (dtype != DT_INVALID) {
requested_dtype = dtype;
}
// NOTE(josh11b): If don't successfully convert to the requested type,
// we just try instead to create a tensor of the inferred type and
// let the caller convert it to the requested type using a cast
// operation.
const char* error = nullptr;
TFE_TensorHandle* handle = nullptr;
status = errors::Unimplemented("Missing Python -> Tensor conversion for ",
DataTypeString(state.inferred_dtype));
switch (requested_dtype) {
case DT_FLOAT:
status = FloatConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_DOUBLE:
status = DoubleConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_HALF:
status = NumpyHalfConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_INT64:
status = Int64Converter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_INT32:
status = Int32Converter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_UINT64:
status = UInt64Converter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_COMPLEX128:
status = Complex128Converter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_STRING:
status = StringConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_BOOL:
status = BoolConverter::Convert(ctx, obj, &state, &handle, &error);
break;
default:
break;
}
if (status.ok()) return handle;
switch (state.inferred_dtype) {
case DT_FLOAT:
// TODO(josh11b): Handle mixed floats and complex numbers?
if (requested_dtype == DT_INVALID) {
// TensorFlow uses float32s to represent floating point numbers
// by default (for space and speed over using doubles).
status = FloatConverter::Convert(ctx, obj, &state, &handle, &error);
} else {
// We are going to do a cast to the user's requested dtype
// after this. We use doubles for this intermediate result so
// we don't lose precision that might be representable in the
// final type.
status = DoubleConverter::Convert(ctx, obj, &state, &handle, &error);
}
break;
case DT_DOUBLE:
status = DoubleConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_HALF:
status = NumpyHalfConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_INT64:
if (requested_dtype == DT_INVALID) {
status = Int32Converter::Convert(ctx, obj, &state, &handle, &error);
if (error == ErrorFoundInt64) {
status = Int64Converter::Convert(ctx, obj, &state, &handle, &error);
}
if (error == ErrorFoundFloat) {
status = FloatConverter::Convert(ctx, obj, &state, &handle, &error);
}
// TODO(josh11b): May also want to fall back to using doubles if
// error == ErrorOutOfRange?
} else {
status = Int64Converter::Convert(ctx, obj, &state, &handle, &error);
if (error == ErrorFoundFloat) {
status = DoubleConverter::Convert(ctx, obj, &state, &handle, &error);
}
}
break;
case DT_STRING:
status = StringConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_COMPLEX128:
status = Complex128Converter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_BOOL:
status = BoolConverter::Convert(ctx, obj, &state, &handle, &error);
break;
case DT_INVALID: // Only occurs for empty tensors.
{
Tensor tensor(requested_dtype == DT_INVALID ? DT_FLOAT : requested_dtype,
TensorShape(state.inferred_shape));
TensorInterface t(std::move(tensor));
return tensorflow::wrap(tensorflow::unwrap(ctx)->CreateLocalHandle(&t));
}
default:
break;
}
if (!status.ok()) {
PyErr_SetString(PyExc_ValueError, status.error_message().c_str());
return nullptr;
}
return handle;
}
} // namespace tensorflow