2021-05-31 10:00:38 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
2023-01-20 11:38:21 +08:00
|
|
|
* Copyright (c) 2020-2023 Vivante Corporation
|
2021-05-31 10:00:38 +08:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
#ifndef TIM_LAYOUT_INFER_DECONV2D_LAYOUT_INFERENCE_H_
|
|
|
|
|
#define TIM_LAYOUT_INFER_DECONV2D_LAYOUT_INFERENCE_H_
|
|
|
|
|
|
2021-10-12 10:44:49 +08:00
|
|
|
#include "ops/op_layout_inference.h"
|
|
|
|
|
#include "permute_vector.h"
|
2022-09-14 15:20:57 +08:00
|
|
|
#include "builtin_op_impl.h"
|
2021-05-31 10:00:38 +08:00
|
|
|
#include "tim/vx/ops/deconv.h"
|
|
|
|
|
|
|
|
|
|
namespace tim {
|
|
|
|
|
namespace transform {
|
|
|
|
|
class DeConv2dLayoutInfer : public OpLayoutInfer {
|
|
|
|
|
public:
|
|
|
|
|
DeConv2dLayoutInfer(
|
|
|
|
|
const std::shared_ptr<vx::Operation>& op,
|
|
|
|
|
std::shared_ptr<layout_inference_impl::LayoutInferContext>& context)
|
|
|
|
|
: OpLayoutInfer(op, context) {}
|
|
|
|
|
|
|
|
|
|
void OnInputs(
|
|
|
|
|
std::vector<std::shared_ptr<vx::Tensor>>& next_tensors) override {
|
2023-02-17 14:17:23 +08:00
|
|
|
auto src_deconv2d = std::static_pointer_cast<vx::ops::DeConv2d>(op_);
|
2021-05-31 10:00:38 +08:00
|
|
|
vx::DataLayout layout = op_->impl()->layout_;
|
2023-02-17 14:17:23 +08:00
|
|
|
auto kernel_layout = src_deconv2d->KernelDataLayout();
|
|
|
|
|
std::shared_ptr<IPermuteVector> required_pv, weight_required_pv;
|
|
|
|
|
switch (layout)
|
|
|
|
|
{ // kernel layout must be IWHO in tflite & nnapi
|
|
|
|
|
case vx::DataLayout::CWHN:
|
|
|
|
|
required_pv = std::make_shared<PermuteVector<4>>(kCWHN2WHCN);
|
|
|
|
|
break;
|
|
|
|
|
case vx::DataLayout::WHCN:
|
|
|
|
|
required_pv = MakeShared(4);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
VSILOGE("The layout of input is not support.");
|
|
|
|
|
required_pv = MakeShared(4);
|
|
|
|
|
break;
|
2021-05-31 10:00:38 +08:00
|
|
|
}
|
2023-02-17 14:17:23 +08:00
|
|
|
switch (kernel_layout) {
|
|
|
|
|
case vx::DataLayout::OcIcWH: // Support TVM Kernel Layout
|
|
|
|
|
weight_required_pv = std::make_shared<PermuteVector<4>>(kOcIcWH2WHIcOc);
|
|
|
|
|
break;
|
|
|
|
|
case vx::DataLayout::IcOcWH:
|
|
|
|
|
weight_required_pv = std::make_shared<PermuteVector<4>>(kIcOcWH2WHIcOc);
|
|
|
|
|
break;
|
|
|
|
|
case vx::DataLayout::IcWHOc: // Support nnapi & tflite Kernel Layout
|
|
|
|
|
weight_required_pv = std::make_shared<PermuteVector<4>>(kIcWHOc2WHIcOc);
|
|
|
|
|
break;
|
|
|
|
|
default: // Default set to IWHO for compatibility with previous APIs
|
|
|
|
|
weight_required_pv = std::make_shared<PermuteVector<4>>(kIcWHOc2WHIcOc);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto input_tensors = op_->impl()->InputsTensor();
|
|
|
|
|
std::shared_ptr<vx::Tensor> infer_input, infer_weight, infer_bias;
|
|
|
|
|
// For input
|
|
|
|
|
auto input_pv = context_->GetPermuteVector(input_tensors[0]);
|
|
|
|
|
auto final_pv = input_pv->Reverse()->Add(required_pv);
|
|
|
|
|
if (!final_pv->IsAligned()) {
|
|
|
|
|
infer_input =
|
|
|
|
|
InsertPermute(context_->GetMapedTensor(input_tensors[0]), final_pv);
|
|
|
|
|
context_->SetPermuteVector(input_tensors[0], required_pv);
|
|
|
|
|
} else {
|
|
|
|
|
infer_input = context_->GetMapedTensor(input_tensors[0]);
|
|
|
|
|
context_->SetPermuteVector(input_tensors[0], input_pv);
|
|
|
|
|
}
|
|
|
|
|
context_->UpdateTensorMap(input_tensors[0], infer_input);
|
2021-05-31 10:00:38 +08:00
|
|
|
|
2023-02-17 14:17:23 +08:00
|
|
|
// For weight
|
|
|
|
|
if (input_tensors[1]->IsConstTensor()) {
|
|
|
|
|
if (!weight_required_pv->IsAligned()) {
|
|
|
|
|
infer_weight = PermuteConstTensor(input_tensors[1], weight_required_pv);
|
|
|
|
|
} else {
|
2023-03-23 21:35:30 +08:00
|
|
|
std::vector<uint8_t> dataRef(input_tensors[1]->GetSpec().GetByteSize());
|
|
|
|
|
input_tensors[1]->CopyDataFromTensor(dataRef.data());
|
2023-02-17 14:17:23 +08:00
|
|
|
infer_weight = context_->infer_graph_->CreateTensor(
|
2023-03-23 21:35:30 +08:00
|
|
|
input_tensors[1]->GetSpec(), (const void*)dataRef.data());
|
2023-02-17 14:17:23 +08:00
|
|
|
}
|
|
|
|
|
context_->SetPermuteVector(input_tensors[1], weight_required_pv);
|
|
|
|
|
context_->UpdateTensorMap(input_tensors[1], infer_weight);
|
|
|
|
|
} else {
|
|
|
|
|
auto weight_pv = context_->GetPermuteVector(input_tensors[1]);
|
|
|
|
|
auto final_pv = weight_pv->Reverse()->Add(weight_required_pv);
|
|
|
|
|
if (!final_pv->IsAligned()) {
|
|
|
|
|
infer_weight =
|
|
|
|
|
InsertPermute(context_->GetMapedTensor(input_tensors[1]), final_pv);
|
|
|
|
|
context_->SetPermuteVector(input_tensors[1], weight_required_pv);
|
2021-05-31 10:00:38 +08:00
|
|
|
} else {
|
2023-02-17 14:17:23 +08:00
|
|
|
infer_weight = context_->GetMapedTensor(input_tensors[1]);
|
|
|
|
|
context_->SetPermuteVector(input_tensors[1], weight_pv);
|
2021-05-31 10:00:38 +08:00
|
|
|
}
|
2023-02-17 14:17:23 +08:00
|
|
|
context_->UpdateTensorMap(input_tensors[1], infer_weight);
|
2021-05-31 10:00:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-17 14:17:23 +08:00
|
|
|
// For bias
|
|
|
|
|
if (input_tensors.size() == 3) {
|
|
|
|
|
if (input_tensors[2]->IsConstTensor()) {
|
2023-03-23 21:35:30 +08:00
|
|
|
std::vector<uint8_t> dataRef(input_tensors[2]->GetSpec().GetByteSize());
|
|
|
|
|
input_tensors[2]->CopyDataFromTensor(dataRef.data());
|
2023-02-17 14:17:23 +08:00
|
|
|
infer_bias = context_->infer_graph_->CreateTensor(
|
2023-03-23 21:35:30 +08:00
|
|
|
input_tensors[2]->GetSpec(), (const void*)dataRef.data());
|
2023-02-17 14:17:23 +08:00
|
|
|
} else {
|
|
|
|
|
infer_bias = context_->GetMapedTensor(input_tensors[2]);
|
|
|
|
|
}
|
|
|
|
|
auto bias_pv = MakeShared(1);
|
|
|
|
|
context_->UpdateTensorMap(input_tensors[2], infer_bias);
|
|
|
|
|
context_->SetPermuteVector(input_tensors[2], bias_pv);
|
|
|
|
|
}
|
2021-05-31 10:00:38 +08:00
|
|
|
|
2023-02-17 14:17:23 +08:00
|
|
|
auto deconv = op_->Clone(context_->infer_graph_);
|
2021-05-31 10:00:38 +08:00
|
|
|
auto infer_out = CreateOutputsTensor(required_pv);
|
2023-02-17 14:17:23 +08:00
|
|
|
for (const auto& i_src : input_tensors) {
|
2021-05-31 10:00:38 +08:00
|
|
|
(*deconv).BindInput(context_->GetMapedTensor(i_src));
|
|
|
|
|
}
|
|
|
|
|
(*deconv).BindOutput(infer_out[0]);
|
|
|
|
|
|
|
|
|
|
context_->SetPermuteVector(op_->impl()->OutputsTensor()[0], required_pv);
|
|
|
|
|
next_tensors.push_back(op_->impl()->OutputsTensor()[0]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace transform
|
|
|
|
|
} // namespace tim
|
|
|
|
|
|
|
|
|
|
#endif
|