Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions tfjs-backend-webgpu/src/binary_op_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum BinaryOpType {
COMPLEX_MULTIPLY_IMAG,
COMPLEX_MULTIPLY_REAL,
DIV,
ELU_DER,
EQUAL,
GREATER,
GREATER_EQUAL,
Expand Down Expand Up @@ -59,6 +60,9 @@ const ADD = 'return a + b;';
const COMPLEX_MULTIPLY_REAL = 'return areal * breal - aimag * bimag;';
const COMPLEX_MULTIPLY_IMAG = 'return areal * bimag + aimag * breal;';
const DIV = 'return a / b;';
const ELU_DER = 'return select(a * (b + 1.0), a, b >= 0.);';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see webgl is using b >= 1.0 https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-webgl/src/kernels/EluGrad.ts#L24 . However, you are using b >= 0.0. Which one is correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On webgl backend, the non PACKED and PACKED versions are different,

const ELU_DER_PACKED = `

I think the PACKED version is correct, but is not 100% sure.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to http://bytedeco.org/javacpp-presets/tensorflow/apidocs/org/bytedeco/tensorflow/EluGrad.html, b >= 0. is correct.
Please also fix the WebGL unpacked version, and I think we also need some test cases to cover.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed them and added case in #7251

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please hold this a bit until #7251 is reviewed.

const ELU_DER_VEC4 =
'return select(a * (b + vec4<f32>(1.0)), a, b >= vec4<f32>(0.));';
const EQUAL = 'return f32(a == b);';
const EQUAL_VEC4 = 'return vec4<f32>(a == b);';
const GREATER = 'return f32(a > b);';
Expand Down Expand Up @@ -223,6 +227,8 @@ export function getBinaryOpString(
return COMPLEX_MULTIPLY_REAL;
case BinaryOpType.DIV:
return DIV;
case BinaryOpType.ELU_DER:
return useVec4 ? ELU_DER_VEC4 : ELU_DER;
case BinaryOpType.EQUAL:
return useVec4 ? EQUAL_VEC4 : EQUAL;
case BinaryOpType.GREATER:
Expand Down
38 changes: 38 additions & 0 deletions tfjs-backend-webgpu/src/kernels/EluGrad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @license
* Copyright 2023 Google LLC.
* 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 {EluGrad, EluGradInputs, KernelConfig, KernelFunc, TensorInfo} from '@tensorflow/tfjs-core';

import {WebGPUBackend} from '../backend_webgpu';
import {BinaryOpType} from '../binary_op_util';
import {BinaryOpProgram} from '../binary_op_webgpu';

export const eluGrad =
(args: {inputs: EluGradInputs, backend: WebGPUBackend}): TensorInfo => {
const {inputs, backend} = args;
const {dy, y} = inputs;

const program =
new BinaryOpProgram(BinaryOpType.ELU_DER, dy.shape, y.shape);
return backend.runWebGPUProgram(program, [dy, y], dy.dtype);
};

export const eluGradConfig: KernelConfig = {
kernelName: EluGrad,
backendName: 'webgpu',
kernelFunc: eluGrad as unknown as KernelFunc
};
2 changes: 2 additions & 0 deletions tfjs-backend-webgpu/src/register_all_kernels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {diagConfig} from './kernels/Diag';
import {dilation2DConfig} from './kernels/Dilation2D';
import {einsumConfig} from './kernels/Einsum';
import {eluConfig} from './kernels/Elu';
import {eluGradConfig} from './kernels/EluGrad';
import {equalConfig} from './kernels/Equal';
import {erfConfig} from './kernels/Erf';
import {expConfig} from './kernels/Exp';
Expand Down Expand Up @@ -201,6 +202,7 @@ const kernelConfigs: KernelConfig[] = [
dilation2DConfig,
einsumConfig,
eluConfig,
eluGradConfig,
equalConfig,
erfConfig,
expConfig,
Expand Down
7 changes: 0 additions & 7 deletions tfjs-backend-webgpu/src/setup_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ const TEST_FILTERS: TestFilter[] = [
'gradient' // gradient function not found.
]
},
{
startsWith: 'elu ',
excludes: [
'derivative', // gradient function not found.
'gradient' // gradient function not found.
]
},
{
startsWith: 'exp ',
excludes: [
Expand Down