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

Method reshape

mlx/data/Array.cpp:167–196  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

165}
166
167void Array::reshape(const std::vector<int64_t>& shape) {
168 std::vector<int64_t> new_shape = shape;
169 int64_t new_size = 1;
170 int infer_dim = -1;
171 for (int dim = 0; dim < new_shape.size(); dim++) {
172 if (new_shape[dim] < 0) {
173 if (infer_dim >= 0) {
174 throw std::runtime_error("Array: can infer only one dimension");
175 } else {
176 infer_dim = dim;
177 }
178 } else {
179 new_size *= new_shape[dim];
180 }
181 }
182 int64_t old_size = size();
183 if (infer_dim >= 0) {
184 if (old_size % new_size == 0) {
185 new_shape[infer_dim] = old_size / new_size;
186 new_size *= new_shape[infer_dim];
187 } else {
188 throw std::runtime_error(
189 "Array: cannot infer dimension: incompatible shape provided");
190 }
191 }
192 if (old_size != new_size) {
193 throw std::runtime_error("Array: incompatible shape provided");
194 }
195 shape_ = new_shape;
196}
197
198Array::~Array() {}
199

Callers 4

_downloadFunction · 0.80
downloadFunction · 0.80
test_sliceMethod · 0.80
reshapeFunction · 0.80

Calls 1

sizeMethod · 0.45

Tested by 1

test_sliceMethod · 0.64