-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Serialization] Implement serialization for @differentiable attribute. (
#17155) Implement (de)serialization for all components of `@differentiable` attribute except the trailing where clause (which needs to be type-checked). This is a necessary step for the `#adjoint` expression to look up `@differentiable` attributes declared on functions in other modules correctly. Addresses SR-7977.
- Loading branch information
Showing
5 changed files
with
163 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SWIFT_ENABLE_TENSORFLOW | ||
// TODO: Handle trailing where clause in @differentiable attribute. | ||
|
||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend %s -emit-module -parse-as-library -o %t | ||
// RUN: %target-sil-opt -disable-sil-linking -enable-sil-verify-all %t/differentiable_attr.swiftmodule -o - | %FileCheck %s | ||
|
||
struct CheckpointsFoo {} | ||
func pfoo(_ x: Float) -> (checkpoints: CheckpointsFoo, originalValue: Float) { | ||
return (CheckpointsFoo(), x * x) | ||
} | ||
func dfoo_checkpointed(_ x: Float, checkpoints: CheckpointsFoo, originalValue: Float, seed: Float) -> Float { | ||
return 2 * x | ||
} | ||
// CHECK-DAG: @differentiable(reverse, primal: pfoo, adjoint: dfoo_checkpointed) | ||
// CHECK-DAG: func foo_checkpointed(_ x: Float) -> Float | ||
@differentiable(reverse, primal: pfoo(_:), adjoint: dfoo_checkpointed(_:checkpoints:originalValue:seed:)) | ||
func foo_checkpointed(_ x: Float) -> Float { | ||
return x * x | ||
} | ||
|
||
struct S<T> { | ||
struct Checkpoints { | ||
let s: S | ||
} | ||
func primal(x: Float) -> (Checkpoints, Float) { | ||
return (Checkpoints(s: self), x) | ||
} | ||
func adjoint_checkpointed(x: Float, _: Checkpoints, _: Float, _: Float) -> S { | ||
return self | ||
} | ||
|
||
// CHECK-DAG: @differentiable(reverse, (self), primal: primal, adjoint: adjoint_checkpointed) | ||
// CHECK-DAG: func original(x: Float) -> Float | ||
@differentiable(reverse, withRespectTo: (self), primal: primal, adjoint: adjoint_checkpointed) | ||
func original(x: Float) -> Float { | ||
return x | ||
} | ||
} | ||
|
||
func pbaz1<T>(_ x: T, _ y: T) -> ((T, T), T) { | ||
return ((y, y), x) | ||
} | ||
func dbaz1_checkpointed<T>(_ x: T, _ y: T, primal: (T, T), originalValue: T, seed: T) -> (T, T) { | ||
return (y, x) | ||
} | ||
// CHECK-DAG: @differentiable(reverse, primal: pbaz1, adjoint: dbaz1_checkpointed) | ||
// CHECK-DAG: func baz1_checkpointed<T>(_ x: T, _ y: T) -> T | ||
@differentiable(reverse, primal: pbaz1(_:_:), adjoint: dbaz1_checkpointed(_:_:primal:originalValue:seed:)) | ||
func baz1_checkpointed<T>(_ x: T, _ y: T) -> T { | ||
return x | ||
} | ||
|
||
struct CheckpointsFP<T : FloatingPoint> { | ||
let meow: T | ||
} | ||
func pbaz2<T : FloatingPoint>(_ x: T, _ y: T) -> (CheckpointsFP<T>, T) { | ||
return (CheckpointsFP(meow: 1), x + y) | ||
} | ||
func dbaz2_checkpointed<T : FloatingPoint>(_ x: T, _ y: T, primal: CheckpointsFP<T>, originalValue: T, seed: T) -> (T, T) { | ||
return (1, 1) | ||
} | ||
// CHECK-DAG: @differentiable(reverse, primal: pbaz2, adjoint: dbaz2_checkpointed) | ||
// CHECK-DAG: func baz2_checkpointed<T>(_ x: T, _ y: T) -> T where T : FloatingPoint | ||
@differentiable(reverse, primal: pbaz2(_:_:), adjoint: dbaz2_checkpointed(_:_:primal:originalValue:seed:)) | ||
func baz2_checkpointed<T : FloatingPoint>(_ x: T, _ y: T) -> T { | ||
return x | ||
} |