Skip to content

Commit

Permalink
Fix ParameterlessLayer conformances.
Browse files Browse the repository at this point in the history
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.

This workaround is forward- and backward-compatible, but makes code more
verbose.
  • Loading branch information
dan-zheng committed Jul 3, 2020
1 parent 23e0a76 commit 28d1aaf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions FastStyleTransfer/Layers/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import TensorFlow

/// A 2-D layer applying padding with reflection over a mini-batch.
public struct ReflectionPad2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector

/// The padding values along the spatial dimensions.
@noDerivative public let padding: ((Int, Int), (Int, Int))

Expand Down Expand Up @@ -39,6 +41,8 @@ public struct ReflectionPad2D<Scalar: TensorFlowFloatingPoint>: ParameterlessLay

/// A layer applying `relu` activation function.
public struct ReLU<Scalar: TensorFlowFloatingPoint>: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector

/// Returns the output obtained from applying the layer to the given input.
///
/// - Parameter input: The input to the layer.
Expand Down
2 changes: 2 additions & 0 deletions Models/ImageClassification/ShuffleNetV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import TensorFlow
// Ningning Ma, Xiangyu Zhang, Hai-Tao Zheng, Jian Sun

public struct ChannelShuffle: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector

@noDerivative public var groups: Int

public init(groups: Int = 2) {
Expand Down
2 changes: 2 additions & 0 deletions Models/Text/GPT2/TransformerLM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func _vjpCausallyMasked(_ dotProducts: Tensor<Float>, enable: Bool)
}

struct Attention: ParameterlessLayer {
typealias TangentVector = EmptyTangentVector

@noDerivative var dropout: Dropout<Float>
@noDerivative var scale: Tensor<Float>
@noDerivative var causal: Bool
Expand Down
2 changes: 2 additions & 0 deletions pix2pix/Layers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import TensorFlow
import Foundation

public struct Identity: ParameterlessLayer {
public typealias TangentVector = EmptyTangentVector

@differentiable
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
input
Expand Down

0 comments on commit 28d1aaf

Please sign in to comment.