Skip to content

Commit

Permalink
Update run.sh (#1412)
Browse files Browse the repository at this point in the history
* Update run.sh

* Update README.md

* Update README.md

* Add support for assign when dtype is double

* Support unsqueeze2

* update

* update README

* fixed bug

* add test
  • Loading branch information
Zheng-Bicheng authored Oct 17, 2024
1 parent 2146244 commit 3bb6cb8
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 161 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protobuf-*
*.pdmodel
*.pdiparams
*.pdiparams.info
*inference.yml
*.onnx
*.temptxt
tests/__pycache_*
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ Paddle2ONNX 在导出模型时,需要传入部署模型格式,包括两个
你可以通过使用命令行并通过以下命令将Paddle模型转换为ONNX模型

```bash
paddle2onnx --model_dir saved_inference_model \
--model_filename model.pdmodel \
--params_filename model.pdiparams \
paddle2onnx --model_dir model_dir \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--save_file model.onnx
```

Expand Down Expand Up @@ -72,9 +72,9 @@ paddle2onnx --model_dir saved_inference_model \

## 4.5 优化ONNX

如你对导出的 ONNX 模型有优化的需求,推荐使用 `onnx-simplifier`,也可使用如下命令对模型进行优化
如你对导出的 ONNX 模型有优化的需求,推荐使用 `onnxslim` 对模型进行优化:

```
```bash
pip install onnxslim
onnxslim model.onnx slim.onnx
```
Expand Down
2 changes: 1 addition & 1 deletion VERSION_NUMBER
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.10
1.2.11
17 changes: 5 additions & 12 deletions paddle2onnx/mapper/tensor/assign_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,21 @@
namespace paddle2onnx {
REGISTER_MAPPER(assign_value, AssignValueMapper)

int32_t AssignValueMapper::GetMinOpsetVersion(bool verbose) {
int32_t dtype = static_cast<int32_t>(dtype_);
if (dtype != P2ODataType::INT32 && dtype != P2ODataType::INT64 &&
dtype != P2ODataType::FP32) {
Error() << "Only supports int32/int64/float32." << std::endl;
return -1;
}
return 7;
}

void AssignValueMapper::Opset7() {
auto output_info = GetOutput("Out");
int32_t dtype = static_cast<int32_t>(dtype_);
if (dtype == P2ODataType::INT32) {
helper_->Assign(output_info[0].name, GetOnnxDtype(output_info[0].dtype),
shape_, int64_values_);
} else if (dtype == P2ODataType::INT64) {
helper_->Assign(output_info[0].name, GetOnnxDtype(output_info[0].dtype),
shape_, int64_values_);
} else if (dtype == P2ODataType::FP32) {
helper_->Assign(output_info[0].name, GetOnnxDtype(output_info[0].dtype),
shape_, fp32_values_);
} else if (dtype == P2ODataType::INT64) {
} else if (dtype == P2ODataType::FP64) {
helper_->Assign(output_info[0].name, GetOnnxDtype(output_info[0].dtype),
shape_, int64_values_);
shape_, double_values_);
}
}

Expand Down
79 changes: 41 additions & 38 deletions paddle2onnx/mapper/tensor/assign_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
// limitations under the License.

#pragma once
#include <functional>
#include <string>
#include <unordered_map>
#include <vector>

#include "paddle2onnx/mapper/mapper.h"
#include <unordered_map>
#include <functional>

namespace paddle2onnx {

Expand All @@ -30,65 +31,67 @@ class AssignValueMapper : public Mapper {
GetAttr("shape", &shape_);
GetAttrValues();
}
int32_t GetMinOpsetVersion(bool verbose) override;

void Opset7() override;

private:
void GetAttrValues(){
void GetAttrValues() {
int32_t dtype = static_cast<int32_t>(dtype_);
const std::string attr_name = HasAttr("values") ? "values" : GetAttrNameByDtype(dtype);
const std::string attr_name =
HasAttr("values") ? "values" : GetAttrNameByDtype(dtype);
std::unordered_map<int32_t, std::function<void()>> type_handlers = {
{P2ODataType::INT32, [&](){
if (attr_name == "values") GetScalars(attr_name, &int64_values_);
else if (attr_name == "int32_values") GetAttr(attr_name, &int64_values_);
}},
{P2ODataType::INT64, [&](){
if (attr_name == "values") GetScalars(attr_name, &int64_values_);
else if (attr_name == "int64_values") GetAttr(attr_name, &int64_values_);
}},
{P2ODataType::FP32, [&](){
if (attr_name == "values") GetScalars(attr_name, &fp32_values_);
else if (attr_name == "fp32_values") GetAttr(attr_name, &fp32_values_);
}},
{P2ODataType::FP64, [&](){
if (attr_name == "values") GetScalars(attr_name, &double_values_);
else if (attr_name == "fp32_values") GetAttr(attr_name, &double_values_);
}},
{P2ODataType::BOOL, [&](){
if (attr_name == "values") GetScalars(attr_name, &bool_values_);
else if (attr_name == "bool_values") GetAttr(attr_name, &bool_values_);
}},
{P2ODataType::INT32,
[&]() {
if (attr_name == "values")
GetScalars(attr_name, &int64_values_);
else if (attr_name == GetAttrNameByDtype(dtype_))
GetAttr(attr_name, &int64_values_);
}},
{P2ODataType::INT64,
[&]() {
if (attr_name == "values")
GetScalars(attr_name, &int64_values_);
else if (attr_name == GetAttrNameByDtype(dtype_))
GetAttr(attr_name, &int64_values_);
}},
{P2ODataType::FP32,
[&]() {
if (attr_name == "values")
GetScalars(attr_name, &fp32_values_);
else if (attr_name == GetAttrNameByDtype(dtype_))
GetAttr(attr_name, &fp32_values_);
}},
{P2ODataType::FP64,
[&]() {
if (attr_name == "values")
GetScalars(attr_name, &double_values_);
else if (attr_name == GetAttrNameByDtype(dtype_))
GetAttr(attr_name, &double_values_);
}},
};

auto handler = type_handlers.find(dtype);
if (handler != type_handlers.end()) {
handler->second();
} else {
Error() << "Unsupported dtype value" << std::endl;
}
Assert(handler != type_handlers.end(), "Unsupported dtype value");
handler->second();
}

std::string GetAttrNameByDtype(int32_t dtype) {
if (dtype == P2ODataType::INT32) {
return "int32_values";
} else if (dtype == P2ODataType::INT64) {
return "int64_values";
}else if (dtype == P2ODataType::FP32) {
} else if (dtype == P2ODataType::FP32) {
return "fp32_values";
} else if (dtype == P2ODataType::FP64) {
return "double_values";
} else if (dtype == P2ODataType::BOOL) {
return "bool_values";
}
Error() << "Unsupported dtype value" << std::endl;

Assert(false, "Only supports int32/int64/fp32/fp64.");
return "";
}

std::vector<float> fp32_values_;
std::vector<int64_t> int64_values_;
std::vector<bool> bool_values_;
std::vector<float> fp32_values_;
std::vector<double> double_values_;
std::vector<int32_t> int32_values_;
std::vector<int64_t> shape_;
int64_t dtype_;
};
Expand Down
63 changes: 42 additions & 21 deletions paddle2onnx/mapper/tensor/unsqueeze2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@ namespace paddle2onnx {
REGISTER_MAPPER(unsqueeze2, Unsqueeze2Mapper)

int32_t Unsqueeze2Mapper::GetMinOpsetVersion(bool verbose) {
if (axes_.size() == 0) {
if (HasInput("AxesTensorList")) {
Logger(verbose, 13) << "While AxisTensorList as input, "
<< RequireOpset(13) << std::endl;
return 13;
} else if (HasInput("AxesTensor")) {
auto info = GetInput("AxesTensor");
if (!IsConstantInput("AxesTensor")) {
Logger(verbose, 13)
<< "While AxesTensor as input, and it's not a constant tensor, "
<< RequireOpset(13) << std::endl;
return 13;
} else {
return 7;
}
int32_t opset = 7;
if (!axes_.empty()) {
return opset;
}

opset = 13;
if (HasInput("AxesTensorList")) {
opset = 13;
} else if (HasInput("AxesTensor")) {
auto info = GetInput("AxesTensor");
if (!IsConstantInput("AxesTensor")) {
opset = 13;
}
}
return 7;
return opset;
}

void Unsqueeze2Mapper::Opset7() {
Expand All @@ -44,9 +41,17 @@ void Unsqueeze2Mapper::Opset7() {

std::vector<int64_t> axes;
if (axes_.empty()) {
Assert(TryGetInputValue("AxesTensor", &axes),
"While unsqueeze2 has input AxesTensor, it cannot be exported by "
"Paddle2ONNX");
if (HasInput("AxesTensor")) {
Assert(TryGetInputValue("AxesTensor", &axes),
"While unsqueeze2 has input AxesTensor, it cannot be exported by "
"Paddle2ONNX");
} else {
Warn() << "AxesTensor not found, using Identity instead of Unsqueeze."
<< std::endl;
helper_->MakeNode("Identity", {input_info[0].name},
{output_info[0].name});
return;
}
} else {
axes.assign(axes_.begin(), axes_.end());
}
Expand All @@ -64,10 +69,19 @@ void Unsqueeze2Mapper::Opset13() {

std::vector<int64_t> axes;
if (axes_.empty()) {
TryGetInputValue("AxesTensor", &axes);
if (HasInput("AxesTensor")) {
TryGetInputValue("AxesTensor", &axes);
} else {
Warn() << "AxesTensor not found, using Identity instead of Unsqueeze."
<< std::endl;
helper_->MakeNode("Identity", {input_info[0].name},
{output_info[0].name});
return;
}
} else {
axes.assign(axes_.begin(), axes_.end());
}

for (size_t i = 0; i < axes.size(); ++i) {
if (axes[i] < 0) {
axes[i] = axes[i] + input_info[0].Rank() + i + 1;
Expand All @@ -81,10 +95,17 @@ void Unsqueeze2Mapper::Opset13() {
if (HasInput("AxesTensorList")) {
auto info = GetInput("AxesTensorList");
axes_node = helper_->ConcatIndices(info);
} else {
} else if (HasInput("AxesTensor")) {
auto info = GetInput("AxesTensor");
axes_node =
helper_->AutoCast(info[0].name, info[0].dtype, P2ODataType::INT64);
} else {
Warn() << "AxesTensor or AxesTensor not found, using Identity "
"instead of Unsqueeze."
<< std::endl;
helper_->MakeNode("Identity", {input_info[0].name},
{output_info[0].name});
return;
}
helper_->MakeNode("Unsqueeze", {input_info[0].name, axes_node},
{output_info[0].name});
Expand Down
1 change: 0 additions & 1 deletion tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ ignore="test_auto_scan_multiclass_nms.py
test_nn_Upsample.py \
test_normalize.py \
test_scatter_nd_add.py \
test_unsqueeze.py \
test_quantize_model.py \
test_quantize_model_minist.py \
test_quantize_model_speedup.py \
Expand Down
29 changes: 18 additions & 11 deletions tests/test_assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@ def forward(self, inputs):
return x


def test_assign_9():
def test_assign_int32():
"""
api: paddle.assign
op version: 9
op version: 7
"""
op = Net()
op.eval()
# net, name, ver_list, delta=1e-6, rtol=1e-5
obj = APIOnnx(op, 'assign', [9])
obj = APIOnnx(op, 'assign', [7])
obj.set_input_data(
"input_data",
paddle.to_tensor(randtool("float", -1, 1, [3, 10]).astype('float32')))
paddle.to_tensor(randtool("float", -1, 1, [3, 10]).astype('int32')))
obj.run()


def test_assign_10():
def test_assign_int64():
"""
api: paddle.assign
op version: 10
op version: 7
"""
op = Net()
op.eval()
# net, name, ver_list, delta=1e-6, rtol=1e-5
obj = APIOnnx(op, 'assign', [10])
obj.set_input_data(
"input_data",
paddle.to_tensor(randtool("float", -1, 1, [3, 10]).astype('float32')))
paddle.to_tensor(randtool("float", -1, 1, [3, 10]).astype('int64')))
obj.run()


def test_assign_11():
def test_assign_fp32():
"""
api: paddle.assign
op version: 11
op version: 7
"""
op = Net()
op.eval()
Expand All @@ -78,7 +78,7 @@ def test_assign_11():
obj.run()


def test_assign_12():
def test_assign_fp64():
"""
api: paddle.assign
op version: 12
Expand All @@ -89,5 +89,12 @@ def test_assign_12():
obj = APIOnnx(op, 'assign', [12])
obj.set_input_data(
"input_data",
paddle.to_tensor(randtool("float", -1, 1, [3, 10]).astype('float32')))
paddle.to_tensor(randtool("float", -1, 1, [3, 10]).astype('float64')))
obj.run()


if __name__ == "__main__":
test_assign_int32()
test_assign_int64()
test_assign_fp32()
test_assign_fp64()
Loading

0 comments on commit 3bb6cb8

Please sign in to comment.