Skip to content

Instantly share code, notes, and snippets.

@marcrasi
marcrasi / XXXX-constexpr.md
Last active April 19, 2024 21:10
Compile Time Constant Expressions for Swift
protocol Proto {
@differentiable(reverse)
func f(_ x: Float) -> Float
}
func callF<T: Proto>(_ t: T, _ x: Float) -> Float {
return t.f(x)
}
func gradFWrtX<T: Proto>(_ t: T, at x: Float) -> Float {
sil_stage canonical
import Builtin
import Swift
import SwiftShims
protocol Proto {
@differentiable(reverse)
func f(_ x: Float) -> Float
}
[1/33] Performing build step for 'libdispatch'
ninja: no work to do.
[2/27] Compiling /usr/local/google/home/marcrasi/swift-base/build/buildbot_linux/swift-linux-x86_64/stdlib/public/core/linux/x86_64/Swift.o
FAILED: stdlib/public/core/linux/x86_64/Swift.o
cd /usr/local/google/home/marcrasi/swift-base/build/buildbot_linux/swift-linux-x86_64/stdlib/public/core && /usr/bin/python /usr/local/google/home/marcrasi/swift-base/swift/utils/line-directive @/usr/local/google/home/marcrasi/swift-base/build/buildbot_linux/swift-linux-x86_64/stdlib/public/core/OdCaQ.txt -- /usr/local/google/home/marcrasi/swift-base/build/buildbot_linux/swift-linux-x86_64/./bin/swiftc -c -sdk / -target x86_64-unknown-linux-gnu -resource-dir /usr/local/google/home/marcrasi/swift-base/build/buildbot_linux/swift-linux-x86_64/./lib/swift -O -I /usr/local/google/home/marcrasi/swift-base/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux/x86_64 -module-cache-path /usr/local/google/home/marcrasi/swift-base/build/buildbot_linux/swift-linux
# -*- coding: utf-8 -*-
"""split_into_batches_oom.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1UWb-URpxZgdkTICBZlxGWfJr7U35uvJk
"""
# %enableCompletion
import TensorFlow
struct ConvBN: Layer {
var conv: Conv2D<Float>
var norm: BatchNorm<Float>
public init(filterShape: (Int, Int, Int, Int), strides: (Int, Int) = (1, 1),
padding: Padding, learningPhaseIndicator: LearningPhaseIndicator) {
self.conv = Conv2D(
filterShape: filterShape, strides: strides, padding: padding)
@marcrasi
marcrasi / installing-swift-packages.md
Created February 22, 2019 04:30
Installing Swift Packages in Colab

First the package needs a dynamic library product. You might have to modify the package to have one. Here's an example.

Now make a notebook that clones the package, builds the package, and copies the build artifacts into the right places. Here's an example.

In a Swift notebook, dlopen the shared library and then import the module. For example:

import Glibc
dlopen("/swift/toolchain/usr/lib/swift/linux/libDeckOfPlayingCards.so", RTLD_NOW)
import DeckOfPlayingCards
public struct ProductSpaceVector<Element> {
public var elements: [Element]
public init(_ elements: [Element]) { self.elements = elements }
}
extension ProductSpaceVector : Equatable where Element : Equatable {
public static func == (lhs: ProductSpaceVector, rhs: ProductSpaceVector) -> Bool {
return lhs.elements == rhs.elements
}
}
public struct ProductSpaceVector<Element> {
public var elements: [Element]
public init(_ elements: [Element]) { self.elements = elements }
}
extension ProductSpaceVector : Equatable where Element : Equatable {
public static func == (lhs: ProductSpaceVector, rhs: ProductSpaceVector) -> Bool {
return lhs.elements == rhs.elements
}
}
extension Array where Element: Differentiable {
/// Views the array as the differentiable product manifold of `Element` with itself `count` times.
public struct DifferentiableView: Differentiable {
/// The array that we are viewing.
public var base: [Element]
/// Construct a view of the given array.
public init(_ base: [Element]) { self.base = base }
// MARK: - Differentiable conformance.