Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 31e8dc2

Browse files
committed
Merge branch 'master' of https://github.com/tensorflow/swift-apis into separable-conv
2 parents e900edb + 268055e commit 31e8dc2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2499
-1403
lines changed

.swift-format

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": 1,
3+
"lineLength": 100,
4+
"indentation": {
5+
"spaces": 4
6+
},
7+
"maximumBlankLines": 1,
8+
"respectsExistingLineBreaks": true,
9+
"blankLineBetweenMembers": {
10+
"ignoreSingleLineProperties": true
11+
},
12+
"lineBreakBeforeControlFlowKeywords": false,
13+
"lineBreakBeforeEachArgument": false
14+
}

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,30 @@ RUN echo "/usr/local/cuda-10.0/targets/x86_64-linux/lib/stubs" > /etc/ld.so.conf
3939
# Print out swift version for better debugging for toolchain problems
4040
RUN /swift-tensorflow-toolchain/usr/bin/swift --version
4141

42+
# Clean out existing artifacts.
43+
# TODO: move into bash scripts...
44+
RUN rm /swift-tensorflow-toolchain/usr/lib/swift/linux/x86_64/TensorFlow.swiftinterface
45+
RUN rm /swift-tensorflow-toolchain/usr/lib/swift/linux/x86_64/TensorFlow.swiftdoc
46+
RUN rm /swift-tensorflow-toolchain/usr/lib/swift/linux/x86_64/TensorFlow.swiftmodule
47+
RUN rm /swift-tensorflow-toolchain/usr/lib/swift/linux/libswiftTensorFlow.so
48+
4249
# Run SwiftPM tests
4350
RUN /swift-tensorflow-toolchain/usr/bin/swift test
51+
52+
# Install into toolchain
53+
# TODO: Unify this with testing. (currently there is a demangling bug).
54+
RUN /swift-tensorflow-toolchain/usr/bin/swift build -Xswiftc -module-link-name -Xswiftc TensorFlow
55+
RUN cp /swift-apis/.build/debug/TensorFlow.swiftmodule /swift-tensorflow-toolchain/usr/lib/swift/linux/x86_64/
56+
RUN cp /swift-apis/.build/debug/libTensorFlow.so /swift-tensorflow-toolchain/usr/lib/swift/linux/
57+
58+
WORKDIR /
59+
RUN git clone https://github.com/tensorflow/swift-models.git
60+
RUN git clone https://github.com/fastai/fastai_dev.git
61+
62+
WORKDIR /swift-models
63+
64+
RUN /swift-tensorflow-toolchain/usr/bin/swift build
65+
66+
WORKDIR /fastai_dev/swift/FastaiNotebook_07_batchnorm
67+
68+
RUN /swift-tensorflow-toolchain/usr/bin/swift build

Package.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ let package = Package(
2222
products: [
2323
.library(
2424
name: "TensorFlow",
25+
type: .dynamic,
2526
targets: ["TensorFlow"]),
2627
],
2728
dependencies: [],
2829
targets: [
2930
.target(
3031
name: "TensorFlow",
3132
dependencies: []),
33+
.target(
34+
name: "Experimental",
35+
dependencies: [],
36+
path: "Sources/third_party/Experimental"),
37+
.testTarget(
38+
name: "ExperimentalTests",
39+
dependencies: ["Experimental"]),
3240
.testTarget(
3341
name: "TensorFlowTests",
3442
dependencies: ["TensorFlow"]),

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ for _ in 0..<1000 {
5757
print("Loss: \(loss)")
5858
return loss
5959
}
60-
optimizer.update(&classifier.allDifferentiableVariables, along: 𝛁model)
60+
optimizer.update(&classifier, along: 𝛁model)
6161
}
6262
```
6363

@@ -69,7 +69,7 @@ for _ in 0..<1000 {
6969
let (loss, 𝛁ŷ) = ŷ.valueWithGradient { ŷ in softmaxCrossEntropy(logits: ŷ, labels: y) }
7070
print("Model output: \(ŷ), Loss: \(loss)")
7171
let 𝛁model = backprop(𝛁ŷ)
72-
optimizer.update(&classifier.allDifferentiableVariables, along: 𝛁model)
72+
optimizer.update(&classifier, along: 𝛁model)
7373
}
7474
```
7575

Sources/TensorFlow/BackwardsCompatibility.swift

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// TODO: Remove this file after 0.4.
1616

1717
public extension Tensor where Scalar == Int32 {
18-
/// Creates a tensor with the specified shape, randomly sampling scalar values from a discrete
18+
/// Creates a tensor with the specified shape, randomly sampling scalar values from a discrete
1919
/// uniform distribution.
2020
///
2121
/// - Parameters:
@@ -40,7 +40,7 @@ public extension Tensor where Scalar == Int32 {
4040
self.init(shape: shape, scalars: scalars)
4141
}
4242

43-
/// Creates a tensor with the specified shape, randomly sampling scalar values from a discrete
43+
/// Creates a tensor with the specified shape, randomly sampling scalar values from a discrete
4444
/// uniform distribution, using the default random number generator.
4545
///
4646
/// - Parameters:
@@ -63,7 +63,7 @@ public extension Tensor where Scalar == Int32 {
6363

6464
public extension Tensor where Scalar: BinaryFloatingPoint,
6565
Scalar.RawSignificand: FixedWidthInteger {
66-
/// Creates a tensor with the specified shape, randomly sampling scalar values from a uniform
66+
/// Creates a tensor with the specified shape, randomly sampling scalar values from a uniform
6767
/// distribution between `lowerBound` and `upperBound`.
6868
///
6969
/// - Parameters:
@@ -89,7 +89,7 @@ public extension Tensor where Scalar: BinaryFloatingPoint,
8989
self = (upperBound - lowerBound) * sample + lowerBound
9090
}
9191

92-
/// Creates a tensor with the specified shape, randomly sampling scalar values from a normal
92+
/// Creates a tensor with the specified shape, randomly sampling scalar values from a normal
9393
/// distribution.
9494
///
9595
/// - Parameters:
@@ -128,7 +128,7 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint {
128128
let uniform = Tensor(randomUniform: shape, generator: &generator)
129129
self = Tensor.glorot(fromStandardUniform: uniform, shape: shape)
130130
}
131-
131+
132132
/// Performs Glorot normal initialization for the specified shape, creating a tensor by
133133
/// randomly sampling scalar values from a uniform distribution between `-limit` and `limit`,
134134
/// where limit is `sqrt(2 / (fanIn + fanOut))` and `fanIn`/`fanOut` represent the number of
@@ -200,7 +200,11 @@ public extension Conv1D where Scalar.RawSignificand: FixedWidthInteger {
200200
///
201201
/// - Note: Use `init(filterShape:stride:padding:dilation:activation:seed:)` for faster random
202202
/// initialization.
203-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
203+
@available(*, deprecated, message: """
204+
This API will be removed after Swift for TensorFlow 0.4, please consider using
205+
`init(filterShape:stride:padding:dilation:activation:filterIntializer:biasInitializer:)`
206+
instead.
207+
""")
204208
init<G: RandomNumberGenerator>(
205209
filterShape: (Int, Int, Int),
206210
stride: Int = 1,
@@ -221,18 +225,22 @@ public extension Conv1D where Scalar.RawSignificand: FixedWidthInteger {
221225
}
222226
}
223227

224-
public extension Conv1D {
225-
/// Creates a `Conv1D` layer with the specified filter shape, strides, padding, dilation and
226-
/// element-wise activation function. The filter tensor is initialized using Glorot uniform
227-
/// initialization with the specified seed. The bias vector is initialized with zeros.
228+
public extension Conv1D {
229+
/// Creates a `Conv1D` layer with the specified filter shape, strides, padding, dilation and
230+
/// element-wise activation function. The filter tensor is initialized using Glorot uniform
231+
/// initialization with the specified seed. The bias vector is initialized with zeros.
228232
///
229233
/// - Parameters:
230-
/// - filterShape: The 3-D shape of the filter, representing
234+
/// - filterShape: The 3-D shape of the filter, representing
231235
/// - padding: The padding algorithm for convolution.
232236
/// - dilation: The dilation factor for the temporal dimension.
233237
/// - activation: The element-wise activation function.
234238
/// - seed: The random seed for initialization. The default value is random.
235-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
239+
@available(*, deprecated, message: """
240+
This API will be removed after Swift for TensorFlow 0.4, please consider using
241+
`init(filterShape:stride:padding:dilation:activation:filterIntializer:biasInitializer:)`
242+
instead.
243+
""")
236244
init(
237245
filterShape: (Int, Int, Int),
238246
stride: Int = 1,
@@ -271,7 +279,11 @@ public extension Conv2D {
271279
///
272280
/// - Note: Use `init(filterShape:strides:padding:activation:seed:)` for faster random
273281
/// initialization.
274-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
282+
@available(*, deprecated, message: """
283+
This API will be removed after Swift for TensorFlow 0.4, please consider using
284+
`init(filterShape:strides:padding:dilations:activation:filterIntializer:biasInitializer:)`
285+
instead.
286+
""")
275287
init<G: RandomNumberGenerator>(
276288
filterShape: (Int, Int, Int, Int),
277289
strides: (Int, Int) = (1, 1),
@@ -307,7 +319,11 @@ public extension Conv2D {
307319
/// (dilation height, dilation width).
308320
/// - activation: The element-wise activation function.
309321
/// - seed: The random seed for initialization. The default value is random.
310-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
322+
@available(*, deprecated, message: """
323+
This API will be removed after Swift for TensorFlow 0.4, please consider using
324+
`init(filterShape:strides:padding:dilations:activation:filterIntializer:biasInitializer:)`
325+
instead.
326+
""")
311327
init(
312328
filterShape: (Int, Int, Int, Int),
313329
strides: (Int, Int) = (1, 1),
@@ -345,7 +361,10 @@ public extension Conv3D {
345361
///
346362
/// - Note: Use `init(filterShape:strides:padding:activation:seed:)` for faster random
347363
/// initialization.
348-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
364+
@available(*, deprecated, message: """
365+
This API will be removed after Swift for TensorFlow 0.4, please consider using
366+
`init(filterShape:strides:padding:activation:filterIntializer:biasInitializer:)` instead.
367+
""")
349368
init<G: RandomNumberGenerator>(
350369
filterShape: (Int, Int, Int, Int, Int),
351370
strides: (Int, Int, Int) = (1, 1, 1),
@@ -378,7 +397,10 @@ public extension Conv3D {
378397
/// - padding: The padding algorithm for convolution.
379398
/// - activation: The element-wise activation function.
380399
/// - seed: The random seed for initialization. The default value is random.
381-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
400+
@available(*, deprecated, message: """
401+
This API will be removed after Swift for TensorFlow 0.4, please consider using
402+
`init(filterShape:strides:padding:activation:filterIntializer:biasInitializer:)` instead.
403+
""")
382404
init(
383405
filterShape: (Int, Int, Int, Int, Int),
384406
strides: (Int, Int, Int) = (1, 1, 1),
@@ -412,7 +434,10 @@ public extension TransposedConv2D {
412434
///
413435
/// - Note: Use `init(filterShape:strides:padding:activation:seed:)` for faster random
414436
/// initialization.
415-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
437+
@available(*, deprecated, message: """
438+
This API will be removed after Swift for TensorFlow 0.4, please consider using
439+
`init(filterShape:strides:padding:activation:filterIntializer:biasInitializer:)` instead.
440+
""")
416441
init<G: RandomNumberGenerator>(
417442
filterShape: (Int, Int, Int, Int),
418443
strides: (Int, Int) = (1, 1),
@@ -443,7 +468,10 @@ public extension TransposedConv2D {
443468
/// - padding: The padding algorithm for convolution.
444469
/// - activation: The element-wise activation function.
445470
/// - seed: The random seed for initialization. The default value is random.
446-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
471+
@available(*, deprecated, message: """
472+
This API will be removed after Swift for TensorFlow 0.4, please consider using
473+
`init(filterShape:strides:padding:activation:filterIntializer:biasInitializer:)` instead.
474+
""")
447475
init(
448476
filterShape: (Int, Int, Int, Int),
449477
strides: (Int, Int) = (1, 1),
@@ -476,7 +504,10 @@ public extension DepthwiseConv2D {
476504
///
477505
/// - Note: Use `init(filterShape:strides:padding:activation:seed:)` for faster random
478506
/// initialization.
479-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
507+
@available(*, deprecated, message: """
508+
This API will be removed after Swift for TensorFlow 0.4, please consider using
509+
`init(filterShape:strides:padding:activation:filterIntializer:biasInitializer:)` instead.
510+
""")
480511
init<G: RandomNumberGenerator>(
481512
filterShape: (Int, Int, Int, Int),
482513
strides: (Int, Int) = (1, 1),
@@ -506,7 +537,10 @@ public extension DepthwiseConv2D {
506537
/// - padding: The padding algorithm for convolution.
507538
/// - activation: The element-wise activation function.
508539
/// - seed: The random seed for initialization. The default value is random.
509-
@available(*, deprecated, message: "This API will be removed after Swift for TensorFlow 0.4.")
540+
@available(*, deprecated, message: """
541+
This API will be removed after Swift for TensorFlow 0.4, please consider using
542+
`init(filterShape:strides:padding:activation:filterIntializer:biasInitializer:)` instead.
543+
""")
510544
init(
511545
filterShape: (Int, Int, Int, Int),
512546
strides: (Int, Int) = (1, 1),

Sources/TensorFlow/Bindings/EagerExecution.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ internal struct TFE_Op: TFTensorOperation {
6868
let count = input._tensorHandleCount
6969
var buffer = UnsafeMutableBufferPointer<CTensorHandle>.allocate(capacity: Int(count))
7070
defer { buffer.deallocate() }
71-
let pointer = UnsafeMutablePointer<OpaquePointer?>(buffer.baseAddress)
7271
input._unpackTensorHandles(into: buffer.baseAddress)
73-
TFE_OpAddInputList(op, pointer, count, status)
74-
// TODO: checkOk(status)
72+
for i in 0..<Int(count) {
73+
TFE_OpAddInput(op, buffer[i], status)
74+
checkOk(status)
75+
}
7576
}
7677

7778
@inlinable @inline(__always)

Sources/TensorFlow/Bindings/EagerExecution.swift.gyb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ internal struct TFE_Op: TFTensorOperation {
6868
let count = input._tensorHandleCount
6969
var buffer = UnsafeMutableBufferPointer<CTensorHandle>.allocate(capacity: Int(count))
7070
defer { buffer.deallocate() }
71-
let pointer = UnsafeMutablePointer<OpaquePointer?>(buffer.baseAddress)
7271
input._unpackTensorHandles(into: buffer.baseAddress)
73-
TFE_OpAddInputList(op, pointer, count, status)
74-
// TODO: checkOk(status)
72+
for i in 0..<Int(count) {
73+
TFE_OpAddInput(op, buffer[i], status)
74+
checkOk(status)
75+
}
7576
}
7677

7778
@inlinable @inline(__always)
@@ -271,7 +272,12 @@ internal struct TFE_Op: TFTensorOperation {
271272
_ name: String,
272273
_ value: (In) -> Out
273274
) {
274-
_tffunc(value).utf8CString.withUnsafeBufferPointer { buffer in
275+
updateAttribute(name, _TensorFunctionPointer(name: _tffunc(value)))
276+
}
277+
278+
@inlinable @inline(__always)
279+
internal func updateAttribute(_ name: String, _ value: _TensorFunctionPointer) {
280+
value.name.utf8CString.withUnsafeBufferPointer { buffer in
275281
// utf8CString is null-terminated; TFE_OpSetAttrFunctionName wants
276282
// non-null-terminated.
277283
TFE_OpSetAttrFunctionName(op, name, buffer.baseAddress, buffer.count - 1)

Sources/TensorFlow/Context.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public struct Context {
6666
internal var randomNumberGenerator: AnyRandomNumberGenerator =
6767
AnyRandomNumberGenerator(PhiloxRandomNumberGenerator(uint64Seed: UInt64(time(nil))))
6868

69+
internal var globalTensorCount: Int = 0
70+
6971
/// Creates a context with default properties.
7072
public init() {}
7173

Sources/TensorFlow/Core/DataTypes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ extension Int64: TensorFlowIndex {}
8484
public protocol TensorFlowFloatingPoint:
8585
TensorFlowScalar & BinaryFloatingPoint & Differentiable & ElementaryFunctions
8686
where Self.RawSignificand: FixedWidthInteger,
87-
Self == Self.TangentVector,
88-
Self == Self.AllDifferentiableVariables {}
87+
Self == Self.TangentVector {}
8988

9089
extension Float: TensorFlowFloatingPoint {}
9190
extension Double: TensorFlowFloatingPoint {}

0 commit comments

Comments
 (0)