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 expm1 #7467

Merged
merged 2 commits into from
Mar 13, 2023
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
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 @@ -851,6 +851,16 @@ tfjs_cc_library(
],
)

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

tfjs_cc_library(
name = "FlipLeftRight",
srcs = ["kernels/FlipLeftRight.cc"],
Expand Down
57 changes: 57 additions & 0 deletions tfjs-backend-wasm/src/cc/kernels/Expm1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* 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/backend.h"
#include "tfjs-backend-wasm/src/cc/unary.h"
#include "tfjs-backend-wasm/src/cc/util.h"

namespace {

template <typename T>
inline float expm1_impl(T n) {
return std::expm1f(static_cast<float>(n));
}

} // namespace

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

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void Expm1(const int x_id, const DType dtype, const int out_id) {
switch (dtype) {
case DType::float32:
unary_f32(x_id, out_id, expm1_impl<float>);
break;
case DType::int32:
unary_i32_with_f32_out(x_id, out_id, expm1_impl<int>);
break;
default:
util::warn("Expm1 for tensor id %d failed. Unsupported dtype %d", x_id,
dtype);
}
}

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

import {createUnaryKernelConfig} from './unary_kernel';

export const expm1Config: KernelConfig =
createUnaryKernelConfig(Expm1, 'float32');
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 @@ -63,6 +63,7 @@ import {eluGradConfig} from './kernels/EluGrad';
import {equalConfig} from './kernels/Equal';
import {expConfig} from './kernels/Exp';
import {expandDimsConfig} from './kernels/ExpandDims';
import {expm1Config} from './kernels/Expm1';
import {fillConfig} from './kernels/Fill';
import {flipLeftRightConfig} from './kernels/FlipLeftRight';
import {floorConfig} from './kernels/Floor';
Expand Down Expand Up @@ -209,6 +210,7 @@ const kernelConfigs: KernelConfig[] = [
equalConfig,
expConfig,
expandDimsConfig,
expm1Config,
fillConfig,
flipLeftRightConfig,
floorConfig,
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 @@ -407,6 +407,7 @@ const TEST_FILTERS: TestFilter[] = [
{include: 'softplus '},
{include: 'linspace'},
{include: 'bincount'},
{include: 'expm1 '},
];

const customInclude = (testName: string) => {
Expand Down