|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | +#include "paddle/pserver/ParameterServer2.h" |
| 17 | +#include "paddle/trainer/Trainer.h" |
| 18 | +#include "paddle/utils/PythonUtil.h" |
| 19 | +#include "paddle/utils/Util.h" |
| 20 | + |
| 21 | +P_DECLARE_bool(local); |
| 22 | + |
| 23 | +static std::unique_ptr<paddle::Trainer> createTrainer( |
| 24 | + bool useGpu, size_t trainerCount, const std::string& configFilename) { |
| 25 | + FLAGS_use_gpu = useGpu; |
| 26 | + FLAGS_trainer_count = trainerCount; |
| 27 | + paddle::Trainer* trainer = new paddle::Trainer(); |
| 28 | + |
| 29 | + trainer->init(paddle::TrainerConfigHelper::create(configFilename)); |
| 30 | + return std::unique_ptr<paddle::Trainer>(trainer); |
| 31 | +} |
| 32 | + |
| 33 | +TEST(SgdLocalUpdater, RemoteSparseNNCpu) { |
| 34 | + FLAGS_ports_num_for_sparse = 1; |
| 35 | + FLAGS_num_passes = 1; |
| 36 | + FLAGS_local = false; |
| 37 | + std::vector<std::shared_ptr<paddle::ParameterServer2>> pservers; |
| 38 | + |
| 39 | + for (int i = 0; i < FLAGS_ports_num + FLAGS_ports_num_for_sparse; ++i) { |
| 40 | + auto pserver = |
| 41 | + std::make_shared<paddle::ParameterServer2>("127.0.0.1", FLAGS_port + i); |
| 42 | + pserver->init(); |
| 43 | + pserver->start(); |
| 44 | + pservers.push_back(pserver); |
| 45 | + } |
| 46 | + |
| 47 | + auto trainerPtr = createTrainer(false, 1, "sparse_updated_network.py"); |
| 48 | + ASSERT_TRUE(trainerPtr != nullptr); |
| 49 | + paddle::Trainer& trainer = *trainerPtr; |
| 50 | + trainer.startTrain(); |
| 51 | + trainer.train(1); |
| 52 | + trainer.finishTrain(); |
| 53 | +} |
| 54 | + |
| 55 | +TEST(SgdLocalUpdater, LocalSparseNNCpu) { |
| 56 | + FLAGS_local = true; |
| 57 | + auto trainerPtr = createTrainer(false, 1, "sparse_updated_network.py"); |
| 58 | + ASSERT_TRUE(trainerPtr != nullptr); |
| 59 | + paddle::Trainer& trainer = *trainerPtr; |
| 60 | + trainer.startTrain(); |
| 61 | + trainer.train(1); |
| 62 | + trainer.finishTrain(); |
| 63 | +} |
| 64 | +// TEST(SgdLocalUpdater, SparseNNGpu) { |
| 65 | +// auto trainerPtr = createTrainer(true, 1, "sparse_updated_network.py"); |
| 66 | +// ASSERT_TRUE(trainerPtr != nullptr); |
| 67 | +// paddle::Trainer& trainer = *trainerPtr; |
| 68 | +// trainer.startTrain(); |
| 69 | +// trainer.train(1); |
| 70 | +// trainer.finishTrain(); |
| 71 | +//} |
| 72 | + |
| 73 | +int main(int argc, char** argv) { |
| 74 | + testing::InitGoogleTest(&argc, argv); |
| 75 | + paddle::initMain(argc, argv); |
| 76 | + paddle::initPython(argc, argv); |
| 77 | + return RUN_ALL_TESTS(); |
| 78 | +} |
0 commit comments