Skip to content

Commit

Permalink
[onert] Add cker test for RoPE
Browse files Browse the repository at this point in the history
This commit adds cker test for RoPE

ONE-DCO-1.0-Signed-off-by: youngsik kim <ys44.kim@samsung.com>
  • Loading branch information
ys44kim committed Oct 2, 2024
1 parent 1c704e7 commit 12a419d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
13 changes: 10 additions & 3 deletions compute/cker/include/cker/operation/RoPE.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,25 @@
#include "cker/Types.h"
#include "cker/Utils.h"

#include <cmath>

namespace nnfw
{
namespace cker
{

template <typename T>
inline void RoPE(const RoPEMode mode, const Shape &input_shape, const T *input_data,
const T *sin_table_data, const T *cos_table_data, const Shape &output_shape,
const Shape &sin_table_shape, const T *sin_table_data,
const Shape &cos_table_shape, const T *cos_table_data, const Shape &output_shape,
T *output_data)
{
assert(input_shape.DimensionsCount() == 4);
assert(input_shape.DimensionsCount() == output_shape.DimensionsCount());
assert(input_shape.Dims(3) == sin_table_shape.Dims(3));
assert(input_shape.Dims(3) == cos_table_shape.Dims(3));

UNUSED_RELEASE(sin_table_shape);
UNUSED_RELEASE(cos_table_shape);

const int32_t i0_n = MatchingDim(input_shape, 0, output_shape, 0);
const int32_t i1_n = MatchingDim(input_shape, 1, output_shape, 1);
const int32_t i2_n = MatchingDim(input_shape, 2, output_shape, 2);
Expand Down
79 changes: 79 additions & 0 deletions compute/cker/src/RoPE.test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2024 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 "cker/Shape.h"
#include <cker/operation/RoPE.h>

#include <gtest/gtest.h>
#include <vector>

using nnfw::cker::Shape;
using nnfw::cker::RoPEMode;

TEST(CKer_Operation, RoPE)
{
// Simple
{
RoPEMode mode = RoPEMode::kGptNeox;

Shape input_shape{1, 1, 1, 4};
std::vector<float> input{0, 1.0, 2.0, 3.0};

Shape sin_table_shape{1, 1, 1, 4};
std::vector<float> sin_table{0.5, 1.0, 1.0, 0.5};
Shape cos_table_shape{1, 1, 1, 4};
std::vector<float> cos_table{1.0, 0.5, 0.5, 1.0};

Shape ref_output_shape{1, 1, 1, 4};
std::vector<float> ref_output_data{-1.0, -2.5, 1.0, 3.5};

Shape output_shape{1, 1, 1, 4};
std::vector<float> output(ref_output_data.size());

nnfw::cker::RoPE<float>(mode, input_shape, input.data(), sin_table_shape, sin_table.data(),
cos_table_shape, cos_table.data(), ref_output_shape, output.data());

for (size_t i = 0; i < ref_output_data.size(); ++i)
{
EXPECT_FLOAT_EQ(ref_output_data[i], output[i]); // Same as input
}
}
}

TEST(CKer_Operation, neg_RoPE)
{
// input, sin_table, cos_table size mismatch
{
RoPEMode mode = RoPEMode::kGptNeox;

Shape input_shape{1, 1, 1, 4};
std::vector<float> input{0, 1.0, 2.0, 3.0};

Shape sin_table_shape{1, 1, 1, 3};
std::vector<float> sin_table{0.5, 1.0, 1.0};
Shape cos_table_shape{1, 1, 1, 3};
std::vector<float> cos_table{1.0, 0.5, 0.5};

Shape ref_output_shape{1, 1, 1, 4};
std::vector<float> ref_output_data{-1.0, -2.5, 1.0, 3.5};

std::vector<float> output(ref_output_data.size());
Shape output_shape{1, 1, 1, 4};

EXPECT_ANY_THROW(nnfw::cker::RoPE<float>(mode, input_shape, input.data(), sin_table_shape,
sin_table.data(), cos_table_shape, cos_table.data(),
ref_output_shape, output.data()));
}
}
6 changes: 3 additions & 3 deletions runtime/onert/backend/cpu/ops/RoPELayer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ template <typename T> void RoPELayer::rope()
{
auto input_shape = _input->getShape();
assert(input_shape.rank() == 4);
assert(_mode == nnfw::cker::RoPEMode::kGptNeox);

nnfw::cker::RoPE(_mode, getShape(_input), getBuffer<T>(_input), getBuffer<T>(_sin),
getBuffer<T>(_cos), getShape(_output), getBuffer<T>(_output));
nnfw::cker::RoPE(_mode, getShape(_input), getBuffer<T>(_input), getShape(_sin),
getBuffer<T>(_sin), getShape(_cos), getBuffer<T>(_cos), getShape(_output),
getBuffer<T>(_output));
}

void RoPELayer::run()
Expand Down

0 comments on commit 12a419d

Please sign in to comment.