Skip to content

Commit 16dd2fd

Browse files
Man Wangfacebook-github-bot
Man Wang
authored andcommitted
Fix Unchecked errors in Partitioner #3505 (#3511)
Summary: Fix unchecked errors in Partitioner, rewrite the way to report an error. #3505 Documentation: [Optional Fixes] Pull Request resolved: #3511 Test Plan: ninja test. Please see a detailed explanation of how to fill out the fields in the relevant sections in PULL_REQUEST.md. Differential Revision: D17403326 Pulled By: beicy fbshipit-source-id: fbab4dbf1d235e148e40b344665459533f60fdde
1 parent 2d7b7cb commit 16dd2fd

File tree

4 files changed

+62
-35
lines changed

4 files changed

+62
-35
lines changed

include/glow/Support/Error.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class GlowErr final : public llvm::ErrorInfo<GlowErr> {
6969
MODEL_LOADER_UNSUPPORTED_ONNX_VERSION,
7070
// Model loader encountered an invalid protobuf.
7171
MODEL_LOADER_INVALID_PROTOBUF,
72+
// Partitioner error.
73+
PARTITIONER_ERROR,
7274
// Runtime error, out of device memory.
7375
RUNTIME_ERROR,
7476
// Runtime error, out of device memory.
@@ -148,6 +150,8 @@ class GlowErr final : public llvm::ErrorInfo<GlowErr> {
148150
return "MODEL_LOADER_UNSUPPORTED_ONNX_VERSION";
149151
case ErrorCode::MODEL_LOADER_INVALID_PROTOBUF:
150152
return "MODEL_LOADER_INVALID_PROTOBUF";
153+
case ErrorCode::PARTITIONER_ERROR:
154+
return "PARTITIONER_ERROR";
151155
case ErrorCode::RUNTIME_ERROR:
152156
return "RUNTIME_ERROR";
153157
case ErrorCode::RUNTIME_OUT_OF_DEVICE_MEMORY:

lib/Partitioner/Partitioner.cpp

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ llvm::Error Partitioner::finalize(const DAGListTy &partitions,
7777

7878
// Validate the functions after partitioning.
7979
for (Function *subF : module_->getFunctions()) {
80-
RETURN_ERR_IF_NOT(subF->verify(),
81-
strFormat("Conversion led to invalid function: %s",
82-
subF->getName().str().data()));
80+
if (!subF->verify()) {
81+
return MAKE_ERR(GlowErr::ErrorCode::PARTITIONER_ERROR,
82+
"Conversion led to invalid function " +
83+
subF->getName().str());
84+
}
8385
}
8486

8587
if (logPartition) {
@@ -94,8 +96,10 @@ llvm::Error Partitioner::finalize(const DAGListTy &partitions,
9496
if (dumpPartition) {
9597
for (const auto &node : partitions[0].nodes) {
9698
Function *subF = module_->getFunction(node->name);
97-
RETURN_ERR_IF_NOT(
98-
subF, strFormat("Invalid function name %s.", node->name.data()));
99+
if (!subF) {
100+
return MAKE_ERR(GlowErr::ErrorCode::PARTITIONER_ERROR,
101+
"Invalid function name " + node->name);
102+
}
99103
subF->dumpDAG("partitionLogicalID" +
100104
std::to_string(node->logicalDevices[0]) + "__" +
101105
subF->getName().str() + "__" + node->backendName + ".dot");
@@ -284,8 +288,10 @@ llvm::Expected<DAGListTy> Partitioner::backendBasedPartition(
284288
break;
285289
}
286290
}
287-
RETURN_ERR_IF_NOT(nodeToBackendName.find(&N) != nodeToBackendName.end(),
291+
if (nodeToBackendName.find(&N) == nodeToBackendName.end()) {
292+
return MAKE_ERR(GlowErr::ErrorCode::PARTITIONER_ERROR,
288293
"Node is not supported by any of the provided backends");
294+
}
289295
}
290296

291297
BFSLevel bfs = getBFSLevel(F);
@@ -427,11 +433,13 @@ llvm::Expected<DAGListTy> Partitioner::createDAGWithoutPartition(
427433
llvm::Expected<DAGListTy>
428434
Partitioner::loadBalancedPartition(CompilationContext &cctx,
429435
size_t numDevices) {
430-
RETURN_ERR_IF_NOT(
431-
module_->getFunctions().size() == 1,
432-
strFormat("Invalid : %lu functions in a module. Now in load-balanced "
433-
"partition flow, the module can only contain 1 function",
434-
module_->getFunctions().size()));
436+
if (module_->getFunctions().size() != 1) {
437+
return MAKE_ERR(
438+
GlowErr::ErrorCode::PARTITIONER_ERROR,
439+
strFormat("Invalid : %lu functions in a module. Now in load-balanced "
440+
"partition flow, the module can only contain 1 function",
441+
module_->getFunctions().size()));
442+
}
435443

436444
if (multiBackendNames_) {
437445
VLOG(1) << "For multi backend types, load-balanced partition can't be "
@@ -570,8 +578,10 @@ Partitioner::loadBalancedPartition(CompilationContext &cctx,
570578
}
571579

572580
// Throw error if we were not able to put this node into any partition
573-
RETURN_ERR_IF_NOT(curPartition < numDevices,
581+
if (curPartition >= numDevices) {
582+
return MAKE_ERR(GlowErr::ErrorCode::PARTITIONER_ERROR,
574583
"Load balance partition error");
584+
}
575585
}
576586
}
577587
for (size_t i = 0; i < numDevices; i++) {
@@ -601,12 +611,14 @@ llvm::Expected<DAGListTy>
601611
Partitioner::quantizationProfilingPartition(CompilationContext &cctx) {
602612
// For quantization profiling flow, currently we assume there is only 1
603613
// function in a module.
604-
RETURN_ERR_IF_NOT(
605-
module_->getFunctions().size() == 1,
606-
strFormat(
607-
"Invalid : %lu functions in a module. In quantization profiling "
608-
"partition flow, the module can only contain 1 function",
609-
module_->getFunctions().size()));
614+
if (module_->getFunctions().size() != 1) {
615+
return MAKE_ERR(
616+
GlowErr::ErrorCode::PARTITIONER_ERROR,
617+
strFormat(
618+
"Invalid : %lu functions in a module. In quantization profiling "
619+
"partition flow, the module can only contain 1 function",
620+
module_->getFunctions().size()));
621+
}
610622

611623
// Quantization profiling flow is run under CPU backend, so we don't really
612624
// need the concrete partition. The backendBasedPartition is necessary since
@@ -668,19 +680,24 @@ Partitioner::heterogeneousPartition(CompilationContext &cctx) {
668680
}
669681
// NOTE: the following error detection will be removed once multi-functions
670682
// in a module is supported.
671-
RETURN_ERR_IF_NOT(
672-
module_->getFunctions().size() == 1,
673-
strFormat("Invalid : %lu functions in a module. Now in heterogeneous "
674-
"partition flow, the module can only contain 1 function",
675-
module_->getFunctions().size()));
683+
if (module_->getFunctions().size() != 1) {
684+
return MAKE_ERR(
685+
GlowErr::ErrorCode::PARTITIONER_ERROR,
686+
strFormat("Invalid : %lu functions in a module. Now in heterogeneous "
687+
"partition flow, the module can only contain 1 function",
688+
module_->getFunctions().size()));
689+
}
676690
} else {
677691
// NOTE: the following error detection will be removed once multi-functions
678692
// in a module is supported.
679-
RETURN_ERR_IF_NOT(
680-
module_->getFunctions().size() == 1,
681-
strFormat("Invalid : %lu functions in a module. Now in heterogeneous "
682-
"partition flow, the module can only contain 1 function",
683-
module_->getFunctions().size()));
693+
if (module_->getFunctions().size() != 1) {
694+
return MAKE_ERR(
695+
GlowErr::ErrorCode::PARTITIONER_ERROR,
696+
strFormat(
697+
"Invalid : %lu functions in a module. Now in heterogeneous partition\
698+
flow, the module can only contain 1 function",
699+
module_->getFunctions().size()));
700+
}
684701
ASSIGN_VALUE_OR_RETURN_ERR(
685702
partitions, backendBasedPartition(funcToBackend, F_, backends, cctx));
686703
module_->eraseFunction(F_);
@@ -750,8 +767,11 @@ Partitioner::partitionFromConfig(const PartitionConfig &partitionConfig) {
750767
std::vector<Backend *> backends;
751768
genBackendMap(backendMap_, backendHolder, backends);
752769
Function *F = module_->getFunction(partitionConfig.funcName);
753-
RETURN_ERR_IF_NOT(F, strFormat("Can't find function %s in current module.",
754-
F->getName().str().data()));
770+
if (!F) {
771+
return MAKE_ERR(GlowErr::ErrorCode::PARTITIONER_ERROR,
772+
strFormat("Can't find function %s in current module.",
773+
F->getName().str().data()));
774+
}
755775

756776
DCHECK(
757777
partitionConfig.numOfPartitions == partitionConfig.backendNames.size() &&

lib/Runtime/HostManager/HostManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,11 @@ llvm::Error HostManager::addNetwork(std::unique_ptr<Module> module,
187187
if (cctx.precisionConfig.quantMode == QuantizationMode::Profile) {
188188
// Since for profiling the provisioner will be reset, we only allow one
189189
// network in one HM.
190-
RETURN_ERR_IF_NOT(networks_.size() == 0,
190+
if (networks_.size() > 0) {
191+
return MAKE_ERR(GlowErr::ErrorCode::RUNTIME_ERROR,
191192
"For quantization profiling flow, there can't be other "
192193
"registered networks before this one");
194+
}
193195
// For profiling, we use CPU backend. Overwrite Provisioner and Executor
194196
// to force the network is compiled and run in profilingBackend. backend.
195197
size_t devicesNum = devices_.size();

tests/unittests/PartitionerTest.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ TEST_F(PartitionerTest, Basic2) {
250250
Partitioner myPartitioner(&EEP.getModule(), devices, /* saturateHost */ true);
251251
CompilationContext cctx;
252252
auto dagList = myPartitioner.partition(cctx);
253+
EXPECT_TRUE((bool)dagList);
253254
EXPECT_EQ(EEP.getModule().getFunctions().size(), 2);
254255
EXPECT_EQ(dagList->size(), 1);
255256
ASSERT_TRUE(checkSaveNode(EEP.getModule()));
@@ -343,7 +344,7 @@ TEST_F(PartitionerTest, Error1) {
343344
Partitioner myPartitioner(&EEP.getModule(), devices);
344345
CompilationContext cctx;
345346
auto dagList = myPartitioner.partition(cctx);
346-
EXPECT_FALSE((bool)dagList);
347+
EXPECT_TRUE(glow::errToBool(dagList.takeError()));
347348
}
348349

349350
/// This one tests the roofline computed with compute, memory and
@@ -890,7 +891,7 @@ TEST_F(PartitionerTest, memoryUsageValidation1) {
890891
Partitioner myPartitioner(&mod_, devices);
891892
CompilationContext cctx;
892893
auto dagList = myPartitioner.partition(cctx);
893-
EXPECT_FALSE((bool)dagList);
894+
EXPECT_TRUE(glow::errToBool(dagList.takeError()));
894895
}
895896

896897
/// This one test dagValidation in partitioner : p1->p2, p2->p1.
@@ -919,7 +920,7 @@ TEST_F(PartitionerTest, dagValidation1) {
919920
auto partitioner = Partitioner(&mod_, devices, false, false, partitionConfig);
920921
CompilationContext cctx;
921922
auto dagList = partitioner.partition(cctx);
922-
EXPECT_FALSE((bool)dagList);
923+
EXPECT_TRUE(glow::errToBool(dagList.takeError()));
923924
}
924925

925926
/// This one test dagValidation in partitioner: p0->p1, p1->p2, p2->p1.
@@ -951,7 +952,7 @@ TEST_F(PartitionerTest, dagValidation2) {
951952
auto partitioner = Partitioner(&mod_, devices, false, false, partitionConfig);
952953
CompilationContext cctx;
953954
auto dagList = partitioner.partition(cctx);
954-
EXPECT_FALSE((bool)dagList);
955+
EXPECT_TRUE(glow::errToBool(dagList.takeError()));
955956
}
956957

957958
/// This one tests partition from a user-defined config.

0 commit comments

Comments
 (0)