MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / npz_load

Method npz_load

examples/cpp/xlnet/cnpy.cpp:253–308  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

251}
252
253cnpy::npz_t cnpy::npz_load(std::string fname)
254{
255 FILE* fp = fopen(fname.c_str(), "rb");
256
257 // if(!fp) {
258 // throw std::runtime_error("npz_load: Error! Unable to open file
259 // "+fname+"!");
260 //}
261
262 cnpy::npz_t arrays;
263 if (fp) {
264 while (1) {
265 std::vector<char> local_header(30);
266 size_t headerres = fread(&local_header[0], sizeof(char), 30, fp);
267 if (headerres != 30)
268 throw std::runtime_error("npz_load: failed fread");
269
270 // if we've reached the global header, stop reading
271 if (local_header[2] != 0x03 || local_header[3] != 0x04)
272 break;
273
274 // read in the variable name
275 uint16_t name_len = *(uint16_t*)&local_header[26];
276 std::string varname(name_len, ' ');
277 size_t vname_res = fread(&varname[0], sizeof(char), name_len, fp);
278 if (vname_res != name_len)
279 throw std::runtime_error("npz_load: failed fread");
280
281 // erase the lagging .npy
282 varname.erase(varname.end() - 4, varname.end());
283
284 // read in the extra field
285 uint16_t extra_field_len = *(uint16_t*)&local_header[28];
286 if (extra_field_len > 0) {
287 std::vector<char> buff(extra_field_len);
288 size_t efield_res = fread(&buff[0], sizeof(char), extra_field_len, fp);
289 if (efield_res != extra_field_len)
290 throw std::runtime_error("npz_load: failed fread");
291 }
292
293 uint16_t compr_method = *reinterpret_cast<uint16_t*>(&local_header[0] + 8);
294 uint32_t compr_bytes = *reinterpret_cast<uint32_t*>(&local_header[0] + 18);
295 uint32_t uncompr_bytes = *reinterpret_cast<uint32_t*>(&local_header[0] + 22);
296
297 if (compr_method == 0) {
298 arrays[varname] = load_the_npy_file(fp);
299 }
300 else {
301 arrays[varname] = load_the_npz_array(fp, compr_bytes, uncompr_bytes);
302 }
303 }
304 fclose(fp);
305 }
306
307 return arrays;
308}
309
310cnpy::NpyArray cnpy::npz_load(std::string fname, std::string varname)

Callers

nothing calls this directly

Calls 3

load_the_npy_fileFunction · 0.85
load_the_npz_arrayFunction · 0.85
endMethod · 0.45

Tested by

no test coverage detected