Skip to content
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
4 changes: 2 additions & 2 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ DECL_ATTR(differentiable, Differentiable,
AllowMultipleAttributes,
83)
DECL_ATTR(differentiating, Differentiating,
OnFunc | LongAttribute | AllowMultipleAttributes,
84)
OnFunc | LongAttribute | AllowMultipleAttributes |
NotSerialized, 84)
SIMPLE_DECL_ATTR(compilerEvaluable, CompilerEvaluable,
OnAccessor | OnFunc | OnConstructor | OnSubscript,
/* Not serialized */ 85)
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1497,9 +1497,9 @@ class DifferentiableAttr final
void setRequirements(ASTContext &context, ArrayRef<Requirement> requirements);

FuncDecl *getJVPFunction() const { return JVPFunction; }
void setJVPFunction(FuncDecl *decl) { JVPFunction = decl; }
void setJVPFunction(FuncDecl *decl);
FuncDecl *getVJPFunction() const { return VJPFunction; }
void setVJPFunction(FuncDecl *decl) { VJPFunction = decl; }
void setVJPFunction(FuncDecl *decl);

bool parametersMatch(const DifferentiableAttr &other) const {
assert(ParameterIndices && other.ParameterIndices);
Expand Down
12 changes: 12 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,18 @@ void DifferentiableAttr::setRequirements(ASTContext &context,
Requirements = context.AllocateCopy(requirements);
}

void DifferentiableAttr::setJVPFunction(FuncDecl *decl) {
JVPFunction = decl;
if (decl && !JVP)
JVP = {decl->getFullName(), DeclNameLoc(decl->getNameLoc())};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the location of decl's name here (DeclNameLoc(decl->getNameLoc())) might not make sense.

}

void DifferentiableAttr::setVJPFunction(FuncDecl *decl) {
VJPFunction = decl;
if (decl && !VJP)
VJP = {decl->getFullName(), DeclNameLoc(decl->getNameLoc())};
}

void DifferentiableAttr::print(llvm::raw_ostream &OS, const Decl *D,
ModuleDecl *prettyPrintInModule) const {
StreamPrinter P(OS);
Expand Down
25 changes: 0 additions & 25 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2590,31 +2590,6 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
break;
}

// SWIFT_ENABLE_TENSORFLOW
case decls_block::Differentiating_DECL_ATTR: {
bool isImplicit;
uint64_t origNameId;
DeclID origDeclId;
ArrayRef<uint64_t> parameters;

serialization::decls_block::DifferentiatingDeclAttrLayout::readRecord(
scratch, isImplicit, origNameId, origDeclId, parameters);

DeclNameWithLoc origName = {getIdentifier(origNameId), DeclNameLoc()};
FuncDecl *origDecl = cast<FuncDecl>(getDecl(origDeclId));

llvm::SmallBitVector parametersBitVector(parameters.size());
for (unsigned i : indices(parameters))
parametersBitVector[i] = parameters[i];
auto *indices = AutoDiffParameterIndices::get(parametersBitVector, ctx);

auto diffAttr = DifferentiatingAttr::create(
ctx, isImplicit, SourceLoc(), SourceRange(), origName, indices);
diffAttr->setOriginalFunction(origDecl);
Attr = diffAttr;
break;
}

case decls_block::DynamicReplacement_DECL_ATTR: {
bool isImplicit;
uint64_t numArgs;
Expand Down
22 changes: 2 additions & 20 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,8 @@ void Serializer::writeDeclAttribute(const DeclAttribute *DA) {
case DAK_RestatedObjCConformance:
case DAK_ClangImporterSynthesizedType:
case DAK_PrivateImport:
// SWIFT_ENABLE_TENSORFLOW
case DAK_Differentiating:
llvm_unreachable("cannot serialize attribute");

case DAK_Count:
Expand Down Expand Up @@ -2397,26 +2399,6 @@ void Serializer::writeDeclAttribute(const DeclAttribute *DA) {
writeGenericRequirements(attr->getRequirements(), DeclTypeAbbrCodes);
return;
}

// SWIFT_ENABLE_TENSORFLOW
case DAK_Differentiating: {
auto abbrCode = DeclTypeAbbrCodes[DifferentiatingDeclAttrLayout::Code];
auto attr = cast<DifferentiatingAttr>(DA);
IdentifierID origName =
addDeclBaseNameRef(attr->getOriginal().Name.getBaseName());
DeclID origRef = addDeclRef(attr->getOriginalFunction());

auto paramIndices = attr->getParameterIndices();
assert(paramIndices && "Checked parameter indices must be resolved");
SmallVector<bool, 4> indices;
for (unsigned i : swift::indices(paramIndices->parameters))
indices.push_back(paramIndices->parameters[i]);

DifferentiatingDeclAttrLayout::emitRecord(
Out, ScratchRecord, abbrCode, attr->isImplicit(), origName, origRef,
indices);
return;
}
}
}

Expand Down
23 changes: 10 additions & 13 deletions test/Serialization/differentiating_attr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@

// BCANALYZER-NOT: UnknownCode

// CHECK: @differentiable(wrt: x, jvp: jvpAddWrtX)
// CHECK-NEXT: @differentiable(vjp: vjpAdd)
func add(x: Float, y: Float) -> Float {
return x + y
}
// CHECK: @differentiating(add, wrt: x)
// CHECK-NEXT: func jvpAddWrtX(x: Float, y: Float) -> (value: Float, pullback: (Float) -> (Float))
@differentiating(add, wrt: x)
func jvpAddWrtX(x: Float, y: Float) -> (value: Float, pullback: (Float) -> (Float)) {
func jvpAddWrtX(x: Float, y: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x + y, { $0 })
}
// CHECK: @differentiating(add)
// CHECK-NEXT: func vjpAddWrtXY(x: Float, y: Float) -> (value: Float, pullback: (Float) -> (Float, Float))
@differentiating(add)
func vjpAddWrtXY(x: Float, y: Float) -> (value: Float, pullback: (Float) -> (Float, Float)) {
func vjpAdd(x: Float, y: Float) -> (value: Float, pullback: (Float) -> (Float, Float)) {
return (x + y, { ($0, $0) })
}

// CHECK: @differentiable(vjp: vjpGeneric where T : Differentiable)
func generic<T : Numeric>(x: T) -> T {
return x
}
// CHECK: @differentiating(generic)
// CHECK-NEXT: func vjpGeneric<T>(x: T) -> (value: T, pullback: (T.CotangentVector) -> T.CotangentVector)
@differentiating(generic)
func vjpGeneric<T>(x: T) -> (value: T, pullback: (T.CotangentVector) -> T.CotangentVector)
where T : Numeric, T : Differentiable
Expand All @@ -36,21 +33,21 @@ func vjpGeneric<T>(x: T) -> (value: T, pullback: (T.CotangentVector) -> T.Cotang
}

protocol InstanceMethod : Differentiable {
// CHECK: @differentiable(vjp: vjpFoo)
func foo(_ x: Self) -> Self
// CHECK: @differentiable(jvp: jvpBarWrt where T == T.TangentVector)
func bar<T : Differentiable>(_ x: T) -> Self
}
extension InstanceMethod {
// CHECK: @differentiating(foo)
// CHECK-NEXT: func vjpFoo(x: Self) -> (value: Self, pullback: (Self.CotangentVector) -> (Self.CotangentVector, Self.CotangentVector))
@differentiating(foo)
func vjpFoo(x: Self) -> (value: Self, pullback: (Self.CotangentVector) -> (Self.CotangentVector, Self.CotangentVector)) {
return (x, { ($0, $0) })
}

// CHECK: @differentiating(bar)
// CHECK-NEXT: func jvpBarWrt<T>(_ x: T) -> (value: Self, differential: (Self.TangentVector, T.TangentVector) -> Self.TangentVector) where T : Differentiable
@differentiating(bar, wrt: (self, x))
func jvpBarWrt<T : Differentiable>(_ x: T) -> (value: Self, differential: (Self.TangentVector, T.TangentVector) -> Self.TangentVector) {
func jvpBarWrt<T : Differentiable>(_ x: T) -> (value: Self, differential: (Self.TangentVector, T) -> Self.TangentVector)
where T == T.TangentVector
{
return (self, { dself, dx in dself })
}
}