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

[wasm] Add Sinh kernel #7671

Merged
merged 26 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"**/bazel-out/**/*": true,
"**/bazel-genfiles/**/*": true,
"**/bazel-testlogs/**/*": true,
"**/bazel-tfjs*/**/*": true,
"**/bazel-tfjs*/**/*": true
},
"files.trimTrailingWhitespace": true,
"editor.tabSize": 2,
Expand Down
10 changes: 10 additions & 0 deletions tfjs-backend-wasm/src/cc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ tfjs_cc_library(
":Sigmoid",
":Sign",
":Sin",
":Sinh",
":Softmax",
":Softplus",
":SparseFillEmptyRows",
Expand Down Expand Up @@ -1524,6 +1525,15 @@ tfjs_cc_library(
],
)

tfjs_cc_library(
name = "Sinh",
srcs = ["kernels/Sinh.cc"],
deps = [
":unary",
":util",
],
)

tfjs_cc_library(
name = "Softmax",
srcs = ["kernels/Softmax.cc"],
Expand Down
58 changes: 58 additions & 0 deletions tfjs-backend-wasm/src/cc/kernels/Sinh.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* 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.
* ===========================================================================*/
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

#include <cmath>

#include "tfjs-backend-wasm/src/cc/unary.h"
#include "tfjs-backend-wasm/src/cc/util.h"

namespace tfjs {
namespace wasm {

namespace {

template <typename T>
inline T SinhImpl(T x) {
return static_cast<T>(std::sinhf(static_cast<float>(x)));
}

} // namespace

// We use C-style API to interface with Javascript.
extern "C" {

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif

void Sinh(const int x_id, const DType dtype, const int out_id) {
switch (dtype) {
case DType::float32:
unary_f32(x_id, out_id, SinhImpl<float>);
break;
case DType::int32:
unary_i32(x_id, out_id, SinhImpl<int32_t>);
break;
default:
util::warn("Sinh for tensor id %d failed. Unsupported dtype %d", x_id,
dtype);
}
}

} // extern "C"
} // namespace wasm
} // namespace tfjs
21 changes: 21 additions & 0 deletions tfjs-backend-wasm/src/kernels/Sinh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @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 {KernelConfig, Sinh} from '@tensorflow/tfjs-core';

import {createUnaryKernelConfig} from './unary_kernel';

export const sinhConfig: KernelConfig = createUnaryKernelConfig(Sinh);
2 changes: 2 additions & 0 deletions tfjs-backend-wasm/src/register_all_kernels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import {seluConfig} from './kernels/Selu';
import {sigmoidConfig} from './kernels/Sigmoid';
import {signConfig} from './kernels/Sign';
import {sinConfig} from './kernels/Sin';
import {sinhConfig} from './kernels/Sinh';
import {sliceConfig} from './kernels/Slice';
import {softmaxConfig} from './kernels/Softmax';
import {softplusConfig} from './kernels/Softplus';
Expand Down Expand Up @@ -294,6 +295,7 @@ const kernelConfigs: KernelConfig[] = [
sigmoidConfig,
signConfig,
sinConfig,
sinhConfig,
sliceConfig,
softmaxConfig,
softplusConfig,
Expand Down
1 change: 1 addition & 0 deletions tfjs-backend-wasm/src/setup_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ const TEST_FILTERS: TestFilter[] = [
]
},
{startsWith: 'sin '},
{startsWith: 'sinh '},
{
startsWith: 'cos ',
excludes: [
Expand Down