forked from explosion/thinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.py
989 lines (811 loc) · 40 KB
/
types.py
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
from typing import Union, Tuple, Sized, Container, Any, TypeVar, Callable
from typing import Iterable, Iterator, Sequence, Dict, Generic, cast
from typing import Optional, List, overload
from dataclasses import dataclass
import numpy
import sys
try:
import cupy
get_array_module = cupy.get_array_module
except ImportError:
get_array_module = lambda obj: numpy
# Use typing_extensions for Python versions < 3.8
if sys.version_info < (3, 8):
from typing_extensions import Protocol, Literal
else:
from typing import Protocol, Literal # noqa: F401
# fmt: off
XY_YZ_OutT = TypeVar("XY_YZ_OutT")
XY_XY_OutT = TypeVar("XY_XY_OutT")
DeviceTypes = Literal["cpu", "gpu", "tpu"]
Batchable = Union["Pairs", "Ragged", "Padded", "ArrayXd", List, Tuple]
Xp = Union["numpy", "cupy"] # type: ignore
Shape = Tuple[int, ...]
DTypes = Literal["f", "i", "float16", "float32", "float64", "int32", "int64", "uint32", "uint64"]
DTypesFloat = Literal["f", "float32", "float16", "float64"]
DTypesInt = Literal["i", "int32", "int64", "uint32", "uint64"]
Array1d = Union["Floats1d", "Ints1d"]
Array2d = Union["Floats2d", "Ints2d"]
Array3d = Union["Floats3d", "Ints3d"]
Array4d = Union["Floats4d", "Ints4d"]
FloatsXd = Union["Floats1d", "Floats2d", "Floats3d", "Floats4d"]
IntsXd = Union["Ints1d", "Ints2d", "Ints3d", "Ints4d"]
ArrayXd = Union[FloatsXd, IntsXd]
List1d = Union[List["Floats1d"], List["Ints1d"]]
List2d = Union[List["Floats2d"], List["Ints2d"]]
List3d = Union[List["Floats3d"], List["Ints3d"]]
List4d = Union[List["Floats4d"], List["Ints4d"]]
ListXd = Union[List["FloatsXd"], List["IntsXd"]]
ArrayT = TypeVar("ArrayT")
SelfT = TypeVar("SelfT")
Array1dT = TypeVar("Array1dT", bound="Array1d")
# These all behave the same as far as indexing is concerned
Slicish = Union[slice, List[int], "ArrayXd"]
_1_KeyScalar = int
_1_Key1d = Slicish
_1_AllKeys = Union[_1_KeyScalar, _1_Key1d]
_F1_AllReturns = Union[float, "Floats1d"]
_I1_AllReturns = Union[int, "Ints1d"]
_2_KeyScalar = Tuple[int, int]
_2_Key1d = Union[int, Tuple[Slicish, int], Tuple[int, Slicish]]
_2_Key2d = Union[Tuple[Slicish, Slicish], Slicish]
_2_AllKeys = Union[_2_KeyScalar, _2_Key1d, _2_Key2d]
_F2_AllReturns = Union[float, "Floats1d", "Floats2d"]
_I2_AllReturns = Union[int, "Ints1d", "Ints2d"]
_3_KeyScalar = Tuple[int, int, int]
_3_Key1d = Union[Tuple[int, int], Tuple[int, int, Slicish], Tuple[int, Slicish, int], Tuple[Slicish, int, int]]
_3_Key2d = Union[int, Tuple[int, Slicish], Tuple[Slicish, int], Tuple[int, Slicish, Slicish], Tuple[Slicish, int, Slicish], Tuple[Slicish, Slicish, int]]
_3_Key3d = Union[Slicish, Tuple[Slicish, Slicish], Tuple[Slicish, Slicish, Slicish]]
_3_AllKeys = Union[_3_KeyScalar, _3_Key1d, _3_Key2d, _3_Key3d]
_F3_AllReturns = Union[float, "Floats1d", "Floats2d", "Floats3d"]
_I3_AllReturns = Union[int, "Ints1d", "Ints2d", "Ints3d"]
_4_KeyScalar = Tuple[int, int, int, int]
_4_Key1d = Union[Tuple[int, int, int], Tuple[int, int, int, Slicish], Tuple[int, int, Slicish, int], Tuple[int, Slicish, int, int], Tuple[Slicish, int, int, int]]
_4_Key2d = Union[Tuple[int, int], Tuple[int, int, Slicish], Tuple[int, Slicish, int], Tuple[Slicish, int, int], Tuple[int, int, Slicish, Slicish], Tuple[int, Slicish, int, Slicish], Tuple[int, Slicish, Slicish, int], Tuple[Slicish, int, int, Slicish], Tuple[Slicish, int, Slicish, int], Tuple[Slicish, Slicish, int, int]]
_4_Key3d = Union[int, Tuple[int, Slicish], Tuple[Slicish, int], Tuple[int, Slicish, Slicish], Tuple[Slicish, int, Slicish], Tuple[Slicish, Slicish, int], Tuple[int, Slicish, Slicish, Slicish], Tuple[Slicish, int, Slicish, Slicish], Tuple[Slicish, Slicish, int, Slicish], Tuple[Slicish, Slicish, Slicish, int]]
_4_Key4d = Union[Slicish, Tuple[Slicish, Slicish], Tuple[Slicish, Slicish, Slicish], Tuple[Slicish, Slicish, Slicish, Slicish]]
_4_AllKeys = Union[_4_KeyScalar, _4_Key1d, _4_Key2d, _4_Key3d, _4_Key4d]
_F4_AllReturns = Union[float, "Floats1d", "Floats2d", "Floats3d", "Floats4d"]
_I4_AllReturns = Union[int, "Ints1d", "Ints2d", "Ints3d", "Ints4d"]
# Typedefs for the reduction methods.
Tru = Literal[True]
Fal = Literal[False]
OneAx = Union[int, Tuple[int]]
TwoAx = Tuple[int, int]
ThreeAx = Tuple[int, int, int]
FourAx = Tuple[int, int, int, int]
_1_AllAx = Optional[OneAx]
_2_AllAx = Union[Optional[TwoAx], OneAx]
_3_AllAx = Union[Optional[ThreeAx], TwoAx, OneAx]
_4_AllAx = Union[Optional[FourAx], ThreeAx, TwoAx, OneAx]
_1F_ReduceResults = Union[float, "Floats1d"]
_2F_ReduceResults = Union[float, "Floats1d", "Floats2d"]
_3F_ReduceResults = Union[float, "Floats1d", "Floats2d", "Floats3d"]
_4F_ReduceResults = Union[float, "Floats1d", "Floats2d", "Floats3d", "Floats4d"]
_1I_ReduceResults = Union[int, "Ints1d"]
_2I_ReduceResults = Union[int, "Ints1d", "Ints2d"]
_3I_ReduceResults = Union[int, "Ints1d", "Ints2d", "Ints3d"]
_4I_ReduceResults = Union[int, "Ints1d", "Ints2d", "Ints3d", "Ints4d"]
# TODO:
# We need to get correct overloads in for the following reduction methods.
# The 'sum' reduction is correct --- the others need to be just the same,
# but with a different name.
# max, min, prod, round, var, mean, ptp, std
# There's also one *slightly* different function, cumsum. This doesn't
# have a scalar version -- it always makes an array.
class _Array(Sized, Container):
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v)
@property
def dtype(self) -> DTypes: ...
@property
def data(self) -> memoryview: ...
@property
def flags(self) -> Any: ...
@property
def size(self) -> int: ...
@property
def itemsize(self) -> int: ...
@property
def nbytes(self) -> int: ...
@property
def ndim(self) -> int: ...
@property
def shape(self) -> Shape: ...
@property
def strides(self) -> Tuple[int, ...]: ...
# TODO: Is ArrayT right?
def astype(self: ArrayT, dtype: DTypes, order: str = ..., casting: str = ..., subok: bool = ..., copy: bool = ...) -> ArrayT: ...
def copy(self: ArrayT, order: str = ...) -> ArrayT: ...
def fill(self, value: Any) -> None: ...
# Shape manipulation
def reshape(self: ArrayT, shape: Shape, *, order: str = ...) -> ArrayT: ...
def transpose(self: ArrayT, axes: Shape) -> ArrayT: ...
# TODO: is this right? It returns 1d
def flatten(self, order: str = ...): ...
# TODO: is this right? It returns 1d
def ravel(self, order: str = ...): ...
def squeeze(self, axis: Union[int, Shape] = ...): ...
def __len__(self) -> int: ...
def __setitem__(self, key, value): ...
def __iter__(self) -> Iterator[Any]: ...
def __contains__(self, key) -> bool: ...
def __index__(self) -> int: ...
def __int__(self) -> int: ...
def __float__(self) -> float: ...
def __complex__(self) -> complex: ...
def __bool__(self) -> bool: ...
def __bytes__(self) -> bytes: ...
def __str__(self) -> str: ...
def __repr__(self) -> str: ...
def __copy__(self, order: str = ...): ...
def __deepcopy__(self, memo: dict) -> ArrayT: ...
def __lt__(self, other): ...
def __le__(self, other): ...
def __eq__(self, other): ...
def __ne__(self, other): ...
def __gt__(self, other): ...
def __ge__(self, other): ...
def __add__(self, other): ...
def __radd__(self, other): ...
def __iadd__(self, other): ...
def __sub__(self, other): ...
def __rsub__(self, other): ...
def __isub__(self, other): ...
def __mul__(self, other): ...
def __rmul__(self, other): ...
def __imul__(self, other): ...
def __truediv__(self, other): ...
def __rtruediv__(self, other): ...
def __itruediv__(self, other): ...
def __floordiv__(self, other): ...
def __rfloordiv__(self, other): ...
def __ifloordiv__(self, other): ...
def __mod__(self, other): ...
def __rmod__(self, other): ...
def __imod__(self, other): ...
def __divmod__(self, other): ...
def __rdivmod__(self, other): ...
# NumPy's __pow__ doesn't handle a third argument
def __pow__(self, other): ...
def __rpow__(self, other): ...
def __ipow__(self, other): ...
def __lshift__(self, other): ...
def __rlshift__(self, other): ...
def __ilshift__(self, other): ...
def __rshift__(self, other): ...
def __rrshift__(self, other): ...
def __irshift__(self, other): ...
def __and__(self, other): ...
def __rand__(self, other): ...
def __iand__(self, other): ...
def __xor__(self, other): ...
def __rxor__(self, other): ...
def __ixor__(self, other): ...
def __or__(self, other): ...
def __ror__(self, other): ...
def __ior__(self, other): ...
def __matmul__(self, other): ...
def __rmatmul__(self, other): ...
def __neg__(self: ArrayT) -> ArrayT: ...
def __pos__(self: ArrayT) -> ArrayT: ...
def __abs__(self: ArrayT) -> ArrayT: ...
def __invert__(self: ArrayT) -> ArrayT: ...
def get(self: ArrayT) -> ArrayT: ...
def all(self, axis: int = -1, out: Optional[ArrayT] = None, keepdims: bool = False) -> ArrayT: ...
def any(self, axis: int = -1, out: Optional[ArrayT] = None, keepdims: bool = False) -> ArrayT: ...
# def argmax(self, axis: int = -1, out: Optional["Array"] = None, keepdims: Union[Tru, Fal]=False) -> Union[int, "Ints1d"]: ...
def argmin(self, axis: int = -1, out: Optional[ArrayT] = None) -> ArrayT: ...
def clip(self, a_min: Any, a_max: Any, out: Optional[ArrayT]) -> ArrayT: ...
#def cumsum( self: ArrayT, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional[ArrayT] = None) -> ArrayT: ...
def max(self, axis: int = -1, out: Optional[ArrayT] = None) -> ArrayT: ...
# def mean(self, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional[SelfT] = None, keepdims: bool = False) -> "Array": ...
def min(self, axis: int = -1, out: Optional[ArrayT] = None) -> ArrayT: ...
def nonzero(self) -> ArrayT: ...
def prod(self, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional[ArrayT] = None, keepdims: bool = False) -> ArrayT: ...
def round(self, decimals: int = 0, out: Optional[ArrayT] = None) -> ArrayT: ...
# def sum(self, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional[ArrayT] = None, keepdims: bool = False) -> ArrayT: ...
def tobytes(self, order: str = "C") -> bytes: ...
def tolist(self) -> List[Any]: ...
def var(self: SelfT, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional[ArrayT] = None, ddof: int = 0, keepdims: bool = False) -> SelfT: ...
class _Floats(_Array):
@property
def dtype(self) -> DTypesFloat: ...
def fill(self, value: float) -> None: ...
def reshape(self, shape: Shape, *, order: str = ...) -> "_Floats": ...
class _Ints(_Array):
@property
def dtype(self) -> DTypesInt: ...
def fill(self, value: int) -> None: ...
def reshape(self, shape: Shape, *, order: str = ...) -> "_Ints": ...
"""
Extensive overloads to represent __getitem__ behaviour.
In an N+1 dimensional array, there will be N possible return types. For instance,
if you have a 2d array, you could get back a float (array[i, j]), a floats1d
(array[i]) or a floats2d (array[:i, :j]). You'll get the scalar if you have N
ints in the index, a 1d array if you have N-1 ints, etc.
So the trick here is to make a union with the various combinations that produce
each result type, and then only have one overload per result. If we overloaded
on each *key* type, that would get crazy, because there's tonnes of combinations.
In each rank, we can use the same key-types for float and int, but we need a
different return-type union.
"""
class _Array1d(_Array):
"""1-dimensional array."""
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=1)
@property
def ndim(self) -> Literal[1]: ...
@property
def shape(self) -> Tuple[int]: ...
def __iter__(self) -> Iterator[Union[float, int]]: ...
def astype(self, dtype: DTypes, order: str = ..., casting: str = ..., subok: bool = ..., copy: bool = ...) -> "_Array1d": ...
def flatten(self: SelfT, order: str = ...) -> SelfT: ...
def ravel(self: SelfT, order: str = ...) -> SelfT: ...
# These is actually a bit too strict: It's legal to say 'array1d + array2d'
# That's kind of bad code though; it's better to write array2d + array1d.
# We could relax this, but let's try the strict version.
def __add__(self: SelfT, other: Union[float, int, "Array1d"]) -> SelfT: ...
def __sub__(self: SelfT, other: Union[float, int, "Array1d"]) -> SelfT: ...
def __mul__(self: SelfT, other: Union[float, int, "Array1d"]) -> SelfT: ...
def __pow__(self: SelfT, other: Union[float, int, "Array1d"]) -> SelfT: ...
def __matmul__(self: SelfT, other: Union[float, int, "Array1d"]) -> SelfT: ...
# These are not too strict though: you can't do += with higher dimensional.
def __iadd__(self, other: Union[float, int, "Array1d"]): ...
def __isub__(self, other: Union[float, int, "Array1d"]): ...
def __imul__(self, other: Union[float, int, "Array1d"]): ...
def __ipow__(self, other: Union[float, int, "Array1d"]): ...
@overload
def argmax(self, keepdims: Fal = False, axis: int = -1, out: Optional[_Array] = None) -> int: ...
@overload
def argmax(self, keepdims: Tru, axis: int = -1, out: Optional[_Array] = None) -> "Ints1d": ...
def argmax(self, keepdims: bool = False, axis: int = -1, out: Optional[_Array] = None) -> Union[int, "Ints1d"]: ...
@overload
def mean(self, keepdims: Tru, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional["Floats1d"] = None) -> "Floats1d": ...
@overload
def mean(self, keepdims: Fal = False, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional["Floats1d"] = None) -> float: ...
def mean(self, keepdims: bool = False, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional["Floats1d"] = None) -> Union["Floats1d", float]: ...
class Floats1d(_Array1d, _Floats):
"""1-dimensional array of floats."""
T: "Floats1d"
@classmethod
def __get_validators__(cls):
"""Runtine validation for pydantic."""
yield lambda v: validate_array(v, ndim=1, dtype="f")
def __iter__(self) -> Iterator[float]: ...
@overload
def __getitem__(self, key: _1_KeyScalar) -> float: ...
@overload
def __getitem__(self, key: _1_Key1d) -> "Floats1d": ...
def __getitem__(self, key: _1_AllKeys) -> _F1_AllReturns: ...
@overload
def __setitem__(self, key: _1_KeyScalar, value: float) -> None: ...
@overload
def __setitem__(self, key: _1_Key1d, value: "Floats1d") -> None: ...
def __setitem__(self, key: _1_AllKeys, _F1_AllReturns) -> None: ...
@overload
def cumsum(self, *, keepdims: Tru, axis: Optional[OneAx] = None, out: Optional["Floats1d"] = None) -> "Floats1d": ...
@overload # Cumsum is unusual in this
def cumsum(self, *, keepdims: Fal, axis: Optional[OneAx] = None, out: Optional["Floats1d"] = None) -> "Floats1d": ...
def cumsum(self, *, keepdims: bool = False, axis: _1_AllAx = None, out: Optional["Floats1d"] = None) -> "Floats1d": ...
@overload
def sum(self, *, keepdims: Tru, axis: Optional[OneAx] = None, out: Optional["Floats1d"] = None) -> "Floats1d": ...
@overload
def sum(self, *, keepdims: Fal, axis: Optional[OneAx] = None, out = None) -> float: ...
def sum(self, *, keepdims: bool = False, axis: _1_AllAx = None, out: Optional["Floats1d"] = None) -> _1F_ReduceResults: ...
class Ints1d(_Array1d, _Ints):
"""1-dimensional array of ints."""
T: "Ints1d"
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=1, dtype="i")
def __iter__(self) -> Iterator[int]: ...
@overload
def __getitem__(self, key: _1_KeyScalar) -> int: ...
@overload
def __getitem__(self, key: _1_Key1d) -> "Ints1d": ...
def __getitem__(self, key: _1_AllKeys) -> _I1_AllReturns: ...
@overload
def __setitem__(self, key: _1_KeyScalar, value: int) -> None: ...
@overload
def __setitem__(self, key: _1_Key1d, value: Union[int, "Ints1d"]) -> None: ...
def __setitem__(self, key: _1_AllKeys, _I1_AllReturns) -> None: ...
@overload
def cumsum(self, *, keepdims: Tru, axis: Optional[OneAx] = None, out: Optional["Ints1d"] = None) -> "Ints1d": ...
@overload
def cumsum(self, *, keepdims: Fal = False, axis: Optional[OneAx] = None, out: Optional["Ints1d"] = None) -> "Ints1d": ...
def cumsum(self, *, keepdims: bool = False, axis: _1_AllAx = None, out: Optional["Ints1d"] = None) -> "Ints1d": ...
@overload
def sum(self, *, keepdims: Tru, axis: Optional[OneAx] = None, out: Optional["Ints1d"] = None) -> "Ints1d": ...
@overload
def sum(self, *, keepdims: Fal = False, axis: Optional[OneAx] = None, out = None) -> int: ...
def sum(self, *, keepdims: bool = False, axis: _1_AllAx = None, out: Optional["Ints1d"] = None) -> _1I_ReduceResults: ...
class _Array2d(_Array):
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=2)
@property
def ndim(self) -> Literal[2]: ...
@property
def shape(self) -> Tuple[int, int]: ...
def __iter__(self) -> Iterator[Array1d]: ...
def astype(self, dtype: DTypes, order: str = ..., casting: str = ..., subok: bool = ..., copy: bool = ...) -> "Array2d": ...
# These is actually a bit too strict: It's legal to say 'array2d + array3d'
# That's kind of bad code though; it's better to write array3d + array2d.
# We could relax this, but let's try the strict version.
def __add__(self: ArrayT, other: Union[float, int, Array1d, "Array2d"]) -> ArrayT: ...
def __sub__(self: ArrayT, other: Union[float, int, Array1d, "Array2d"]) -> ArrayT: ...
def __mul__(self: ArrayT, other: Union[float, int, Array1d, "Array2d"]) -> ArrayT: ...
def __pow__(self: ArrayT, other: Union[float, int, Array1d, "Array2d"]) -> ArrayT: ...
def __matmul__(self: ArrayT, other: Union[float, int, Array1d, "Array2d"]) -> ArrayT: ...
# These are not too strict though: you can't do += with higher dimensional.
def __iadd__(self, other: Union[float, int, Array1d, "Array2d"]): ...
def __isub__(self, other: Union[float, int, Array1d, "Array2d"]): ...
def __imul__(self, other: Union[float, int, Array1d, "Array2d"]): ...
def __ipow__(self, other: Union[float, int, Array1d, "Array2d"]): ...
@overload
def argmax(self, keepdims: Fal = False, axis: int = -1, out: Optional[_Array] = None) -> Ints1d: ...
@overload
def argmax(self, keepdims: Tru, axis: int = -1, out: Optional[_Array] = None) -> "Ints2d": ...
def argmax(self, keepdims: bool = False, axis: int = -1, out: Optional[_Array] = None) -> Union[Ints1d, "Ints2d"]: ...
@overload
def mean(self, keepdims: Fal = False, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional["Floats2d"] = None) -> Floats1d: ...
@overload
def mean(self, keepdims: Tru, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional["Floats2d"] = None) -> "Floats2d": ...
def mean(self, keepdims: bool = False, axis: int = -1, dtype: Optional[DTypes] = None, out: Optional["Floats2d"] = None) -> Union["Floats2d", Floats1d]: ...
class Floats2d(_Array2d, _Floats):
"""2-dimensional array of floats"""
T: "Floats2d"
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=2, dtype="f")
def __iter__(self) -> Iterator[Floats1d]: ...
@overload
def __getitem__(self, key: _2_KeyScalar) -> float: ...
@overload
def __getitem__(self, key: _2_Key1d) -> Floats1d: ...
@overload
def __getitem__(self, key: _2_Key2d) -> "Floats2d": ...
def __getitem__(self, key: _2_AllKeys) -> _F2_AllReturns: ...
@overload
def __setitem__(self, key: _2_KeyScalar, value: float) -> None: ...
@overload
def __setitem__(self, key: _2_Key1d, value: Union[float, Floats1d]) -> None: ...
@overload
def __setitem__(self, key: _2_Key2d, value: _F2_AllReturns) -> None: ...
def __setitem__(self, key: _2_AllKeys, value: _F2_AllReturns) -> None: ...
@overload
def sum(self, *, keepdims: Tru, axis: _2_AllAx = None, out: Optional["Floats2d"] = None) -> "Floats2d": ...
@overload
def sum(self, *, keepdims: Fal = False, axis: OneAx, out: Optional[Floats1d] = None) -> Floats1d: ...
@overload
def sum(self, *, keepdims: Fal = False, axis: TwoAx, out = None) -> float: ...
def sum(self, *, keepdims: bool = False, axis: _2_AllAx = None, out: Union[None, "Floats1d", "Floats2d"] = None) -> _2F_ReduceResults: ...
class Ints2d(_Array2d, _Ints):
"""2-dimensional array of ints."""
T: "Ints2d"
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=2, dtype="i")
def __iter__(self) -> Iterator[Ints1d]: ...
@overload
def __getitem__(self, key: _2_KeyScalar) -> int: ...
@overload
def __getitem__(self, key: _2_Key1d) -> Ints1d: ...
@overload
def __getitem__(self, key: _2_Key2d) -> "Ints2d": ...
def __getitem__(self, key: _2_AllKeys) -> _I2_AllReturns: ...
@overload
def __setitem__(self, key: _2_KeyScalar, value: int) -> None: ...
@overload
def __setitem__(self, key: _2_Key1d, value: Ints1d) -> None: ...
@overload
def __setitem__(self, key: _2_Key2d, value: "Ints2d") -> None: ...
def __setitem__(self, key: _2_AllKeys, value: _I2_AllReturns) -> None: ...
@overload
def sum(self, keepdims: Fal = False, axis: int = -1, out: Optional["Ints1d"] = None) -> Ints1d: ...
@overload
def sum(self, keepdims: Tru, axis: int = -1, out: Optional["Ints2d"] = None) -> "Ints2d": ...
def sum(self, keepdims: bool = False, axis: int = -1, out: Optional[Union["Ints1d", "Ints2d"]] = None) -> Union["Ints2d", Ints1d]: ...
class _Array3d(_Array):
"""3-dimensional array of floats"""
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=3)
@property
def ndim(self) -> Literal[3]: ...
@property
def shape(self) -> Tuple[int, int, int]: ...
def __iter__(self) -> Iterator[Array2d]: ...
def astype(self, dtype: DTypes, order: str = ..., casting: str = ..., subok: bool = ..., copy: bool = ...) -> "Array3d": ...
# These is actually a bit too strict: It's legal to say 'array2d + array3d'
# That's kind of bad code though; it's better to write array3d + array2d.
# We could relax this, but let's try the strict version.
def __add__(self: SelfT, other: Union[float, int, Array1d, Array2d, "Array3d"]) -> SelfT: ...
def __sub__(self: SelfT, other: Union[float, int, Array1d, Array2d, "Array3d"]) -> SelfT: ...
def __mul__(self: SelfT, other: Union[float, int, Array1d, Array2d, "Array3d"]) -> SelfT: ...
def __pow__(self: SelfT, other: Union[float, int, Array1d, Array2d, "Array3d"]) -> SelfT: ...
def __matmul__(self: SelfT, other: Union[float, int, Array1d, Array2d, "Array3d"]) -> SelfT: ...
# These are not too strict though: you can't do += with higher dimensional.
def __iadd__(self, other: Union[float, int, Array1d, Array2d, "Array3d"]): ...
def __isub__(self, other: Union[float, int, Array1d, Array2d, "Array3d"]): ...
def __imul__(self, other: Union[float, int, Array1d, Array2d, "Array3d"]): ...
def __ipow__(self, other: Union[float, int, Array1d, Array2d, "Array3d"]): ...
@overload
def argmax(self, keepdims: Fal = False, axis: int = -1, out: Optional[_Array] = None) -> Ints2d: ...
@overload
def argmax(self, keepdims: Tru, axis: int = -1, out: Optional[_Array] = None) -> "Ints3d": ...
def argmax(self, keepdims: bool = False, axis: int = -1, out: Optional[_Array] = None) -> Union[Ints2d, "Ints3d"]: ...
class Floats3d(_Array3d, _Floats):
"""3-dimensional array of floats"""
T: "Floats3d"
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=3, dtype="f")
def __iter__(self) -> Iterator[Floats2d]: ...
@overload
def __getitem__(self, key: _3_KeyScalar) -> float: ...
@overload
def __getitem__(self, key: _3_Key1d) -> Floats1d: ...
@overload
def __getitem__(self, key: _3_Key2d) -> Floats2d: ...
@overload
def __getitem__(self, key: _3_Key3d) -> "Floats3d": ...
def __getitem__(self, key: _3_AllKeys) -> _F3_AllReturns: ...
@overload
def __setitem__(self, key: _3_KeyScalar, value: float) -> None: ...
@overload
def __setitem__(self, key: _3_Key1d, value: Floats1d) -> None: ...
@overload
def __setitem__(self, key: _3_Key2d, value: Floats2d) -> None: ...
@overload
def __setitem__(self, key: _3_Key3d, value: "Floats3d") -> None: ...
def __setitem__(self, key: _3_AllKeys, value: _F3_AllReturns) -> None: ...
@overload
def sum(self, *, keepdims: Tru, axis: _3_AllAx = None, out: Optional["Floats3d"] = None) -> "Floats3d": ...
@overload
def sum(self, *, keepdims: Fal, axis: OneAx, out: Optional[Floats2d] = None) -> Floats2d: ...
@overload
def sum(self, *, keepdims: Fal, axis: TwoAx, out: Optional[Floats1d] = None) -> Floats1d: ...
@overload
def sum(self, *, keepdims: Fal, axis: Optional[ThreeAx], out = None) -> float: ...
def sum(self, *, keepdims: bool = False, axis: _3_AllAx = None, out: Union[None, Floats1d, Floats2d, "Floats3d"] = None) -> _3F_ReduceResults: ...
class Ints3d(_Array3d, _Ints):
"""3-dimensional array of ints."""
T: "Ints3d"
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=3, dtype="i")
def __iter__(self) -> Iterator[Ints2d]: ...
@overload
def __getitem__(self, key: _3_KeyScalar) -> int: ...
@overload
def __getitem__(self, key: _3_Key1d) -> Ints1d: ...
@overload
def __getitem__(self, key: _3_Key2d) -> Ints2d: ...
@overload
def __getitem__(self, key: _3_Key3d) -> "Ints3d": ...
def __getitem__(self, key: _3_AllKeys) -> _I3_AllReturns: ...
@overload
def __setitem__(self, key: _3_KeyScalar, value: int) -> None: ...
@overload
def __setitem__(self, key: _3_Key1d, value: Ints1d) -> None: ...
@overload
def __setitem__(self, key: _3_Key2d, value: Ints2d) -> None: ...
@overload
def __setitem__(self, key: _3_Key3d, value: "Ints3d") -> None: ...
def __setitem__(self, key: _3_AllKeys, value: _I3_AllReturns) -> None: ...
@overload
def sum(self, *, keepdims: Tru, axis: _3_AllAx = None, out: Optional["Ints3d"] = None) -> "Ints3d": ...
@overload
def sum(self, *, keepdims: Fal, axis: OneAx, out: Optional[Ints2d] = None) -> Ints2d: ...
@overload
def sum(self, *, keepdims: Fal, axis: TwoAx, out: Optional[Ints1d] = None) -> Ints1d: ...
@overload
def sum(self, *, keepdims: Fal, axis: Optional[ThreeAx], out = None) -> int: ...
def sum(self, *, keepdims: bool = False, axis: _3_AllAx = None, out: Union[None, Ints1d, Ints2d, "Ints3d"] = None) -> _3I_ReduceResults: ...
class _Array4d(_Array):
"""4-dimensional array."""
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=4)
@property
def ndim(self) -> Literal[4]: ...
@property
def shape(self) -> Tuple[int, int, int, int]: ...
def __iter__(self) -> Iterator[Array3d]: ...
def astype(self, dtype: DTypes, order: str = ..., casting: str = ..., subok: bool = ..., copy: bool = ...) -> "_Array4d": ...
# These is actually a bit too strict: It's legal to say 'array4d + array5d'
# That's kind of bad code though; it's better to write array5d + array4d.
# We could relax this, but let's try the strict version.
def __add__(self: SelfT, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]) -> SelfT: ...
def __sub__(self: SelfT, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]) -> SelfT: ...
def __mul__(self: SelfT, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]) -> SelfT: ...
def __pow__(self: SelfT, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]) -> SelfT: ...
def __matmul__(self: SelfT, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]) -> SelfT: ...
# These are not too strict though: you can't do += with higher dimensional.
def __iadd__(self, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]): ...
def __isub__(self, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]): ...
def __imul__(self, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]): ...
def __ipow__(self, other: Union[float, int, Array1d, Array2d, Array3d, "Array4d"]): ...
class Floats4d(_Array4d, _Floats):
"""4-dimensional array of floats."""
T: "Floats4d"
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=4, dtype="f")
def __iter__(self) -> Iterator[Floats3d]: ...
@overload
def __getitem__(self, key: _4_KeyScalar) -> float: ...
@overload
def __getitem__(self, key: _4_Key1d) -> Floats1d: ...
@overload
def __getitem__(self, key: _4_Key2d) -> Floats2d: ...
@overload
def __getitem__(self, key: _4_Key3d) -> Floats3d: ...
@overload
def __getitem__(self, key: _4_Key4d) -> "Floats4d": ...
def __getitem__(self, key: _4_AllKeys) -> _F4_AllReturns: ...
@overload
def __setitem__(self, key: _4_KeyScalar, value: float) -> None: ...
@overload
def __setitem__(self, key: _4_Key1d, value: Floats1d) -> None: ...
@overload
def __setitem__(self, key: _4_Key2d, value: Floats2d) -> None: ...
@overload
def __setitem__(self, key: _4_Key3d, value: Floats3d) -> None: ...
@overload
def __setitem__(self, key: _4_Key4d, value: "Floats4d") -> None: ...
def __setitem__(self, key: _4_AllKeys, value: _F4_AllReturns) -> None: ...
@overload
def sum(self, *, keepdims: Tru, axis: _4_AllAx = None, out: Optional["Floats4d"] = None) -> "Floats4d": ...
@overload
def sum(self, *, keepdims: Fal = False, axis: OneAx, out: Optional[Floats3d] = None) -> Floats3d: ...
@overload
def sum(self, *, keepdims: Fal = False, axis: TwoAx, out: Optional[Floats2d] = None) -> Floats2d: ...
@overload
def sum(self, *, keepdims: Fal = False, axis: ThreeAx, out: Optional[Floats1d] = None) -> Floats1d: ...
@overload
def sum(self, *, keepdims: Fal = False, axis: Optional[FourAx], out = None) -> float: ...
def sum(self, *, keepdims: bool = False, axis: _4_AllAx = None, out: Union[None, Floats1d, Floats2d, Floats3d, "Floats4d"] = None) -> _4F_ReduceResults: ...
class Ints4d(_Array4d, _Ints):
"""4-dimensional array of ints."""
T: "Ints4d"
@classmethod
def __get_validators__(cls):
"""Runtime validation for pydantic."""
yield lambda v: validate_array(v, ndim=4, dtype="i")
def __iter__(self) -> Iterator[Ints3d]: ...
# def __getitem__(self, key: int) -> Ints3d: ...
@overload
def sum(self, *, keepdims: Tru, axis: _4_AllAx = None, out: Optional["Ints4d"] = None) -> "Ints4d": ...
@overload
def sum(self, *, keepdims: Fal = False, axis: OneAx, out: Optional[Ints3d] = None) -> Ints3d: ...
@overload
def sum(self, *, keepdims: Fal = False, axis: TwoAx, out: Optional[Ints2d] = None) -> Ints2d: ...
@overload
def sum(self, *, keepdims: Fal = False, axis: ThreeAx, out: Optional[Ints1d] = None) -> Ints1d: ...
@overload
def sum(self, *, keepdims: Fal = False, axis: Optional[FourAx] = None, out = None) -> int: ...
def sum(self, *, keepdims: bool = False, axis: _4_AllAx = None, out: Optional[Union[Ints1d, Ints2d, Ints3d, "Ints4d"]] = None) -> _4I_ReduceResults: ...
_DIn = TypeVar("_DIn")
class Decorator(Protocol):
"""Protocol to mark a function as returning its child with identical signature."""
def __call__(self, name: str) -> Callable[[_DIn], _DIn]: ...
# fmt: on
class Generator(Iterator):
"""Custom generator type. Used to annotate function arguments that accept
generators so they can be validated by pydantic (which doesn't support
iterators/iterables otherwise).
"""
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v):
if not hasattr(v, "__iter__") and not hasattr(v, "__next__"):
raise TypeError("not a valid iterator")
return v
@dataclass
class SizedGenerator:
"""A generator that has a __len__ and can repeatedly call the generator
function.
"""
get_items: Callable[[], Generator]
length: int
def __len__(self):
return self.length
def __iter__(self):
yield from self.get_items()
@dataclass
class Padded:
"""A batch of padded sequences, sorted by decreasing length. The data array
is of shape (step, batch, ...). The auxiliary array size_at_t indicates the
length of the batch at each timestep, so you can do data[:, :size_at_t[t]] to
shrink the batch. The lengths array indicates the length of each row b,
and the indices indicates the original ordering.
"""
data: Floats3d
size_at_t: Ints1d
lengths: Ints1d
indices: Ints1d
def copy(self):
return Padded(
self.data.copy(),
self.size_at_t.copy(),
self.lengths.copy(),
self.indices.copy()
)
def __len__(self) -> int:
return self.lengths.shape[0]
def __getitem__(self, index: Union[int, slice, Ints1d]) -> "Padded":
if isinstance(index, int):
# Slice to keep the dimensionality
return Padded(
self.data[:, index : index + 1],
self.lengths[index : index + 1],
self.lengths[index : index + 1],
self.indices[index : index + 1],
)
elif isinstance(index, slice):
return Padded(
self.data[:, index],
self.lengths[index],
self.lengths[index],
self.indices[index],
)
else:
# If we get a sequence of indices, we need to be careful that
# we maintain the length-sorting, while also keeping the mapping
# back to the original order correct.
sorted_index = list(sorted(index))
return Padded(
self.data[sorted_index],
self.size_at_t[sorted_index],
self.lengths[sorted_index],
self.indices[index], # Use original, to maintain order.
)
@dataclass
class Ragged:
"""A batch of concatenated sequences, that vary in the size of their
first dimension. Ragged allows variable-length sequence data to be contiguous
in memory, without padding.
Indexing into Ragged is just like indexing into the *lengths* array, except
it returns a Ragged object with the accompanying sequence data. For instance,
you can write ragged[1:4] to get a Ragged object with sequences 1, 2 and 3.
"""
data: Array2d
lengths: Ints1d
data_shape: Tuple[int, ...]
starts_ends: Optional[Ints1d] = None
def __init__(self, data: _Array, lengths: Ints1d):
self.lengths = lengths
# Frustratingly, the -1 dimension doesn't work with 0 size...
if data.size:
self.data = cast(Array2d, data.reshape((data.shape[0], -1)))
else:
self.data = cast(Array2d, data.reshape((0, 0)))
self.data_shape = (-1,) + data.shape[1:]
@property
def dataXd(self) -> ArrayXd:
if self.data.size:
reshaped = self.data.reshape(self.data_shape)
else:
reshaped = self.data.reshape((self.data.shape[0],) + self.data_shape[1:])
return cast(ArrayXd, reshaped)
def __len__(self) -> int:
return self.lengths.shape[0]
def __getitem__(self, index: Union[int, slice, Array1d]) -> "Ragged":
if isinstance(index, tuple):
raise IndexError("Ragged arrays do not support 2d indexing.")
starts = self._get_starts()
ends = self._get_ends()
if isinstance(index, int):
s = starts[index]
e = ends[index]
return Ragged(self.data[s:e], self.lengths[index : index + 1])
elif isinstance(index, slice):
lengths = self.lengths[index]
if len(lengths) == 0:
return Ragged(self.data[0:0].reshape(self.data_shape), lengths)
start = starts[index][0] if index.start >= 1 else 0
end = ends[index][-1]
return Ragged(self.data[start:end].reshape(self.data_shape), lengths)
else:
# There must be a way to do this "properly" :(. Sigh, hate numpy.
xp = get_array_module(self.data)
data = xp.vstack([self[int(i)].data for i in index])
return Ragged(data.reshape(self.data_shape), self.lengths[index])
def _get_starts_ends(self) -> Ints1d:
if self.starts_ends is None:
xp = get_array_module(self.lengths)
self.starts_ends = xp.empty(self.lengths.size + 1, dtype="i")
self.starts_ends[0] = 0
self.lengths.cumsum(out=self.starts_ends[1:])
return self.starts_ends
def _get_starts(self) -> Ints1d:
return self._get_starts_ends()[:-1]
def _get_ends(self) -> Ints1d:
return self._get_starts_ends()[1:]
_P = TypeVar("_P", bound=Sequence)
@dataclass
class Pairs(Generic[_P]):
"""Dataclass for pairs of sequences that allows indexing into the sequences
while keeping them aligned.
"""
one: _P
two: _P
def __getitem__(self, index) -> "Pairs[_P]":
return Pairs(self.one[index], self.two[index])
def __len__(self) -> int:
return len(self.one)
@dataclass
class ArgsKwargs:
"""A tuple of (args, kwargs) that can be spread into some function f:
f(*args, **kwargs)
"""
args: Tuple[Any, ...]
kwargs: Dict[str, Any]
@classmethod
def from_items(cls, items: Sequence[Tuple[Union[int, str], Any]]) -> "ArgsKwargs":
"""Create an ArgsKwargs object from a sequence of (key, value) tuples,
such as produced by argskwargs.items(). Each key should be either a string
or an integer. Items with int keys are added to the args list, and
items with string keys are added to the kwargs list. The args list is
determined by sequence order, not the value of the integer.
"""
args = []
kwargs = {}
for key, value in items:
if isinstance(key, int):
args.append(value)
else:
kwargs[key] = value
return cls(args=tuple(args), kwargs=kwargs)
def keys(self) -> Iterable[Union[int, str]]:
"""Yield indices from self.args, followed by keys from self.kwargs."""
yield from range(len(self.args))
yield from self.kwargs.keys()
def values(self) -> Iterable[Any]:
"""Yield elements of from self.args, followed by values from self.kwargs."""
yield from self.args
yield from self.kwargs.values()
def items(self) -> Iterable[Tuple[Union[int, str], Any]]:
"""Yield enumerate(self.args), followed by self.kwargs.items()"""
yield from enumerate(self.args)
yield from self.kwargs.items()
@dataclass
class Unserializable:
"""Wrap a value to prevent it from being serialized by msgpack."""
obj: Any
def validate_array(obj, ndim=None, dtype=None):
"""Runtime validator for pydantic to validate array types."""
xp = get_array_module(obj)
if not isinstance(obj, xp.ndarray):
raise TypeError("not a valid numpy or cupy array")
errors = []
if ndim is not None and obj.ndim != ndim:
errors.append(f"wrong array dimensions (expected {ndim}, got {obj.ndim})")
if dtype is not None:
dtype_mapping = {"f": ["float32"], "i": ["int32", "int64", "uint32", "uint64"]}
expected_types = dtype_mapping.get(dtype, [])
if obj.dtype not in expected_types:
expected = "/".join(expected_types)
err = f"wrong array data type (expected {expected}, got {obj.dtype})"
errors.append(err)
if errors:
raise ValueError(", ".join(errors))
return obj