Skip to content

Commit

Permalink
[Draft][Onert] Add Fill operator to cpu backend (Samsung#791)
Browse files Browse the repository at this point in the history
This pull request add "fill" operator to cpu backend.

Signed-off-by: Leehansol <[email protected]>
  • Loading branch information
hasw7569 authored May 14, 2020
1 parent f1ef1a7 commit 720f629
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 0 deletions.
50 changes: 50 additions & 0 deletions compute/cker/include/cker/operation/Fill.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
* Copyright 2018 The TensorFlow Authors. 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 __NNFW_CKER_FILL_H__
#define __NNFW_CKER_FILL_H__

#include "cker/Shape.h"

namespace nnfw
{
namespace cker
{
template <typename T>
inline void Fill(const Shape &input_shape, int *input_data, const T value_data,
const Shape &output_shape, T output_data)
{
int input_size = input_shape.FlatSize();
int output_size = 1;
for (int i = 0; i < input_size; i++)
{
output_size *= input_data[i];
}

if (output_size == output_shape.FlatSize())
{
for (int i = 0; i < output_size; i++)
{
output_data[i] = *value_data;
}
}
}

} // namespace cker
} // namespace nnfw

#endif // __NNFW_CKER_FILL_H__
18 changes: 18 additions & 0 deletions runtime/onert/backend/cpu/KernelGenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "kernel/DivLayer.h"
#include "kernel/ExpLayer.h"
#include "kernel/ExpandDimsLayer.h"
#include "kernel/FillLayer.h"
#include "kernel/FullyConnectedLayer.h"
#include "kernel/GatherLayer.h"
#include "kernel/LogLayer.h"
Expand Down Expand Up @@ -262,6 +263,23 @@ void KernelGenerator::visit(const ir::operation::Concat &node)
_return_fn = std::move(fn);
}

void KernelGenerator::visit(const ir::operation::Fill &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(ir::operation::Fill::Input::INPUT)};
const auto value_index{node.getInputs().at(ir::operation::Fill::Input::VALUE)};

auto output_alloc = _tensor_builder->at(output_index).get();
auto input_alloc = _tensor_builder->at(input_index).get();
auto value_alloc = _tensor_builder->at(value_index).get();

auto fn = std::make_unique<::onert::backend::cpu::kernel::FillLayer>();

fn->configure(input_alloc, value_alloc, output_alloc);

_return_fn = std::move(fn);
}

void KernelGenerator::visit(const ir::operation::FullyConnected &node)
{
using ir::operation::FullyConnected;
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/backend/cpu/KernelGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class KernelGenerator : public IKernelGenerator
void visit(const ir::operation::MaxPool2D &) override;
void visit(const ir::operation::AvgPool2D &) override;
void visit(const ir::operation::Concat &) override;
void visit(const ir::operation::Fill &) override;
void visit(const ir::operation::FullyConnected &) override;
void visit(const ir::operation::Reshape &) override;
void visit(const ir::operation::Squeeze &) override;
Expand Down
2 changes: 2 additions & 0 deletions runtime/onert/backend/cpu/ShapeFixer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ void ShapeFixer::visit(const ir::operation::Concat &) { /* DO NOTHING */}
void ShapeFixer::visit(const ir::operation::Exp &) { /* DO NOTHING */}
void ShapeFixer::visit(const ir::operation::ExpandDims &) { /* DO NOTHING */}

void ShapeFixer::visit(const ir::operation::Fill &) { /* DO NOTHING */}

void ShapeFixer::visit(const ir::operation::FullyConnected &) { /* DO NOTHING */}

void ShapeFixer::visit(const ir::operation::Reshape &) { /* DO NOTHING */}
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/backend/cpu/ShapeFixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ShapeFixer : public IShapeFixer
void visit(const ir::operation::MaxPool2D &) override;
void visit(const ir::operation::AvgPool2D &) override;
void visit(const ir::operation::Concat &) override;
void visit(const ir::operation::Fill &) override;
void visit(const ir::operation::FullyConnected &) override;
void visit(const ir::operation::Reshape &) override;
void visit(const ir::operation::Squeeze &) override;
Expand Down
76 changes: 76 additions & 0 deletions runtime/onert/backend/cpu/kernel/FillLayer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 "FillLayer.h"

#include "OperationUtils.h"

#include <cker/operation/Fill.h>

namespace onert
{
namespace backend
{
namespace cpu
{
namespace kernel
{

FillLayer::FillLayer() : _input(nullptr), _value(nullptr), _output(nullptr)
{
// DO NOTHING
}

void FillLayer::configure(const operand::Tensor *input, const operand::Tensor *value,
operand::Tensor *output)
{
_input = input;
_value = value;
_output = output;
}

void FillLayer::run()
{
switch (_output->data_type())
{
case OperandType::FLOAT32:
nnfw::cker::Fill<float *>(
convertTensorToCkerShape(_input), reinterpret_cast<int *>(_input->buffer()),
reinterpret_cast<float *>(_value->buffer()), convertTensorToCkerShape(_output),
reinterpret_cast<float *>(_output->buffer()));
break;
case OperandType::INT32:
nnfw::cker::Fill<int32_t *>(
convertTensorToCkerShape(_input), reinterpret_cast<int *>(_input->buffer()),
reinterpret_cast<int32_t *>(_value->buffer()), convertTensorToCkerShape(_output),
reinterpret_cast<int32_t *>(_output->buffer()));
break;
case OperandType::UINT32:
nnfw::cker::Fill<uint32_t *>(
convertTensorToCkerShape(_input), reinterpret_cast<int *>(_input->buffer()),
reinterpret_cast<uint32_t *>(_value->buffer()), convertTensorToCkerShape(_output),
reinterpret_cast<uint32_t *>(_output->buffer()));
break;
default:
throw std::runtime_error{"Fill: unsupported data type"};
break;
}
}

} // namespace kernel
} // namespace cpu
} // namespace backend
} // namespace onert
60 changes: 60 additions & 0 deletions runtime/onert/backend/cpu/kernel/FillLayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 riting, 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 __ONERT_BACKEND_CPU_KERNEL_FILLLAYER_H__
#define __ONERT_BACKEND_CPU_KERNEL_FILLLAYER_H__

#include "../operand/Tensor.h"

#include <exec/IFunction.h>

namespace onert
{
namespace backend
{
namespace cpu
{
namespace kernel
{

class FillLayer : public ::onert::exec::IFunction
{
public:
FillLayer();

void configure(const operand::Tensor *input, const operand::Tensor *value,
operand::Tensor *output);

void run();
void runSync()
{
// this abstract method is used just for profiling and called for
// backend::acl_common::AclFunction
run();
}

private:
const operand::Tensor *_input;
const operand::Tensor *_value;
operand::Tensor *_output;
};

} // namespace kernel
} // namespace cpu
} // namespace backend
} // namespace onert

#endif // __ONERT_BACKEND_CPU_KERNEL_FILLLAYER_H__
1 change: 1 addition & 0 deletions runtime/onert/core/include/ir/Operations.Include.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ir/operation/AvgPool2D.h"
#include "ir/operation/Concat.h"
#include "ir/operation/Reshape.h"
#include "ir/operation/Fill.h"
#include "ir/operation/FullyConnected.h"
#include "ir/operation/Softmax.h"
#include "ir/operation/Transpose.h"
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/core/include/ir/Operations.lst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OP(DepthwiseConv2D)
OP(AvgPool2D)
OP(MaxPool2D)
OP(Concat)
OP(Fill)
OP(FullyConnected)
OP(ReduceSum)
OP(Reshape)
Expand Down
50 changes: 50 additions & 0 deletions runtime/onert/core/include/ir/operation/Fill.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 __ONERT_IR_OPERATION_FILL_H__
#define __ONERT_IR_OPERATION_FILL_H__

#include "ir/Operation.h"

namespace onert
{
namespace ir
{
namespace operation
{

class Fill : public Operation
{
public:
enum Input
{
INPUT = 0,
VALUE,
};

public:
Fill(const OperandIndexSequence &inputs, const OperandIndexSequence &outputs);

public:
void accept(OperationVisitor &v) const override;
OpCode opcode() const final { return OpCode::Fill; }
};

} // namespace operation
} // namespace ir
} // namespace onert

#endif // __ONERT_IR_OPERATION_FILL_H__
11 changes: 11 additions & 0 deletions runtime/onert/core/src/compiler/OperationValidator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,17 @@ void OperationValidator::visit(const ir::operation::ExpandDims &node)
assert(_ctx.at(axis_index).shape().rank() <= 1);
}

void OperationValidator::visit(const ir::operation::Fill &node)
{
const auto output_index{node.getOutputs().at(0)};
const auto input_index{node.getInputs().at(0)};
const auto value_index{node.getInputs().at(1)};

UNUSED_RELEASE(output_index);
UNUSED_RELEASE(input_index);
UNUSED_RELEASE(value_index);
}

void OperationValidator::visit(const ir::operation::Floor &node)
{
const auto output_index{node.getOutputs().at(0)};
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/core/src/compiler/OperationValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class OperationValidator : public ir::OperationVisitor
void visit(const ir::operation::EmbeddingLookup &node) override;
void visit(const ir::operation::Exp &node) override;
void visit(const ir::operation::ExpandDims &node) override;
void visit(const ir::operation::Fill &node) override;
void visit(const ir::operation::Floor &node) override;
void visit(const ir::operation::HashtableLookup &node) override;
void visit(const ir::operation::TransposeConv &node) override;
Expand Down
39 changes: 39 additions & 0 deletions runtime/onert/core/src/ir/operation/Fill.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 "ir/operation/Fill.h"

#include <cassert>

#include "ir/OperationVisitor.h"

namespace onert
{
namespace ir
{
namespace operation
{

void Fill::accept(OperationVisitor &v) const { v.visit(*this); }

Fill::Fill(const OperandIndexSequence &inputs, const OperandIndexSequence &outputs)
: Operation{OperandConstraint::createExact(1u), inputs, outputs}
{
}

} // namespace operation
} // namespace ir
} // namespace onert
Loading

0 comments on commit 720f629

Please sign in to comment.