Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Use customGrad in exposed GPGPUProgram tests #1204

Merged
merged 13 commits into from
Aug 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/kernels/webgl/webgl_custom_op_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,18 @@ describeWithFlags('custom-op webgl', WEBGL_ENVS, () => {
}

function squareAndAdd<T extends tf.Tensor>(x: T): T {
const webglBackend = tf.ENV.backend as tf.webgl.MathBackendWebGL;
const program = new SquareAndAddKernel(x.shape);
const backpropProgram = new SquareAndAddBackpropKernel(x.shape);
const fn = tf.customGrad(x => {
const webglBackend = tf.ENV.backend as tf.webgl.MathBackendWebGL;
const program = new SquareAndAddKernel(x.shape);
const backpropProgram = new SquareAndAddBackpropKernel(x.shape);

const forward = () => webglBackend.compileAndRun(program, [x]);
const value = webglBackend.compileAndRun(program, [x]);

const backward = (dy: T) => {
return {
x: () => webglBackend.compileAndRun(backpropProgram, [x]).mul(dy) as T
};
};

const res = tf.ENV.engine.runKernel(forward, {x}, backward);
return res as T;
const gradFunc = (dy: T) =>
webglBackend.compileAndRun(backpropProgram, [x]).mul(dy) as T;
return {value, gradFunc};
});
return fn(x) as T;
}

it('lets users use custom operations', () => {
Expand Down