-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff upstream] Add derivative function type calculation. #29218
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 1 commit into
swiftlang:master
from
dan-zheng:autodiff-upstream-derivative-type
Jan 15, 2020
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4910,6 +4910,134 @@ TypeBase::getAutoDiffTangentSpace(LookupConformanceFn lookupConformance) { | |
return cache(None); | ||
} | ||
|
||
// Creates an `AnyFunctionType` from the given parameters, result type, | ||
// generic signature, and `ExtInfo`. | ||
static AnyFunctionType * | ||
makeFunctionType(ArrayRef<AnyFunctionType::Param> parameters, Type resultType, | ||
GenericSignature genericSignature, | ||
AnyFunctionType::ExtInfo extInfo) { | ||
if (genericSignature) | ||
return GenericFunctionType::get(genericSignature, parameters, resultType, | ||
extInfo); | ||
return FunctionType::get(parameters, resultType, extInfo); | ||
} | ||
|
||
AnyFunctionType *AnyFunctionType::getAutoDiffDerivativeFunctionType( | ||
IndexSubset *parameterIndices, unsigned resultIndex, | ||
AutoDiffDerivativeFunctionKind kind, LookupConformanceFn lookupConformance, | ||
GenericSignature derivativeGenSig, bool makeSelfParamFirst) { | ||
assert(!parameterIndices->isEmpty() && | ||
"Expected at least one differentiability parameter"); | ||
auto &ctx = getASTContext(); | ||
|
||
// If `derivativeGenSig` is not defined, use the current function's type | ||
// generic signature. | ||
if (!derivativeGenSig) | ||
derivativeGenSig = getOptGenericSignature(); | ||
|
||
// Get differentiability parameter types. | ||
SmallVector<Type, 8> diffParamTypes; | ||
autodiff::getSubsetParameterTypes(parameterIndices, this, diffParamTypes, | ||
/*reverseCurryLevels*/ !makeSelfParamFirst); | ||
|
||
// Unwrap curry levels. At most, two parameter lists are necessary, for | ||
// curried method types with a `(Self)` parameter list. | ||
// TODO(TF-874): Simplify curry level logic. | ||
SmallVector<AnyFunctionType *, 2> curryLevels; | ||
auto *currentLevel = castTo<AnyFunctionType>(); | ||
for (unsigned i : range(2)) { | ||
(void)i; | ||
if (currentLevel == nullptr) | ||
break; | ||
curryLevels.push_back(currentLevel); | ||
currentLevel = currentLevel->getResult()->getAs<AnyFunctionType>(); | ||
} | ||
|
||
Type originalResult = curryLevels.back()->getResult(); | ||
|
||
// Build the result linear map function type. | ||
Type linearMapType; | ||
switch (kind) { | ||
case AutoDiffDerivativeFunctionKind::JVP: { | ||
// Differential function type, a result of the JVP: | ||
// `LinearMapType = (T.TangentVector, ...) -> (R.TangentVector)` | ||
SmallVector<AnyFunctionType::Param, 8> differentialParams; | ||
for (auto diffParamType : diffParamTypes) | ||
differentialParams.push_back(AnyFunctionType::Param( | ||
diffParamType->getAutoDiffTangentSpace(lookupConformance) | ||
->getType())); | ||
SmallVector<TupleTypeElt, 8> differentialResults; | ||
if (auto *resultTuple = originalResult->getAs<TupleType>()) { | ||
auto resultTupleEltType = resultTuple->getElementType(resultIndex); | ||
differentialResults.push_back( | ||
resultTupleEltType->getAutoDiffTangentSpace(lookupConformance) | ||
->getType()); | ||
} else { | ||
assert(resultIndex == 0 && "resultIndex out of bounds"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: this should match the assertion below:
|
||
differentialResults.push_back( | ||
originalResult->getAutoDiffTangentSpace(lookupConformance) | ||
->getType()); | ||
} | ||
Type differentialResult = differentialResults.size() > 1 | ||
? TupleType::get(differentialResults, ctx) | ||
: differentialResults[0].getType(); | ||
linearMapType = FunctionType::get(differentialParams, differentialResult); | ||
break; | ||
} | ||
case AutoDiffDerivativeFunctionKind::VJP: { | ||
// Pullback function type, a result of the VJP: | ||
// `LinearMapType = (R.TangentVector) -> (T.TangentVector, ...)` | ||
SmallVector<AnyFunctionType::Param, 8> pullbackParams; | ||
if (auto *resultTuple = originalResult->getAs<TupleType>()) { | ||
auto resultTupleEltType = resultTuple->getElementType(resultIndex); | ||
pullbackParams.push_back(AnyFunctionType::Param( | ||
resultTupleEltType->getAutoDiffTangentSpace(lookupConformance) | ||
->getType())); | ||
} else { | ||
assert(resultIndex == 0 && | ||
"Expected result index 0 for non-tuple result"); | ||
pullbackParams.push_back(AnyFunctionType::Param( | ||
originalResult->getAutoDiffTangentSpace(lookupConformance) | ||
->getType())); | ||
} | ||
SmallVector<TupleTypeElt, 8> pullbackResults; | ||
for (auto diffParamType : diffParamTypes) | ||
pullbackResults.push_back( | ||
diffParamType->getAutoDiffTangentSpace(lookupConformance)->getType()); | ||
Type pullbackResult = pullbackResults.size() > 1 | ||
? TupleType::get(pullbackResults, ctx) | ||
: pullbackResults[0].getType(); | ||
linearMapType = FunctionType::get(pullbackParams, pullbackResult); | ||
break; | ||
} | ||
} | ||
assert(linearMapType && "Expected linear map type"); | ||
|
||
// Build the full derivative function type: `(T...) -> (R, LinearMapType)`. | ||
SmallVector<TupleTypeElt, 2> retElts; | ||
retElts.push_back(originalResult); | ||
retElts.push_back(linearMapType); | ||
auto retTy = TupleType::get(retElts, ctx); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: I prefer using the name "result type" instead of "return type". |
||
auto *derivativeFunctionType = | ||
makeFunctionType(curryLevels.back()->getParams(), retTy, | ||
curryLevels.size() == 1 ? derivativeGenSig : nullptr, | ||
curryLevels.back()->getExtInfo()); | ||
|
||
// Wrap the derivative function type in additional curry levels. | ||
auto curryLevelsWithoutLast = | ||
ArrayRef<AnyFunctionType *>(curryLevels).drop_back(1); | ||
for (auto pair : enumerate(llvm::reverse(curryLevelsWithoutLast))) { | ||
unsigned i = pair.index(); | ||
auto *curryLevel = pair.value(); | ||
derivativeFunctionType = makeFunctionType( | ||
curryLevel->getParams(), derivativeFunctionType, | ||
i == curryLevelsWithoutLast.size() - 1 ? derivativeGenSig : nullptr, | ||
curryLevel->getExtInfo()); | ||
} | ||
|
||
return derivativeFunctionType; | ||
} | ||
|
||
CanSILFunctionType | ||
SILFunctionType::withSubstitutions(SubstitutionMap subs) const { | ||
return SILFunctionType::get(getSubstGenericSignature(), | ||
|
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.
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.
This is a general utility for AST function types. Consider adding it to
AnyFunctionType
, or just making it a closure insidegetAutoDiffDerivativeFunctionType
.