Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update swift-rl to compile with modern S4TF toolchains. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 4 additions & 29 deletions Sources/ReinforcementLearning/Utilities/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,35 +304,9 @@ extension KeyPathIterable {
extension KeyPathIterable where Self: Differentiable, Self.TangentVector: KeyPathIterable {
// TODO: Differentiable `stack` and `unstacked`.

@differentiable(wrt: self, vjp: _vjpFlattenedBatch)
public func flattenedBatch(outerDimCount: Int) -> Self {
var result = self
for kp in result.recursivelyAllWritableKeyPaths(to: Tensor<Float>.self) {
result[keyPath: kp] = result[keyPath: kp].flattenedBatch(outerDimCount: outerDimCount)
}
for kp in result.recursivelyAllWritableKeyPaths(to: Tensor<Double>.self) {
result[keyPath: kp] = result[keyPath: kp].flattenedBatch(outerDimCount: outerDimCount)
}
return result
}

@differentiable(wrt: self, vjp: _vjpUnflattenedBatch)
public func unflattenedBatch(outerDims: [Int]) -> Self {
var result = self
for kp in result.recursivelyAllWritableKeyPaths(to: Tensor<Float>.self) {
result[keyPath: kp] = result[keyPath: kp].unflattenedBatch(outerDims: outerDims)
}
for kp in result.recursivelyAllWritableKeyPaths(to: Tensor<Double>.self) {
result[keyPath: kp] = result[keyPath: kp].unflattenedBatch(outerDims: outerDims)
}
return result
}
}

internal extension KeyPathIterable
where Self: Differentiable, Self.TangentVector: KeyPathIterable {
@derivative(of: flattenedBatch(outerDimCount:), wrt: self)
@usableFromInline
func _vjpFlattenedBatch(outerDimCount: Int) -> (Self, (TangentVector) -> TangentVector) {
func _vjpFlattenedBatch(outerDimCount: Int) -> (value: Self, pullback: (TangentVector) -> TangentVector) {
// TODO: This is very hacky.
var outerDims = [Int]()
for kp in recursivelyAllWritableKeyPaths(to: Tensor<Float>.self) {
Expand All @@ -352,8 +326,9 @@ where Self: Differentiable, Self.TangentVector: KeyPathIterable {
})
}

@derivative(of: unflattenedBatch(outerDims:), wrt: self)
@usableFromInline
func _vjpUnflattenedBatch(outerDims: [Int]) -> (Self, (TangentVector) -> TangentVector) {
func _vjpUnflattenedBatch(outerDims: [Int]) -> (value: Self, pullback: (TangentVector) -> TangentVector) {
let result = unflattenedBatch(outerDims: outerDims)
return (result, { seed in
var result = seed
Expand Down