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

Function slice

mlx/data/Array.cpp:301–322  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

299}
300
301std::shared_ptr<Array> slice(
302 const std::shared_ptr<const Array>& arr,
303 int index0) {
304 // build a new shape dropping the first dimension
305 auto new_shape = std::vector<int64_t>();
306 auto current_shape = arr->shape();
307
308 for (int i = 1; i < current_shape.size(); i++) {
309 new_shape.push_back(current_shape[i]);
310 }
311
312 int64_t slice_size = (new_shape.size() > 0);
313 for (auto dim : new_shape) {
314 slice_size *= dim;
315 }
316
317 // return a slice of this at the given index
318 auto item_size = arr->itemsize();
319 auto new_pointer = ((char*)arr->data() + index0 * item_size * slice_size);
320 std::shared_ptr<void> new_data((void*)new_pointer, [](void*) {});
321 return std::make_shared<Array>(arr->type(), new_shape, new_data);
322}
323
324void copy(
325 const std::shared_ptr<Array>& dst,

Callers 3

loadFunction · 0.85
apply_videoMethod · 0.85
applyMethod · 0.85

Calls 5

itemsizeMethod · 0.80
typeMethod · 0.80
shapeMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected