2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <cker/train/operation/FullyConnected.h>
19 #include <gtest/gtest.h>
22 TEST(CKer_Operation, FullyConnectedBiasGrad)
26 std::vector<float> incoming_backward = {-1, 2, -3, 4, 5, -6, -7, 8};
28 std::vector<float> expected_bias_backward = {4, -4, -10, 12};
29 std::vector<float> bias_backward(4);
31 nnfw::cker::train::FullyConnectedBiasGrad(
32 nnfw::cker::Shape{2, 4}, incoming_backward.data(),
33 nnfw::cker::Shape{static_cast<int>(bias_backward.size())}, bias_backward.data());
35 for (size_t i = 0; i < bias_backward.size(); ++i)
36 ASSERT_EQ(bias_backward[i], expected_bias_backward[i]);
41 std::vector<float> incoming_backward = {-1, 2, -3, 4, 5, -6, -7, 8, 9};
43 std::vector<float> expected_bias_backward = {-4, 15, 0};
44 std::vector<float> bias_backward(3);
46 nnfw::cker::train::FullyConnectedBiasGrad(
47 nnfw::cker::Shape{3, 3}, incoming_backward.data(),
48 nnfw::cker::Shape{static_cast<int>(bias_backward.size())}, bias_backward.data());
50 for (size_t i = 0; i < bias_backward.size(); ++i)
51 ASSERT_EQ(bias_backward[i], expected_bias_backward[i]);
55 // Shape: {1, 2, 2, 3}
56 std::vector<float> incoming_backward = {-1, 2, -3, 4, 5, -6, -7, 8, 9, -10, -11, 12};
58 std::vector<float> expected_bias_backward = {-14, 4, 12};
59 std::vector<float> bias_backward(3);
61 nnfw::cker::train::FullyConnectedBiasGrad(
62 nnfw::cker::Shape{1, 2, 2, 3}, incoming_backward.data(),
63 nnfw::cker::Shape{static_cast<int>(bias_backward.size())}, bias_backward.data());
65 for (size_t i = 0; i < bias_backward.size(); ++i)
66 ASSERT_EQ(bias_backward[i], expected_bias_backward[i]);
70 TEST(CKer_Operation, neg_FullyConnectedBiasGrad)
75 std::vector<float> incoming_backward = {-1, 2, -3, 4, 5, -6, -7, 8};
77 std::vector<float> bias_backward(3);
78 EXPECT_ANY_THROW(nnfw::cker::train::FullyConnectedBiasGrad(
79 nnfw::cker::Shape{2, 4}, incoming_backward.data(),
80 nnfw::cker::Shape{static_cast<int>(bias_backward.size())},
81 bias_backward.data()););