Skip to content

Commit 778f62a

Browse files
jgartmanannxingyuan
authored andcommitted
Packed reverse (tensorflow#1617)
PERF
1 parent a470087 commit 778f62a

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

src/kernels/backend_webgl.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import {ResizeBilinearPackedProgram} from './webgl/resize_bilinear_packed_gpu';
9696
import {ResizeNearestNeigborBackpropProgram} from './webgl/resize_nearest_neighbor_backprop_gpu';
9797
import {ResizeNearestNeighborProgram} from './webgl/resize_nearest_neighbor_gpu';
9898
import {ReverseProgram} from './webgl/reverse_gpu';
99+
import {ReversePackedProgram} from './webgl/reverse_packed_gpu';
99100
import {ScatterProgram} from './webgl/scatter_gpu';
100101
import {SegmentOpProgram} from './webgl/segment_gpu';
101102
import {SelectProgram} from './webgl/select_gpu';
@@ -713,7 +714,9 @@ export class MathBackendWebGL implements KernelBackend {
713714
}
714715

715716
reverse<T extends Tensor>(x: T, axis: number[]): T {
716-
const program = new ReverseProgram(x.shape, axis);
717+
const program = ENV.get('WEBGL_PACK_ARRAY_OPERATIONS') ?
718+
new ReversePackedProgram(x.shape, axis) :
719+
new ReverseProgram(x.shape, axis);
717720
return this.compileAndRun(program, [x]);
718721
}
719722

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google LLC All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
import {getChannels} from '../packing_util';
19+
import {GPGPUProgram} from './gpgpu_math';
20+
import {getCoordsDataType} from './shader_compiler';
21+
22+
export class ReversePackedProgram implements GPGPUProgram {
23+
variableNames = ['x'];
24+
outputShape: number[];
25+
userCode: string;
26+
usesPackedTextures = true;
27+
28+
constructor(xShape: number[], axis: number[]) {
29+
const rank = xShape.length;
30+
if (rank > 4) {
31+
throw new Error(
32+
`WebGL backend: Reverse of rank-${rank} tensor is not yet supported`);
33+
}
34+
this.outputShape = xShape;
35+
const channels = getChannels('rc', rank);
36+
const nextColumn =
37+
`${channels[rank - 1]} + 1 < ${this.outputShape[rank - 1]}`;
38+
const nextRow = `${channels[rank - 2]} + 1 < ${this.outputShape[rank - 2]}`;
39+
const type = getCoordsDataType(rank);
40+
if (rank === 1) {
41+
this.userCode = `
42+
void main(){
43+
int rc = getOutputCoords();
44+
vec4 result = vec4(0.);
45+
result.r = getChannel(getX(${xShape[0]} - rc - 1), rc);
46+
if(${nextColumn}){
47+
result.g = getChannel(getX(${xShape[0]} - (rc + 1) - 1), rc + 1);
48+
}
49+
setOutput(result);
50+
}
51+
`;
52+
} else {
53+
this.userCode = `
54+
void main() {
55+
${type} rc = getOutputCoords();
56+
vec4 result = vec4(0.);
57+
result.r = ${getR(channels.slice())};
58+
if(${nextColumn}){
59+
result.g = ${getG(channels.slice())};
60+
}
61+
if(${nextRow}) {
62+
result.b = ${getB(channels.slice())};
63+
if(${nextColumn}) {
64+
result.a = ${getA(channels.slice())};
65+
}
66+
}
67+
setOutput(result);
68+
}
69+
`;
70+
}
71+
72+
function getR(channels: string[]): string {
73+
return getChannel(channels);
74+
}
75+
76+
function getG(channels: string[]): string {
77+
channels[rank - 1] = '(' + channels[rank - 1] + ` + 1)`;
78+
return getChannel(channels);
79+
}
80+
81+
function getB(channels: string[]): string {
82+
channels[rank - 2] = '(' + channels[rank - 2] + ` + 1)`;
83+
return getChannel(channels);
84+
}
85+
86+
function getA(channels: string[]): string {
87+
channels[rank - 1] = '(' + channels[rank - 1] + ` + 1)`;
88+
channels[rank - 2] = '(' + channels[rank - 2] + ` + 1)`;
89+
return getChannel(channels);
90+
}
91+
92+
function getChannel(channels: string[]): string {
93+
const inCoordsArray = xShape.map((_, i) => getInCoord(i, channels));
94+
const inCoords = inCoordsArray.join(',');
95+
const innerDims = inCoordsArray.slice(-2).join(',');
96+
return `getChannel(getX(${inCoords}), vec2(${innerDims}))`;
97+
}
98+
99+
function getInCoord(i: number, channels1: string[]): string {
100+
if (axis.indexOf(i) !== -1 && xShape[i] !== 1) {
101+
return `${xShape[i]} - ${channels1[i]} - 1`;
102+
} else {
103+
return `${channels1[i]}`;
104+
}
105+
}
106+
}
107+
}

src/ops/reverse_test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ describeWithFlags('reverse2d', ALL_ENVS, () => {
7171
expectArraysClose(result, [3, 2, 1, 6, 5, 4]);
7272
});
7373

74+
it('reverse a 2D array odd rows and columns at axis [0, 1]', () => {
75+
const axis = [0, 1];
76+
const a = tf.tensor2d(
77+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [3, 5]);
78+
const result = tf.reverse2d(a, axis);
79+
80+
expect(result.shape).toEqual(a.shape);
81+
expectArraysClose(
82+
result, [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
83+
});
84+
7485
it('throws error with invalid input', () => {
7586
// tslint:disable-next-line:no-any
7687
const x: any = tf.tensor1d([1, 20, 300, 4]);

0 commit comments

Comments
 (0)