Skip to content

Instantly share code, notes, and snippets.

View dan-zheng's full-sized avatar
🥛
우유맛

Dan Zheng dan-zheng

🥛
우유맛
View GitHub Profile
@dan-zheng
dan-zheng / README.md
Last active April 29, 2020 17:43
Sequential layer shape inference

Keras-style Sequential in Swift for TensorFlow.


The current Sequential in Swift for TensorFlow does not support shape inference. Previous layers' output sizes are redundantly repeated as next layers' input sizes. This increases user burden and adds room for error.

let modelOld = Sequential {
@dan-zheng
dan-zheng / README.md
Last active January 13, 2020 13:51
Debug tensorflow/swift-apis compilation time

tensorflow/swift-apis compilation is painfully slow. It's probably due to type-checking.

Swift compilation debugging tips here. swift build -Xswiftc -Xfrontend -Xswiftc -debug-time-function-bodies results below.

# Worst offenders, time in milliseconds.
(1767, "global function 'gelu'", '/Users/danielzheng/swift-apis/Sources/TensorFlow/Operators/Math.swift:1225:13')
(1767, "global function 'gelu'", '/Users/danielzheng/swift-apis/Sources/TensorFlow/Operators/Math.swift:1225:13')
(1767, "global function 'gelu'", '/Users/danielzheng/swift-apis/Sources/TensorFlow/Operators/Math.swift:1225:13')
@dan-zheng
dan-zheng / README.md
Last active January 4, 2020 19:47
Retroactive derivative registration in Swift (https://bugs.swift.org/browse/TF-866)

Retroactive derivative registration: register derivatives for functions in other modules.

Previously:

  • If module A defines func foo, then its derivatives must be in the same module.

With retroactive derivative registration:

  • If module A defines func foo;
  • And if module B imports module A and defines @differentiating(foo) func derivativeFoo:
  • Then module C can import modules A and B and differentiate func foo (e.g. via differentiation APIs).
@dan-zheng
dan-zheng / loop.py
Last active June 18, 2019 06:55
Loop differentiation
import torch
import tangent
from tangent import grad
def nested_loop(x):
outer = x
for _ in range(1, 3):
outer = outer * x
inner = outer
@dan-zheng
dan-zheng / _errors.md
Last active June 15, 2019 09:22
Generic Adam optimizer experiment.

I tried to define a generic Adam optimizer using ElementaryFunctions, Differentiable, and VectorProtocol (latter two are being incubated on tensorflow branch).

It doesn't work due to missing scalar-vector and vector-vector operations.

$ swift adam.swift
adam.swift:82:95: error: binary operator '+' cannot be applied to operands of type 'Model.TangentVector' and 'Model.TangentVector.VectorSpaceScalar'
        model.move(along: -stepSize * firstMoments / (Model.TangentVector.sqrt(secondMoments) + epsilon))
                                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~
adam.swift:82:95: note: expected an argument list of type '(Self, Self)'
@dan-zheng
dan-zheng / README.md
Last active March 27, 2019 08:20
Python interoperability in Swift

Python interoperability

Check out this tutorial demonstrating Python interoperability in Swift.

Python library version details

This function is intended to set the Python version before any references to the global PythonInterface called Python - it cannot be used to switch Python versions dynamically.

@dan-zheng
dan-zheng / hello.swift
Last active February 18, 2019 01:19
POC API change to avoid manual specification of super long generic type parameter
import TensorFlow
extension SGD {
// Take `Model` metatype as an argument, so it doesn't need to be written out
// explicitly as a generic parameter.
// IMPORTANT: This API change might not be desirable, because usage involves
// getting dynamic type via `type(of:)`.
convenience init(_ modelType: Model.Type, learningRate: Scalar) {
self.init(learningRate: learningRate)
}
## Building Swift
./swift/utils/update-checkout --skip-repository swift --clone --scheme tensorflow
./swift/utils/build-script -x -R --debug-swift --skip-build-benchmarks --stdlib-deployment-targets=macosx-x86_64 2>&1 | tee log.txt
./swift/utils/build-script -R --skip-build-benchmarks --stdlib-deployment-targets=macosx-x86_64 --enable-tensorflow --no-swift-stdlib-assertions --reconfigure
import TensorFlow
struct Model: Parameterized, Differentiable {
@TFParameter var w: Tensor<Float>
func tangentVector(from cotangent: Parameters) -> Parameters {
return cotangent
}
}
let model = Model(w: Tensor<Float>(zeros: [5]))
let pb = pullback(at: model) { m in m.w }
@dan-zheng
dan-zheng / _DynamicParameters.md
Last active December 15, 2019 06:03
Dynamic parameters and `allKeyPaths` synthesis