cavis/libnd4j/tests_cpu/layers_tests/DeclarableOpsTests14.cpp

1492 lines
51 KiB
C++

/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* 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
******************************************************************************/
//
// Created by raver on 8/4/2018.
//
#include "testlayers.h"
#include <ops/declarable/CustomOperations.h>
#include <NDArray.h>
#include <ops/ops.h>
#include <GradCheck.h>
using namespace nd4j;
class DeclarableOpsTests14 : public testing::Test {
public:
DeclarableOpsTests14() {
printf("\n");
fflush(stdout);
}
};
TEST_F(DeclarableOpsTests14, Test_Validation_Edge_1) {
auto x = NDArrayFactory::create<int>('c', {2}, {2, 2});
auto exp = NDArrayFactory::create('c', {2, 2}, Environment::getInstance()->defaultFloatDataType());
exp.assign(4.0f);
nd4j::ops::fill op;
auto result = op.evaluate({&x}, {4.0f});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(exp, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, Test_Reshape_CF_1) {
auto x = NDArrayFactory::create<double>('f', {2, 3}, {1.0, 4.0, 2.0, 5.0, 3.0, 6.0});
auto e = NDArrayFactory::create<double>('f', {3, 2}, {1.0, 3.0, 5.0, 2.0, 4.0, 6.0});
auto r = x.reshape('c', {3, 2});;
r.streamline('f');
nd4j::ops::reshape op;
auto result = op.evaluate({&x}, {3, 2});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
delete result;
}
TEST_F(DeclarableOpsTests14, Test_Inf_Comparison_1) {
auto x = NDArrayFactory::create<double>('c', {5}, {1, 2, 3, std::numeric_limits<double>::infinity(), 5});
auto y = NDArrayFactory::create<double>('c', {5}, {1, 2, 3, std::numeric_limits<double>::infinity(), 5});
ASSERT_EQ(x, y);
}
TEST_F(DeclarableOpsTests14, Test_Inf_Comparison_2) {
#ifdef FFAST_MATH
if (1 > 0)
return;
#endif
auto x = NDArrayFactory::create<double>('c', {5}, {1, 2, 3, std::numeric_limits<double>::infinity(), 5});
auto y = NDArrayFactory::create<double>('c', {5}, {1, 2, 3, -std::numeric_limits<double>::infinity(), 5});
ASSERT_NE(x, y);
}
TEST_F(DeclarableOpsTests14, Multiply_test) {
for(int k=2;k<10;k++){
//nd4j_printf("k=%d\n", k);
NDArray x = NDArrayFactory::create<double>('c', {k, 1});
NDArray y = NDArrayFactory::create<double>('c', {k});
NDArray e = NDArrayFactory::create<double>('c', {k, k});
x.assign(1.0);
y.assign(1.0);
e.assign(1.0);
nd4j::ops::multiply op;
auto result = op.evaluate({&x, &y});
auto f = result->at(0);
NDArray r = *f;
ASSERT_EQ(e, r);
ASSERT_EQ(e, *f);
delete result;
}
}
TEST_F(DeclarableOpsTests14, Test_EvalReductionShape_1) {
auto x = NDArrayFactory::create<int>('c', {3}, {5, 3, 4});
auto y = NDArrayFactory::create<int>('c', {1}, {1});
auto e = NDArrayFactory::create<Nd4jLong>('c', {2}, {5, 4});
nd4j::ops::evaluate_reduction_shape op;
auto result = op.evaluate({&x, &y}, {}, {}, {false, false});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(e, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, Test_EvalReductionShape_2) {
auto x = NDArrayFactory::create<int>('c', {3}, {5, 3, 4});
auto y = NDArrayFactory::create<int>('c', {1}, {1});
auto e = NDArrayFactory::create<Nd4jLong>('c', {3}, {5, 1, 4});
nd4j::ops::evaluate_reduction_shape op;
auto result = op.evaluate({&x, &y}, {}, {}, {true, false});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(e, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, Test_Reduce_Min_Small_0) {
auto x = NDArrayFactory::create<float>('c', {3, 4}, {-999.f, 0.2236f, 0.7973f, 0.0962f, 0.7231f, 0.3381f, -0.7301f, 0.9115f, -0.5094f, 0.9749f, -2.1340f, 0.6023f});
auto z = NDArrayFactory::create<float>('c', {4});
auto e = NDArrayFactory::create<float>('c', {4}, {-999.f, 0.2236f, -2.1340f, 0.0962f});
nd4j::ops::reduce_min op;
op.execute({&x}, {&z}, {}, {0}, {});
//z.printIndexedBuffer("Z");
ASSERT_EQ(e, z);
}
TEST_F(DeclarableOpsTests14, Test_Reduce_Min_Small_1) {
auto x = NDArrayFactory::create<float>('c', {3, 4}, {-999.f, 0.2236f, 0.7973f, 0.0962f, 0.7231f, 0.3381f, -0.7301f, 0.9115f, -0.5094f, 0.9749f, -2.1340f, 0.6023f});
auto z = NDArrayFactory::create<float>('c', {3});
auto e = NDArrayFactory::create<float>('c', {3}, {-999.f, -0.7301f, -2.1340f});
nd4j::ops::reduce_min op;
op.execute({&x}, {&z}, {}, {1}, {});
//z.printIndexedBuffer("Z");
ASSERT_EQ(e, z);
}
TEST_F(DeclarableOpsTests14, Test_Diag_Zeros_1) {
auto x = NDArrayFactory::create<double>('c', {2}, {1, 2});
auto z = NDArrayFactory::create<double>('c', {2, 2}, {-119, -119, -119, -119});
auto exp = NDArrayFactory::create<double>('c', {2, 2}, {1, 0, 0, 2});
nd4j::ops::diag op;
auto status = op.execute({&x}, {&z}, {}, {}, {});
ASSERT_EQ(Status::OK(), status);
ASSERT_EQ(exp, z);
}
TEST_F(DeclarableOpsTests14, Test_scalar_broadcast_1) {
auto x = NDArrayFactory::create<float>(1.0f);
auto y = NDArrayFactory::create<float>('c', {5, 10});
auto e = NDArrayFactory::create<float>('c', {5, 10});
e.assign(1.0);
nd4j::ops::add op;
auto result = op.evaluate({&x, &y});
ASSERT_EQ(Status::OK(), result->status());
ASSERT_EQ(e, *result->at(0));
delete result;
}
TEST_F(DeclarableOpsTests14, Test_scalar_broadcast_2) {
auto x = NDArrayFactory::create<float>(1.0f);
auto y = NDArrayFactory::create<float>('c', {5, 10});
auto e = NDArrayFactory::create<float>('c', {5, 10});
y.assign(2.0f);
e.assign(-1.0f);
nd4j::ops::subtract op;
auto result = op.evaluate({&x, &y});
ASSERT_EQ(Status::OK(), result->status());
ASSERT_EQ(e, *result->at(0));
delete result;
}
TEST_F(DeclarableOpsTests14, test_empty_fill_1) {
auto x = NDArrayFactory::empty<int>();
auto y = NDArrayFactory::create<int>(1);
nd4j::ops::fill op;
auto result = op.evaluate({&x, &y});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(y, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, test_lstmBlockCell_1) {
auto a = NDArrayFactory::create<double>('c', {1, 5}, {0.7787856f, 0.80119777f, 0.72437465f, 0.23089433f, 0.72714126f});
auto b = NDArrayFactory::create<double>('c', {1, 3});
auto c = NDArrayFactory::create<double>('c', {1, 3});
auto d = NDArrayFactory::create<double>('c', {8, 12}, {-0.15320599,-0.120416045,0.33126968,0.13921785,-0.32313538,-0.43956736,0.4756174,0.4335605,-0.5450856,-0.3943429,-0.28687626,0.068032146,-0.2793799,0.17298919,-0.36553562,-0.097853184,-0.2544747,-0.39872527,-0.14556861,-0.31479517,0.2559092,0.47166896,-0.31330687,0.47313118,0.5134543,-0.4678212,-0.12853557,0.26142156,0.43472284,-0.42842552,-0.1895876,0.538689,0.508651,-0.020272732,0.112327516,0.2704304,-0.046546757,0.32570732,-0.15148133,-0.19145513,0.18631572,-0.024152994,0.41603214,-0.3421499,0.0106860995,-0.2966229,-0.36713937,0.25841123,0.0843398,0.49082482,0.10800403,0.1874243,-0.26379472,-0.22531849,0.24924624,0.23119557,0.49940765,-0.051413506,0.20315129,-0.41888732,0.44097036,0.40453392,0.013338983,0.23434466,0.23942488,0.47894,-0.19898453,0.09253675,-0.032358468,-0.15213022,-0.3441009,-0.15600958,-0.08235118,0.12165731,-0.4481289,-0.4842423,-0.45797008,-0.4606034,0.08163166,-0.2981107,0.50207126,0.44195646,0.13850057,0.072246075,-0.34388685,0.030900061,0.35821778,0.47900867,0.5094063,0.23683065,0.18020362,-0.1369732,0.015235603,0.2786904,0.07954317,0.12543976});
auto e = NDArrayFactory::create<double>('c', {3});
auto f = NDArrayFactory::create<double>('c', {3});
auto g = NDArrayFactory::create<double>('c', {3});
auto h = NDArrayFactory::create<double>('c', {12});
auto z0 = NDArrayFactory::create<double>('c', {1, 3});
auto z1 = NDArrayFactory::create<double>('c', {1, 3});
auto z2 = NDArrayFactory::create<double>('c', {1, 3});
auto z3 = NDArrayFactory::create<double>('c', {1, 3});
auto z4 = NDArrayFactory::create<double>('c', {1, 3});
auto z5 = NDArrayFactory::create<double>('c', {1, 3});
auto z6 = NDArrayFactory::create<double>('c', {1, 3});
nd4j::ops::lstmBlockCell op;
auto result = op.execute({&a, &b, &c, &d, &e, &f, &g, &h}, {&z0, &z1, &z2, &z3, &z4, &z5, &z6}, {1.0, -1.0}, {0}, {});
ASSERT_EQ(Status::OK(), result);
}
TEST_F(DeclarableOpsTests14, test_empty_stack_1) {
auto x = NDArrayFactory::create<float>('c', {0});
auto e = NDArrayFactory::create<float>('c', {1, 0});
nd4j::ops::stack op;
auto result = op.evaluate({&x}, {}, {0});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(e, *z);
nd4j::ops::reduce_min sumOp;
auto res2 = sumOp.evaluate({&e}, {1.}, {1});
ASSERT_EQ(res2->status(), Status::OK());
auto out = res2->at(0);
ASSERT_EQ(out->e<float>(0), DataTypeUtils::infOrMax<float>());
delete res2;
delete result;
}
TEST_F(DeclarableOpsTests14, test_empty_stack_2) {
auto x = NDArrayFactory::empty<float>();
auto e = NDArrayFactory::create<float>('c', {0});
nd4j::ops::stack op;
auto result = op.evaluate({&x}, {}, {0});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(e, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, test_empty_stack_3) {
auto x = NDArrayFactory::empty<float>();
auto e = NDArrayFactory::create<float>('c', {2, 0});
nd4j::ops::stack op;
auto result = op.evaluate({&x, &x}, {}, {0});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(e, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, test_empty_stack_4) {
auto x = NDArrayFactory::create<float>('c', {0});
auto e = NDArrayFactory::create<float>('c', {2, 0});
nd4j::ops::stack op;
auto result = op.evaluate({&x, &x}, {}, {0});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(e, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, test_empty_reduce_min_1) {
auto e = NDArrayFactory::create<float>('c', {1, 0});
nd4j::ops::reduce_min sumOp;
auto res2 = sumOp.evaluate({&e}, {1.}, {1});
ASSERT_EQ(res2->status(), Status::OK());
auto out = res2->at(0);
ASSERT_EQ(out->e<float>(0), DataTypeUtils::infOrMax<float>());
delete res2;
}
TEST_F(DeclarableOpsTests14, test_empty_reduce_max_1) {
auto e = NDArrayFactory::create<float>('c', {1, 0});
nd4j::ops::reduce_max sumOp;
auto res2 = sumOp.evaluate({&e}, {1.}, {1});
ASSERT_EQ(res2->status(), Status::OK());
auto out = res2->at(0);
ASSERT_EQ(out->e<float>(0), -DataTypeUtils::infOrMax<float>());
delete res2;
}
TEST_F(DeclarableOpsTests14, test_empty_reduce_sum_1) {
#ifdef FFAST_MATH
if (1 > 0)
return;
#endif
auto e = NDArrayFactory::create<float>('c', {1, 0});
nd4j::ops::reduce_sum sumOp;
auto res2 = sumOp.evaluate({&e}, {1.}, {1});
ASSERT_EQ(res2->status(), Status::OK());
auto out = res2->at(0);
ASSERT_EQ(out->e<float>(0), 0.f);
delete res2;
}
TEST_F(DeclarableOpsTests14, test_empty_reduce_mean_1) {
#ifdef FFAST_MATH
if (1 > 0)
return;
#endif
auto e = NDArrayFactory::create<float>('c', {1, 0});
nd4j::ops::reduce_mean sumOp;
auto res2 = sumOp.evaluate({&e}, {1.}, {1});
ASSERT_EQ(res2->status(), Status::OK());
auto out = res2->at(0);
// out->printShapeInfo("ReduceMean empty shape with keep dims");
// out->printIndexedBuffer("ReduceMean scalar");
ASSERT_TRUE(std::isnan(out->e<float>(0)));
delete res2;
}
TEST_F(DeclarableOpsTests14, Test_StridedSliceZeros_1) {
auto matrix = NDArrayFactory::create<double>('c', {1, 2, 0, 4});
auto b = NDArrayFactory::create<int>('c', {3}, {0, 0, 0});
auto e = NDArrayFactory::create<int>('c', {3}, {2,0,2});
auto s = NDArrayFactory::create<int>('c', {3}, {1,1,1});
auto exp = NDArrayFactory::create<double>('c', {1,0,0,4});
matrix.linspace(1);
nd4j::ops::strided_slice op;
auto result = op.evaluate({&matrix, &b, &e, &s}, {}, {0, 0, 0, 0, 0});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.isSameShape(z));
delete result;
}
TEST_F(DeclarableOpsTests14, Test_StridedSliceZeros_2) {
auto matrix = NDArrayFactory::create<double>('c', {1, 2, 0, 4});
auto b = NDArrayFactory::create<int>('c', {3}, {0, 0, 0});
auto e = NDArrayFactory::create<int>('c', {3}, {2,0,2});
auto s = NDArrayFactory::create<int>('c', {3}, {1,1,1});
auto exp = NDArrayFactory::create<double>('c', {0,0,4});
matrix.linspace(1);
nd4j::ops::strided_slice op;
auto result = op.evaluate({&matrix, &b, &e, &s}, {}, {0, 0, 0, 0, 1});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.isSameShape(z));
delete result;
}
TEST_F(DeclarableOpsTests14, test_empty_argmax_1) {
auto x = NDArrayFactory::create<float>('c', {1, 0});
auto y = NDArrayFactory::create<int>(0);
auto e = NDArrayFactory::create<Nd4jLong>('c', {0});
nd4j::ops::argmax op;
//nd4j::ops::reduce_max op;
auto result = op.evaluate({&x, &y}, {}, {});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_EQ(e, *z);
delete result;
}
TEST_F(DeclarableOpsTests14, test_empty_argmax_2) {
auto x = NDArrayFactory::create<float>('c', {1, 0});
auto y = NDArrayFactory::create<int>(1);
nd4j::ops::argmax op;
try {
auto result = op.execute({&x, &y}, {&y}, {}, {}, {});
ASSERT_TRUE(false);
} catch (std::exception &e) {
//
}
}
TEST_F(DeclarableOpsTests14, test_empty_tanh_5) {
auto x = NDArrayFactory::create<float>('c', {32, 0});
nd4j::ops::tanh op;
auto result = op.evaluate({&x}, {}, {});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(x.isSameShape(z));
ASSERT_EQ(x, *z);
delete result;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, repeat_1) {
NDArray x('c', {2, 3}, {1, 2, 3, 4, 5, 6});
NDArray e('c', {4, 3}, {1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6});
nd4j::ops::repeat op;
auto result = op.evaluate({&x}, {}, {2, 0});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(e.isSameShape(z));
ASSERT_TRUE(e.equalsTo(z));
delete result;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, repeat_2) {
NDArray x('c', {2, 3}, {1, 2, 3, 4, 5, 6});
NDArray e('c', {2, 6}, {1, 1, 2, 2, 3, 3,4, 4, 5, 5, 6, 6});
nd4j::ops::repeat op;
auto result = op.evaluate({&x}, {}, {2, 1});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(e.isSameShape(z));
ASSERT_TRUE(e.equalsTo(z));
delete result;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, repeat_3) {
NDArray x('c', {2, 3}, {1, 2, 3, 4, 5, 6});
NDArray e('c', {2, 6}, {1, 2, 2, 3, 3, 3,4, 5, 5, 6, 6, 6});
nd4j::ops::repeat op;
auto result = op.evaluate({&x}, {}, {1,2,3, 1});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(e.isSameShape(z));
ASSERT_TRUE(e.equalsTo(z));
delete result;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, repeat_4) {
NDArray x('c', {2, 3}, {1, 2, 3, 4, 5, 6});
NDArray e('c', {7, 3}, {1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6});
nd4j::ops::repeat op;
auto result = op.evaluate({&x}, {}, {3,4, 0});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(e.isSameShape(z));
ASSERT_TRUE(e.equalsTo(z));
delete result;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, repeat_5) {
NDArray x('c', {2, 3, 4}, {1, 2, 3, 4, 5, 6, 7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24});
NDArray e('c', {2, 4, 4}, {1, 2, 3, 4, 5, 6, 7, 8, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 17, 18, 19, 20, 21, 22, 23, 24});
nd4j::ops::repeat op;
auto result = op.evaluate({&x}, {}, {1,2,1, 1});
ASSERT_EQ(Status::OK(), result->status());
auto z = result->at(0);
ASSERT_TRUE(e.isSameShape(z));
ASSERT_TRUE(e.equalsTo(z));
delete result;
}
/////////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest) {
auto y = NDArray('c', { 3 }, nd4j::DataType::FLOAT32);
auto x = NDArray('c', { 5, 2, 1 }, nd4j::DataType::FLOAT32);
auto e = NDArray('c', { 5, 2, 3 }, { 2., 2., 2., 3., 3., 3., 4., 4., 4., 5., 5., 5., 6., 6., 6., 7., 7., 7., 8., 8., 8., 9., 9., 9., 10., 10., 10., 11., 11., 11. }, nd4j::DataType::FLOAT32);
y.assign(1.0);
x.linspace(1.0);
nd4j::ops::add op;
auto result = op.evaluate({ &x, &y });
ASSERT_EQ(Status::OK(), result->status());
auto res = *result->at(0);
ASSERT_EQ(e, res);
delete result;
}
/////////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest2) {
auto y = NDArray('c', { 1, 3 }, nd4j::DataType::FLOAT32);
auto x = NDArray('c', { 5, 2, 1 }, nd4j::DataType::FLOAT32);
auto e = NDArray('c', { 5, 2, 3 }, { 2., 2., 2., 3., 3., 3., 4., 4., 4., 5., 5., 5., 6., 6., 6., 7., 7., 7., 8., 8., 8., 9., 9., 9., 10., 10., 10., 11., 11., 11. }, nd4j::DataType::FLOAT32);
y.assign(1.0);
x.linspace(1.0);
nd4j::ops::add op;
auto result = op.evaluate({ &x, &y });
ASSERT_EQ(Status::OK(), result->status());
auto res = *result->at(0);
ASSERT_EQ(e, res);
delete result;
}
///////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest3) {
auto x = NDArray('c', { 3, 5, 1 }, nd4j::DataType::FLOAT32);
auto y = NDArray('c', { 3, 1, 4 }, nd4j::DataType::FLOAT32);
auto z = NDArray('c', { 3, 5, 4 }, nd4j::DataType::FLOAT32);
// recieved by main algorithm
auto e = NDArray('c', { 3, 5, 4 }, { 10., 11., 12., 13., 20., 22., 24., 26., 30., 33., 36., 39., 40., 44., 48., 52., 50., 55., 60., 65., 84., 90., 96., 102., 98., 105., 112., 119., 112., 120., 128., 136., 126., 135., 144., 153., 140., 150., 160., 170., 198., 209., 220., 231., 216., 228., 240., 252., 234., 247., 260., 273., 252., 266., 280., 294., 270., 285., 300., 315. }, nd4j::DataType::FLOAT32);
x.linspace(1.f);
y.linspace(10.f);
z.assign(0.f);
x.applyTrueBroadcast(BroadcastOpsTuple::Multiply(), y, z);
ASSERT_EQ(e, z);
}
///////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest4) {
auto x = NDArray('c', { 2, 3, 5, 1 }, nd4j::DataType::FLOAT32);
auto y = NDArray('c', { 2, 3, 1, 4 }, nd4j::DataType::FLOAT32);
auto z = NDArray('c', { 2, 3, 5, 4 }, nd4j::DataType::FLOAT32);
// recieved by main algorithm
auto e = NDArray('c', { 2, 3, 5, 4 }, { 10., 11., 12., 13.,20., 22., 24., 26.,30., 33., 36., 39.,40., 44., 48., 52.,50., 55., 60., 65.,84., 90., 96., 102.,98., 105., 112., 119.,112., 120., 128., 136.,126., 135., 144., 153.,140., 150., 160., 170.,198., 209., 220., 231.,216., 228., 240., 252.,234., 247., 260., 273.,252., 266., 280., 294.,270., 285., 300., 315.,352., 368., 384., 400.,374., 391., 408., 425.,396., 414., 432., 450.,418., 437., 456., 475.,440., 460., 480., 500.,546., 567., 588., 609.,572., 594., 616., 638.,598., 621., 644., 667.,624., 648., 672., 696.,650., 675., 700., 725.,780., 806., 832., 858.,810., 837., 864., 891.,840., 868., 896., 924.,870., 899., 928., 957.,900., 930., 960., 990. }, nd4j::DataType::FLOAT32);
x.linspace(1.f);
y.linspace(10.f);
z.assign(0.f);
x.applyTrueBroadcast(BroadcastOpsTuple::Multiply(), y, z);
ASSERT_EQ(e, z);
}
///////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest5) {
auto x = NDArray('c', { 3, 5, 1 }, nd4j::DataType::FLOAT32);
auto y = NDArray('c', { 3, 1, 4 }, nd4j::DataType::FLOAT32);
auto z = NDArray('c', { 3, 5, 4 }, nd4j::DataType::FLOAT32);
// recieved by main algorithm
auto e = NDArray('c', { 3, 5, 4 }, { 0.1, 0.090909, 0.083333, 0.076923,0.2, 0.181818, 0.166667, 0.153846,0.3, 0.272727, 0.250000, 0.230769,0.4, 0.363636, 0.333333, 0.307692,0.5, 0.454545, 0.416667, 0.384615, 0.428571, 0.400000, 0.375000, 0.352941, 0.500000, 0.466667, 0.437500, 0.411765, 0.571429, 0.533333, 0.500000, 0.470588, 0.642857, 0.600000, 0.562500, 0.529412, 0.714286, 0.666667, 0.625000, 0.588235, 0.611111, 0.578947, 0.550000, 0.523810, 0.666667, 0.631579, 0.600000, 0.571429, 0.722222, 0.684211, 0.650000, 0.619048, 0.777778, 0.736842, 0.700000, 0.666667, 0.833333, 0.789474, 0.750000, 0.714286 }, nd4j::DataType::FLOAT32);
x.linspace(1.f);
y.linspace(10.f);
z.assign(0.f);
x.applyTrueBroadcast(BroadcastOpsTuple::Divide(), y, z);
ASSERT_EQ(e, z);
}
///////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest6) {
auto x = NDArray('c', { 2, 3, 5, 1 }, nd4j::DataType::FLOAT32);
auto y = NDArray('c', { 2, 3, 1, 4 }, nd4j::DataType::FLOAT32);
auto z = NDArray('c', { 2, 3, 5, 4 }, nd4j::DataType::FLOAT32);
// recieved by main algorithm
auto e = NDArray('c', { 2, 3, 5, 4 }, { 0.1, 0.090909, 0.083333, 0.076923,0.2, 0.181818, 0.166667, 0.153846,0.3, 0.272727, 0.250000, 0.230769,0.4, 0.363636, 0.333333, 0.307692,0.5, 0.454545, 0.416667, 0.384615, 0.428571, 0.400000, 0.375000, 0.352941, 0.500000, 0.466667, 0.437500, 0.411765, 0.571429, 0.533333, 0.500000, 0.470588, 0.642857, 0.600000, 0.562500, 0.529412, 0.714286, 0.666667, 0.625000, 0.588235,0.611111, 0.578947, 0.550000, 0.523810,0.666667, 0.631579, 0.600000, 0.571429,0.722222, 0.684211, 0.650000, 0.619048,0.777778, 0.736842, 0.700000, 0.666667,0.833333, 0.789474, 0.750000, 0.714286, 0.727273, 0.695652, 0.666667, 0.64, 0.772727, 0.739130, 0.708333, 0.68, 0.818182, 0.782609, 0.750000, 0.72, 0.863636, 0.826087, 0.791667, 0.76, 0.909091, 0.869565, 0.833333, 0.80, 0.807692, 0.777778, 0.750000, 0.724138, 0.846154, 0.814815, 0.785714, 0.758621, 0.884615, 0.851852, 0.821429, 0.793103, 0.923077, 0.888889, 0.857143, 0.827586, 0.961538, 0.925926, 0.892857, 0.862069, 0.866667, 0.838710, 0.812500, 0.787879, 0.900000, 0.870968, 0.843750, 0.818182, 0.933333, 0.903226, 0.875000, 0.848485, 0.966667, 0.935484, 0.906250, 0.878788, 1.000000, 0.967742, 0.937500, 0.909091 }, nd4j::DataType::FLOAT32);
x.linspace(1.f);
y.linspace(10.f);
z.assign(0.f);
x.applyTrueBroadcast(BroadcastOpsTuple::Divide(), y, z);
ASSERT_EQ(e, z);
}
///////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest7) {
auto x = NDArray('c', { 3, 5, 1 }, nd4j::DataType::FLOAT32);
auto y = NDArray('c', { 3, 1, 4 }, nd4j::DataType::FLOAT32);
auto z = NDArray('c', { 3, 5, 4 }, nd4j::DataType::FLOAT32);
// recieved by main algorithm
auto e = NDArray('c', { 3, 5, 4 }, { -9., -10., -11., -12.,-8., -9., -10., -11., -7., -8., -9., -10.,-6., -7., -8., -9.,-5., -6., -7., -8.,-8., -9., -10., -11.,-7., -8., -9., -10.,-6., -7., -8., -9.,-5., -6., -7., -8.,-4., -5., -6., -7.,-7., -8.000000, -9.000000, -10.00,-6.000000, -7.000000, -8.000000, -9.000,-5.000000, -6.000000, -7.000000, -8.000,-4.000000, -5.000000, -6.000000, -7.000,-3.000000, -4.000000, -5.000000, -6.000 }, nd4j::DataType::FLOAT32);
x.linspace(1.f);
y.linspace(10.f);
z.assign(0.f);
x.applyTrueBroadcast(BroadcastOpsTuple::Subtract(), y, z);
ASSERT_EQ(e, z);
}
///////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, Test_broadcast_SpecialCaseTest8) {
auto x = NDArray('c', { 2, 3, 5, 1 }, nd4j::DataType::FLOAT32);
auto y = NDArray('c', { 2, 3, 1, 4 }, nd4j::DataType::FLOAT32);
auto z = NDArray('c', { 2, 3, 5, 4 }, nd4j::DataType::FLOAT32);
// recieved by main algorithm
auto e = NDArray('c', { 2, 3, 5, 4 }, { -9.0, -10., -11., -12.,-8., -9., -10., -11.0,-7., -8., -9., -10.,-6., -7., -8., -9.,-5., -6., -7., -8.,-8., -9., -10., -11.,-7., -8., -9., -10.,-6., -7., -8., -9.,-5., -6., -7., -8.,-4., -5., -6., -7.,-7., -8., -9., -10.,-6., -7., -8., -9.,-5., -6., -7., -8.,-4., -5., -6., -7.,-3., -4., -5., -6.,-6., -7., -8., -9.,-5., -6., -7., -8.,-4., -5., -6., -7.,-3., -4., -5., -6.,-2., -3., -4., -5.,-5., -6., -7., -8.,-4., -5., -6., -7.,-3., -4., -5., -6.,-2., -3., -4., -5.,-1., -2., -3., -4.,-4., -5., -6., -7.,-3., -4., -5., -6.,-2., -3., -4., -5.,-1., -2., -3., -4., 0., -1., -2., -3. }, nd4j::DataType::FLOAT32);
x.linspace(1.f);
y.linspace(10.f);
z.assign(0.f);
x.applyTrueBroadcast(BroadcastOpsTuple::Subtract(), y, z);
ASSERT_EQ(e, z);
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test1) {
auto x = NDArrayFactory::create<double>('c', {3, 4});
auto y = NDArrayFactory::create<double>('c', {4, 3});
auto exp = NDArrayFactory::create<double>('f', {3, 3}, {35., 79., 123., 40., 92., 144., 45., 105., 165.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test2) {
auto x = NDArrayFactory::create<double>('c', {3, 4});
auto y = NDArrayFactory::create<double>('f', {4, 3});
auto exp = NDArrayFactory::create<double>('f', {3, 3}, {35., 79., 123.,40., 92., 144.,45.,105., 165.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test3) {
auto x = NDArrayFactory::create<double>('f', {3, 4});
auto y = NDArrayFactory::create<double>('c', {4, 3});
auto exp = NDArrayFactory::create<double>('f', {3, 3}, {35., 79., 123.,40., 92., 144.,45.,105., 165.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test4) {
auto x = NDArrayFactory::create<double> ('f', {3, 4});
auto y = NDArrayFactory::create<double>('f', {4, 3});
auto exp = NDArrayFactory::create<double>('f', {3, 3}, {35., 79., 123.,40., 92., 144.,45.,105., 165.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test5) {
auto x = NDArrayFactory::create<double>('c', {4, 3});
auto y = NDArrayFactory::create<double>('c', {4, 3});
auto exp = NDArrayFactory::create<double>('f', {3, 3}, {83., 94., 105., 94., 107., 120., 105., 120., 135.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test6) {
auto x = NDArrayFactory::create<double>('c', {4, 3});
auto y = NDArrayFactory::create<double>('f', {3, 4});
auto exp = NDArrayFactory::create<double>('f', {3, 3}, {35., 40., 45., 79., 92., 105., 123., 144., 165.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test7) {
auto x = NDArrayFactory::create<double>('c', {5, 3,4});
auto y = NDArrayFactory::create<double>('f', {5, 3,4});
auto exp = NDArrayFactory::create<double>('f',{5, 3,3}, {3. , 84.6, 281.4, 593.4, 1020.6, 7. , 107.8, 323.8, 655. , 1101.4,11. , 131. , 366.2, 716.6, 1182.2,
7. , 107.8, 323.8, 655. , 1101.4,17.4, 137.4, 372.6, 723. , 1188.6,27.8, 167. , 421.4, 791. , 1275.8,
11. , 131. , 366.2, 716.6, 1182.2,27.8, 167. , 421.4, 791. , 1275.8,44.6, 203. , 476.6, 865.4, 1369.4,});
x.linspace(1.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {0, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test8) {
auto x = NDArrayFactory::create<double>('c', {2,5, 3,4});
auto y = NDArrayFactory::create<double>('f', {2,5, 3,4});
auto exp = NDArrayFactory::create<double>('f',{2,5, 3,3}, {3. , 1563. , 84.6, 2220.6, 281.4, 2993.4, 593.4, 3881.4,1020.6, 4884.6, 7. , 1663. , 107.8, 2339.8, 323.8, 3131.8, 655. , 4039. ,1101.4, 5061.4,
11. , 1763. , 131. , 2459. , 366.2, 3270.2, 716.6, 4196.6,1182.2, 5238.2, 7. , 1663. , 107.8, 2339.8, 323.8, 3131.8, 655. , 4039. ,1101.4, 5061.4,
17.4, 1769.4, 137.4, 2465.4, 372.6, 3276.6, 723. , 4203. ,1188.6, 5244.6, 27.8, 1875.8, 167. , 2591. , 421.4, 3421.4, 791. , 4367. ,1275.8, 5427.8,
11. , 1763. , 131. , 2459. , 366.2, 3270.2, 716.6, 4196.6,1182.2, 5238.2, 27.8, 1875.8, 167. , 2591. , 421.4, 3421.4, 791. , 4367. ,1275.8, 5427.8,
44.6, 1988.6, 203. , 2723. , 476.6, 3572.6, 865.4, 4537.4,1369.4, 5617.4});
x.linspace(1.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {0, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test9) {
auto x = NDArrayFactory::create<double>('c', {2,5, 4,3});
auto y = NDArrayFactory::create<double>('f', {2,5, 3,4});
auto exp = NDArrayFactory::create<double>('f',{2,5, 3,3}, {7. , 1639. , 103. , 2311. , 314.2, 3098.2, 640.6, 4000.6,1082.2, 5018.2, 8. , 1664. , 108.8, 2340.8, 324.8, 3132.8, 656. , 4040. ,1102.4, 5062.4,
9. , 1689. , 114.6, 2370.6, 335.4, 3167.4, 671.4, 4079.4,1122.6, 5106.6, 15.8, 1743.8, 131. , 2435. , 361.4, 3241.4, 707. , 4163. ,1167.8, 5199.8,
18.4, 1770.4, 138.4, 2466.4, 373.6, 3277.6, 724. , 4204. ,1189.6, 5245.6, 21. , 1797. , 145.8, 2497.8, 385.8, 3313.8, 741. , 4245. ,1211.4, 5291.4,
24.6, 1848.6, 159. , 2559. , 408.6, 3384.6, 773.4, 4325.4,1253.4, 5381.4, 28.8, 1876.8, 168. , 2592. , 422.4, 3422.4, 792. , 4368. ,1276.8, 5428.8,
33. , 1905. , 177. , 2625. , 436.2, 3460.2, 810.6, 4410.6,1300.2, 5476.2});
x.linspace(1.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
TEST_F(DeclarableOpsTests14, matmul_test10) {
auto x = NDArrayFactory::create_<float>('c', {3, 5});
x->linspace(1);
auto y = NDArrayFactory::create_<float>('c', {5, 3});
y->linspace(1);
float _expB[]{135.0f, 310.0f, 485.0f, 150.0f, 350.0f, 550.0f, 165.0f, 390.0f, 615.0f};
Nd4jLong _expS[] {2, 3, 3, 1, 3, 0, 1, 102}; // expected shape
ArrayOptions::setDataType(_expS, nd4j::DataType::FLOAT32);
NDArray exp(_expB, _expS);
auto variableSpace = new VariableSpace();
variableSpace->putVariable(-1, x);
variableSpace->putVariable(-2, y);
variableSpace->putVariable(1, new Variable());
auto block = new Context(1, variableSpace, false);
block->fillInputs({-1, -2});
nd4j::ops::matmul op;
Nd4jStatus status = op.execute(block);
ASSERT_EQ(ND4J_STATUS_OK, status);
ASSERT_TRUE(variableSpace->hasVariable(1));
auto result = variableSpace->getVariable(1)->getNDArray();
ASSERT_TRUE(result->equalsTo(&exp));
delete block;
delete variableSpace;
}
TEST_F(DeclarableOpsTests14, matmul_test11) {
auto A = NDArrayFactory::create<float>('c', {3, 3});
auto B = NDArrayFactory::create<float>('c', {3, 1});
auto exp = NDArrayFactory::create<float>('c', {3, 1}, {14.00f, 32.00f, 50.00f});
A.linspace(1);
B.linspace(1);
nd4j::ops::matmul op;
auto result = op.evaluate({&A, &B}, {}, {});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test12) {
auto x= NDArrayFactory::create<double>('c', {3, 4}, {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10, 11, 12});
auto y= NDArrayFactory::create<double>('c', {4, 3}, {1, 2, 3, 4, 5, 6, 7, 8 , 9, 10, 11, 12});
auto exp= NDArrayFactory::create<double>('f', {4, 4}, {38.0, 44.0, 50.0, 56.0, 83.0, 98.0, 113.0, 128.0, 128.0, 152.0, 176.0, 200.0, 173.0, 206.0, 239.0, 272.0});
nd4j::ops::matmul op;
auto result = op.evaluate({&x, &y}, {}, {1, 1});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test13) {
auto x= NDArrayFactory::create<double>('c', {1, 3}, {1, 2, 3});
auto y= NDArrayFactory::create<double>('c', {1, 4}, {1, 2, 3, 4});
auto exp= NDArrayFactory::create<double>('f', {3, 4}, {1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 3.0, 6.0, 9.0, 4.0, 8.0, 12.0});
nd4j::ops::matmul op;
auto result = op.evaluate({&x, &y}, {}, {1, 0});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
//z->printIndexedBuffer("z");
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test14) {
auto x= NDArrayFactory::create<double>('c', {3, 1}, {1, 2, 3});
auto y= NDArrayFactory::create<double>('c', {4, 1}, {1, 2, 3, 4});
auto exp= NDArrayFactory::create<double>('f', {3, 4}, {1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 3.0, 6.0, 9.0, 4.0, 8.0, 12.0});
nd4j::ops::matmul op;
auto result = op.evaluate({&x, &y}, {}, {0, 1});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
//z->printIndexedBuffer("z");
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test15) {
auto x= NDArrayFactory::create<double>('c', {3, 1}, {1, 2, 3});
auto y= NDArrayFactory::create<double>('c', {1, 4}, {1, 2, 3, 4});
auto exp= NDArrayFactory::create<double>('f', {3, 4}, {1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 3.0, 6.0, 9.0, 4.0, 8.0, 12.0});
nd4j::ops::matmul op;
auto result = op.evaluate({&x, &y}, {}, {});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
//z->printIndexedBuffer("z");
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test16) {
auto x= NDArrayFactory::create<double>('c', {4, 1}, {1, 2, 3, 4});
auto y= NDArrayFactory::create<double>('c', {1, 4}, {1, 2, 3, 4});
auto exp= NDArrayFactory::create<double>('f', {4, 4}, {1,2, 3, 4,2,4, 6, 8,3,6, 9,12,4,8,12,16});
nd4j::ops::matmul op;
auto result = op.evaluate({&x, &y});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
//z->printIndexedBuffer("z");
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test17) {
auto x = NDArrayFactory::create<double>('c', {1, 2}, {2.0f, 2.0f});
auto y = NDArrayFactory::create<double>('c', {2, 1}, {2.0f, 2.0f});
auto exp = NDArrayFactory::create<double>('c', {1, 1}, {8.0f});
nd4j::ops::matmul op;
auto result = op.evaluate({&x, &y}, {}, {});
ASSERT_EQ(Status::OK(), result->status());
ASSERT_EQ(exp, *result->at(0));
delete result;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test18) {
auto x = NDArrayFactory::create<double>('c', {1, 4, 3});
auto y = NDArrayFactory::create<double>('f', {1, 3, 4});
auto exp = NDArrayFactory::create<double>('f', {1, 3, 3}, {35., 40., 45., 79., 92., 105., 123., 144., 165.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test19) {
auto x = NDArrayFactory::create<double>('c', {4, 1});
auto y = NDArrayFactory::create<double>('f', {1, 4});
auto exp = NDArrayFactory::create<double>('f', {1, 1}, {15});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1});
ASSERT_EQ(Status::OK(), results->status());
auto z = results->at(0);
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test20) {
auto x = NDArrayFactory::create<double>('c', {1, 4, 1});
auto y = NDArrayFactory::create<double>('f', {1, 1, 4});
auto exp = NDArrayFactory::create<double>('f', {1, 1, 1}, {15});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1});
ASSERT_EQ(Status::OK(), results->status());
auto z = results->at(0);
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test21) {
auto x = NDArrayFactory::create<double>('c', {2, 3});
auto y = NDArrayFactory::create<double>('c', {3, 5});
auto exp = NDArrayFactory::create<double>('f', {5, 2}, {23. , 26. , 29. , 32. , 35., 50. , 57.5, 65. , 72.5, 80.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {0, 0, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test22) {
auto x = NDArrayFactory::create<double>('c', {3, 2});
auto y = NDArrayFactory::create<double>('c', {3, 5});
auto exp = NDArrayFactory::create<double>('f', {5, 2}, {37. , 41.5, 46. , 50.5, 55., 46. , 52. , 58. , 64. , 70.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 0, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test23) {
auto x = NDArrayFactory::create<double>('c', {3, 2});
auto y = NDArrayFactory::create<double>('c', {3, 5});
auto exp = NDArrayFactory::create<double>('f', {5, 2}, {37. , 41.5, 46. , 50.5, 55., 46. , 52. , 58. , 64. , 70.});
x.linspace(1.);
y.linspace(0.5, 0.5);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 0, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test24) {
auto x = NDArrayFactory::create<double>('c', {2,2, 3,5});
auto y = NDArrayFactory::create<double>('c', {2,2, 4,3});
auto exp = NDArrayFactory::create<double>('f',{2,2, 4,5}, {4.6, 281.8, 89.2, 582.4, 10. , 314.2,108.1, 628.3, 15.4, 346.6,127. , 674.2, 20.8, 379. ,145.9, 720.1, 5.2, 289.6, 93.4, 593.8,
11.5, 322.9,113.2, 640.6, 17.8, 356.2,133. , 687.4, 24.1, 389.5,152.8, 734.2, 5.8, 297.4, 97.6, 605.2, 13. , 331.6,118.3, 652.9,
20.2, 365.8,139. , 700.6, 27.4, 400. ,159.7, 748.3, 6.4, 305.2,101.8, 616.6, 14.5, 340.3,123.4, 665.2, 22.6, 375.4,145. , 713.8,
30.7, 410.5,166.6, 762.4, 7. , 313. ,106. , 628. , 16. , 349. ,128.5, 677.5, 25. , 385. ,151. , 727. , 34. , 421. ,173.5, 776.5});
x.linspace(1.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test25) {
auto x = NDArrayFactory::create<double>('f', {4, 3});
auto y = NDArrayFactory::create<double>('c', {4});
auto exp = NDArrayFactory::create<double>('f',{3}, {7., 8., 9.});
x.linspace(1.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 0});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test26) {
auto x = NDArrayFactory::create<double>('f', {3});
auto y = NDArrayFactory::create<double>('c', {4, 3});
auto exp = NDArrayFactory::create<double>('f',{4}, {1.4, 3.2, 5., 6.8});
x.linspace(1.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {0, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test27) {
auto x = NDArrayFactory::create<double>('f', {1, 1});
auto y = NDArrayFactory::create<double>('c', {1, 1});
auto exp = NDArrayFactory::create<double>('f',{1, 1}, {0.2});
x.linspace(2.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test28) {
auto x = NDArrayFactory::create<double>('f', {1, 1});
auto y = NDArrayFactory::create<double>('c', {1, 1});
auto exp = NDArrayFactory::create<double>('f',{1, 1}, {0.2});
x.linspace(2.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1,1,1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test29) {
auto x = NDArrayFactory::create<double>('f', {1});
auto y = NDArrayFactory::create<double>('c', {1, 1});
auto exp = NDArrayFactory::create<double>('f',{1}, {0.2});
x.linspace(2.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test30) {
auto x = NDArrayFactory::create<double>('f', {1,1});
auto y = NDArrayFactory::create<double>('c', {1});
auto exp = NDArrayFactory::create<double>('f',{1}, {0.2});
x.linspace(2.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test31) {
auto x = NDArrayFactory::create<double>('f', {4});
auto y = NDArrayFactory::create<double>('c', {4});
auto exp = NDArrayFactory::create<double>(3.);
x.linspace(1.);
y.linspace(0.1, 0.1);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test32) {
auto x = NDArrayFactory::create<double>('f', {1}, {2.});
auto y = NDArrayFactory::create<double>('c', {1}, {3.});
auto exp = NDArrayFactory::create<double>(6.);
nd4j::ops::matmul op;
auto results = op.evaluate({&x, &y}, {}, {1, 1});
auto z = results->at(0);
ASSERT_EQ(Status::OK(), results->status());
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete results;
}
TEST_F(DeclarableOpsTests14, matmul_test33) {
auto x = NDArrayFactory::create<double>('c', {4, 3});
auto y = NDArrayFactory::create<double>('c', {4, 1});
auto exp = NDArrayFactory::create<double>('c',{ 3, 1}, {70, 80, 90});
x.linspace(1);
y.linspace(1);
nd4j::ops::matmul op;
auto result = op.evaluate({&x, &y}, {}, {1, 0});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test34) {
auto a = NDArrayFactory::create<double>('c', {3, 4}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
auto b = NDArrayFactory::create<double>('c', {4}, {1, 2, 3, 4});
auto exp = NDArrayFactory::create<double>('c', {3}, {30, 70, 110});
nd4j::ops::matmul op;
auto result = op.evaluate({&a, &b});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test35) {
auto a = NDArrayFactory::create<double>('c', {4}, {1, 2, 3, 4});
auto b = NDArrayFactory::create<double>('c', {4, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
auto exp = NDArrayFactory::create<double>('c', {3}, {70, 80, 90});
nd4j::ops::matmul op;
auto result = op.evaluate({&a, &b});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
TEST_F(DeclarableOpsTests14, matmul_test36) {
auto a = NDArrayFactory::create<double>('c', {1, 4}, {1, 2, 3, 4});
auto b = NDArrayFactory::create<double>('c', {4, 3}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
auto exp = NDArrayFactory::create<double>('c', {1, 3}, {70, 80, 90});
nd4j::ops::matmul op;
auto result = op.evaluate({&a, &b});
ASSERT_EQ(ND4J_STATUS_OK, result->status());
auto z = result->at(0);
ASSERT_TRUE(exp.isSameShape(z));
ASSERT_TRUE(exp.equalsTo(z));
delete result;
}
//////////////////////////////////////////////////////////////////////
TEST_F(DeclarableOpsTests14, matmul_test37) {
NDArray a('c', {32, 12, 128, 64}, nd4j::DataType::FLOAT32);
NDArray b('c', {32, 12, 128, 64}, nd4j::DataType::FLOAT32);
NDArray c('c', {32,12,128,128}, nd4j::DataType::FLOAT32);
NDArray cExp('c', {32,12,128,128}, nd4j::DataType::FLOAT32);
a = 1;
b = 1;
cExp = 64; //Each entry in output c is sum of 64 (1.0 x 1.0) multiplications
nd4j::ops::matmul op;
auto status = op.execute({&a, &b}, {&c}, {}, {0,1});
ASSERT_EQ(ND4J_STATUS_OK, status);
ASSERT_TRUE(cExp.isSameShape(c));
ASSERT_TRUE(cExp.equalsTo(c));
}
// @Test
// public void testMmulRank4_simple(){
// INDArray arr1 = Nd4j.ones(DataType.FLOAT, 32, 12, 128, 64);
// INDArray arr2 = Nd4j.ones(DataType.FLOAT, 32, 12, 128, 64);
// DynamicCustomOp op = DynamicCustomOp.builder("matmul")
// .addInputs(arr1, arr2)
// .addIntegerArguments(0, 1) //Transpose arr2 only
// .build();
// List<LongShapeDescriptor> shapes = op.calculateOutputShape();
// assertEquals(1, shapes.size());
// long[] shape = new long[]{32,12,128,128};
// assertArrayEquals(shape, shapes.get(0).getShape());
// INDArray out = Nd4j.create(DataType.FLOAT, shape);
// op.setOutputArgument(0, out);
// Nd4j.exec(op);
// // System.out.println(out);
// INDArray exp = Nd4j.valueArrayOf(shape, 64.0, DataType.FLOAT); //Each entry in output is sum of 64 (1.0 x 1.0) multiplications
// assertEquals(exp, out);
// }