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
@marcrasi
marcrasi / aunifying.md
Last active February 25, 2021 13:25
Unifying the differentiation modes

Unifying the differentiation modes

Today, Swift AD has two separate transforms that produce forward mode derivatives (JVPs) and reverse mode derivatives (VJPs).

We can generalize this to a single transform that produces a single "derivative function" that can evaluate derivatives in both modes.

The "derivative function" is generic over a "derivative type" that determines the derivative mode.

The advantages over the existing system are:

@marcrasi
marcrasi / gist:59c540994fd1644300c6def0b8453a08
Created August 8, 2020 17:00
generalized tangent vectors
import TensorFlow
// MARK: - Protocols
protocol Vector {
func scaled(by factor: Float) -> Self
func adding(_ other: Self) -> Self
static var zero: Self { get }
}
import TensorFlow
// MARK: Example function and its transformed version.
func cube(_ x: Float) -> Float {
return x.product(x).product(x)
}
func cubeT<A: WrapsFloat>(_ x: A) -> A {
return x.product(x).product(x)
Don't use this. ArrayReader here is better: https://groups.google.com/a/tensorflow.org/g/swift/c/Xo5YmLIt12s/m/OM8n6J4TAQAJ
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.