forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryop_packed_gpu.ts
125 lines (114 loc) · 4.08 KB
/
binaryop_packed_gpu.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
/**
* @license
* Copyright 2018 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, util} from '@tensorflow/tfjs-core';
import {GPGPUProgram, useShapeUniforms} from './gpgpu_math';
import {getChannels} from './packing_util';
import {getCoordsDataType} from './shader_compiler';
export const CHECK_NAN_SNIPPET = `
result.r = isNaN.r > 0. ? NAN : result.r;
result.g = isNaN.g > 0. ? NAN : result.g;
result.b = isNaN.b > 0. ? NAN : result.b;
result.a = isNaN.a > 0. ? NAN : result.a;
`;
export const ELU_DER = `
vec4 bGTEZero = vec4(greaterThanEqual(b, vec4(0.)));
return (bGTEZero * a) + ((vec4(1.0) - bGTEZero) * (a * (b + vec4(1.0))));
`;
export const NOT_EQUAL = `
return vec4(notEqual(a, b));
`;
export class BinaryOpPackedProgram implements GPGPUProgram {
variableNames = ['A', 'B'];
outputShape: number[];
userCode: string;
supportsBroadcasting = true;
packedInputs = true;
packedOutput = true;
enableShapeUniforms: boolean;
constructor(
op: string, aShape: number[], bShape: number[],
checkOutOfBounds = false) {
this.outputShape = backend_util.assertAndGetBroadcastShape(aShape, bShape);
const rank = this.outputShape.length;
this.enableShapeUniforms = useShapeUniforms(rank);
let checkOutOfBoundsString = '';
if (checkOutOfBounds) {
if (rank === 0 || util.sizeFromShape(this.outputShape) === 1) {
checkOutOfBoundsString = `
result.y = 0.;
result.z = 0.;
result.w = 0.;
`;
} else {
const dtype = getCoordsDataType(rank);
checkOutOfBoundsString = `
${dtype} coords = getOutputCoords();
`;
if (rank === 1) {
if (this.enableShapeUniforms) {
checkOutOfBoundsString += `
result.y = (coords + 1) >= outShape ? 0. : result.y;
result.z = 0.;
result.w = 0.;
`;
} else {
checkOutOfBoundsString += `
result.y = (coords + 1) >= ${this.outputShape[0]} ? 0. : result.y;
result.z = 0.;
result.w = 0.;
`;
}
} else {
const channels = getChannels('coords', rank);
if (this.enableShapeUniforms) {
checkOutOfBoundsString += `
bool nextRowOutOfBounds =
(${channels[rank - 2]} + 1) >= outShape[${rank} - 2];
bool nextColOutOfBounds =
(${channels[rank - 1]} + 1) >= outShape[${rank} - 1];
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`;
} else {
checkOutOfBoundsString += `
bool nextRowOutOfBounds =
(${channels[rank - 2]} + 1) >= ${this.outputShape[rank - 2]};
bool nextColOutOfBounds =
(${channels[rank - 1]} + 1) >= ${this.outputShape[rank - 1]};
result.y = nextColOutOfBounds ? 0. : result.y;
result.z = nextRowOutOfBounds ? 0. : result.z;
result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
`;
}
}
}
}
this.userCode = `
vec4 binaryOperation(vec4 a, vec4 b) {
${op}
}
void main() {
vec4 a = getAAtOutCoords();
vec4 b = getBAtOutCoords();
vec4 result = binaryOperation(a, b);
${checkOutOfBoundsString}
setOutput(result);
}
`;
}
}