From 51ce6927fd03f4fc42f498b02dd28475ecab1df5 Mon Sep 17 00:00:00 2001 From: raver119 Date: Sat, 16 May 2020 10:44:58 +0300 Subject: [PATCH] FP Mod (#468) * mod Signed-off-by: raver119@gmail.com * couple of tests for updated mod Signed-off-by: raver119@gmail.com --- libnd4j/include/ops/ops.h | 13 +-- .../layers_tests/PlaygroundTests.cpp | 2 - .../layers_tests/PrimitivesTests.cpp | 92 +++++++++++++++++++ 3 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 libnd4j/tests_cpu/layers_tests/PrimitivesTests.cpp diff --git a/libnd4j/include/ops/ops.h b/libnd4j/include/ops/ops.h index 21cd07c40..ea52e9ba0 100644 --- a/libnd4j/include/ops/ops.h +++ b/libnd4j/include/ops/ops.h @@ -919,17 +919,12 @@ namespace simdOps { template class Mod { public: - /* - - // just a optional note, feel free to remove later - - op_def static half op(half d1, half d2, half *params) { - return __float2half(simdOps::Mod::op(__half2float(d1), __half2float(d2), nullptr)); - } - */ op_def static Z op(X d1, Y d2) { - return static_cast(d1) % static_cast(d2); + auto dx = static_cast(d2); + auto f = sd::math::nd4j_floor(d1 / dx); + auto r = f * dx; + return d1 - r; } op_def static Z op(X d1, Y d2, Z *params) { diff --git a/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp b/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp index f4c8bd2fa..91ddcbd30 100644 --- a/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp +++ b/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp @@ -59,8 +59,6 @@ public: int poolSize = 10; PlaygroundTests() { - printf("\n"); - fflush(stdout); } }; diff --git a/libnd4j/tests_cpu/layers_tests/PrimitivesTests.cpp b/libnd4j/tests_cpu/layers_tests/PrimitivesTests.cpp new file mode 100644 index 000000000..f131a1520 --- /dev/null +++ b/libnd4j/tests_cpu/layers_tests/PrimitivesTests.cpp @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2020 Konduit K.K. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://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. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +// +// @author raver110@gmail.com +// + +#include "testlayers.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace sd; +using namespace sd::graph; + +class PrimitivesTests : public testing::Test { + public: + + PrimitivesTests() { + } +}; + +TEST_F(PrimitivesTests, test_mod_1) { + int ix = 7; + int iy = 3; + + + auto v = simdOps::Mod::op(ix, iy); + + ASSERT_EQ(7 % 3, v); +} + +TEST_F(PrimitivesTests, test_mod_2) { + float ix = 7.f; + float iy = 3.f; + + + auto e = sd::math::nd4j_fmod(ix, iy); + auto v = simdOps::Mod::op(ix, iy); + + ASSERT_NEAR(e, v, 1e-5f); +} + +TEST_F(PrimitivesTests, test_mod_3) { + float ix = 7.f; + float iy = 0.f; + + + auto e = sd::math::nd4j_fmod(ix, iy); + auto v = simdOps::Mod::op(ix, iy); + + // absence of SIGFPE will be a good enough +} \ No newline at end of file