Skip to content

[AutoDiff] Require [parameters ...] for `{differentiable,linear}_fu… #29015

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions docs/SIL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5606,7 +5606,7 @@ differentiable_function
::

sil-instruction ::= 'differentiable_function'
sil-differentiable-function-parameter-indices?
sil-differentiable-function-parameter-indices
sil-value ':' sil-type
sil-differentiable-function-derivative-functions-clause?

Expand All @@ -5624,8 +5624,7 @@ function. There are two derivative functions: a Jacobian-vector products (JVP)
function and a vector-Jacobian products (VJP) function.

``[parameters ...]`` specifies parameter indices that the original function is
differentiable with respect to. When not specified, it defaults to all
parameters.
differentiable with respect to.

A ``with_derivative`` clause specifies the differentiation functions associated
with the original function. When a ``with_derivative`` clause is not specified,
Expand All @@ -5641,7 +5640,7 @@ linear_function
::

sil-instruction ::= 'linear_function'
sil-linear-function-parameter-indices?
sil-linear-function-parameter-indices
sil-value ':' sil-type
sil-linear-function-transpose-function-clause?

Expand All @@ -5656,7 +5655,7 @@ Bundles a function with its transpose function into a
``@differentiable(linear)`` function.

``[parameters ...]`` specifies parameter indices that the original function is
linear with respect to. When not specified, it defaults to all parameters.
linear with respect to.

A ``with_transpose`` clause specifies the transpose function associated
with the original function. When a ``with_transpose`` clause is not specified,
Expand Down
106 changes: 40 additions & 66 deletions lib/ParseSIL/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,32 @@ static bool parseAssignOwnershipQualifier(AssignOwnershipQualifier &Result,
return false;
}

// SWIFT_ENABLE_TENSORFLOW
// Parse a list of integer indices, prefaced with the given string label.
// Returns true on error.
static bool parseIndexList(Parser &P, StringRef label,
SmallVectorImpl<unsigned> &indices,
const Diagnostic &parseIndexDiag) {
SourceLoc loc;
// Parse `[<label> <integer_literal>...]`.
if (P.parseToken(tok::l_square, diag::sil_autodiff_expected_lsquare,
"index list") ||
P.parseSpecificIdentifier(
label, diag::sil_autodiff_expected_index_list_label, label))
return true;
while (P.Tok.is(tok::integer_literal)) {
unsigned index;
if (P.parseUnsignedInteger(index, loc, parseIndexDiag))
return true;
indices.push_back(index);
}
if (P.parseToken(tok::r_square, diag::sil_autodiff_expected_rsquare,
"index list"))
return true;
return false;
};
// SWIFT_ENABLE_TENSORFLOW END

// SWIFT_ENABLE_TENSORFLOW
/// sil-differentiability-witness-config-and-function ::=
/// '[' 'parameters' index-subset ']'
Expand All @@ -2083,35 +2109,14 @@ static bool parseAssignOwnershipQualifier(AssignOwnershipQualifier &Result,
static Optional<std::pair<AutoDiffConfig, SILFunction *>>
parseSILDifferentiabilityWitnessConfigAndFunction(Parser &P, SILParser &SP,
SILLocation L) {
SourceLoc lastLoc;
// Parse an index set, prefaced with the given label.
auto parseIndexSet = [&](StringRef label, SmallVectorImpl<unsigned> &indices,
const Diagnostic &parseIndexDiag) -> bool {
// Parse `[<label> <integer_literal>...]`.
if (P.parseToken(tok::l_square, diag::sil_autodiff_expected_lsquare,
"index list") ||
P.parseSpecificIdentifier(
label, diag::sil_autodiff_expected_index_list_label, label))
return true;
while (P.Tok.is(tok::integer_literal)) {
unsigned index;
if (P.parseUnsignedInteger(index, lastLoc, parseIndexDiag))
return true;
indices.push_back(index);
}
if (P.parseToken(tok::r_square, diag::sil_autodiff_expected_rsquare,
"index list"))
return true;
return false;
};
// Parse parameter and result indices.
SmallVector<unsigned, 8> parameterIndices;
SmallVector<unsigned, 8> resultIndices;
if (parseIndexSet("parameters", parameterIndices,
diag::sil_autodiff_expected_parameter_index))
if (parseIndexList(P, "parameters", parameterIndices,
diag::sil_autodiff_expected_parameter_index))
return {};
if (parseIndexSet("results", resultIndices,
diag::sil_autodiff_expected_result_index))
if (parseIndexList(P, "results", resultIndices,
diag::sil_autodiff_expected_result_index))
return {};
// Parse witness generic parameter clause.
GenericSignature witnessGenSig = GenericSignature();
Expand Down Expand Up @@ -2938,27 +2943,12 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
//
// e.g. differentiable_function [parameters 0 1 2] %0 : $T with_derivative
// {%1 : $T, %2 : $T}
// ^ jvp ^ vjp
SourceLoc lastLoc;
// ^~ jvp ^~ vjp
// Parse `[parameters <integer_literal>...]`.
SmallVector<unsigned, 8> parameterIndices;
// Parse optional `[parameters <integer_literal>...]`
if (P.Tok.is(tok::l_square) &&
P.peekToken().is(tok::identifier) &&
P.peekToken().getText() == "parameters") {
P.consumeToken(tok::l_square);
P.consumeToken(tok::identifier);
// Parse indices.
while (P.Tok.is(tok::integer_literal)) {
unsigned index;
if (P.parseUnsignedInteger(index, lastLoc,
diag::sil_autodiff_expected_parameter_index))
return true;
parameterIndices.push_back(index);
}
if (P.parseToken(tok::r_square, diag::sil_autodiff_expected_rsquare,
"parameter index list"))
return true;
}
if (parseIndexList(P, "parameters", parameterIndices,
diag::sil_autodiff_expected_parameter_index))
return true;
// Parse the original function value.
SILValue original;
SourceLoc originalOperandLoc;
Expand Down Expand Up @@ -3001,26 +2991,11 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
case SILInstructionKind::LinearFunctionInst: {
// e.g. linear_function [parameters 0 1 2] %0 : $T
// e.g. linear_function [parameters 0 1 2] %0 : $T with_transpose %1 : $T
SourceLoc lastLoc;
// Parse `[parameters <integer_literal>...]`.
SmallVector<unsigned, 8> parameterIndices;
// Parse optional `[parameters <integer_literal>...]`
if (P.Tok.is(tok::l_square) &&
P.peekToken().is(tok::identifier) &&
P.peekToken().getText() == "parameters") {
P.consumeToken(tok::l_square);
P.consumeToken(tok::identifier);
// Parse indices.
while (P.Tok.is(tok::integer_literal)) {
unsigned index;
if (P.parseUnsignedInteger(index, lastLoc,
diag::sil_autodiff_expected_parameter_index))
return true;
parameterIndices.push_back(index);
}
if (P.parseToken(tok::r_square, diag::sil_autodiff_expected_rsquare,
"parameter index list"))
return true;
}
if (parseIndexList(P, "parameters", parameterIndices,
diag::sil_autodiff_expected_parameter_index))
return true;
// Parse the original function value.
SILValue original;
SourceLoc originalOperandLoc;
Expand Down Expand Up @@ -3117,9 +3092,8 @@ bool SILParser::parseSILInstruction(SILBuilder &B) {
SourceLoc keyStartLoc = P.Tok.getLoc();
auto configAndFn = parseSILDifferentiabilityWitnessConfigAndFunction(
P, *this, InstLoc);
if (!configAndFn) {
if (!configAndFn)
return true;
}
auto config = configAndFn->first;
auto originalFn = configAndFn->second;
auto *witness = SILMod.lookUpDifferentiabilityWitness(
Expand Down
20 changes: 8 additions & 12 deletions lib/SIL/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,12 +1212,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {

// SWIFT_ENABLE_TENSORFLOW
void visitDifferentiableFunctionInst(DifferentiableFunctionInst *dfi) {
if (!dfi->getParameterIndices()->isEmpty()) {
*this << "[parameters";
for (auto i : dfi->getParameterIndices()->getIndices())
*this << ' ' << i;
*this << "] ";
}
*this << "[parameters";
for (auto i : dfi->getParameterIndices()->getIndices())
*this << ' ' << i;
*this << "] ";
*this << getIDAndType(dfi->getOriginalFunction());
if (dfi->hasDerivativeFunctions()) {
*this << " with_derivative ";
Expand All @@ -1227,12 +1225,10 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
}

void visitLinearFunctionInst(LinearFunctionInst *lfi) {
if (!lfi->getParameterIndices()->isEmpty()) {
*this << "[parameters";
for (auto i : lfi->getParameterIndices()->getIndices())
*this << ' ' << i;
*this << "] ";
}
*this << "[parameters";
for (auto i : lfi->getParameterIndices()->getIndices())
*this << ' ' << i;
*this << "] ";
*this << getIDAndType(lfi->getOriginalFunction());
if (lfi->hasTransposeFunction()) {
*this << " with_transpose ";
Expand Down