Skip to content

Commit 334f471

Browse files
PetroZarytskyivgvassilev
authored andcommitted
Remove DiffMode::error_estimation by merging it with DiffMode::reverse
Unlike pullback requests, gradient requests track information about error estimation by having 2 separate modes: `DiffMode::reverse` and `DiffMode::error_estimation`. Even though `DiffMode::error_estimation` is immediately changed to `DiffMode::reverse` at the beginning of the reverse mode visitation. This PR also removes `DiffMode::error_estimation` completely, as this information is already tracked with `EnableErrorEstimation`.
1 parent ff26aa8 commit 334f471

6 files changed

Lines changed: 8 additions & 29 deletions

File tree

include/clad/Differentiator/DiffMode.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ enum class DiffMode {
1313
hessian,
1414
hessian_diagonal,
1515
jacobian,
16-
reverse_mode_forward_pass,
17-
error_estimation
16+
reverse_mode_forward_pass
1817
};
1918

2019
/// Convert enum value to string.
@@ -40,8 +39,6 @@ inline const char* DiffModeToString(DiffMode mode) {
4039
return "jacobian";
4140
case DiffMode::reverse_mode_forward_pass:
4241
return "reverse_forw";
43-
case DiffMode::error_estimation:
44-
return "error_estimation";
4542
default:
4643
return "unknown";
4744
}

include/clad/Differentiator/DiffPlanner.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ struct DiffRequest {
104104
bool ImmediateMode = false;
105105
/// A flag specifying whether this differentiation is to be used
106106
/// for error estimation.
107-
/// FIXME: Should this be reflected in the DiffMode?
108107
bool EnableErrorEstimation = false;
109108
/// Puts the derived function and its code in the diff call
110109
void updateCall(clang::FunctionDecl* FD, clang::FunctionDecl* OverloadedFD,

lib/Differentiator/CladUtils.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,8 +1187,7 @@ namespace clad {
11871187
return resType;
11881188
}
11891189

1190-
if (Mode == DiffMode::reverse || Mode == DiffMode::pullback ||
1191-
Mode == DiffMode::error_estimation) {
1190+
if (Mode == DiffMode::reverse || Mode == DiffMode::pullback) {
11921191
QualType ValueType = GetNonConstValueType(Type);
11931192
QualType nonRefValueType = ValueType.getNonReferenceType();
11941193
return C.getPointerType(nonRefValueType);
@@ -1253,7 +1252,6 @@ namespace clad {
12531252
QualType dRetTy = C.VoidTy;
12541253
bool returnVoid = mode == DiffMode::reverse ||
12551254
mode == DiffMode::pullback ||
1256-
mode == DiffMode::error_estimation ||
12571255
mode == DiffMode::vector_forward_mode;
12581256
if (mode == DiffMode::reverse_mode_forward_pass) {
12591257
if (isMemoryType(oRetTy) || isa<CXXConstructorDecl>(FD)) {

lib/Differentiator/DerivativeBuilder.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,8 @@ static void registerDerivative(Decl* D, Sema& S, const DiffRequest& R) {
552552
} else if (request.Mode == DiffMode::vector_pushforward) {
553553
VectorPushForwardModeVisitor V(*this, request);
554554
result = V.Derive();
555-
} else if (request.Mode == DiffMode::reverse) {
556-
ReverseModeVisitor V(*this, request);
557-
result = V.Derive();
558-
} else if (request.Mode == DiffMode::pullback) {
555+
} else if (request.Mode == DiffMode::reverse ||
556+
request.Mode == DiffMode::pullback) {
559557
ErrorEstimationHandler handler;
560558
std::unique_ptr<FPErrorEstimationModel> model;
561559
ReverseModeVisitor V(*this, request);
@@ -575,14 +573,6 @@ static void registerDerivative(Decl* D, Sema& S, const DiffRequest& R) {
575573
} else if (request.Mode == DiffMode::jacobian) {
576574
JacobianModeVisitor J(*this, request);
577575
result = J.Derive();
578-
} else if (request.Mode == DiffMode::error_estimation) {
579-
ErrorEstimationHandler handler;
580-
FPErrorEstimationModel model(*this, request);
581-
handler.SetErrorEstimationModel(&model);
582-
ReverseModeVisitor R(*this, request);
583-
R.AddExternalSource(handler);
584-
// Finally begin estimation.
585-
result = R.Derive();
586576
} else if (const VarDecl* VD = request.Global) {
587577
// The request represents a global variable, construct the adjoint and
588578
// register it.

lib/Differentiator/DiffPlanner.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ static QualType GetDerivedFunctionType(const CallExpr* CE) {
812812
std::string Annotation = A->getAnnotation().str();
813813
if (Annotation == "E") {
814814
// Error estimation has no options yet.
815-
request.Mode = DiffMode::error_estimation;
815+
request.Mode = DiffMode::reverse;
816816
request.EnableErrorEstimation = true;
817817
return false;
818818
}
@@ -827,9 +827,7 @@ static QualType GetDerivedFunctionType(const CallExpr* CE) {
827827
request.Mode = DiffMode::reverse;
828828
else
829829
llvm_unreachable("unknown mode");
830-
if (request.Mode == DiffMode::reverse ||
831-
request.Mode == DiffMode::hessian ||
832-
request.Mode == DiffMode::error_estimation)
830+
if (request.Mode == DiffMode::reverse || request.Mode == DiffMode::hessian)
833831
request.EnableTBRAnalysis = ReqOpts.EnableTBRAnalysis;
834832
request.EnableVariedAnalysis = ReqOpts.EnableVariedAnalysis;
835833
request.EnableUsefulAnalysis = ReqOpts.EnableUsefulAnalysis;
@@ -1184,6 +1182,7 @@ static QualType GetDerivedFunctionType(const CallExpr* CE) {
11841182
request.CallContext = E;
11851183
bool canUsePushforwardInRevMode =
11861184
m_TopMostReq->Mode == DiffMode::reverse &&
1185+
!request.EnableErrorEstimation &&
11871186
utils::canUsePushforwardInRevMode(FD);
11881187

11891188
std::string FDName = FD->getNameAsString();
@@ -1201,8 +1200,7 @@ static QualType GetDerivedFunctionType(const CallExpr* CE) {
12011200
m_TopMostReq->Mode == DiffMode::hessian ||
12021201
canUsePushforwardInRevMode)
12031202
request.Mode = DiffMode::pushforward;
1204-
else if (m_TopMostReq->Mode == DiffMode::reverse ||
1205-
m_TopMostReq->Mode == DiffMode::error_estimation)
1203+
else if (m_TopMostReq->Mode == DiffMode::reverse)
12061204
request.Mode = DiffMode::pullback;
12071205
else if (m_TopMostReq->Mode == DiffMode::vector_forward_mode ||
12081206
m_TopMostReq->Mode == DiffMode::jacobian ||

lib/Differentiator/ReverseModeVisitor.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,6 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
255255

256256
if (m_ExternalSource)
257257
m_ExternalSource->ActOnStartOfDerive();
258-
if (m_DiffReq.Mode == DiffMode::error_estimation)
259-
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
260-
const_cast<DiffRequest&>(m_DiffReq).Mode = DiffMode::reverse;
261258

262259
QualType returnTy = m_DiffReq->getReturnType();
263260
// If reverse mode differentiates only part of the arguments it needs to

0 commit comments

Comments
 (0)