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

Method parse_npy_header

examples/cpp/xlnet/cnpy.cpp:86–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

84}
85
86void cnpy::parse_npy_header(unsigned char* buffer, size_t& word_size, std::vector<size_t>& shape, bool& fortran_order)
87{
88 uint16_t header_len = *reinterpret_cast<uint16_t*>(buffer + 8);
89 std::string header(reinterpret_cast<char*>(buffer + 9), header_len);
90
91 size_t loc1, loc2;
92
93 // fortran order
94 loc1 = header.find("fortran_order") + 16;
95 fortran_order = (header.substr(loc1, 4) == "True" ? true : false);
96
97 // shape
98 loc1 = header.find("(");
99 loc2 = header.find(")");
100
101 std::regex num_regex("[0-9][0-9]*");
102 std::smatch sm;
103 shape.clear();
104
105 std::string str_shape = header.substr(loc1 + 1, loc2 - loc1 - 1);
106 while (std::regex_search(str_shape, sm, num_regex)) {
107 shape.push_back(std::stoi(sm[0].str()));
108 str_shape = sm.suffix().str();
109 }
110
111 // endian, word size, data type
112 // byte order code | stands for not applicable.
113 // not sure when this applies except for byte array
114 loc1 = header.find("descr") + 9;
115 bool littleEndian = (header[loc1] == '<' || header[loc1] == '|' ? true : false);
116 assert(littleEndian);
117
118 // char type = header[loc1+1];
119 // assert(type == map_type(T));
120
121 std::string str_ws = header.substr(loc1 + 2);
122 loc2 = str_ws.find("'");
123 word_size = atoi(str_ws.substr(0, loc2).c_str());
124}
125
126void cnpy::parse_npy_header(FILE* fp, size_t& word_size, std::vector<size_t>& shape, bool& fortran_order)
127{

Callers

nothing calls this directly

Calls 2

clearMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected