Skip to content

[AutoDiff] Remove 'differentiableFunction(from:)' and 'linearFunction(from:)' #35366

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 1 commit into from
Jan 13, 2021
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
6 changes: 0 additions & 6 deletions include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,6 @@ BUILTIN_SIL_OPERATION(ApplyDerivative, "applyDerivative", Special)
/// applyTranspose
BUILTIN_SIL_OPERATION(ApplyTranspose, "applyTranspose", Special)

/// differentiableFunction
BUILTIN_SIL_OPERATION(DifferentiableFunction, "differentiableFunction", Special)

/// linearFunction
BUILTIN_SIL_OPERATION(LinearFunction, "linearFunction", Special)

/// withUnsafeContinuation<T> : (Builtin.RawUnsafeContinuation -> ()) async -> T
///
/// Unsafely capture the current continuation and pass it to the given
Expand Down
191 changes: 0 additions & 191 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,179 +1289,6 @@ static ValueDecl *getAutoDiffApplyTransposeFunction(
return builder.build(Id);
}

static ValueDecl *getDifferentiableFunctionConstructor(
ASTContext &Context, Identifier Id, unsigned arity, bool throws) {
assert(arity >= 1);
unsigned numGenericParams = 1 + arity;
BuiltinFunctionBuilder builder(Context, numGenericParams);
// Get the `Differentiable` and `AdditiveArithmetic` protocols.
auto *diffableProto =
Context.getProtocol(KnownProtocolKind::Differentiable);
auto *tangentVectorDecl =
diffableProto->getAssociatedType(Context.Id_TangentVector);
assert(tangentVectorDecl);
// Create type parameters and add conformance constraints.
auto origResultGen = makeGenericParam(arity);
builder.addConformanceRequirement(origResultGen, diffableProto);
SmallVector<decltype(origResultGen), 2> fnArgGens;
for (auto i : range(arity)) {
auto T = makeGenericParam(i);
builder.addConformanceRequirement(T, diffableProto);
fnArgGens.push_back(T);
}

BuiltinFunctionBuilder::LambdaGenerator origFnGen {
[=, &fnArgGens](BuiltinFunctionBuilder &builder) -> Type {
SmallVector<FunctionType::Param, 2> params;
for (auto &paramGen : fnArgGens)
params.push_back(FunctionType::Param(paramGen.build(builder)));
return FunctionType::get(params, origResultGen.build(builder))
->withExtInfo(FunctionType::ExtInfoBuilder(
FunctionTypeRepresentation::Swift, throws)
.build());
}
};

BuiltinFunctionBuilder::LambdaGenerator jvpGen {
[=, &fnArgGens, &Context](BuiltinFunctionBuilder &builder) -> Type {
SmallVector<FunctionType::Param, 2> params;
for (auto &paramGen : fnArgGens)
params.push_back(FunctionType::Param(paramGen.build(builder)));
auto origResultType = origResultGen.build(builder);
SmallVector<FunctionType::Param, 2> differentialParams;
for (auto &param : params) {
auto tanType = DependentMemberType::get(
param.getPlainType(), tangentVectorDecl);
differentialParams.push_back(FunctionType::Param(tanType));
}
auto differentialResultType = DependentMemberType::get(
origResultType, tangentVectorDecl);
auto differentialType =
FunctionType::get({differentialParams}, differentialResultType);
auto jvpResultType = TupleType::get(
{TupleTypeElt(origResultType, Context.Id_value),
TupleTypeElt(differentialType, Context.Id_differential)}, Context);
return FunctionType::get(params, jvpResultType)
->withExtInfo(FunctionType::ExtInfoBuilder(
FunctionTypeRepresentation::Swift, throws)
.build());
}
};

BuiltinFunctionBuilder::LambdaGenerator vjpGen {
[=, &fnArgGens, &Context](BuiltinFunctionBuilder &builder) -> Type {
SmallVector<FunctionType::Param, 2> params;
for (auto &paramGen : fnArgGens)
params.push_back(FunctionType::Param(paramGen.build(builder)));
auto origResultType = origResultGen.build(builder);
SmallVector<TupleTypeElt, 2> pullbackResultTupleElts;
for (auto &param : params) {
auto tanType = DependentMemberType::get(
param.getPlainType(), tangentVectorDecl);
pullbackResultTupleElts.push_back(TupleTypeElt(tanType));
}
auto pullbackParam = FunctionType::Param(
DependentMemberType::get(origResultType, tangentVectorDecl));
auto pullbackType = FunctionType::get(
{pullbackParam},
pullbackResultTupleElts.size() == 1
? pullbackResultTupleElts.front().getType()
: TupleType::get(pullbackResultTupleElts, Context));
auto vjpResultType = TupleType::get(
{TupleTypeElt(origResultType, Context.Id_value),
TupleTypeElt(pullbackType, Context.Id_pullback)}, Context);
return FunctionType::get(params, vjpResultType)
->withExtInfo(FunctionType::ExtInfoBuilder(
FunctionTypeRepresentation::Swift, throws)
.build());
}
};

BuiltinFunctionBuilder::LambdaGenerator resultGen {
[&](BuiltinFunctionBuilder &builder) -> Type {
auto origFnType = origFnGen.build(builder)->castTo<FunctionType>();
return origFnType->withExtInfo(
origFnType->getExtInfo()
.intoBuilder()
.withDifferentiabilityKind(DifferentiabilityKind::Normal)
.build());
}
};

builder.addParameter(origFnGen, ValueOwnership::Owned);
builder.addParameter(jvpGen, ValueOwnership::Owned);
builder.addParameter(vjpGen, ValueOwnership::Owned);
builder.setResult(resultGen);
return builder.build(Id);
}

static ValueDecl *getLinearFunctionConstructor(
ASTContext &Context, Identifier Id, unsigned arity, bool throws) {
assert(arity >= 1);
unsigned numGenericParams = 1 + arity;
BuiltinFunctionBuilder builder(Context, numGenericParams);
// Get the `Differentiable` and `AdditiveArithmetic` protocols.
auto *diffableProto =
Context.getProtocol(KnownProtocolKind::Differentiable);
auto *addArithProto =
Context.getProtocol(KnownProtocolKind::AdditiveArithmetic);
// Create type parameters and add conformance constraints.
auto origResultGen = makeGenericParam(arity);
builder.addConformanceRequirement(origResultGen, diffableProto);
builder.addConformanceRequirement(origResultGen, addArithProto);
SmallVector<decltype(origResultGen), 2> fnArgGens;
for (auto i : range(arity)) {
auto T = makeGenericParam(i);
builder.addConformanceRequirement(T, diffableProto);
builder.addConformanceRequirement(T, addArithProto);
fnArgGens.push_back(T);
}

BuiltinFunctionBuilder::LambdaGenerator origFnGen {
[=, &fnArgGens](BuiltinFunctionBuilder &builder) -> Type {
SmallVector<FunctionType::Param, 2> params;
for (auto &paramGen : fnArgGens)
params.push_back(FunctionType::Param(paramGen.build(builder)));
return FunctionType::get(params, origResultGen.build(builder))
->withExtInfo(FunctionType::ExtInfoBuilder(
FunctionTypeRepresentation::Swift, throws)
.build());
}
};

BuiltinFunctionBuilder::LambdaGenerator transposeFnGen {
[=, &fnArgGens, &Context](BuiltinFunctionBuilder &builder) -> Type {
auto origResultType = origResultGen.build(builder);
SmallVector<TupleTypeElt, 2> resultTupleElts;
for (auto &paramGen : fnArgGens)
resultTupleElts.push_back(paramGen.build(builder));
return FunctionType::get(
{FunctionType::Param(origResultType)},
resultTupleElts.size() == 1
? resultTupleElts.front().getType()
: TupleType::get(resultTupleElts, Context));
}
};

BuiltinFunctionBuilder::LambdaGenerator resultGen {
[&](BuiltinFunctionBuilder &builder) -> Type {
auto origFnType = origFnGen.build(builder)->castTo<FunctionType>();
return origFnType->withExtInfo(
origFnType->getExtInfo()
.intoBuilder()
.withDifferentiabilityKind(DifferentiabilityKind::Linear)
.build());
}
};

builder.addParameter(origFnGen, ValueOwnership::Owned);
builder.addParameter(transposeFnGen, ValueOwnership::Owned);
builder.setResult(resultGen);
return builder.build(Id);
}



static ValueDecl *getGlobalStringTablePointer(ASTContext &Context,
Identifier Id) {
// String -> Builtin.RawPointer
Expand Down Expand Up @@ -2403,22 +2230,6 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
return nullptr;
return getAutoDiffApplyTransposeFunction(Context, Id, arity, throws);
}
if (OperationName.startswith("differentiableFunction_")) {
unsigned arity;
bool throws;
if (!autodiff::getBuiltinDifferentiableOrLinearFunctionConfig(
OperationName, arity, throws))
return nullptr;
return getDifferentiableFunctionConstructor(Context, Id, arity, throws);
}
if (OperationName.startswith("linearFunction_")) {
unsigned arity;
bool throws;
if (!autodiff::getBuiltinDifferentiableOrLinearFunctionConfig(
OperationName, arity, throws))
return nullptr;
return getLinearFunctionConstructor(Context, Id, arity, throws);
}

auto BV = llvm::StringSwitch<BuiltinValueKind>(OperationName)
#define BUILTIN(id, name, Attrs) .Case(name, BuiltinValueKind::id)
Expand Down Expand Up @@ -2702,8 +2513,6 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {

case BuiltinValueKind::ApplyDerivative:
case BuiltinValueKind::ApplyTranspose:
case BuiltinValueKind::DifferentiableFunction:
case BuiltinValueKind::LinearFunction:
llvm_unreachable("Handled above");

case BuiltinValueKind::OnFastPath:
Expand Down
4 changes: 0 additions & 4 deletions lib/SIL/IR/SILModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,6 @@ const BuiltinInfo &SILModule::getBuiltinInfo(Identifier ID) {
Info.ID = BuiltinValueKind::ApplyDerivative;
else if (OperationName.startswith("applyTranspose_"))
Info.ID = BuiltinValueKind::ApplyTranspose;
else if (OperationName.startswith("differentiableFunction_"))
Info.ID = BuiltinValueKind::DifferentiableFunction;
else if (OperationName.startswith("linearFunction_"))
Info.ID = BuiltinValueKind::LinearFunction;
else
Info.ID = llvm::StringSwitch<BuiltinValueKind>(OperationName)
#define BUILTIN(id, name, attrs) .Case(name, BuiltinValueKind::id)
Expand Down
6 changes: 2 additions & 4 deletions stdlib/public/Differentiation/DifferentialOperators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ import Swift
// Transpose

@inlinable
public func transpose<T, R>(
public func _transpose<T, R>(
of body: @escaping @differentiable(linear) (T) -> R
) -> @differentiable(linear) (R) -> T {
let original = body as (T) -> R
let transpose = { x in Builtin.applyTranspose_arity1(body, x) }
return Builtin.linearFunction_arity1(transpose, original)
fatalError("Transpose is unimplemented and unsupported")
}

// Value with differential
Expand Down
57 changes: 0 additions & 57 deletions stdlib/public/Differentiation/DifferentiationUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,6 @@

import Swift

//===----------------------------------------------------------------------===//
// Differentiable function creation
//===----------------------------------------------------------------------===//

/// Create a differentiable function from a vector-Jacobian products function.
@inlinable
public func differentiableFunction<T : Differentiable, R : Differentiable>(
from vjp: @escaping (T)
-> (value: R, pullback: (R.TangentVector) -> T.TangentVector)
) -> @differentiable (T) -> R {
Builtin.differentiableFunction_arity1(
/*original*/ { vjp($0).value },
/*jvp*/ { _ in
fatalError("""
Functions formed with `differentiableFunction(from:)` cannot yet \
be used with differential-producing differential operators.
""")
},
/*vjp*/ vjp)
}

/// Create a differentiable function from a vector-Jacobian products function.
@inlinable
public func differentiableFunction<T, U, R>(
from vjp: @escaping (T, U)
-> (value: R, pullback: (R.TangentVector)
-> (T.TangentVector, U.TangentVector))
) -> @differentiable (T, U) -> R {
Builtin.differentiableFunction_arity2(
/*original*/ { vjp($0, $1).value },
/*jvp*/ { _, _ in
fatalError("""
Functions formed with `differentiableFunction(from:)` cannot yet \
be used with differential-producing differential operators.
""")
},
/*vjp*/ vjp)
}

/// Create a differentiable function from a vector-Jacobian products function.
@inlinable
public func differentiableFunction<T, U, V, R>(
from vjp: @escaping (T, U, V)
-> (value: R, pullback: (R.TangentVector)
-> (T.TangentVector, U.TangentVector, V.TangentVector))
) -> @differentiable (T, U, V) -> R {
Builtin.differentiableFunction_arity3(
/*original*/ { vjp($0, $1, $2).value },
/*jvp*/ { _, _, _ in
fatalError("""
Functions formed with `differentiableFunction(from:)` cannot yet \
be used with differential-producing differential operators.
""")
},
/*vjp*/ vjp)
}

//===----------------------------------------------------------------------===//
// Derivative customization
//===----------------------------------------------------------------------===//
Expand Down
26 changes: 0 additions & 26 deletions test/AutoDiff/SILGen/autodiff_builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,6 @@ func applyTranspose_f_indirect_arity1<T: AdditiveArithmetic & Differentiable>(_
// CHECK: bb0([[OUT_PARAM:%.*]] : $*T, [[X:%.*]] : $*T):
// CHECK: [[RESULT:%.*]] = apply [[TRANSPOSE:%.*]]([[OUT_PARAM]], [[X]])

// MARK: - differentiableFunction

@_silgen_name("differentiableFunction_f_direct_arity1")
func differentiableFunction_f_direct_arity1() -> @differentiable (Float) -> Float {
return Builtin.differentiableFunction_arity1(f_direct_arity1, f_direct_arity1_jvp, f_direct_arity1_vjp)
}
// CHECK-LABEL: sil{{.*}}@differentiableFunction_f_direct_arity1
// CHECK: [[DIFF_FN:%.*]] = differentiable_function
// CHECK: return [[DIFF_FN]]

// MARK: - linearFunction
// TODO(TF-1142): Add linear_funcion to this test when it exists.

@_silgen_name("linearFunction_f_direct_arity1")
func linearFunction_f_direct_arity1() -> @differentiable(linear) (Float) -> Float {
return Builtin.linearFunction_arity1(f_direct_arity1, f_direct_arity1)
}
// CHECK-LABEL: sil{{.*}}@linearFunction_f_direct_arity1
// CHECK: bb0:
// CHECK: [[ORIG1:%.*]] = function_ref @f_direct_arity1 : $@convention(thin) (Float) -> Float
// CHECK: [[THICK_ORIG1:%.*]] = thin_to_thick_function [[ORIG1]] : $@convention(thin) (Float) -> Float to $@callee_guaranteed (Float) -> Float
// CHECK: [[ORIG2:%.*]] = function_ref @f_direct_arity1 : $@convention(thin) (Float) -> Float
// CHECK: [[THICK_ORIG2:%.*]] = thin_to_thick_function [[ORIG2]] : $@convention(thin) (Float) -> Float to $@callee_guaranteed (Float) -> Float
// CHECK: [[LINEAR:%.*]] = linear_function [parameters 0] [[THICK_ORIG1]] : $@callee_guaranteed (Float) -> Float with_transpose [[THICK_ORIG2]] : $@callee_guaranteed (Float) -> Float
// CHECK: return [[LINEAR]] : $@differentiable(linear) @callee_guaranteed (Float) -> Float

struct ExamplePullbackStruct<T: Differentiable> {
var pb0: (T.TangentVector) -> T.TangentVector
}
Expand Down
Loading