MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / parse_npy

Method parse_npy

lite/load_and_run/src/helpers/data_parser.cpp:151–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

149}
150
151void DataParser::parse_npy(const std::string& name, const std::string& path) {
152 std::string type_str;
153 std::vector<npy::ndarray_len_t> stl_shape;
154 std::vector<int8_t> raw;
155 npy::LoadArrayFromNumpy(path, type_str, stl_shape, raw);
156
157 megdnn::SmallVector<size_t> shape;
158 for (auto val : stl_shape) {
159 shape.append({static_cast<size_t>(val)});
160 }
161
162 const std::map<std::string, megdnn::DType> type_map = {
163 {"f4", mgb::dtype::Float32()}, {"i4", mgb::dtype::Int32()},
164 {"i2", mgb::dtype::Int16()}, {"u2", mgb::dtype::Uint16()},
165 {"i1", mgb::dtype::Int8()}, {"u1", mgb::dtype::Uint8()}};
166
167 megdnn::DType hv_type;
168 for (auto& item : type_map) {
169 if (type_str.find(item.first) != std::string::npos) {
170 hv_type = item.second;
171 break;
172 }
173 }
174
175 mgb::HostTensorND hv;
176 hv.comp_node(mgb::CompNode::default_cpu(), true).dtype(hv_type).resize(shape);
177 mgb::dt_byte* raw_ptr = hv.raw_ptr();
178 memcpy(raw_ptr, raw.data(), raw.size());
179
180 inputs.insert(std::make_pair(name, std::move(hv)));
181}
182
183void DataParser::parse_string(const std::string& name, const std::string& str) {
184 //! parse shape

Callers

nothing calls this directly

Calls 10

resizeMethod · 0.80
LoadArrayFromNumpyFunction · 0.70
appendMethod · 0.45
findMethod · 0.45
dtypeMethod · 0.45
comp_nodeMethod · 0.45
raw_ptrMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected