Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.
22 changes: 11 additions & 11 deletions Sources/DeepLearning/Layer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public struct TransposedConv2D: Layer {
self.activation = activation
(self.strides.0, self.strides.1) = (Int32(strides.0), Int32(strides.1))
self.padding = padding
self.paddingIndex = padding == .same ? 0 : 1
self.paddingIndex = padding == Padding.same ? 0 : 1
}

/// Returns the output obtained from applying the layer to the given input.
Expand Down Expand Up @@ -836,7 +836,7 @@ public struct MaxPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
/// The stride of the sliding window for temporal dimension.
@noDerivative let stride: Int32
/// The padding algorithm for pooling.
@noDerivative let padding: Padding
@noDerivative let padding: PaddingV1

/// Creates a max pooling layer.
///
Expand All @@ -847,7 +847,7 @@ public struct MaxPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
public init(
poolSize: Int,
stride: Int,
padding: Padding
padding: PaddingV1
) {
self.poolSize = Int32(poolSize)
self.stride = Int32(stride)
Expand Down Expand Up @@ -878,13 +878,13 @@ public struct MaxPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
/// Strides in non-spatial dimensions must be `1`.
@noDerivative let strides: (Int32, Int32, Int32, Int32)
/// The padding algorithm for pooling.
@noDerivative let padding: Padding
@noDerivative let padding: PaddingV1

/// Creates a max pooling layer.
public init(
poolSize: (Int, Int, Int, Int),
strides: (Int, Int, Int, Int),
padding: Padding
padding: PaddingV1
) {
(self.poolSize.0, self.poolSize.1, self.poolSize.2, self.poolSize.3)
= (Int32(poolSize.0), Int32(poolSize.1), Int32(poolSize.2), Int32(poolSize.3))
Expand All @@ -899,7 +899,7 @@ public struct MaxPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
/// - poolSize: Vertical and horizontal factors by which to downscale.
/// - strides: The strides.
/// - padding: The padding.
public init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
public init(poolSize: (Int, Int), strides: (Int, Int), padding: PaddingV1 = .valid) {
self.poolSize = (1, Int32(poolSize.0), Int32(poolSize.1), 1)
self.strides = (1, Int32(strides.0), Int32(strides.1), 1)
self.padding = padding
Expand Down Expand Up @@ -927,7 +927,7 @@ public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
/// The stride of the sliding window for temporal dimension.
@noDerivative let stride: Int32
/// The padding algorithm for pooling.
@noDerivative let padding: Padding
@noDerivative let padding: PaddingV1

/// Creates an average pooling layer.
///
Expand All @@ -938,7 +938,7 @@ public struct AvgPool1D<Scalar: TensorFlowFloatingPoint>: Layer {
public init(
poolSize: Int,
stride: Int,
padding: Padding
padding: PaddingV1
) {
self.poolSize = Int32(poolSize)
self.stride = Int32(stride)
Expand Down Expand Up @@ -969,13 +969,13 @@ public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
/// Strides in non-spatial dimensions must be `1`.
@noDerivative let strides: (Int32, Int32, Int32, Int32)
/// The padding algorithm for pooling.
@noDerivative let padding: Padding
@noDerivative let padding: PaddingV1

/// Creates a average pooling layer.
public init(
poolSize: (Int, Int, Int, Int),
strides: (Int, Int, Int, Int),
padding: Padding
padding: PaddingV1
) {
(self.poolSize.0, self.poolSize.1, self.poolSize.2, self.poolSize.3)
= (Int32(poolSize.0), Int32(poolSize.1), Int32(poolSize.2), Int32(poolSize.3))
Expand All @@ -990,7 +990,7 @@ public struct AvgPool2D<Scalar: TensorFlowFloatingPoint>: Layer {
/// - poolSize: Vertical and horizontal factors by which to downscale.
/// - strides: The strides.
/// - padding: The padding.
public init(poolSize: (Int, Int), strides: (Int, Int), padding: Padding = .valid) {
public init(poolSize: (Int, Int), strides: (Int, Int), padding: PaddingV1 = .valid) {
self.poolSize = (1, Int32(poolSize.0), Int32(poolSize.1), 1)
self.strides = (1, Int32(strides.0), Int32(strides.1), 1)
self.padding = padding
Expand Down
39 changes: 34 additions & 5 deletions Sources/DeepLearning/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public extension Tensor where Scalar: BinaryFloatingPoint {

/// A padding scheme. Used by padding, convolution, and pooling ops.
// @_frozen // SR-9739
public enum Padding {
public enum Padding: Equatable {
/// The "explicit" padding scheme, which is defined by an array indicating the explicit padding
/// sizes at the start and end of each dimension.
case explicit([Int32])
Expand Down Expand Up @@ -133,6 +133,35 @@ public extension Padding {
}
}

/// An older padding scheme. Used by padding, convolution, and pooling ops.
// @_frozen // SR-9739
public enum PaddingV1 {
/// The "valid" padding scheme.
case valid
/// The "same" padding scheme.
case same
}

public extension PaddingV1 {
@inlinable
var raw: Raw.Padding {
switch self {
case .explicit: return .explicit
case .same: return .same
case .valid: return .valid
}
}

@inlinable
internal var explicitPaddings: [Int32] {
switch self {
case .explicit(let paddings): return paddings
case .same: return []
case .valid: return []
}
}
}

public extension Tensor where Scalar: TensorFlowFloatingPoint {
/// TensorFlow builtin conv2d gradient helper for the input.
@inlinable
Expand Down Expand Up @@ -232,7 +261,7 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint {
internal func _vjpMaxPooled(
kernelSize: (Int32, Int32, Int32, Int32),
strides: (Int32, Int32, Int32, Int32),
padding: Padding
padding: PaddingV1
) -> (Tensor, (Tensor) -> Tensor) {
// TODO: Currently this is not higher order differentiable. Redefine in
// closed form.
Expand All @@ -254,7 +283,7 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint {
internal func _vjpAveragePooled(
kernelSize: (Int32, Int32, Int32, Int32),
strides: (Int32, Int32, Int32, Int32),
padding: Padding
padding: PaddingV1
) -> (Tensor, (Tensor) -> Tensor) {
// TODO: Currently this is not higher order differentiable. Redefine in
// closed form.
Expand Down Expand Up @@ -317,7 +346,7 @@ public extension Tensor where Scalar: FloatingPoint {
func maxPooled(
kernelSize: (Int32, Int32, Int32, Int32),
strides: (Int32, Int32, Int32, Int32),
padding: Padding
padding: PaddingV1
) -> Tensor {
return Raw.maxPoolV2(
self,
Expand All @@ -344,7 +373,7 @@ public extension Tensor where Scalar: FloatingPoint {
func averagePooled(
kernelSize: (Int32, Int32, Int32, Int32),
strides: (Int32, Int32, Int32, Int32),
padding: Padding
padding: PaddingV1
) -> Tensor {
return Raw.avgPool(
value: self,
Expand Down