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

[onert] Remove layout in graph #13560

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ template <typename T_BackendContext> ITensorRegistry *genTensors(T_BackendContex
graph.operands().iterate([&](const ir::OperandIndex &ind, const ir::Operand &obj) {
if (ctx.external_operands().contains(ind))
return;
// NOTE Assuming there is no layout changes (Always assume NHWC or UNKNOWN)
assert(graph.layout() != ir::Layout::NCHW);
tensor_builder->registerTensorInfo(ind, obj.info());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ ITensorRegistry *genTensors(backend::train::TrainableBackendContext &ctx,
tgraph.operands().iterate([&](const ir::OperandIndex &ind, const ir::Operand &obj) {
if (ctx.external_operands().contains(ind))
return;
// NOTE Assuming there is no layout changes (Always assume NHWC or UNKNOWN)
assert(tgraph.layout() != ir::Layout::NCHW);
tensor_builder->registerTensorInfo(ind, obj.info());
});

Expand Down
3 changes: 0 additions & 3 deletions runtime/onert/core/include/ir/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class Graph : public IGraph
Operands &operands() { return _operands; } // TODO Remove this non-const accessor
const Operations &operations() const override { return _operations; }
Operations &operations() { return _operations; }
Layout layout() const { return _layout; }

// Topological sort
public:
Expand All @@ -126,8 +125,6 @@ class Graph : public IGraph
OperandIndexSequence _outputs;
std::unordered_map<std::string, IOIndex> _name_to_input;
std::unordered_map<std::string, IOIndex> _name_to_output;
// TFLite and circle's default layout is NHWC;
Layout _layout{Layout::NHWC};
};

} // namespace ir
Expand Down
1 change: 0 additions & 1 deletion runtime/onert/core/include/ir/train/TrainableGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class TrainableGraph : public IGraph
const Operations &operations() const override { return _graph.operations(); }
const Operands &backward_operands() const { return _backward_operands; }
OperandIndex getLossIndex(const IOIndex &pred_io_ind) const;
Layout layout() const { return _graph.layout(); }
const Graph &graph() const { return _graph; }

public:
Expand Down
10 changes: 3 additions & 7 deletions runtime/onert/core/include/util/ShapeInference.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "ir/operation/StridedSlice.h"
#include "compiler/LoweredGraph.h"
#include "ir/Index.h"
#include "ir/Layout.h"
#include "ir/OperationVisitor.h"
#include "backend/ITensor.h"
#include "backend/ITensorRegistry.h"
Expand Down Expand Up @@ -58,12 +57,10 @@ ir::Shape inferBroadcastToShape(const ir::Shape shp_shape, const int32_t *shp_bu
ir::Shape inferConcatShape(const Shapes &in_shapes, const ir::operation::Concat::Param &param);

ir::Shape inferConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
const ir::operation::Conv2D::Param &param,
ir::Layout layout = ir::Layout::NHWC);
const ir::operation::Conv2D::Param &param);

ir::Shape inferDepthwiseConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
const ir::operation::DepthwiseConv2D::Param &param,
ir::Layout layout = ir::Layout::NHWC);
const ir::operation::DepthwiseConv2D::Param &param);

ir::Shape inferEltwiseShape(const ir::Shape &lhs_shape, const ir::Shape &rhs_shape);

Expand All @@ -82,8 +79,7 @@ ir::Shape inferPackShape(const ir::Shape &input_shape, int axis, int rank, int n

ir::Shape inferPadShape(const ir::Shape &in_shape, const int32_t *pad_buf, const size_t num_pads);

ir::Shape inferPoolShape(const ir::Shape &in_shape, const ir::operation::Pool2D::Param &param,
ir::Layout layout = ir::Layout::NHWC);
ir::Shape inferPoolShape(const ir::Shape &in_shape, const ir::operation::Pool2D::Param &param);

template <typename T> ir::Shape inferRangeShape(T start_val, T limit_val, T delta_val);

Expand Down
1 change: 0 additions & 1 deletion runtime/onert/core/src/compiler/ExecutorFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ void initializeSubgraphIOTensors(compiler::ILoweredGraph &lowered_graph,
for (auto &&ind : indices)
{
const auto &operand = lowered_graph.graph().operands().at(ind);
assert(lowered_graph.graph().layout() == ir::Layout::NHWC);
auto tensor = std::make_unique<backend::builtin::IOTensor>(operand.info());

// Add tensor to builtin TensorRegistry.
Expand Down
2 changes: 1 addition & 1 deletion runtime/onert/core/src/compiler/LoweredGraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void LoweredGraph::makeLowerInfo(const compiler::BackendResolver &backend_resolv

// Handle graph inputs and outputs
const auto builtin_backend = BackendManager::get().getBuiltin();
auto factor = PermuteFactor{builtin_backend, _graph.layout()};
auto factor = PermuteFactor{builtin_backend, ir::Layout::NHWC};
for (auto &&index : _graph.getInputs() | ir::Remove::UNDEFINED)
{
auto &operand_li = lower_info().operand.at(index);
Expand Down
25 changes: 10 additions & 15 deletions runtime/onert/core/src/compiler/ShapeValidator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ void ShapeValidator::visit(const ir::operation::BatchToSpaceND &node)
const auto block_size_index{
node.getInputs().at(ir::operation::BatchToSpaceND::Input::BLOCK_SIZE)};

const auto frontend_layout = _graph.layout();
const auto input_shape = operands.at(ifm_index).shape().asFeature(frontend_layout);
const auto output_shape = operands.at(ofm_index).shape().asFeature(frontend_layout);
const auto input_shape = operands.at(ifm_index).shape().asFeature(ir::Layout::NHWC);
const auto output_shape = operands.at(ofm_index).shape().asFeature(ir::Layout::NHWC);

// All requirement as per NNAPI specification.
OP_REQUIRES(operands.at(ifm_index).shape().rank() == 4);
Expand Down Expand Up @@ -392,9 +391,8 @@ void ShapeValidator::visit(const ir::operation::SpaceToBatchND &node)
node.getInputs().at(ir::operation::SpaceToBatchND::Input::BLOCK_SIZE)};
const auto paddings_index{node.getInputs().at(ir::operation::SpaceToBatchND::Input::PADDINGS)};

const auto frontend_layout = _graph.layout();
const auto input_shape = operands.at(ifm_index).shape().asFeature(frontend_layout);
const auto output_shape = operands.at(ofm_index).shape().asFeature(frontend_layout);
const auto input_shape = operands.at(ifm_index).shape().asFeature(ir::Layout::NHWC);
const auto output_shape = operands.at(ofm_index).shape().asFeature(ir::Layout::NHWC);

// All requirement as per NNAPI specification.
OP_REQUIRES(operands.at(ifm_index).shape().rank() == 4);
Expand All @@ -418,9 +416,8 @@ void ShapeValidator::visit(const ir::operation::SpaceToDepth &node)

const auto ifm_index{node.getInputs().at(ir::operation::SpaceToDepth::Input::INPUT)};

const auto frontend_layout = _graph.layout();
const auto input_shape = operands.at(ifm_index).shape().asFeature(frontend_layout);
const auto output_shape = operands.at(ofm_index).shape().asFeature(frontend_layout);
const auto input_shape = operands.at(ifm_index).shape().asFeature(ir::Layout::NHWC);
const auto output_shape = operands.at(ofm_index).shape().asFeature(ir::Layout::NHWC);
const auto block_size = node.param().block_size;

// All assertions as per NNAPI specification.
Expand Down Expand Up @@ -540,9 +537,8 @@ void ShapeValidator::visit(const ir::operation::TransposeConv &node)
OP_REQUIRES(operands.at(ofm_index).shape().rank() == operands.at(ifm_index).shape().rank());
OP_REQUIRES(operands.at(ofm_index).shape().rank() == operands.at(ker_index).shape().rank());

const auto frontend_layout = _graph.layout();
const auto ofm_shape = operands.at(ofm_index).shape().asFeature(frontend_layout);
const auto ifm_shape = operands.at(ifm_index).shape().asFeature(frontend_layout);
const auto ofm_shape = operands.at(ofm_index).shape().asFeature(ir::Layout::NHWC);
const auto ifm_shape = operands.at(ifm_index).shape().asFeature(ir::Layout::NHWC);
// The kernel has only IHWO layout on frontend
// So ker_shape is treated here below
// I -> N
Expand Down Expand Up @@ -587,9 +583,8 @@ void ShapeValidator::visit(const ir::operation::DepthToSpace &node)

const auto input_index{node.getInputs().at(ir::operation::DepthToSpace::Input::INPUT)};

const auto frontend_layout = _graph.layout();
const auto output_shape = operands.at(output_index).shape().asFeature(frontend_layout);
const auto input_shape = operands.at(input_index).shape().asFeature(frontend_layout);
const auto output_shape = operands.at(output_index).shape().asFeature(ir::Layout::NHWC);
const auto input_shape = operands.at(input_index).shape().asFeature(ir::Layout::NHWC);

OP_REQUIRES(operands.at(input_index).shape().rank() == 4);
OP_REQUIRES(operands.at(output_index).shape().rank() == 4);
Expand Down
4 changes: 1 addition & 3 deletions runtime/onert/core/src/compiler/StaticShapeInferer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,6 @@ void StaticShapeInferer::visit(const ir::operation::Pool2D &op)
{
auto &operands = _lowered_subg->graph().operands();

const auto layout = _lowered_subg->graph().layout();

const auto input_idx{op.getInputs().at(ir::operation::Pool2D::Input::INPUT)};
const auto &input = operands.at(input_idx);
if (input.info().shape().rank() != 4)
Expand All @@ -863,7 +861,7 @@ void StaticShapeInferer::visit(const ir::operation::Pool2D &op)
const auto output_idx = op.getOutputs().at(0);
ir::Operand &output = operands.at(output_idx);

ir::Shape new_shape = shape_inference::inferPoolShape(input.info().shape(), op.param(), layout);
ir::Shape new_shape = shape_inference::inferPoolShape(input.info().shape(), op.param());
output.info().shape(new_shape);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void PermutationOperationPass::applyExpandRanks(const Operation &node)

assert(output.getDef().valid());
const auto node_index = output.getDef();
const auto frontend_layout = _graph.layout();
const auto frontend_layout = ir::Layout::NHWC;
const auto backend_layout = _lowered_graph.lower_info().operation.getRawPtr(node_index)->layout();

if (frontend_layout == backend_layout)
Expand Down Expand Up @@ -86,7 +86,7 @@ void PermutationOperationPass::changeToKeepLayout(const Operation &node)

auto &operation_li_map = _lowered_graph.lower_info().operation;
auto &operand_li_map = _lowered_graph.lower_info().operand;
const auto frontend_layout = _graph.layout();
const auto frontend_layout = ir::Layout::NHWC;
const auto backend_layout = operation_li_map.getRawPtr(node_index)->layout();

if (frontend_layout == backend_layout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void LoweredTrainableGraph::makeLowerInfo(const compiler::BackendResolver &backe

// Handle graph inputs and outputs
const auto builtin_backend = BackendManager::get().getBuiltin();
auto factor = PermuteFactor{builtin_backend, _trainable_graph.layout()};
auto factor = PermuteFactor{builtin_backend, ir::Layout::NHWC};
for (auto &&index : _trainable_graph.getInputs() | ir::Remove::UNDEFINED)
{
auto &operand_li = lower_info().operand.at(index);
Expand Down
20 changes: 8 additions & 12 deletions runtime/onert/core/src/util/ShapeInference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ ir::Shape inferConcatShape(const Shapes &in_shapes, const ir::operation::Concat:
}

ir::Shape inferConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
const ir::operation::Conv2D::Param &param, ir::Layout layout)
const ir::operation::Conv2D::Param &param)
{
if (param.stride.horizontal == 0 || param.stride.vertical == 0)
throw std::runtime_error{"Conv2D: stride values must be positive"};

auto ifm_shape = in_shape.asFeature(layout);
auto ifm_shape = in_shape.asFeature(ir::Layout::NHWC);

// Kernel format is [depth_out, kernel_height, kernel_width, depth_in]
auto kf_shape = ker_shape.asFeature(layout);
auto kf_shape = ker_shape.asFeature(ir::Layout::NHWC);
assert(ifm_shape.C == kf_shape.C);

const auto [out_h, out_w] = calcConvLikeHeightAndWidth(
Expand All @@ -341,17 +341,15 @@ ir::Shape inferConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape
}

ir::Shape inferDepthwiseConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
const ir::operation::DepthwiseConv2D::Param &param,
ir::Layout layout)
const ir::operation::DepthwiseConv2D::Param &param)
{
if (param.stride.horizontal == 0 || param.stride.vertical == 0)
throw std::runtime_error{"DepthwiseConv2D: stride values must be positive"};

assert(layout == ir::Layout::NHWC);
auto ifm_shape = in_shape.asFeature(layout);
auto ifm_shape = in_shape.asFeature(ir::Layout::NHWC);

// Kernel format is [1, kernel_height, kernel_width, depth_out]
auto kf_shape = ker_shape.asFeature(layout);
auto kf_shape = ker_shape.asFeature(ir::Layout::NHWC);
assert(kf_shape.C == static_cast<int32_t>(ifm_shape.C * param.multiplier));
assert(kf_shape.N == 1);

Expand Down Expand Up @@ -546,14 +544,12 @@ ir::Shape inferPadShape(const ir::Shape &in_shape, const int32_t *pad_buf, const
return ret;
}

ir::Shape inferPoolShape(const ir::Shape &in_shape, const ir::operation::Pool2D::Param &param,
const ir::Layout layout)
ir::Shape inferPoolShape(const ir::Shape &in_shape, const ir::operation::Pool2D::Param &param)
{
if (param.stride.horizontal == 0 || param.stride.vertical == 0)
throw std::runtime_error{"Pool2D: stride values must be positive"};

assert(layout == ir::Layout::NHWC);
auto ifm_shape = in_shape.asFeature(layout);
auto ifm_shape = in_shape.asFeature(ir::Layout::NHWC);
const auto [out_h, out_w] = calcConvLikeHeightAndWidth(ifm_shape.H, ifm_shape.W, param.kh,
param.kw, param.padding, param.stride);
// Pooling don't change number of channels and batch size
Expand Down