mlx-sys 0.0.10-alpha

Rust bindings for mlx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
// Copyright © 2023 Apple Inc.

#include <numeric>
#include <sstream>

#include "python/src/indexing.h"

#include "mlx/ops.h"

bool is_none_slice(const py::slice& in_slice) {
  return (
      py::getattr(in_slice, "start").is_none() &&
      py::getattr(in_slice, "stop").is_none() &&
      py::getattr(in_slice, "step").is_none());
}

int get_slice_int(py::object obj, int default_val) {
  if (!obj.is_none()) {
    if (!py::isinstance<py::int_>(obj)) {
      throw std::invalid_argument("Slice indices must be integers or None.");
    }
    return py::cast<int>(py::cast<py::int_>(obj));
  }
  return default_val;
}

void get_slice_params(
    int& starts,
    int& ends,
    int& strides,
    const py::slice& in_slice,
    int axis_size) {
  // Following numpy's convention
  //    Assume n is the number of elements in the dimension being sliced.
  //    Then, if i is not given it defaults to 0 for k > 0 and n - 1 for
  //    k < 0 . If j is not given it defaults to n for k > 0 and -n-1 for
  //    k < 0 . If k is not given it defaults to 1

  strides = get_slice_int(py::getattr(in_slice, "step"), 1);
  starts = get_slice_int(
      py::getattr(in_slice, "start"), strides < 0 ? axis_size - 1 : 0);
  ends = get_slice_int(
      py::getattr(in_slice, "stop"), strides < 0 ? -axis_size - 1 : axis_size);
}

array get_int_index(py::object idx, int axis_size) {
  int idx_ = py::cast<int>(idx);
  idx_ = (idx_ < 0) ? idx_ + axis_size : idx_;

  return array(idx_, uint32);
}

bool is_valid_index_type(const py::object& obj) {
  return py::isinstance<py::slice>(obj) || py::isinstance<py::int_>(obj) ||
      py::isinstance<array>(obj) || obj.is_none() || py::ellipsis().is(obj);
}

array mlx_get_item_slice(const array& src, const py::slice& in_slice) {
  // Check input and raise error if 0 dim for parity with np
  if (src.ndim() == 0) {
    throw std::invalid_argument(
        "too many indices for array: array is 0-dimensional");
  }

  // Return a copy of the array if none slice is request
  if (is_none_slice(in_slice)) {
    return src;
  }

  std::vector<int> starts(src.ndim(), 0);
  std::vector<int> ends = src.shape();
  std::vector<int> strides(src.ndim(), 1);

  // Check and update slice params
  get_slice_params(starts[0], ends[0], strides[0], in_slice, ends[0]);
  return slice(src, starts, ends, strides);
}

array mlx_get_item_array(const array& src, const array& indices) {
  // Check input and raise error if 0 dim for parity with np
  if (src.ndim() == 0) {
    throw std::invalid_argument(
        "too many indices for array: array is 0-dimensional");
  }

  if (indices.dtype() == bool_) {
    throw std::invalid_argument("boolean indices are not yet supported");
  }

  // If only one input array is mentioned, we set axis=0 in take
  // for parity with np
  return take(src, indices, 0);
}

array mlx_get_item_int(const array& src, const py::int_& idx) {
  // Check input and raise error if 0 dim for parity with np
  if (src.ndim() == 0) {
    throw std::invalid_argument(
        "too many indices for array: array is 0-dimensional");
  }

  // If only one input idx is mentioned, we set axis=0 in take
  // for parity with np
  return take(src, get_int_index(idx, src.shape(0)), 0);
}

array mlx_gather_nd(
    array src,
    const std::vector<py::object>& indices,
    bool gather_first,
    int& max_dims) {
  max_dims = 0;
  std::vector<array> gather_indices;
  std::vector<bool> is_slice(indices.size(), false);
  int num_slices = 0;
  // gather all the arrays
  for (int i = 0; i < indices.size(); i++) {
    auto& idx = indices[i];

    if (py::isinstance<py::slice>(idx)) {
      int start, end, stride;
      get_slice_params(start, end, stride, idx, src.shape(i));

      // Handle negative indices
      start = (start < 0) ? start + src.shape(i) : start;
      end = (end < 0) ? end + src.shape(i) : end;

      gather_indices.push_back(arange(start, end, stride, uint32));
      num_slices++;
      is_slice[i] = true;
    } else if (py::isinstance<py::int_>(idx)) {
      gather_indices.push_back(get_int_index(idx, src.shape(i)));
    } else if (py::isinstance<array>(idx)) {
      auto arr = py::cast<array>(idx);
      max_dims = std::max(static_cast<int>(arr.ndim()), max_dims);
      gather_indices.push_back(arr);
    }
  }

  // reshape them so that the int/array indices are first
  if (gather_first) {
    int slice_index = 0;
    for (int i = 0; i < gather_indices.size(); i++) {
      if (is_slice[i]) {
        std::vector<int> index_shape(max_dims + num_slices, 1);
        index_shape[max_dims + slice_index] = gather_indices[i].shape(0);
        gather_indices[i] = reshape(gather_indices[i], index_shape);
        slice_index++;
      } else {
        std::vector<int> index_shape = gather_indices[i].shape();
        index_shape.insert(index_shape.end(), num_slices, 1);
        gather_indices[i] = reshape(gather_indices[i], index_shape);
      }
    }
  } else {
    // reshape them so that the int/array indices are last
    for (int i = 0; i < gather_indices.size(); i++) {
      if (i < num_slices) {
        std::vector<int> index_shape(max_dims + num_slices, 1);
        index_shape[i] = gather_indices[i].shape(0);
        gather_indices[i] = reshape(gather_indices[i], index_shape);
      }
    }
  }

  // Do the gather
  std::vector<int> axes(indices.size());
  std::iota(axes.begin(), axes.end(), 0);
  std::vector<int> slice_sizes = src.shape();
  std::fill(slice_sizes.begin(), slice_sizes.begin() + indices.size(), 1);
  src = gather(src, gather_indices, axes, slice_sizes);

  // Squeeze the dims
  std::vector<int> out_shape;
  out_shape.insert(
      out_shape.end(),
      src.shape().begin(),
      src.shape().begin() + max_dims + num_slices);
  out_shape.insert(
      out_shape.end(),
      src.shape().begin() + max_dims + num_slices + indices.size(),
      src.shape().end());
  src = reshape(src, out_shape);

  return src;
}

array mlx_get_item_nd(array src, const py::tuple& entries) {
  // No indices make this a noop
  if (entries.size() == 0) {
    return src;
  }

  // The plan is as follows:
  // 1. Replace the ellipsis with a series of slice(None)
  // 2. Loop over the indices and calculate the gather indices
  // 3. Calculate the remaining slices and reshapes

  // Ellipsis handling
  std::vector<py::object> indices;
  {
    int non_none_indices_before = 0;
    int non_none_indices_after = 0;
    std::vector<py::object> r_indices;
    int i = 0;
    for (; i < entries.size(); i++) {
      auto idx = entries[i];
      if (!is_valid_index_type(idx)) {
        throw std::invalid_argument(
            "Cannot index mlx array using the given type yet");
      }
      if (!py::ellipsis().is(idx)) {
        indices.push_back(idx);
        non_none_indices_before += !idx.is_none();
      } else {
        break;
      }
    }
    for (int j = entries.size() - 1; j > i; j--) {
      auto idx = entries[j];
      if (!is_valid_index_type(idx)) {
        throw std::invalid_argument(
            "Cannot index mlx array using the given type yet");
      }
      if (py::ellipsis().is(idx)) {
        throw std::invalid_argument(
            "An index can only have a single ellipsis (...)");
      }
      r_indices.push_back(idx);
      non_none_indices_after += !idx.is_none();
    }
    for (int axis = non_none_indices_before;
         axis < src.ndim() - non_none_indices_after;
         axis++) {
      indices.push_back(py::slice(0, src.shape(axis), 1));
    }
    indices.insert(indices.end(), r_indices.rbegin(), r_indices.rend());
  }

  // Check for the number of indices passed
  {
    int cnt = src.ndim();
    for (auto& idx : indices) {
      if (!idx.is_none()) {
        cnt--;
      }
    }
    if (cnt < 0) {
      std::ostringstream msg;
      msg << "Too many indices for array with " << src.ndim() << "dimensions.";
      throw std::invalid_argument(msg.str());
    }
  }

  // Gather handling
  //
  // Check whether we have arrays or integer indices and delegate to gather_nd
  // after removing the slices at the end and all Nones.
  std::vector<py::object> remaining_indices;
  bool have_array = false;
  {
    // First check whether the results of gather are going to be 1st or
    // normally in between.
    bool have_non_array = false;
    bool gather_first = false;
    for (auto& idx : indices) {
      if (py::isinstance<array>(idx) || py::isinstance<py::int_>(idx)) {
        if (have_array && have_non_array) {
          gather_first = true;
          break;
        }
        have_array = true;
      } else {
        have_non_array |= have_array;
      }
    }

    if (have_array) {
      int last_array;
      // Then find the last array
      for (last_array = indices.size() - 1; last_array >= 0; last_array--) {
        auto& idx = indices[last_array];
        if (py::isinstance<array>(idx) || py::isinstance<py::int_>(idx)) {
          break;
        }
      }

      std::vector<py::object> gather_indices;
      for (int i = 0; i <= last_array; i++) {
        auto& idx = indices[i];
        if (!idx.is_none()) {
          gather_indices.push_back(idx);
        }
      }
      int max_dims;
      src = mlx_gather_nd(src, gather_indices, gather_first, max_dims);

      // Reassemble the indices for the slicing or reshaping if there are any
      if (gather_first) {
        for (int i = 0; i < max_dims; i++) {
          remaining_indices.push_back(
              py::slice(py::none(), py::none(), py::none()));
        }
        for (int i = 0; i < last_array; i++) {
          auto& idx = indices[i];
          if (idx.is_none()) {
            remaining_indices.push_back(indices[i]);
          } else if (py::isinstance<py::slice>(idx)) {
            remaining_indices.push_back(
                py::slice(py::none(), py::none(), py::none()));
          }
        }
        for (int i = last_array + 1; i < indices.size(); i++) {
          remaining_indices.push_back(indices[i]);
        }
      } else {
        for (int i = 0; i < indices.size(); i++) {
          auto& idx = indices[i];
          if (py::isinstance<array>(idx) || py::isinstance<py::int_>(idx)) {
            break;
          } else if (idx.is_none()) {
            remaining_indices.push_back(idx);
          } else {
            remaining_indices.push_back(
                py::slice(py::none(), py::none(), py::none()));
          }
        }
        for (int i = 0; i < max_dims; i++) {
          remaining_indices.push_back(
              py::slice(py::none(), py::none(), py::none()));
        }
        for (int i = last_array + 1; i < indices.size(); i++) {
          remaining_indices.push_back(indices[i]);
        }
      }
    }
  }
  if (have_array && remaining_indices.empty()) {
    return src;
  }
  if (remaining_indices.empty()) {
    remaining_indices = indices;
  }

  // Slice handling
  {
    std::vector<int> starts(src.ndim(), 0);
    std::vector<int> ends = src.shape();
    std::vector<int> strides(src.ndim(), 1);
    int axis = 0;
    for (auto& idx : remaining_indices) {
      if (!idx.is_none()) {
        get_slice_params(
            starts[axis], ends[axis], strides[axis], idx, ends[axis]);
        axis++;
      }
    }
    src = slice(src, starts, ends, strides);
  }

  // Unsqueeze handling
  if (remaining_indices.size() > src.ndim()) {
    std::vector<int> out_shape;
    int axis = 0;
    for (auto& idx : remaining_indices) {
      if (idx.is_none()) {
        out_shape.push_back(1);
      } else {
        out_shape.push_back(src.shape(axis++));
      }
    }
    src = reshape(src, out_shape);
  }

  return src;
}

array mlx_get_item(const array& src, const py::object& obj) {
  if (py::isinstance<py::slice>(obj)) {
    return mlx_get_item_slice(src, obj);
  } else if (py::isinstance<array>(obj)) {
    return mlx_get_item_array(src, py::cast<array>(obj));
  } else if (py::isinstance<py::int_>(obj)) {
    return mlx_get_item_int(src, obj);
  } else if (py::isinstance<py::tuple>(obj)) {
    return mlx_get_item_nd(src, obj);
  } else if (obj.is_none()) {
    std::vector<int> s(1, 1);
    s.insert(s.end(), src.shape().begin(), src.shape().end());
    return reshape(src, s);
  }
  throw std::invalid_argument("Cannot index mlx array using the given type.");
}

std::tuple<std::vector<array>, array, std::vector<int>> mlx_scatter_args_int(
    const array& src,
    const py::int_& idx,
    const array& update) {
  if (src.ndim() == 0) {
    throw std::invalid_argument(
        "too many indices for array: array is 0-dimensional");
  }

  // Remove any leading singleton dimensions from the update
  // and then broadcast update to shape of src[0, ...]
  int s = 0;
  for (; s < update.ndim() && update.shape(s) == 1; s++)
    ;
  auto up_shape =
      std::vector<int>(update.shape().begin() + s, update.shape().end());
  auto shape = src.shape();
  shape[0] = 1;

  return {
      {get_int_index(idx, src.shape(0))},
      broadcast_to(reshape(update, up_shape), shape),
      {0}};
}

std::tuple<std::vector<array>, array, std::vector<int>> mlx_scatter_args_array(
    const array& src,
    const array& indices,
    const array& update) {
  if (src.ndim() == 0) {
    throw std::invalid_argument(
        "too many indices for array: array is 0-dimensional");
  }

  // Remove any leading singleton dimensions from the update
  int s = 0;
  for (; s < update.ndim() && update.shape(s) == 1; s++)
    ;
  auto up_shape =
      std::vector<int>(update.shape().begin() + s, update.shape().end());
  auto up = reshape(update, up_shape);

  // The update shape must broadcast with indices.shape + [1] + src.shape[1:]
  up_shape = indices.shape();
  up_shape.insert(up_shape.end(), src.shape().begin() + 1, src.shape().end());
  up = broadcast_to(up, up_shape);
  up_shape.insert(up_shape.begin() + indices.ndim(), 1);
  up = reshape(up, up_shape);

  return {{indices}, up, {0}};
}

std::tuple<std::vector<array>, array, std::vector<int>> mlx_scatter_args_slice(
    const array& src,
    const py::slice& in_slice,
    const array& update) {
  // Check input and raise error if 0 dim for parity with np
  if (src.ndim() == 0) {
    throw std::invalid_argument(
        "too many indices for array: array is 0-dimensional");
  }

  // If none slice is requested broadcast the update
  // to the src size and return it.
  if (is_none_slice(in_slice)) {
    int s = 0;
    for (; s < update.ndim() && update.shape(s) == 1; s++)
      ;
    auto up_shape =
        std::vector<int>(update.shape().begin() + s, update.shape().end());
    return {{}, broadcast_to(reshape(update, up_shape), src.shape()), {}};
  }

  int start = 0;
  int end = src.shape(0);
  int stride = 1;

  // Check and update slice params
  get_slice_params(start, end, stride, in_slice, end);

  return mlx_scatter_args_array(
      src, arange(start, end, stride, uint32), update);
}

std::tuple<std::vector<array>, array, std::vector<int>> mlx_scatter_args_nd(
    const array& src,
    const py::tuple& entries,
    const array& update) {
  std::vector<py::object> indices;
  int non_none_indices = 0;

  // Expand ellipses into a series of ':' slices
  {
    int non_none_indices_before = 0;
    int non_none_indices_after = 0;
    bool has_ellipsis = false;
    int indices_before = 0;
    for (int i = 0; i < entries.size(); ++i) {
      auto idx = entries[i];
      if (!is_valid_index_type(idx)) {
        throw std::invalid_argument(
            "Cannot index mlx array using the given type yet");
      } else if (!py::ellipsis().is(idx)) {
        if (!has_ellipsis) {
          indices_before++;
          non_none_indices_before += !idx.is_none();
        } else {
          non_none_indices_after += !idx.is_none();
        }
        indices.push_back(idx);
      } else if (has_ellipsis) {
        throw std::invalid_argument(
            "An index can only have a single ellipsis (...)");
      } else {
        has_ellipsis = true;
      }
    }
    if (has_ellipsis) {
      for (int axis = non_none_indices_before;
           axis < src.ndim() - non_none_indices_after;
           axis++) {
        indices.insert(
            indices.begin() + indices_before, py::slice(0, src.shape(axis), 1));
      }
      non_none_indices = src.ndim();
    } else {
      non_none_indices = non_none_indices_before + non_none_indices_after;
    }
  }

  if (non_none_indices > src.ndim()) {
    std::ostringstream msg;
    msg << "Too many indices for array with " << src.ndim() << "dimensions.";
    throw std::invalid_argument(msg.str());
  }

  // Remove leading singletons dimensions from the update
  int s = 0;
  for (; s < update.ndim() && update.shape(s) == 1; s++) {
  };
  auto up_shape =
      std::vector<int>(update.shape().begin() + s, update.shape().end());
  auto up = reshape(update, up_shape);

  // If no non-None indices return the broadcasted update
  if (non_none_indices == 0) {
    return {{}, broadcast_to(up, src.shape()), {}};
  }

  unsigned long max_dim = 0;
  bool arrays_first = false;
  int num_slices = 0;
  int num_arrays = 0;
  {
    bool have_array = false;
    bool have_non_array = false;
    for (auto& idx : indices) {
      if (py::isinstance<py::slice>(idx) || idx.is_none()) {
        have_non_array = have_array;
        num_slices++;
      } else if (py::isinstance<array>(idx)) {
        have_array = true;
        if (have_array && have_non_array) {
          arrays_first = true;
        }
        max_dim = std::max(py::cast<array>(idx).ndim(), max_dim);
        num_arrays++;
      }
    }
  }

  std::vector<array> arr_indices;
  int slice_num = 0;
  int array_num = 0;
  int ax = 0;
  for (int i = 0; i < indices.size(); ++i) {
    auto& pyidx = indices[i];
    if (py::isinstance<py::slice>(pyidx)) {
      int start, end, stride;
      auto axis_size = src.shape(ax++);
      get_slice_params(start, end, stride, pyidx, axis_size);

      // Handle negative indices
      start = (start < 0) ? start + axis_size : start;
      end = (end < 0) ? end + axis_size : end;

      auto idx = arange(start, end, stride, uint32);
      std::vector<int> idx_shape(max_dim + num_slices, 1);
      auto loc = slice_num + (arrays_first ? max_dim : 0);
      slice_num++;
      idx_shape[loc] = idx.size();
      arr_indices.push_back(reshape(idx, idx_shape));
    } else if (py::isinstance<py::int_>(pyidx)) {
      arr_indices.push_back(get_int_index(pyidx, src.shape(ax++)));
    } else if (pyidx.is_none()) {
      slice_num++;
    } else if (py::isinstance<array>(pyidx)) {
      ax++;
      auto idx = py::cast<array>(pyidx);
      std::vector<int> idx_shape;
      if (!arrays_first) {
        idx_shape.insert(idx_shape.end(), slice_num, 1);
      }
      idx_shape.insert(idx_shape.end(), max_dim - idx.ndim(), 1);
      idx_shape.insert(idx_shape.end(), idx.shape().begin(), idx.shape().end());
      idx_shape.insert(
          idx_shape.end(), num_slices - (arrays_first ? 0 : slice_num), 1);
      arr_indices.push_back(reshape(idx, idx_shape));
      if (!arrays_first && ++array_num == num_arrays) {
        slice_num += max_dim;
      }
    } else {
      throw std::invalid_argument(
          "Cannot index mlx array using the given type yet");
    }
  }

  arr_indices = broadcast_arrays(arr_indices);
  up_shape = arr_indices[0].shape();
  up_shape.insert(
      up_shape.end(),
      src.shape().begin() + non_none_indices,
      src.shape().end());
  up = broadcast_to(up, up_shape);
  up_shape.insert(
      up_shape.begin() + arr_indices[0].ndim(), non_none_indices, 1);
  up = reshape(up, up_shape);

  std::vector<int> axes(arr_indices.size(), 0);
  std::iota(axes.begin(), axes.end(), 0);

  return {arr_indices, up, axes};
}

std::tuple<std::vector<array>, array, std::vector<int>>
mlx_compute_scatter_args(
    const array& src,
    const py::object& obj,
    const ScalarOrArray& v) {
  auto vals = to_array(v, src.dtype());
  if (py::isinstance<py::slice>(obj)) {
    return mlx_scatter_args_slice(src, obj, vals);
  } else if (py::isinstance<array>(obj)) {
    return mlx_scatter_args_array(src, py::cast<array>(obj), vals);
  } else if (py::isinstance<py::int_>(obj)) {
    return mlx_scatter_args_int(src, obj, vals);
  } else if (py::isinstance<py::tuple>(obj)) {
    return mlx_scatter_args_nd(src, obj, vals);
  } else if (obj.is_none()) {
    return {{}, broadcast_to(vals, src.shape()), {}};
  }
  throw std::invalid_argument("Cannot index mlx array using the given type.");
}

void mlx_set_item(array& src, const py::object& obj, const ScalarOrArray& v) {
  auto [indices, updates, axes] = mlx_compute_scatter_args(src, obj, v);
  if (indices.size() > 0) {
    auto out = scatter(src, indices, updates, axes);
    src.overwrite_descriptor(out);
  } else {
    src.overwrite_descriptor(updates);
  }
}

array mlx_add_item(
    const array& src,
    const py::object& obj,
    const ScalarOrArray& v) {
  auto [indices, updates, axes] = mlx_compute_scatter_args(src, obj, v);
  if (indices.size() > 0) {
    return scatter_add(src, indices, updates, axes);
  } else {
    return src + updates;
  }
}

array mlx_subtract_item(
    const array& src,
    const py::object& obj,
    const ScalarOrArray& v) {
  auto [indices, updates, axes] = mlx_compute_scatter_args(src, obj, v);
  if (indices.size() > 0) {
    return scatter_add(src, indices, -updates, axes);
  } else {
    return src - updates;
  }
}

array mlx_multiply_item(
    const array& src,
    const py::object& obj,
    const ScalarOrArray& v) {
  auto [indices, updates, axes] = mlx_compute_scatter_args(src, obj, v);
  if (indices.size() > 0) {
    return scatter_prod(src, indices, updates, axes);
  } else {
    return src * updates;
  }
}

array mlx_divide_item(
    const array& src,
    const py::object& obj,
    const ScalarOrArray& v) {
  auto [indices, updates, axes] = mlx_compute_scatter_args(src, obj, v);
  if (indices.size() > 0) {
    return scatter_prod(src, indices, reciprocal(updates), axes);
  } else {
    return src / updates;
  }
}

array mlx_maximum_item(
    const array& src,
    const py::object& obj,
    const ScalarOrArray& v) {
  auto [indices, updates, axes] = mlx_compute_scatter_args(src, obj, v);
  if (indices.size() > 0) {
    return scatter_max(src, indices, updates, axes);
  } else {
    return maximum(src, updates);
  }
}

array mlx_minimum_item(
    const array& src,
    const py::object& obj,
    const ScalarOrArray& v) {
  auto [indices, updates, axes] = mlx_compute_scatter_args(src, obj, v);
  if (indices.size() > 0) {
    return scatter_min(src, indices, updates, axes);
  } else {
    return minimum(src, updates);
  }
}