Skip to content

Commit

Permalink
[luci/import] Add Unique operation (Samsung#3605)
Browse files Browse the repository at this point in the history
This commit will add unique operation to luci import

ONE-DCO-1.0-Signed-off-by: venkat.iyer <[email protected]>

Signed-off-by: venkat.iyer <[email protected]>
  • Loading branch information
dr-venkman authored Aug 3, 2020
1 parent cb81eee commit 80ff3fd
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
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 @@ -120,6 +120,7 @@
#include "Nodes/CircleTopKV2.h"
#include "Nodes/CircleTranspose.h"
#include "Nodes/CircleTransposeConv.h"
#include "Nodes/CircleUnique.h"
#include "Nodes/CircleUnpack.h"
#include "Nodes/CircleWhere.h"
#include "Nodes/CircleWhile.h"
Expand Down
35 changes: 35 additions & 0 deletions compiler/luci/import/include/luci/Import/Nodes/CircleUnique.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2020 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_UNIQUE_H__
#define __LUCI_IMPORT_OP_CIRCLE_UNIQUE_H__

#include "luci/Import/GraphBuilderBase.h"

namespace luci
{

class CircleUniqueGraphBuilder : public GraphBuilderBase
{
public:
bool validate(const ValidateArgs &args) const final;

void build(const circle::OperatorT &op, GraphBuilderContext *context) const final;
};

} // namespace luci

#endif // __LUCI_IMPORT_OP_CIRCLE_UNIQUE_H__
2 changes: 1 addition & 1 deletion compiler/luci/import/src/GraphBuilderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ GraphBuilderRegistry::GraphBuilderRegistry()
CIRCLE_NODE(TOPK_V2, CircleTopKV2GraphBuilder); // 48
CIRCLE_NODE(TRANSPOSE, CircleTransposeGraphBuilder); // 39
CIRCLE_NODE(TRANSPOSE_CONV, CircleTransposeConvGraphBuilder); // 67
CIRCLE_NODE(UNIQUE, CircleUniqueGraphBuilder); // 103
CIRCLE_NODE(UNPACK, CircleUnpackGraphBuilder); // 88
CIRCLE_NODE(WHERE, CircleWhereGraphBuilder); // 109
CIRCLE_NODE(WHILE, CircleWhileGraphBuilder); // 119
Expand All @@ -155,7 +156,6 @@ GraphBuilderRegistry::GraphBuilderRegistry()
// BuiltinOperator_ARG_MAX = 56,
// BuiltinOperator_PADV2 = 60,
// BuiltinOperator_FAKE_QUANT = 80,
// BuiltinOperator_UNIQUE = 103,
// BuiltinOperator_QUANTIZE = 114,
// BuiltinOperator_HARD_SWISH = 117,
// BuiltinOperator_NON_MAX_SUPPRESSION_V4 = 120,
Expand Down
89 changes: 89 additions & 0 deletions compiler/luci/import/src/Nodes/CircleUnique.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2020 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 "luci/Import/Nodes/CircleUnique.h"

#include <luci/IR/Nodes/CircleUnique.h>
#include <luci/IR/Nodes/CircleUniqueOut.h>

#include <loco.h>

namespace luci
{

bool CircleUniqueGraphBuilder::validate(const ValidateArgs &args) const
{
if (args.op.inputs.size() != 1)
return false;

if (args.op.outputs.size() != 2)
return false;

return true;
}

void CircleUniqueGraphBuilder::build(const circle::OperatorT &op,
GraphBuilderContext *context) const
{
assert(context != nullptr);

auto graph = context->graph();

const std::vector<int32_t> &inputs = op.inputs;
const std::vector<int32_t> &outputs = op.outputs;
const auto &tensors = context->reader()->tensors();
auto tensors_ptr = context->reader()->tensors_ptr();
assert(tensors_ptr != nullptr);

std::vector<CircleNode *> input_nodes;
for (const int32_t input_tensor_index : inputs)
{
input_nodes.push_back(context->nodefinder()->node(input_tensor_index));
}

// Create CircleUnique
auto node = graph->nodes()->create<CircleUnique>();
node->input(input_nodes[0]);

const auto *options = op.builtin_options.AsUniqueOptions();
node->output_type(luci_datatype(options->idx_out_type));

assert(int32_t(outputs.size()) == 2);
// Let's use name of output 0 as Unique name
const circle::TensorT &output_tensor = *tensors[outputs[0]];
node->name(tensor_name(output_tensor));

// Create virtual outputs of Unique
for (int32_t n = 0; n < 2; ++n)
{
const circle::TensorT &output_tensor = *tensors[outputs[n]];

auto *nodeout = graph->nodes()->create<CircleUniqueOut>();
copy_tensor_attributes(output_tensor, nodeout);
// mark shape_status
if (tensors_ptr->Get(outputs[n])->shape() == nullptr)
nodeout->shape_status(ShapeStatus::NOSHAPE);
else
nodeout->shape_status(ShapeStatus::VALID);

nodeout->input(node);
nodeout->index(n);

context->nodefinder()->enroll(outputs[n], nodeout);
}
}

} // namespace luci

0 comments on commit 80ff3fd

Please sign in to comment.