This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
/
ShapedArray.swift
998 lines (897 loc) · 38.2 KB
/
ShapedArray.swift
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
// Copyright 2019 The TensorFlow Authors. 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 Swift
//===------------------------------------------------------------------------------------------===//
// TensorBuffer
//===------------------------------------------------------------------------------------------===//
/// `TensorBuffer` is the internal storage of `ShapedArray`. This buffer has two modes of storage:
/// `native` and `tensorFlow`. In `native` mode, the buffer object stores a pointer to contiguous
/// scalars; in `tensorFlow` mode, the buffer object stores a `TF_Tensor*` and bridges to
/// TensorFlow. In either mode, the buffer object owns the memory and will deallocate it on
/// `deinit`.
@usableFromInline
internal class TensorBuffer<Scalar> {
/// Cached element count of the underlying buffer.
let count: Int
init(count: Int) { self.count = count }
func withUnsafeBufferPointer<R>(
_ body: (UnsafeBufferPointer<Scalar>) throws -> R
) rethrows -> R {
fatalError("withUnsafeBufferPointer unimplemented because TensorBuffer is abstract")
}
func withUnsafeMutableBufferPointer<R>(
_ body: (inout UnsafeMutableBufferPointer<Scalar>) throws -> R
) rethrows -> R {
fatalError("withUnsafeMutableBufferPointer unimplemented because TensorBuffer is abstract")
}
}
// TensorBuffer backed by a native swift array.
internal class ArrayTensorBuffer<Scalar>: TensorBuffer<Scalar> {
var array: [Scalar]
init(_ array: __owned [Scalar]) {
self.array = array
super.init(count: array.count)
}
override func withUnsafeBufferPointer<R>(
_ body: (UnsafeBufferPointer<Scalar>) throws -> R
) rethrows -> R {
return try array.withUnsafeBufferPointer(body)
}
override func withUnsafeMutableBufferPointer<R>(
_ body: (inout UnsafeMutableBufferPointer<Scalar>) throws -> R
) rethrows -> R {
return try array.withUnsafeMutableBufferPointer(body)
}
}
//===------------------------------------------------------------------------------------------===//
// ShapedArrayProtocol: The protocol unifying ShapedArray and ShapedArraySlice.
//===------------------------------------------------------------------------------------------===//
public protocol _ShapedArrayProtocol: RandomAccessCollection, MutableCollection {
associatedtype Scalar
/// The number of dimensions of the array.
var rank: Int { get }
/// The shape of the array.
var shape: [Int] { get }
/// The total number of scalars in the array.
var scalarCount: Int { get }
/// Creates an array with the specified shape and contiguous scalars in row-major order.
/// - Precondition: The number of scalars must equal the product of the dimensions of the shape.
init(shape: [Int], scalars: [Scalar])
/// Creates an array with the specified shape and sequence of scalars in row-major order.
/// - Precondition: The number of scalars must equal the product of the dimensions of the shape.
init<S: Sequence>(shape: [Int], scalars: S) where S.Element == Scalar
/// Calls a closure with a pointer to the array’s contiguous storage.
/// - Parameter body: A closure with an `UnsafeBufferPointer` parameter that points to the
/// contiguous storage for the array. If no such storage exists, it is created. If body has a
/// return value, that value is also used as the return value for the
/// `withUnsafeBufferPointer(_:)` method. The pointer argument is valid only for the duration
/// of the method's execution.
func withUnsafeBufferPointer<R>(_ body: (UnsafeBufferPointer<Scalar>) throws -> R) rethrows -> R
/// Calls the given closure with a pointer to the array’s mutable contiguous storage.
/// - Parameter body: A closure with an `UnsafeMutableBufferPointer` parameter that points to
/// the contiguous storage for the array. If no such storage exists, it is created. If body
/// has a return value, that value is also used as the return value for the
/// `withUnsafeMutableBufferPointer(_:)` method. The pointer argument is valid only for the
/// duration of the method's execution.
mutating func withUnsafeMutableBufferPointer<R>(
_ body: (inout UnsafeMutableBufferPointer<Scalar>) throws -> R
) rethrows -> R
}
extension _ShapedArrayProtocol {
/// The scalars of the array in row-major order.
public var scalars: [Scalar] {
get {
return withUnsafeBufferPointer(Array.init)
}
set {
precondition(newValue.count == scalarCount, "Scalar count mismatch.")
withUnsafeMutableBufferPointer { pointer in
pointer.baseAddress!.initialize(from: newValue, count: newValue.count)
}
}
}
/// Returns `true` if the array has rank 0.
public var isScalar: Bool {
return rank == 0
}
/// Returns the single scalar element if the array has rank 0 and `nil` otherwise.
public var scalar: Scalar? {
get {
guard rank == 0 else { return nil }
return scalars.first
}
set {
precondition(isScalar, "Array does not have shape [].")
guard let newValue = newValue else {
preconditionFailure("New scalar value cannot be nil.")
}
scalars[0] = newValue
}
}
}
extension _ShapedArrayProtocol where Scalar: Equatable {
public static func == <Other>(lhs: Self, rhs: Other) -> Bool
where Other: _ShapedArrayProtocol, Scalar == Other.Scalar {
return lhs.shape == rhs.shape && lhs.scalars.elementsEqual(rhs.scalars)
}
}
extension _ShapedArrayProtocol {
/// Returns the number of element arrays in an array (equivalent to the first dimension).
/// - Note: `count` is distinct from `scalarCount`, which represents the
/// total number of scalars.
public var count: Int {
return shape.first ?? 0
}
}
extension _ShapedArrayProtocol {
/// Returns the scalar count for an element of the array.
var scalarCountPerElement: Int {
return shape.isEmpty ? 0 : shape.dropFirst().reduce(1, *)
}
/// Returns the scalar index corresponding to an index in the leading dimension of the array.
func scalarIndex(fromIndex index: Int) -> Int {
return scalarCountPerElement * index
}
/// Returns the range of scalars corresponding to a range in the leading dimension of the array.
func scalarSubrange(from arraySubrange: Range<Int>) -> Range<Int> {
return scalarIndex(
fromIndex: arraySubrange.lowerBound)..<scalarIndex(fromIndex: arraySubrange.upperBound)
}
}
extension String {
/// Returns a string of the specified length, padded with whitespace to the left.
fileprivate func leftPadded(toLength length: Int) -> String {
return repeatElement(" ", count: max(0, length - count)) + self
}
}
// Common public protocol implementations.
extension _ShapedArrayProtocol
where Element: _ShapedArrayProtocol, Element == Element.Element {
/// Returns the whitespace separator between elements, given the current indent level.
fileprivate func separator(indentLevel: Int) -> String {
if rank == 1 {
return ", "
}
return String(repeating: "\n", count: rank - 1)
+ String(repeating: " ", count: indentLevel + 1)
}
/// A textual representation of the 1-D shaped array, starting at the given indent level.
/// Returns a summarized description if `summarizing` is true and the element count exceeds
/// twice the `edgeElementCount`.
///
/// - Parameters:
/// - indentLevel: The indentation level.
/// - edgeElementCount: The maximum number of elements to print before and after summarization
/// via ellipses (`...`).
/// - maxScalarLength: The length of the longest scalar description in the entire original
/// array-to-print.
/// - maxScalarCountPerLine: The maximum number of scalars to print per line, used when
/// printing 1-D vectors.
/// - summarizing: If true, summarize description if element count exceeds twice
/// `edgeElementCount`.
fileprivate func vectorDescription(
indentLevel: Int,
edgeElementCount: Int,
maxScalarLength: Int,
maxScalarCountPerLine: Int,
summarizing: Bool
) -> String {
// Get scalar descriptions.
func scalarDescription(_ element: Element) -> String {
let description = String(describing: element)
return description.leftPadded(toLength: maxScalarLength)
}
var scalarDescriptions: [String] = []
if summarizing && count > 2 * edgeElementCount {
scalarDescriptions += prefix(edgeElementCount).map(scalarDescription)
scalarDescriptions += ["..."]
scalarDescriptions += suffix(edgeElementCount).map(scalarDescription)
} else {
scalarDescriptions += map(scalarDescription)
}
// Combine scalar descriptions into lines, based on the scalar count per line.
let lines = stride(
from: scalarDescriptions.startIndex,
to: scalarDescriptions.endIndex,
by: maxScalarCountPerLine
).map { i -> ArraySlice<String> in
let upperBound = Swift.min(
i.advanced(by: maxScalarCountPerLine),
scalarDescriptions.count)
return scalarDescriptions[i..<upperBound]
}
// Return lines joined with separators.
let lineSeparator = ",\n" + String(repeating: " ", count: indentLevel + 1)
return lines.enumerated().reduce(into: "[") { result, entry in
let (i, line) = entry
result += line.joined(separator: ", ")
result += i != lines.count - 1 ? lineSeparator : ""
} + "]"
}
/// A textual representation of the shaped array, starting at the given indent level. Returns a
/// summarized description if `summarizing` is true and the element count exceeds twice the
/// `edgeElementCount`.
///
/// - Parameters:
/// - indentLevel: The indentation level.
/// - edgeElementCount: The maximum number of elements to print before and after summarization
/// via ellipses (`...`).
/// - maxScalarLength: The length of the longest scalar description in the entire original
/// array-to-print.
/// - maxScalarCountPerLine: The maximum number of scalars to print per line, used when
/// printing 1-D vectors.
/// - summarizing: If true, summarizing description if element count exceeds twice
/// `edgeElementCount`.
fileprivate func description(
indentLevel: Int,
edgeElementCount: Int,
maxScalarLength: Int,
maxScalarCountPerLine: Int,
summarizing: Bool
) -> String {
// Handle scalars.
if let scalar = scalar {
return String(describing: scalar)
}
// Handle vectors, which have special line-width-sensitive logic.
if rank == 1 {
return vectorDescription(
indentLevel: indentLevel,
edgeElementCount: edgeElementCount,
maxScalarLength: maxScalarLength,
maxScalarCountPerLine: maxScalarCountPerLine,
summarizing: summarizing)
}
// Handle higher-rank tensors.
func elementDescription(_ element: Element) -> String {
return element.description(
indentLevel: indentLevel + 1,
edgeElementCount: edgeElementCount,
maxScalarLength: maxScalarLength,
maxScalarCountPerLine: maxScalarCountPerLine,
summarizing: summarizing)
}
var elementDescriptions: [String] = []
if summarizing && count > 2 * edgeElementCount {
elementDescriptions += prefix(edgeElementCount).map(elementDescription)
elementDescriptions += ["..."]
elementDescriptions += suffix(edgeElementCount).map(elementDescription)
} else {
elementDescriptions += map(elementDescription)
}
// Return lines joined with separators.
let lineSeparator =
"," + String(repeating: "\n", count: rank - 1)
+ String(repeating: " ", count: indentLevel + 1)
return elementDescriptions.enumerated().reduce(into: "[") { result, entry in
let (i, elementDescription) = entry
result += elementDescription
result += i != elementDescriptions.count - 1 ? lineSeparator : ""
} + "]"
}
}
extension _ShapedArrayProtocol
where Element: _ShapedArrayProtocol, Element == Element.Element {
/// A textual representation of the shaped array. Returns a summarized description if
/// `summarizing` is true and the element count exceeds twice the `edgeElementCount`.
///
/// - Parameters:
/// - lineWidth: The max line width for printing. Used to determine number of scalars to print
/// per line.
/// - edgeElementCount: The maximum number of elements to print before and after summarization
/// via ellipses (`...`).
/// - summarizing: If true, summarizing description if element count exceeds twice
/// `edgeElementCount`.
public func description(
lineWidth: Int = 80,
edgeElementCount: Int = 3,
summarizing: Bool = false
) -> String {
// Compute the number of scalars to print per line.
let maxScalarLength = scalars.lazy.map { String(describing: $0).count }.max() ?? 3
let maxScalarCountPerLine = Swift.max(1, lineWidth / maxScalarLength)
return description(
indentLevel: 0,
edgeElementCount: edgeElementCount,
maxScalarLength: maxScalarLength,
maxScalarCountPerLine: maxScalarCountPerLine,
summarizing: summarizing)
}
/// A full, non-pretty-printed textual representation of the shaped array, showing all scalars.
public var fullDescription: String {
if let scalar = scalar {
return String(describing: scalar)
}
return "[\( map({"\($0.fullDescription)"}).joined(separator: ", ") )]"
}
}
//===------------------------------------------------------------------------------------------===//
// ShapedArray
//===------------------------------------------------------------------------------------------===//
/// `ShapedArray` is a multi-dimensional array. It has a shape, which has type `[Int]` and defines
/// the array dimensions, and uses a `TensorBuffer` internally as storage.
@frozen
public struct ShapedArray<Scalar>: _ShapedArrayProtocol {
/// Contiguous memory storing scalars.
internal var buffer: TensorBuffer<Scalar>
/// The dimensions of the array.
public private(set) var shape: [Int]
/// Creates a `ShapedArray` from a `TensorBuffer` and a shape.
internal init(buffer: __owned TensorBuffer<Scalar>, shape: __owned [Int]) {
precondition(
buffer.count == shape.reduce(1, *),
"The scalar count of the buffer does not match the shape.")
self.buffer = buffer
self.shape = shape
debugLog("Done initializing ShapedArray from TensorBuffer.")
}
}
extension ShapedArray {
fileprivate mutating func ensureUniquelyReferenced() {
if isKnownUniquelyReferenced(&buffer) { return }
let oldBuffer = buffer
debugLog("Unique reference check")
buffer = oldBuffer.withUnsafeBufferPointer { oldBufferPointer in
ArrayTensorBuffer<Scalar>([Scalar](oldBufferPointer))
}
}
}
extension ShapedArray {
/// The number of dimensions of the array.
public var rank: Int {
return shape.count
}
/// The total number of scalars in the array.
public var scalarCount: Int {
return buffer.count
}
/// Creates a `ShapedArray` with the same shape and scalars as the specified instance.
public init(_ other: ShapedArray) {
debugLog("Initializing from another ShapedArray.")
self.init(buffer: other.buffer, shape: other.shape)
}
/// Creates a `ShapedArray` with the specified shape and contiguous scalars in row-major order.
/// - Precondition: The number of scalars must equal the product of the dimensions of the shape.
public init(shape: __owned [Int], scalars: __owned [Scalar]) {
precondition(shape.reduce(1, *) == scalars.count, "Scalar count mismatch.")
let buffer = ArrayTensorBuffer<Scalar>(scalars)
self.init(buffer: buffer, shape: shape)
}
/// Creates a `ShapedArray` with the specified shape and sequence of scalars in row-major order.
/// - Precondition: The number of scalars must equal the product of the dimensions of the shape.
public init<S: Sequence>(shape: __owned [Int], scalars: __shared S) where S.Element == Scalar {
let scalarCount = shape.reduce(1, *)
let buffer = ArrayTensorBuffer<Scalar>([Scalar](scalars))
precondition(
buffer.count == scalarCount,
"The sequence has fewer elements than needed by the shape.")
self.init(buffer: buffer, shape: shape)
}
/// Creates a `ShapedArray` from a scalar value.
public init(_ scalar: __owned Scalar) {
self.init(buffer: ArrayTensorBuffer([scalar]), shape: [])
}
/// Creates a `ShapedArray` with the specified shape and a single, repeated scalar value.
/// - Parameters:
/// - shape: The shape of the `ShapedArray`.
/// - repeatedValue: The scalar value to repeat.
@inlinable
@available(*, deprecated, renamed: "init(repeating:shape:)")
public init(shape: __owned [Int], repeating repeatedValue: __owned Scalar) {
self.init(repeating: repeatedValue, shape: shape)
}
/// Creates a `ShapedArray` with the specified shape and a single, repeated scalar value.
/// - Parameters:
/// - repeatedValue: The scalar value to repeat.
/// - shape: The shape of the `ShapedArray`.
public init(repeating repeatedValue: __owned Scalar, shape: __owned [Int]) {
let scalarCount = shape.reduce(1, *)
let buffer = ArrayTensorBuffer<Scalar>(Array(repeating: repeatedValue, count: scalarCount))
self.init(buffer: buffer, shape: shape)
}
}
extension ShapedArray: RandomAccessCollection, MutableCollection {
public typealias Index = Int
public typealias Element = ShapedArraySlice<Scalar>
public typealias SubSequence = ShapedArraySlice<Scalar>
public var indices: Range<Int> {
return 0..<count
}
public var startIndex: Int {
return 0
}
public var endIndex: Int {
return count
}
/// Access the element array specified by an index in the leading dimension.
/// - Parameter index: Index of the element array.
public subscript(index: Int) -> Element {
get {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(index < endIndex, "ShapedArray index is out of range.")
precondition(index >= startIndex, "Negative ShapedArray index is out of range.")
return ShapedArraySlice(base: self, baseIndices: [index])
}
set {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(index < endIndex, "ShapedArray index is out of range.")
precondition(index >= startIndex, "Negative ShapedArray index is out of range.")
precondition(shape.dropFirst().elementsEqual(newValue.shape), "Element shape mismatch.")
let scalarIndex = self.scalarIndex(fromIndex: index)
withUnsafeMutableBufferPointer { destBuffPtr in
let ptr = destBuffPtr.baseAddress!.advanced(by: scalarIndex)
newValue.withUnsafeBufferPointer { srcBuffPtr in
ptr.initialize(from: srcBuffPtr.baseAddress!, count: srcBuffPtr.count)
}
}
}
}
/// Access the subarray specified by a contiguous range of indices.
/// - Parameter bounds: Contiguous range of indices.
public subscript(bounds: Range<Int>) -> SubSequence {
get {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(
bounds.lowerBound >= startIndex && bounds.lowerBound <= endIndex
&& bounds.upperBound >= startIndex && bounds.upperBound <= endIndex,
"ShapedArray indices are out of range")
return ShapedArraySlice(base: self, bounds: bounds)
}
set {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(
indices ~= bounds.lowerBound && indices ~= bounds.upperBound - 1,
"ShapedArray indices are out of range.")
let subArrayShape = [bounds.count] + shape.dropFirst()
precondition(subArrayShape == newValue.shape, "Subarray shape mismatch.")
let scalarIndex = self.scalarIndex(fromIndex: bounds.lowerBound)
withUnsafeMutableBufferPointer { destBuffPtr in
let ptr = destBuffPtr.baseAddress!.advanced(by: scalarIndex)
newValue.withUnsafeBufferPointer { srcBuffPtr in
ptr.initialize(from: srcBuffPtr.baseAddress!, count: srcBuffPtr.count)
}
}
}
}
}
extension ShapedArray {
/// Calls a closure with a pointer to the array’s contiguous storage.
/// - Parameter body: A closure with an `UnsafeBufferPointer` parameter that points to the
/// contiguous storage for the array. If no such storage exists, it is created. If body has a
/// return value, that value is also used as the return value for the
/// `withUnsafeBufferPointer(_:)` method. The pointer argument is valid only for the duration
/// of the method's execution.
public func withUnsafeBufferPointer<Result>(
_ body: (UnsafeBufferPointer<Scalar>) throws -> Result
) rethrows -> Result {
return try buffer.withUnsafeBufferPointer { ptr in try body(ptr) }
}
/// Calls the given closure with a pointer to the array’s mutable contiguous storage.
/// - Parameter body: A closure with an `UnsafeMutableBufferPointer` parameter that points to
/// the contiguous storage for the array. If no such storage exists, it is created. If body
/// has a return value, that value is also used as the return value for the
/// `withUnsafeMutableBufferPointer(_:)` method. The pointer argument is valid only for the
/// duration of the method's execution.
public mutating func withUnsafeMutableBufferPointer<Result>(
_ body: (inout UnsafeMutableBufferPointer<Scalar>) throws -> Result
) rethrows -> Result {
ensureUniquelyReferenced()
return try buffer.withUnsafeMutableBufferPointer { ptr in try body(&ptr) }
}
}
// Array literal conversion.
extension ShapedArray: ExpressibleByArrayLiteral where Scalar: TensorFlowScalar {
public typealias ArrayLiteralElement = _TensorElementLiteral<Scalar>
@inlinable
public init(arrayLiteral elements: _TensorElementLiteral<Scalar>...) {
precondition(!elements.isEmpty, "Cannot create a 'ShapedArray' with no elements.")
self = Tensor<Scalar>(_tensorElementLiterals: elements).array
}
}
// Equatable conformance.
extension ShapedArray: Equatable where Scalar: Equatable {
public static func == (lhs: ShapedArray, rhs: ShapedArray) -> Bool {
return lhs._isEqual(to: rhs)
}
}
// Hashable conformance.
extension ShapedArray: Hashable where Scalar: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(shape)
hasher.combine(scalars)
}
}
// String conversion.
extension ShapedArray: CustomStringConvertible {
/// A textual representation of this `ShapedArray`.
///
/// - Note: use `fullDescription` for a non-pretty-printed description showing all scalars.
public var description: String {
// Summarize if there are more than 1000 scalars.
let summarizing = scalarCount > 1000
return description(summarizing: summarizing)
}
}
// Xcode Playground display conversion.
extension ShapedArray: CustomPlaygroundDisplayConvertible {
public var playgroundDescription: Any {
return description
}
}
// Mirror representation, used by debugger/REPL.
extension ShapedArray: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: [], displayStyle: .struct)
}
}
// Codable conformance.
extension ShapedArray: Codable where Scalar: Codable {
private enum CodingKeys: String, CodingKey {
case shape
case scalars
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let shape = try container.decode([Int].self, forKey: .shape)
let scalars = try container.decode([Scalar].self, forKey: .scalars)
self.init(shape: shape, scalars: scalars)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(shape, forKey: .shape)
try container.encode(scalars, forKey: .scalars)
}
}
//===------------------------------------------------------------------------------------------===//
// ShapedArraySlice
//===------------------------------------------------------------------------------------------===//
/// A contiguous slice of a `ShapedArray` or `ShapedArraySlice` instance.
///
/// `ShapedArraySlice` enables fast, efficient operations on contiguous slices of `ShapedArray`
/// instances. `ShapedArraySlice` instances do not have their own storage. Instead, they provides a
/// view onto the storage of their base `ShapedArray`. `ShapedArraySlice` can represent two
/// different kinds of slices: element arrays and subarrays.
///
/// Element arrays are subdimensional elements of a `ShapedArray`: their rank is one less than that
/// of their base. Element array slices are obtained by indexing a `ShapedArray` instance with a
/// singular `Int32` index.
///
/// For example:
/// ```
/// var matrix = ShapedArray(shape: [2, 2], scalars: [0, 1, 2, 3])
/// // `matrix` represents [[0, 1], [2, 3]].
///
/// let element = matrix[0]
/// // `element` is a `ShapedArraySlice` with shape [2]. It is an element
/// // array, specifically the first element in `matrix`: [0, 1].
///
/// matrix[1] = ShapedArraySlice(shape: [2], scalars: [4, 8])
/// // The second element in `matrix` has been mutated.
/// // `matrix` now represents [[0, 1, 4, 8]].
/// ```
///
/// Subarrays are a contiguous range of the elements in a `ShapedArray`. The rank of a subarray is
/// the same as that of its base, but its leading dimension is the count of the slice range.
/// Subarray slices are obtained by indexing a `ShapedArray` with a `Range<Int32>` that represents a
/// range of elements (in the leading dimension). Methods like `prefix(:)` and `suffix(:)` that
/// internally index with a range also produce subarray.
///
/// For example:
/// ```
/// let zeros = ShapedArray(repeating: 0, shape: [3, 2])
/// var matrix = ShapedArray(shape: [3, 2], scalars: Array(0..<6))
/// // `zeros` represents [[0, 0], [0, 0], [0, 0]].
/// // `matrix` represents [[0, 1], [2, 3], [4, 5]].
///
/// let subarray = matrix.prefix(2)
/// // `subarray` is a `ShapedArraySlice` with shape [2, 2]. It is a slice
/// // of the first 2 elements in `matrix` and represents [[0, 1], [2, 3]].
///
/// matrix[0..<2] = zeros.prefix(2)
/// // The first 2 elements in `matrix` have been mutated.
/// // `matrix` now represents [[0, 0], [0, 0], [4, 5]].
/// ```
@frozen
public struct ShapedArraySlice<Scalar>: _ShapedArrayProtocol {
/// The underlying `ShapedArray` of the slice.
@usableFromInline internal var base: ShapedArray<Scalar>
/// The subdimensional indices of a slice.
@usableFromInline internal var baseIndices: [Int]
/// The subarray bounds of a slice.
@usableFromInline internal var bounds: Range<Int>?
/// Creates a `ShapedArraySlice` from a base `ShapedArray`, with the specified subdimensional
/// indices and subarray bounds.
@inlinable
internal init(
base: __owned ShapedArray<Scalar>,
baseIndices indices: __owned [Int] = [],
bounds: Range<Int>? = nil
) {
precondition(indices.count <= base.rank, "Number of base indices exceeds base rank.")
precondition(
zip(base.shape, indices).allSatisfy { $1 >= 0 && $1 < $0 },
"Base indices are out of range")
self.base = base
self.baseIndices = indices
self.bounds = bounds
}
}
extension ShapedArraySlice {
/// Indexing depth of this slice, i.e. the difference in rank between the base and the slice.
internal var indexingDepth: Int {
return baseIndices.count
}
/// The number of dimensions of the array.
public var rank: Int {
return base.rank - indexingDepth
}
/// The shape of the array.
public var shape: [Int] {
if let bounds = bounds {
return [bounds.count] + Array(base.shape.dropFirst(indexingDepth + 1))
}
return Array(base.shape.dropFirst(indexingDepth))
}
/// The total number of scalars in the array.
public var scalarCount: Int {
return shape.reduce(1, *)
}
}
// Slice initializers.
extension ShapedArraySlice {
/// Creates a `ShapedArraySlice` with the specified shape and contiguous scalars in row-major
/// order.
/// - Precondition: The number of scalars must equal the product of the dimensions of the shape.
public init(shape: __owned [Int], scalars: __owned [Scalar]) {
self.init(base: ShapedArray(shape: shape, scalars: scalars))
}
/// Creates an `ShapedArraySlice` with the specified shape and sequence of scalars in row-major
/// order.
/// - Precondition: The number of scalars must equal the product of the dimensions of the shape.
public init<S: Sequence>(shape: __owned [Int], scalars: __shared S) where S.Element == Scalar {
self.init(base: ShapedArray(shape: shape, scalars: scalars))
}
/// Creates a `ShapedArraySlice` from a scalar value.
public init(_ scalar: __owned Scalar) {
self.init(base: ShapedArray(scalar))
}
/// Creates a `ShapedArraySlice` with the specified shape and a single, repeated scalar value.
/// - Parameters:
/// - repeatedValue: The scalar value to repeat.
/// - shape: The shape of the `ShapedArraySlice`.
@inlinable
@available(*, deprecated, renamed: "init(repeating:shape:)")
public init(shape: __owned [Int], repeating repeatedValue: __owned Scalar) {
self.init(repeating: repeatedValue, shape: shape)
}
/// Creates a `ShapedArraySlice` with the specified shape and a single, repeated scalar value.
/// - Parameters:
/// - repeatedValue: The scalar value to repeat.
/// - shape: The shape of the `ShapedArraySlice`.
public init(repeating repeatedValue: __owned Scalar, shape: __owned [Int]) {
self.init(base: ShapedArray(repeating: repeatedValue, shape: shape))
}
}
extension ShapedArraySlice {
/// The range of scalars from the base `ShapedArray` represented by a `ShapedArraySlice`.
var scalarRange: Range<Int> {
let trimmedShape = base.shape.dropFirst()
var (start, end) = baseIndices.enumerated().reduce((0, base.scalarCount)) { (acc, next) in
let stride = trimmedShape.dropFirst(next.offset).reduce(1, *)
if next.offset == indexingDepth - 1 {
let temp = acc.0 + next.element * stride
return (temp, temp + stride)
}
return (acc.0 + next.element * stride, acc.1)
}
if let bounds = bounds {
let stride = trimmedShape.dropFirst(indexingDepth).reduce(1, *)
let oldStart = start
start = start + bounds.startIndex * stride
end = oldStart + bounds.endIndex * stride
}
return start..<end
}
}
extension ShapedArraySlice {
/// Calls a closure with a pointer to the `ShapedArraySlice`’s contiguous storage.
/// - Parameter body: A closure with an `UnsafeBufferPointer` parameter that points to the
/// contiguous storage for the `ShapedArraySlice`. If no such storage exists, it is created.
/// If body has a return value, that value is also used as the return value for the
/// `withUnsafeBufferPointer(_:)` method. The pointer argument is valid only for the duration
/// of the method's execution.
public func withUnsafeBufferPointer<Result>(
_ body: (UnsafeBufferPointer<Scalar>) throws -> Result
) rethrows -> Result {
return try base.withUnsafeBufferPointer { baseBuffPtr in
let basePtr = baseBuffPtr.baseAddress!
let ptr = UnsafeBufferPointer(
start: basePtr.advanced(by: scalarRange.startIndex),
count: scalarRange.count)
return try body(ptr)
}
}
/// Calls the given closure with a pointer to the `ShapedArraySlice`'s mutable contiguous
/// storage.
/// - Parameter body: A closure with an `UnsafeMutableBufferPointer` parameter that points to
/// the contiguous storage for the `ShapedArraySlice`. If no such storage exists, it is
/// created. If body has a return value, that value is also used as the return value for the
/// `withUnsafeMutableBufferPointer(_:)` method. The pointer argument is valid only for the
/// duration of the method’s execution.
public mutating func withUnsafeMutableBufferPointer<Result>(
_ body: (inout UnsafeMutableBufferPointer<Scalar>) throws -> Result
) rethrows -> Result {
// NOTE: Copying `scalarRange` to a local variable here is necessary for
// exclusive access.
let scalarRange = self.scalarRange
return try base.withUnsafeMutableBufferPointer { baseBuffPtr in
let basePtr = baseBuffPtr.baseAddress!
var ptr = UnsafeMutableBufferPointer(
start: basePtr.advanced(by: scalarRange.startIndex),
count: scalarRange.count)
return try body(&ptr)
}
}
}
extension ShapedArraySlice: RandomAccessCollection, MutableCollection {
public typealias Index = Int
public typealias Element = ShapedArraySlice
public typealias SubSequence = ShapedArraySlice
public var indices: Range<Int> {
if let bounds = bounds {
return bounds
} else if indexingDepth < base.rank {
return 0..<base.shape[indexingDepth]
}
return 0..<0
}
public var startIndex: Int {
return indices.startIndex
}
public var endIndex: Int {
return indices.endIndex
}
/// Access the element array specified by an index in the leading dimension.
/// - Parameter index: Index of the element array.
public subscript(index: Int) -> Element {
get {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(index < endIndex, "ShapedArraySlice index is out of range.")
precondition(
index >= startIndex,
"ShapeArraySlice index is out of range (before startIndex).")
return ShapedArraySlice(base: base, baseIndices: baseIndices + [index], bounds: nil)
}
set {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(index < endIndex, "ShapedArraySlice index is out of range")
precondition(
index >= startIndex,
"ShapeArraySlice index is out of range (before startIndex).")
precondition(shape.dropFirst().elementsEqual(newValue.shape), "Element shape mismatch.")
let scalarIndex = self.scalarIndex(fromIndex: index)
withUnsafeMutableBufferPointer { destBuffPtr in
let ptr = destBuffPtr.baseAddress!.advanced(by: scalarIndex)
newValue.withUnsafeBufferPointer { srcBuffPtr in
ptr.initialize(from: srcBuffPtr.baseAddress!, count: srcBuffPtr.count)
}
}
}
}
/// Access the subarray specified by a contiguous range of indices.
/// - Parameter bounds: Contiguous range of indices.
public subscript(bounds: Range<Int>) -> SubSequence {
get {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(
indices ~= bounds.lowerBound && indices ~= bounds.upperBound - 1,
"ShapedArraySlice indices are out of range.")
return ShapedArraySlice(base: base, baseIndices: baseIndices, bounds: bounds)
}
set {
precondition(!isScalar, "Scalar has no elements and cannot be subscripted.")
precondition(
indices ~= bounds.lowerBound && indices ~= bounds.upperBound - 1,
"ShapedArraySlice indices are out of range.")
let subArrayShape = [bounds.count] + shape.dropFirst()
precondition(subArrayShape == newValue.shape, "Subarray shape mismatch.")
let scalarIndex = self.scalarIndex(fromIndex: bounds.lowerBound)
withUnsafeMutableBufferPointer { destBuffPtr in
let ptr = destBuffPtr.baseAddress!.advanced(by: scalarIndex)
newValue.withUnsafeBufferPointer { srcBuffPtr in
ptr.initialize(from: srcBuffPtr.baseAddress!, count: srcBuffPtr.count)
}
}
}
}
}
// Tensor conversion.
extension ShapedArraySlice where Scalar: TensorFlowScalar {
public init(_ tensor: __shared Tensor<Scalar>) {
self.init(base: tensor.array)
}
}
// Array literal conversion.
extension ShapedArraySlice: ExpressibleByArrayLiteral where Scalar: TensorFlowScalar {
public typealias ArrayLiteralElement = _TensorElementLiteral<Scalar>
@inlinable
public init(arrayLiteral elements: _TensorElementLiteral<Scalar>...) {
precondition(!elements.isEmpty, "Cannot create a 'ShapedArraySlice' with no elements.")
self.init(base: Tensor(_tensorElementLiterals: elements).array)
}
}
// Equatable conformance.
extension ShapedArraySlice: Equatable where Scalar: Equatable {
public static func == (lhs: ShapedArraySlice, rhs: ShapedArraySlice) -> Bool {
return lhs._isEqual(to: rhs)
}
}
// Hashable conformance.
extension ShapedArraySlice: Hashable where Scalar: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(shape)
hasher.combine(scalars)
}
}
// String conversion.
extension ShapedArraySlice: CustomStringConvertible {
/// A textual representation of this `ShapedArraySlice`.
///
/// - Note: use `fullDescription` for a non-pretty-printed representation showing all scalars.
public var description: String {
// Summarize if there are more than 1000 scalars.
let summarizing = scalarCount > 1000
return description(summarizing: summarizing)
}
}
// Xcode Playground display conversion.
extension ShapedArraySlice: CustomPlaygroundDisplayConvertible {
public var playgroundDescription: Any {
return description
}
}
// Mirror representation, used by debugger/REPL.
extension ShapedArraySlice: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: [], displayStyle: .struct)
}
}
// Codable conformance.
extension ShapedArraySlice: Codable where Scalar: Codable {
private enum CodingKeys: String, CodingKey {
case shape
case scalars
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(shape, forKey: .shape)
try container.encode(scalars, forKey: .scalars)
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let shape = try container.decode([Int].self, forKey: .shape)
let scalars = try container.decode([Scalar].self, forKey: .scalars)
self.init(shape: shape, scalars: scalars)
}
}
extension _ShapedArrayProtocol where Scalar: Equatable {
fileprivate func _isEqual(to other: Self) -> Bool {
return shape == other.shape
&& withUnsafeBufferPointer { selfBuf in
other.withUnsafeBufferPointer { otherBuf in
selfBuf.elementsEqual(otherBuf)
}
}
}
}