Skip to content

Commit

Permalink
Merge branch 'master' into comparisons_kernels_onert_micro
Browse files Browse the repository at this point in the history
  • Loading branch information
chunseoklee authored Aug 1, 2023
2 parents 6336ecf + 88ea00f commit 1caa149
Show file tree
Hide file tree
Showing 48 changed files with 2,594 additions and 556 deletions.
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 @@ -24,6 +24,7 @@ REGISTER_KERNEL(Gather)
REGISTER_KERNEL(Gelu)
REGISTER_KERNEL(Greater)
REGISTER_KERNEL(GreaterEqual)
REGISTER_KERNEL(HardSwish)
REGISTER_KERNEL(If)
REGISTER_KERNEL(InstanceNorm)
REGISTER_KERNEL(L2Normalize)
Expand Down
31 changes: 31 additions & 0 deletions compiler/luci-interpreter/pal/linux/PALHardSwish.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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_INTERPRETER_PAL_HARDSWISH_H
#define LUCI_INTERPRETER_PAL_HARDSWISH_H

#include <tensorflow/lite/kernels/internal/optimized/optimized_ops.h>

namespace luci_interpreter_pal
{
static inline void HardSwish(const tflite::RuntimeShape &input_shape, const float *input_data,
const tflite::RuntimeShape &output_shape, float *output_data)
{
tflite::optimized_ops::HardSwish(input_shape, input_data, output_shape, output_data);
}
} // namespace luci_interpreter_pal

#endif // LUCI_INTERPRETER_PAL_HARDSWISH_H
52 changes: 52 additions & 0 deletions compiler/luci-interpreter/src/kernels/HardSwish.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 "kernels/HardSwish.h"
#include "kernels/Utils.h"

#include "PALHardSwish.h"

#include <stdexcept>

namespace luci_interpreter
{

namespace kernels
{

HardSwish::HardSwish(const Tensor *input, Tensor *output) : Kernel({input}, {output}) {}

void HardSwish::configure()
{
LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
output()->resize(input()->shape());
}

void HardSwish::execute() const
{
switch (input()->element_type())
{
case DataType::FLOAT32:
luci_interpreter_pal::HardSwish(getTensorShape(input()), getTensorData<float>(input()),
getTensorShape(output()), getTensorData<float>(output()));
break;
default:
throw std::runtime_error("Unsupported type.");
}
}

} // namespace kernels
} // namespace luci_interpreter
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
* 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.
Expand All @@ -14,8 +14,8 @@
* limitations under the License.
*/

#ifndef LUCI_INTERPRETER_KERNELS_LOGICALNOT_H
#define LUCI_INTERPRETER_KERNELS_LOGICALNOT_H
#ifndef LUCI_INTERPRETER_KERNELS_HARDSWISH_H
#define LUCI_INTERPRETER_KERNELS_HARDSWISH_H

#include "core/Kernel.h"
#include "core/KernelParams.h"
Expand All @@ -25,22 +25,19 @@ namespace luci_interpreter
namespace kernels
{

class LogicalNot : public Kernel
class HardSwish : public Kernel
{
public:
LogicalNot(const Tensor *input, Tensor *output);
HardSwish(const Tensor *input, Tensor *output);

const Tensor *input() const { return _inputs[0]; }
Tensor *output() const { return _outputs[0]; }

void configure() override;
void execute() const override;

private:
inline void evalLogicalNot() const;
};

} // namespace kernels
} // namespace luci_interpreter

#endif // LUCI_INTERPRETER_KERNELS_LOGICALNOT_H
#endif // LUCI_INTERPRETER_KERNELS_HARDSWISH_H
81 changes: 81 additions & 0 deletions compiler/luci-interpreter/src/kernels/HardSwish.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 "kernels/HardSwish.h"
#include "kernels/TestUtils.h"
#include "luci_interpreter/TestMemoryManager.h"

namespace luci_interpreter
{
namespace kernels
{
namespace
{

using namespace testing;

void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
std::initializer_list<float> input_data, std::initializer_list<float> output_data)
{
std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
Tensor input_tensor =
makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);

HardSwish kernel(&input_tensor, &output_tensor);
kernel.configure();
memory_manager->allocate_memory(output_tensor);
kernel.execute();

(void)output_shape;
EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(output_data));
}

TEST(HardSwishTest, SimpleHardSwish)
{
Check(
/*input_shape=*/{1, 2, 4, 1}, /*output_shape=*/{1, 2, 4, 1},
/*input_data=*/
{
0, -6, 2, -4, //
3, -2, 10, -0.1, //
},
/*output_data=*/
{
0, -0, 1.66667, -0, //
3, -0.333333, 10, -0.0483333, //
});
}

TEST(HardSwishTest, InOutTypeMismatch_NEG)
{
std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
Shape input_shape{1, 2, 4, 1};
std::vector<float> input_data{
0, -6, 2, -4, //
3, -2, 10, -0.1, //
};
Tensor input_tensor =
makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
Tensor output_tensor = makeOutputTensor(DataType::U8);

HardSwish kernel(&input_tensor, &output_tensor);
EXPECT_ANY_THROW(kernel.configure());
}

} // namespace
} // namespace kernels
} // namespace luci_interpreter
35 changes: 35 additions & 0 deletions compiler/luci-interpreter/src/loader/nodes/HardSwish.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 "Builders.h"

#include "kernels/HardSwish.h"

namespace luci_interpreter
{

std::unique_ptr<Kernel> build_kernel_CircleHardSwish(const luci::CircleNode *circle_node,
KernelBuilderHelper &helper)
{
const auto *node = loco::must_cast<const luci::CircleHardSwish *>(circle_node);
assert(node->arity() == 1);

const Tensor *input = helper.getInputTensor(node->features());
Tensor *output = helper.getOutputTensor(node);

return std::make_unique<kernels::HardSwish>(input, output);
}
} // namespace luci_interpreter
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 @@ -55,6 +55,7 @@
#include "Nodes/CircleGelu.h"
#include "Nodes/CircleGreater.h"
#include "Nodes/CircleGreaterEqual.h"
#include "Nodes/CircleHardSwish.h"
#include "Nodes/CircleIf.h"
#include "Nodes/CircleInstanceNorm.h"
#include "Nodes/CircleL2Normalize.h"
Expand Down
37 changes: 37 additions & 0 deletions compiler/luci/import/include/luci/Import/Nodes/CircleHardSwish.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_HARDSWISH_H__
#define __LUCI_IMPORT_OP_CIRCLE_HARDSWISH_H__

#include "luci/Import/GraphBuilder.h"

namespace luci
{

class CircleHardSwishGraphBuilder : 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_HARDSWISH_H__
12 changes: 6 additions & 6 deletions compiler/luci/import/src/GraphBuilderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ GraphBuilderRegistry::GraphBuilderRegistry()
CIRCLE_NODE(GELU, CircleGeluGraphBuilder); // 150
CIRCLE_NODE(GREATER, CircleGreaterGraphBuilder); // 61
CIRCLE_NODE(GREATER_EQUAL, CircleGreaterEqualGraphBuilder); // 62
CIRCLE_NODE(HARD_SWISH, CircleHardSwishGraphBuilder); // 117
CIRCLE_NODE(IF, CircleIfGraphBuilder); // 118
CIRCLE_NODE(INSTANCE_NORM, CircleInstanceNormGraphBuilder); // 254
CIRCLE_NODE(L2_NORMALIZATION, CircleL2NormalizeGraphBuilder); // 11
CIRCLE_NODE(L2_POOL_2D, CircleL2Pool2DGraphBuilder); // 12
CIRCLE_NODE(LEAKY_RELU, CircleLeakyReluGraphBuilder); // 98,
CIRCLE_NODE(LEAKY_RELU, CircleLeakyReluGraphBuilder); // 98
CIRCLE_NODE(LESS, CircleLessGraphBuilder); // 58
CIRCLE_NODE(LESS_EQUAL, CircleLessEqualGraphBuilder); // 63
CIRCLE_NODE(LOCAL_RESPONSE_NORMALIZATION, CircleLocalResponseNormalizationGraphBuilder); // 13
Expand All @@ -87,16 +88,16 @@ GraphBuilderRegistry::GraphBuilderRegistry()
CIRCLE_NODE(MIRROR_PAD, CircleMirrorPadGraphBuilder); // 100
CIRCLE_NODE(MUL, CircleMulGraphBuilder); // 18
CIRCLE_NODE(NEG, CircleNegGraphBuilder); // 59
CIRCLE_NODE(NON_MAX_SUPPRESSION_V4, CircleNonMaxSuppressionV4GraphBuilder); // 120,
CIRCLE_NODE(NON_MAX_SUPPRESSION_V5, CircleNonMaxSuppressionV5GraphBuilder); // 121,
CIRCLE_NODE(NON_MAX_SUPPRESSION_V4, CircleNonMaxSuppressionV4GraphBuilder); // 120
CIRCLE_NODE(NON_MAX_SUPPRESSION_V5, CircleNonMaxSuppressionV5GraphBuilder); // 121
CIRCLE_NODE(NOT_EQUAL, CircleNotEqualGraphBuilder); // 72
CIRCLE_NODE(ONE_HOT, CircleOneHotGraphBuilder); // 85
CIRCLE_NODE(PACK, CirclePackGraphBuilder); // 83
CIRCLE_NODE(PAD, CirclePadGraphBuilder); // 34
CIRCLE_NODE(PADV2, CirclePadV2GraphBuilder); // 60
CIRCLE_NODE(POW, CirclePowGraphBuilder); // 78
CIRCLE_NODE(PRELU, CirclePReluGraphBuilder); // 54,
CIRCLE_NODE(QUANTIZE, CircleQuantizeGraphBuilder); // 114,
CIRCLE_NODE(PRELU, CirclePReluGraphBuilder); // 54
CIRCLE_NODE(QUANTIZE, CircleQuantizeGraphBuilder); // 114
CIRCLE_NODE(RANGE, CircleRangeGraphBuilder); // 96
CIRCLE_NODE(RANK, CircleRankGraphBuilder); // 110
CIRCLE_NODE(REDUCE_ANY, CircleReduceAnyGraphBuilder); // 91
Expand Down Expand Up @@ -161,7 +162,6 @@ GraphBuilderRegistry::GraphBuilderRegistry()
// BuiltinOperator_BIDIRECTIONAL_SEQUENCE_RNN = 46,
// BuiltinOperator_DELEGATE = 51,
// BuiltinOperator_ARG_MAX = 56,
// BuiltinOperator_HARD_SWISH = 117,

// Register builders for nodes which not handles in builders registered above.
#define CIRCLE_NODE(CLASS) add(std::make_unique<CLASS>())
Expand Down
41 changes: 41 additions & 0 deletions compiler/luci/import/src/Nodes/CircleHardSwish.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 "luci/Import/Nodes/CircleHardSwish.h"

#include <luci/IR/Nodes/CircleHardSwish.h>

#include <loco.h>

namespace luci
{

bool CircleHardSwishGraphBuilder::validate(const ValidateArgs &args) const
{
return GraphBuilder::validate(args, 1);
}

CircleNode *CircleHardSwishGraphBuilder::build_node(const circle::OperatorT &,
const std::vector<CircleNode *> &inputs,
loco::Graph *graph) const
{
auto *node = graph->nodes()->create<CircleHardSwish>();
node->features(inputs.at(0));

return node;
}

} // namespace luci
1 change: 1 addition & 0 deletions compiler/luci/partition/include/luci/ConnectNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class ConnectNode final : public luci::CircleNodeVisitor<void>
void visit(const luci::CircleGelu *) final;
void visit(const luci::CircleGreater *) final;
void visit(const luci::CircleGreaterEqual *) final;
void visit(const luci::CircleHardSwish *) final;
void visit(const luci::CircleIf *) final;
void visit(const luci::CircleL2Normalize *) final;
void visit(const luci::CircleL2Pool2D *) final;
Expand Down
Loading

0 comments on commit 1caa149

Please sign in to comment.