File: iterator.go

package info (click to toggle)
golang-github-gorgonia-tensor 0.9.24-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,696 kB
  • sloc: sh: 18; asm: 18; makefile: 8
file content (716 lines) | stat: -rw-r--r-- 16,359 bytes parent folder | download
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
package tensor

import (
	"runtime"
)

func requiresOrderedIterator(e Engine, t Tensor) bool {
	if t.IsScalar() {
		return false
	}
	if t.RequiresIterator() {
		return true
	}
	switch tt := t.(type) {
	case DenseTensor:
		return !e.WorksWith(tt.DataOrder())
	case SparseTensor:
		return true
	}
	panic("Unreachable")
}

// Iterator is the generic iterator interface.
// It's used to iterate across multi-dimensional slices, no matter the underlying data arrangement
type Iterator interface {
	// Start returns the first index
	Start() (int, error)

	// Next returns the next index. Next is defined as the next value in the coordinates
	// For example: let x be a (5,5) matrix that is row-major. Current index is for the coordinate (3,3).
	// Next() returns the index of (3,4).
	//
	// If there is no underlying data store for (3,4) - say for example, the matrix is a sparse matrix, it return an error.
	// If however, there is an underlying data store for (3,4), but it's not valid (for example, masked tensors), it will not return an error.
	//
	// Second example: let x be a (5,5) matrix that is col-major. Current index is for coordinate (3,3).
	// Next() returns the index of (4,3).
	Next() (int, error)

	// NextValidity is like Next, but returns the validity of the value at the index as well.
	NextValidity() (int, bool, error)

	// NextValid returns the next valid index, as well as a skip count.
	NextValid() (int, int, error)

	// NextInvalid returns the next invalid index, as well as a skip count.
	NextInvalid() (int, int, error)

	// Reset resets the iterator
	Reset()

	// SetReverse tells the iterator to iterate in reverse
	SetReverse()

	// SetForward tells the iterator to iterate forwards
	SetForward()

	// Coord returns the coordinates
	Coord() []int

	// Done returns true when the iterator is done iterating.
	Done() bool

	// Shape returns the shape of the multidimensional tensor it's iterating on.
	Shape() Shape
}

// NewIterator creates a new Iterator from an ap. The type of iterator depends on number of
// aps passed, and whether they are masked or not
func NewIterator(aps ...*AP) Iterator {
	switch len(aps) {
	case 0:
		return nil
	case 1:
		return newFlatIterator(aps[0])
	default:
		return NewMultIterator(aps...)
	}
}

// IteratorFromDense creates a new Iterator from a list of dense tensors
func IteratorFromDense(tts ...DenseTensor) Iterator {
	switch len(tts) {
	case 0:
		return nil
	case 1:
		if mt, ok := tts[0].(MaskedTensor); ok && mt.IsMasked() {
			return FlatMaskedIteratorFromDense(mt)
		}
		return FlatIteratorFromDense(tts[0])
	default:
		return MultIteratorFromDense(tts...)
	}
}

func destroyIterator(it Iterator) {
	switch itt := it.(type) {
	case *MultIterator:
		destroyMultIterator(itt)
	}
}

func iteratorLoadAP(it Iterator, ap *AP) {
	switch itt := it.(type) {
	case *FlatIterator:
		itt.AP = ap
	case *FlatMaskedIterator:
		itt.AP = ap
	case *MultIterator: // Do nothing, TODO: perhaps add something here

	}
}

/* FLAT ITERATOR */

// FlatIterator is an iterator that iterates over Tensors according to the data's layout.
// It utilizes the *AP of a Tensor to determine what the next index is.
// This data structure is similar to Numpy's flatiter, with some standard Go based restrictions of course
// (such as, not allowing negative indices)
type FlatIterator struct {
	*AP

	//state
	track      []int
	nextIndex  int
	lastIndex  int
	size       int
	done       bool
	veclikeDim int  // the dimension of a  vectorlike shape that is not a 1.
	reverse    bool // if true, iterator starts at end of array and runs backwards

	isScalar bool
	isVector bool

	outerFirst bool
}

// newFlatIterator creates a new FlatIterator.
func newFlatIterator(ap *AP) *FlatIterator {
	var dim int
	if ap.IsVectorLike() {
		for d, i := range ap.shape {
			if i != 1 {
				dim = d
				break
			}
		}
	}

	return &FlatIterator{
		AP:         ap,
		track:      make([]int, len(ap.shape)),
		size:       ap.shape.TotalSize(),
		veclikeDim: dim,

		isScalar: ap.IsScalar(),
		isVector: ap.IsVectorLike(),
	}
}

// FlatIteratorFromDense creates a new FlatIterator from a dense tensor
func FlatIteratorFromDense(tt DenseTensor) *FlatIterator {
	return newFlatIterator(tt.Info())
}

// SetReverse initializes iterator to run backwards
func (it *FlatIterator) SetReverse() {
	it.reverse = true
	it.Reset()
	return
}

// SetForward initializes iterator to run forwards
func (it *FlatIterator) SetForward() {
	it.reverse = false
	it.Reset()
	return
}

//Start begins iteration
func (it *FlatIterator) Start() (int, error) {
	it.Reset()
	return it.Next()
}

//Done checks whether iterators are done
func (it *FlatIterator) Done() bool {
	return it.done
}

// Next returns the index of the current coordinate.
func (it *FlatIterator) Next() (int, error) {
	if it.done {
		return -1, noopError{}
	}

	switch {
	case it.isScalar:
		it.done = true
		return 0, nil
	case it.isVector:
		if it.reverse {
			return it.singlePrevious()
		}
		return it.singleNext()
	default:
		if it.reverse {
			return it.ndPrevious()
		}
		if it.outerFirst {
			return it.colMajorNDNext()
		}
		return it.ndNext()
	}
}

// NextValidity returns the index of the current coordinate, and whether or not it's valid. Identical to Next()
func (it *FlatIterator) NextValidity() (int, bool, error) {
	i, err := it.Next()
	return i, true, err
}

// NextValid returns the index of the current coordinate. Identical to Next for FlatIterator
// Also returns the number of increments to get to next element ( 1,  or -1 in reverse case). This is to maintain
// consistency with the masked iterator, for which the step between valid elements can be more than 1
func (it *FlatIterator) NextValid() (int, int, error) {
	if it.done {
		return -1, 1, noopError{}
	}
	switch {
	case it.isScalar:
		it.done = true
		return 0, 0, nil
	case it.isVector:
		if it.reverse {
			a, err := it.singlePrevious()
			return a, -1, err
		}
		a, err := it.singleNext()
		return a, 1, err
	default:
		if it.reverse {
			a, err := it.ndPrevious()
			return a, -1, err
		}

		if it.outerFirst {
			a, err := it.colMajorNDNext()
			return a, 1, err
		}
		a, err := it.ndNext()
		return a, 1, err
	}
}

// NextInvalid returns the index of the current coordinate. Identical to Next for FlatIterator
// also returns the number of increments to get to next invalid element (1 or -1 in reverse case).
// Like NextValid, this method's purpose is to maintain consistency with the masked iterator,
// for which the step between invalid elements can be anywhere from 0 to the  tensor's length
func (it *FlatIterator) NextInvalid() (int, int, error) {
	if it.reverse {
		return -1, -it.lastIndex, noopError{}
	}
	return -1, it.Size() - it.lastIndex, noopError{}
}

func (it *FlatIterator) singleNext() (int, error) {
	it.lastIndex = it.nextIndex
	it.nextIndex++

	var tracked int
	it.track[it.veclikeDim]++
	tracked = it.track[it.veclikeDim]

	if tracked >= it.size {
		it.done = true
	}

	return it.lastIndex, nil
}

func (it *FlatIterator) singlePrevious() (int, error) {
	it.lastIndex = it.nextIndex
	it.nextIndex--

	var tracked int
	it.track[it.veclikeDim]--
	tracked = it.track[it.veclikeDim]

	if tracked < 0 {
		it.done = true
	}
	return it.lastIndex, nil
}

func (it *FlatIterator) ndNext() (int, error) {
	// the reason for this weird looking bits of code is because the SSA compiler doesn't
	// know how to optimize for this bit of code, not keeping things in registers correctly
	// @stuartcarnie optimized this iout to great effect

	v := len(it.shape) - 1
	nextIndex := it.nextIndex
	it.lastIndex = nextIndex

	// the following 3 lines causes the compiler to perform bounds check here,
	// instead of being done in the loop
	coord := it.shape[:v+1]
	track := it.track[:v+1]
	strides := it.strides[:v+1]
	for i := v; i >= 0; i-- {
		track[i]++
		shapeI := coord[i]
		strideI := strides[i]

		if track[i] == shapeI {
			if i == 0 {
				it.done = true
			}
			track[i] = 0
			nextIndex -= (shapeI - 1) * strideI
			continue
		}
		nextIndex += strideI
		break
	}
	it.nextIndex = nextIndex
	return it.lastIndex, nil
}

func (it *FlatIterator) colMajorNDNext() (int, error) {
	// the reason for this weird looking bits of code is because the SSA compiler doesn't
	// know how to optimize for this bit of code, not keeping things in registers correctly
	// @stuartcarnie optimized this iout to great effect

	v := len(it.shape) - 1
	nextIndex := it.nextIndex
	it.lastIndex = nextIndex

	// the following 3 lines causes the compiler to perform bounds check here,
	// instead of being done in the loop
	coord := it.shape[:v+1]
	track := it.track[:v+1]
	strides := it.strides[:v+1]
	for i := 0; i <= v; i++ {
		track[i]++
		shapeI := coord[i]
		strideI := strides[i]

		if track[i] == shapeI {
			if i == v {
				it.done = true
			}
			track[i] = 0

			nextIndex -= (shapeI - 1) * strideI
			continue
		}
		nextIndex += strideI
		break
	}
	it.nextIndex = nextIndex
	return it.lastIndex, nil

}

func (it *FlatIterator) ndPrevious() (int, error) {
	it.lastIndex = it.nextIndex
	for i := len(it.shape) - 1; i >= 0; i-- {
		it.track[i]--
		if it.track[i] < 0 {
			if i == 0 {
				it.done = true
			}
			it.track[i] = it.shape[i] - 1
			it.nextIndex += (it.shape[i] - 1) * it.strides[i]
			continue
		}
		it.nextIndex -= it.strides[i]
		break
	}
	return it.lastIndex, nil
}

// TODO v0.9.0
func (it *FlatIterator) colMajorNDPrevious() (int, error) {
	return 0, nil
}

// Coord returns the next coordinate.
// When Next() is called, the coordinates are updated AFTER the Next() returned.
// See example for more details.
//
// The returned coordinates is mutable. Changing any values in the return value will
// change the state of the iterator
func (it *FlatIterator) Coord() []int { return it.track }

// Slice is a convenience function that augments
func (it *FlatIterator) Slice(sli Slice) (retVal []int, err error) {
	var next int
	var nexts []int
	for next, err = it.Next(); err == nil; next, err = it.Next() {
		nexts = append(nexts, next)
	}
	if _, ok := err.(NoOpError); err != nil && !ok {
		return
	}

	if sli == nil {
		retVal = nexts
		return
	}

	start := sli.Start()
	end := sli.End()
	step := sli.Step()

	// sanity checks
	if err = CheckSlice(sli, len(nexts)); err != nil {
		return
	}

	if step < 0 {
		// reverse the nexts
		for i := len(nexts)/2 - 1; i >= 0; i-- {
			j := len(nexts) - 1 - i
			nexts[i], nexts[j] = nexts[j], nexts[i]
		}
		step = -step
	}

	// cleanup before loop
	if end > len(nexts) {
		end = len(nexts)
	}
	// nexts = nexts[:end]

	for i := start; i < end; i += step {
		retVal = append(retVal, nexts[i])
	}

	err = nil
	return
}

// Reset resets the iterator state.
func (it *FlatIterator) Reset() {
	it.done = false
	if it.reverse {
		for i := range it.track {
			it.track[i] = it.shape[i] - 1
		}

		switch {
		case it.IsScalar():
			it.nextIndex = 0
		case it.isVector:
			it.nextIndex = (it.shape[0] - 1) * it.strides[0]
		// case it.IsRowVec():
		// 	it.nextIndex = (it.shape[1] - 1) * it.strides[1]
		// case it.IsColVec():
		// 	it.nextIndex = (it.shape[0] - 1) * it.strides[0]
		default:
			it.nextIndex = 0
			for i := range it.track {
				it.nextIndex += (it.shape[i] - 1) * it.strides[i]
			}
		}
	} else {
		it.nextIndex = 0
		for i := range it.track {
			it.track[i] = 0
		}
	}
}

// Chan returns a channel of ints. This is useful for iterating multiple Tensors at the same time.
func (it *FlatIterator) Chan() (retVal chan int) {
	retVal = make(chan int)

	go func() {
		for next, err := it.Next(); err == nil; next, err = it.Next() {
			retVal <- next
		}
		close(retVal)
	}()

	return
}

/* FLAT MASKED ITERATOR */

// FlatMaskedIterator is an iterator that iterates over simple masked Tensors.
// It is used when the mask stride is identical to data stride with the exception of trailing zeros,
// in which case the data index is always a perfect integer multiple of the mask index
type FlatMaskedIterator struct {
	*FlatIterator
	mask []bool
}

// FlatMaskedIteratorFromDense creates a new FlatMaskedIterator from dense tensor
func FlatMaskedIteratorFromDense(tt MaskedTensor) *FlatMaskedIterator {
	it := new(FlatMaskedIterator)
	runtime.SetFinalizer(it, destroyIterator)
	it.FlatIterator = FlatIteratorFromDense(tt)
	it.mask = tt.Mask()
	return it
}

func (it *FlatMaskedIterator) NextValidity() (int, bool, error) {
	if len(it.mask) == 0 {
		return it.FlatIterator.NextValidity()
	}

	var i int
	var err error
	if i, err = it.Next(); err == nil {
		return i, !it.mask[i], err
	}
	return -1, false, err
}

// NextValid returns the index of the next valid element,
// as well as the number of increments to get to next element
func (it *FlatMaskedIterator) NextValid() (int, int, error) {
	if len(it.mask) == 0 {
		return it.FlatIterator.NextValid()
	}
	var count int
	var mult = 1
	if it.reverse {
		mult = -1
	}

	for i, err := it.Next(); err == nil; i, err = it.Next() {
		count++
		if !(it.mask[i]) {
			return i, mult * count, err
		}
	}
	return -1, mult * count, noopError{}
}

// NextInvalid returns the index of the next invalid element
// as well as the number of increments to get to next invalid element
func (it *FlatMaskedIterator) NextInvalid() (int, int, error) {
	if it.mask == nil {
		return it.FlatIterator.NextInvalid()
	}
	var count int
	var mult = 1
	if it.reverse {
		mult = -1
	}
	for i, err := it.Next(); err == nil; i, err = it.Next() {
		count++
		if it.mask[i] {
			return i, mult * count, err
		}
	}
	return -1, mult * count, noopError{}
}

// FlatSparseIterator is an iterator that works very much in the same way as flatiterator, except for sparse tensors
type FlatSparseIterator struct {
	*CS

	//state
	nextIndex int
	lastIndex int
	track     []int
	done      bool
	reverse   bool
}

func NewFlatSparseIterator(t *CS) *FlatSparseIterator {
	it := new(FlatSparseIterator)
	it.CS = t
	it.track = BorrowInts(len(t.s))
	return it
}

func (it *FlatSparseIterator) Start() (int, error) {
	it.Reset()
	return it.Next()
}

func (it *FlatSparseIterator) Next() (int, error) {
	if it.done {
		return -1, noopError{}
	}

	// var ok bool
	it.lastIndex, _ = it.at(it.track...)

	// increment the coordinates
	for i := len(it.s) - 1; i >= 0; i-- {
		it.track[i]++
		if it.track[i] == it.s[i] {
			if i == 0 {
				it.done = true
			}
			it.track[i] = 0
			continue
		}
		break
	}

	return it.lastIndex, nil
}

func (it *FlatSparseIterator) NextValidity() (int, bool, error) {
	i, err := it.Next()
	if i == -1 {
		return i, false, err
	}
	return i, true, err
}

func (it *FlatSparseIterator) NextValid() (int, int, error) {
	var i int
	var err error
	for i, err = it.Next(); err == nil && i == -1; i, err = it.Next() {

	}
	return i, -1, err
}

func (it *FlatSparseIterator) NextInvalid() (int, int, error) {
	var i int
	var err error
	for i, err = it.Next(); err == nil && i != -1; i, err = it.Next() {

	}
	return i, -1, err
}

func (it *FlatSparseIterator) Reset() {
	if it.reverse {
		for i := range it.track {
			it.track[i] = it.s[i] - 1
		}

	} else {
		it.nextIndex = 0
		for i := range it.track {
			it.track[i] = 0
		}
	}
	it.done = false
}

func (it *FlatSparseIterator) SetReverse() {
	it.reverse = true
	it.Reset()
}

func (it *FlatSparseIterator) SetForward() {
	it.reverse = false
	it.Reset()
}

func (it *FlatSparseIterator) Coord() []int {
	return it.track
}

func (it *FlatSparseIterator) Done() bool {
	return it.done
}

/* TEMPORARILY REMOVED
// SortedMultiStridePerm takes multiple input strides, and creates a sorted stride permutation.
// It's based very closely on Numpy's PyArray_CreateMultiSortedStridePerm, where a stable insertion sort is used
// to create the permutations.
func SortedMultiStridePerm(dims int, aps []*AP) (retVal []int) {
	retVal = BorrowInts(dims)
	for i := 0; i < dims; i++ {
		retVal[i] = i
	}

	for i := 1; i < dims; i++ {
		ipos := i
		axisi := retVal[i]

		for j := i - 1; j >= 0; j-- {
			var ambig, swap bool
			ambig = true
			axisj := retVal[j]

			for _, ap := range aps {
				if ap.shape[axisi] != 1 && ap.shape[axisj] != 1 {
					if ap.strides[axisi] <= ap.strides[axisj] {
						swap = true
					} else if ambig {
						swap = true
					}
					ambig = false
				}
			}

			if !ambig && swap {
				ipos = j
			} else {
				break
			}

		}
		if ipos != i {
			for j := i; j > ipos; j-- {
				retVal[j] = retVal[j-1]
			}
			retVal[ipos] = axisi
		}
	}
	return
}
*/