Skip to content

Commit a4f2540

Browse files
nickggfacebook-github-bot
authored andcommitted
Preserve output PH names in C2 loader (#3798)
Summary: It's important to preserve the name of each external output in outside models, but since they must be unique with Node names we'll need to claim them before naming any nodes. This fixes the C2 Model Loader to do that. Documentation: Fixes #3774 Pull Request resolved: #3798 Test Plan: ninja test, image-classifier with -dump-ir to make sure weights were named right. Differential Revision: D18630019 Pulled By: nickgg fbshipit-source-id: 0b0e771c0e6489384f7e40a772178bf45ff64b0e
1 parent 5beebe1 commit a4f2540

File tree

6 files changed

+24
-6
lines changed

6 files changed

+24
-6
lines changed

examples/bundles/lenet_mnist/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ uint8_t activations[LENET_MNIST_ACTIVATIONS_MEM_SIZE];
222222
uint8_t *inputAddr = GLOW_GET_ADDR(mutableWeight, LENET_MNIST_data);
223223

224224
/// Bundle output data absolute address.
225-
uint8_t *outputAddr = GLOW_GET_ADDR(mutableWeight, LENET_MNIST_softmax__1);
225+
uint8_t *outputAddr = GLOW_GET_ADDR(mutableWeight, LENET_MNIST_softmax);
226226

227227
/// Copy the pre-processed images into the mutable region of the bundle.
228228
static void initInputImages() {

examples/bundles/resnet50/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static uint8_t *allocateMutableWeightVars(const BundleConfig &config) {
300300
static void dumpInferenceResults(const BundleConfig &config,
301301
uint8_t *mutableWeightVars) {
302302
const SymbolTableEntry &outputWeights =
303-
getMutableWeightVar(config, "gpu_0_softmax__1");
303+
getMutableWeightVar(config, "gpu_0_softmax");
304304
int maxIdx = 0;
305305
float maxValue = 0;
306306
float *results = (float *)(mutableWeightVars + outputWeights.offset);

examples/resnet-runtime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void dispatchClassify(unsigned int id, HostManager *hostManager,
101101
EXIT_ON_ERR(std::move(err));
102102
auto *bindings = context->getPlaceholderBindings();
103103
size_t maxIdx =
104-
bindings->get(bindings->getPlaceholderByName("gpu_0_softmax__1"))
104+
bindings->get(bindings->getPlaceholderByName("gpu_0_softmax"))
105105
->getHandle()
106106
.minMaxArg()
107107
.second;

include/glow/Graph/Graph.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ class Module final {
109109
usedNodeNames_.insert(name);
110110
}
111111

112+
/// Registers a name as used by a Storage node (Constant or Placeholder) in
113+
/// this module.
114+
void registerStorageName(llvm::StringRef name) {
115+
usedStorageNames_.insert(name);
116+
}
117+
112118
/// Return a pointer to a uniqued type \p T.
113119
TypeRef uniqueType(const Type &T);
114120

lib/Importer/Caffe2ModelLoader.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,12 @@ Error Caffe2ModelLoader::loadInputs(const caffe2::NetDef &net,
14441444
}
14451445

14461446
Error Caffe2ModelLoader::loadNetwork(caffe2::NetDef &net) {
1447+
// Make a claim on the unique name of all output Placeholders.
1448+
for (int i = 0; i < net.external_output_size(); i++) {
1449+
auto &outputName = net.external_output(i);
1450+
G_.getParent()->registerStorageName(legalizeName(outputName));
1451+
}
1452+
14471453
/// Load the network operators:
14481454
for (int i = 0; i < net.op_size(); i++) {
14491455
auto &op = net.op(i);
@@ -1464,8 +1470,13 @@ Error Caffe2ModelLoader::loadNetwork(caffe2::NetDef &net) {
14641470
auto &outputName = net.external_output(i);
14651471
NodeValue r;
14661472
ASSIGN_VALUE_OR_RETURN_ERR(r, getNodeValueByName(outputName));
1467-
auto *PH =
1468-
G_.getParent()->createPlaceholder(r.getType(), outputName, false);
1473+
1474+
PlaceholderList &PHList = G_.getParent()->getPlaceholders();
1475+
// Create a Placeholder with the previously claimed name.
1476+
auto *PH = new Placeholder(legalizeName(outputName),
1477+
G_.getParent()->uniqueType(*r.getType()), false,
1478+
ANY_LAYOUT);
1479+
PHList.push_back(PH);
14691480
auto *SN = G_.createSave("save_" + outputName, r, PH);
14701481
outputVarsByName_[outputName] = SN->getPlaceholder();
14711482
}

tests/unittests/Caffe2ImporterTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2551,7 +2551,8 @@ TEST(caffe2, importNames) {
25512551
Tensor input(ElemKind::FloatTy, {6});
25522552
Caffe2ModelLoader caffe2LD(NetDescFilename, NetWeightFilename,
25532553
{"sigmoid_test_input"}, {&input.getType()}, *F);
2554-
EXPECT_TRUE(F->getNodeByName("sigmoid_test_output"));
2554+
EXPECT_TRUE(mod.getPlaceholderByName("sigmoid_test_output"));
2555+
EXPECT_TRUE(F->getNodeByName("sigmoid_test_output__1"));
25552556
}
25562557

25572558
TEST(caffe2, importSqr) {

0 commit comments

Comments
 (0)