-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathlazy_iterator.ts
1190 lines (1067 loc) · 40.8 KB
/
lazy_iterator.ts
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
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
import * as tf from '@tensorflow/tfjs-core';
import * as seedrandom from 'seedrandom';
import {Container} from '../types';
import {deepClone} from '../util/deep_clone';
import {deepMapAndAwaitAll, DeepMapAsyncResult, DeepMapResult, deepZip, zipToList} from '../util/deep_map';
import {GrowingRingBuffer} from '../util/growing_ring_buffer';
import {RingBuffer} from '../util/ring_buffer';
/**
* A nested structure of LazyIterators, used as the input to zip().
*/
export type IteratorContainer = Container<LazyIterator<tf.TensorContainer>>;
// Here we implement a simple asynchronous iterator.
// This lets us avoid using either third-party stream libraries or
// recent TypeScript language support requiring polyfills.
/**
* Create a `LazyIterator` from an array of items.
*/
export function iteratorFromItems<T>(items: T[]): LazyIterator<T> {
return new ArrayIterator(items);
}
/**
* Create a `LazyIterator` of incrementing integers.
*/
export function iteratorFromIncrementing(start: number): LazyIterator<number> {
let i = start;
return iteratorFromFunction(() => ({value: i++, done: false}));
}
/**
* Create a `LazyIterator` from a function.
*
* ```js
* let i = -1;
* const func = () =>
* ++i < 5 ? {value: i, done: false} : {value: null, done: true};
* const iter = tf.data.iteratorFromFunction(func);
* await iter.forEachAsync(e => console.log(e));
* ```
*
* @param func A function that produces data on each call.
*/
export function iteratorFromFunction<T>(
func: () =>
IteratorResult<T>| Promise<IteratorResult<T>>): LazyIterator<T> {
return new FunctionCallIterator(func);
}
/**
* Create a `LazyIterator` by concatenating underlying streams, which are
* themselves provided as a stream.
*
* This can also be thought of as a "stream flatten" operation.
*
* @param baseIterators A stream of streams to be concatenated.
* @param baseErrorHandler An optional function that can intercept `Error`s
* raised during a `next()` call on the base stream. This function can decide
* whether the error should be propagated, whether the error should be
* ignored, or whether the base stream should be terminated.
*/
export function iteratorFromConcatenated<T>(
baseIterators: LazyIterator<LazyIterator<T>>,
baseErrorHandler?: (e: Error) => boolean): LazyIterator<T> {
return new ChainedIterator(baseIterators, baseErrorHandler);
}
/**
* Create a `LazyIterator` by concatenating streams produced by calling a
* stream-generating function a given number of times.
*
* Since a `LazyIterator` is read-once, it cannot be repeated, but this
* function can be used to achieve a similar effect:
*
* LazyIterator.ofConcatenatedFunction(() => new MyIterator(), 6);
*
* @param iteratorFunc: A function that produces a new stream on each call.
* @param count: The number of times to call the function.
* @param baseErrorHandler An optional function that can intercept `Error`s
* raised during a `next()` call on the base stream. This function can decide
* whether the error should be propagated, whether the error should be
* ignored, or whether the base stream should be terminated.
*/
export function iteratorFromConcatenatedFunction<T>(
iteratorFunc: () => IteratorResult<LazyIterator<T>>, count: number,
baseErrorHandler?: (e: Error) => boolean): LazyIterator<T> {
return iteratorFromConcatenated(
iteratorFromFunction(iteratorFunc).take(count), baseErrorHandler);
}
/**
* Create a `LazyIterator` by zipping together an array, dict, or nested
* structure of `LazyIterator`s (and perhaps additional constants).
*
* The underlying streams must provide elements in a consistent order such
* that they correspond.
*
* Typically, the underlying streams should have the same number of
* elements. If they do not, the behavior is determined by the
* `mismatchMode` argument.
*
* The nested structure of the `iterators` argument determines the
* structure of elements in the resulting iterator.
*
* @param iterators: An array or object containing LazyIterators at the
* leaves.
* @param mismatchMode: Determines what to do when one underlying iterator
* is exhausted before the others. `ZipMismatchMode.FAIL` (the default)
* causes an error to be thrown in this case. `ZipMismatchMode.SHORTEST`
* causes the zipped iterator to terminate with the furst underlying
* streams, so elements remaining on the longer streams are ignored.
* `ZipMismatchMode.LONGEST` causes the zipped stream to continue, filling
* in nulls for the exhausted streams, until all streams are exhausted.
*/
export function iteratorFromZipped<O extends tf.TensorContainer>(
iterators: IteratorContainer,
mismatchMode: ZipMismatchMode = ZipMismatchMode.FAIL): LazyIterator<O> {
return new ZipIterator<O>(iterators, mismatchMode);
}
/**
* An asynchronous iterator, providing lazy access to a potentially
* unbounded stream of elements.
*
* Iterator can be obtained from a dataset:
* `const iter = await dataset.iterator();`
*/
export abstract class LazyIterator<T> {
// This class implements AsyncIterator<T>, but we have not yet set the
// TypeScript --downlevelIteration flag to enable that.
abstract summary(): string;
/**
* Returns a `Promise` for the next element in the stream.
*
* When an item can be provided successfully, the return value is
* `{value:T, done:false}`.
*
* Calling next() on a closed stream returns `{value:null, done:true}`.
*/
abstract next(): Promise<IteratorResult<T>>;
/**
* Collect all remaining elements of a bounded stream into an array.
* Obviously this will succeed only for small streams that fit in memory.
* Useful for testing.
*
* @returns A Promise for an array of stream elements, which will resolve
* when the stream is exhausted.
*/
async toArray(): Promise<T[]> {
const result: T[] = [];
let x = await this.next();
while (!x.done) {
result.push(x.value);
x = await this.next();
}
return result;
}
/**
* Collect all elements of this dataset into an array with prefetching 100
* elements. This is useful for testing, because the prefetch changes the
* order in which the Promises are resolved along the processing pipeline.
* This may help expose bugs where results are dependent on the order of
* Promise resolution rather than on the logical order of the stream (i.e.,
* due to hidden mutable state).
*
* @returns A Promise for an array of stream elements, which will resolve
* when the stream is exhausted.
*/
async toArrayForTest(): Promise<T[]> {
const stream = this.prefetch(100);
const result: T[] = [];
let x = await stream.next();
while (!x.done) {
result.push(x.value);
x = await stream.next();
}
return result;
}
/**
* Draw items from the stream until it is exhausted.
*
* This can be useful when the stream has side effects but no output. In
* that case, calling this function guarantees that the stream will be
* fully processed.
*/
async resolveFully(): Promise<void> {
let x = await this.next();
while (!x.done) {
x = await this.next();
}
}
/**
* Draw items from the stream until it is exhausted, or a predicate fails.
*
* This can be useful when the stream has side effects but no output. In
* that case, calling this function guarantees that the stream will be
* fully processed.
*/
async resolveWhile(predicate: (r: T) => boolean): Promise<void> {
let x = await this.next();
let shouldContinue = predicate(x.value);
while ((!x.done) && shouldContinue) {
x = await this.next();
shouldContinue = predicate(x.value);
}
}
/**
* Handles errors thrown on this stream using a provided handler function.
*
* @param handler A function that handles any `Error` thrown during a `next()`
* call and returns true if the stream should continue (dropping the failed
* call) or false if the stream should quietly terminate. If the handler
* itself throws (or rethrows) an `Error`, that will be propagated.
*
* @returns A `LazyIterator` of elements passed through from upstream,
* possibly filtering or terminating on upstream `next()` calls that
* throw an `Error`.
*/
handleErrors(handler: (error: Error) => boolean): LazyIterator<T> {
return new ErrorHandlingLazyIterator(this, handler);
}
// TODO(soergel): Implement reduce() etc.
/**
* Filters this stream according to `predicate`.
*
* @param predicate A function mapping a stream element to a boolean or a
* `Promise` for one.
*
* @returns A `LazyIterator` of elements for which the predicate was true.
*/
filter(predicate: (value: T) => boolean): LazyIterator<T> {
return new FilterIterator(this, predicate);
}
/**
* Maps this stream through a 1-to-1 transform.
*
* @param transform A function mapping a stream element to a transformed
* element.
*
* @returns A `LazyIterator` of transformed elements.
*/
map<O>(transform: (value: T) => O): LazyIterator<O> {
return new MapIterator(this, transform);
}
/**
* Maps this stream through an async 1-to-1 transform.
*
* @param transform A function mapping a stream element to a `Promise` for a
* transformed stream element.
*
* @returns A `LazyIterator` of transformed elements.
*/
mapAsync<O>(transform: (value: T) => Promise<O>): LazyIterator<O> {
return new AsyncMapIterator(this, transform);
}
/**
* Maps this stream through a 1-to-1 transform, forcing serial execution.
*
* @param transform A function mapping a stream element to a transformed
* element.
*
* @returns A `LazyIterator` of transformed elements.
*/
serialMapAsync<O>(transform: (value: T) => Promise<O>): LazyIterator<O> {
return new AsyncMapIterator(this, transform).serial();
}
/**
* Maps this stream through a 1-to-many transform.
*
* @param transform A function mapping a stream element to an array of
* transformed elements.
*
* @returns A `DataStream` of transformed elements.
*/
flatmap<O>(transform: (value: T) => O[]): LazyIterator<O> {
return new FlatmapIterator(this, transform);
}
/**
* Apply a function to every element of the stream.
*
* @param f A function to apply to each stream element.
*/
async forEachAsync(f: (value: T) => void): Promise<void> {
return this.map(f).resolveFully();
}
/**
* Apply a function to every element of the stream, forcing serial execution.
*
* @param f A function to apply to each stream element. Should return 'true'
* to indicate that the stream should continue, or 'false' to cause it to
* terminate.
*/
async serialForEach(f: (value: T) => Promise<boolean>): Promise<void> {
return this.serialMapAsync(f).resolveWhile(x => (x === true));
}
/**
* Groups elements into batches, represented as arrays of elements.
*
* We can think of the elements of this iterator as 'rows' (even if they are
* nested structures). By the same token, consecutive values for a given
* key within the elements form a 'column'. This matches the usual sense of
* 'row' and 'column' when processing tabular data (e.g., parsing a CSV).
*
* Thus, "Row-major" means that the resulting batch is simply a collection of
* rows: `[row1, row2, row3, ...]`. This is contrast to the column-major
* form, which is needed for vectorized computation.
*
* @param batchSize The number of elements desired per batch.
* @param smallLastBatch Whether to emit the final batch when it has fewer
* than batchSize elements. Default true.
* @returns A `LazyIterator` of batches of elements, represented as arrays
* of the original element type.
*/
rowMajorBatch(batchSize: number, smallLastBatch = true): LazyIterator<T[]> {
return new RowMajorBatchIterator(this, batchSize, smallLastBatch);
}
/**
* Groups elements into batches, represented in column-major form.
*
* We can think of the elements of this iterator as 'rows' (even if they are
* nested structures). By the same token, consecutive values for a given
* key within the elements form a 'column'. This matches the usual sense of
* 'row' and 'column' when processing tabular data (e.g., parsing a CSV).
*
* Thus, "column-major" means that the resulting batch is a (potentially
* nested) structure representing the columns. Each column entry, then,
* contains a collection of the values found in that column for a range of
* input elements. This representation allows for vectorized computation, in
* contrast to the row-major form.
*
* The inputs should all have the same nested structure (i.e., of arrays and
* dicts). The result is a single object with the same nested structure,
* where the leaves are arrays collecting the values of the inputs at that
* location (or, optionally, the result of a custom function applied to those
* arrays).
*
* @param batchSize The number of elements desired per batch.
* @param smallLastBatch Whether to emit the final batch when it has fewer
* than batchSize elements. Default true.
* @param zipFn: (optional) A function that expects an array of elements at a
* single node of the object tree, and returns a `DeepMapResult`. The
* `DeepMapResult` either provides a result value for that node (i.e.,
* representing the subtree), or indicates that the node should be processed
* recursively. The default zipFn recurses as far as possible and places
* arrays at the leaves.
* @returns A `LazyIterator` of batches of elements, represented as an object
* with collections at the leaves.
*/
columnMajorBatch(
batchSize: number, smallLastBatch = true,
// tslint:disable-next-line:no-any
zipFn: (xs: any[]) => DeepMapResult = zipToList):
LazyIterator<tf.TensorContainer> {
// First collect the desired number of input elements as a row-major batch.
const rowBatches = this.rowMajorBatch(batchSize, smallLastBatch);
// Now 'rotate' or 'pivot' the data, collecting all values from each column
// in the batch (i.e., for each key within the elements) into an array.
return rowBatches.map(x => deepZip(x, zipFn));
}
/**
* Concatenate this `LazyIterator` with another.
*
* @param iterator A `LazyIterator` to be concatenated onto this one.
* @param baseErrorHandler An optional function that can intercept `Error`s
* raised during a `next()` call on the base stream. This function can
* decide whether the error should be propagated, whether the error should
* be ignored, or whether the base stream should be terminated.
* @returns A `LazyIterator`.
*/
concatenate(
iterator: LazyIterator<T>,
baseErrorHandler?: (e: Error) => boolean): LazyIterator<T> {
return new ChainedIterator(
iteratorFromItems([this, iterator]), baseErrorHandler);
}
/**
* Limits this stream to return at most `count` items.
*
* @param count The maximum number of items to provide from the stream. If
* a negative or undefined value is given, the entire stream is returned
* unaltered.
*/
take(count: number): LazyIterator<T> {
if (count < 0 || count == null) {
return this;
}
return new TakeIterator(this, count);
}
/**
* Skips the first `count` items in this stream.
*
* @param count The number of items to skip. If a negative or undefined
* value is given, the entire stream is returned unaltered.
*/
skip(count: number): LazyIterator<T> {
if (count < 0 || count == null) {
return this;
}
return new SkipIterator(this, count);
}
/**
* Prefetch the first `bufferSize` items in this stream.
*
* Note this prefetches Promises, but makes no guarantees about when those
* Promises resolve.
*
* @param bufferSize: An integer specifying the number of elements to be
* prefetched.
*/
prefetch(bufferSize: number): LazyIterator<T> {
return new PrefetchIterator(this, bufferSize);
}
// TODO(soergel): deep sharded shuffle, where supported
/**
* Randomly shuffles the elements of this stream.
*
* @param bufferSize: An integer specifying the number of elements from
* this stream from which the new stream will sample.
* @param seed: (Optional.) An integer specifying the random seed that
* will be used to create the distribution.
*/
shuffle(windowSize: number, seed?: string): LazyIterator<T> {
return new ShuffleIterator(this, windowSize, seed);
}
/**
* Force an iterator to execute serially: each next() call will await the
* prior one, so that they cannot execute concurrently.
*/
serial(): LazyIterator<T> {
return new SerialIterator(this);
}
}
// ============================================================================
// The following private classes serve to implement the chainable methods
// on LazyIterator. Unfortunately they can't be placed in separate files,
// due to resulting trouble with circular imports.
// ============================================================================
// Iterators that just extend LazyIterator directly
// ============================================================================
class ArrayIterator<T> extends LazyIterator<T> {
private trav = 0;
constructor(protected items: T[]) {
super();
}
summary() {
return `Array of ${this.items.length} items`;
}
async next(): Promise<IteratorResult<T>> {
if (this.trav >= this.items.length) {
return {value: null, done: true};
}
const item = this.items[this.trav];
this.trav++;
return {value: deepClone(item), done: false};
}
}
class FunctionCallIterator<T> extends LazyIterator<T> {
constructor(
protected nextFn: () => IteratorResult<T>| Promise<IteratorResult<T>>) {
super();
}
summary() {
return `Function call`;
}
async next(): Promise<IteratorResult<T>> {
try {
return this.nextFn();
} catch (e) {
// Modify the error message but leave the stack trace intact
e.message =
`Error thrown while iterating through a dataset: ${e.message}`;
throw e;
}
}
}
class SerialIterator<T> extends LazyIterator<T> {
// Strict Promise execution order:
// a next() call may not even begin until the previous one completes.
private lastRead: Promise<IteratorResult<T>>;
constructor(protected upstream: LazyIterator<T>) {
super();
this.lastRead = Promise.resolve({value: null, done: false});
}
summary() {
return `${this.upstream.summary()} -> Serial`;
}
async next(): Promise<IteratorResult<T>> {
// This sets this.lastRead to a new Promise right away, as opposed to
// saying `await this.lastRead; this.lastRead = this.serialNext();` which
// would not work because this.nextRead would be updated only after the
// promise resolves.
this.lastRead = this.lastRead.then(() => this.serialNext());
return this.lastRead;
}
private async serialNext(): Promise<IteratorResult<T>> {
return this.upstream.next();
}
}
class SkipIterator<T> extends LazyIterator<T> {
// Strict Promise execution order:
// a next() call may not even begin until the previous one completes.
private lastRead: Promise<IteratorResult<T>>;
// Local state that should not be clobbered by out-of-order execution.
count = 0;
constructor(protected upstream: LazyIterator<T>, protected maxCount: number) {
super();
this.lastRead = Promise.resolve({value: null, done: false});
}
summary() {
return `${this.upstream.summary()} -> Skip`;
}
async next(): Promise<IteratorResult<T>> {
// This sets this.lastRead to a new Promise right away, as opposed to
// saying `await this.lastRead; this.lastRead = this.serialNext();` which
// would not work because this.nextRead would be updated only after the
// promise resolves.
this.lastRead = this.lastRead.then(() => this.serialNext());
return this.lastRead;
}
private async serialNext(): Promise<IteratorResult<T>> {
// TODO(soergel): consider tradeoffs of reading in parallel, eg.
// collecting next() promises in an Array and then waiting for
// Promise.all() of those. Benefit: pseudo-parallel execution. Drawback:
// maybe delayed GC.
while (this.count++ < this.maxCount) {
const skipped = await this.upstream.next();
// short-circuit if upstream is already empty
if (skipped.done) {
return skipped;
}
tf.dispose(skipped.value as {});
}
return this.upstream.next();
}
}
class TakeIterator<T> extends LazyIterator<T> {
count = 0;
constructor(protected upstream: LazyIterator<T>, protected maxCount: number) {
super();
}
summary() {
return `${this.upstream.summary()} -> Take`;
}
async next(): Promise<IteratorResult<T>> {
if (this.count++ >= this.maxCount) {
return {value: null, done: true};
}
return this.upstream.next();
}
}
// Note this batch just groups items into row-wise element arrays.
// Rotating these to a column-wise representation happens only at the dataset
// level.
class RowMajorBatchIterator<T> extends LazyIterator<T[]> {
// Strict Promise execution order:
// a next() call may not even begin until the previous one completes.
private lastRead: Promise<IteratorResult<T[]>>;
constructor(
protected upstream: LazyIterator<T>, protected batchSize: number,
protected enableSmallLastBatch = true) {
super();
this.lastRead = Promise.resolve({value: null, done: false});
}
summary() {
return `${this.upstream.summary()} -> RowMajorBatch`;
}
async next(): Promise<IteratorResult<T[]>> {
// This sets this.lastRead to a new Promise right away, as opposed to
// saying `await this.lastRead; this.lastRead = this.serialNext();` which
// would not work because this.nextRead would be updated only after the
// promise resolves.
this.lastRead = this.lastRead.then(() => this.serialNext());
return this.lastRead;
}
private async serialNext(): Promise<IteratorResult<T[]>> {
const batch: T[] = [];
while (batch.length < this.batchSize) {
const item = await this.upstream.next();
if (item.done) {
if (this.enableSmallLastBatch && batch.length > 0) {
return {value: batch, done: false};
}
return {value: null, done: true};
}
batch.push(item.value);
}
return {value: batch, done: false};
}
}
class FilterIterator<T> extends LazyIterator<T> {
// Strict Promise execution order:
// a next() call may not even begin until the previous one completes.
private lastRead: Promise<IteratorResult<T>>;
constructor(
protected upstream: LazyIterator<T>,
protected predicate: (value: T) => boolean) {
super();
this.lastRead = Promise.resolve({value: null, done: false});
}
summary() {
return `${this.upstream.summary()} -> Filter`;
}
async next(): Promise<IteratorResult<T>> {
// This sets this.lastRead to a new Promise right away, as opposed to
// saying `await this.lastRead; this.lastRead = this.serialNext();` which
// would not work because this.nextRead would be updated only after the
// promise resolves.
this.lastRead = this.lastRead.then(() => this.serialNext());
return this.lastRead;
}
private async serialNext(): Promise<IteratorResult<T>> {
while (true) {
const item = await this.upstream.next();
if (item.done || this.predicate(item.value)) {
return item;
}
tf.dispose(item.value as {});
}
}
}
class MapIterator<I, O> extends LazyIterator<O> {
constructor(
protected upstream: LazyIterator<I>,
protected transform: (value: I) => O) {
super();
}
summary() {
return `${this.upstream.summary()} -> Map`;
}
async next(): Promise<IteratorResult<O>> {
const item = await this.upstream.next();
if (item.done) {
return {value: null, done: true};
}
const inputTensors = tf.tensor_util.getTensorsInContainer(item.value as {});
// Careful: the transform may mutate the item in place.
// That's why we have to remember the input Tensors above, and then
// below dispose only those that were not passed through to the output.
// Note too that the transform function is responsible for tidying
// any intermediate Tensors. Here we are concerned only about the
// inputs.
const mapped = this.transform(item.value);
const outputTensors = tf.tensor_util.getTensorsInContainer(mapped as {});
// TODO(soergel) faster intersection
// TODO(soergel) move to tf.disposeExcept(in, out)?
for (const t of inputTensors) {
if (!tf.tensor_util.isTensorInList(t, outputTensors)) {
t.dispose();
}
}
return {value: mapped, done: false};
}
}
class ErrorHandlingLazyIterator<T> extends LazyIterator<T> {
count = 0;
constructor(
protected upstream: LazyIterator<T>,
protected handler: (error: Error) => boolean) {
super();
this.lastRead = Promise.resolve({value: null, done: false});
}
summary() {
return `${this.upstream.summary()} -> handleErrors`;
}
// Strict Promise execution order:
// a next() call may not even begin until the previous one completes.
private lastRead: Promise<IteratorResult<T>>;
async next(): Promise<IteratorResult<T>> {
// This sets this.lastRead to a new Promise right away, as opposed to
// saying `await this.lastRead; this.lastRead = this.serialNext();` which
// would not work because this.nextRead would be updated only after the
// promise resolves.
this.lastRead = this.lastRead.then(() => this.serialNext());
return this.lastRead;
}
async serialNext(): Promise<IteratorResult<T>> {
while (true) {
try {
return await this.upstream.next();
} catch (e) {
if (!this.handler(e)) {
return {value: null, done: true};
}
// If the handler returns true, loop and fetch the next upstream item.
// If the upstream iterator throws an endless stream of errors, and if
// the handler says to ignore them, then we loop forever here. That is
// the correct behavior-- it's up to the handler to decide when to stop.
}
}
}
}
class AsyncMapIterator<I, O> extends LazyIterator<O> {
constructor(
protected upstream: LazyIterator<I>,
protected transform: (value: I) => Promise<O>) {
super();
}
summary() {
return `${this.upstream.summary()} -> AsyncMap`;
}
async next(): Promise<IteratorResult<O>> {
const item = await this.upstream.next();
if (item.done) {
return {value: null, done: true};
}
const inputTensors = tf.tensor_util.getTensorsInContainer(item.value as {});
// Careful: the transform may mutate the item in place.
// That's why we have to remember the input Tensors above, and then
// below dispose only those that were not passed through to the output.
// Note too that the transform function is responsible for tidying
// any intermediate Tensors. Here we are concerned only about the
// inputs.
const mapped = await this.transform(item.value);
const outputTensors = tf.tensor_util.getTensorsInContainer(mapped as {});
// TODO(soergel) faster intersection
// TODO(soergel) move to tf.disposeExcept(in, out)?
for (const t of inputTensors) {
if (!tf.tensor_util.isTensorInList(t, outputTensors)) {
t.dispose();
}
}
return {value: mapped, done: false};
}
}
// Iterators that maintain a queue of pending items
// ============================================================================
/**
* A base class for transforming streams that operate by maintaining an
* output queue of elements that are ready to return via next(). This is
* commonly required when the transformation is 1-to-many: A call to next()
* may trigger a call to the underlying stream, which will produce many
* mapped elements of this stream-- of which we need to return only one, so
* we have to queue the rest.
*/
export abstract class OneToManyIterator<T> extends LazyIterator<T> {
// Strict Promise execution order:
// a next() call may not even begin until the previous one completes.
private lastRead: Promise<IteratorResult<T>>;
// Local state that should not be clobbered by out-of-order execution.
protected outputQueue: RingBuffer<T>;
constructor() {
super();
this.outputQueue = new GrowingRingBuffer<T>();
this.lastRead = Promise.resolve({value: null, done: false});
}
async next(): Promise<IteratorResult<T>> {
// This sets this.lastRead to a new Promise right away, as opposed to
// saying `await this.lastRead; this.lastRead = this.serialNext();` which
// would not work because this.nextRead would be updated only after the
// promise resolves.
this.lastRead = this.lastRead.then(() => this.serialNext());
return this.lastRead;
}
/**
* Read one or more chunks from upstream and process them, possibly
* reading or writing a carryover, and adding processed items to the
* output queue. Note it's possible that no items are added to the queue
* on a given pump() call, even if the upstream stream is not closed
* (e.g., because items are filtered).
*
* @return `true` if any action was taken, i.e. fetching items from the
* upstream source OR adding items to the output queue. `false` if the
* upstream source is exhausted AND nothing was added to the queue
* (i.e., any remaining carryover).
*/
protected abstract pump(): Promise<boolean>;
async serialNext(): Promise<IteratorResult<T>> {
// Fetch so that the queue contains at least one item if possible.
// If the upstream source is exhausted, AND there are no items left in
// the output queue, then this stream is also exhausted.
while (this.outputQueue.length() === 0) {
// TODO(soergel): consider parallel reads.
if (!await this.pump()) {
return {value: null, done: true};
}
}
return {value: this.outputQueue.shift(), done: false};
}
}
class FlatmapIterator<I, O> extends OneToManyIterator<O> {
constructor(
protected upstream: LazyIterator<I>,
protected transform: (value: I) => O[]) {
super();
}
summary() {
return `${this.upstream.summary()} -> Flatmap`;
}
async pump(): Promise<boolean> {
const item = await this.upstream.next();
if (item.done) {
return false;
}
const inputTensors = tf.tensor_util.getTensorsInContainer(item.value as {});
// Careful: the transform may mutate the item in place.
// that's why we have to remember the input Tensors above, and then
// below dispose only those that were not passed through to the output.
// Note too that the transform function is responsible for tidying any
// intermediate Tensors. Here we are concerned only about the inputs.
const mappedArray = this.transform(item.value);
const outputTensors =
tf.tensor_util.getTensorsInContainer(mappedArray as {});
this.outputQueue.pushAll(mappedArray);
// TODO(soergel) faster intersection, and deduplicate outputTensors
// TODO(soergel) move to tf.disposeExcept(in, out)?
for (const t of inputTensors) {
if (!tf.tensor_util.isTensorInList(t, outputTensors)) {
t.dispose();
}
}
return true;
}
}
/**
* Provides a `LazyIterator` that concatenates a stream of underlying
* streams.
*
* Doing this in a concurrency-safe way requires some trickery. In
* particular, we want this stream to return the elements from the
* underlying streams in the correct order according to when next() was
* called, even if the resulting Promises resolve in a different order.
*/
export class ChainedIterator<T> extends LazyIterator<T> {
// Strict Promise execution order:
// a next() call may not even begin until the previous one completes.
private lastRead: Promise<IteratorResult<T>> = null;
// Local state that should not be clobbered by out-of-order execution.
private iterator: LazyIterator<T> = null;
private moreIterators: LazyIterator<LazyIterator<T>>;
constructor(
iterators: LazyIterator<LazyIterator<T>>,
private readonly baseErrorHandler?: (e: Error) => boolean) {
super();
this.moreIterators = iterators;
}
summary() {
const upstreamSummaries = 'TODO: fill in upstream of chained summaries';
return `${upstreamSummaries} -> Chained`;
}
async next(): Promise<IteratorResult<T>> {
this.lastRead = this.readFromChain(this.lastRead);
return this.lastRead;
}
private async readFromChain(lastRead: Promise<IteratorResult<T>>):
Promise<IteratorResult<T>> {
// Must await on the previous read since the previous read may have advanced
// the stream of streams, from which we need to read.
// This is unfortunate since we can't parallelize reads. Which means
// prefetching of chained streams is a no-op.
// One solution is to prefetch immediately upstream of this.
await lastRead;
if (this.iterator == null) {
const iteratorResult = await this.moreIterators.next();
if (iteratorResult.done) {
// No more streams to stream from.
return {value: null, done: true};
}
this.iterator = iteratorResult.value;
if (this.baseErrorHandler != null) {
this.iterator = this.iterator.handleErrors(this.baseErrorHandler);
}
}
const itemResult = await this.iterator.next();
if (itemResult.done) {
this.iterator = null;
return this.readFromChain(lastRead);
}
return itemResult;
}
}
export enum ZipMismatchMode {
FAIL, // require zipped streams to have the same length
SHORTEST, // terminate zip when the first stream is exhausted
LONGEST // use nulls for exhausted streams; use up the longest stream.
}
/**
* Provides a `LazyIterator` that zips together an array, dict, or nested
* structure of `LazyIterator`s (and perhaps additional constants).
*
* The underlying streams must provide elements in a consistent order such
* that they correspond.
*
* Typically, the underlying streams should have the same number of
* elements. If they do not, the behavior is determined by the
* `mismatchMode` argument.
*
* The nested structure of the `iterators` argument determines the
* structure of elements in the resulting iterator.
*
* Doing this in a concurrency-safe way requires some trickery. In