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 / AdamOptimizer.swift
Last active November 17, 2018 23:22
Adam optimizer example
struct MNISTParameters : ParameterAggregate {
var w1 = Tensor<Float>(randomNormal: [784, 30])
var w2 = Tensor<Float>(randomNormal: [30, 10])
// Compiler-synthesized:
// static var allKeyPaths: [WritableKeyPath<MNISTParameters, Tensor<Float>>] {
// return [\MNISTParameters.w1, \MNISTParameters.w2]
// }
// Learn more about key paths here: https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md
}
@dan-zheng
dan-zheng / artificial-loc.swift
Created July 31, 2018 01:54
SIL verification failed, graph_op has artificial location
import TensorFlow
func train(iterationCount: Int) {
let images = Tensor<Float>(ones: [1000, 784])
let batchSize = Float(images.shape[0])
print("Begin training for \(iterationCount) iterations.")
for _ in 0...iterationCount {
let bound = Int32(batchSize)/25
@dan-zheng
dan-zheng / max-pool-graph-op.txt
Created July 30, 2018 18:28
Max pooling produces send/recv
%24 = graph_op "MaxPoolV2,i,i,i"(%4 : $TensorHandle<Float>, %14 : $TensorHandle<Int32>, %22 : $TensorHandle<Int32>) {T: $Float, padding: "SAME", data_format: "NHWC", __device: "/device:CPU:0"} : $TensorHandle<Float>
As you can see, kernel size and strides are SSA operands of `graph_op`, not constant attributes.
This is because of how MaxPool is defined:
REGISTER_OP("MaxPoolV2")
.Attr(
"T: {half, bfloat16, float, double, int32, int64, uint8, int16, int8, "
"uint16, qint8} = DT_FLOAT")
.Attr(GetPaddingAttrString())
@dan-zheng
dan-zheng / unexpected-send-recv.swift
Created July 28, 2018 01:28
Unexpected send/receive, scalar promotion deficiency
import TensorFlow
func test() {
for _ in 0...10 {
let x = Tensor<Float>(ones: [2, 2])
_ = x.reshaped(toShape: Tensor<Int32>([4, Int32(1 * 1)]))
// Alternative 1: literal `1` avoids send/receive.
// _ = x.reshaped(toShape: Tensor<Int32>([4, 1]))
// Alternative 2: avoiding `Int32` avoids send/receive.
// RUN: %target-swift-frontend -Xllvm -tf-dump-intermediates -O -emit-sil -verify %s | %FileCheck %s
import TensorFlow
// This test is intended to verify that all of the operations end up in the
// graph: that there are no host/accelerator copies generated. This tests a
// combination of the partitioning pass being able to recognize various forms,
// but also checks that certain ops implementations are promotable as well.
// Please keep it so no errors or warnings are generated by functions in this
@dan-zheng
dan-zheng / param.swift
Created July 15, 2018 03:55
Check if AD works with parameter update SIL
struct Model {
var w: Float
struct Parameters {
var w: Float
}
var allParameters: Parameters {
get {
return Parameters(w: w)
import TensorFlow
func test(_ array: [Tensor<Float>]) {
for (i, x) in array.enumerated() {
print(x + x)
}
}
test([Tensor(1), Tensor(2)])
@dan-zheng
dan-zheng / crash.swift
Last active July 24, 2018 20:54
SIL verification failed: Basic block contains a non-contiguous lexical scope at -Onone
// crash.swift
import TensorFlow
func test() {
print(Tensor(1))
}
@dan-zheng
dan-zheng / compiler-output.txt
Created July 13, 2018 22:11
Swift struct stored property
$ swiftc -dump-ast stored.swift
(source_file
(struct_decl range=[stored.swift:1:1 - line:3:1] "Model" interface type='Model.Type' access=internal non-resilient
(pattern_binding_decl range=[stored.swift:2:3 - line:2:10]
(pattern_typed type='Float'
(pattern_named type='Float' 'w')
(type_ident
(component id='Float' bind=Swift.(file).Float))))
(var_decl range=[stored.swift:2:7 - line:2:7] "w" type='Float' interface type='Float' access=internal storage_kind=stored_with_trivial_accessors
(accessor_decl implicit range=[stored.swift:2:7 - line:2:7] 'anonname=0x7f83f5055fd0' interface type='(Model) -> () -> Float' access=internal get_for=w
@dan-zheng
dan-zheng / codable.swift
Created July 13, 2018 21:54
Codable conformance for Tensor crashes
import TensorFlow
import Foundation
func testTensor() throws {
let tensor = Tensor<Int32>(shape: [2, 3], scalars: Array(1...6))
let encoder = JSONEncoder()
let data = try encoder.encode(tensor)
}
try testTensor()