MCPcopy Create free account
hub / github.com/ml-explore/mlx-data / parse_numpy_format

Function parse_numpy_format

mlx/data/core/Numpy.cpp:76–296  ·  view source on GitHub ↗

parse a numpy format string like `{'descr': '<f2', 'fortran_order': False, 'shape': (4, 3), }`

Source from the content-addressed store, hash-verified

74/// parse a numpy format string like `{'descr': '<f2', 'fortran_order': False,
75/// 'shape': (4, 3), }`
76static Format parse_numpy_format(
77 std::vector<unsigned char>& format,
78 const std::string filename) {
79 Format result;
80 State state = START;
81
82 // current key and value as strings
83 std::string key = "";
84 std::string value = "";
85
86 // when reading tuples, current int and collection of ints
87 int64_t int_value = 0;
88 bool int_value_valid = false;
89 std::vector<int64_t> values;
90
91 for (auto c : format) {
92 Token t;
93
94 switch (c) {
95 case '{':
96 t = LEFT_BRACE;
97 break;
98 case '}':
99 t = RIGHT_BRACE;
100 break;
101 case '\'':
102 t = QUOTE;
103 break;
104 case ':':
105 t = COLON;
106 break;
107 case ',':
108 t = COMMA;
109 break;
110 case '(':
111 t = LEFT_PAREN;
112 break;
113 case ')':
114 t = RIGHT_PAREN;
115 break;
116 case ' ':
117 case '\n':
118 case '\t':
119 case '\r':
120 t = WHITESPACE;
121 break;
122 default:
123 t = CHARACTER;
124 break;
125 }
126
127 switch (state) {
128 case START:
129 if (t == LEFT_BRACE) {
130 state = READING_DICTIONARY;
131 } else {
132 throw std::runtime_error(
133 std::string("loadNumpy: bad format, expected '{', got ") +

Callers 1

load_numpyFunction · 0.85

Calls 1

clearMethod · 0.80

Tested by

no test coverage detected