-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathbinary_utils.ts
194 lines (154 loc) · 7.33 KB
/
binary_utils.ts
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
/**
* @license
* Copyright 2020 Google LLC. 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.
* =============================================================================
*/
import {backend_util, BinaryInputs, DataType, KernelFunc, TypedArray, util} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';
import {assertNotComplex} from '../cpu_util';
import {cast} from '../kernels/Cast';
import {complex} from '../kernels/Complex';
import {ComplexBinaryKernelImpl, ComplexBinaryOperation, SimpleBinaryKernelImpl} from './binary_types';
/**
* Template that creates a `KernelFunc` for binary ops.
* @param name Kernel name.
* @param binaryKernelImpl A `SimpleBinaryKernelImpl` for the kernel.
* @param binaryKernelComplexImpl Optional. If exists, represents a
* `ComplexBinaryKernelImpl` for the kernel, will be used when input dtype
* is `complex64`.
* @param dtype Optional. If set, the result has this dtype. Otherwise, the
* result has the same dtype as the first input. This is mainly used in
* comparison kernels, such as Equal, Less, Greater, etc.
*/
export function binaryKernelFunc(
name: string, simpleImpl: SimpleBinaryKernelImpl,
complexImpl?: ComplexBinaryKernelImpl, dtype?: DataType): KernelFunc {
if (complexImpl == null) {
return ({inputs, backend}) => {
const {a, b} = inputs as BinaryInputs;
const cpuBackend = backend as MathBackendCPU;
assertNotComplex([a, b], name);
const aVals = cpuBackend.data.get(a.dataId).values as TypedArray;
const bVals = cpuBackend.data.get(b.dataId).values as TypedArray;
const decodedAVals = a.dtype === 'string' ?
// tslint:disable-next-line: no-any
backend_util.fromUint8ToStringArray(aVals as any as Uint8Array[]) :
aVals;
const decodedBVals = a.dtype === 'string' ?
// tslint:disable-next-line: no-any
backend_util.fromUint8ToStringArray(bVals as any as Uint8Array[]) :
bVals;
const $dtype = dtype || a.dtype;
const [resultData, resultShape] =
simpleImpl(a.shape, b.shape, decodedAVals, decodedBVals, $dtype);
return cpuBackend.makeTensorInfo(resultShape, $dtype, resultData);
};
}
return ({inputs, backend}) => {
const {a, b} = inputs as BinaryInputs;
const cpuBackend = backend as MathBackendCPU;
if (a.dtype === 'complex64' || b.dtype === 'complex64') {
const $aComplex = cast(
{inputs: {x: a}, backend: cpuBackend, attrs: {dtype: 'complex64'}});
const $aComplexVals = cpuBackend.data.get($aComplex.dataId);
const aReal = $aComplexVals.complexTensorInfos.real;
const aImag = $aComplexVals.complexTensorInfos.imag;
const aRealVals =
cpuBackend.data.get(aReal.dataId).values as Float32Array;
const aImagVals =
cpuBackend.data.get(aImag.dataId).values as Float32Array;
const $bComplex = cast(
{inputs: {x: b}, backend: cpuBackend, attrs: {dtype: 'complex64'}});
const $bComplexVals = cpuBackend.data.get($bComplex.dataId);
const bReal = $bComplexVals.complexTensorInfos.real;
const bImag = $bComplexVals.complexTensorInfos.imag;
const bRealVals =
cpuBackend.data.get(bReal.dataId).values as Float32Array;
const bImagVals =
cpuBackend.data.get(bImag.dataId).values as Float32Array;
const [resultRealData, resultImagData, resultShape] = complexImpl(
a.shape, b.shape, aRealVals, aImagVals, bRealVals, bImagVals);
const resultReal =
cpuBackend.makeTensorInfo(resultShape, 'float32', resultRealData);
const resultImag =
cpuBackend.makeTensorInfo(resultShape, 'float32', resultImagData);
const result = complex(
{inputs: {real: resultReal, imag: resultImag}, backend: cpuBackend});
cpuBackend.disposeIntermediateTensorInfo($aComplex);
cpuBackend.disposeIntermediateTensorInfo($bComplex);
cpuBackend.disposeIntermediateTensorInfo(resultReal);
cpuBackend.disposeIntermediateTensorInfo(resultImag);
return result;
} else {
const aVals = cpuBackend.data.get(a.dataId).values as TypedArray;
const bVals = cpuBackend.data.get(b.dataId).values as TypedArray;
const $dtype = dtype || a.dtype;
const [resultData, resultShape] =
simpleImpl(a.shape, b.shape, aVals, bVals, $dtype);
return cpuBackend.makeTensorInfo(resultShape, $dtype, resultData);
}
};
}
/**
* Template that creates the complex type implementation for binary ops.
* Supports broadcast.
*/
export function createComplexBinaryKernelImpl(op: ComplexBinaryOperation):
ComplexBinaryKernelImpl {
return (aShape: number[], bShape: number[], aRealVals: Float32Array,
aImagVals: Float32Array, bRealVals: Float32Array,
bImagVals: Float32Array): [TypedArray, TypedArray, number[]] => {
const resultShape = backend_util.assertAndGetBroadcastShape(aShape, bShape);
const resultSize = util.sizeFromShape(resultShape);
const resultRank = resultShape.length;
const resultStrides = util.computeStrides(resultShape);
const resultRealVals = util.getTypedArrayFromDType('float32', resultSize);
const resultImagVals = util.getTypedArrayFromDType('float32', resultSize);
const aBroadcastDims = backend_util.getBroadcastDims(aShape, resultShape);
const bBroadcastDims = backend_util.getBroadcastDims(bShape, resultShape);
const aVals = backend_util.mergeRealAndImagArrays(aRealVals, aImagVals);
const bVals = backend_util.mergeRealAndImagArrays(bRealVals, bImagVals);
const aRank = aShape.length;
const aStrides = util.computeStrides(aShape);
const bRank = bShape.length;
const bStrides = util.computeStrides(bShape);
if (aBroadcastDims.length + bBroadcastDims.length === 0) {
for (let i = 0; i < resultRealVals.length; i++) {
const aIdx = i % aVals.length;
const bIdx = i % bVals.length;
const result =
op(aVals[aIdx * 2], aVals[aIdx * 2 + 1], bVals[bIdx * 2],
bVals[bIdx * 2 + 1]);
resultRealVals[i] = result.real;
resultImagVals[i] = result.imag;
}
} else {
for (let i = 0; i < resultRealVals.length; i++) {
const loc = util.indexToLoc(i, resultRank, resultStrides);
const aLoc = loc.slice(-aRank);
aBroadcastDims.forEach(d => aLoc[d] = 0);
const aIndex = util.locToIndex(aLoc, aRank, aStrides);
const bLoc = loc.slice(-bRank);
bBroadcastDims.forEach(d => bLoc[d] = 0);
const bIndex = util.locToIndex(bLoc, bRank, bStrides);
const opResult =
op(aVals[aIndex * 2], aVals[aIndex * 2 + 1], bVals[bIndex * 2],
bVals[bIndex * 2 + 1]);
resultRealVals[i] = opResult.real;
resultImagVals[i] = opResult.imag;
}
}
return [resultRealVals, resultImagVals, resultShape];
};
}