@@ -35,7 +35,7 @@ internal final class TensorBuffer<Scalar> {
35
35
final class BoxedArray {
36
36
var array : [ Scalar ]
37
37
38
- init ( _ array: [ Scalar ] ) {
38
+ init ( _ array: __owned [ Scalar ] ) {
39
39
self . array = array
40
40
}
41
41
}
@@ -292,7 +292,7 @@ public struct ShapedArray<Scalar> : _ShapedArrayProtocol {
292
292
public private( set) var shape : [ Int ]
293
293
294
294
/// Creates a `ShapedArray` from a `TensorBuffer` and a shape.
295
- internal init ( buffer: TensorBuffer < Scalar > , shape: [ Int ] ) {
295
+ internal init ( buffer: __owned TensorBuffer< Scalar > , shape: __owned [ Int ] ) {
296
296
precondition ( buffer. count == shape. reduce ( 1 , * ) ,
297
297
" The scalar count of the buffer does not match the shape. " )
298
298
self . buffer = buffer
@@ -349,19 +349,15 @@ public extension ShapedArray {
349
349
self . init ( buffer: other. buffer, shape: other. shape)
350
350
}
351
351
352
- init ( shape: [ Int ] , scalars: [ Scalar ] ) {
353
- let scalarCount = shape. reduce ( 1 , * )
354
- precondition ( scalarCount == scalars. count, " Scalar count mismatch. " )
355
- let buffer = TensorBuffer< Scalar> . create( count: scalarCount) { buffPtr in
356
- let ptr = buffPtr. baseAddress!
357
- scalars. withUnsafeBufferPointer { arrayBuf in
358
- ptr. initialize ( from: arrayBuf. baseAddress!, count: scalarCount)
359
- }
360
- }
352
+ init ( shape: __owned [ Int ] , scalars: __owned [ Scalar ] ) {
353
+ precondition ( shape. reduce ( 1 , * ) == scalars. count, " Scalar count mismatch. " )
354
+ let buffer = TensorBuffer < Scalar > ( allocation: . native( . init( scalars) ) ,
355
+ count: scalars. count)
361
356
self . init ( buffer: buffer, shape: shape)
362
357
}
363
358
364
- init < S : Sequence > ( shape: [ Int ] , scalars: S ) where S. Element == Scalar {
359
+ init < S : Sequence > ( shape: __owned [ Int ] ,
360
+ scalars: __shared S) where S. Element == Scalar {
365
361
let scalarCount = shape. reduce ( 1 , * )
366
362
let buffer = TensorBuffer< Scalar> . create( count: scalarCount) { buffPtr in
367
363
let ptr = buffPtr. baseAddress!
@@ -381,25 +377,25 @@ public extension ShapedArray {
381
377
}
382
378
383
379
/// Creates a `ShapedArray` from a scalar value.
384
- init ( _ scalar: Scalar ) {
385
- let buffer = TensorBuffer< Scalar> . create( count: 1 ) { buffPtr in
386
- let ptr = buffPtr. baseAddress!
387
- ptr. initialize ( to: scalar)
388
- }
389
- self . init ( buffer: buffer, shape: [ ] )
380
+ init ( _ scalar: __owned Scalar) {
381
+ self . init (
382
+ buffer: TensorBuffer ( allocation: . native( . init( [ scalar] ) ) , count: 1 ) ,
383
+ shape: [ ]
384
+ )
390
385
}
391
386
392
387
/// Creates a `ShapedArray` with the specified shape and a single, repeated
393
388
/// value.
394
389
/// - Parameters:
395
390
/// - shape: The dimensions of the array.
396
391
/// - repeatedValue: The scalar value to repeat.
397
- init ( shape: [ Int ] , repeating repeatedValue: Scalar ) {
392
+ init ( shape: __owned [ Int ] , repeating repeatedValue: __owned Scalar) {
398
393
let scalarCount = shape. reduce ( 1 , * )
399
- let buffer = TensorBuffer< Scalar> . create( count: scalarCount) { buffPtr in
400
- let ptr = buffPtr. baseAddress!
401
- ptr. initialize ( repeating: repeatedValue, count: scalarCount)
402
- }
394
+ let buffer = TensorBuffer < Scalar > (
395
+ allocation: . native( . init( Array ( repeating: repeatedValue,
396
+ count: scalarCount) ) ) ,
397
+ count: scalarCount
398
+ )
403
399
self . init ( buffer: buffer, shape: shape)
404
400
}
405
401
}
@@ -507,7 +503,7 @@ extension ShapedArray where Scalar : TensorFlowScalar {
507
503
}
508
504
509
505
@usableFromInline
510
- func makeTensorHandle( ) -> TensorHandle < Scalar > {
506
+ __ consuming func makeTensorHandle( ) -> TensorHandle < Scalar > {
511
507
// This initializer is designed to optimize conversion from TF-allocated
512
508
// `ShapedArray` instances.
513
509
switch buffer. allocation {
@@ -533,7 +529,7 @@ extension ShapedArray where Scalar : TensorFlowScalar {
533
529
534
530
/// Tensor conversion.
535
531
public extension Tensor {
536
- init ( _ array: ShapedArray < Scalar > ) {
532
+ init ( _ array: __owned ShapedArray< Scalar > ) {
537
533
self . init ( handle: array. makeTensorHandle ( ) )
538
534
}
539
535
}
@@ -670,8 +666,8 @@ public struct ShapedArraySlice<Scalar> : _ShapedArrayProtocol {
670
666
/// subdimensional indices and subarray bounds.
671
667
@inlinable
672
668
internal init (
673
- base: ShapedArray < Scalar > ,
674
- baseIndices indices: [ Int ] = [ ] ,
669
+ base: __owned ShapedArray< Scalar > ,
670
+ baseIndices indices: __owned [ Int ] = [ ] ,
675
671
bounds: Range < Int > ? = nil
676
672
) {
677
673
precondition ( indices. count <= base. rank,
@@ -709,16 +705,17 @@ public extension ShapedArraySlice {
709
705
710
706
/// Slice initializers.
711
707
public extension ShapedArraySlice {
712
- init ( shape: [ Int ] , scalars: [ Scalar ] ) {
708
+ init ( shape: __owned [ Int ] , scalars: __owned [ Scalar ] ) {
713
709
self . init ( base: ShapedArray ( shape: shape, scalars: scalars) )
714
710
}
715
711
716
- init < S : Sequence > ( shape: [ Int ] , scalars: S ) where S. Element == Scalar {
712
+ init < S : Sequence > ( shape: __owned [ Int ] ,
713
+ scalars: __shared S) where S. Element == Scalar {
717
714
self . init ( base: ShapedArray ( shape: shape, scalars: scalars) )
718
715
}
719
716
720
717
/// Creates a `ShapedArraySlice` from a scalar value.
721
- init ( _ scalar: Scalar ) {
718
+ init ( _ scalar: __owned Scalar) {
722
719
self . init ( base: ShapedArray ( scalar) )
723
720
}
724
721
@@ -727,7 +724,7 @@ public extension ShapedArraySlice {
727
724
/// - Parameters:
728
725
/// - shape: The dimensions of the array.
729
726
/// - repeatedValue: The scalar value to repeat.
730
- init ( shape: [ Int ] , repeating repeatedValue: Scalar ) {
727
+ init ( shape: __owned [ Int ] , repeating repeatedValue: __owned Scalar) {
731
728
self . init ( base: ShapedArray ( shape: shape, repeating: repeatedValue) )
732
729
}
733
730
}
@@ -874,7 +871,7 @@ extension ShapedArraySlice : RandomAccessCollection, MutableCollection {
874
871
875
872
/// Tensor conversion.
876
873
public extension ShapedArraySlice where Scalar : TensorFlowScalar {
877
- init ( _ tensor: Tensor < Scalar > ) {
874
+ init ( _ tensor: __shared Tensor< Scalar > ) {
878
875
self . init ( base: tensor. array)
879
876
}
880
877
}
0 commit comments