-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[luci-interpreter] Support RoPE operation #14104
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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 "kernels/RoPE.h" | ||
|
||
#include "kernels/Utils.h" | ||
|
||
namespace luci_interpreter | ||
{ | ||
namespace kernels | ||
{ | ||
|
||
RoPE::RoPE(const Tensor *input, const Tensor *sin_table, const Tensor *cos_table, Tensor *output, | ||
const RoPEParams ¶ms) | ||
: KernelWithParams<RoPEParams>({input, sin_table, cos_table}, {output}, params) | ||
{ | ||
} | ||
|
||
void RoPE::configure() | ||
{ | ||
LUCI_INTERPRETER_CHECK(input()->shape().num_dims() == 4); | ||
LUCI_INTERPRETER_CHECK(sin_table()->shape().dim(3) == input()->shape().dim(3)); | ||
LUCI_INTERPRETER_CHECK(cos_table()->shape().dim(3) == input()->shape().dim(3)); | ||
|
||
LUCI_INTERPRETER_CHECK(params().mode == RoPEMode::GPT_NEOX); | ||
|
||
output()->resize(input()->shape()); | ||
} | ||
|
||
void RoPE::execute() const | ||
{ | ||
switch (input()->element_type()) | ||
{ | ||
case DataType::FLOAT32: | ||
evalFloat(); | ||
break; | ||
default: | ||
throw std::runtime_error("luci-rope Unsupported data type."); | ||
} | ||
} | ||
|
||
void RoPE::evalFloat() const | ||
{ | ||
const auto input_shape = getTensorShape(input()); | ||
const auto sin_table_shape = getTensorShape(sin_table()); | ||
const auto cos_table_shape = getTensorShape(cos_table()); | ||
auto output_shape = getTensorShape(output()); | ||
|
||
const float *input_data = getTensorData<float>(input()); | ||
const float *sin_table_data = getTensorData<float>(sin_table()); | ||
const float *cos_table_data = getTensorData<float>(cos_table()); | ||
float *output_data = getTensorData<float>(output()); | ||
|
||
if (params().mode == RoPEMode::GPT_NEOX) | ||
{ | ||
const int32_t i0_n = input_shape.Dims(0); | ||
const int32_t i1_n = input_shape.Dims(1); // multihead | ||
const int32_t i2_n = input_shape.Dims(2); | ||
const int32_t i3_n = input_shape.Dims(3); // head | ||
|
||
for (int32_t i0 = 0; i0 < i0_n; ++i0) | ||
{ | ||
for (int32_t i1 = 0; i1 < i1_n; ++i1) | ||
{ | ||
for (int32_t i2 = 0; i2 < i2_n; ++i2) | ||
{ | ||
for (int32_t i3 = 0; i3 < i3_n / 2; ++i3) | ||
{ | ||
const int32_t offset = tflite::Offset(input_shape, i0, i1, i2, i3); | ||
const float x0 = input_data[offset]; | ||
const float x1 = input_data[offset + i3_n / 2]; | ||
|
||
output_data[offset] = x0 * cos_table_data[i3] - x1 * sin_table_data[i3]; | ||
output_data[offset + i3_n / 2] = | ||
x0 * sin_table_data[i3 + i3_n / 2] + x1 * cos_table_data[i3 + i3_n / 2]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
else | ||
throw std::runtime_error("luci-intp RoPE unsupported mode."); | ||
} | ||
|
||
} // namespace kernels | ||
} // namespace luci_interpreter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#ifndef LUCI_INTERPRETER_KERNELS_ROPE_H | ||
#define LUCI_INTERPRETER_KERNELS_ROPE_H | ||
|
||
#include "core/Kernel.h" | ||
#include "core/KernelParams.h" | ||
|
||
namespace luci_interpreter | ||
{ | ||
namespace kernels | ||
{ | ||
|
||
class RoPE : public KernelWithParams<RoPEParams> | ||
{ | ||
public: | ||
RoPE(const Tensor *input, const Tensor *sin_table, const Tensor *cos_table, Tensor *output, | ||
const RoPEParams ¶ms); | ||
|
||
const Tensor *input() const { return _inputs[0]; } | ||
const Tensor *sin_table() const { return _inputs[1]; } | ||
const Tensor *cos_table() const { return _inputs[2]; } | ||
Tensor *output() const { return _outputs[0]; } | ||
|
||
void configure() override; | ||
void execute() const override; | ||
|
||
private: | ||
void evalFloat() const; | ||
}; | ||
|
||
} // namespace kernels | ||
} // namespace luci_interpreter | ||
|
||
#endif // LUCI_INTERPRETER_KERNELS_ROPE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* 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 "kernels/RoPE.h" | ||
#include "kernels/TestUtils.h" | ||
#include "luci_interpreter/TestMemoryManager.h" | ||
|
||
namespace luci_interpreter | ||
{ | ||
namespace kernels | ||
{ | ||
namespace | ||
{ | ||
|
||
using namespace testing; | ||
|
||
class RoPETest : public ::testing::Test | ||
{ | ||
protected: | ||
void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); } | ||
|
||
std::unique_ptr<IMemoryManager> _memory_manager; | ||
}; | ||
|
||
TEST_F(RoPETest, floatTest) | ||
{ | ||
Shape input_shape{1, 1, 1, 4}; | ||
std::vector<float> input_data{0, 1.0, 2.0, 3.0}; | ||
|
||
Shape sin_shape{1, 1, 1, 4}; | ||
std::vector<float> sin_data{0.5, 1.0, 1.0, 0.5}; | ||
|
||
Shape cos_shape{1, 1, 1, 4}; | ||
std::vector<float> cos_data{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}; | ||
|
||
Tensor input_tensor = | ||
makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get()); | ||
Tensor output_tensor = makeOutputTensor(DataType::FLOAT32); | ||
|
||
Tensor sin_table = makeInputTensor<DataType::FLOAT32>(sin_shape, sin_data, _memory_manager.get()); | ||
Tensor cos_table = makeInputTensor<DataType::FLOAT32>(cos_shape, cos_data, _memory_manager.get()); | ||
|
||
RoPEParams params{}; | ||
params.mode = RoPEMode::GPT_NEOX; | ||
|
||
RoPE kernel(&input_tensor, &sin_table, &cos_table, &output_tensor, params); | ||
kernel.configure(); | ||
_memory_manager->allocate_memory(output_tensor); | ||
kernel.execute(); | ||
|
||
EXPECT_THAT(extractTensorData<float>(output_tensor), | ||
::testing::ElementsAreArray(ref_output_data)); | ||
EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 1, 1, 4})); | ||
} | ||
|
||
TEST_F(RoPETest, Unsupported_dims_NEG) | ||
{ | ||
Shape input_shape{1, 1, 3}; | ||
std::vector<float> input_data{0, 1.0, 2.0}; | ||
|
||
Shape sin_shape{1, 1, 3}; | ||
std::vector<float> sin_data{0.5, 1.0, 1.0}; | ||
|
||
Shape cos_shape{1, 1, 3}; | ||
std::vector<float> cos_data{1.0, 0.5, 0.5}; | ||
|
||
Shape ref_output_shape{1, 1, 3}; | ||
std::vector<float> ref_output_data{-1.0, -2.5, 1.0}; | ||
|
||
Tensor input_tensor = | ||
makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get()); | ||
Tensor output_tensor = makeOutputTensor(DataType::FLOAT32); | ||
|
||
Tensor sin_table = makeInputTensor<DataType::FLOAT32>(sin_shape, sin_data, _memory_manager.get()); | ||
Tensor cos_table = makeInputTensor<DataType::FLOAT32>(cos_shape, cos_data, _memory_manager.get()); | ||
|
||
RoPEParams params{}; | ||
params.mode = RoPEMode::GPT_NEOX; | ||
|
||
RoPE kernel(&input_tensor, &sin_table, &cos_table, &output_tensor, params); | ||
EXPECT_ANY_THROW(kernel.configure()); | ||
} | ||
|
||
TEST_F(RoPETest, Unsupported_mode_NEG) | ||
{ | ||
Shape input_shape{1, 1, 1, 4}; | ||
std::vector<float> input_data{0, 1.0, 2.0, 3.0}; | ||
|
||
Shape sin_shape{1, 1, 1, 4}; | ||
std::vector<float> sin_data{0.5, 1.0, 1.0, 0.5}; | ||
|
||
Shape cos_shape{1, 1, 1, 4}; | ||
std::vector<float> cos_data{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}; | ||
|
||
Tensor input_tensor = | ||
makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get()); | ||
Tensor output_tensor = makeOutputTensor(DataType::FLOAT32); | ||
|
||
Tensor sin_table = makeInputTensor<DataType::FLOAT32>(sin_shape, sin_data, _memory_manager.get()); | ||
Tensor cos_table = makeInputTensor<DataType::FLOAT32>(cos_shape, cos_data, _memory_manager.get()); | ||
|
||
RoPEParams params{}; | ||
params.mode = RoPEMode::GPT_J; | ||
|
||
RoPE kernel(&input_tensor, &sin_table, &cos_table, &output_tensor, params); | ||
EXPECT_ANY_THROW(kernel.configure()); | ||
} | ||
|
||
TEST_F(RoPETest, Invalid_input_sin_table_NEG) | ||
{ | ||
Shape input_shape{1, 1, 1, 4}; | ||
std::vector<float> input_data{0, 1.0, 2.0, 3.0}; | ||
|
||
Shape sin_shape{1, 1, 1, 3}; | ||
std::vector<float> sin_data{0.5, 1.0, 1.0}; | ||
|
||
Shape cos_shape{1, 1, 1, 4}; | ||
std::vector<float> cos_data{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}; | ||
|
||
Tensor input_tensor = | ||
makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get()); | ||
Tensor output_tensor = makeOutputTensor(DataType::FLOAT32); | ||
|
||
Tensor sin_table = makeInputTensor<DataType::FLOAT32>(sin_shape, sin_data, _memory_manager.get()); | ||
Tensor cos_table = makeInputTensor<DataType::FLOAT32>(cos_shape, cos_data, _memory_manager.get()); | ||
|
||
RoPEParams params{}; | ||
params.mode = RoPEMode::GPT_NEOX; | ||
|
||
RoPE kernel(&input_tensor, &sin_table, &cos_table, &output_tensor, params); | ||
EXPECT_ANY_THROW(kernel.configure()); | ||
} | ||
|
||
TEST_F(RoPETest, Invalid_input_cos_table_NEG) | ||
{ | ||
Shape input_shape{1, 1, 1, 4}; | ||
std::vector<float> input_data{0, 1.0, 2.0, 3.0}; | ||
|
||
Shape sin_shape{1, 1, 1, 4}; | ||
std::vector<float> sin_data{0.5, 1.0, 1.0, 0.5}; | ||
|
||
Shape cos_shape{1, 1, 1, 3}; | ||
std::vector<float> cos_data{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}; | ||
|
||
Tensor input_tensor = | ||
makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get()); | ||
Tensor output_tensor = makeOutputTensor(DataType::FLOAT32); | ||
|
||
Tensor sin_table = makeInputTensor<DataType::FLOAT32>(sin_shape, sin_data, _memory_manager.get()); | ||
Tensor cos_table = makeInputTensor<DataType::FLOAT32>(cos_shape, cos_data, _memory_manager.get()); | ||
|
||
RoPEParams params{}; | ||
params.mode = RoPEMode::GPT_NEOX; | ||
|
||
RoPE kernel(&input_tensor, &sin_table, &cos_table, &output_tensor, params); | ||
EXPECT_ANY_THROW(kernel.configure()); | ||
} | ||
|
||
} // namespace | ||
} // namespace kernels | ||
} // namespace luci_interpreter |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from seanshpark,
the head size (dim(3)) of input and the size of sin/cos should be the same.
so, it is necessary to check the condition here.