forked from MichaelMULLER/tfjs-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[webgpu] Concat kernel + logical input samplers. (tensorflow#1740)
FEATURE
- Loading branch information
1 parent
a6acfce
commit dc8836a
Showing
8 changed files
with
227 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 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 * as tf from '@tensorflow/tfjs-core'; | ||
|
||
import * as tfwebgpu from './index'; | ||
|
||
describe('concat', () => { | ||
beforeAll(async () => tfwebgpu.ready); | ||
|
||
it('3 + 5', async () => { | ||
const a = tf.tensor1d([3]); | ||
const b = tf.tensor1d([5]); | ||
|
||
const result = tf.concat1d([a, b]); | ||
const expected = [3, 5]; | ||
const resultData = await result.data(); | ||
tf.test_util.expectArraysClose(resultData, new Float32Array(expected)); | ||
}); | ||
|
||
it('[[3]] + [[5]], axis=0', async () => { | ||
const axis = 0; | ||
const a = tf.tensor2d([3], [1, 1]); | ||
const b = tf.tensor2d([5], [1, 1]); | ||
|
||
const result = tf.concat2d([a, b], axis); | ||
const expected = [3, 5]; | ||
|
||
expect(result.shape).toEqual([2, 1]); | ||
const resultData = await result.data(); | ||
tf.test_util.expectArraysClose(resultData, new Float32Array(expected)); | ||
}); | ||
|
||
it('[[1, 2],[3, 4]] + [[5, 6],[7, 8]] + [[9, 10],[11, 12]], axis=1', | ||
async () => { | ||
const axis = 1; | ||
const a = tf.tensor2d([[1, 2], [3, 4]]); | ||
const b = tf.tensor2d([[5, 6], [7, 8]]); | ||
const c = tf.tensor2d([[9, 10], [11, 12]]); | ||
|
||
const result = tf.concat2d([a, b, c], axis); | ||
const expected = [1, 2, 5, 6, 9, 10, 3, 4, 7, 8, 11, 12]; | ||
|
||
expect(result.shape).toEqual([2, 6]); | ||
const resultData = await result.data(); | ||
tf.test_util.expectArraysClose(resultData, new Float32Array(expected)); | ||
}); | ||
|
||
it('concat axis=2', async () => { | ||
const tensor1 = tf.tensor3d([1, 11, 2, 22, 3, 33, 4, 44], [2, 2, 2]); | ||
const tensor2 = tf.tensor3d( | ||
[5, 55, 555, 6, 66, 666, 7, 77, 777, 8, 88, 888], [2, 2, 3]); | ||
const values = tf.concat3d([tensor1, tensor2], 2); | ||
expect(values.shape).toEqual([2, 2, 5]); | ||
const valuesData = await values.data(); | ||
tf.test_util.expectArraysClose(valuesData, new Float32Array([ | ||
1, 11, 5, 55, 555, 2, 22, 6, 66, 666, | ||
3, 33, 7, 77, 777, 4, 44, 8, 88, 888 | ||
])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 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 * as concat_util from '@tensorflow/tfjs-core/dist/ops/concat_util'; | ||
import {computeDispatch} from '../webgpu_util'; | ||
import {WebGPUProgram} from './webgpu_program'; | ||
|
||
export class ConcatProgram implements WebGPUProgram { | ||
outputShape: number[]; | ||
userCode: string; | ||
dispatchLayout: {x: number[], y: number[]}; | ||
dispatch: [number, number, number]; | ||
variableNames: string[]; | ||
|
||
constructor(shapes: Array<[number, number]>) { | ||
this.outputShape = | ||
concat_util.computeOutShape(shapes, 1 /* axis */) as [number, number]; | ||
this.variableNames = shapes.map((_, i) => `T${i}`); | ||
|
||
this.dispatchLayout = {x: [0], y: [1]}; | ||
this.dispatch = computeDispatch(this.dispatchLayout, this.outputShape); | ||
|
||
const offsets: number[] = new Array(shapes.length - 1); | ||
offsets[0] = shapes[0][1]; | ||
for (let i = 1; i < offsets.length; i++) { | ||
offsets[i] = offsets[i - 1] + shapes[i][1]; | ||
} | ||
|
||
const snippets = [ | ||
`if (yC < ${offsets[0]}) setOutput(coords.x, coords.y, getT0(yR, yC));` | ||
]; | ||
|
||
for (let i = 1; i < offsets.length; i++) { | ||
const shift = offsets[i - 1]; | ||
snippets.push( | ||
`else if (yC < ${offsets[i]}) ` + | ||
`setOutput(coords.x, coords.y, getT${i}(yR, yC-${shift}));`); | ||
} | ||
const lastIndex = offsets.length; | ||
const lastShift = offsets[offsets.length - 1]; | ||
snippets.push(`else setOutput(coords.x, coords.y, getT${lastIndex}(yR, yC-${ | ||
lastShift}));`); | ||
|
||
this.userCode = ` | ||
void main() { | ||
ivec2 coords = getOutputCoords(); | ||
int yR = coords.x; | ||
int yC = coords.y; | ||
${snippets.join('\n ')} | ||
} | ||
`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.