Skip to content

Commit

Permalink
Add unittest to coverage SgdThreadUpdater's enableBufType
Browse files Browse the repository at this point in the history
  • Loading branch information
reyoung committed Dec 15, 2016
1 parent dd894c2 commit 22b9b66
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions paddle/trainer/ThreadParameterUpdater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ void SgdThreadUpdater::init(std::vector<ParameterPtr>& parameters) {
// not create parameter buf for PARAMETER_GRADIENT for sparse update in
// Parameter::enableType(). But gradient parameter buf is still used
// in SgdThreadUpdater. We need to explicitly create it.
//
// The AverageOptimizer::restore/apply method will use PARAMETER_GRADIENT
// as a temp buffer.
para->enableBufType(PARAMETER_GRADIENT);
}
}
Expand Down
1 change: 1 addition & 0 deletions paddle/trainer/tests/fake_file_list.list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
do_not_matter.txt
23 changes: 23 additions & 0 deletions paddle/trainer/tests/simple_sparse_neural_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from paddle.trainer_config_helpers import *

settings(batch_size=128, learning_method=AdaGradOptimizer(), learning_rate=1e-4)

file_list = 'trainer/tests/fake_file_list.list'

define_py_data_sources2(
train_list=file_list,
test_list=file_list,
module="simple_sparse_neural_network_dp",
obj="process")

embedding = embedding_layer(
input=data_layer(
name="word_ids", size=65536),
size=128,
param_attr=ParamAttr(sparse_update=True))
prediction = fc_layer(input=embedding, size=10, act=SoftmaxActivation())

outputs(
classification_cost(
input=prediction, label=data_layer(
name='label', size=10)))
21 changes: 21 additions & 0 deletions paddle/trainer/tests/simple_sparse_neural_network_dp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from paddle.trainer.PyDataProvider2 import provider, integer_sequence, integer_value
import random


def init_hook(settings, is_train, **kwargs):
settings.is_train = is_train


@provider(
input_types={'word_ids': integer_value(65536),
'label': integer_value(10)},
min_pool_size=0,
init_hook=init_hook)
def process(settings, filename):
if settings.is_train:
data_size = 2**20
else:
data_size = 2**10

for _ in xrange(data_size):
yield random.randint(0, 65535), random.randint(0, 9)
9 changes: 8 additions & 1 deletion paddle/trainer/tests/test_TrainerOnePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ static const string& configFile1 = "trainer/tests/sample_trainer_config.conf";
static const string& configFile2 =
"trainer/tests/sample_trainer_config_parallel.conf";

static const string& configFileSimpleSparse =
"trainer/tests/simple_sparse_neural_network.py";

DECLARE_bool(use_gpu);
DECLARE_string(config);
DECLARE_int32(gpu_id);
Expand Down Expand Up @@ -298,11 +301,15 @@ TEST(checkRemoteUpdater, cpuDeltaTrainerOldUpdater) {
checkRemoteParameterUpdaterTest(configFile1, false, false, 1, true, 10);
}

TEST(SgdThreadUpdater, simpleSparseNN) {
trainerOnePassTest(configFileSimpleSparse, false, false, 1, 0.5, true);
}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
initMain(argc, argv);
initPython(argc, argv);
gNumDevices = hl_get_device_count();
testing::InitGoogleTest(&argc, argv);

FLAGS_num_passes = 1; // train one pass
FLAGS_saving_period = 100000; // do not save parameteres
Expand Down

0 comments on commit 22b9b66

Please sign in to comment.