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

Commit 9185cf1

Browse files
committed
Fix ParameterlessLayer conformances.
Associated type inference behavior was changed in swiftlang/swift#32578: derived conformances are now attempted before associated type inference. This broke `ParameterlessLayer`, which relied on a `TangentVector == EmptyTangentVector` same-type constraint to set a default `TangentVector` type witness for conforming types. Add explicit `TangentVector` type witnesses to `ParameterlessLayer`-conforming types to fix this regression.
1 parent 8246abb commit 9185cf1

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

Sources/TensorFlow/Layers/Convolutional.swift

+6
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,8 @@ extension DepthwiseConv2D {
790790

791791
/// A layer for adding zero-padding in the temporal dimension.
792792
public struct ZeroPadding1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
793+
public typealias TangentVector = EmptyTangentVector
794+
793795
/// The padding values along the temporal dimension.
794796
@noDerivative public let padding: (Int, Int)
795797

@@ -821,6 +823,8 @@ public struct ZeroPadding1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer
821823

822824
/// A layer for adding zero-padding in the spatial dimensions.
823825
public struct ZeroPadding2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
826+
public typealias TangentVector = EmptyTangentVector
827+
824828
/// The padding values along the spatial dimensions.
825829
@noDerivative public let padding: ((Int, Int), (Int, Int))
826830

@@ -853,6 +857,8 @@ public struct ZeroPadding2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer
853857

854858
/// A layer for adding zero-padding in the spatial/spatio-temporal dimensions.
855859
public struct ZeroPadding3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
860+
public typealias TangentVector = EmptyTangentVector
861+
856862
/// The padding values along the spatial/spatio-temporal dimensions.
857863
@noDerivative public let padding: ((Int, Int), (Int, Int), (Int, Int))
858864

Sources/TensorFlow/Layers/Core.swift

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import _Differentiation
1919
/// A flatten layer flattens the input when applied without affecting the batch size.
2020
@frozen
2121
public struct Flatten<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
22+
public typealias TangentVector = EmptyTangentVector
23+
2224
/// Creates a flatten layer.
2325
public init() {}
2426

@@ -37,6 +39,8 @@ public struct Flatten<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
3739
/// A reshape layer.
3840
@frozen
3941
public struct Reshape<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
42+
public typealias TangentVector = EmptyTangentVector
43+
4044
/// The target shape.
4145
@noDerivative public var shape: Tensor<Int32>
4246

@@ -70,6 +74,7 @@ public struct Reshape<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
7074

7175
/// A layer that encloses a custom differentiable function.
7276
public struct Function<Input: Differentiable, Output: Differentiable>: ParameterlessLayer {
77+
public typealias TangentVector = EmptyTangentVector
7378
public typealias Body = @differentiable (Input) -> Output
7479

7580
@noDerivative public let body: Body

Sources/TensorFlow/Layers/Dropout.swift

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ extension Tensor where Scalar: TensorFlowFloatingPoint {
3434
/// training time, which helps prevent overfitting.
3535
@frozen
3636
public struct Dropout<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
37+
public typealias TangentVector = EmptyTangentVector
38+
3739
@noDerivative public let probability: Double
3840

3941
/// Creates a dropout layer.
@@ -66,6 +68,8 @@ public struct Dropout<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
6668
///
6769
/// The noise added always has mean zero, but has a configurable standard deviation.
6870
public struct GaussianNoise<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
71+
public typealias TangentVector = EmptyTangentVector
72+
6973
@noDerivative public let standardDeviation: Tensor<Scalar>
7074

7175
/// Creates a Gaussian noise layer
@@ -95,6 +99,8 @@ public struct GaussianNoise<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer
9599
/// Because this is a regularization layer, it is only active during training time. During inference,
96100
/// `GaussianDropout` passes through the input unmodified.
97101
public struct GaussianDropout<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
102+
public typealias TangentVector = EmptyTangentVector
103+
98104
@noDerivative public let probability: Scalar
99105
@noDerivative public let standardDeviation: Scalar
100106

@@ -135,6 +141,8 @@ public struct GaussianDropout<Scalar: TensorFlowFloatingPoint>: ParameterlessLay
135141
/// Source : Self-Normalizing Neural Networks: https://arxiv.org/abs/1706.02515
136142
@frozen
137143
public struct AlphaDropout<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
144+
public typealias TangentVector = EmptyTangentVector
145+
138146
@noDerivative public let probability: Double
139147

140148
/// Initializes an `AlphaDropout` layer with a configurable `probability`.

Sources/TensorFlow/Layers/Pooling.swift

+26
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import _Differentiation
1717
/// A max pooling layer for temporal data.
1818
@frozen
1919
public struct MaxPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
20+
public typealias TangentVector = EmptyTangentVector
21+
2022
/// The size of the sliding reduction window for pooling.
2123
@noDerivative public let poolSize: Int
2224
/// The stride of the sliding window for temporal dimension.
@@ -56,6 +58,8 @@ public struct MaxPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
5658
/// A max pooling layer for spatial data.
5759
@frozen
5860
public struct MaxPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
61+
public typealias TangentVector = EmptyTangentVector
62+
5963
/// The size of the sliding reduction window for pooling.
6064
@noDerivative public let poolSize: (Int, Int, Int, Int)
6165
/// The strides of the sliding window for each dimension of a 4-D input.
@@ -105,6 +109,8 @@ extension MaxPool2D {
105109
/// A max pooling layer for spatial or spatio-temporal data.
106110
@frozen
107111
public struct MaxPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
112+
public typealias TangentVector = EmptyTangentVector
113+
108114
/// The size of the sliding reduction window for pooling.
109115
@noDerivative public let poolSize: (Int, Int, Int, Int, Int)
110116
/// The strides of the sliding window for each dimension of a 5-D input.
@@ -171,6 +177,8 @@ extension MaxPool3D {
171177
/// An average pooling layer for temporal data.
172178
@frozen
173179
public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
180+
public typealias TangentVector = EmptyTangentVector
181+
174182
/// The size of the sliding reduction window for pooling.
175183
@noDerivative public let poolSize: Int
176184
/// The stride of the sliding window for temporal dimension.
@@ -210,6 +218,8 @@ public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
210218
/// An average pooling layer for spatial data.
211219
@frozen
212220
public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
221+
public typealias TangentVector = EmptyTangentVector
222+
213223
/// The size of the sliding reduction window for pooling.
214224
@noDerivative public let poolSize: (Int, Int, Int, Int)
215225
/// The strides of the sliding window for each dimension of a 4-D input.
@@ -259,6 +269,8 @@ extension AvgPool2D {
259269
/// An average pooling layer for spatial or spatio-temporal data.
260270
@frozen
261271
public struct AvgPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
272+
public typealias TangentVector = EmptyTangentVector
273+
262274
/// The size of the sliding reduction window for pooling.
263275
@noDerivative public let poolSize: (Int, Int, Int, Int, Int)
264276
/// The strides of the sliding window for each dimension of a 5-D input.
@@ -325,6 +337,8 @@ extension AvgPool3D {
325337
/// A global average pooling layer for temporal data.
326338
@frozen
327339
public struct GlobalAvgPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
340+
public typealias TangentVector = EmptyTangentVector
341+
328342
/// Creates a global average pooling layer.
329343
public init() {}
330344

@@ -342,6 +356,8 @@ public struct GlobalAvgPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLay
342356
/// A global average pooling layer for spatial data.
343357
@frozen
344358
public struct GlobalAvgPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
359+
public typealias TangentVector = EmptyTangentVector
360+
345361
/// Creates a global average pooling layer.
346362
public init() {}
347363

@@ -359,6 +375,8 @@ public struct GlobalAvgPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLay
359375
/// A global average pooling layer for spatial and spatio-temporal data.
360376
@frozen
361377
public struct GlobalAvgPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
378+
public typealias TangentVector = EmptyTangentVector
379+
362380
/// Creates a global average pooling layer.
363381
public init() {}
364382

@@ -376,6 +394,8 @@ public struct GlobalAvgPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLay
376394
/// A global max pooling layer for temporal data.
377395
@frozen
378396
public struct GlobalMaxPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
397+
public typealias TangentVector = EmptyTangentVector
398+
379399
/// Creates a global max pooling layer.
380400
public init() {}
381401

@@ -396,6 +416,8 @@ public struct GlobalMaxPool1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLay
396416
/// A global max pooling layer for spatial data.
397417
@frozen
398418
public struct GlobalMaxPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
419+
public typealias TangentVector = EmptyTangentVector
420+
399421
/// Creates a global max pooling layer.
400422
public init() {}
401423

@@ -413,6 +435,8 @@ public struct GlobalMaxPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLay
413435
/// A global max pooling layer for spatial and spatio-temporal data.
414436
@frozen
415437
public struct GlobalMaxPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
438+
public typealias TangentVector = EmptyTangentVector
439+
416440
/// Creates a global max pooling layer.
417441
public init() {}
418442

@@ -431,6 +455,8 @@ public struct GlobalMaxPool3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLay
431455
/// Note: `FractionalMaxPool` does not have an XLA implementation, and thus may have performance implications.
432456
@frozen
433457
public struct FractionalMaxPool2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
458+
public typealias TangentVector = EmptyTangentVector
459+
434460
/// Pooling ratios for each dimension of input of shape (batch, height, width, channels).
435461
/// Currently pooling in only height and width is supported.
436462
@noDerivative public let poolingRatio: (Double, Double, Double, Double)

Sources/TensorFlow/Layers/Upsampling.swift

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import _Differentiation
1717
/// An upsampling layer for 1-D inputs.
1818
@frozen
1919
public struct UpSampling1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
20+
public typealias TangentVector = EmptyTangentVector
21+
2022
@noDerivative public let size: Int
2123

2224
/// Creates an upsampling layer.
@@ -43,6 +45,8 @@ public struct UpSampling1D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer
4345
/// An upsampling layer for 2-D inputs.
4446
@frozen
4547
public struct UpSampling2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
48+
public typealias TangentVector = EmptyTangentVector
49+
4650
@noDerivative public let size: Int
4751

4852
/// Creates an upsampling layer.
@@ -70,6 +74,8 @@ public struct UpSampling2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer
7074
/// An upsampling layer for 3-D inputs.
7175
@frozen
7276
public struct UpSampling3D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
77+
public typealias TangentVector = EmptyTangentVector
78+
7379
@noDerivative public let size: Int
7480

7581
/// Creates an upsampling layer.

0 commit comments

Comments
 (0)