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

NFC: fix warnings related to VectorProtocol.VectorSpaceScalar. #1178

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 24 additions & 106 deletions Sources/TensorFlow/Layers/Sequential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ import _Differentiation
/// ````
public struct Sequential<Layer1: Module, Layer2: Layer>: Module
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kokoro CI fails (for TENSORFLOW_USE_STANDARD_TOOLCHAIN=NO):

/swift-apis/Sources/TensorFlow/Layers/Sequential.swift:68:1: error: type 'Sequential<Layer1, Layer2>.TangentVector' does not conform to protocol 'VectorProtocol'
}
^
Swift.VectorProtocol:2:20: note: protocol requires nested type 'VectorSpaceScalar'; do you want to add it?
    associatedtype VectorSpaceScalar : AdditiveArithmetic
                   ^

It may be possible to fix this by adding an explicit typealias VectorSpaceScalar = Float definition for Sequential.TangentVector.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried doing this in 34fca6a but it didn't work, so I'm not sure what to try next.

Does anyone else have ideas? @BradLarson @compnerd

One option is to bite the bullet and drop support for the old baked-in-TensorFlow toolchains (which is what's causing the failure). I think that's a good option if we and the community are all on-board with it. That also allows us to drop custom internal support for CI and building OSS toolchains - we can just use GitHub Actions, which is much simpler.

where
Layer1.Output == Layer2.Input,
Layer1.TangentVector.VectorSpaceScalar == Layer2.TangentVector.VectorSpaceScalar
Layer1.Output == Layer2.Input
{
public var layer1: Layer1
public var layer2: Layer2
Expand All @@ -75,45 +74,38 @@ extension Sequential: Layer where Layer1: Layer {
}
}

extension Sequential.TangentVector: VectorProtocol {
/// - Note: this typealias declaration is necessary for building with old non-stock toolchains
/// where `VectorProtocol.VectorSpaceScalar` is an associated type and not a typealias
/// declaration.
typealias VectorSpaceScalar = Float
}

/// A layer that sequentially composes 3 layers.
public typealias Sequential3<L1: Module, L2: Layer, L3: Layer> = Sequential<L1, Sequential<L2, L3>>
where
L1.Output == L2.Input, L2.Output == L3.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar
L1.Output == L2.Input, L2.Output == L3.Input

/// A layer that sequentially composes 4 layers.
public typealias Sequential4<L1: Module, L2: Layer, L3: Layer, L4: Layer> = Sequential<
L1, Sequential<L2, Sequential<L3, L4>>
>
where
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input

/// A layer that sequentially composes 5 layers.
public typealias Sequential5<L1: Module, L2: Layer, L3: Layer, L4: Layer, L5: Layer> = Sequential<
L1, Sequential<L2, Sequential<L3, Sequential<L4, L5>>>
>
where
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input, L4.Output == L5.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input, L4.Output == L5.Input

/// A layer that sequentially composes 6 layers.
public typealias Sequential6<L1: Module, L2: Layer, L3: Layer, L4: Layer, L5: Layer, L6: Layer> =
Sequential<L1, Sequential<L2, Sequential<L3, Sequential<L4, Sequential<L5, L6>>>>>
where
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input, L4.Output == L5.Input,
L5.Output == L6.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar
L5.Output == L6.Input

/// A layer that sequentially composes 7 layers.
public typealias Sequential7<
Expand All @@ -123,13 +115,7 @@ public typealias Sequential7<
>
where
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input, L4.Output == L5.Input,
L5.Output == L6.Input, L6.Output == L7.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar
L5.Output == L6.Input, L6.Output == L7.Input

/// A layer that sequentially composes 8 layers.
public typealias Sequential8<
Expand All @@ -140,14 +126,7 @@ public typealias Sequential8<
>
where
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input, L4.Output == L5.Input,
L5.Output == L6.Input, L6.Output == L7.Input, L7.Output == L8.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar,
L7.TangentVector.VectorSpaceScalar == L8.TangentVector.VectorSpaceScalar
L5.Output == L6.Input, L6.Output == L7.Input, L7.Output == L8.Input

/// A layer that sequentially composes 9 layers.
public typealias Sequential9<
Expand All @@ -163,15 +142,7 @@ public typealias Sequential9<
>
where
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input, L4.Output == L5.Input,
L5.Output == L6.Input, L6.Output == L7.Input, L7.Output == L8.Input, L8.Output == L9.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar,
L7.TangentVector.VectorSpaceScalar == L8.TangentVector.VectorSpaceScalar,
L8.TangentVector.VectorSpaceScalar == L9.TangentVector.VectorSpaceScalar
L5.Output == L6.Input, L6.Output == L7.Input, L7.Output == L8.Input, L8.Output == L9.Input

/// A layer that sequentially composes 10 layers.
public typealias Sequential10<
Expand All @@ -192,16 +163,7 @@ public typealias Sequential10<
where
L1.Output == L2.Input, L2.Output == L3.Input, L3.Output == L4.Input, L4.Output == L5.Input,
L5.Output == L6.Input, L6.Output == L7.Input, L7.Output == L8.Input, L8.Output == L9.Input,
L9.Output == L10.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar,
L7.TangentVector.VectorSpaceScalar == L8.TangentVector.VectorSpaceScalar,
L8.TangentVector.VectorSpaceScalar == L9.TangentVector.VectorSpaceScalar,
L9.TangentVector.VectorSpaceScalar == L10.TangentVector.VectorSpaceScalar
L9.Output == L10.Input

@resultBuilder
public struct LayerBuilder {
Expand All @@ -218,9 +180,7 @@ public struct LayerBuilder {
-> Sequential<L1, Sequential<L2, L3>>
where
L1.Output == L2.Input,
L2.Output == L3.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar
L2.Output == L3.Input
{
Sequential(l1, Sequential(l2, l3))
}
Expand All @@ -235,10 +195,7 @@ public struct LayerBuilder {
where
L1.Output == L2.Input,
L2.Output == L3.Input,
L3.Output == L4.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar
L3.Output == L4.Input
{
Sequential(l1, Sequential(l2, Sequential(l3, l4)))
}
Expand All @@ -255,11 +212,7 @@ public struct LayerBuilder {
L1.Output == L2.Input,
L2.Output == L3.Input,
L3.Output == L4.Input,
L4.Output == L5.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar
L4.Output == L5.Input
{
Sequential(l1, Sequential(l2, Sequential(l3, Sequential(l4, l5))))
}
Expand All @@ -278,12 +231,7 @@ public struct LayerBuilder {
L2.Output == L3.Input,
L3.Output == L4.Input,
L4.Output == L5.Input,
L5.Output == L6.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar
L5.Output == L6.Input
{
Sequential(l1, Sequential(l2, Sequential(l3, Sequential(l4, Sequential(l5, l6)))))
}
Expand All @@ -306,13 +254,7 @@ public struct LayerBuilder {
L3.Output == L4.Input,
L4.Output == L5.Input,
L5.Output == L6.Input,
L6.Output == L7.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar
L6.Output == L7.Input
{
Sequential(
l1, Sequential(l2, Sequential(l3, Sequential(l4, Sequential(l5, Sequential(l6, l7))))))
Expand Down Expand Up @@ -341,14 +283,7 @@ public struct LayerBuilder {
L4.Output == L5.Input,
L5.Output == L6.Input,
L6.Output == L7.Input,
L7.Output == L8.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar,
L7.TangentVector.VectorSpaceScalar == L8.TangentVector.VectorSpaceScalar
L7.Output == L8.Input
{
Sequential(
l1,
Expand Down Expand Up @@ -384,15 +319,7 @@ public struct LayerBuilder {
L5.Output == L6.Input,
L6.Output == L7.Input,
L7.Output == L8.Input,
L8.Output == L9.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar,
L7.TangentVector.VectorSpaceScalar == L8.TangentVector.VectorSpaceScalar,
L8.TangentVector.VectorSpaceScalar == L9.TangentVector.VectorSpaceScalar
L8.Output == L9.Input
{
Sequential(
l1,
Expand Down Expand Up @@ -438,16 +365,7 @@ public struct LayerBuilder {
L6.Output == L7.Input,
L7.Output == L8.Input,
L8.Output == L9.Input,
L9.Output == L10.Input,
L1.TangentVector.VectorSpaceScalar == L2.TangentVector.VectorSpaceScalar,
L2.TangentVector.VectorSpaceScalar == L3.TangentVector.VectorSpaceScalar,
L3.TangentVector.VectorSpaceScalar == L4.TangentVector.VectorSpaceScalar,
L4.TangentVector.VectorSpaceScalar == L5.TangentVector.VectorSpaceScalar,
L5.TangentVector.VectorSpaceScalar == L6.TangentVector.VectorSpaceScalar,
L6.TangentVector.VectorSpaceScalar == L7.TangentVector.VectorSpaceScalar,
L7.TangentVector.VectorSpaceScalar == L8.TangentVector.VectorSpaceScalar,
L8.TangentVector.VectorSpaceScalar == L9.TangentVector.VectorSpaceScalar,
L9.TangentVector.VectorSpaceScalar == L10.TangentVector.VectorSpaceScalar
L9.Output == L10.Input
{
Sequential(
l1,
Expand Down
9 changes: 2 additions & 7 deletions Sources/TensorFlow/Layers/Sequential.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ import _Differentiation
/// }
/// ````
public struct Sequential<Layer1: Module, Layer2: Layer>: Module
where Layer1.Output == Layer2.Input,
Layer1.TangentVector.VectorSpaceScalar == Layer2.TangentVector.VectorSpaceScalar {
where Layer1.Output == Layer2.Input {
public var layer1: Layer1
public var layer2: Layer2

Expand Down Expand Up @@ -94,8 +93,7 @@ def sequential_type(arity):
%for n in SEQUENTIAL_ARITY_RANGE:
/// A layer that sequentially composes ${n} layers.
public typealias Sequential${n}<${generic_parameters(n, ", ")}> = ${sequential_type(n)}
where ${", ".join(["L{}.Output == L{}.Input".format(i, i+1) for i in range(1, n)])},
${", ".join(["L{}.TangentVector.VectorSpaceScalar == L{}.TangentVector.VectorSpaceScalar".format(i, i+1) for i in range(1, n)])}
where ${", ".join(["L{}.Output == L{}.Input".format(i, i+1) for i in range(1, n)])}

%end

Expand All @@ -112,9 +110,6 @@ public struct LayerBuilder {
>(${", ".join(["_ l{}: L{}".format(i, i) for i in range(1, n+1)])})
-> ${sequential_type(n)} where
${",\n ".join(["L{}.Output == L{}.Input".format(i, i+1)
for i in range(1, n)])},
${",\n ".join(["L{}.TangentVector.VectorSpaceScalar == ".format(i) +
"L{}.TangentVector.VectorSpaceScalar".format(i+1)
for i in range(1, n)])}
{
${"".join(["Sequential(l{}, ".format(i) for i in range(1, n)])}l${n}${"".join([")" for _ in range(1, n)])}
Expand Down
21 changes: 7 additions & 14 deletions Sources/TensorFlow/Optimizers/MomentumBased.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import Numerics
public class RMSProp<Model: Differentiable>: Optimizer
where
Model.TangentVector: VectorProtocol & PointwiseMultiplicative
& ElementaryFunctions & KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
& ElementaryFunctions & KeyPathIterable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nifty Vim search-and-replace command: :%s/,\_s*Model.TangentVector.VectorSpaceScalar == Float//g.

Apparently the \_s pattern matches a whitespace, tab, or newline character (source).

{
public typealias Model = Model
/// The learning rate.
Expand Down Expand Up @@ -108,8 +107,7 @@ where
public class AdaGrad<Model: Differentiable>: Optimizer
where
Model.TangentVector: VectorProtocol & PointwiseMultiplicative
& ElementaryFunctions & KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
& ElementaryFunctions & KeyPathIterable
{
public typealias Model = Model
/// The learning rate.
Expand Down Expand Up @@ -169,8 +167,7 @@ where
public class AdaDelta<Model: Differentiable>: Optimizer
where
Model.TangentVector: VectorProtocol & PointwiseMultiplicative
& ElementaryFunctions & KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
& ElementaryFunctions & KeyPathIterable
{
public typealias Model = Model
/// The learning rate.
Expand Down Expand Up @@ -319,8 +316,7 @@ where
public class Adam<Model: Differentiable>: Optimizer
where
Model.TangentVector: VectorProtocol & PointwiseMultiplicative
& ElementaryFunctions & KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
& ElementaryFunctions & KeyPathIterable
{
public typealias Model = Model
/// The learning rate.
Expand Down Expand Up @@ -403,8 +399,7 @@ where
public class AdaMax<Model: Differentiable & KeyPathIterable>: Optimizer
where
Model.TangentVector: VectorProtocol & PointwiseMultiplicative & ElementaryFunctions
& KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
& KeyPathIterable
{
public typealias Model = Model
/// The learning rate.
Expand Down Expand Up @@ -489,8 +484,7 @@ where
public class AMSGrad<Model: Differentiable & KeyPathIterable>: Optimizer
where
Model.TangentVector: VectorProtocol & PointwiseMultiplicative & ElementaryFunctions
& KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
& KeyPathIterable
{
public typealias Model = Model
/// The learning rate.
Expand Down Expand Up @@ -582,8 +576,7 @@ where
public class RAdam<Model: Differentiable>: Optimizer
where
Model.TangentVector: VectorProtocol & PointwiseMultiplicative & ElementaryFunctions
& KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
& KeyPathIterable
{
public typealias Model = Model
/// The learning rate.
Expand Down
3 changes: 1 addition & 2 deletions Sources/TensorFlow/Optimizers/SGD.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import Numerics
/// (Nesterov, 1983)
public class SGD<Model: Differentiable>: Optimizer
where
Model.TangentVector: VectorProtocol & ElementaryFunctions & KeyPathIterable,
Model.TangentVector.VectorSpaceScalar == Float
Model.TangentVector: VectorProtocol & ElementaryFunctions & KeyPathIterable
{
public typealias Model = Model
/// The learning rate.
Expand Down