1#cython: boundscheck=False 2#cython: wraparound=False 3#cython: infer_types=True 4import numpy as np 5cimport numpy as np 6 7from tensorflow.python.util import compat 8 9def AppendBFloat16ArrayToTensorProto( 10 tensor_proto, np.ndarray[np.uint16_t, ndim=1] nparray): 11 cdef long i, n 12 n = nparray.size 13 for i in range(n): 14 tensor_proto.half_val.append(nparray[i]) 15 16 17def AppendFloat16ArrayToTensorProto( 18 # For numpy, npy_half is a typedef for npy_uint16, 19 # see: https://github.com/numpy/numpy/blob/master/doc/source/reference/c-api.coremath.rst#half-precision-functions 20 # Because np.float16_t dosen't exist in cython, we use uint16_t here. 21 # TODO: Use np.float16_t when cython supports it. 22 tensor_proto, np.ndarray[np.uint16_t, ndim=1] nparray): 23 cdef long i, n 24 n = nparray.size 25 for i in range(n): 26 tensor_proto.half_val.append(nparray[i]) 27 28 29def AppendFloat32ArrayToTensorProto( 30 tensor_proto, np.ndarray[np.float32_t, ndim=1] nparray): 31 cdef long i, n 32 n = nparray.size 33 for i in range(n): 34 tensor_proto.float_val.append(nparray[i]) 35 36 37def AppendFloat64ArrayToTensorProto( 38 tensor_proto, np.ndarray[np.float64_t, ndim=1] nparray): 39 cdef long i, n 40 n = nparray.size 41 for i in range(n): 42 tensor_proto.double_val.append(nparray[i]) 43 44 45def AppendInt32ArrayToTensorProto( 46 tensor_proto, np.ndarray[np.int32_t, ndim=1] nparray): 47 cdef long i, n 48 n = nparray.size 49 for i in range(n): 50 tensor_proto.int_val.append(nparray[i]) 51 52def AppendUInt32ArrayToTensorProto( 53 tensor_proto, np.ndarray[np.uint32_t, ndim=1] nparray): 54 cdef long i, n 55 n = nparray.size 56 for i in range(n): 57 tensor_proto.uint32_val.append(nparray[i]) 58 59def AppendInt64ArrayToTensorProto( 60 tensor_proto, np.ndarray[np.int64_t, ndim=1] nparray): 61 cdef long i, n 62 n = nparray.size 63 for i in range(n): 64 tensor_proto.int64_val.append(nparray[i]) 65 66def AppendUInt64ArrayToTensorProto( 67 tensor_proto, np.ndarray[np.uint64_t, ndim=1] nparray): 68 cdef long i, n 69 n = nparray.size 70 for i in range(n): 71 tensor_proto.uint64_val.append(nparray[i]) 72 73def AppendUInt8ArrayToTensorProto( 74 tensor_proto, np.ndarray[np.uint8_t, ndim=1] nparray): 75 cdef long i, n 76 n = nparray.size 77 for i in range(n): 78 tensor_proto.int_val.append(nparray[i]) 79 80 81def AppendUInt16ArrayToTensorProto( 82 tensor_proto, np.ndarray[np.uint16_t, ndim=1] nparray): 83 cdef long i, n 84 n = nparray.size 85 for i in range(n): 86 tensor_proto.int_val.append(nparray[i]) 87 88 89def AppendInt16ArrayToTensorProto( 90 tensor_proto, np.ndarray[np.int16_t, ndim=1] nparray): 91 cdef long i, n 92 n = nparray.size 93 for i in range(n): 94 tensor_proto.int_val.append(nparray[i]) 95 96 97def AppendInt8ArrayToTensorProto( 98 tensor_proto, np.ndarray[np.int8_t, ndim=1] nparray): 99 cdef long i, n 100 n = nparray.size 101 for i in range(n): 102 tensor_proto.int_val.append(nparray[i]) 103 104 105def AppendComplex64ArrayToTensorProto( 106 tensor_proto, np.ndarray[np.complex64_t, ndim=1] nparray): 107 cdef long i, n 108 n = nparray.size 109 for i in range(n): 110 tensor_proto.scomplex_val.append(nparray[i].real) 111 tensor_proto.scomplex_val.append(nparray[i].imag) 112 113 114def AppendComplex128ArrayToTensorProto( 115 tensor_proto, np.ndarray[np.complex128_t, ndim=1] nparray): 116 cdef long i, n 117 n = nparray.size 118 for i in range(n): 119 tensor_proto.dcomplex_val.append(nparray[i].real) 120 tensor_proto.dcomplex_val.append(nparray[i].imag) 121 122 123def AppendObjectArrayToTensorProto(tensor_proto, np.ndarray nparray): 124 cdef long i, n 125 n = nparray.size 126 for i in range(n): 127 tensor_proto.string_val.append(compat.as_bytes(nparray[i])) 128 129 130def AppendBoolArrayToTensorProto(tensor_proto, nparray): 131 cdef long i, n 132 n = nparray.size 133 for i in range(n): 134 tensor_proto.bool_val.append(nparray.item(i)) 135