summaryrefslogtreecommitdiff
path: root/libs/kernel/acl/src/cl/Softmax.test.cpp
blob: 8ee8b41e220480127e6fb5b9085977b881362fa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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.
 */

#include <gtest/gtest.h>
#include <OperationsUtils.h>
#include <kernel/acl/nnfw_kernel_acl.h>
#include <arm_compute/core/Types.h>
#include <kernel/acl/Softmax.h>

#include "../util.h"

using namespace nnfw::kernel::acl;

TEST(KernelACL_TC, softmaxFloat32_1xn)
{
  float inputData[4];
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,4}, 1.0, 0 };
  float outputData[4];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,4}, 1.0, 0 };
  const float beta = 1.0f;
  bool bret;

  util::initData(inputData, sizeof(inputData) / sizeof(inputData[0]), 1.0);
  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = softmaxFloat32(inputData, inputShape, beta, outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectData[] = { 0.25f, 0.25f, 0.25f, 0.25f };
  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}

TEST(KernelACL_TC, softmaxFloat32_4d)
{
  float inputData[4];
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,1,4,1}, 1.0, 0 };
  float outputData[4];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,1,4,1}, 1.0, 0 };
  const float beta = 1.0f;
  bool bret;

  util::initData(inputData, sizeof(inputData) / sizeof(inputData[0]), 1.0);
  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = softmaxFloat32(inputData, inputShape, beta, outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectData[] = { 0.25f, 0.25f, 0.25f, 0.25f };
  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}

TEST(KernelACL_TC, softmaxFloat32_1xn_seq)
{
  float inputData[4];
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,4}, 1.0, 0 };
  float outputData[4];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,4}, 1.0, 0 };
  const float beta = 1.0f;
  bool bret;

  util::initData_Increasing(inputData, sizeof(inputData) / sizeof(inputData[0]), 1.0);
  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = softmaxFloat32(inputData, inputShape, beta, outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectData[] = {0.032058603280085, 0.0871443187420326, 0.23688281808991, 0.643914259887972};
  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}

TEST(KernelACL_TC, softmaxFloat32_4d_seq)
{
  float inputData[4];
  const nnfw::rt::Shape inputShape = { OperandType::FLOAT32, {1,1,4,1}, 1.0, 0 };
  float outputData[4];
  const nnfw::rt::Shape outputShape = { OperandType::FLOAT32, {1,1,4,1}, 1.0, 0 };
  const float beta = 1.0f;
  bool bret;

  util::initData_Increasing(inputData, sizeof(inputData) / sizeof(inputData[0]), 1.0);
  util::initData(outputData, sizeof(outputData) / sizeof(outputData[0]), 0.0);

  bret = softmaxFloat32(inputData, inputShape, beta, outputData, outputShape);
  EXPECT_EQ(bret, true);

  float expectData[] = {0.032058603280085, 0.0871443187420326, 0.23688281808991, 0.643914259887972};
  bret = util::compareData(outputData, expectData, outputShape);
  EXPECT_EQ(bret, true);
}