diff --git a/.vscode/settings.json b/.vscode/settings.json index 00f147af1f3..4ad1d9a65b1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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, diff --git a/tfjs-backend-wasm/src/cc/BUILD.bazel b/tfjs-backend-wasm/src/cc/BUILD.bazel index 24567f34cc5..347a3934fa4 100644 --- a/tfjs-backend-wasm/src/cc/BUILD.bazel +++ b/tfjs-backend-wasm/src/cc/BUILD.bazel @@ -436,6 +436,7 @@ tfjs_cc_library( ":Sigmoid", ":Sign", ":Sin", + ":Sinh", ":Softmax", ":Softplus", ":SparseFillEmptyRows", @@ -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"], diff --git a/tfjs-backend-wasm/src/cc/kernels/Sinh.cc b/tfjs-backend-wasm/src/cc/kernels/Sinh.cc new file mode 100644 index 00000000000..dff6d07faad --- /dev/null +++ b/tfjs-backend-wasm/src/cc/kernels/Sinh.cc @@ -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 +#endif + +#include + +#include "tfjs-backend-wasm/src/cc/unary.h" +#include "tfjs-backend-wasm/src/cc/util.h" + +namespace tfjs { +namespace wasm { + +namespace { + +template +inline T SinhImpl(T x) { + return static_cast(std::sinhf(static_cast(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); + break; + case DType::int32: + unary_i32(x_id, out_id, SinhImpl); + break; + default: + util::warn("Sinh for tensor id %d failed. Unsupported dtype %d", x_id, + dtype); + } +} + +} // extern "C" +} // namespace wasm +} // namespace tfjs diff --git a/tfjs-backend-wasm/src/kernels/Sinh.ts b/tfjs-backend-wasm/src/kernels/Sinh.ts new file mode 100644 index 00000000000..1506965e3ea --- /dev/null +++ b/tfjs-backend-wasm/src/kernels/Sinh.ts @@ -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); diff --git a/tfjs-backend-wasm/src/register_all_kernels.ts b/tfjs-backend-wasm/src/register_all_kernels.ts index 80ab27351b3..303f315d182 100644 --- a/tfjs-backend-wasm/src/register_all_kernels.ts +++ b/tfjs-backend-wasm/src/register_all_kernels.ts @@ -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'; @@ -294,6 +295,7 @@ const kernelConfigs: KernelConfig[] = [ sigmoidConfig, signConfig, sinConfig, + sinhConfig, sliceConfig, softmaxConfig, softplusConfig, diff --git a/tfjs-backend-wasm/src/setup_test.ts b/tfjs-backend-wasm/src/setup_test.ts index 7f29d7acb87..4fdcac5decd 100644 --- a/tfjs-backend-wasm/src/setup_test.ts +++ b/tfjs-backend-wasm/src/setup_test.ts @@ -298,6 +298,7 @@ const TEST_FILTERS: TestFilter[] = [ ] }, {startsWith: 'sin '}, + {startsWith: 'sinh '}, { startsWith: 'cos ', excludes: [