Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
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
2 changes: 2 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ try {
make('build_cuda', flag)
pack_lib('gpu')
stash includes: 'build/cpp-package/example/test_score', name: 'cpp_test_score'
stash includes: 'build/cpp-package/example/test_optimizer', name: 'cpp_test_optimizer'
}
}
},
Expand Down Expand Up @@ -658,6 +659,7 @@ try {
init_git()
unpack_lib('gpu')
unstash 'cpp_test_score'
unstash 'cpp_test_optimizer'
timeout(time: max_time, unit: 'MINUTES') {
sh "${docker_run} gpu --dockerbinary nvidia-docker cpp-package/tests/ci_test.sh"
}
Expand Down
32 changes: 32 additions & 0 deletions cpp-package/example/test_optimizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "mxnet-cpp/MxNetCpp.h"

using namespace std;
using namespace mxnet::cpp;

int main(int argc, char** argv) {
// Confirm >1 optimizers can be created w/o error
Optimizer* opt = OptimizerRegistry::Find("sgd");
opt = OptimizerRegistry::Find("adam");
int ret = (opt == 0) ? 1 : 0;

MXNotifyShutdown();
return ret;
}
17 changes: 10 additions & 7 deletions cpp-package/include/mxnet-cpp/optimizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ inline float Optimizer::GetWD_(int index) {
}

inline Optimizer* OptimizerRegistry::Find(const std::string& name) {
MXNETCPP_REGISTER_OPTIMIZER(sgd, SGDOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(ccsgd, SGDOptimizer); // For backward compatibility
MXNETCPP_REGISTER_OPTIMIZER(rmsprop, RMSPropOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(adam, AdamOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(adagrad, AdaGradOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(adadelta, AdaDeltaOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(signum, SignumOptimizer);
if (cmap().empty()) {
Copy link
Copy Markdown
Contributor

@lebeg lebeg Feb 20, 2018

Choose a reason for hiding this comment

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

You are checking all optimizers at once, but wouldn't it be better to check every optimizer separately? AFAICT Registry::__REGISTER__ is used, but Registry::__REGISTER_OR_GET__ would make more sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Optimizers are all registered at once in the original CPP-Package code and in the Python code using decorators. In the original code #5251, they were all registered at once in the file, so when the program was initialized, all optimizers would already be registered (which is the case in Python), not in the first call to the function Find, but that caused multiple definition errors, so in #5545, the were all registered at once in the first call to Find, which caused the inability to have more than one optimizer bc they would be re-registered with each call, so this change makes sure they are registered once. Why check every optimizer separately when they should all be registered at the same time and only once?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's strange for a look-up function to register optimizers. Would it make more sense to have a macro to handle the multiple-definition problem for built-in optimizers?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not exactly sure what you mean, but it sounds like you want to revert to the original code, but that would break #5545. @sifmelcara why in #5545 did you
"3. In optimizer.hpp, I delayed the registration of sgd optimizer to the first call of OptimizerRegistry::Find."?

// Optimizers should only be registered once
MXNETCPP_REGISTER_OPTIMIZER(sgd, SGDOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(ccsgd, SGDOptimizer); // For backward compatibility
MXNETCPP_REGISTER_OPTIMIZER(rmsprop, RMSPropOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(adam, AdamOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(adagrad, AdaGradOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(adadelta, AdaDeltaOptimizer);
MXNETCPP_REGISTER_OPTIMIZER(signum, SignumOptimizer);
}
auto it = cmap().find(name);
if (it == cmap().end())
return nullptr;
Expand Down
3 changes: 3 additions & 0 deletions cpp-package/tests/ci_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export LD_LIBRARY_PATH=$(readlink -f ../../lib):$LD_LIBRARY_PATH
echo $LD_LIBRARY_PATH
ls -l ../../lib/

cp ../../build/cpp-package/example/test_optimizer .
./test_optimizer

cp ../../build/cpp-package/example/test_score .
./get_mnist.sh
./test_score 0.93