Skip to content
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

[Draft] Support RoPE Op and fuse #13978

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/circle2circle-dredd-recipe-test/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Add(PadV2_001 PASS substitute_padv2_to_pad)
Add(Softmax_001 PASS decompose_softmax)
Add(Softmax_002 PASS decompose_softmax)
Add(StridedSlice_003 PASS substitute_strided_slice_to_reshape)
Add(Net_RoPE_000 PASS fuse_rope)

# CSE test

Expand Down
2 changes: 2 additions & 0 deletions compiler/circle2circle/src/Circle2Circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ int entry(int argc, char **argv)
add_switch(arser, "--disable_validation",
"This will turn off operator validations. May help input model investigation.");
add_switch(arser, "--generate_profile_data", "This will turn on profiling data generation.");
add_switch(arser, "--fuse_rope", "This will fuse operators to rope operator");

// NOTE Experimental options; these will be removed someday
// Add experimental options here
Expand Down Expand Up @@ -343,6 +344,7 @@ int entry(int argc, char **argv)
option_str_to_enum["decompose_softmax"] = Algorithms::DecomposeSoftmaxPass;
option_str_to_enum["expand_broadcast_const"] = Algorithms::ExpandBroadcastConst;
option_str_to_enum["unroll_unidirseqlstm"] = Algorithms::UnrollUnidirSeqLSTM;
option_str_to_enum["fuse_rope"] = Algorithms::FuseRoPE;
// clang-format on

if (arser.get<bool>("--verbose"))
Expand Down
2 changes: 1 addition & 1 deletion compiler/circlechef/circle/src/CircleOpChefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
#include "Op/BCQGather.h"
#include "Op/GRU.h"
#include "Op/InstanceNorm.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 @@ -60,6 +60,7 @@ class CircleOpRegistry
REG_TFL_OP(BCQ_GATHER, CircleOpBCQGather);
REG_TFL_OP(GRU, CircleOpGRU);
REG_TFL_OP(INSTANCE_NORM, CircleOpInstanceNorm);
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 @@ -10,3 +10,4 @@ OP_CHEF(BCQGather, BCQGatherChefFactory)
OP_CHEF(FullyConnected, FullyConnectedChefFactory)
OP_CHEF(GRU, GRUChefFactory)
OP_CHEF(InstanceNorm, InstanceNormChefFactory)
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 @@ -23,5 +23,6 @@
#include "Op/FullyConnected.h"
#include "Op/GRU.h"
#include "Op/InstanceNorm.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 @@ -99,6 +99,10 @@ message BCQGatherOptions {
optional int32 axis = 2 [default = 0];
}

message RoPEOptions {
optional int32 mode = 1 [default = 0];;
}

message Operation {
optional string type = 1;
repeated string input = 2;
Expand All @@ -112,6 +116,7 @@ message Operation {
optional BCQGatherOptions bcq_gather_options = 103;
optional GRUOptions gru_options = 104;
optional FullyConnectedOptions fullyconnected_options = 105;
optional RoPEOptions rope_options = 106;
}

// For additional subgraphs
Expand Down
15 changes: 15 additions & 0 deletions compiler/circledump/src/OpPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,20 @@ class InstanceNormPrinter : public OpPrinter
}
};

class RoPEPrinter : public OpPrinter
{
public:
void options(const circle::Operator *op, std::ostream &os) const override
{
if (auto *params = op->builtin_options_as_RoPEOptions())
{
os << " ";
os << "mode(" << EnumNameRoPEMode(params->mode()) << ") ";
os << std::endl;
}
}
};

OpPrinterRegistry::OpPrinterRegistry()
{
_op_map[circle::BuiltinOperator_ADD] = make_unique<AddPrinter>();
Expand Down Expand Up @@ -912,6 +926,7 @@ OpPrinterRegistry::OpPrinterRegistry()
_op_map[circle::BuiltinOperator_BCQ_GATHER] = make_unique<BCQGatherPrinter>();
_op_map[circle::BuiltinOperator_GRU] = make_unique<GRUPrinter>();
_op_map[circle::BuiltinOperator_INSTANCE_NORM] = make_unique<InstanceNormPrinter>();
_op_map[circle::BuiltinOperator_ROPE] = make_unique<RoPEPrinter>();
}

} // namespace circledump
1 change: 1 addition & 0 deletions compiler/common-artifacts/exclude.lst
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,4 @@ tcgenerate(CircleFullyConnected_U4_002)
tcgenerate(GRU_000) # luci-interpreter does not support custom GRU
tcgenerate(InstanceNorm_000)
tcgenerate(InstanceNorm_001)
tcgenerate(RoPE_000)
1 change: 1 addition & 0 deletions compiler/luci-interpreter/pal/linux/KernelsToBuild.lst
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ REGISTER_KERNEL(TransposeConv)
REGISTER_KERNEL(UnidirectionalSequenceLSTM)
REGISTER_KERNEL(Unpack)
REGISTER_KERNEL(While)
REGISTER_KERNEL(RoPE)
1 change: 1 addition & 0 deletions compiler/luci-interpreter/pal/mcu/KernelsToBuild.lst
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ REGISTER_KERNEL(Tanh)
REGISTER_KERNEL(Transpose)
REGISTER_KERNEL(TransposeConv)
REGISTER_KERNEL(While)
REGISTER_KERNEL(RoPE)
7 changes: 7 additions & 0 deletions compiler/luci-interpreter/src/core/KernelParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <luci/IR/AttrPadding.h>
#include <luci/IR/AttrFusedActFunc.h>
#include <luci/IR/AttrMirrorPadMode.h>
#include <luci/IR/AttrRoPEMode.h>
#include <luci_interpreter/core/DataType.h>

#include <cstdint>
Expand All @@ -32,6 +33,7 @@ namespace luci_interpreter
using Activation = luci::FusedActFunc;
using Padding = luci::Padding;
using MirrorPadMode = luci::MirrorPadMode;
using RoPEMode = luci::RoPEMode;

struct AddParams
{
Expand Down Expand Up @@ -115,6 +117,11 @@ struct InstanceNormParams
Activation activation;
};

struct RoPEParams
{
RoPEMode mode;
};

struct L2NormParams
{
Activation activation;
Expand Down
Loading