Skip to main content

nuvec_core/
array.rs

1//! The lazy array handle and the graph nodes behind it.
2
3use std::collections::{HashMap, HashSet};
4use std::fmt;
5use std::hash::{BuildHasherDefault, Hasher};
6use std::panic::Location;
7use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock, PoisonError};
8use std::thread::ThreadId;
9
10use smallvec::SmallVec;
11
12use crate::buffer::Buffer;
13use crate::primitive::Primitive;
14use crate::shape::{self, Flags, Shape, Strides};
15use crate::transforms::TraceId;
16use crate::{DType, Device, Element, Error, Result, Stream, default_stream};
17
18/// A lazily evaluated n-dimensional array.
19///
20/// Cloning is cheap: clones share the computation and its result. Operations record a
21/// graph node and return immediately; values are computed on [`Array::eval`],
22/// [`crate::eval`], or when read back.
23///
24/// An operation that rejects its arguments returns a *failed* array instead of panicking:
25/// it remembers the operation, the caller's source location and the reason, every array
26/// computed from it fails with the same error, and the error surfaces as `Err` from
27/// [`Array::check`], [`Array::eval`], [`Array::item`] or [`Array::to_vec`].
28#[derive(Clone)]
29#[must_use = "arrays are lazy: an unused result computes nothing (and may hold an error)"]
30pub struct Array {
31    pub(crate) node: Arc<Node>,
32    pub(crate) output: u32,
33}
34
35/// One recorded computation, possibly with several outputs.
36pub(crate) struct Node {
37    pub(crate) stream: Stream,
38    /// The outermost transform whose tracers the node depends on (see `transforms`). The
39    /// node is a tracer while that transform runs, and then keeps its graph when evaluated.
40    pub(crate) trace: Option<TraceId>,
41    pub(crate) outputs: SmallVec<[Output; 1]>,
42    pub(crate) state: Mutex<State>,
43    /// Whether the node is a failed array: an operation rejected its arguments, or one of
44    /// its inputs had failed, when it was recorded (see [`Array::is_failed`]).
45    pub(crate) failed: bool,
46}
47
48/// Metadata and (once evaluated) data of one node output.
49pub(crate) struct Output {
50    pub(crate) dtype: DType,
51    pub(crate) shape: Shape,
52    /// `shape` as the public `i32` dimensions (validated to fit when the output is made).
53    pub(crate) ishape: SmallVec<[i32; 4]>,
54    pub(crate) strides: Strides,
55    pub(crate) flags: Flags,
56    pub(crate) size: usize,
57    /// Set exactly once, when the node is evaluated (or at creation for leaves).
58    pub(crate) slot: OnceLock<Result<Data>>,
59    /// GPU data whose command buffer is committed on the device timeline but may not have
60    /// completed: later GPU work (ordered after it on the device) may read it before `slot` is set.
61    pub(crate) inflight: InFlight,
62}
63
64/// An output's in-flight data (see [`Output::inflight`]). It is published to the slot from
65/// here on completion, unless a consumer took it over first (donation): the lock orders
66/// the two.
67#[derive(Default)]
68pub(crate) struct InFlight {
69    state: Mutex<InFlightState>,
70    /// The failure cell of the command buffer producing the data: consumers in other
71    /// evaluations that read the data while it is in flight fail with it.
72    chunk: OnceLock<ChunkStatus>,
73}
74
75/// Set when a committed chunk of GPU work fails (see `gpu`).
76pub(crate) type ChunkStatus = Arc<OnceLock<Error>>;
77
78#[derive(Default)]
79pub(crate) enum InFlightState {
80    #[default]
81    Unset,
82    #[cfg_attr(not(all(target_os = "macos", not(miri))), allow(dead_code))]
83    Set(Data),
84    /// Taken over by a consumer that writes into it in place; never published.
85    Donated,
86}
87
88impl InFlight {
89    pub(crate) fn lock(&self) -> MutexGuard<'_, InFlightState> {
90        self.state.lock().unwrap_or_else(PoisonError::into_inner)
91    }
92
93    /// Exposes `data` produced by the chunk whose failure cell is `chunk`.
94    #[cfg_attr(not(all(target_os = "macos", not(miri))), allow(dead_code))]
95    pub(crate) fn set_from(&self, data: Data, chunk: &ChunkStatus) {
96        let _ = self.chunk.set(chunk.clone());
97        self.set(data);
98    }
99
100    /// The failure cell of the chunk producing the in-flight data, if known.
101    #[cfg_attr(not(all(target_os = "macos", not(miri))), allow(dead_code))]
102    pub(crate) fn chunk(&self) -> Option<ChunkStatus> {
103        self.chunk.get().cloned()
104    }
105
106    /// Exposes `data` (once; only GPU work has in-flight data).
107    #[cfg_attr(not(all(target_os = "macos", not(miri))), allow(dead_code))]
108    pub(crate) fn set(&self, data: Data) {
109        let mut state = self.lock();
110        if matches!(*state, InFlightState::Unset) {
111            *state = InFlightState::Set(data);
112        }
113    }
114
115    /// The data, if exposed and not donated.
116    pub(crate) fn get(&self) -> Option<Data> {
117        match &*self.lock() {
118            InFlightState::Set(data) => Some(data.clone()),
119            _ => None,
120        }
121    }
122
123    /// Drops the data (the slot holds it now), unless donated.
124    pub(crate) fn clear(&mut self) {
125        let state = self.state.get_mut().unwrap_or_else(PoisonError::into_inner);
126        if matches!(state, InFlightState::Set(_)) {
127            *state = InFlightState::Unset;
128        }
129    }
130}
131
132/// Evaluated data: elements of a (possibly shared) buffer, starting at `offset` and laid
133/// out by the output's strides. Views share their input's buffer.
134#[derive(Clone)]
135pub(crate) struct Data {
136    pub(crate) buffer: Arc<Buffer>,
137    /// Position of the first element, in elements.
138    pub(crate) offset: isize,
139}
140
141impl Data {
142    pub(crate) fn new(buffer: Buffer) -> Self {
143        Self {
144            buffer: Arc::new(buffer),
145            offset: 0,
146        }
147    }
148}
149
150/// Whether a node still has to run. `Done` nodes have dropped their inputs (detach);
151/// `Retained` nodes are evaluated but keep their graph for a running transform.
152///
153/// Transitions: `Pending → Running → Done | Retained | Failed`, and `Retained → Done` when
154/// the transform that retained the node exits. The node's lock is held only for a
155/// transition, never while the node computes or waits for another node.
156pub(crate) enum State {
157    Pending {
158        primitive: Primitive,
159        inputs: Vec<Array>,
160    },
161    /// One thread (`owner`) is computing the node; `done` is signalled once it has moved
162    /// on to `Done`, `Retained` or `Failed` (its outputs set, or deferred in the owner's
163    /// GPU work).
164    Running {
165        owner: ThreadId,
166        done: Arc<Completion>,
167    },
168    Retained {
169        primitive: Primitive,
170        inputs: Vec<Array>,
171    },
172    Done,
173    /// The evaluation failed; the outputs' slots hold `error`.
174    Failed {
175        error: Error,
176    },
177}
178
179/// Signalled once when a running node's evaluation has finished.
180#[derive(Default)]
181pub(crate) struct Completion {
182    done: Mutex<bool>,
183    cv: Condvar,
184}
185
186impl Completion {
187    /// Marks the evaluation finished and wakes every waiter.
188    pub(crate) fn finish(&self) {
189        *self.done.lock().unwrap_or_else(PoisonError::into_inner) = true;
190        self.cv.notify_all();
191    }
192
193    /// Blocks until [`Completion::finish`] has been called.
194    pub(crate) fn wait(&self) {
195        let mut done = self.done.lock().unwrap_or_else(PoisonError::into_inner);
196        while !*done {
197            done = self.cv.wait(done).unwrap_or_else(PoisonError::into_inner);
198        }
199    }
200}
201
202/// Hashes node addresses, the keys of the graph walks' sets and maps. Addresses are
203/// unique and not attacker-controlled, so a multiplicative hash replaces SipHash.
204#[derive(Default, Clone, Copy)]
205pub(crate) struct NodeHasher(u64);
206
207impl Hasher for NodeHasher {
208    fn finish(&self) -> u64 {
209        // Move the well-mixed high bits of the product to the low bits the table indexes
210        // with.
211        self.0.rotate_left(26)
212    }
213    fn write(&mut self, bytes: &[u8]) {
214        for &b in bytes {
215            self.write_u64(u64::from(b));
216        }
217    }
218    fn write_u64(&mut self, n: u64) {
219        self.0 = (self.0.rotate_left(5) ^ n).wrapping_mul(0x9E37_79B9_7F4A_7C15);
220    }
221    fn write_u32(&mut self, n: u32) {
222        self.write_u64(u64::from(n));
223    }
224    fn write_usize(&mut self, n: usize) {
225        self.write_u64(n as u64);
226    }
227}
228
229/// A set of nodes by address.
230pub(crate) type NodeSet = HashSet<*const Node, BuildHasherDefault<NodeHasher>>;
231/// A map keyed by node address (optionally with an output index).
232pub(crate) type NodeMap<K, V> = HashMap<K, V, BuildHasherDefault<NodeHasher>>;
233
234const _: () = {
235    const fn assert_send_sync<T: Send + Sync>() {}
236    assert_send_sync::<Array>();
237};
238
239impl Node {
240    pub(crate) fn lock(&self) -> MutexGuard<'_, State> {
241        self.state.lock().unwrap_or_else(PoisonError::into_inner)
242    }
243
244    /// Whether the transform this node depends on is running (see [`Array::is_tracer`]).
245    pub(crate) fn is_tracer(&self) -> bool {
246        self.trace.is_some_and(crate::transforms::is_live)
247    }
248}
249
250impl Output {
251    /// Why `shape` cannot describe an output of `dtype`.
252    #[cold]
253    fn invalid(shape: &[usize], dtype: DType) -> Error {
254        if shape.iter().any(|&d| i32::try_from(d).is_err()) {
255            Error::invalid_shape("shape", shape.iter().map(|&d| d as i64))
256        } else {
257            shape::checked_nbytes(shape, dtype.size())
258                .err()
259                .unwrap_or_else(|| Error::invalid_shape("shape", shape.iter().map(|&d| d as i64)))
260        }
261    }
262
263    pub(crate) fn new(
264        dtype: DType,
265        shape: Shape,
266        strides: Option<Strides>,
267        slot: OnceLock<Result<Data>>,
268    ) -> Result<Self> {
269        // One pass: the element count (checked), the byte size (checked) and the public
270        // `i32` dimensions (each must fit, as in MLX's `Shape`).
271        let mut size = 1usize;
272        let mut ishape = SmallVec::<[i32; 4]>::new();
273        for &d in &shape {
274            let (Some(n), Ok(di)) = (size.checked_mul(d), i32::try_from(d)) else {
275                return Err(Self::invalid(&shape, dtype));
276            };
277            size = n;
278            ishape.push(di);
279        }
280        if size
281            .checked_mul(dtype.size())
282            .is_none_or(|n| n > isize::MAX as usize)
283        {
284            return Err(Self::invalid(&shape, dtype));
285        }
286        let strides = strides.unwrap_or_else(|| shape::row_contiguous_strides(&shape));
287        let flags = shape::flags_for(&shape, &strides);
288        Ok(Self {
289            dtype,
290            shape,
291            ishape,
292            strides,
293            flags,
294            size,
295            slot,
296            inflight: InFlight::default(),
297        })
298    }
299}
300
301impl Array {
302    // ── Construction ─────────────────────────────────────────────────────────
303
304    /// An evaluated array over a row-contiguous `buffer`.
305    pub(crate) fn from_buffer(buffer: Buffer, dtype: DType, shape: &[usize]) -> Result<Self> {
306        Self::leaf(Ok(Data::new(buffer)), dtype, shape.into())
307    }
308
309    /// A node that is already evaluated, holding `data` (or a sticky error).
310    pub(crate) fn leaf(data: Result<Data>, dtype: DType, shape: Shape) -> Result<Self> {
311        let output = Output::new(dtype, shape, None, OnceLock::from(data))?;
312        Ok(Self::from_node(Node {
313            stream: default_stream(),
314            trace: None,
315            outputs: SmallVec::from_buf([output]),
316            state: Mutex::new(State::Done),
317            failed: false,
318        }))
319    }
320
321    /// A failed array: a leaf whose evaluation returns `error`, with the given metadata
322    /// (a 0-D shape if `shape` is not a valid one). It takes no buffer.
323    #[cold]
324    pub(crate) fn failed(error: Error, dtype: DType, shape: &[usize]) -> Self {
325        let output = Output::new(
326            dtype,
327            shape.into(),
328            None,
329            OnceLock::from(Err(error.clone())),
330        )
331        .unwrap_or_else(|_| {
332            Output::new(dtype, Shape::new(), None, OnceLock::from(Err(error)))
333                .unwrap_or_else(|_| unreachable!("a 0-D output is always valid"))
334        });
335        Self::from_node(Node {
336            stream: default_stream(),
337            trace: None,
338            outputs: SmallVec::from_buf([output]),
339            state: Mutex::new(State::Done),
340            failed: true,
341        })
342    }
343
344    /// Runs the body `f` of the public operation `op`, called at `at` with the array
345    /// operands `inputs`, and returns its array; on error, a failed array instead (the
346    /// sticky error model). The error is the first failed input's (argument order), else
347    /// `f`'s error located at `at` under the name `op`; the failed array takes the first
348    /// operand's shape and dtype.
349    #[inline]
350    pub(crate) fn sticky(
351        op: &'static str,
352        at: &'static Location<'static>,
353        inputs: &[&Array],
354        f: impl FnOnce() -> Result<Array>,
355    ) -> Array {
356        match f() {
357            Ok(out) => out,
358            Err(error) => Self::failed_like(Self::sticky_error(op, at, inputs, error), inputs),
359        }
360    }
361
362    /// [`Array::sticky`] for an operation with `N` outputs: on error, `N` failed arrays.
363    #[inline]
364    pub(crate) fn sticky_n<const N: usize>(
365        op: &'static str,
366        at: &'static Location<'static>,
367        inputs: &[&Array],
368        f: impl FnOnce() -> Result<[Array; N]>,
369    ) -> [Array; N] {
370        match f() {
371            Ok(out) => out,
372            Err(error) => {
373                let error = Self::sticky_error(op, at, inputs, error);
374                std::array::from_fn(|_| Self::failed_like(error.clone(), inputs))
375            }
376        }
377    }
378
379    /// [`Array::sticky`] for an operation with a variable number of outputs: on error,
380    /// `count` failed arrays.
381    #[inline]
382    pub(crate) fn sticky_vec(
383        op: &'static str,
384        at: &'static Location<'static>,
385        inputs: &[&Array],
386        count: usize,
387        f: impl FnOnce() -> Result<Vec<Array>>,
388    ) -> Vec<Array> {
389        match f() {
390            Ok(out) => out,
391            Err(error) => {
392                let error = Self::sticky_error(op, at, inputs, error);
393                (0..count)
394                    .map(|_| Self::failed_like(error.clone(), inputs))
395                    .collect()
396            }
397        }
398    }
399
400    /// The error a failed operation reports: the first failed input's, else `error` with
401    /// the caller's location.
402    #[cold]
403    fn sticky_error(
404        op: &'static str,
405        at: &'static Location<'static>,
406        inputs: &[&Array],
407        error: Error,
408    ) -> Error {
409        inputs
410            .iter()
411            .find_map(|a| a.slot_error())
412            .unwrap_or_else(|| error.locate(op, at))
413    }
414
415    /// A failed array with the first operand's shape and dtype (0-D float32 without one).
416    #[cold]
417    pub(crate) fn failed_like(error: Error, inputs: &[&Array]) -> Self {
418        match inputs.first() {
419            Some(a) => Self::failed(error, a.dtype(), a.dims()),
420            None => Self::failed(error, DType::F32, &[]),
421        }
422    }
423
424    /// Records `primitive` applied to `inputs` and returns one array per output.
425    ///
426    /// Inputs that already failed short-circuit with their error.
427    pub(crate) fn from_op(primitive: Primitive, inputs: Vec<Array>) -> Result<Vec<Array>> {
428        let trace = crate::transforms::inherited(&inputs);
429        Self::record(primitive, inputs, trace)
430    }
431
432    /// [`Array::from_op`] with an explicit trace (`None`: not a tracer, whatever the
433    /// inputs).
434    ///
435    /// Failure is sticky: if an input has failed, nothing is recorded and every output is
436    /// a failed array carrying the first failed input's error, with the shape and dtype the
437    /// operation would have produced (an error if those cannot be inferred).
438    pub(crate) fn record(
439        primitive: Primitive,
440        inputs: Vec<Array>,
441        trace: Option<TraceId>,
442    ) -> Result<Vec<Array>> {
443        if let Some(err) = inputs.iter().find_map(Array::slot_error) {
444            return Self::record_failed(&primitive, &inputs, err);
445        }
446        Self::record_unchecked(primitive, inputs, trace)
447    }
448
449    #[cold]
450    fn record_failed(primitive: &Primitive, inputs: &[Array], err: Error) -> Result<Vec<Array>> {
451        let specs = primitive.output_specs(inputs).map_err(|_| err.clone())?;
452        Ok(specs
453            .into_iter()
454            .map(|spec| Self::failed(err.clone(), spec.dtype, &spec.shape))
455            .collect())
456    }
457
458    /// [`Array::record`] without the failed-input check: the node fails only when
459    /// evaluated. Used for `vmap` placeholders.
460    pub(crate) fn record_unchecked(
461        primitive: Primitive,
462        inputs: Vec<Array>,
463        trace: Option<TraceId>,
464    ) -> Result<Vec<Array>> {
465        let outputs = primitive
466            .output_specs(&inputs)?
467            .into_iter()
468            .map(|spec| Output::new(spec.dtype, spec.shape, spec.strides, OnceLock::new()))
469            .collect::<Result<SmallVec<_>>>()?;
470        let count = outputs.len() as u32;
471        let node = Arc::new(Node {
472            stream: default_stream(),
473            trace,
474            outputs,
475            state: Mutex::new(State::Pending { primitive, inputs }),
476            failed: false,
477        });
478        Ok((0..count)
479            .map(|output| Self {
480                node: node.clone(),
481                output,
482            })
483            .collect())
484    }
485
486    fn from_node(node: Node) -> Self {
487        Self {
488            node: Arc::new(node),
489            output: 0,
490        }
491    }
492
493    // ── Graph (for graph transformations such as `compile`) ──────────────────
494
495    /// A placeholder with this dtype and shape, standing for an input while a function is
496    /// traced (as in MLX): operations can be recorded on it, but evaluating anything that
497    /// depends on it fails. Placeholders are tracers.
498    pub(crate) fn placeholder(dtype: DType, shape: &[usize]) -> Result<Array> {
499        let failed = Self::failed(
500            Error::InvalidArgument(
501                "arrays cannot be evaluated while a function is traced (compile)".into(),
502            ),
503            dtype,
504            &[],
505        );
506        let primitive = Primitive::Broadcast(shape.to_vec());
507        let trace = Some(crate::transforms::PLACEHOLDER);
508        Ok(Self::record_unchecked(primitive, vec![failed], trace)?.swap_remove(0))
509    }
510
511    pub(crate) fn out(&self) -> &Output {
512        &self.node.outputs[self.output as usize]
513    }
514
515    /// The error this array evaluated to, if it has already failed.
516    pub(crate) fn slot_error(&self) -> Option<Error> {
517        match self.out().slot.get() {
518            Some(Err(e)) => Some(e.clone()),
519            _ => None,
520        }
521    }
522
523    // ── Failure (record time, never evaluates) ───────────────────────────────
524
525    /// Whether this is a failed array: the operation that produced it rejected its
526    /// arguments, or one of its inputs had failed, when it was recorded. O(1); errors that
527    /// happen during evaluation are not visible here (they surface from [`Array::eval`]).
528    pub fn is_failed(&self) -> bool {
529        self.node.failed
530    }
531
532    /// A failed array (0-D, float32) carrying the op error `kind`, located at the caller,
533    /// under the name `op`: for functions built from operations that validate their own
534    /// arguments and report failures the same way the operations do.
535    ///
536    /// ```
537    /// use nuvec_core::{Array, OpErrorKind};
538    ///
539    /// let a = Array::failure("my_layer", OpErrorKind::InvalidArgument("wrong cache".into()));
540    /// assert_eq!(a.check().unwrap_err().op_name(), Some("my_layer"));
541    /// ```
542    #[track_caller]
543    pub fn failure(op: &'static str, kind: crate::OpErrorKind) -> Array {
544        Self::failed(
545            Error::op(op, kind).locate(op, Location::caller()),
546            DType::F32,
547            &[],
548        )
549    }
550
551    /// The error of a failed array (see [`Array::is_failed`]), without evaluating.
552    pub fn error(&self) -> Option<&Error> {
553        match (self.node.failed, self.out().slot.get()) {
554            (true, Some(Err(e))) => Some(e),
555            _ => None,
556        }
557    }
558
559    /// `Ok(self)`, or the error of a failed array (see [`Array::is_failed`]). Never
560    /// evaluates: use it before host decisions that depend on shapes, and at the boundary
561    /// of functions that return `Result`.
562    pub fn check(self) -> Result<Array> {
563        if self.node.failed
564            && let Some(error) = self.slot_error()
565        {
566            return Err(error);
567        }
568        Ok(self)
569    }
570
571    // ── Metadata (never evaluates) ───────────────────────────────────────────
572
573    /// Dimension sizes (MLX's `Shape`: `i32`).
574    ///
575    /// A failed array keeps a best-effort shape: the one the operation would have
576    /// produced if that can be inferred, else its first operand's. Call
577    /// [`Array::check`] first when host logic depends on the shape.
578    pub fn shape(&self) -> &[i32] {
579        &self.out().ishape
580    }
581
582    /// The shape as an array of `N` sizes, for destructuring and host decisions
583    /// (`let [b, l, d] = x.shape_n::<3>()?;`): the array's error if it is a failed array
584    /// (whose shape is only a best guess), or an error if it does not have `N` dimensions.
585    ///
586    /// ```
587    /// use nuvec_core::array;
588    ///
589    /// let x = array![[1, 2, 3], [4, 5, 6]];
590    /// let [rows, cols] = x.shape_n::<2>().unwrap();
591    /// assert_eq!((rows, cols), (2, 3));
592    /// assert!(x.shape_n::<3>().is_err());
593    /// ```
594    #[track_caller]
595    pub fn shape_n<const N: usize>(&self) -> Result<[i32; N]> {
596        if let Some(e) = self.error() {
597            return Err(e.clone());
598        }
599        <[i32; N]>::try_from(self.shape()).map_err(|_| {
600            Error::InvalidArgument(format!(
601                "shape_n: expected {N} dimensions, the array has shape {:?}",
602                self.shape()
603            ))
604            .locate("shape_n", Location::caller())
605        })
606    }
607
608    /// Dimension sizes as `usize`, for nuvec's internals.
609    pub(crate) fn dims(&self) -> &[usize] {
610        &self.out().shape
611    }
612
613    /// Number of dimensions.
614    pub fn ndim(&self) -> usize {
615        self.out().shape.len()
616    }
617
618    /// Number of elements.
619    pub fn size(&self) -> usize {
620        self.out().size
621    }
622
623    /// Element type.
624    pub fn dtype(&self) -> DType {
625        self.out().dtype
626    }
627
628    /// Size of one element in bytes.
629    pub fn itemsize(&self) -> usize {
630        self.dtype().size()
631    }
632
633    /// Size of all elements in bytes.
634    pub fn nbytes(&self) -> usize {
635        self.size() * self.itemsize()
636    }
637
638    /// Per-dimension steps, in elements.
639    pub(crate) fn flags(&self) -> Flags {
640        self.out().flags
641    }
642
643    /// Per-dimension steps, in elements.
644    pub(crate) fn strides(&self) -> &[isize] {
645        &self.out().strides
646    }
647
648    /// The stream the producing operation was recorded on.
649    pub fn stream(&self) -> Stream {
650        self.node.stream
651    }
652
653    /// The device the producing operation runs on.
654    pub fn device(&self) -> Device {
655        self.node.stream.device
656    }
657
658    /// Whether the array has been evaluated (successfully or not).
659    pub fn is_evaluated(&self) -> bool {
660        self.out().slot.get().is_some()
661    }
662
663    // ── Evaluation and readback ──────────────────────────────────────────────
664
665    /// Evaluates this array, if it has not been evaluated yet.
666    pub fn eval(&self) -> Result<()> {
667        crate::eval(std::slice::from_ref(self))
668    }
669
670    /// The elements in row-major order, evaluating first if needed.
671    pub fn to_vec<T: Element>(&self) -> Result<Vec<T>> {
672        if let Some(e) = self.error() {
673            return Err(e.clone());
674        }
675        if T::DTYPE != self.dtype() {
676            return Err(Error::dtype_mismatch("to_vec", self.dtype(), T::DTYPE));
677        }
678        self.eval()?;
679        let data = self.data().expect("eval succeeded, so data is set");
680        let out = self.out();
681        if out.size == 0 {
682            Ok(Vec::new())
683        } else if out.flags.row_contiguous || out.size == 1 {
684            let start = data.offset.max(0) as usize * out.dtype.size();
685            Ok(T::read_bytes(
686                &data.buffer.bytes()[start..start + self.nbytes()],
687            ))
688        } else {
689            let gathered = crate::kernels::contiguous(out, data)?;
690            Ok(T::read_bytes(gathered.bytes()))
691        }
692    }
693
694    /// The single element of a one-element array, evaluating first if needed.
695    pub fn item<T: Element>(&self) -> Result<T> {
696        if let Some(e) = self.error() {
697            return Err(e.clone());
698        }
699        if self.size() != 1 {
700            return Err(Error::InvalidArgument(format!(
701                "item() needs exactly one element, the array has shape {:?}",
702                self.dims()
703            )));
704        }
705        if T::DTYPE != self.dtype() {
706            return Err(Error::dtype_mismatch("item", self.dtype(), T::DTYPE));
707        }
708        Ok(self.to_vec::<T>()?[0])
709    }
710
711    /// The evaluated data, if evaluation succeeded.
712    pub(crate) fn data(&self) -> Option<&Data> {
713        self.out().slot.get().and_then(|r| r.as_ref().ok())
714    }
715}
716
717/// Never evaluates: shape, dtype, device and status; the values only when the array is
718/// already evaluated, the error of a failed array.
719impl fmt::Debug for Array {
720    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
721        let mut d = f.debug_struct("Array");
722        d.field("shape", &self.shape())
723            .field("dtype", &self.dtype())
724            .field("device", &self.device());
725        match self.out().slot.get() {
726            None => d.field("status", &"unevaluated"),
727            Some(Ok(_)) => match crate::display::values(self) {
728                Ok(values) => d.field("values", &format_args!("{values}")),
729                Err(_) => d.field("status", &"evaluated"),
730            },
731            Some(Err(e)) if self.node.failed => d.field("failed", &format_args!("{e}")),
732            Some(Err(e)) => d.field("error", &format_args!("{e}")),
733        };
734        d.finish()
735    }
736}
737
738/// Evaluates and prints the values as MLX and NumPy do: `array([[1, 2], [3, 4]], dtype=int32)`
739/// (more than 1,000 elements are summarised with the first and last 3 along each axis), or
740/// `<failed: …>` with the error.
741impl fmt::Display for Array {
742    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
743        match crate::display::values(self) {
744            Ok(values) => write!(f, "array({values}, dtype={})", self.dtype()),
745            Err(e) => write!(f, "<failed: {e}>"),
746        }
747    }
748}
749
750/// A cheap placeholder: a 0-D float32 zero. Handy with [`std::mem::take`] to update an array
751/// held in a struct by value, so the update can write in place:
752/// `self.k = std::mem::take(&mut self.k).at(..).set(&v);`.
753impl Default for Array {
754    fn default() -> Self {
755        crate::creation::from_data(&[0.0f32], &[])
756            .unwrap_or_else(|e| Self::failed(e, DType::F32, &[]))
757    }
758}
759
760/// Drops unevaluated graphs iteratively. Recursive destruction would overflow the stack
761/// on long chains: each node's last reference would drop its inputs from inside its own
762/// destructor.
763impl Drop for Node {
764    fn drop(&mut self) {
765        let mut stack = take_inputs(self);
766        while let Some(Array { node, .. }) = stack.pop() {
767            if let Some(mut node) = Arc::into_inner(node) {
768                stack.append(&mut take_inputs(&mut node));
769            }
770        }
771    }
772}
773
774fn take_inputs(node: &mut Node) -> Vec<Array> {
775    let state = node.state.get_mut().unwrap_or_else(PoisonError::into_inner);
776    match std::mem::replace(state, State::Done) {
777        State::Pending { inputs, .. } | State::Retained { inputs, .. } => inputs,
778        State::Running { .. } | State::Done | State::Failed { .. } => Vec::new(),
779    }
780}
781
782/// Graph accessors as methods, for the unit tests.
783#[cfg(test)]
784impl Array {
785    pub(crate) fn primitive_t(&self) -> Option<Primitive> {
786        crate::graph::primitive(self)
787    }
788    pub(crate) fn inputs_t(&self) -> Vec<Array> {
789        crate::graph::inputs(self)
790    }
791    pub(crate) fn id_t(&self) -> usize {
792        crate::graph::id(self)
793    }
794    pub(crate) fn siblings_t(&self) -> Vec<Array> {
795        crate::graph::siblings(self)
796    }
797    pub(crate) fn output_index_t(&self) -> usize {
798        crate::graph::output_index(self)
799    }
800    pub(crate) fn is_tracer_t(&self) -> bool {
801        crate::graph::is_tracer(self)
802    }
803}
804
805#[cfg(test)]
806mod tests {
807    use super::*;
808    use crate::OpErrorKind;
809
810    #[test]
811    fn graph_accessors_describe_pending_nodes() {
812        let a = Array::from_slice(&[1i32, 2], [2]);
813        let b = Array::from_slice(&[5i32, 7], [2]);
814        let outs =
815            crate::graph::from_primitive(Primitive::AddMultiply, vec![a.clone(), b]).unwrap();
816        let (sum, product) = (&outs[0], &outs[1]);
817        assert_eq!(sum.id_t(), product.id_t(), "siblings share their operation");
818        assert_ne!(sum.id_t(), a.id_t());
819        assert_eq!((sum.output_index_t(), product.output_index_t()), (0, 1));
820        assert_eq!(product.siblings_t().len(), 2);
821        assert_eq!(sum.inputs_t()[0].id_t(), a.id_t());
822        assert!(a.inputs_t().is_empty(), "host data has no inputs");
823        assert!(!sum.is_tracer_t());
824        product.eval().unwrap();
825        assert!(sum.inputs_t().is_empty(), "evaluated: detached");
826
827        let p = Array::placeholder(DType::F32, &[3]).unwrap();
828        let q = &p + 1.0f32;
829        assert!(p.is_tracer_t() && q.is_tracer_t());
830        assert_eq!(q.dims(), [3]);
831        assert!(q.eval().is_err(), "placeholders cannot be evaluated");
832    }
833
834    #[test]
835    fn failed_leaves_are_sticky_and_typed() {
836        let error = Error::size_mismatch("from_slice", 6, 5);
837        let f = Array::failed(error.clone(), DType::F16, &[2, 3]);
838        assert!(f.is_failed());
839        assert_eq!(f.shape(), [2, 3]);
840        assert_eq!(f.dtype(), DType::F16);
841        assert_eq!(f.eval(), Err(error.clone()));
842        assert_eq!(f.clone().check().unwrap_err(), error);
843        // Recording on a failed input records nothing and keeps the inferred metadata.
844        let g = crate::ops::imp::reshape(&f, &[3, 2]).unwrap();
845        assert!(g.is_failed() && g.primitive_t().is_none());
846        assert_eq!(g.shape(), [3, 2]);
847        assert_eq!(g.eval(), Err(error));
848        // A valid array is not failed, and `check` does not evaluate it.
849        let ok = &Array::from(1.0f32) + 1.0;
850        assert!(!ok.is_failed());
851        let ok = ok.check().unwrap();
852        assert!(!ok.is_evaluated());
853    }
854
855    #[test]
856    fn shapes_are_i32_and_bounded() {
857        let a = crate::ops::imp::zeros(&[2, 3, 4], DType::U8).unwrap();
858        let shape: &[i32] = a.shape();
859        assert_eq!(shape, [2, 3, 4]);
860        assert_eq!(a.shape_n::<3>().unwrap(), [2, 3, 4]);
861        assert!(a.shape_n::<2>().is_err(), "rank mismatch");
862        // A single dimension above i32::MAX is an invalid shape, never a wrap.
863        let big = crate::ops::imp::broadcast_to(&Array::from(0u8), &[1usize << 31]).unwrap_err();
864        assert!(
865            matches!(big.kind(), Some(OpErrorKind::InvalidShape(_))),
866            "{big}"
867        );
868        let fits = crate::ops::imp::broadcast_to(&Array::from(0u8), &[i32::MAX as usize]).unwrap();
869        assert_eq!(fits.shape(), [i32::MAX]);
870    }
871
872    #[test]
873    fn node_hasher_spreads_aligned_addresses() {
874        use std::hash::BuildHasher;
875        let build = BuildHasherDefault::<NodeHasher>::default();
876        // 336-byte nodes, as allocated: every low table index should be reachable.
877        let buckets: HashSet<u64> = (0..1024u64)
878            .map(|i| {
879                build.hash_one(std::ptr::without_provenance::<Node>(
880                    0x6000_0000_0000 + i as usize * 336,
881                )) & 1023
882            })
883            .collect();
884        // A random hash fills about 1 - 1/e ≈ 63% of them.
885        assert!(
886            buckets.len() > 600,
887            "only {} of 1024 buckets used",
888            buckets.len()
889        );
890    }
891
892    #[test]
893    fn metadata_without_eval() {
894        let a = Array::from_slice(&[1i32, 2, 3, 4, 5, 6], [2, 3]);
895        let c = crate::ops::imp::add(&a, &a).unwrap();
896        assert!(!c.is_evaluated());
897        assert_eq!(c.dims(), &[2, 3]);
898        assert_eq!(c.ndim(), 2);
899        assert_eq!(c.size(), 6);
900        assert_eq!(c.dtype(), DType::I32);
901        assert_eq!(c.itemsize(), 4);
902        assert_eq!(c.nbytes(), 24);
903        assert_eq!(c.strides(), &[3, 1]);
904        assert!(c.flags().row_contiguous);
905        assert_eq!(c.device(), crate::default_device());
906        assert_eq!(
907            c.primitive_t(),
908            Some(Primitive::Binary(crate::graph::BinaryOp::Add))
909        );
910        assert!(!c.is_evaluated(), "metadata queries must not evaluate");
911    }
912
913    #[test]
914    fn item_on_scalar_and_non_scalar() {
915        assert_eq!(Array::from(7u16).item::<u16>().unwrap(), 7);
916        let a = Array::from_slice(&[1.0f32, 2.0], [2]);
917        assert!(matches!(a.item::<f32>(), Err(Error::InvalidArgument(_))));
918        assert!(matches!(
919            Array::from(1.0f32).item::<f64>(),
920            Err(Error::Op {
921                kind: OpErrorKind::DTypeMismatch { .. },
922                ..
923            })
924        ));
925    }
926
927    #[test]
928    fn debug_does_not_evaluate() {
929        let a = Array::from(1i8);
930        let c = crate::ops::imp::add(&a, &a).unwrap();
931        assert!(format!("{c:?}").contains("unevaluated"));
932        assert!(!c.is_evaluated());
933    }
934}