-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] Serialize derivative function configurations per module. #28608
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
[AutoDiff] Serialize derivative function configurations per module. #28608
Conversation
}); | ||
} | ||
|
||
ArrayRef<AutoDiffConfig> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: even though @differentiable
and @derivative
attributes do not currently store result indices, I decided to use AutoDiffConfig
(which contains result indices) to represent AST derivative function configurations. I also tried creating a new "ASTAutoDiffConfig" struct containing just parameter indices and derivative generic signature, but decided against the code duplication.
I think the direction is to eventually plumb result indices through the differentiation system (TF-1038), so preemptively using result indices (AutoDiffConfig
) seems good.
d38c72b
to
7ab66b9
Compare
`@differentiable` and `@derivative` attributes register derivatives for `AbstractFunctionDecl`s for a particular "derivative function configuration": parameter indices and dervative generic signature. To find `@derivative` functions registered in other Swift modules, derivative function configurations must be serialized per module. When configurations for a `AbstractFunctionDecl` are requested, all configurations from imported modules are deserialized. This module serialization technique has precedent: it is used for protocol conformances (e.g. extension declarations for a nominal type) and Obj-C members for a class type. Add `AbstractFunctionDecl::getDerivativeFunctionConfigurations` entry point for accessing derivative function configurations. Use `AbstractFunctionDecl::getDerivativeFunctionConfigurations` to implement `findMinimalDerivativeConfiguration` for canonical derivative function configuration lookup, replacing `getMinimalASTDifferentiableAttr`. Unblocks TF-815: lowering `@derivative` attributes directly to SIL differentiability witnesses without generating implicit `@differentiable` attributes.
7ab66b9
to
ceb8ce0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cooool!
include/swift/SILOptimizer/Utils/Differentiation/DerivativeLookup.h
Outdated
Show resolved
Hide resolved
Change `findMinimalDerivativeConfiguration` to return `Optional<AutoDiffConfig>`.
@swift-ci Please test tensorflow |
Extended test suite passed. Merging to unblock progress. |
Very nice. Thanks for pushing this through :) |
Previously, `@derivative` attribute type-checking created implicit `@differentiable` attributes on the original declaration. This was a longstanding hack powering `@derivative` attribute derivative registration. swiftlang#28608 made these changes: - Derivative function configurations (from `@differentiable` and `@derivative` attributes) are serialized in modules and are loaded from imported modules. - The differentiation transform uses these derivative function configurations for derivative function lookup instead of `@differentiable` attributes. Now, `@derivative` attributes are directly lowered to differentiability witnesses during SILGen, and implicit `@differentiable` attribute generation is removed. Type-checking changes: - "Overlapping" `@differentiable` and `@derivative` attributes (for the same original declaration and parameter indices) are now disallowed. They semantically conflict because the first "requests derivative generation" while the second "registers a derivative". - "Overlapping" `@differentiable` and `@derivative` attributes are allowed for protocol requirements. Requirement `@differentiable` attributes mean "add JVP/VJP witness table entries" - not "request derivative generation", because there is no function body. - Note that relaxing the "overlapping" condition to consider derivative generic signatures is possible after derivative generic signature mangling for derivative functions: TF-680. Resolves TF-835. Unblocks TF-1021: lifting the "same-file derivative registration only" limitation in `@derivative` attribute type-checking. This should be possible without much work, but needs testing! Exposes TF-1040: `@differentiable` attribute limitations for class methods. Exposes TF-1041: untested protocol requirement `@differentiable` attribute type-checking logic.
Previously, `@derivative` attribute type-checking created implicit `@differentiable` attributes on the original declaration. This was a longstanding hack powering `@derivative` attribute derivative registration. #28608 made these changes: - Derivative function configurations (from `@differentiable` and `@derivative` attributes) are serialized in modules and are loaded from imported modules. - The differentiation transform uses these derivative function configurations for derivative function lookup instead of `@differentiable` attributes. Now, `@derivative` attributes are directly lowered to differentiability witnesses during SILGen, and implicit `@differentiable` attribute generation is removed. Type-checking changes: - "Overlapping" `@differentiable` and `@derivative` attributes (for the same original declaration and parameter indices) are now disallowed. They semantically conflict because the first "requests derivative generation" while the second "registers a derivative". - "Overlapping" `@differentiable` and `@derivative` attributes are allowed for protocol requirements. Requirement `@differentiable` attributes mean "add JVP/VJP witness table entries" - not "request derivative generation", because there is no function body. - Note that relaxing the "overlapping" condition to consider derivative generic signatures is possible after derivative generic signature mangling for derivative functions: TF-680. Resolves TF-835. Unblocks TF-1021: lifting the "same-file derivative registration only" limitation in `@derivative` attribute type-checking. This should be possible without much work, but needs testing! Exposes TF-1040: `@differentiable` attribute limitations for class methods. Exposes TF-1041: untested protocol requirement `@differentiable` attribute type-checking logic.
#28621) Previously, `@derivative` attribute type-checking created implicit `@differentiable` attributes on the original declaration. This was a longstanding hack powering `@derivative` attribute derivative registration. #28608 made these changes: - Derivative function configurations (from `@differentiable` and `@derivative` attributes) are serialized in modules and are loaded from imported modules. - The differentiation transform uses these derivative function configurations for derivative function lookup instead of `@differentiable` attributes. Now, `@derivative` attributes are directly lowered to differentiability witnesses during SILGen, and implicit `@differentiable` attribute generation is removed. `@derivative` attributes are now serialized. Resolves TF-835. Unblocks TF-1021: lifting the "same-file derivative registration only" limitation in `@derivative` attribute type-checking. This should be trivially possible but requires more testing. Exposes TF-1037: crash due to `@differentiable` + `@derivative` attribute with different derivative generic signatures. Exposes TF-1040: `@differentiable` attribute limitations for class methods. Exposes TF-1041: untested protocol requirement `@differentiable` attribute type-checking logic. Tracks TF-1042: remove `ASTContext::{Differentiable,Derivative}Attrs` and use `AbstractFunctionDecl::getDerivativeFunctionConfigurations` to detect duplicate `@differentiable` + `@derivative` attributes.
@differentiable
and@derivative
attributes register derivatives forAbstractFunctionDecl
s for a particular "derivative function configuration":parameter indices and derivative generic signature.
To find
@derivative
functions registered in other Swift modules, derivativefunction configurations must be serialized per module. When configurations for
a
AbstractFunctionDecl
are requested, all configurations from importedmodules are deserialized. This module serialization technique has precedent: it
is used for protocol conformances (e.g. extension declarations for a nominal
type) and Obj-C members for a class type.
Add
AbstractFunctionDecl::getDerivativeFunctionConfigurations
entry pointfor accessing derivative function configurations.
Use
AbstractFunctionDecl::getDerivativeFunctionConfigurations
toimplement
findMinimalDerivativeConfiguration
for canonical derivativefunction configuration lookup, replacing
getMinimalASTDifferentiableAttr
.Unblocks TF-835: lowering
@derivative
attributes directly to SILdifferentiability witnesses without generating implicit
@differentiable
attributes.