Commit Graph

22 Commits

Author SHA1 Message Date
Jaap Wijnen
b4fb57459d [AutoDiff] Make sure rhs is negated in all cases when subtracting two Array.TangentVectors (#84731)
`Array.TangentVector` conformance to `AdditiveArithmetic` was incorrect as the returned values weren't negated if the lhs was an empty vector (considered to be a zero tangentvector)
2025-10-08 17:07:59 -07:00
Jaap Wijnen
a5ef5fe226 Improve differentiableMap vjp performance (#83807)
Improve differentiableMap vjp performance by reserving capacity for the values and pullbacks arrays.
2025-08-19 17:08:14 -07:00
Jaap Wijnen
ccfbc38ef5 mark multiple autodiff related array methods as inlinable for increased specialization possibilities (#75778)
Co-authored-by: Jaap Wijnen <jaap@passivelogic.com>
2024-11-07 07:20:35 -08:00
Richard Wei
06b20662b8 [AutoDiff] Conform Array's and Optional's TangentVector to CustomReflectable
Resolves rdar://88542240.
2022-02-15 02:59:46 -08:00
Philip Turner
20ca9464e1 [AutoDiff] Add missing constraint to Array.DifferentiableView declaration (#41030)
Add `where Element: Differentiable` to `Array.DifferentiableView` declaration.
2022-01-27 12:34:43 -08:00
Vojta Molda
9697dd8905 [AutoDiff] Add .zero tangent vector handling to Array.DifferentiableView.+ and .move
Implement correct zero tangent vector, i.e. empty array [], handling during the backward pass of Array.DifferentiableView.+ and move methods. The precondition is no longer triggered by .zero/empty arrays.

Fixes SR-14297
2021-03-19 19:16:40 -05:00
Richard Wei
0b53a02544 [AutoDiff] Rename 'in:' to 'of:' in differential operators.
Rename the argument label `in:` in `gradient(at:in:)`, `pullback(at:in:)`, etc to `of:`, as suggested in the [pitch thread](https://forums.swift.org/t/differentiable-programming-for-gradient-based-machine-learning/42147).
2021-02-24 01:33:42 -05:00
Richard Wei
8bc6143a4c [AutoDiff] Rename 'move(along:)' to 'move(by:)'.
Rename `move(along:)` to `move(by:)` based on the proposal feedback. The main argument for the change is that tangent vectors specify both a direction and a magnitude, whereas `along:` does not indicate that `self` is being moved by the specified magnitude.
2021-02-23 21:45:01 -05:00
Richard Wei
af8942d940 [AutoDiff] Rename '@differentiable' to '@differentiable(reverse)'.
Compiler:
- Add `Forward` and `Reverse` to `DifferentiabilityKind`.
- Expand `DifferentiabilityMask` in `ExtInfo` to 3 bits so that it now holds all 4 cases of `DifferentiabilityKind`.
- Parse `@differentiable(reverse)` and `@differentiable(_forward)` declaration attributes and type attributes.
- Emit a warning for `@differentiable` without `reverse`.
- Emit an error for `@differentiable(_forward)`.
- Rename `@differentiable(linear)` to `@differentiable(_linear)`.
- Make `@differentiable(reverse)` type lowering go through today's `@differentiable` code path. We will specialize it to reverse-mode in a follow-up patch.

ABI:
- Add `Forward` and `Reverse` to `FunctionMetadataDifferentiabilityKind`.
- Extend `TargetFunctionTypeFlags` by 1 bit to store the highest bit of differentiability kind (linear). Note that there is a 2-bit gap in `DifferentiabilityMask` which is reserved for `AsyncMask` and `ConcurrentMask`; `AsyncMask` is ABI-stable so we cannot change that.

_Differentiation module:
- Replace all occurrences of `@differentiable` with `@differentiable(reverse)`.
- Delete `_transpose(of:)`.

Resolves rdar://69980056.
2021-02-07 14:09:46 -08:00
Richard Wei
e3db926e0c [AutoDiff] Remove '_Differentiable.zeroTangentVectorInitializer'. (#35329)
Remove `_Differentiable.zeroTangentVectorInitializer` to address the feedback on the [proposal thread](https://forums.swift.org/t/differentiable-programming-for-gradient-based-machine-learning/42147). The corresponding change has already been made in the [proposal](https://github.com/rxwei/swift-evolution/blob/autodiff/proposals/0000-differentiable-programming.md).

Removed components:
- `zeroTangentVectorInitializer` and `zeroTangentVector` in `Differentiable`, `Array`, `Optional`, `Float`, `Double`, `Float80`, and SIMD types.
- `zeroTangentVectorInitializer` synthesis logic in `Differentiable` derived conformances.
2021-01-20 10:45:03 -08:00
Dan Zheng
f3685f9a12 [AutoDiff] Conform Optional to Differentiable. (#32948)
Make `Optional` conditionally conform to `Differentiable` when the `Wrapped` type does.
`Optional.TangentVector` is a wrapper around `Wrapped.TangentVector?`.

Also, fix `Array.TangentVector.zeroTangentVectorInitializer`.

Resolves TF-1301.
2020-07-17 12:07:52 -07:00
Alex Efremov
bfcf12bac6 [AutoDiff][stdlib] Add JVPs to ArrayDifferentiation.swift (#32840)
Adds JVPs to `Array` and `Array.DifferentiableView`

`_jvpDifferentiableReduce` is taken from #29324
2020-07-12 15:04:33 -07:00
Dan Zheng
f76169be58 [AutoDiff] Improve Array.TangentVector precondition messages. (#32154)
Improve count-related precondition messages.

Co-authored-by: Richard Wei <rxwei@apple.com>
2020-06-03 00:50:15 -07:00
Dan Zheng
f9c5d7ae6c [AutoDiff] Derive Differentiable.zeroTangentVectorInitializer. (#31823)
`Differentiable` conformance derivation now supports
`Differentiable.zeroTangentVectorInitializer`.

There are two potential cases:
1. Memberwise derivation: done when `TangentVector` can be initialized memberwise.
2. `{ TangentVector.zero }` derivation: done as a fallback.

`zeroTangentVectorInitializer` is a closure that produces a zero tangent vector,
capturing minimal necessary information from `self`.

It is an instance property, unlike the static property `AdditiveArithmetic.zero`,
and should be used by the differentiation transform for correctness.

Remove `Differentiable.zeroTangentVectorInitializer` dummy default implementation.

Update stdlib `Differentiable` conformances and tests.
Clean up DerivedConformanceDifferentiable.cpp cruft.

Resolves TF-1007.
Progress towards TF-1008: differentiation correctness for projection operations.
2020-05-29 01:59:52 -07:00
Dan Zheng
e3938b5c7d [AutoDiff] Fix Array.append(_:) pullback.
The derivative wrt `self` should drop the last element from the incoming seed.

Example:
- Incoming seed: [1, 2, 3, 4]
- Derivative wrt `self`: [1, 2, 3]
- Derivative wrt appended element: 4
2020-05-14 09:27:50 -07:00
Dan Zheng
2e690e2d5e [AutoDiff] NFC: garden array differentiation.
Use consistent variable naming. Reorganize code.
2020-05-14 09:27:50 -07:00
Dan Zheng
8829a1432b [AutoDiff] Register derivatives for Array.+=. (#31782)
Register JVP and VJP for `Array.+=`.
Simplify and use same array concatenation derivative tests as `Array.+`.
2020-05-14 09:27:03 -07:00
marcrasi
eaa1051dfa [AutoDiff] make differentiable array ops inlinable (#31704) 2020-05-11 11:17:12 -07:00
Dan Zheng
65ab642b1a [AutoDiff upstream] Add Differentiable.zeroTangentVector.
Add `Differentiable.zeroTangentVectorInitializer` protocol requirement and
`Differentiable.zeroTangentVector` default implementation.
2020-04-09 11:15:18 -07:00
Dan Zheng
d599105715 [AutoDiff upstream] Fix stdlib differentiation tests.
Temporarily disable not-yet-supported differentiation tests:

- Forward-mode differentiation.
  - TF-1237: to be upstreamed.
- `Differentiable.zeroTangentVector`.
  - TF-1238: to be upstreamed.
- `SIMD.sum` differentiation.
  - TF-1103: `@_alwaysEmitIntoClient` derivative registration.
2020-04-08 02:50:43 -07:00
Dan Zheng
da36555aa5 [AutoDiff upstream] Gardening.
- Standardize filenames: `XXXDifferentiation.swift`.
  - Use Pascal or snake case consistently.
- Formatting changes.
2020-04-08 02:49:12 -07:00
Ewa Matejska
f07ae89636 First pass at upstreaming Differentiable conformances and derivatives 2020-04-02 19:55:26 -07:00