forked from OpenPPL/ppl.nn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_intro.cc
More file actions
144 lines (121 loc) · 5.25 KB
/
api_intro.cc
File metadata and controls
144 lines (121 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "ppl/nn/models/onnx/onnx_runtime_builder_factory.h"
#include "ppl/nn/engines/x86/engine_factory.h"
#include <random>
#include <iostream>
using namespace std;
using namespace ppl::nn;
using namespace ppl::common;
static bool SetRandomInputs(Runtime* runtime) {
for (uint32_t c = 0; c < runtime->GetInputCount(); ++c) {
auto t = runtime->GetInputTensor(c);
auto& shape = t->GetShape();
auto nr_element = shape.GetBytesIncludingPadding() / sizeof(float);
unique_ptr<float[]> buffer(new float[nr_element]);
// fill random input data
std::default_random_engine eng;
std::uniform_real_distribution<float> dis(-1.0f, 1.0f);
for (uint32_t i = 0; i < nr_element; ++i) {
buffer.get()[i] = dis(eng);
}
auto status = t->ReallocBuffer();
if (status != RC_SUCCESS) {
cerr << "ReallocBuffer for tensor[" << t->GetName() << "] failed: " << GetRetCodeStr(status) << endl;
return false;
}
// our random data is treated as NDARRAY
TensorShape src_desc = t->GetShape();
src_desc.SetDataFormat(DATAFORMAT_NDARRAY);
// input tensors may require different data format
status = t->ConvertFromHost(buffer.get(), src_desc);
if (status != RC_SUCCESS) {
cerr << "set tensor[" << t->GetName() << "] content failed: " << GetRetCodeStr(status) << endl;
return false;
}
}
return true;
}
static void PrintInputOutputInfo(const Runtime* runtime) {
cout << "----- input info -----" << endl;
for (uint32_t i = 0; i < runtime->GetInputCount(); ++i) {
auto tensor = runtime->GetInputTensor(i);
cout << "input[" << i << "]:" << endl << " name: " << tensor->GetName() << endl;
string dims_str;
auto& shape = tensor->GetShape();
for (uint32_t j = 0; j < shape.GetDimCount(); ++j) {
dims_str += " " + std::to_string(shape.GetDim(j));
}
cout << " dim(s):" << dims_str << endl
<< " DataType: " << GetDataTypeStr(shape.GetDataType()) << endl
<< " DataFormat: " << GetDataFormatStr(shape.GetDataFormat()) << endl
<< " BytesIncludePadding: " << shape.GetBytesIncludingPadding() << endl
<< " BytesExcludePadding: " << shape.GetBytesExcludingPadding() << endl;
}
cout << "----- output info -----" << endl;
for (uint32_t i = 0; i < runtime->GetOutputCount(); ++i) {
auto tensor = runtime->GetOutputTensor(i);
cout << "output[" << i << "]:" << endl << " name: " << tensor->GetName();
string dims_str;
auto& shape = tensor->GetShape();
for (uint32_t j = 0; j < shape.GetDimCount(); ++j) {
dims_str += " " + std::to_string(shape.GetDim(j));
}
cout << " dim(s):" << dims_str << endl
<< " DataType: " << GetDataTypeStr(shape.GetDataType()) << endl
<< " DataFormat: " << GetDataFormatStr(shape.GetDataFormat()) << endl
<< " BytesIncludePadding: " << shape.GetBytesIncludingPadding() << endl
<< " BytesExcludePadding: " << shape.GetBytesExcludingPadding() << endl;
}
cout << "----------------------" << endl;
}
int main(void) {
const char* model_file = "tests/testdata/conv.onnx";
auto x86_engine = X86EngineFactory::Create(X86EngineOptions());
vector<unique_ptr<Engine>> engines;
engines.emplace_back(unique_ptr<Engine>(x86_engine));
vector<Engine*> engine_ptrs(engines.size());
for (uint32_t i = 0; i < engines.size(); ++i) {
engine_ptrs[i] = engines[i].get();
}
auto builder = unique_ptr<RuntimeBuilder>(
OnnxRuntimeBuilderFactory::Create(model_file, engine_ptrs.data(), engine_ptrs.size()));
if (!builder) {
cerr << "create RuntimeBuilder failed." << endl;
return -1;
}
auto runtime = unique_ptr<Runtime>(builder->CreateRuntime());
if (!runtime) {
cerr << "CreateRuntime failed." << endl;
return -1;
}
if (!SetRandomInputs(runtime.get())) {
cerr << "SetRandomInputs failed." << endl;
return -1;
}
auto status = runtime->Run();
if (status != RC_SUCCESS) {
cerr << "Run() failed: " << GetRetCodeStr(status) << endl;
return -1;
}
status = runtime->Sync();
if (status != RC_SUCCESS) {
cerr << "Sync() failed: " << GetRetCodeStr(status) << endl;
}
PrintInputOutputInfo(runtime.get());
return 0;
}