-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] Add SIL differentiability witnesses. #27487
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
Merged
dan-zheng
merged 28 commits into
swiftlang:tensorflow
from
dan-zheng:sil-differentiability-witness
Oct 13, 2019
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
7e5d804
Diff witness
rxwei a5c1d13
Rename SILDifferentiabilityWitness.h.
dan-zheng 0ce7d91
Update SILDifferentiabilityWitness definition.
dan-zheng 471e660
Add SILDifferentiabilityWitness to SILModule.
dan-zheng bf87d2e
[WIP] Start SILDifferentiabilityWitness parsing/printing.
dan-zheng 6b78684
Use improved syntax.
dan-zheng ab304f1
Merge branch 'tensorflow' of github.com:apple/swift into sil-differen…
dan-zheng a2ae0f2
Finish parsing/printing/serialization.
dan-zheng 419eea2
Revamp serialization to enable lookup by key.
dan-zheng b6cd1d7
Add SIL verification.
dan-zheng 573dd3e
Add miscellaneous todo comments.
dan-zheng 843b631
Add round-trip parsing/printing test.
dan-zheng 835f1c0
Clean up.
dan-zheng aca1abb
Add Swift source for parsing test.
dan-zheng d75f6ce
Merge branch 'tensorflow' of github.com:apple/swift into sil-differen…
dan-zheng 4187700
`AutoDiffIndexSubset` -> `IndexSubset`
dan-zheng 873468f
Minor fix.
dan-zheng aea64d3
Update differentiability witness syntax.
dan-zheng bb93af3
Add `AutoDiffConfig` and use in `SILDifferentiabilityWitnessKey`.
dan-zheng f240ed2
Change `AutoDiffConfig` to a POD.
dan-zheng ad6b7aa
Add `DeclAttribute *` to `SILDifferentiabilityWitness`.
dan-zheng 7c63d03
Clean up.
dan-zheng c3959ad
Address review feedback.
dan-zheng 2073f86
Address review feedback.
dan-zheng 27d7abc
Parse/print `[serialized]` flag.
dan-zheng 69209be
Add parsing/printing tests, address review feedback.
dan-zheng df9cd49
Fix serialization and add test.
dan-zheng d673b70
Fix verification.
dan-zheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,153 @@ | ||
//===--- SILDifferentiabilityWitness.h - Differentiability witnesses ------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines the SILDifferentiabilityWitness class, which maps an | ||
// original SILFunction and derivative configuration (parameter indices, result | ||
// indices, derivative generic signature) to derivative functions (JVP and VJP). | ||
// | ||
// SIL differentiability witnesses are generated from the `@differentiable` | ||
// and `@differentiating` attributes AST declaration attributes. | ||
// Differentiability witnesses are canonicalized by the differentiation SIL | ||
// transform, which fills in missing derivative functions. Canonical | ||
// differentiability witnesses from other modules can be deserialized to look up | ||
// derivative functions. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_SIL_SILDIFFERENTIABILITYWITNESS_H | ||
#define SWIFT_SIL_SILDIFFERENTIABILITYWITNESS_H | ||
|
||
#include "swift/AST/Attr.h" | ||
#include "swift/AST/AutoDiff.h" | ||
#include "swift/AST/GenericSignature.h" | ||
#include "swift/SIL/SILAllocated.h" | ||
#include "llvm/ADT/ilist_node.h" | ||
#include "llvm/ADT/ilist.h" | ||
|
||
namespace swift { | ||
|
||
class SILPrintContext; | ||
|
||
class SILDifferentiabilityWitness | ||
: public llvm::ilist_node<SILDifferentiabilityWitness>, | ||
public SILAllocated<SILDifferentiabilityWitness> | ||
{ | ||
private: | ||
/// The module which contains the differentiability witness. | ||
SILModule &module; | ||
/// The linkage of the differentiability witness. | ||
SILLinkage linkage; | ||
/// The original function. | ||
SILFunction *originalFunction; | ||
/// The parameter indices. | ||
IndexSubset *parameterIndices; | ||
/// The result indices. | ||
IndexSubset *resultIndices; | ||
/// The derivative generic signature (optional). | ||
GenericSignature *derivativeGenericSignature; | ||
/// The JVP (Jacobian-vector products) derivative function. | ||
SILFunction *jvp; | ||
/// The VJP (vector-Jacobian products) derivative function. | ||
SILFunction *vjp; | ||
/// Whether or not this differentiability witness is serialized, which allows | ||
/// devirtualization from another module. | ||
bool serialized; | ||
/// The AST `@differentiable` or `@differentiating` attribute from which the | ||
/// differentiability witness is generated. Used for diagnostics. | ||
/// Null if the differentiability witness is parsed from SIL or if it is | ||
/// deserialized. | ||
DeclAttribute *attribute = nullptr; | ||
|
||
SILDifferentiabilityWitness(SILModule &module, SILLinkage linkage, | ||
SILFunction *originalFunction, | ||
IndexSubset *parameterIndices, | ||
IndexSubset *resultIndices, | ||
GenericSignature *derivativeGenSig, | ||
SILFunction *jvp, SILFunction *vjp, | ||
bool isSerialized, DeclAttribute *attribute) | ||
: module(module), linkage(linkage), originalFunction(originalFunction), | ||
parameterIndices(parameterIndices), resultIndices(resultIndices), | ||
derivativeGenericSignature(derivativeGenSig), jvp(jvp), vjp(vjp), | ||
serialized(isSerialized), attribute(attribute) {} | ||
|
||
public: | ||
static SILDifferentiabilityWitness *create( | ||
SILModule &module, SILLinkage linkage, SILFunction *originalFunction, | ||
IndexSubset *parameterIndices, IndexSubset *resultIndices, | ||
GenericSignature *derivativeGenSig, SILFunction *jvp, SILFunction *vjp, | ||
bool isSerialized, DeclAttribute *attribute = nullptr); | ||
|
||
SILDifferentiabilityWitnessKey getKey() const; | ||
SILModule &getModule() const { return module; } | ||
SILLinkage getLinkage() const { return linkage; } | ||
SILFunction *getOriginalFunction() const { return originalFunction; } | ||
IndexSubset *getParameterIndices() const { | ||
return parameterIndices; | ||
} | ||
IndexSubset *getResultIndices() const { | ||
return resultIndices; | ||
} | ||
GenericSignature *getDerivativeGenericSignature() const { | ||
return derivativeGenericSignature; | ||
} | ||
SILFunction *getJVP() const { return jvp; } | ||
dan-zheng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SILFunction *getVJP() const { return vjp; } | ||
SILFunction *getDerivative(AutoDiffDerivativeFunctionKind kind) const { | ||
switch (kind) { | ||
case AutoDiffDerivativeFunctionKind::JVP: return jvp; | ||
case AutoDiffDerivativeFunctionKind::VJP: return vjp; | ||
} | ||
} | ||
void setJVP(SILFunction *jvp) { this->jvp = jvp; } | ||
void setVJP(SILFunction *vjp) { this->vjp = vjp; } | ||
void setDerivative(AutoDiffDerivativeFunctionKind kind, | ||
SILFunction *derivative) { | ||
switch (kind) { | ||
case AutoDiffDerivativeFunctionKind::JVP: jvp = derivative; break; | ||
case AutoDiffDerivativeFunctionKind::VJP: vjp = derivative; break; | ||
} | ||
} | ||
bool isSerialized() const { return serialized; } | ||
DeclAttribute *getAttribute() const { return attribute; } | ||
|
||
/// Verify that the differentiability witness is well-formed. | ||
void verify(const SILModule &module) const; | ||
|
||
void print(llvm::raw_ostream &os, bool verbose = false) const; | ||
void dump() const; | ||
}; | ||
|
||
} // end namespace swift | ||
|
||
namespace llvm { | ||
|
||
//===----------------------------------------------------------------------===// | ||
// ilist_traits for SILDifferentiabilityWitness | ||
//===----------------------------------------------------------------------===// | ||
|
||
template <> | ||
struct ilist_traits<::swift::SILDifferentiabilityWitness> | ||
: public ilist_node_traits<::swift::SILDifferentiabilityWitness> { | ||
using SILDifferentiabilityWitness = ::swift::SILDifferentiabilityWitness; | ||
|
||
public: | ||
static void deleteNode(SILDifferentiabilityWitness *DW) { | ||
DW->~SILDifferentiabilityWitness(); | ||
} | ||
|
||
private: | ||
void createNode(const SILDifferentiabilityWitness &); | ||
}; | ||
|
||
} // namespace llvm | ||
|
||
#endif // SWIFT_SIL_SILDIFFERENTIABILITYWITNESS_H |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.