forked from microsoft/onnxruntime
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request microsoft#6 from chenfeiyue-cfy/vsinpu
Added pool/matmul/flatten ops
- Loading branch information
Showing
5 changed files
with
229 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
onnxruntime/core/providers/vsinpu/builders/impl/flatten_op_builder.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/**************************************************************************** | ||
* | ||
* 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 "core/providers/vsinpu/builders/impl/base_op_builder.h" | ||
#include "core/providers/common.h" | ||
#include "core/providers/shared/utils/utils.h" | ||
|
||
namespace onnxruntime { | ||
namespace vsi { | ||
namespace npu { | ||
class FlattenOpBuilder : public BaseOpBuilder { | ||
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 Node* node) override { | ||
LOGS_DEFAULT(VERBOSE) << "Creating Flatten Op."; | ||
std::vector<uint32_t> reshape_param; | ||
if (outputs[0]->GetShape().size() == 2) | ||
reshape_param = outputs[0]->GetShape(); | ||
else { | ||
auto input_shape = inputs[0]->GetShape(); | ||
NodeAttrHelper helper(*node); | ||
int64_t axis = helper.Get("axis", 0); | ||
if (axis < 0) { | ||
axis = HandleNegativeAxis(axis, inputs[0]->GetShape().size()); // handle negative and enforce axis is valid | ||
} | ||
uint32_t first_dim = 1; | ||
for (int64_t i = 0; i < axis; i++) { | ||
first_dim *= inputs[0]->GetShape()[i]; | ||
} | ||
uint32_t second_dim = inputs[0]->GetSpec().GetElementNum() / first_dim; | ||
reshape_param.push_back(first_dim); | ||
reshape_param.push_back(second_dim); | ||
} | ||
auto op = graph_ep->GetGraph()->CreateOperation<tim::vx::ops::Reshape>(reshape_param); | ||
(*op).BindInput(inputs[0]).BindOutput(outputs[0]); | ||
graph_ep->GetOps().push_back(std::move(op)); | ||
return true; | ||
} | ||
}; | ||
} // namespace npu | ||
|
||
} // namespace vsi | ||
} // namespace onnxruntime |
53 changes: 53 additions & 0 deletions
53
onnxruntime/core/providers/vsinpu/builders/impl/matmul_op_builder.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/**************************************************************************** | ||
* | ||
* 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 "core/providers/vsinpu/builders/impl/base_op_builder.h" | ||
|
||
namespace onnxruntime { | ||
namespace vsi { | ||
namespace npu { | ||
class MatMulOpBuilder : public BaseOpBuilder { | ||
bool IsOpSupported(const onnxruntime::GraphViewer& graph_viewer, | ||
const Node* node) const override { | ||
auto output_defs = node->OutputDefs(); | ||
if (output_defs[0]->Shape()->dim_size() == 0) { | ||
LOGS_DEFAULT(ERROR) << "Inner product of 1-D tensor is not supported in MatMul op."; | ||
return false; | ||
} | ||
return true; | ||
} | ||
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 Node* node) override { | ||
LOGS_DEFAULT(VERBOSE) << "Creating Matmul Op."; | ||
auto op = graph_ep->GetGraph()->CreateOperation<tim::vx::ops::Matmul>(); | ||
(*op).BindInputs(inputs).BindOutputs(outputs); | ||
graph_ep->GetOps().push_back(std::move(op)); | ||
return true; | ||
} | ||
}; | ||
} // namespace npu | ||
|
||
} // namespace vsi | ||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters