Skip to content

Commit

Permalink
[circlechef] support RoPE Operation
Browse files Browse the repository at this point in the history
this commits support RoPE for circle chef

ONE-DCO-1.0-Signed-off-by: youngsik kim <[email protected]>

draft : Samsung#13978
issue : Samsung#13972
  • Loading branch information
ys44kim committed Sep 13, 2024
1 parent 7233b7c commit f794266
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/circlechef/circle/src/CircleOpChefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
#include "Op/GRU.h"
#include "Op/InstanceNorm.h"
#include "Op/RmsNorm.h"

#include "Op/RoPE.h"
#endif // __CIRCLE_OP_CHEFS_H__
1 change: 1 addition & 0 deletions compiler/circlechef/circle/src/CircleOpRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
50 changes: 50 additions & 0 deletions compiler/circlechef/circle/src/Op/RoPE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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<int32_t> &inputs = as_index_vector(op->inputs());

// To Do: input parameters
assert(inputs.size() == 3);
}

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
39 changes: 39 additions & 0 deletions compiler/circlechef/circle/src/Op/RoPE.h
Original file line number Diff line number Diff line change
@@ -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__
36 changes: 36 additions & 0 deletions compiler/circlechef/core/src/Op/RoPE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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<void> 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<circle::RoPEMode>(operation.rope_options().mode()));

return options_builder.Finish().Union();
}

std::unique_ptr<OpChef>
RoPEChefFactory::create(const circlechef::Operation *operation) const
{
return std::unique_ptr<OpChef>{new RoPEChef{operation}};
}
52 changes: 52 additions & 0 deletions compiler/circlechef/core/src/Op/RoPE.h
Original file line number Diff line number Diff line change
@@ -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.
*/

#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<void> value(flatbuffers::FlatBufferBuilder &fbb) const override;

private:
const circlechef::Operation *_operation;
};

struct RoPEChefFactory final : public OpChefFactory
{
std::unique_ptr<OpChef> create(const circlechef::Operation *operation) const override;
};

#endif // __OP_ROPE_H__
1 change: 1 addition & 0 deletions compiler/circlechef/core/src/OpChef.def
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ OP_CHEF(FullyConnected, FullyConnectedChefFactory)
OP_CHEF(GRU, GRUChefFactory)
OP_CHEF(InstanceNorm, InstanceNormChefFactory)
OP_CHEF(RmsNorm, RmsNormChefFactory)
OP_CHEF(RoPE, RoPEChefFactory)
1 change: 1 addition & 0 deletions compiler/circlechef/core/src/OpChefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
#include "Op/GRU.h"
#include "Op/InstanceNorm.h"
#include "Op/RmsNorm.h"
#include "Op/RoPE.h"

#endif // __OP_CHEFS_H__
5 changes: 5 additions & 0 deletions compiler/circlechef/proto/circlechef.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit f794266

Please sign in to comment.