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

Commit ca669ca

Browse files
authored
Fix "unable to type-check this expression in reasonable time" errors. (#120)
Fix compilation via `swift build` using a post-merge toolchain (`swift-DEVELOPMENT-SNAPSHOT-2019-05-02-a`).
1 parent c7c9471 commit ca669ca

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Sources/DeepLearning/Operators.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ public extension Tensor where Scalar: TensorFlowFloatingPoint {
4747
let squaredDiff: Tensor = Raw.squaredDifference(self, mean)
4848
let variance = squaredDiff.mean(alongAxes: axis)
4949

50-
let diff = self - mean
50+
let diff = self - mean
5151
let inv = rsqrt(variance + epsilon)
5252
let norm = diff * inv
5353

54-
let dNorm = v * scale
54+
let dNorm = v * scale
5555
let dVariance = -(dNorm * diff).sum(alongAxes: axis) / 2 * pow(inv, -3)
56-
let dMean = (-dNorm * inv).sum(alongAxes: axis) +
57-
dVariance * (-diff * 2).mean(alongAxes: axis)
56+
// Note: `dMean` is split into two lines to avoid the "compiler is unable to type-check
57+
// this expression in reasonable time" error.
58+
var dMean = (-dNorm * inv).sum(alongAxes: axis)
59+
dMean = dMean + dVariance * (-diff * 2).mean(alongAxes: axis)
5860
let dOffset = v.sum(alongAxes: axis)
5961
let dScale = (norm * v).sum(alongAxes: axis)
6062
let dim = Tensor(Tensor<Int32>(self.shapeTensor[axis]))

Sources/DeepLearning/Optimizer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ public class Adam<Model: Layer>: Optimizer
105105
along direction: Model.AllDifferentiableVariables) {
106106
step += 1
107107
let learningRate = self.learningRate * 1 / (1 + decay * Float(step))
108-
let stepSize = learningRate * (sqrt(1 - pow(beta2, Float(step))) /
109-
(1 - pow(beta1, Float(step))))
108+
// Note: `stepSize` is split into two lines to avoid the "compiler is unable to type-check
109+
// this expression in reasonable time" error.
110+
var stepSize = learningRate * sqrt(1 - pow(beta2, Float(step)))
111+
stepSize = stepSize / (1 - pow(beta1, Float(step)))
110112
// Update Float & Double Tensor variables.
111113
for kp in model.recursivelyAllWritableKeyPaths(to: Tensor<Float>.self) {
112114
firstMoments[keyPath: kp] =

0 commit comments

Comments
 (0)