ONE - On-device Neural Engine
Loading...
Searching...
No Matches
RoPE.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef __NNFW_CKER_ROPE_H__
18#define __NNFW_CKER_ROPE_H__
19
20#include <stdexcept>
21
22#include "cker/Shape.h"
23#include "cker/Types.h"
24#include "cker/Utils.h"
25
26namespace nnfw
27{
28namespace cker
29{
30
31template <typename T>
32inline void RoPE(const RoPEMode mode, const Shape &input_shape, const T *input_data,
33 const Shape &sin_table_shape, const T *sin_table_data,
34 const Shape &cos_table_shape, const T *cos_table_data, const Shape &output_shape,
35 T *output_data)
36{
37 if (input_shape.Dims(3) != sin_table_shape.Dims(3))
38 throw std::runtime_error("the dimension(3) of input and sin_table do not match");
39
40 if (input_shape.Dims(3) != cos_table_shape.Dims(3))
41 throw std::runtime_error("the dimension(3) of input and cos_table do not match");
42
43 const int32_t i0_n = MatchingDim(input_shape, 0, output_shape, 0);
44 const int32_t i1_n = MatchingDim(input_shape, 1, output_shape, 1);
45 const int32_t i2_n = MatchingDim(input_shape, 2, output_shape, 2);
46 const int32_t i3_n = MatchingDim(input_shape, 3, output_shape, 3);
47
48 if (i3_n % 2 != 0)
49 throw std::runtime_error("i3_n must be even number");
50
51 if (mode == RoPEMode::kGptNeox)
52 {
53 for (int32_t i0 = 0; i0 < i0_n; ++i0)
54 {
55 for (int32_t i1 = 0; i1 < i1_n; ++i1)
56 {
57 for (int32_t i2 = 0; i2 < i2_n; ++i2)
58 {
59 for (int32_t i3 = 0; i3 < i3_n / 2; ++i3)
60 {
61 const int32_t offset = Offset(input_shape, i0, i1, i2, i3);
62 const T x0 = input_data[offset];
63 const T x1 = input_data[offset + i3_n / 2];
64
65 output_data[offset] = x0 * cos_table_data[i3] - x1 * sin_table_data[i3];
66 output_data[offset + i3_n / 2] =
67 x0 * sin_table_data[i3 + i3_n / 2] + x1 * cos_table_data[i3 + i3_n / 2];
68 }
69 }
70 }
71 }
72 }
73 else
74 {
75 throw std::runtime_error("Unsupported RoPE mode");
76 }
77}
78
79} // namespace cker
80} // namespace nnfw
81
82#endif // __NNFW_CKER_ROPE_H__
int32_t Dims(int i) const
Definition Shape.h:92
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
const luci_interpreter::RuntimeShape output_shape
int MatchingDim(const Shape &shape1, int index1, const Shape &shape2, int index2)
Definition Shape.h:220
int Offset(const Shape &shape, int i0, int i1, int i2, int i3)
Definition Shape.h:237
void RoPE(const RoPEMode mode, const Shape &input_shape, const T *input_data, 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)
Definition RoPE.h:32
Definition topk_v2.h:30