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
42 changes: 42 additions & 0 deletions tfjs-backend-wasm/src/cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ tfjs_cc_library(
":FusedDepthwiseConv2D",
":Greater",
":GreaterEqual",
":Less",
":LessEqual",
":Max",
":MaxPool",
":Maximum",
Expand All @@ -172,6 +174,7 @@ tfjs_cc_library(
":ResizeBilinear",
":Sigmoid",
":Sub",
":Tile",
":Transpose",
],
)
Expand Down Expand Up @@ -403,6 +406,36 @@ tfjs_cc_library(
],
)

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

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

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

tfjs_cc_library(
name = "Log",
srcs = ["kernels/Log.cc"],
Expand Down Expand Up @@ -581,6 +614,15 @@ tfjs_cc_library(
],
)

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

tfjs_cc_library(
name = "Transpose",
srcs = ["kernels/Transpose.cc"],
Expand Down
9 changes: 9 additions & 0 deletions tfjs-backend-wasm/src/cc/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ inline void compare_bool(const int a_id, const int b_id, const int out_id,
out_info.b_write(), operation);
}

inline void logical(const int a_id, const int b_id, const int out_id,
bool operation(bool, bool)) {
auto& a_info = backend::get_tensor_info(a_id);
auto& b_info = backend::get_tensor_info(b_id);
auto& out_info = backend::get_tensor_info_out(out_id);
binary_impl<bool, bool>(a_info.b(), a_info.size, b_info.b(), b_info.size,
out_info.b_write(), operation);
}

typedef xnn_status (*xnn_create_binary_op)(float, float, uint32_t,
xnn_operator_t*);
typedef xnn_status (*xnn_setup_binary_op)(xnn_operator_t, size_t, const size_t*,
Expand Down
59 changes: 59 additions & 0 deletions tfjs-backend-wasm/src/cc/kernels/Less.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright 2019 Google Inc. All Rights Reserved.
* 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 "src/cc/binary.h"
#include "src/cc/util.h"

namespace {
template <class T>
inline bool less(T a, T b) {
return a < b;
}
} // namespace

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

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void Less(const int a_id, const size_t* a_shape_ptr, const int a_shape_len,
const int b_id, const size_t* b_shape_ptr, const int b_shape_len,
const DType input_type, const int out_id) {
switch (input_type) {
case DType::float32:
compare_f32(a_id, b_id, out_id, less<float>);
break;
case DType::int32:
compare_i32(a_id, b_id, out_id, less<int>);
break;
case DType::boolean:
compare_bool(a_id, b_id, out_id, less<bool>);
break;
default:
util::warn(
"Less for tensor ids %d and %d failed. Unsupported input_type %d",
a_id, b_id, input_type);
}
}

} // extern "C"
} // namespace wasm
} // namespace tfjs
60 changes: 60 additions & 0 deletions tfjs-backend-wasm/src/cc/kernels/LessEqual.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2019 Google Inc. All Rights Reserved.
* 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 "src/cc/binary.h"
#include "src/cc/util.h"

namespace {
template <class T>
inline bool lessEqual(T a, T b) {
return a <= b;
}
} // namespace

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

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void LessEqual(const int a_id, const size_t* a_shape_ptr, const int a_shape_len,
const int b_id, const size_t* b_shape_ptr, const int b_shape_len,
const DType input_type, const int out_id) {
switch (input_type) {
case DType::float32:
compare_f32(a_id, b_id, out_id, lessEqual<float>);
break;
case DType::int32:
compare_i32(a_id, b_id, out_id, lessEqual<int>);
break;
case DType::boolean:
compare_bool(a_id, b_id, out_id, lessEqual<bool>);
break;
default:
util::warn(
"LessEqual for tensor ids %d and %d failed."
"Unsupported input_type %d",
a_id, b_id, input_type);
}
}

} // extern "C"
} // namespace wasm
} // namespace tfjs
52 changes: 52 additions & 0 deletions tfjs-backend-wasm/src/cc/kernels/LogicalAnd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright 2019 Google Inc. All Rights Reserved.
* 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 "src/cc/binary.h"
#include "src/cc/util.h"

namespace {
inline bool logical_and(bool a, bool b) { return a && b; }
} // namespace

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

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void LogicalAnd(const int a_id, const size_t* a_shape_ptr,
const int a_shape_len, const int b_id,
const size_t* b_shape_ptr, const int b_shape_len,
const DType input_type, const int out_id) {
switch (input_type) {
case DType::boolean:
compare_bool(a_id, b_id, out_id, logical_and);
break;
default:
util::warn(
"LogicalAnd for tensor ids %d and %d failed. Unsupported input_type "
"%d",
a_id, b_id, input_type);
}
}

} // extern "C"
} // namespace wasm
} // namespace tfjs
90 changes: 90 additions & 0 deletions tfjs-backend-wasm/src/cc/kernels/Tile.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* Copyright 2019 Google Inc. All Rights Reserved.
* 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 <cstddef>
#include <vector>

#include "src/cc/backend.h"
#include "src/cc/util.h"

namespace {
template <typename T>
void tile_slow(const T* x_data, const std::vector<size_t>& x_shape,
const std::vector<size_t>& new_shape, T* out_data) {
const size_t x_rank = x_shape.size();
const std::vector<size_t> x_strides = tfjs::util::compute_strides(x_shape);

const size_t out_size = tfjs::util::size_from_shape(new_shape);
const std::vector<size_t> out_strides =
tfjs::util::compute_strides(new_shape);

for (size_t i = 0; i < out_size; ++i) {
const std::vector<size_t> new_loc =
tfjs::util::offset_to_loc(i, out_strides);

std::vector<size_t> original_loc(x_rank);

for (size_t j = 0; j < original_loc.size(); ++j) {
original_loc[j] = new_loc[j] % x_shape[j];
}

const size_t original_index =
tfjs::util::loc_to_offset(original_loc, x_strides);

out_data[i] = x_data[original_index];
}
}
} // namespace

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

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void Tile(const size_t x_id, const size_t* x_shape_ptr,
const size_t x_shape_length, const size_t* new_shape_ptr,
const size_t new_shape_length, const DType dtype,
const size_t out_id) {
auto x_shape = std::vector<size_t>(x_shape_ptr, x_shape_ptr + x_shape_length);
auto new_shape =
std::vector<size_t>(new_shape_ptr, new_shape_ptr + new_shape_length);
auto& x_info = backend::get_tensor_info(x_id);
auto& out_info = backend::get_tensor_info_out(out_id);

switch (dtype) {
case DType::float32:
tile_slow<float>(x_info.f32(), x_shape, new_shape, out_info.f32_write());
break;
case DType::int32:
tile_slow<int32_t>(x_info.i32(), x_shape, new_shape,
out_info.i32_write());
break;
case DType::boolean:
tile_slow<bool>(x_info.b(), x_shape, new_shape, out_info.b_write());
break;
default:
util::warn("Tile for tensor id %d failed. Unknown dtype %d", x_id, dtype);
}
}

} // extern "C"
} // namespace wasm
} // namespace tfjs
4 changes: 2 additions & 2 deletions tfjs-backend-wasm/src/kernels/Add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
*/

import {registerBinaryKernel} from './binary_kernel';
const supportsBroadcast = true;
registerBinaryKernel('Add', supportsBroadcast);
const supportsFullBroadcast = true;
registerBinaryKernel('Add', supportsFullBroadcast);
4 changes: 2 additions & 2 deletions tfjs-backend-wasm/src/kernels/Div.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
*/

import {registerBinaryKernel} from './binary_kernel';
const supportsBroadcast = false;
registerBinaryKernel('Div', supportsBroadcast);
const supportsFullBroadcast = false;
registerBinaryKernel('Div', supportsFullBroadcast);
4 changes: 2 additions & 2 deletions tfjs-backend-wasm/src/kernels/FloorDiv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
*/

import {registerBinaryKernel} from './binary_kernel';
const supportsBroadcast = false;
registerBinaryKernel('FloorDiv', supportsBroadcast);
const supportsFullBroadcast = false;
registerBinaryKernel('FloorDiv', supportsFullBroadcast);
6 changes: 3 additions & 3 deletions tfjs-backend-wasm/src/kernels/Greater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
* =============================================================================
*/

import { registerBinaryKernel } from './binary_kernel';
const supportsBroadcast = true;
registerBinaryKernel('Greater', supportsBroadcast, 'bool');
import {registerBinaryKernel} from './binary_kernel';
const supportsFullBroadcast = false;
registerBinaryKernel('Greater', supportsFullBroadcast, 'bool');
6 changes: 3 additions & 3 deletions tfjs-backend-wasm/src/kernels/GreaterEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
* =============================================================================
*/

import { registerBinaryKernel } from './binary_kernel';
const supportsBroadcast = true;
registerBinaryKernel('GreaterEqual', supportsBroadcast, 'bool');
import {registerBinaryKernel} from './binary_kernel';
const supportsFullBroadcast = false;
registerBinaryKernel('GreaterEqual', supportsFullBroadcast, 'bool');
Loading