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
11 changes: 11 additions & 0 deletions tfjs-backend-wasm/src/cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ tfjs_cc_library(
":CropAndResize",
":Conv2D",
":DepthwiseConv2dNative",
":FloorDiv",
":FusedConv2D",
":Div",
":Mul",
Expand Down Expand Up @@ -213,6 +214,16 @@ tfjs_cc_library(
],
)

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

tfjs_cc_library(
name = "FusedConv2D",
srcs = ["kernels/FusedConv2D.cc"],
Expand Down
55 changes: 55 additions & 0 deletions tfjs-backend-wasm/src/cc/kernels/FloorDiv.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* 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 <cmath>

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

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

#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
#endif
void FloorDiv(const int a_id, const int b_id, const DType dtype,
const int out_id) {
auto& a_info = backend::get_tensor_info(a_id);
switch (dtype) {
case DType::float32:
binary_f32(a_id, b_id, out_id,
[](float a, float b) { return floor(a / b); });
break;
case DType::int32:
binary_i32(a_id, b_id, out_id, [](int a, int b) {
return static_cast<int>(floor(static_cast<float>(a) / b));
});
break;
default:
util::warn(
"FloorDiv for tensor ids %d and %d failed. Unsupported dtype %d",
a_id, b_id, dtype);
}
}

} // extern "C"
} // namespace wasm
} // namespace tfjs
19 changes: 19 additions & 0 deletions tfjs-backend-wasm/src/kernels/FloorDiv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* 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.
* =============================================================================
*/

import {registerBinaryKernel} from './binary_kernel';
registerBinaryKernel('FloorDiv');
1 change: 1 addition & 0 deletions tfjs-backend-wasm/src/kernels/all_kernels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import './Conv2D';
import './CropAndResize';
import './DepthwiseConv2dNative';
import './Div';
import './FloorDiv';
import './FusedBatchNorm';
import './FusedConv2D';
import './Max';
Expand Down
5 changes: 2 additions & 3 deletions tfjs-backend-wasm/src/setup_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ const TEST_FILTERS: TestFilter[] = [
{
include: 'div ',
excludes: [
'gradient', // Gradient not defined yet.
'integer division', // FloorDiv not yet implemented.
'upcasts', // Cast not supported yet.
'gradient', // Gradient not defined yet.
'upcasts', // Cast not supported yet.
'broadcasting same rank Tensors different shape', // Broadcasting along
// inner dims not
// supported yet.
Expand Down
4 changes: 2 additions & 2 deletions tfjs-core/src/ops/binary_ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,13 @@ function floorDiv_<T extends Tensor>(
const tmp = $b.square();
return res.div(tmp.toFloat()).neg();
};
return {$a: derA, $b: derB};
return {a: derA, b: derB};
};
return ENGINE.runKernelFunc((backend, save) => {
const res = backend.floorDiv($a, $b);
save([$a, $b]);
return res;
}, {$a, $b}, der) as T;
}, {a: $a, b: $b}, der, 'FloorDiv') as T;
}

/**
Expand Down