diff --git a/compiler/circlechef/circle/src/CircleOpChefs.h b/compiler/circlechef/circle/src/CircleOpChefs.h index 93982417a33..702d063bb66 100644 --- a/compiler/circlechef/circle/src/CircleOpChefs.h +++ b/compiler/circlechef/circle/src/CircleOpChefs.h @@ -24,5 +24,6 @@ #include "Op/GRU.h" #include "Op/InstanceNorm.h" #include "Op/RmsNorm.h" +#include "Op/RoPE.h" #endif // __CIRCLE_OP_CHEFS_H__ diff --git a/compiler/circlechef/circle/src/CircleOpRegistry.h b/compiler/circlechef/circle/src/CircleOpRegistry.h index a6b730044e2..388a1d753c8 100644 --- a/compiler/circlechef/circle/src/CircleOpRegistry.h +++ b/compiler/circlechef/circle/src/CircleOpRegistry.h @@ -61,6 +61,7 @@ class CircleOpRegistry REG_TFL_OP(GRU, CircleOpGRU); REG_TFL_OP(INSTANCE_NORM, CircleOpInstanceNorm); REG_TFL_OP(RMS_NORM, CircleOpRmsNorm); + REG_TFL_OP(ROPE, CircleOpRoPE); #undef REG_TFL_OP } diff --git a/compiler/circlechef/circle/src/Op/RoPE.cpp b/compiler/circlechef/circle/src/Op/RoPE.cpp new file mode 100644 index 00000000000..67411a616ba --- /dev/null +++ b/compiler/circlechef/circle/src/Op/RoPE.cpp @@ -0,0 +1,52 @@ +/* + * 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 "RoPE.h" + +#include "Convert.h" + +namespace circlechef +{ + +void CircleOpRoPE::filler(const circle::Operator *op, CircleImport *import, + circlechef::ModelRecipe *model_recipe) const +{ + const std::vector &inputs = as_index_vector(op->inputs()); + + assert(inputs.size() == 3); + + import->set_tensor_filler(inputs[1]); + import->set_tensor_filler(inputs[2]); +} + +circlechef::Operation *CircleOpRoPE::build(const circle::Operator *op, CircleImport *import, + circlechef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("RoPE"); + + auto op_options = operation->mutable_rope_options(); + + auto op_params = op->builtin_options_as_RoPEOptions(); + assert(op_params != nullptr); + + op_options->set_mode(op_params->mode()); + + return operation; +} + +} // namespace circlechef diff --git a/compiler/circlechef/circle/src/Op/RoPE.h b/compiler/circlechef/circle/src/Op/RoPE.h new file mode 100644 index 00000000000..947b054c704 --- /dev/null +++ b/compiler/circlechef/circle/src/Op/RoPE.h @@ -0,0 +1,39 @@ +/* + * 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 __CIRCLE_OP_ROPE_H__ +#define __CIRCLE_OP_ROPE_H__ + +#include "CircleOpChef.h" + +namespace circlechef +{ + +/** + * @brief circlechef operator builder for RoPE + */ +class CircleOpRoPE : public CircleOpChef +{ +public: + void filler(const circle::Operator *op, CircleImport *import, + circlechef::ModelRecipe *model_recipe) const override; + circlechef::Operation *build(const circle::Operator *op, CircleImport *import, + circlechef::ModelRecipe *model_recipe) const override; +}; + +} // namespace circlechef + +#endif // __CIRCLE_OP_ROPE_H__ diff --git a/compiler/circlechef/core/src/Op/RoPE.cpp b/compiler/circlechef/core/src/Op/RoPE.cpp new file mode 100644 index 00000000000..916df763fe2 --- /dev/null +++ b/compiler/circlechef/core/src/Op/RoPE.cpp @@ -0,0 +1,35 @@ +/* + * 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 "RoPE.h" + +#include "Convert.h" + +flatbuffers::Offset RoPEChef::value(flatbuffers::FlatBufferBuilder &fbb) const +{ + auto &operation = (*_operation); + assert(operation.has_rope_options()); + + circle::RoPEOptionsBuilder options_builder{fbb}; + options_builder.add_mode(static_cast(operation.rope_options().mode())); + + return options_builder.Finish().Union(); +} + +std::unique_ptr RoPEChefFactory::create(const circlechef::Operation *operation) const +{ + return std::unique_ptr{new RoPEChef{operation}}; +} diff --git a/compiler/circlechef/core/src/Op/RoPE.h b/compiler/circlechef/core/src/Op/RoPE.h new file mode 100644 index 00000000000..33e4e4b0374 --- /dev/null +++ b/compiler/circlechef/core/src/Op/RoPE.h @@ -0,0 +1,46 @@ +/* + * 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 __OP_ROPE_H__ +#define __OP_ROPE_H__ + +#include "OpChef.h" + +class RoPEChef final : public OpChef +{ +public: + explicit RoPEChef(const circlechef::Operation *operation) : _operation{operation} + { + // DO NOTHING + } + +public: + circle::BuiltinOperator code(void) const override { return circle::BuiltinOperator_ROPE; } + + circle::BuiltinOptions type(void) const override { return circle::BuiltinOptions_RoPEOptions; } + + flatbuffers::Offset value(flatbuffers::FlatBufferBuilder &fbb) const override; + +private: + const circlechef::Operation *_operation; +}; + +struct RoPEChefFactory final : public OpChefFactory +{ + std::unique_ptr create(const circlechef::Operation *operation) const override; +}; + +#endif // __OP_ROPE_H__ diff --git a/compiler/circlechef/core/src/OpChef.def b/compiler/circlechef/core/src/OpChef.def index 2f0bae75d52..9d782b3c3f7 100644 --- a/compiler/circlechef/core/src/OpChef.def +++ b/compiler/circlechef/core/src/OpChef.def @@ -11,3 +11,4 @@ OP_CHEF(FullyConnected, FullyConnectedChefFactory) OP_CHEF(GRU, GRUChefFactory) OP_CHEF(InstanceNorm, InstanceNormChefFactory) OP_CHEF(RmsNorm, RmsNormChefFactory) +OP_CHEF(RoPE, RoPEChefFactory) diff --git a/compiler/circlechef/core/src/OpChefs.h b/compiler/circlechef/core/src/OpChefs.h index 4a0f463ba78..6ced8403ea0 100644 --- a/compiler/circlechef/core/src/OpChefs.h +++ b/compiler/circlechef/core/src/OpChefs.h @@ -24,5 +24,6 @@ #include "Op/GRU.h" #include "Op/InstanceNorm.h" #include "Op/RmsNorm.h" +#include "Op/RoPE.h" #endif // __OP_CHEFS_H__ diff --git a/compiler/circlechef/proto/circlechef.proto b/compiler/circlechef/proto/circlechef.proto index c1eef141553..4bdd1bef1b4 100644 --- a/compiler/circlechef/proto/circlechef.proto +++ b/compiler/circlechef/proto/circlechef.proto @@ -103,6 +103,10 @@ message RmsNormOptions { optional float epsilon = 1 [default = 1e-06]; } +message RoPEOptions { + optional int32 mode = 1 [default = 0];; +} + message Operation { optional string type = 1; repeated string input = 2; @@ -117,6 +121,7 @@ message Operation { optional GRUOptions gru_options = 104; optional FullyConnectedOptions fullyconnected_options = 105; optional RmsNormOptions rms_norm_options = 106; + optional RoPEOptions rope_options = 107; } // For additional subgraphs