Skip to content

Commit 20948e3

Browse files
authored
---
yaml --- r: 277501 b: refs/heads/tensorflow-merge c: 0790a41 h: refs/heads/master i: 277499: 926a272
1 parent 19297ed commit 20948e3

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-29-a: 1b087071edaea398480fb778e
11241124
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-10-30-a: 8bc9e108e1480d9217299984e428c601c7aaac75
11251125
refs/tags/swift-4.2.1-RELEASE: 02a6ca969ea1387475b6caeb69c31186df7d30b6
11261126
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-11-01-a: 3b0299288f8287094b9ef587f46df54f42a347af
1127-
refs/heads/tensorflow-merge: 133501508aa0a8d80caa210d6a25e4d757750952
1127+
refs/heads/tensorflow-merge: 0790a41665fc5b30f9099e6508177b6b87de0eec
11281128
refs/heads/TensorFlowLite: b91446471276e37bbfe64767c875f3c7f7102954
11291129
refs/heads/ad-side-effects: 19e0c0de1f59b0929c381925df2e8c72cdf4a728
11301130
refs/heads/add-test-for-asan-compiler-crash: 3cdeecffb47bf28707b299fa2b5bdf0769a4a826

branches/tensorflow-merge/stdlib/public/TensorFlow/ShapedArray.swift

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal final class TensorBuffer<Scalar> {
3535
final class BoxedArray {
3636
var array: [Scalar]
3737

38-
init(_ array: [Scalar]) {
38+
init(_ array: __owned [Scalar]) {
3939
self.array = array
4040
}
4141
}
@@ -292,7 +292,7 @@ public struct ShapedArray<Scalar> : _ShapedArrayProtocol {
292292
public private(set) var shape: [Int]
293293

294294
/// 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]) {
296296
precondition(buffer.count == shape.reduce(1, *),
297297
"The scalar count of the buffer does not match the shape.")
298298
self.buffer = buffer
@@ -349,19 +349,15 @@ public extension ShapedArray {
349349
self.init(buffer: other.buffer, shape: other.shape)
350350
}
351351

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)
361356
self.init(buffer: buffer, shape: shape)
362357
}
363358

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 {
365361
let scalarCount = shape.reduce(1, *)
366362
let buffer = TensorBuffer<Scalar>.create(count: scalarCount) { buffPtr in
367363
let ptr = buffPtr.baseAddress!
@@ -381,25 +377,25 @@ public extension ShapedArray {
381377
}
382378

383379
/// 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+
)
390385
}
391386

392387
/// Creates a `ShapedArray` with the specified shape and a single, repeated
393388
/// value.
394389
/// - Parameters:
395390
/// - shape: The dimensions of the array.
396391
/// - repeatedValue: The scalar value to repeat.
397-
init(shape: [Int], repeating repeatedValue: Scalar) {
392+
init(shape: __owned [Int], repeating repeatedValue: __owned Scalar) {
398393
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+
)
403399
self.init(buffer: buffer, shape: shape)
404400
}
405401
}
@@ -507,7 +503,7 @@ extension ShapedArray where Scalar : TensorFlowScalar {
507503
}
508504

509505
@usableFromInline
510-
func makeTensorHandle() -> TensorHandle<Scalar> {
506+
__consuming func makeTensorHandle() -> TensorHandle<Scalar> {
511507
// This initializer is designed to optimize conversion from TF-allocated
512508
// `ShapedArray` instances.
513509
switch buffer.allocation {
@@ -533,7 +529,7 @@ extension ShapedArray where Scalar : TensorFlowScalar {
533529

534530
/// Tensor conversion.
535531
public extension Tensor {
536-
init(_ array: ShapedArray<Scalar>) {
532+
init(_ array: __owned ShapedArray<Scalar>) {
537533
self.init(handle: array.makeTensorHandle())
538534
}
539535
}
@@ -670,8 +666,8 @@ public struct ShapedArraySlice<Scalar> : _ShapedArrayProtocol {
670666
/// subdimensional indices and subarray bounds.
671667
@inlinable
672668
internal init(
673-
base: ShapedArray<Scalar>,
674-
baseIndices indices: [Int] = [],
669+
base: __owned ShapedArray<Scalar>,
670+
baseIndices indices: __owned [Int] = [],
675671
bounds: Range<Int>? = nil
676672
) {
677673
precondition(indices.count <= base.rank,
@@ -709,16 +705,17 @@ public extension ShapedArraySlice {
709705

710706
/// Slice initializers.
711707
public extension ShapedArraySlice {
712-
init(shape: [Int], scalars: [Scalar]) {
708+
init(shape: __owned [Int], scalars: __owned [Scalar]) {
713709
self.init(base: ShapedArray(shape: shape, scalars: scalars))
714710
}
715711

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 {
717714
self.init(base: ShapedArray(shape: shape, scalars: scalars))
718715
}
719716

720717
/// Creates a `ShapedArraySlice` from a scalar value.
721-
init(_ scalar: Scalar) {
718+
init(_ scalar: __owned Scalar) {
722719
self.init(base: ShapedArray(scalar))
723720
}
724721

@@ -727,7 +724,7 @@ public extension ShapedArraySlice {
727724
/// - Parameters:
728725
/// - shape: The dimensions of the array.
729726
/// - repeatedValue: The scalar value to repeat.
730-
init(shape: [Int], repeating repeatedValue: Scalar) {
727+
init(shape: __owned [Int], repeating repeatedValue: __owned Scalar) {
731728
self.init(base: ShapedArray(shape: shape, repeating: repeatedValue))
732729
}
733730
}
@@ -874,7 +871,7 @@ extension ShapedArraySlice : RandomAccessCollection, MutableCollection {
874871

875872
/// Tensor conversion.
876873
public extension ShapedArraySlice where Scalar : TensorFlowScalar {
877-
init(_ tensor: Tensor<Scalar>) {
874+
init(_ tensor: __shared Tensor<Scalar>) {
878875
self.init(base: tensor.array)
879876
}
880877
}

0 commit comments

Comments
 (0)