Skip to content

Commit

Permalink
add circlechef and circledump
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Balyshev committed Dec 19, 2023
1 parent 586baac commit 29060f0
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 2 deletions.
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/CircleGRU.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, CircleOpCircleGRU);
REG_TFL_OP(INSTANCE_NORM, CircleOpInstanceNorm);
#undef REG_TFL_OP
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/circlechef/circle/src/Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ circlechef::Activation as_circlechef_activation(const circle::ActivationFunction
return circlechef::RELU;
case circle::ActivationFunctionType_RELU6:
return circlechef::RELU6;
case circle::ActivationFunctionType_TANH:
return circlechef::TANH;
// TODO handle other types
// ActivationFunctionType_RELU_N1_TO_1
// ActivationFunctionType_TANH
// ActivationFunctionType_SIGN_BIT
default:
throw std::runtime_error{"unsupported activation type"};
Expand Down
55 changes: 55 additions & 0 deletions compiler/circlechef/circle/src/Op/CircleGRU.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 "CircleGRU.h"

#include "Convert.h"

namespace circlechef
{

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

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

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

auto operation = model_recipe->add_operation();

operation->set_type("CircleGRU");

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/CircleGRU.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 circle_gru
*/
class CircleOpCircleGRU : 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__
2 changes: 2 additions & 0 deletions compiler/circlechef/core/src/Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ circle::ActivationFunctionType as_circle_activation(const circlechef::Activation
return circle::ActivationFunctionType_RELU;
case circlechef::RELU6:
return circle::ActivationFunctionType_RELU6;
case circlechef::TANH:
return circle::ActivationFunctionType_TANH;
default:
break;
}
Expand Down
41 changes: 41 additions & 0 deletions compiler/circlechef/core/src/Op/CircleGRU.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 "CircleGRU.h"

#include "Convert.h"

flatbuffers::Offset<void> CircleGRUChef::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::CircleGRUOptionsBuilder 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> CircleGRUChefFactory::create(const circlechef::Operation *operation) const
{
return std::unique_ptr<OpChef>{new CircleGRUChef{operation}};
}
49 changes: 49 additions & 0 deletions compiler/circlechef/core/src/Op/CircleGRU.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 CircleGRUChef final : public OpChef
{
public:
explicit CircleGRUChef(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_CircleGRUOptions;
}

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

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

struct CircleGRUChefFactory 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(CircleGRU, CircleGRUChefFactory)
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/CircleGRU.h"
#include "Op/InstanceNorm.h"

#endif // __OP_CHEFS_H__
8 changes: 8 additions & 0 deletions compiler/circlechef/proto/circlechef.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum Activation {
NONE = 0;
RELU = 1;
RELU6 = 3;
TANH = 4;
}

message BatchMatMulOptions {
Expand All @@ -76,6 +77,12 @@ message InstanceNormOptions {
optional Activation activation = 2 [default = NONE];
}

message CircleGRUOptions {
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 +104,7 @@ message Operation {
optional InstanceNormOptions instance_norm_options = 101;
optional BCQFullyConnectedOptions bcq_fully_connected_options = 102;
optional BCQGatherOptions bcq_gather_options = 103;
optional CircleGRUOptions 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 CircleGRUPrinter : public OpPrinter
{
public:
void options(const circle::Operator *op, std::ostream &os) const override
{
if (auto *params = op->builtin_options_as_CircleGRUOptions())
{
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<CircleGRUPrinter>();
}

} // namespace circledump
2 changes: 1 addition & 1 deletion res/CircleRecipes/CircleGRU_000/test.recipe
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ operand {
operation {
type: "CircleGRU"
circle_gru_options {
activation: TANH
activation: RELU
return_sequences: false
time_major: false
}
Expand Down

0 comments on commit 29060f0

Please sign in to comment.