Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js/webgpu] Fix Tanh explosion #19201

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion js/web/lib/wasm/jsep/webgpu/ops/unary-op.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ export const tan = (context: ComputeContext): void => {
};

export const tanh = (context: ComputeContext): void => {
guschmue marked this conversation as resolved.
Show resolved Hide resolved
context.compute(createElementwiseProgramInfo(context.inputs[0], 'Tanh', 'tanh'));
context.compute(createElementwiseProgramInfo(
context.inputs[0], 'Tanh', a => `sign(${a}) * (1 - exp(-2 * abs(${a}))) / (1 + exp(-2 * abs(${a})))`));
guschmue marked this conversation as resolved.
Show resolved Hide resolved
};

export const thresholdedRelu = (context: ComputeContext, attributes: AlphaAttributes): number => {
Expand Down
26 changes: 26 additions & 0 deletions js/web/test/data/ops/tanh.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"name": "tanh with no attributes",
"operator": "Tanh",
"attributes": [],
"cases": [
{
"name": "T[2,4]",
"inputs": [
{
"data": [-1000, -1, 0, 0.1, 0.2, 0.3, 0.4, 1000],
"dims": [2, 4],
"type": "float32"
}
],
"outputs": [
{
"data": [-1, -0.761594, 0, 0.099668, 0.197375, 0.291313, 0.379949, 1],
"dims": [2, 4],
"type": "float32"
}
]
}
]
}
]
1 change: 1 addition & 0 deletions js/web/test/suite-test-list.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@
"sub.jsonc",
"sub_int32.jsonc",
"tan.jsonc",
"tanh.jsonc",
"tile.jsonc",
"transpose.jsonc",
"transpose_int32_uint32.jsonc",
Expand Down
Loading