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 CircleGRU #12319

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
3 changes: 3 additions & 0 deletions compiler/circle2circle/src/Circle2Circle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ int entry(int argc, char **argv)
"This will fuse BatchNorm operators of pre-activations to Convolution operator");
add_switch(arser, "--fuse_prelu", "This will fuse operators to PReLU operator");
add_switch(arser, "--fuse_gelu", "This will fuse operators to GeLU operator");
add_switch(arser, "--fuse_gru", "This will fuse operators to CirGru operator");
add_switch(arser, "--remove_duplicate_const", "This will remove all duplicate constant nodes");
add_switch(arser, "--remove_fakequant", "This will remove FakeQuant operators");
add_switch(arser, "--remove_quantdequant", "This will remove Quantize-Dequantize sequence");
Expand Down Expand Up @@ -306,6 +307,8 @@ int entry(int argc, char **argv)
options->enable(Algorithms::FusePRelu);
if (arser.get<bool>("--fuse_gelu"))
options->enable(Algorithms::FuseGelu);
if (arser.get<bool>("--fuse_gru"))
options->enable(Algorithms::FuseCirGru);
if (arser.get<bool>("--fuse_transpose_with_mean"))
options->enable(Algorithms::FuseTransposeWithMean);
if (arser.get<bool>("--remove_duplicate_const"))
Expand Down
1 change: 1 addition & 0 deletions compiler/circlechef/circle/src/CircleOpChefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Op/BatchMatMul.h"
#include "Op/BCQFullyConnected.h"
#include "Op/BCQGather.h"
#include "Op/CirGru.h"
#include "Op/InstanceNorm.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 @@ -58,6 +58,7 @@ class CircleOpRegistry
REG_TFL_OP(BATCH_MATMUL, CircleOpBatchMatMul);
REG_TFL_OP(BCQ_FULLY_CONNECTED, CircleOpBCQFullyConnected);
REG_TFL_OP(BCQ_GATHER, CircleOpBCQGather);
REG_TFL_OP(CIR_GRU, CircleOpCirGru);
REG_TFL_OP(INSTANCE_NORM, CircleOpInstanceNorm);
#undef REG_TFL_OP
}
Expand Down
57 changes: 57 additions & 0 deletions compiler/circlechef/circle/src/Op/CirGru.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2023 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 "CirGru.h"

#include "Convert.h"

namespace circlechef
{

void CircleOpCirGru::filler(const circle::Operator *op, CircleImport *import,
circlechef::ModelRecipe *model_recipe) const
{
// index 1, 2, 3, 4, 5 maybe constant
const std::vector<int32_t> &inputs = as_index_vector(op->inputs());
assert(inputs.size() == 6);

import->set_tensor_filler(inputs[1]); // set gaussian filler
import->set_tensor_filler(inputs[2]);
import->set_tensor_filler(inputs[3]);
import->set_tensor_filler(inputs[4]);
import->set_tensor_filler(inputs[5]);
}

circlechef::Operation *CircleOpCirGru::build(const circle::Operator *op, CircleImport *import,
circlechef::ModelRecipe *model_recipe) const
{
auto op_params = op->builtin_options_as_CirGruOptions();
assert(op_params != nullptr);

auto operation = model_recipe->add_operation();

operation->set_type("CirGru");

auto op_options = operation->mutable_circle_gru_options();

op_options->set_activation(as_circlechef_activation(op_params->fused_activation_function()));
op_options->set_return_sequences(op_params->return_sequences());
op_options->set_time_major(op_params->time_major());

return operation;
}

} // namespace circlechef
39 changes: 39 additions & 0 deletions compiler/circlechef/circle/src/Op/CirGru.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 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_CIRCLE_GRU_H__
#define __CIRCLE_OP_CIRCLE_GRU_H__

#include "CircleOpChef.h"

namespace circlechef
{

/**
* @brief circlechef operator builder for CirGru
*/
class CircleOpCirGru : 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_CIRCLE_GRU_H__
41 changes: 41 additions & 0 deletions compiler/circlechef/core/src/Op/CirGru.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 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 "CirGru.h"

#include "Convert.h"

flatbuffers::Offset<void> CirGruChef::value(flatbuffers::FlatBufferBuilder &fbb) const
{
auto &operation = (*_operation);

assert(operation.has_circle_gru_options());
auto circle_activation = as_circle_activation(operation.circle_gru_options().activation());
auto return_sequences = operation.circle_gru_options().return_sequences();
auto time_major = operation.circle_gru_options().time_major();

circle::CirGruOptionsBuilder options_builder{fbb};
options_builder.add_fused_activation_function(circle_activation);
options_builder.add_return_sequences(return_sequences);
options_builder.add_time_major(time_major);

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

std::unique_ptr<OpChef> CirGruChefFactory::create(const circlechef::Operation *operation) const
{
return std::unique_ptr<OpChef>{new CirGruChef{operation}};
}
46 changes: 46 additions & 0 deletions compiler/circlechef/core/src/Op/CirGru.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 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_CIRCLE_GRU_H__
#define __OP_CIRCLE_GRU_H__

#include "OpChef.h"

class CirGruChef final : public OpChef
{
public:
explicit CirGruChef(const circlechef::Operation *operation) : _operation{operation}
{
// DO NOTHING
}

public:
circle::BuiltinOperator code(void) const override { return circle::BuiltinOperator_CIR_GRU; }

circle::BuiltinOptions type(void) const override { return circle::BuiltinOptions_CirGruOptions; }

flatbuffers::Offset<void> value(flatbuffers::FlatBufferBuilder &fbb) const override;

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

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

#endif // __OP_CIRCLE_GRU_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 @@ -7,4 +7,5 @@
OP_CHEF(BatchMatMul, BatchMatMulChefFactory)
OP_CHEF(BCQFullyConnected, BCQFullyConnectedChefFactory)
OP_CHEF(BCQGather, BCQGatherChefFactory)
OP_CHEF(CirGru, CirGruChefFactory)
OP_CHEF(InstanceNorm, InstanceNormChefFactory)
1 change: 1 addition & 0 deletions compiler/circlechef/core/src/OpChefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Op/BatchMatMul.h"
#include "Op/BCQFullyConnected.h"
#include "Op/BCQGather.h"
#include "Op/CirGru.h"
#include "Op/InstanceNorm.h"

#endif // __OP_CHEFS_H__
7 changes: 7 additions & 0 deletions compiler/circlechef/proto/circlechef.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ message InstanceNormOptions {
optional Activation activation = 2 [default = NONE];
}

message CirGruOptions {
optional Activation activation = 1 [default = NONE];
optional bool return_sequences = 2 [default = false];
optional bool time_major = 3 [default = false];
}

message BCQFullyConnectedOptions {
optional int32 weights_hidden_size = 1 [default = 0];
optional Activation activation = 2 [default = NONE];
Expand All @@ -97,6 +103,7 @@ message Operation {
optional InstanceNormOptions instance_norm_options = 101;
optional BCQFullyConnectedOptions bcq_fully_connected_options = 102;
optional BCQGatherOptions bcq_gather_options = 103;
optional CirGruOptions circle_gru_options = 104;
}

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

class CirGruPrinter : public OpPrinter
{
public:
void options(const circle::Operator *op, std::ostream &os) const override
{
if (auto *params = op->builtin_options_as_CirGruOptions())
{
os << " ";
os << "Activation(" << EnumNameActivationFunctionType(params->fused_activation_function())
<< ") ";
os << "return_sequences(" << params->return_sequences() << ") ";
os << "time_major(" << params->time_major() << ") ";

os << std::endl;
}
}
};

OpPrinterRegistry::OpPrinterRegistry()
{
_op_map[circle::BuiltinOperator_ADD] = make_unique<AddPrinter>();
Expand All @@ -832,6 +850,7 @@ OpPrinterRegistry::OpPrinterRegistry()
_op_map[circle::BuiltinOperator_FULLY_CONNECTED] = make_unique<FullyConnectedPrinter>();
_op_map[circle::BuiltinOperator_GATHER] = make_unique<GatherPrinter>();
_op_map[circle::BuiltinOperator_GELU] = make_unique<GeluPrinter>();
_op_map[circle::BuiltinOperator_GELU] = make_unique<GeluPrinter>();
_op_map[circle::BuiltinOperator_IF] = make_unique<IfPrinter>();
_op_map[circle::BuiltinOperator_L2_NORMALIZATION] = make_unique<L2NormPrinter>();
_op_map[circle::BuiltinOperator_L2_POOL_2D] = make_unique<Pool2DPrinter>();
Expand Down Expand Up @@ -892,6 +911,7 @@ OpPrinterRegistry::OpPrinterRegistry()
_op_map[circle::BuiltinOperator_BCQ_FULLY_CONNECTED] = make_unique<BCQFullyConnectedPrinter>();
_op_map[circle::BuiltinOperator_BCQ_GATHER] = make_unique<BCQGatherPrinter>();
_op_map[circle::BuiltinOperator_INSTANCE_NORM] = make_unique<InstanceNormPrinter>();
_op_map[circle::BuiltinOperator_CIR_GRU] = make_unique<CirGruPrinter>();
}

} // namespace circledump
2 changes: 2 additions & 0 deletions compiler/common-artifacts/exclude.lst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ tcgenerate(Neg_000)
tcgenerate(Net_BroadcastTo_AddV2_001) # luci-interpreter doesn't support custom operator
tcgenerate(Net_Conv_FakeQuant_000) # luci-interpreter doesn't support FakeQuant yet
tcgenerate(Net_Dangle_001)
tcgenerate(Net_DecomposedGru_000)
tcgenerate(Net_Densify_Add_000) # luci-interpreter doesn't support Densify yet
tcgenerate(Net_Densify_Dequantize_Add_000) # luci-interpreter doesn't support Densify/Dequantize yet
tcgenerate(Net_FC_Gelu_FC_000) # luci-interpreter doesn't support custom operator Erf
Expand Down Expand Up @@ -165,5 +166,6 @@ tcgenerate(ZerosLike_000)
tcgenerate(BCQFullyConnected_000)
tcgenerate(BCQFullyConnected_001)
tcgenerate(BCQGather_000)
tcgenerate(CirGru_000) # luci-interpreter does not support custom CirGru
tcgenerate(InstanceNorm_000)
tcgenerate(InstanceNorm_001)
6 changes: 6 additions & 0 deletions compiler/luci/export/src/CircleBuiltinTypesExtractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ class BuiltinOptionsExtractor final
to_circle_actfunc(node->fusedActivationFunction()))
.Union();
}
flatbuffers::Offset<void> visit(luci::CircleCirGru *node)
{
return circle::CreateCirGruOptions(_builder, to_circle_actfunc(node->fusedActivationFunction()),
node->returnSequences(), node->timeMajor())
.Union();
}

protected:
flatbuffers::FlatBufferBuilder &_builder;
Expand Down
1 change: 1 addition & 0 deletions compiler/luci/export/src/CircleOps.lst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ CIRCLE_NODE(CircleZerosLike, BuiltinOperator_ZEROS_LIKE, BuiltinOptions_ZerosLik
CIRCLE_NODE(CircleBCQFullyConnected, BuiltinOperator_BCQ_FULLY_CONNECTED, BuiltinOptions_BCQFullyConnectedOptions)
CIRCLE_NODE(CircleBCQGather, BuiltinOperator_BCQ_GATHER, BuiltinOptions_BCQGatherOptions)
CIRCLE_NODE(CircleInstanceNorm, BuiltinOperator_INSTANCE_NORM, BuiltinOptions_InstanceNormOptions)
CIRCLE_NODE(CircleCirGru, BuiltinOperator_CIR_GRU, BuiltinOptions_CirGruOptions)
// Virtual node(s)
CIRCLE_VNODE(CircleBidirectionalSequenceLSTMOut)
CIRCLE_VNODE(CircleConst)
Expand Down
1 change: 1 addition & 0 deletions compiler/luci/import/include/luci/Import/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "Nodes/CircleGelu.h"
#include "Nodes/CircleGreater.h"
#include "Nodes/CircleGreaterEqual.h"
#include "Nodes/CircleCirGru.h"
#include "Nodes/CircleHardSwish.h"
#include "Nodes/CircleIf.h"
#include "Nodes/CircleInstanceNorm.h"
Expand Down
37 changes: 37 additions & 0 deletions compiler/luci/import/include/luci/Import/Nodes/CircleCirGru.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 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_IMPORT_OP_CIRCLE_GRU_H__
#define __LUCI_IMPORT_OP_CIRCLE_GRU_H__

#include "luci/Import/GraphBuilder.h"

namespace luci
{

class CircleCirGruGraphBuilder : public GraphBuilder
{
public:
bool validate(const ValidateArgs &args) const final;

private:
CircleNode *build_node(const circle::OperatorT &op, const std::vector<CircleNode *> &inputs,
loco::Graph *graph) const final;
};

} // namespace luci

#endif // __LUCI_IMPORT_OP_CIRCLE_GRU_H__
1 change: 1 addition & 0 deletions compiler/luci/import/src/GraphBuilderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ GraphBuilderRegistry::GraphBuilderRegistry()
CIRCLE_NODE(GREATER, CircleGreaterGraphBuilder); // 61
CIRCLE_NODE(GREATER_EQUAL, CircleGreaterEqualGraphBuilder); // 62
CIRCLE_NODE(HARD_SWISH, CircleHardSwishGraphBuilder); // 117
CIRCLE_NODE(CIR_GRU, CircleCirGruGraphBuilder); // 251
CIRCLE_NODE(IF, CircleIfGraphBuilder); // 118
CIRCLE_NODE(INSTANCE_NORM, CircleInstanceNormGraphBuilder); // 254
CIRCLE_NODE(L2_NORMALIZATION, CircleL2NormalizeGraphBuilder); // 11
Expand Down
Loading
Loading