-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy patheager.py
More file actions
1374 lines (1036 loc) · 37.6 KB
/
eager.py
File metadata and controls
1374 lines (1036 loc) · 37.6 KB
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
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
770
771
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
817
818
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import builtins
import operator
import sys
from abc import ABC
from collections.abc import Sequence
from typing import Any
from ..algebra import FinchOperator
from .overrides import OverrideTensor
class EagerTensor(OverrideTensor, ABC):
def override_module(self):
return sys.modules[__name__]
def __add__(self, other):
return add(self, other)
def __radd__(self, other):
return add(other, self)
def __sub__(self, other):
return subtract(self, other)
def __rsub__(self, other):
return subtract(other, self)
def __mul__(self, other):
return multiply(self, other)
def __rmul__(self, other):
return multiply(other, self)
def __abs__(self):
return abs(self)
def __pos__(self):
return positive(self)
def __neg__(self):
return negative(self)
def __invert__(self):
return bitwise_inverse(self)
def __and__(self, other):
return bitwise_and(self, other)
def __rand__(self, other):
return bitwise_and(other, self)
def __lshift__(self, other):
return bitwise_left_shift(self, other)
def __rlshift__(self, other):
return bitwise_left_shift(other, self)
def __or__(self, other):
return bitwise_or(self, other)
def __ror__(self, other):
return bitwise_or(other, self)
def __rshift__(self, other):
return bitwise_right_shift(self, other)
def __rrshift__(self, other):
return bitwise_right_shift(other, self)
def __xor__(self, other):
return bitwise_xor(self, other)
def __rxor__(self, other):
return bitwise_xor(other, self)
def __truediv__(self, other):
return truediv(self, other)
def __rtruediv__(self, other):
return truediv(other, self)
def __floordiv__(self, other):
return floordiv(self, other)
def __rfloordiv__(self, other):
return floordiv(other, self)
def __mod__(self, other):
return mod(self, other)
def __rmod__(self, other):
return mod(other, self)
def __pow__(self, other):
return power(self, other)
def __rpow__(self, other):
return power(other, self)
def __matmul__(self, other):
return matmul(self, other)
def __rmatmul__(self, other):
return matmul(other, self)
def __sin__(self):
return sin(self)
def __sinh__(self):
return sinh(self)
def __cos__(self):
return cos(self)
def __cosh__(self):
return cosh(self)
def __tan__(self):
return tan(self)
def __tanh__(self):
return tanh(self)
def __asin__(self):
return asin(self)
def __asinh__(self):
return asinh(self)
def __acos__(self):
return acos(self)
def __acosh__(self):
return acosh(self)
def __atan__(self):
return atan(self)
def __atanh__(self):
return atanh(self)
def __atan2__(self, other):
return atan2(self, other)
def __complex__(self):
"""
Converts a zero-dimensional array to a Python `complex` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to complex.")
# dispatch to the scalar value's `__complex__` method
return complex(self[()])
def __float__(self):
"""
Converts a zero-dimensional array to a Python `float` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to float.")
# dispatch to the scalar value's `__float__` method
return float(self[()])
def __int__(self):
"""
Converts a zero-dimensional array to a Python `int` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to int.")
# dispatch to the scalar value's `__int__` method
return int(self[()])
def __bool__(self):
"""
Converts a zero-dimensional array to a Python `bool` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to bool.")
# dispatch to the scalar value's `__bool__` method
return bool(self[()])
def __index__(self) -> int:
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to index.")
return operator.index(self.__int__())
def __log__(self):
return log(self)
def __log1p__(self):
return log1p(self)
def __log2__(self):
return log2(self)
def __log10__(self):
return log10(self)
def __logaddexp__(self, other):
return logaddexp(self, other)
def __logical_and__(self, other):
return logical_and(self, other)
def __logical_or__(self, other):
return logical_or(self, other)
def __logical_xor__(self, other):
return logical_xor(self, other)
def __logical_not__(self):
return logical_not(self)
def __lt__(self, other):
return less(self, other)
def __le__(self, other):
return less_equal(self, other)
def __gt__(self, other):
return greater(self, other)
def __ge__(self, other):
return greater_equal(self, other)
def __eq__(self, other):
return equal(self, other)
def __ne__(self, other):
return not_equal(self, other)
from . import lazy # noqa: E402
from .fuse import compute # noqa: E402
def full(
shape: int | tuple[int, ...],
fill_value: bool | complex,
*,
dtype: Any | None = 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.
"""
return compute(lazy.full(shape, fill_value, dtype=dtype))
def permute_dims(arg, /, axis: tuple[int, ...]):
if isinstance(arg, lazy.LazyTensor):
return lazy.permute_dims(arg, axis=axis)
return compute(lazy.permute_dims(arg, axis=axis))
def expand_dims(
x,
/,
axis: int | tuple[int, ...] = 0,
):
if isinstance(x, lazy.LazyTensor):
return lazy.expand_dims(x, axis=axis)
return compute(lazy.expand_dims(x, axis=axis))
def squeeze(
x,
/,
axis: int | tuple[int, ...],
):
if isinstance(x, lazy.LazyTensor):
return lazy.squeeze(x, axis=axis)
return compute(lazy.squeeze(x, axis=axis))
def reduce(
op: FinchOperator,
x,
/,
*,
axis: int | tuple[int, ...] | None = None,
dtype=None,
keepdims: bool = False,
init=None,
):
if isinstance(x, lazy.LazyTensor):
return lazy.reduce(op, x, axis=axis, dtype=dtype, keepdims=keepdims, init=init)
return compute(
lazy.reduce(op, x, axis=axis, dtype=dtype, keepdims=keepdims, init=init)
)
def round(x):
if isinstance(x, lazy.LazyTensor):
return lazy.round(x)
return compute(lazy.round(x))
def floor(x):
if isinstance(x, lazy.LazyTensor):
return lazy.floor(x)
return compute(lazy.floor(x))
def ceil(x):
if isinstance(x, lazy.LazyTensor):
return lazy.ceil(x)
return compute(lazy.ceil(x))
def trunc(x):
if isinstance(x, lazy.LazyTensor):
return lazy.trunc(x)
return compute(lazy.trunc(x))
def sum(
x,
/,
*,
axis: int | tuple[int, ...] | None = None,
dtype=None,
keepdims: bool = False,
):
if isinstance(x, lazy.LazyTensor):
return lazy.sum(x, axis=axis, dtype=dtype, keepdims=keepdims)
return compute(lazy.sum(x, axis=axis, dtype=dtype, keepdims=keepdims))
def prod(
x,
/,
*,
axis: int | tuple[int, ...] | None = None,
dtype=None,
keepdims: bool = False,
):
if isinstance(x, lazy.LazyTensor):
return lazy.prod(x, axis=axis, dtype=dtype, keepdims=keepdims)
return compute(lazy.prod(x, axis=axis, dtype=dtype, keepdims=keepdims))
def elementwise(f: FinchOperator, *args):
if builtins.any(isinstance(arg, lazy.LazyTensor) for arg in args):
return lazy.elementwise(f, *args)
return compute(lazy.elementwise(f, *args))
def add(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.add(x1, x2)
return compute(lazy.add(x1, x2))
def reciprocal(x):
if isinstance(x, lazy.LazyTensor):
return lazy.reciprocal(x)
return compute(lazy.reciprocal(x))
def subtract(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.subtract(x1, x2)
return compute(lazy.subtract(x1, x2))
def multiply(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.multiply(x1, x2)
return compute(lazy.multiply(x1, x2))
def divide(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.divide(x1, x2)
return compute(lazy.divide(x1, x2))
def abs(x):
if isinstance(x, lazy.LazyTensor):
return lazy.abs(x)
return compute(lazy.abs(x))
def positive(x):
if isinstance(x, lazy.LazyTensor):
return lazy.positive(x)
return compute(lazy.positive(x))
def negative(x):
if isinstance(x, lazy.LazyTensor):
return lazy.negative(x)
return compute(lazy.negative(x))
def matmul(x1, x2, /):
"""
Computes the matrix product.
Returns a LazyTensor if either x1 or x2 is a LazyTensor.
Otherwise, computes the result eagerly.
"""
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.matmul(x1, x2)
c = lazy.matmul(x1, x2)
return compute(c)
def matrix_transpose(x, /):
"""
Computes the transpose of a matrix or stack of matrices.
"""
if isinstance(x, lazy.LazyTensor):
return lazy.matrix_transpose(x)
return compute(lazy.matrix_transpose(x))
def bitwise_inverse(x):
if isinstance(x, lazy.LazyTensor):
return lazy.bitwise_inverse(x)
return compute(lazy.bitwise_inverse(x))
def bitwise_and(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.bitwise_and(x1, x2)
return compute(lazy.bitwise_and(x1, x2))
def bitwise_left_shift(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.bitwise_left_shift(x1, x2)
return compute(lazy.bitwise_left_shift(x1, x2))
def bitwise_or(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.bitwise_or(x1, x2)
return compute(lazy.bitwise_or(x1, x2))
def bitwise_right_shift(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.bitwise_right_shift(x1, x2)
return compute(lazy.bitwise_right_shift(x1, x2))
def bitwise_xor(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.bitwise_xor(x1, x2)
return compute(lazy.bitwise_xor(x1, x2))
def truediv(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.truediv(x1, x2)
return compute(lazy.truediv(x1, x2))
def floordiv(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.floordiv(x1, x2)
return compute(lazy.floordiv(x1, x2))
def mod(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.mod(x1, x2)
return compute(lazy.mod(x1, x2))
def pow(x1, x2):
return power(x1, x2)
def power(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.power(x1, x2)
return compute(lazy.power(x1, x2))
def remainder(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.remainder(x1, x2)
return compute(lazy.remainder(x1, x2))
def tensordot(x1, x2, /, *, axes: int | tuple[Sequence[int], Sequence[int]]):
"""
Computes the tensordot operation.
Returns a LazyTensor if either x1 or x2 is a LazyTensor.
Otherwise, computes the result eagerly.
"""
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.tensordot(x1, x2, axes=axes)
return compute(lazy.tensordot(x1, x2, axes=axes))
def vecdot(x1, x2, /, *, axis=-1):
"""
Computes the (vector) dot product of two arrays.
Parameters
----------
x1: array
The first input tensor.
x2: array
The second input tensor.
axis: int, optional
The axis along which to compute the dot product. Default is -1 (last axis).
Returns
-------
out: array
A tensor containing the dot product of `x1` and `x2` along the specified axis.
"""
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.vecdot(x1, x2, axis=axis)
return compute(lazy.vecdot(x1, x2, axis=axis))
def any(x, /, *, axis: int | tuple[int, ...] | None = None, keepdims: bool = False):
if isinstance(x, lazy.LazyTensor):
return lazy.any(x, axis=axis, keepdims=keepdims)
return compute(lazy.any(x, axis=axis, keepdims=keepdims))
def all(x, /, *, axis: int | tuple[int, ...] | None = None, keepdims: bool = False):
if isinstance(x, lazy.LazyTensor):
return lazy.all(x, axis=axis, keepdims=keepdims)
return compute(lazy.all(x, axis=axis, keepdims=keepdims))
def real(x):
if isinstance(x, lazy.LazyTensor):
return lazy.real(x)
return compute(lazy.real(x))
def imag(x):
if isinstance(x, lazy.LazyTensor):
return lazy.imag(x)
return compute(lazy.imag(x))
def min(x, /, *, axis: int | tuple[int, ...] | None = None, keepdims: bool = False):
if isinstance(x, lazy.LazyTensor):
return lazy.min(x, axis=axis, keepdims=keepdims)
return compute(lazy.min(x, axis=axis, keepdims=keepdims))
minimum = min
def max(x, /, *, axis: int | tuple[int, ...] | None = None, keepdims: bool = False):
if isinstance(x, lazy.LazyTensor):
return lazy.max(x, axis=axis, keepdims=keepdims)
return compute(lazy.max(x, axis=axis, keepdims=keepdims))
maximum = max
def clip(x, /, *, min=None, max=None):
if (
isinstance(x, lazy.LazyTensor)
or isinstance(min, lazy.LazyTensor)
or isinstance(max, lazy.LazyTensor)
):
return lazy.clip(x, min=min, max=max)
return compute(lazy.clip(x, min=min, max=max))
def sqrt(x):
if isinstance(x, lazy.LazyTensor):
return lazy.sqrt(x)
return compute(lazy.sqrt(x))
def square(x):
if isinstance(x, lazy.LazyTensor):
return lazy.square(x)
return compute(lazy.square(x))
def signbit(x):
if isinstance(x, lazy.LazyTensor):
return lazy.signbit(x)
return compute(lazy.signbit(x))
def sign(x):
if isinstance(x, lazy.LazyTensor):
return lazy.sign(x)
return compute(lazy.sign(x))
# manipulation functions:
# https://data-apis.org/array-api/2024.12/API_specification/manipulation_functions.html
def broadcast_to(x, /, shape: Sequence[int]):
"""
Broadcasts an array to a new shape.
Parameters
----------
x: array
The input tensor to be broadcasted.
shape: Sequence[int]
The target shape to which the input tensor should be broadcasted.
Returns
-------
out: array
A tensor with the same data as `x`, but with the specified shape.
"""
shape = tuple(shape) # Ensure shape is a tuple for consistency
if isinstance(x, lazy.LazyTensor):
return lazy.broadcast_to(x, shape=shape)
return compute(lazy.broadcast_to(x, shape=shape))
def broadcast_arrays(*args):
"""
Broadcasts one or more arrays against one another.
Parameters
----------
*args: array
an arbitrary number of to-be broadcasted arrays.
Returns
-------
out: List[array]
a list of broadcasted arrays. Each array has the same shape.
Element types are preserved.
"""
if builtins.any(isinstance(arg, lazy.LazyTensor) for arg in args):
return lazy.broadcast_arrays(*args)
# compute can take in a list of LazyTensors
return compute(lazy.broadcast_arrays(*args))
def concat(arrays: tuple | list, /, *, axis: int | None = 0):
"""
Concatenates a sequence of arrays along an existing axis.
Parameters
----------
arrays: tuple or list
A sequence of arrays to concatenate. Arrays must have the same shape
except in the dimension corresponding to the specified axis.
axis: int, optional
The axis along which to concatenate the arrays. Default is 0. If None,
the arrays are flattened before concatenation.
Returns
-------
out: array
A new concatenated array.
"""
if builtins.any(isinstance(arr, lazy.LazyTensor) for arr in arrays):
return lazy.concat(arrays, axis=axis)
return compute(lazy.concat(arrays, axis=axis))
def moveaxis(x, source: int | tuple[int, ...], destination: int | tuple[int, ...], /):
"""
Moves array axes (dimensions) to new positions,
while leaving other axes in their original positions.
Args
---------
- x (array) - input array.
- source - Axes to move.
- destination - indices defining the desired
positions for each respective source axis index.
Returns
--------
- out (array) - an array containing reordered axes.
"""
if isinstance(x, lazy.LazyTensor):
return lazy.moveaxis(x, source, destination)
return compute(lazy.moveaxis(x, source, destination))
def stack(arrays: Sequence, /, *, axis: int = 0):
"""
Stacks a sequence of arrays along a new axis.
Parameters
----------
arrays: Sequence
A sequence of arrays to stack. All arrays must have the same shape.
axis: int, optional
The axis along which to stack the arrays. Default is 0.
Returns
-------
out: array
A new array with the stacked arrays along the specified axis.
"""
if builtins.any(isinstance(arr, lazy.LazyTensor) for arr in arrays):
return lazy.stack(arrays, axis=axis)
return compute(lazy.stack(arrays, axis=axis))
def split_dims(x, axis: int, shape: tuple):
"""
Split a dimension into multiple dimensions. The product
of the sizes in the `shape` tuple must equal the size
of the dimension being split.
Parameters
----------
x: array
The input tensor to split
axis: int
The axis to split
shape: tuple
The new shape for the split dimensions
Returns
-------
out: array
A tensor with the specified dimension split into multiple dimensions
Examples
--------
>>> import numpy as np
>>> x = np.arange(12).reshape(2, 6) # shape (2, 6)
>>> result = split_dims(x, axis=1, shape=(2, 3))
>>> result.shape
(2, 2, 3)
"""
if isinstance(x, lazy.LazyTensor):
return lazy.split_dims(x, axis, shape)
return compute(lazy.split_dims(x, axis, shape))
def combine_dims(x, axes: tuple[int, ...]):
"""
Combine multiple consecutive dimensions into a single dimension.
The resulting axis will have a size equal to the product of the
sizes of the combined axes.
Parameters
----------
x: array
The input tensor
axes: tuple[int, ...]
Consecutive axes to combine.
The axes will be considered in increasing order.
So passing axes=(2, 1, 3) will be equivalent to
passing axes=(1, 2, 3).
Returns
-------
out: array
A tensor with the specified dimensions combined into one
Examples
--------
>>> import numpy as np
>>> x = np.arange(24).reshape(2, 3, 4) # shape (2, 3, 4)
>>> result = combine_dims(x, axes=(1, 2))
>>> result.shape
(2, 12)
"""
if isinstance(x, lazy.LazyTensor):
return lazy.combine_dims(x, axes)
return compute(lazy.combine_dims(x, axes))
def flatten(x):
"""
Flattens the input tensor into a 1D tensor.
Parameters
----------
x: array
The input tensor to be flattened.
Returns
-------
out: array
A new tensor that is a flattened version of the input.
Examples
--------
>>> import numpy as np
>>> x = np.arange(24).reshape(2, 3, 4) # shape (2, 3, 4)
>>> result = flatten(x)
>>> result.shape
(24,)
"""
if isinstance(x, lazy.LazyTensor):
return lazy.flatten(x)
return compute(lazy.flatten(x))
# trigonometric functions:
def sin(x):
if isinstance(x, lazy.LazyTensor):
return lazy.sin(x)
return compute(lazy.sin(x))
def sinh(x):
if isinstance(x, lazy.LazyTensor):
return lazy.sinh(x)
return compute(lazy.sinh(x))
def cos(x):
if isinstance(x, lazy.LazyTensor):
return lazy.cos(x)
return compute(lazy.cos(x))
def cosh(x):
if isinstance(x, lazy.LazyTensor):
return lazy.cosh(x)
return compute(lazy.cosh(x))
def tan(x):
if isinstance(x, lazy.LazyTensor):
return lazy.tan(x)
return compute(lazy.tan(x))
def tanh(x):
if isinstance(x, lazy.LazyTensor):
return lazy.tanh(x)
return compute(lazy.tanh(x))
def asin(x):
if isinstance(x, lazy.LazyTensor):
return lazy.asin(x)
return compute(lazy.asin(x))
def asinh(x):
if isinstance(x, lazy.LazyTensor):
return lazy.asinh(x)
return compute(lazy.asinh(x))
def acos(x):
if isinstance(x, lazy.LazyTensor):
return lazy.acos(x)
return compute(lazy.acos(x))
def acosh(x):
if isinstance(x, lazy.LazyTensor):
return lazy.acosh(x)
return compute(lazy.acosh(x))
def atan(x):
if isinstance(x, lazy.LazyTensor):
return lazy.atan(x)
return compute(lazy.atan(x))
def hypot(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.hypot(x1, x2)
return compute(lazy.hypot(x1, x2))
def atanh(x):
if isinstance(x, lazy.LazyTensor):
return lazy.atanh(x)
return compute(lazy.atanh(x))
def atan2(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.atan2(x1, x2)
return compute(lazy.atan2(x1, x2))
def exp(x):
if isinstance(x, lazy.LazyTensor):
return lazy.exp(x)
return compute(lazy.exp(x))
def expm1(x):
if isinstance(x, lazy.LazyTensor):
return lazy.expm1(x)
return compute(lazy.expm1(x))
def log(x):
if isinstance(x, lazy.LazyTensor):
return lazy.log(x)
return compute(lazy.log(x))
def log1p(x):
if isinstance(x, lazy.LazyTensor):
return lazy.log1p(x)
return compute(lazy.log1p(x))
def log2(x):
if isinstance(x, lazy.LazyTensor):
return lazy.log2(x)
return compute(lazy.log2(x))
def log10(x):
if isinstance(x, lazy.LazyTensor):
return lazy.log10(x)
return compute(lazy.log10(x))
def logaddexp(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.logaddexp(x1, x2)
return compute(lazy.logaddexp(x1, x2))
def copysign(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.copysign(x1, x2)
return compute(lazy.copysign(x1, x2))
def nextafter(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.nextafter(x1, x2)
return compute(lazy.nextafter(x1, x2))
def isfinite(x):
if isinstance(x, lazy.LazyTensor):
return lazy.isfinite(x)
return compute(lazy.isfinite(x))
def isinf(x):
if isinstance(x, lazy.LazyTensor):
return lazy.isinf(x)
return compute(lazy.isinf(x))
def isnan(x):
if isinstance(x, lazy.LazyTensor):
return lazy.isnan(x)
return compute(lazy.isnan(x))
def iscomplexobj(x):
if isinstance(x, lazy.LazyTensor):
return lazy.iscomplexobj(x)
return compute(lazy.iscomplexobj(x))
def logical_and(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.logical_and(x1, x2)
return compute(lazy.logical_and(x1, x2))
def logical_or(x1, x2):
if isinstance(x1, lazy.LazyTensor) or isinstance(x2, lazy.LazyTensor):
return lazy.logical_or(x1, x2)
return compute(lazy.logical_or(x1, x2))
def logical_xor(x1, x2):