| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "save_onnx.h" |
| 16 | |
| 17 | #include "onnx-ml.pb.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | #include <fstream> |
| 21 | #include <iostream> |
| 22 | |
| 23 | #include "utils.h" |
| 24 | |
| 25 | namespace pnnx { |
| 26 | |
| 27 | int save_onnx(const Graph& g, const char* onnxpath, int fp16) |
| 28 | { |
| 29 | onnx::ModelProto model; |
| 30 | |
| 31 | onnx::GraphProto* gp = model.mutable_graph(); |
| 32 | |
| 33 | for (const Operand* x : g.operands) |
| 34 | { |
| 35 | onnx::ValueInfoProto* vip = gp->add_value_info(); |
| 36 | |
| 37 | vip->set_name(x->name); |
| 38 | |
| 39 | onnx::TypeProto* tp = vip->mutable_type(); |
| 40 | |
| 41 | onnx::TypeProto_Tensor* tpt = tp->mutable_tensor_type(); |
| 42 | |
| 43 | switch (x->type) |
| 44 | { |
| 45 | case 1: // f32 |
| 46 | tpt->set_elem_type(fp16 ? 10 : 1); |
| 47 | break; |
| 48 | case 2: // f64 |
| 49 | tpt->set_elem_type(fp16 ? 10 : 11); |
| 50 | break; |
| 51 | case 3: // f16 |
| 52 | tpt->set_elem_type(10); |
| 53 | break; |
| 54 | case 4: // i32 |
| 55 | tpt->set_elem_type(6); |
| 56 | break; |
| 57 | case 5: // i64 |
| 58 | tpt->set_elem_type(7); |
| 59 | break; |
| 60 | case 6: // i16 |
| 61 | tpt->set_elem_type(5); |
| 62 | break; |
| 63 | case 7: // i8 |
| 64 | tpt->set_elem_type(3); |
| 65 | break; |
| 66 | case 8: // u8 |
| 67 | tpt->set_elem_type(2); |
| 68 | break; |
| 69 | case 9: // bool |
| 70 | tpt->set_elem_type(9); |
| 71 | break; |
| 72 | case 10: // cp64 |
| 73 | tpt->set_elem_type(14); |
| 74 | break; |
| 75 | case 11: // cp128 |
| 76 | tpt->set_elem_type(15); |
| 77 | break; |
| 78 | case 12: // cp32 |
| 79 | tpt->set_elem_type(0); |
| 80 | break; |
| 81 | default: // null |
| 82 | tpt->set_elem_type(0); |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | onnx::TensorShapeProto* tsp = tpt->mutable_shape(); |
| 87 | |
| 88 | for (auto s : x->shape) |
| 89 | { |
| 90 | onnx::TensorShapeProto_Dimension* tspd = tsp->add_dim(); |
| 91 | |
| 92 | tspd->set_dim_value(s); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | for (const Operator* op : g.ops) |
| 97 | { |
| 98 | onnx::NodeProto* np = gp->add_node(); |
| 99 | |
| 100 | np->set_op_type(op->type); |
| 101 | np->set_name(op->name); |
| 102 | |
| 103 | for (const Operand* oprand : op->inputs) |
| 104 | { |
| 105 | np->add_input(oprand->name); |
| 106 | } |
| 107 | |
| 108 | for (const Operand* oprand : op->outputs) |
| 109 | { |
| 110 | np->add_output(oprand->name); |
| 111 | } |
| 112 | |
| 113 | for (const auto& it : op->params) |
| 114 | { |
| 115 | const Parameter& param = it.second; |
| 116 | |
| 117 | onnx::AttributeProto* ap = np->add_attribute(); |
| 118 | |
| 119 | ap->set_name(it.first); |
| 120 | |
| 121 | if (param.type == 0) |
| 122 | { |
| 123 | ap->set_type(onnx::AttributeProto::STRING); |
| 124 | ap->set_s("None"); |
| 125 | } |
| 126 | if (param.type == 1) |
| 127 | { |
| 128 | ap->set_type(onnx::AttributeProto::INT); |
| 129 | if (param.b) |
| 130 | ap->set_i(1); |
| 131 | else |
| 132 | ap->set_i(0); |
| 133 | } |
| 134 | if (param.type == 2) |
| 135 | { |
| 136 | ap->set_type(onnx::AttributeProto::INT); |
| 137 | ap->set_i(param.i); |
| 138 | } |
| 139 | if (param.type == 3) |
| 140 | { |
| 141 | ap->set_type(onnx::AttributeProto::FLOAT); |
| 142 | ap->set_f(param.f); |
| 143 | } |
| 144 | if (param.type == 4) |
| 145 | { |
| 146 | ap->set_type(onnx::AttributeProto::STRING); |
| 147 | ap->set_s(param.s); |
| 148 | } |
| 149 | if (param.type == 5) |
| 150 | { |
| 151 | ap->set_type(onnx::AttributeProto::INTS); |
| 152 | for (auto i : param.ai) |
| 153 | { |
| 154 | ap->add_ints(i); |
| 155 | } |
| 156 | } |
| 157 | if (param.type == 6) |
| 158 | { |
| 159 | ap->set_type(onnx::AttributeProto::FLOATS); |
| 160 | for (auto f : param.af) |
| 161 | { |
| 162 | ap->add_floats(f); |
| 163 | } |
| 164 | } |
| 165 | if (param.type == 7) |
| 166 | { |
| 167 | ap->set_type(onnx::AttributeProto::STRINGS); |
| 168 | for (auto s : param.as) |
| 169 | { |
| 170 | ap->add_strings(s); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | for (const auto& it : op->attrs) |
| 176 | { |
| 177 | onnx::TensorProto* tp = gp->add_initializer(); |
| 178 | |
| 179 | tp->set_name(op->name + "." + it.first); |
| 180 | |
| 181 | np->add_input(op->name + "." + it.first); |
| 182 | |
| 183 | const Attribute& attr = it.second; |
| 184 | for (auto s : attr.shape) |
| 185 | { |
| 186 | tp->add_dims(s); |
| 187 | } |
| 188 | |
| 189 | switch (attr.type) |
| 190 | { |
| 191 | case 1: // f32 |
| 192 | tp->set_data_type(fp16 ? 10 : 1); |
| 193 | break; |
| 194 | case 2: // f64 |
| 195 | tp->set_data_type(fp16 ? 10 : 11); |
| 196 | break; |
| 197 | case 3: // f16 |
| 198 | tp->set_data_type(10); |
| 199 | break; |
| 200 | case 4: // i32 |
| 201 | tp->set_data_type(6); |
| 202 | break; |
| 203 | case 5: // i64 |
| 204 | tp->set_data_type(7); |
| 205 | break; |
| 206 | case 6: // i16 |
| 207 | tp->set_data_type(5); |
| 208 | break; |
| 209 | case 7: // i8 |
| 210 | tp->set_data_type(3); |
| 211 | break; |
| 212 | case 8: // u8 |
| 213 | tp->set_data_type(2); |
| 214 | break; |
| 215 | case 9: // bool |
| 216 | tp->set_data_type(9); |
| 217 | break; |
| 218 | case 10: // cp64 |
| 219 | tp->set_data_type(14); |
| 220 | break; |
| 221 | case 11: // cp128 |
| 222 | tp->set_data_type(15); |
| 223 | break; |
| 224 | case 12: // cp32 |
| 225 | tp->set_data_type(0); |
| 226 | break; |
| 227 | default: // null |
| 228 | tp->set_data_type(0); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | std::string* d = tp->mutable_raw_data(); |
| 233 | if (fp16 && attr.type == 1) |
| 234 | { |
| 235 | // fp32 to fp16 |
| 236 | const float* p = (const float*)attr.data.data(); |
| 237 | int len = attr.data.size() / 4; |
| 238 | d->resize(len * 2); |
| 239 | unsigned short* p_fp16 = (unsigned short*)d->data(); |
| 240 | for (int i = 0; i < len; i++) |
| 241 | { |
| 242 | p_fp16[i] = float32_to_float16(p[i]); |
| 243 | } |
| 244 | } |
| 245 | else if (fp16 && attr.type == 2) |
| 246 | { |
| 247 | // fp64 to fp16 |
| 248 | const double* p = (const double*)attr.data.data(); |
| 249 | int len = attr.data.size() / 4; |
| 250 | d->resize(len); |
| 251 | unsigned short* p_fp16 = (unsigned short*)d->data(); |
| 252 | for (int i = 0; i < len; i++) |
| 253 | { |
| 254 | p_fp16[i] = float32_to_float16((float)p[i]); |
| 255 | } |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | d->resize(attr.data.size()); |
| 260 | memcpy((void*)d->data(), attr.data.data(), attr.data.size()); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | std::fstream output(onnxpath, std::ios::out | std::ios::trunc | std::ios::binary); |
| 266 | if (!model.SerializeToOstream(&output)) |
| 267 | { |
| 268 | fprintf(stderr, "write onnx failed\n"); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | } // namespace pnnx |