Skip to content

lazy

EffectBlob

The EffectBlob class represents a collection of prior queries in a lazy evaluation context, which can be extracted into a linear trace of LogicStatements (in the order they were issued). Note that the global effect_stamp counter is required to maintain the relative ordering of effect blobs in the face of in-place mutations to tensors. Each effect blob contains a logic statement and references to other effect blobs. The datastructure is designed to be immutable, mergeable in O(1) time, and support linear time extraction. To achieve linear time, we use a hash table to avoid re-extracting already seen blobs.

Source code in src/finch/interface/lazy.py
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
class EffectBlob:
    """
    The EffectBlob class represents a collection of prior queries in a lazy
    evaluation context, which can be extracted into a linear trace of
    LogicStatements (in the order they were issued). Note that the global
    `effect_stamp` counter is required to maintain the relative ordering of
    effect blobs in the face of in-place mutations to tensors.  Each effect blob
    contains a logic statement and references to other effect blobs. The
    datastructure is designed to be immutable, mergeable in O(1) time, and
    support linear time extraction. To achieve linear time, we use a hash table
    to avoid re-extracting already seen blobs.
    """

    stamp: int
    stmt: LogicStatement
    blobs: tuple[EffectBlob, ...]

    def __init__(
        self,
        stmt: LogicStatement | None = None,
        blobs: tuple[EffectBlob, ...] | None = None,
    ):
        global effect_stamp
        if stmt is None:
            stmt = Plan()
        if blobs is None:
            blobs = ()
        self.stmt = stmt
        self.blobs = blobs

        try:
            curr_stamp = effect_stamp.value
        except AttributeError:
            effect_stamp.value = 0
            curr_stamp = 0

        self.stamp = curr_stamp
        effect_stamp.value += 1

    def exec(self, stmt: LogicStatement) -> EffectBlob:
        return EffectBlob(stmt=stmt, blobs=(self,))

    def eval(self, ex: LogicExpression) -> tuple[Alias, EffectBlob]:
        var = Alias(gensym("A"))
        return var, self.exec(Query(Table(var, ex.fields()), ex))

    def join(self, *blobs: EffectBlob) -> EffectBlob:
        return EffectBlob(blobs=(self, *blobs))

    def trace(self) -> tuple[LogicStatement, ...]:
        stmts = list[tuple[int, LogicStatement]]()
        seen = set[int]()
        self._trace(seen, stmts)
        stmts.sort()
        return tuple(stmt for _, stmt in stmts)

    def _trace(self, seen: set[int], stmts: list[tuple[int, LogicStatement]]) -> None:
        if id(self) not in seen:
            seen.add(id(self))
            stmts.append((self.stamp, self.stmt))
            for blob in self.blobs:
                blob._trace(seen, stmts)

LazyTensor

Bases: OverrideTensor

Source code in src/finch/interface/lazy.py
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
class LazyTensor(OverrideTensor):
    def __init__(
        self,
        data: Alias,
        ctx: EffectBlob,
        shape: tuple,
        fill_value: AbstractFill,
        element_type: FType,
        device=None,
    ):
        self.data: Alias = data
        self.ctx = ctx
        self._shape = shape
        # Held as an AbstractFill so a dynamic fill survives ftype round trips.
        self._fill_value = fill_value
        self._element_type = element_type
        self._device = normalize_device(device)

    def override_module(self):
        return sys.modules[__name__]

    @property
    def ftype(self):
        return LazyTensorFType(
            _fill_type=self._fill_value.ftype,
            _element_type=self._element_type,
            _shape_type=tuple(ftype(dim) for dim in self._shape),
            _device=self._device,
        )

    @property
    def shape(self) -> tuple:
        """
        Returns the shape of the LazyTensor as a tuple.
        The shape is determined by the data and is a static property.
        """
        return self._shape

    @property
    def fill_value(self) -> Any:
        """Default value to fill the tensor."""
        return self._fill_value.value

    @property
    def device(self):
        return self._device

    def to_device(self, device, /, *, stream=None):
        if stream is not None:
            raise ValueError(f"stream argument is not supported; got {stream!r}")
        device = normalize_device(device)
        if device == self.device:
            return self
        return LazyTensor(
            self.data,
            self.ctx,
            self._shape,
            self._fill_value,
            self._element_type,
            device=device,
        )

    @property
    def element_type(self) -> FType:
        """Data type of the tensor elements."""
        return self.ftype.element_type

    @property
    def shape_type(self) -> tuple:
        """Shape type of the tensor."""
        return self.ftype.shape_type

    def item(self):
        raise ValueError(
            "Cannot convert LazyTensor to Python scalar. "
            "Use compute() to evaluate it first."
        )

    def to_numpy(self):
        raise ValueError(
            "Cannot convert LazyTensor to Python scalar. "
            "Use compute() to evaluate it first."
        )

    def to_scipy(self):
        raise ValueError(
            "Cannot convert LazyTensor to Python scalar. "
            "Use compute() to evaluate it first."
        )

    # raise ValueError for unsupported operations according to the data-apis spec.
    # NOT tested, since this isn't necessary as it will throw an error anyways.
    def __complex__(self) -> complex:
        """
        Converts the LazyTensor to a complex number.
        This is a no-op for LazyTensors, as they are symbolic representations.
        """
        raise ValueError(
            "Cannot convert LazyTensor to complex. Use compute() to evaluate it first."
        )

    def __float__(self) -> float:
        """
        Converts the LazyTensor to a float.
        This is a no-op for LazyTensors, as they are symbolic representations.
        """
        raise ValueError(
            "Cannot convert LazyTensor to float. Use compute() to evaluate it first."
        )

    def __int__(self) -> int:
        """
        Converts the LazyTensor to an int.
        This is a no-op for LazyTensors, as they are symbolic representations.
        """
        raise ValueError(
            "Cannot convert LazyTensor to int. Use compute() to evaluate it first."
        )

    def __bool__(self) -> bool:
        """
        Converts the LazyTensor to a bool.
        This is a no-op for LazyTensors, as they are symbolic representations.
        """
        raise ValueError(
            "Cannot convert LazyTensor to bool. Use compute() to evaluate it first."
        )

    def reshape(self, shape, /, *, copy=None):
        return reshape(self, shape, copy=copy)

element_type property

Data type of the tensor elements.

fill_value property

Default value to fill the tensor.

shape property

Returns the shape of the LazyTensor as a tuple. The shape is determined by the data and is a static property.

shape_type property

Shape type of the tensor.

__bool__()

Converts the LazyTensor to a bool. This is a no-op for LazyTensors, as they are symbolic representations.

Source code in src/finch/interface/lazy.py
362
363
364
365
366
367
368
369
def __bool__(self) -> bool:
    """
    Converts the LazyTensor to a bool.
    This is a no-op for LazyTensors, as they are symbolic representations.
    """
    raise ValueError(
        "Cannot convert LazyTensor to bool. Use compute() to evaluate it first."
    )

__complex__()

Converts the LazyTensor to a complex number. This is a no-op for LazyTensors, as they are symbolic representations.

Source code in src/finch/interface/lazy.py
335
336
337
338
339
340
341
342
def __complex__(self) -> complex:
    """
    Converts the LazyTensor to a complex number.
    This is a no-op for LazyTensors, as they are symbolic representations.
    """
    raise ValueError(
        "Cannot convert LazyTensor to complex. Use compute() to evaluate it first."
    )

__float__()

Converts the LazyTensor to a float. This is a no-op for LazyTensors, as they are symbolic representations.

Source code in src/finch/interface/lazy.py
344
345
346
347
348
349
350
351
def __float__(self) -> float:
    """
    Converts the LazyTensor to a float.
    This is a no-op for LazyTensors, as they are symbolic representations.
    """
    raise ValueError(
        "Cannot convert LazyTensor to float. Use compute() to evaluate it first."
    )

__int__()

Converts the LazyTensor to an int. This is a no-op for LazyTensors, as they are symbolic representations.

Source code in src/finch/interface/lazy.py
353
354
355
356
357
358
359
360
def __int__(self) -> int:
    """
    Converts the LazyTensor to an int.
    This is a no-op for LazyTensors, as they are symbolic representations.
    """
    raise ValueError(
        "Cannot convert LazyTensor to int. Use compute() to evaluate it first."
    )

LazyTensorFType

Bases: TensorFType

Source code in src/finch/interface/lazy.py
 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
class LazyTensorFType(TensorFType):
    _fill_type: AbstractFillFType
    _element_type: FType
    _shape_type: tuple[FType, ...]

    def __init__(
        self,
        _fill_type: AbstractFillFType,
        _element_type: FType | type,
        _shape_type: tuple[FType | type, ...],
        _device=None,
    ):
        self._fill_type = _fill_type
        self._element_type = ftype(_element_type)
        self._shape_type = tuple(ftype(dim_t) for dim_t in _shape_type)
        self._device = normalize_device(_device)

    def __eq__(self, other):
        if not isinstance(other, LazyTensorFType):
            return False
        return (
            self._fill_type == other._fill_type
            and self._element_type == other._element_type
            and self._shape_type == other._shape_type
            and self.device == other.device
        )

    def __hash__(self):
        return hash(
            (
                self._fill_type,
                self._element_type,
                self._shape_type,
                self.device,
            )
        )

    def construct(self, shape: tuple) -> LazyTensor:
        idxs = tuple(Field(gensym("i")) for _ in shape)
        ctx = EffectBlob()
        fill = resolve_fill(self._fill_type)
        expr = Table(Literal(FillTensor(shape, fill)), idxs)
        data, ctx = ctx.eval(expr)
        return LazyTensor(
            data=data,
            ctx=ctx,
            shape=shape,
            fill_value=fill,
            element_type=self._element_type,
            device=self.device,
        )

    def __call__(self, val: Any) -> LazyTensor:
        """
        Convert a tensor to this tensor type.

        Args:
            val: A tensor to convert to this tensor type.
        Returns:
            A LazyTensor instance of this type.
        """
        raise NotImplementedError(
            f"Tensor conversion not yet implemented for {type(self).__name__}"
        )

    @property
    def fill_type(self) -> AbstractFillFType:
        return self._fill_type

    @property
    def element_type(self) -> FType:
        return self._element_type

    @property
    def shape_type(self):
        return self._shape_type

    @property
    def device(self):
        return self._device

    def from_numpy(self, arr):
        raise NotImplementedError

__call__(val)

Convert a tensor to this tensor type.

Parameters:

Name Type Description Default
val Any

A tensor to convert to this tensor type.

required

Returns: A LazyTensor instance of this type.

Source code in src/finch/interface/lazy.py
143
144
145
146
147
148
149
150
151
152
153
154
def __call__(self, val: Any) -> LazyTensor:
    """
    Convert a tensor to this tensor type.

    Args:
        val: A tensor to convert to this tensor type.
    Returns:
        A LazyTensor instance of this type.
    """
    raise NotImplementedError(
        f"Tensor conversion not yet implemented for {type(self).__name__}"
    )

all(x, /, *, axis=None, keepdims=False, init=None)

Test whether all elements of input array x along given axis are True.

Source code in src/finch/interface/lazy.py
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
def all(
    x,
    /,
    *,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
    init=None,
):
    """
    Test whether all elements of input array ``x`` along given axis are True.
    """
    x = defer(x)
    return reduce(
        ffuncs.and_,
        elementwise(ffuncs.truth, x),
        axis=axis,
        keepdims=keepdims,
        init=init,
    )

any(x, /, *, axis=None, keepdims=False, init=None)

Test whether any element of input array x along given axis is True.

Source code in src/finch/interface/lazy.py
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
def any(
    x,
    /,
    *,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
    init=None,
):
    """
    Test whether any element of input array ``x`` along given axis is True.
    """
    x = defer(x)
    return reduce(
        ffuncs.or_,
        elementwise(ffuncs.truth, x),
        axis=axis,
        keepdims=keepdims,
        init=init,
    )

asarray(obj, /, *, dtype=None, device=None, copy=None, format=None)

asarray(
    obj: np.ndarray,
    /,
    *,
    dtype=None,
    device=None,
    copy=None,
    format: None = None,
) -> BufferizedNDArray
asarray(
    obj: sps.spmatrix | sps.sparray,
    /,
    *,
    dtype=None,
    device=None,
    copy=None,
    format: None = None,
) -> FiberTensor
asarray(
    obj: Any,
    /,
    *,
    dtype=None,
    device=None,
    copy=None,
    format: FiberTensorFType,
) -> FiberTensor
asarray(
    obj: np.generic | complex,
    /,
    *,
    dtype=None,
    device=None,
    copy=None,
    format: None = None,
) -> Scalar
asarray(
    obj: Any,
    /,
    *,
    dtype=None,
    device=None,
    copy=None,
    format: TensorFType | None = None,
) -> Tensor

Convert given argument and return wrapper type instance. If input argument is already array type, return unchanged. https://data-apis.org/array-api/latest/API_specification/generated/array_api.asarray.html

Source code in src/finch/interface/lazy.py
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
def asarray(
    obj: Any,
    /,
    *,
    dtype=None,
    device=None,
    copy=None,
    format: TensorFType | None = None,
) -> Tensor:
    """
    Convert given argument and return wrapper type instance.
    If input argument is already array type, return unchanged.
    https://data-apis.org/array-api/latest/API_specification/generated/array_api.asarray.html
    """
    explicit_device = device is not None
    device = normalize_device(device)

    if format is None:
        if isinstance(obj, Scalar):
            if copy is True or (explicit_device and obj.device != device):
                return Scalar(obj.val, fill_value=obj.fill_value, device=device)
            return obj
        if isinstance(obj, BufferizedNDArray):
            if copy is True:
                return BufferizedNDArray.from_numpy(
                    obj.to_numpy().copy(), fill_value=obj.fill_value, device=device
                )
            if explicit_device and obj.device != device:
                return obj.to_device(device)
            return obj
        if isinstance(obj, Tensor) and copy is not True:
            if explicit_device and obj.device != device:
                return obj.to_device(device)
            return obj
        if isinstance(obj, np.ndarray):
            if copy is True:
                obj = obj.copy()
            return BufferizedNDArray.from_numpy(obj, device=device)
        if sps.issparse(obj):
            if obj.format == "csc":  # CSC has no native Finch layout yet
                if dtype is not None or copy is True:
                    raise NotImplementedError(
                        "asarray cannot honor dtype or copy for CSC input; "
                        "convert to CSR or COO first."
                    )
                return obj
            if copy is False and not (
                obj.format in ("coo", "csr") and obj.has_canonical_format
            ):
                raise ValueError(
                    "Unable to avoid copy while creating an array as requested."
                )
            if obj.format not in ("coo", "csr"):
                obj = obj.asformat("coo")

            if dtype is not None:
                if copy is False:
                    obj = obj.astype(dtype, copy=False)
                else:
                    obj = obj.astype(dtype, copy=True)
            elif copy is True:
                obj = obj.copy()

            if not obj.has_canonical_format:
                obj = obj.copy()
                obj.sum_duplicates()

            match obj.format:
                case "csr":
                    return FiberTensor.from_scipy_csr(obj, device=device)
                case "coo":
                    return FiberTensor.from_scipy_coo(obj, device=device)

        if np.isscalar(obj) or obj is None:
            if dtype is not None:
                obj = ftype(dtype)(obj)
            elif isinstance(obj, bool | int | float | complex):
                obj = np.asarray(obj)[()]
            return Scalar(obj, device=device)
        try:
            np_arr = np.asarray(obj)
            if np_arr.dtype != object:
                if dtype is not None:
                    np_arr = np_arr.astype(np_dtype(ftype(dtype)))
                elif copy is True:
                    np_arr = np_arr.copy()
                return BufferizedNDArray.from_numpy(np_arr, device=device)
        except (TypeError, ValueError):
            pass
        return obj

    out = format.from_numpy(obj) if isinstance(obj, np.ndarray) else format(obj)
    return out.to_device(device) if explicit_device else out

broadcast_arrays(*arrays)

Broadcasts one or more arrays against one another.

Source code in src/finch/interface/lazy.py
2670
2671
2672
2673
2674
2675
def broadcast_arrays(*arrays: LazyTensor) -> tuple[LazyTensor, ...]:
    """
    Broadcasts one or more arrays against one another.
    """
    shape = _broadcast_shape(*(array.shape for array in arrays))
    return tuple(broadcast_to(arr, shape) for arr in arrays)

broadcast_to(tensor, /, shape)

Broadcasts a lazy tensor to a specified shape.

Parameters:

Name Type Description Default
tensor Any

The lazy tensor to broadcast.

required
shape tuple

The target shape to broadcast to.

required

Returns:

Type Description
LazyTensor

A new lazy tensor with the specified shape.

Source code in src/finch/interface/lazy.py
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
def broadcast_to(tensor: Any, /, shape: tuple) -> LazyTensor:
    """
    Broadcasts a lazy tensor to a specified shape.

    Args:
        tensor: The lazy tensor to broadcast.
        shape: The target shape to broadcast to.

    Returns:
        A new lazy tensor with the specified shape.
    """
    tensor = defer(tensor)

    if not is_broadcastable_directional(tensor.shape, shape):
        raise ValueError(
            f"Tensor with shape {tensor.shape} is not broadcastable "
            f"to the shape {shape}"
        )
    return elementwise(ffuncs.first_arg, tensor, FillTensor(shape, np.False_))

conj(x)

Computes the complex conjugate of the input tensor x.

Parameters

x: LazyTensor The input tensor to compute the complex conjugate of.

Returns

LazyTensor A new LazyTensor with the complex conjugate of x.

Source code in src/finch/interface/lazy.py
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
def conj(x) -> LazyTensor:
    """
    Computes the complex conjugate of the input tensor `x`.

    Parameters
    ----------
    x: LazyTensor
        The input tensor to compute the complex conjugate of.

    Returns
    -------
    LazyTensor
        A new LazyTensor with the complex conjugate of `x`.
    """
    return elementwise(ffuncs.conjugate, x)

defer(arr)

defer(arr: tuple[Any, ...]) -> tuple[Any, ...]
defer(arr: Any) -> LazyTensor
  • defer(arr) -> LazyTensor: Converts an array into a LazyTensor. If the input is already a LazyTensor, it is returned as-is. Otherwise, it creates a LazyTensor representation of the input array.

Parameters: - arr: The input array to be converted into a LazyTensor.

Returns: - LazyTensor: A lazy representation of the input array.

Source code in src/finch/interface/lazy.py
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
def defer(arr: Any) -> LazyTensor | tuple[Any, ...]:
    """
    - defer(arr) -> LazyTensor:
    Converts an array into a LazyTensor. If the input is already a LazyTensor, it is
    returned as-is.
    Otherwise, it creates a LazyTensor representation of the input array.

    Parameters:
    - arr: The input array to be converted into a LazyTensor.

    Returns:
    - LazyTensor: A lazy representation of the input array.
    """
    if isinstance(arr, tuple):
        return tuple(defer(arr_i) for arr_i in arr)

    if isinstance(arr, LazyTensor):
        return arr
    if _is_numeric_constant(arr):
        arr = ConstantScalar(arr) if is_specializable_value(arr) else Scalar(arr)
    else:
        arr = asarray(arr)
    tns = Alias(gensym("A"))
    idxs = tuple(Field(gensym("i")) for _ in range(arr.ndim))
    shape = tuple(arr.shape)
    ctx = EffectBlob(stmt=Query(Table(tns, idxs), Table(Literal(arr), idxs)))
    return LazyTensor(tns, ctx, shape, arr.fill, arr.element_type, arr.device)

elementwise(f, *args)

elementwise(f, *args) -> LazyTensor:

Applies the function f elementwise to the given arguments, following broacasting rules. The function f should be a callable that takes the same number of arguments as the number of tensors passed to elementwise.

The function will automatically handle broadcasting of the input tensors to ensure they have compatible shapes. For example, elementwise(ffunc.add, x, y) is equivalent to x + y.

Parameters: - f: The function to apply elementwise. - *args: The tensors to apply the function to. These tensors should be compatible for broadcasting.

Returns: - LazyTensor: The tensor, out, of results from applying f elementwise to the input tensors. After broadcasting the arguments to the same shape, for each index i, out[*i] = f(args[0][*i], args[1][*i], ...).

Source code in src/finch/interface/lazy.py
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
def elementwise(f: FinchOperator, *args) -> LazyTensor:
    """
        elementwise(f, *args) -> LazyTensor:

    Applies the function f elementwise to the given arguments, following
    [broacasting rules](https://numpy.org/doc/stable/user/basics.broadcasting.html).
    The function f should be a callable that takes the same number of arguments
    as the number of tensors passed to `elementwise`.

    The function will automatically handle broadcasting of the input tensors to
    ensure they have compatible shapes.  For example, `elementwise(ffunc.add,
    x, y)` is equivalent to `x + y`.

    Parameters:
    - f: The function to apply elementwise.
    - *args: The tensors to apply the function to. These tensors should be
        compatible for broadcasting.

    Returns:
    - LazyTensor: The tensor, `out`, of results from applying `f` elementwise to
    the input tensors.  After broadcasting the arguments to the same shape, for
    each index `i`, `out[*i] = f(args[0][*i], args[1][*i], ...)`.
    """
    args = tuple(defer(a) for a in args)
    shapes = tuple(a.shape for a in args)
    shape = _broadcast_shape(*shapes)
    ndim = len(shape)
    idxs = tuple(Field(gensym("i")) for _ in range(ndim))
    bargs: list[LogicExpression] = []
    for arg, arg_shape in zip(args, shapes, strict=True):
        arg_ndim = len(arg_shape)
        idims = []
        odims = []
        for i in range(ndim - arg_ndim, ndim):
            if arg_shape[i - ndim + arg_ndim] == shape[i]:
                idims.append(idxs[i])
                odims.append(idxs[i])
            else:
                if arg_shape[i - ndim + arg_ndim] != 1:
                    raise ValueError("Invalid shape for broadcasting")
                idims.append(Field(gensym("j")))
        bargs.append(Reorder(Table(arg.data, tuple(idims)), tuple(odims)))
    expr = Reorder(MapJoin(Literal(f), tuple(bargs)), idxs)
    new_fill_value = apply_fill(f, *[a.fill for a in args])
    op_type = f.ftype
    if not isinstance(op_type, CallableFType):
        raise TypeError(f"Expected a callable type, got {op_type}")
    new_element_type = return_type(op_type, *[a.element_type for a in args])
    ctx = args[0].ctx.join(*[x.ctx for x in args[1:]])
    data, ctx = ctx.eval(expr)
    return LazyTensor(
        data,
        ctx,
        shape,
        new_fill_value,
        new_element_type,
        common_device(*(t.device for t in args)),
    )

expand_dims(x, /, axis=0)

Expands the shape of an array by inserting a new axis (dimension) of size one at the position specified by axis.

Parameters

x: array input array. axis: int axis position (zero-based). If x has rank (i.e, number of dimensions) N, a valid axis must reside on the closed-interval [-N-1, N]. If provided a negative axis, the axis position at which to insert a singleton dimension must be computed as N + axis + 1. Hence, if provided -1, the resolved axis position must be N (i.e., a singleton dimension must be appended to the input array x). If provided -N-1, the resolved axis position must be 0 (i.e., a singleton dimension must be prepended to the input array x).

Returns

out: array an expanded output array having the same data type as x.

Raises

IndexError If provided an invalid axis position, an IndexError should be raised.

Source code in src/finch/interface/lazy.py
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
def expand_dims(
    x,
    /,
    axis: int | tuple[int, ...] = 0,
) -> LazyTensor:
    """
    Expands the shape of an array by inserting a new axis (dimension) of size one at the
    position specified by ``axis``.

    Parameters
    ----------
    x: array
        input array.
    axis: int
        axis position (zero-based). If ``x`` has rank (i.e, number of dimensions) ``N``,
        a valid ``axis`` must reside on the closed-interval ``[-N-1, N]``. If provided a
        negative ``axis``, the axis position at which to insert a singleton dimension
        must be computed as ``N + axis + 1``. Hence, if provided ``-1``, the resolved
        axis position must be ``N`` (i.e., a singleton dimension must be appended to the
        input array ``x``). If provided ``-N-1``, the resolved axis position must be
        ``0`` (i.e., a singleton dimension must be prepended to the input array ``x``).

    Returns
    -------
    out: array
        an expanded output array having the same data type as ``x``.

    Raises
    ------
    IndexError
        If provided an invalid ``axis`` position, an ``IndexError`` should be raised.
    """
    x = defer(x)
    if not isinstance(axis, (tuple, list)):
        axis = (axis,)
    axis = normalize_axis_tuple(axis, x.ndim + len(axis))
    if isinstance(axis, int):
        raise IndexError(
            f"Invalid axis: {axis}. Axis must be an integer or a tuple of integers."
        )
    if not len(axis) == len(set(axis)):
        raise IndexError("axis must be unique")
    if not set(axis).issubset(range(x.ndim + len(axis))):
        raise IndexError(
            f"Invalid axis: {axis}. Axis must be unique and must be in the range "
            f"[-{x.ndim + len(axis) - 1}, {x.ndim + len(axis) - 1}]."
        )
    offset = [0] * (x.ndim + len(axis))
    for d in axis:
        offset[d] = 1
    offset = list(accumulate(offset))
    idxs_1 = tuple(Field(gensym("i")) for _ in range(x.ndim))
    idxs_2 = tuple(
        Field(gensym("i")) if n in axis else idxs_1[n - offset[n]]
        for n in range(x.ndim + len(axis))
    )
    expr = Reorder(Table(x.data, idxs_1), idxs_2)
    shape_2 = tuple(
        1 if n in axis else x.shape[n - offset[n]] for n in range(x.ndim + len(axis))
    )
    data_2, ctx = x.ctx.eval(expr)
    return LazyTensor(data_2, ctx, shape_2, x.fill, x.element_type, x.device)

full(shape, fill_value, *, dtype=None, device=None)

Returns a new array having a specified shape and filled with fill_value.

Parameters: - shape (Union[int, Tuple[int, ...]]): output array shape. - fill_value (Union[bool, int, float, complex]): fill value. - dtype (Optional[dtype]): output array data type. If dtype is None, the output array data type must be inferred from fill_value according to the following rules: * If the fill value is an int, the output array data type must be the default integer data type. * If the fill value is a float, the output array data type must be the default real-valued floating-point data type. * If the fill value is a complex number, the output array data type must be the default complex floating-point data type. * If the fill value is a bool, the output array must have a boolean data type. Default: None.

Returns:

  • out (array): an array where every element is equal to fill_value.
Source code in src/finch/interface/lazy.py
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
def full(
    shape: int | tuple[int, ...],
    fill_value: bool | complex | LazyTensor,
    *,
    dtype: Any | None = None,
    device=None,
):
    """
    Returns a new array having a specified shape and filled with fill_value.

    Parameters:
    - shape (Union[int, Tuple[int, ...]]): output array shape.
    - fill_value (Union[bool, int, float, complex]): fill value.
    - dtype (Optional[dtype]): output array data type. If dtype is None, the
    output array data type must be inferred from fill_value according to the
    following rules:
        * If the fill value is an int, the output array data type must be the
            default integer data type.
        * If the fill value is a float, the output array data type must be the
            default real-valued floating-point data type.
        * If the fill value is a complex number, the output array data type must
            be the default complex floating-point data type.
        * If the fill value is a bool, the output array must have a boolean data
            type. Default: None.

    Returns:

    - out (array): an array where every element is equal to fill_value.
    """
    shape = (shape,) if isinstance(shape, int) else tuple(shape)
    if isinstance(fill_value, LazyTensor):
        # The value may be a zero-dimensional tensor, such as a dynamic fill.
        if dtype is not None:
            fill_value = astype(fill_value, dtype)
        if device is not None:
            fill_value = fill_value.to_device(device)
        return broadcast_to(fill_value, shape)
    return broadcast_to(asarray(fill_value, dtype=dtype, device=device), shape)

is_broadcastable(shape_a, shape_b)

Returns True if shape_a and shape_b are broadcastable according to numpy rules.

Source code in src/finch/interface/lazy.py
1435
1436
1437
1438
1439
1440
1441
1442
def is_broadcastable(shape_a, shape_b):
    """
    Returns True if shape_a and shape_b are broadcastable according to numpy rules.
    """
    for a, b in zip_longest(reversed(shape_a), reversed(shape_b), fillvalue=1):
        if a != b and a != 1 and b != 1:
            return False
    return True

is_broadcastable_directional(shape_a, shape_b)

Returns True if shape_a is broadcastable to shape_b according to numpy rules. This is a directional check, so it allows only shape_a to be changed

Source code in src/finch/interface/lazy.py
1445
1446
1447
1448
1449
1450
1451
1452
1453
def is_broadcastable_directional(shape_a, shape_b):
    """
    Returns True if shape_a is broadcastable to shape_b according to numpy rules.
    This is a directional check, so it allows only shape_a to be changed
    """
    for a, b in zip_longest(reversed(shape_a), reversed(shape_b), fillvalue=1):
        if a != b and a != 1:
            return False
    return True

matmul(x1, x2)

Performs matrix multiplication between two tensors.

Source code in src/finch/interface/lazy.py
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
def matmul(x1, x2) -> LazyTensor:
    """
    Performs matrix multiplication between two tensors.
    """

    def _matmul_helper(a, b) -> LazyTensor:
        """
        For arrays greater than 1D
        """
        if a.ndim < 2 or b.ndim < 2:
            raise ValueError("Both inputs must be at least 2D arrays")
        if a.shape[-1] != b.shape[-2]:
            raise ValueError("Dimensions mismatch for matrix multiplication")
        # check all preceeding dimensions match
        batch_a, batch_b = a.shape[:-2], b.shape[:-2]
        if not is_broadcastable(batch_a, batch_b):
            raise ValueError(
                "Batch dimensions are not broadcastable for matrix multiplication"
            )
        return reduce(
            ffuncs.add,
            multiply(expand_dims(a, axis=-1), expand_dims(b, axis=-3)),
            axis=-2,
        )

    x1 = defer(x1)
    x2 = defer(x2)

    if x1.ndim < 1 or x2.ndim < 1:
        raise ValueError("Both inputs must be at least 1D arrays")

    if x1.ndim == 1 and x2.ndim == 1:
        return reduce(ffuncs.add, multiply(x1, x2), axis=0)

    if x1.ndim == 1:
        x1 = expand_dims(x1, axis=0)  # make it a row vector
        result = _matmul_helper(x1, x2)
        return squeeze(result, axis=-2)  # remove the prepended singleton dimension

    if x2.ndim == 1:
        x2 = expand_dims(x2, axis=1)  # make it a column vector
        result = _matmul_helper(x1, x2)
        return squeeze(result, axis=-1)  # remove the appended singleton dimension

    return _matmul_helper(x1, x2)

matrix_transpose(x)

Transposes the input tensor x.

Parameters

x: LazyTensor The input tensor to be transposed. Must have at least 2 dimensions.

Returns

LazyTensor A new LazyTensor with the axes of x transposed.

Source code in src/finch/interface/lazy.py
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
def matrix_transpose(x) -> LazyTensor:
    """
    Transposes the input tensor `x`.

    Parameters
    ----------
    x: LazyTensor
        The input tensor to be transposed. Must have at least 2 dimensions.

    Returns
    -------
    LazyTensor
        A new LazyTensor with the axes of `x` transposed.
    """
    x = defer(x)
    if x.ndim < 2:
        # this is following numpy's behavior.
        # data-apis specification assumes that input is atleast 2D
        raise ValueError(
            "Input tensor must have at least 2 dimensions for transposition"
        )
    # swap the last two axes
    return permute_dims(x, axes=(*range(x.ndim - 2), x.ndim - 1, x.ndim - 2))

max(x, /, *, axis=None, keepdims=False, init=None)

Return the maximum of input array arr along given axis.

Source code in src/finch/interface/lazy.py
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
def max(
    x,
    /,
    *,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
    init=None,
):
    """
    Return the maximum of input array ``arr`` along given axis.
    """
    x = defer(x)
    return reduce(ffuncs.max, x, axis=axis, keepdims=keepdims, init=init)

mean(x, /, *, axis=None, keepdims=False)

Calculates the arithmetic mean of the input array x.

Source code in src/finch/interface/lazy.py
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
def mean(x, /, *, axis: int | tuple[int, ...] | None = None, keepdims: bool = False):
    """
    Calculates the arithmetic mean of the input array ``x``.
    """
    x = defer(x)
    n = (
        np.prod(tuple(x.shape[i] for i in range(x.ndim) if i in axis))
        if isinstance(axis, tuple)
        else (np.prod(x.shape) if axis is None else x.shape[axis])
    )
    s = sum(x, axis=axis, keepdims=keepdims)
    return truediv(s, x.element_type(n))

min(x, /, *, axis=None, keepdims=False, init=None)

Return the minimum of input array arr along given axis.

Source code in src/finch/interface/lazy.py
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
def min(
    x,
    /,
    *,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
    init=None,
):
    """
    Return the minimum of input array ``arr`` along given axis.
    """
    x = defer(x)
    return reduce(ffuncs.min, x, axis=axis, keepdims=keepdims, init=init)

moveaxis(x, source, destination)

Moves axes of an array to new positions.

Source code in src/finch/interface/lazy.py
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
def moveaxis(x, source: int | tuple[int, ...], destination: int | tuple[int, ...], /):
    """
    Moves axes of an array to new positions.
    """
    x = defer(x)

    # argument validation
    # handles uniqueness, int -> tuple, and bound check
    source = normalize_axis_tuple(source, x.ndim, "source")
    destination = normalize_axis_tuple(destination, x.ndim, "destination")

    if len(source) != len(destination):
        raise ValueError("Source and Destination indices must have the same length")

    final_order = [i for i in range(x.ndim) if i not in source]
    for dest, src in sorted(zip(destination, source, strict=True)):
        final_order.insert(dest, src)

    return permute_dims(x, axes=tuple(final_order))

permute_dims(arg, /, axes)

Permutes the axes (dimensions) of an array x.

Parameters

x: array input array. axes: Tuple[int, ...] tuple containing a permutation of (0, 1, ..., N-1) where N is the number of axes (dimensions) of x.

Returns

out: array an array containing the axes permutation. The returned array must have the same data type as x.

Source code in src/finch/interface/lazy.py
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
def permute_dims(arg, /, axes: tuple[int, ...]) -> LazyTensor:
    """
    Permutes the axes (dimensions) of an array ``x``.

    Parameters
    ----------
    x: array
        input array.
    axes: Tuple[int, ...]
        tuple containing a permutation of ``(0, 1, ..., N-1)`` where ``N`` is the number
        of axes (dimensions) of ``x``.

    Returns
    -------
    out: array
        an array containing the axes permutation. The returned array must have the same
        data type as ``x``.
    """
    arg = defer(arg)
    axis = normalize_axis_tuple(axes, arg.ndim + len(axes))
    idxs = tuple(Field(gensym("i")) for _ in range(arg.ndim))
    expr = Reorder(Table(arg.data, idxs), tuple(idxs[i] for i in axis))
    data, ctx = arg.ctx.eval(expr)
    return LazyTensor(
        data,
        ctx,
        tuple(arg.shape[i] for i in axis),
        arg.fill,
        arg.element_type,
        device=arg.device,
    )

reduce(op, x, /, *, axis=None, dtype=None, keepdims=False, init=None)

Reduces the input array x with the binary operator op. Reduces along the specified axis, with an initial value init.

Parameters

x: array input array. Should have a numeric data type. axis: Optional[Union[int, Tuple[int, ...]]] axis or axes along which reduction must be computed. By default, the reduction must be computed over the entire array. If a tuple of integers, reductions must be computed over multiple axes. Default: None.

Optional[dtype]

data type of the returned array. If None, a suitable data type will be calculated.

bool

if True, the reduced axes (dimensions) must be included in the result as singleton dimensions, and, accordingly, the result must be compatible with the input array (see :ref:broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default: False.

Optional

Initial value for the reduction. If None, a suitable initial value will be calculated. The initial value must be compatible with the operation defined by op. For example, if op is addition, the initial value should be zero; if op is multiplication, the initial value should be one.

Returns

out: array If the reduction was computed over the entire array, a zero-dimensional array containing the reduction; otherwise, a non-zero-dimensional array containing the reduction. The returned array must have a data type as described by the dtype parameter above.

Source code in src/finch/interface/lazy.py
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
def reduce(
    op: FinchOperator,
    x,
    /,
    *,
    axis: int | tuple[int, ...] | None = None,
    dtype=None,
    keepdims: bool = False,
    init=None,
):
    """
    Reduces the input array ``x`` with the binary operator ``op``. Reduces along
    the specified `axis`, with an initial value `init`.

    Parameters
    ----------
    x: array
        input array. Should have a numeric data type.
    axis: Optional[Union[int, Tuple[int, ...]]]
        axis or axes along which reduction must be computed. By default, the reduction
        must be computed over the entire array. If a tuple of integers, reductions must
        be computed over multiple axes. Default: ``None``.

    dtype: Optional[dtype]
        data type of the returned array. If ``None``, a suitable data type will be
        calculated.

    keepdims: bool
        if ``True``, the reduced axes (dimensions) must be included in the result as
        singleton dimensions, and, accordingly, the result must be compatible with the
        input array (see :ref:`broadcasting`). Otherwise, if ``False``, the reduced axes
        (dimensions) must not be included in the result. Default: ``False``.

    init: Optional
        Initial value for the reduction. If ``None``, a suitable initial value will be
        calculated. The initial value must be compatible with the operation defined by
        ``op``. For example, if ``op`` is addition, the initial value should be zero; if
        ``op`` is multiplication, the initial value should be one.

    Returns
    -------
    out: array
        If the reduction was computed over the entire array, a zero-dimensional array
        containing the reduction; otherwise, a non-zero-dimensional array containing the
        reduction. The returned array must have a data type as described by the
        ``dtype`` parameter above.
    """
    x = defer(x)
    op_type = op.ftype
    if not isinstance(op_type, FinchOperatorFType):
        raise TypeError(f"Expected a Finch operator type, got {op_type}")
    explicit_dtype = dtype is not None
    if dtype is not None:
        dtype = ftype(dtype)
    if init is None:
        init = init_value(op_type, dtype or x.element_type)
        if explicit_dtype:
            assert dtype is not None
            init = dtype(init)
    if axis is None:
        axis = tuple(range(x.ndim))
    axis = normalize_axis_tuple(axis, x.ndim)
    if axis is None or isinstance(axis, int):
        raise ValueError("axis must be a tuple")

    shape = tuple(x.shape[n] for n in range(x.ndim) if n not in axis)
    fields = tuple(Field(gensym("i")) for _ in range(x.ndim))
    ctx = x.ctx
    if isinstance(init, LazyTensor):
        # The init may be a zero-dimensional tensor, such as a fill value.
        if init.ndim != 0:
            raise ValueError("The init of a reduction must be zero-dimensional")
        init_expr: LogicExpression = Table(init.data, ())
        init_fill = init.fill
        ctx = ctx.join(init.ctx)
    else:
        init_expr = Literal(init)
        # The init may be a raw value, which is a static fill.
        init_fill = as_fill(init)
    data: LogicExpression = Aggregate(
        Literal(op),
        init_expr,
        Table(x.data, fields),
        tuple(fields[i] for i in axis),
    )
    if keepdims:
        keeps = tuple(
            fields[i] if i not in axis else Field(gensym("j")) for i in range(x.ndim)
        )
        data = Reorder(data, keeps)
        shape = tuple(x.shape[i] if i not in axis else 1 for i in range(x.ndim))
    if dtype is None:
        dtype = fixpoint_type(op_type, init_fill.ftype, x.element_type)
    expr, ctx = ctx.eval(data)
    return LazyTensor(expr, ctx, shape, init_fill, dtype, x.device)

squeeze(x, /, axis)

Removes singleton dimensions (axes) from x.

Parameters

x: array input array. axis: Union[int, Tuple[int, ...]] axis (or axes) to squeeze.

Returns

out: array an output array having the same data type and elements as x.

Raises

ValueError If a specified axis has a size greater than one (i.e., it is not a singleton dimension), a ValueError should be raised.

Source code in src/finch/interface/lazy.py
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
def squeeze(
    x,
    /,
    axis: int | tuple[int, ...],
) -> LazyTensor:
    """
    Removes singleton dimensions (axes) from ``x``.

    Parameters
    ----------
    x: array
        input array.
    axis: Union[int, Tuple[int, ...]]
        axis (or axes) to squeeze.

    Returns
    -------
    out: array
        an output array having the same data type and elements as ``x``.

    Raises
    ------
    ValueError
        If a specified axis has a size greater than one (i.e., it is not a
        singleton dimension), a ``ValueError`` should be raised.
    """
    x = defer(x)
    if not isinstance(axis, (tuple, list)):
        axis = (axis,)
    axis = normalize_axis_tuple(axis, x.ndim)
    if isinstance(axis, int):
        raise ValueError(f"Invalid axis: {axis}. Axis must be a tuple of integers.")
    if len(axis) != len(set(axis)):
        raise ValueError(f"Invalid axis: {axis}. Axis must be unique.")
    if not set(axis).issubset(range(x.ndim)):
        raise ValueError(f"Invalid axis: {axis}. Axis must be within bounds.")
    if not builtins.all(x.shape[d] == 1 for d in axis):
        raise ValueError(f"Invalid axis: {axis}. Axis to drop must have size 1.")
    newaxis = [n for n in range(x.ndim) if n not in axis]
    idxs_1 = tuple(Field(gensym("i")) for _ in range(x.ndim))
    idxs_2 = tuple(idxs_1[n] for n in newaxis)
    expr = Reorder(Table(x.data, idxs_1), idxs_2)
    shape_2 = tuple(x.shape[n] for n in newaxis)
    data_2, ctx = x.ctx.eval(expr)
    return LazyTensor(data_2, ctx, shape_2, x.fill, x.element_type, x.device)

std(x, /, *, axis=None, correction=0.0, keepdims=False)

Calculates the standard deviation of the input array x.

Source code in src/finch/interface/lazy.py
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
def std(
    x,
    /,
    *,
    axis: int | tuple[int, ...] | None = None,
    correction: float = 0.0,
    keepdims: bool = False,
):
    """
    Calculates the standard deviation of the input array ``x``.
    """
    x = defer(x)
    n = (
        np.prod(tuple(x.shape[i] for i in range(x.ndim) if i in axis))
        if isinstance(axis, tuple)
        else (np.prod(x.shape) if axis is None else x.shape[axis])
    )
    m = mean(x, axis=axis, keepdims=True)
    norm = vector_norm(x - m, axis=axis, keepdims=keepdims, ord=2)
    return truediv(norm, sqrt(norm.element_type(n - correction)))

tensordot(x1, x2, /, *, axes=2)

Computes the tensordot operation.

Returns a LazyTensor if either x1 or x2 is a LazyTensor. Otherwise, computes the result eagerly.

Source code in src/finch/interface/lazy.py
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
def tensordot(
    x1, x2, /, *, axes: int | tuple[Sequence[int], Sequence[int]] = 2
) -> LazyTensor:
    """
    Computes the tensordot operation.

    Returns a LazyTensor if either x1 or x2 is a LazyTensor.
    Otherwise, computes the result eagerly.
    """
    x1 = defer(x1)
    x2 = defer(x2)

    # Parse axes
    if not isinstance(axes, tuple):
        N = int(axes)
        if N < 0:
            raise ValueError("expected axes to be a non-negative integer")
        axes_a = list(range(x1.ndim - N, x1.ndim))
        axes_b = list(range(N))
    else:
        axes_a, axes_b = (list(ax) for ax in axes)

    # Normalize negative axes. We need list
    axes_a = [(a if a >= 0 else x1.ndim + a) for a in axes_a]
    axes_b = [(b if b >= 0 else x2.ndim + b) for b in axes_b]

    # Check axes lengths and shapes
    if len(axes_a) != len(axes_b):
        raise ValueError("shape-mismatch for sum")
    for a, b in zip(axes_a, axes_b, strict=True):
        if x1.shape[a] != x2.shape[b]:
            raise ValueError("shape-mismatch for sum")

    # Move axes to contract to the end of x1 and to the front of x2
    notin_a = [k for k in range(x1.ndim) if k not in axes_a]
    notin_b = [k for k in range(x2.ndim) if k not in axes_b]
    newaxes_a = notin_a + axes_a
    newaxes_b = axes_b + notin_b
    # Permute
    x1p = permute_dims(x1, tuple(newaxes_a))
    x2p = permute_dims(x2, tuple(newaxes_b))

    # Expand x1p and x2p so that their contracted axes align for broadcasting
    # so we can multiply them

    # For x1p, add len(notin_b) singleton dims at the end
    added_dims = tuple(-(i + 1) for i in range(len(notin_b)))
    x1p = expand_dims(x1p, axis=added_dims)

    # For x2p, add len(notin_a) singleton dims at the front
    added_dims = tuple(i for i in range(len(notin_a)))
    x2p = expand_dims(x2p, axis=added_dims)

    # Multiply (broadcasted)
    expanded_product = multiply(x1p, x2p)

    sum_axes = tuple(range(len(notin_a), len(notin_a) + len(axes_a)))
    if not sum_axes:
        return expanded_product
    return sum(
        expanded_product,
        axis=sum_axes,
        dtype=result_type(x1.element_type, x2.element_type),
    )

var(x, /, *, axis=None, correction=0.0, keepdims=False)

Calculates the variance of the input array x.

Source code in src/finch/interface/lazy.py
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
def var(
    x,
    /,
    *,
    axis: int | tuple[int, ...] | None = None,
    correction: float = 0.0,
    keepdims: bool = False,
):
    """
    Calculates the variance of the input array ``x``.
    """
    x = defer(x)
    n = (
        np.prod(tuple(x.shape[i] for i in range(x.ndim) if i in axis))
        if isinstance(axis, tuple)
        else (np.prod(x.shape) if axis is None else x.shape[axis])
    )
    m = mean(x, axis=axis, keepdims=True)
    v = sum(pow(x - m, x.element_type(2.0)), axis=axis, keepdims=keepdims)
    return truediv(v, x.element_type(n - correction))

vecdot(x1, x2, /, *, axis=-1)

Computes the vector dot product along the specified axis.

Source code in src/finch/interface/lazy.py
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
def vecdot(x1, x2, /, *, axis=-1) -> LazyTensor:
    """
    Computes the vector dot product along the specified axis.
    """
    x1 = defer(x1)
    x2 = defer(x2)

    # check broadcastability
    if not is_broadcastable(x1.shape, x2.shape):
        raise ValueError("Shapes are not broadcastable for vector dot product")

    # check if dims of axis are the same
    if x1.shape[axis] != x2.shape[axis]:
        raise ValueError(
            "Shapes are not compatible for vector dot product along the specified axis"
        )

    return reduce(
        ffuncs.add,
        multiply(conj(x1), x2),
        axis=axis,
        dtype=result_type(x1.element_type, x2.element_type),
    )