Skip to content

Commit

Permalink
Merge pull request microsoft#15 from chenfeiyue-cfy/base_1.18.0
Browse files Browse the repository at this point in the history
Add gather/squeeze/unsqueeze/tile etc ops
  • Loading branch information
sunshinemyson authored Jun 18, 2024
2 parents 6d2c24a + 1b57ed0 commit 340ca16
Show file tree
Hide file tree
Showing 44 changed files with 764 additions and 116 deletions.
44 changes: 22 additions & 22 deletions include/onnxruntime/core/providers/vsinpu/vsinpu_provider_factory.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/****************************************************************************
*
* Copyright (c) 2023 Vivante Corporation
*
* 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.
*
*****************************************************************************/
*
* Copyright (c) 2023 Vivante Corporation
*
* 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.
*
*****************************************************************************/
#include "onnxruntime_c_api.h"

#ifdef __cplusplus
Expand Down
9 changes: 8 additions & 1 deletion onnxruntime/core/framework/node_unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void NodeUnit::InitForSingleNode() {
const auto& input_defs = target_node_.InputDefs();
const auto& output_defs = target_node_.OutputDefs();
auto qlinear_type = GetQLinearOpType(target_node_);
if (qlinear_type == QLinearOpType::Unknown || IsVariadicQLinearOp(qlinear_type)) { // TODO, add variadic support
if (qlinear_type == QLinearOpType::Unknown) {
// Not a Qlinear op, add all inputs / outputs
auto add_all_io = [](std::vector<NodeUnitIODef>& defs,
const ConstPointerContainer<std::vector<NodeArg*>>& node_defs) {
Expand Down Expand Up @@ -334,6 +334,13 @@ void NodeUnit::InitForSingleNode() {
outputs_.push_back(NodeUnitIODef{*output_defs[0], NodeUnitIODef::QuantParam{*input_defs[1], input_defs.size() == 3
? input_defs[2]
: nullptr}});
} else if (IsVariadicQLinearOp(qlinear_type)) {
size_t input_num = (input_defs.size() - 2) / 3;
for (size_t i = 0; i < input_num; i++) {
inputs_.push_back(NodeUnitIODef{*input_defs[3 * i + 2], NodeUnitIODef::QuantParam{*input_defs[3 * i + 3],
input_defs[3 * i + 4]}});
}
outputs_.push_back(NodeUnitIODef{*output_defs[0], NodeUnitIODef::QuantParam{*input_defs[0], input_defs[1]}});
} else {
ORT_THROW("The QLinear op [", static_cast<uint8_t>(qlinear_type), "] is not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <memory>
#include <vector>
#include <utility>
#include "core/providers/shared/utils/utils.h"
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"

Expand Down
19 changes: 16 additions & 3 deletions onnxruntime/core/providers/vsinpu/builders/impl/base_op_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <string>
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"

namespace onnxruntime {
Expand Down Expand Up @@ -49,13 +50,13 @@ bool BaseOpBuilder::HasSupportedInputOutputs(const InitializedTensorSet& initial
return false;
}

// We do not support dynamic shape input yet
// We do not support dynamic shape input yet, but resize op's second input can be empty cause we not care about this value
for (const auto& dim : shape_proto->dim()) {
if (!dim.has_dim_value()) {
LOGS_DEFAULT(WARNING) << "Dynamic shape is not supported for now, for input:" << node_arg.Name();
return false;
}
if (dim.dim_value() == 0) {
if (dim.dim_value() == 0 && op_type != "Resize") {
LOGS_DEFAULT(WARNING) << "Zero in shape is not supported for now, for input:" << node_arg.Name();
return false;
}
Expand Down Expand Up @@ -91,6 +92,10 @@ bool BaseOpBuilder::HasSupportedInputOutputs(const InitializedTensorSet& initial
return false;
if (!has_initialized_quant_param(*input.quant_param->zero_point, initializers))
return false;
if (input.quant_param->zero_point->Type() != input.node_arg.Type()) {
LOGS_DEFAULT(ERROR) << "Invalid input type because the data type mismatch with its' quant param type.";
return false;
}
}
}
}
Expand All @@ -115,7 +120,7 @@ bool BaseOpBuilder::HasSupportedInputOutputs(const InitializedTensorSet& initial

bool BaseOpBuilder::HasSupportedInputOutputsImpl(
const InitializedTensorSet& /* initializers */, const NodeUnit& node_unit) const {
// Check input data type, int64 is generally unsupported
// Check input/output data type, int64 is generally unsupported
// specific op builder can override this if the int64 input corresponds to VSINPU param
for (const auto& input : node_unit.Inputs()) {
auto input_type = input.node_arg.Type();
Expand All @@ -125,6 +130,14 @@ bool BaseOpBuilder::HasSupportedInputOutputsImpl(
return false;
}
}
for (const auto& output : node_unit.Outputs()) {
auto output_type = output.node_arg.Type();
if (*output_type == "tensor(int64)" || !util::IsTypeSupported(&output.node_arg)) {
LOGS_DEFAULT(WARNING) << node_unit.OpType() << " has unsupported output type : "
<< *output_type;
return false;
}
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*
*****************************************************************************/
#pragma once

#include <memory>
#include <vector>
#include "core/providers/vsinpu/builders/op_builder.h"
#include "core/providers/vsinpu/vsinpu_ep_graph.h"
#include "core/providers/vsinpu/vsinpu_util.h"
Expand Down Expand Up @@ -54,7 +55,7 @@ class BaseOpBuilder : public IOpBuilder {
const InitializedTensorSet& initializers, const NodeUnit& node_unit) const;

// TODO:Check if this node_unit's type is supported
virtual bool IsNodeUnitTypeSupported(const NodeUnit& node_unit) const { return true; };
virtual bool IsNodeUnitTypeSupported(const NodeUnit& node_unit) const { return true; }

virtual bool HandleBuildOp(
vsi::npu::GraphEP* graph_ep,
Expand Down
47 changes: 47 additions & 0 deletions onnxruntime/core/providers/vsinpu/builders/impl/cast_op_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/****************************************************************************
*
* Copyright (c) 2024 Vivante Corporation
*
* 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.
*
*****************************************************************************/
#include <memory>
#include <vector>
#include <utility>
#include "core/providers/shared/utils/utils.h"
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"
namespace onnxruntime {
namespace vsi {
namespace npu {
class CastOpBuilder : public BaseOpBuilder {
protected:
bool HandleBuildOp(vsi::npu::GraphEP* graph_ep, std::vector<std::shared_ptr<tim::vx::Tensor>>& inputs,
std::vector<std::shared_ptr<tim::vx::Tensor>>& outputs, const NodeUnit& node_unit) override {
LOGS_DEFAULT(VERBOSE) << "Creating Cast Op.";
NodeAttrHelper helper(node_unit.GetNode());
auto op = graph_ep->GetGraph()->CreateOperation<tim::vx::ops::DataConvert>();
(*op).BindInput(inputs[0]).BindOutputs(outputs);
graph_ep->GetOps().push_back(std::move(op));
return true;
}
};

} // namespace npu
} // namespace vsi
} // namespace onnxruntime
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <limits>
#include <utility>
#include "core/providers/vsinpu/builders/impl/clip_op_builder.h"

namespace onnxruntime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <memory>
#include <vector>
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"
#include "core/providers/shared/utils/utils.h"

namespace onnxruntime {
namespace vsi {
namespace npu {
class ClipOpBuilder final : public BaseOpBuilder {
bool IsOpSupported(const onnxruntime::GraphViewer& graph_viewer,
bool IsOpSupported(const onnxruntime::GraphViewer& graph_viewer,
const Node* node) const override {
if (node->SinceVersion() > 6) {
if (node->SinceVersion() > 6) {
if (node->InputDefs().size() > 1 && !Contains(graph_viewer.GetAllInitializedTensors(), node->InputDefs()[1]->Name())) {
LOGS_DEFAULT(WARNING) << "Min/Max value must be const input or attribute.";
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <memory>
#include <vector>
#include <utility>
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"
#include "core/providers/common.h"
#include "core/providers/shared/utils/utils.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <string>
#include <memory>
#include <vector>
#include <utility>
#include "core/providers/shared/utils/utils.h"
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"
namespace onnxruntime {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <memory>
#include <vector>
#include <utility>
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"
#include "core/providers/common.h"
#include "core/providers/shared/utils/utils.h"
Expand All @@ -36,7 +39,6 @@ class DequantizeLinearOpBuilder : public BaseOpBuilder {
};
bool HasSupportedInputOutputsImpl(const InitializedTensorSet& initializers,
const NodeUnit& node_unit) const override {

auto input_type = node_unit.Inputs()[0].node_arg.Type();
if (*input_type == "tensor(int64)" || !util::IsTypeSupported(&node_unit.Inputs()[0].node_arg)) {
LOGS_DEFAULT(WARNING) << node_unit.OpType() << " has unsupported input type : "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <memory>
#include <vector>
#include <utility>
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"
namespace onnxruntime {
namespace vsi {
Expand Down Expand Up @@ -85,7 +88,6 @@ class PowOpBuilder : public BaseOpBuilder {
(*op).BindInputs(inputs).BindOutputs(outputs);
graph_ep->GetOps().push_back(std::move(op));
return true;
;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include <memory>
#include <vector>
#include <utility>
#include "core/providers/vsinpu/builders/impl/base_op_builder.h"
#include "core/providers/common.h"
#include "core/providers/shared/utils/utils.h"
Expand All @@ -35,9 +38,9 @@ class FlattenOpBuilder : public BaseOpBuilder {
const NodeUnit& node_unit) override {
LOGS_DEFAULT(VERBOSE) << "Creating Flatten Op.";
std::vector<uint32_t> reshape_param;
if (outputs[0]->GetShape().size() == 2)
if (outputs[0]->GetShape().size() == 2) {
reshape_param = outputs[0]->GetShape();
else {
} else {
auto input_shape = inputs[0]->GetShape();
NodeAttrHelper helper(node_unit.GetNode());
int64_t axis = helper.Get("axis", 1);
Expand Down
Loading

0 comments on commit 340ca16

Please sign in to comment.