Skip to content

Commit ffc3416

Browse files
luotao1reyoung
authored andcommitted
Add parallel_nn api and unittest (#110)
* Add `device` parameter to ExtraAttr in trainer_config_helpers. * add unittest for it.
1 parent 2289c14 commit ffc3416

File tree

3 files changed

+65
-113
lines changed

3 files changed

+65
-113
lines changed

paddle/trainer/tests/sample_trainer_config_parallel.conf

Lines changed: 44 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -13,137 +13,74 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
#Todo(luotao02) This config is only used for unitest. It is out of date now, and will be updated later.
16+
from paddle.trainer_config_helpers import *
1717

18-
TrainData(
19-
SimpleData(
20-
files = "trainer/tests/sample_filelist.txt",
21-
feat_dim = 3,
22-
context_len = 0,
23-
buffer_capacity = 1000000,
24-
)
25-
)
18+
TrainData(SimpleData(
19+
files = "trainer/tests/sample_filelist.txt",
20+
feat_dim = 3,
21+
context_len = 0,
22+
buffer_capacity = 1000000))
2623

27-
TestData(
28-
SimpleData(
29-
files = "trainer/tests/sample_filelist.txt",
30-
feat_dim = 3,
31-
context_len = 0,
32-
buffer_capacity = 1000000,
33-
)
34-
)
24+
TestData(SimpleData(
25+
files = "trainer/tests/sample_filelist.txt",
26+
feat_dim = 3,
27+
context_len = 0,
28+
buffer_capacity = 1000000))
3529

36-
Settings(
37-
algorithm = "sgd",
38-
num_batches_per_send_parameter = 1,
39-
num_batches_per_get_parameter = 1,
40-
batch_size = 100,
41-
learning_rate = 0.001,
42-
learning_rate_decay_a = 1e-5,
43-
learning_rate_decay_b = 0.5,
44-
)
30+
settings(batch_size = 100)
4531

46-
default_initial_std(0.2)
4732
# Output layer, label layer, cost layer, preferably set to the same environment.
4833
output_device = 0
4934

50-
model_type("nn")
51-
5235
# Input Layer does not need to specify the device number.
53-
Layer(
54-
name = "input",
55-
type = "data",
56-
size = 3,
57-
)
36+
data = data_layer(name='input', size=3)
5837

5938
# Calculate in the CPU.
60-
Layer(
61-
name = "layer1_1",
62-
type = "fc",
63-
size = 5,
64-
active_type = "sigmoid",
65-
device = -1,
66-
inputs = "input",
67-
)
39+
fc1 = fc_layer(input=data, size=5,
40+
bias_attr=True,
41+
layer_attr=ExtraAttr(device=-1),
42+
act=SigmoidActivation())
6843

6944
# Calculate in the GPU 0.
70-
Layer(
71-
name = "layer2_1",
72-
type = "fc",
73-
size = 10,
74-
active_type = "sigmoid",
75-
device = 0,
76-
inputs = "layer1_1",
77-
)
45+
fc2 = fc_layer(input=fc1, size=10,
46+
bias_attr=True,
47+
layer_attr=ExtraAttr(device=0),
48+
act=SigmoidActivation())
7849

7950
# Calculate in the GPU 1.
80-
Layer(
81-
name = "layer2_2",
82-
type = "fc",
83-
size = 10,
84-
active_type = "sigmoid",
85-
device = 1,
86-
inputs = "layer1_1",
87-
)
51+
fc3 = fc_layer(input=fc1, size=10,
52+
bias_attr=True,
53+
layer_attr=ExtraAttr(device=1),
54+
act=SigmoidActivation())
8855

8956
# Calculate in the GPU 0.
90-
Layer(
91-
name = "layer3_1",
92-
type = "fc",
93-
size = 10,
94-
device = 0,
95-
active_type = "sigmoid",
96-
inputs = ["layer2_1", "layer2_2"],
97-
)
57+
fc4 = fc_layer(input=[fc2,fc3], size=10,
58+
bias_attr=True,
59+
layer_attr=ExtraAttr(device=0),
60+
act=SigmoidActivation())
9861

9962
# Calculate in the GPU 1.
100-
Layer(
101-
name = "layer3_2",
102-
type = "fc",
103-
size = 10,
104-
device = 1,
105-
active_type = "sigmoid",
106-
inputs = ["layer2_1", "layer2_2"],
107-
)
108-
63+
fc5 = fc_layer(input=[fc2,fc3], size=10,
64+
bias_attr=True,
65+
layer_attr=ExtraAttr(device=1),
66+
act=SigmoidActivation())
10967

110-
Layer(
111-
name = "output",
112-
type = "fc",
113-
size = 10,
114-
device = output_device,
115-
active_type = "sigmoid",
116-
inputs = ["layer3_1", "layer3_2"],
117-
)
68+
output = fc_layer(input=[fc4,fc5], size=10,
69+
bias_attr=True,
70+
layer_attr=ExtraAttr(device=output_device),
71+
act=SoftmaxActivation())
11872

11973
if get_config_arg('with_cost', bool, True):
12074
# This is for training the neural network.
12175
# We need to have another data layer for label
12276
# and a layer for calculating cost
123-
Layer(
124-
name = "label",
125-
type = "data",
126-
device = output_device,
127-
size = 1,
128-
)
129-
130-
Layer(
131-
name = "cost",
132-
type = "multi-class-cross-entropy",
133-
device = output_device,
134-
inputs = ["output", "label"],
135-
)
136-
137-
Evaluator(
138-
name = "error",
139-
type = "classification_error",
140-
inputs = ["output", "label"])
141-
142-
Inputs("input", "label")
143-
Outputs("cost")
144-
77+
lbl = data_layer(name='label', size=1,
78+
layer_attr=ExtraAttr(device=output_device))
79+
80+
outputs(classification_cost(input=output,
81+
label=lbl,
82+
layer_attr=ExtraAttr(device=output_device)))
14583
else:
14684
# This is for prediction where we don't have label
14785
# and don't need to calculate cost
148-
Inputs("input")
149-
Outputs("output")
86+
outputs(output)

python/paddle/trainer_config_helpers/attrs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,16 @@ class ExtraLayerAttribute(object):
174174
The dropout rate is the zero rate of this mask. The
175175
details of what dropout is please refer to `here
176176
<https://www.cs.toronto.edu/~hinton/absps/
177-
JMLRdropout.pdf>`_
177+
JMLRdropout.pdf>`_.
178178
:type drop_rate: float
179-
179+
:param device: device ID of layer. device=-1, use CPU. device>0, use GPU.
180+
The details allocation in parallel_nn please refer to `here
181+
<http://www.paddlepaddle.org/doc/ui/cmd_argument/
182+
use_case.html#case-2-specify-layers-in-different-devices>`_.
183+
:type device: int
180184
"""
181185

182-
def __init__(self, error_clipping_threshold=None, drop_rate=None):
186+
def __init__(self, error_clipping_threshold=None, drop_rate=None, device=None):
183187
self.attr = dict()
184188
if isinstance(error_clipping_threshold, float):
185189
assert error_clipping_threshold > 0
@@ -189,6 +193,9 @@ def __init__(self, error_clipping_threshold=None, drop_rate=None):
189193
assert drop_rate > 0
190194
self.attr["drop_rate"] = drop_rate
191195

196+
if isinstance(device, int):
197+
self.attr["device"] = device
198+
192199
def check(self, layer_name):
193200
for key in self.attr:
194201
if not hasattr(self, 'can_%s' % key) or \

python/paddle/trainer_config_helpers/layers.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def __str__(self):
201201

202202
ERROR_CLIPPING = 'error_clipping_threshold'
203203
DROPOUT = 'drop_rate'
204+
DEVICE = 'device'
204205

205206

206207
def check_input(input):
@@ -223,10 +224,12 @@ def check_input(input):
223224

224225

225226
def layer_support(*attrs):
227+
attrs_list = list(attrs)
228+
attrs_list.append(DEVICE)
226229
def decorator(method):
227230
@functools.wraps(method)
228231
def wrapper(*args, **kwargs):
229-
for attr in attrs:
232+
for attr in attrs_list:
230233
for each in args:
231234
if isinstance(each, ExtraLayerAttribute):
232235
setattr(each, '_'.join(['can', attr]), True)
@@ -2625,9 +2628,11 @@ def regression_cost(input, label, cost='square_error', name=None):
26252628

26262629

26272630
@wrap_name_default("cost")
2631+
@layer_support()
26282632
def classification_cost(input, label, name=None,
26292633
cost="multi-class-cross-entropy",
2630-
evaluator=classification_error_evaluator):
2634+
evaluator=classification_error_evaluator,
2635+
layer_attr=None):
26312636
"""
26322637
classification cost Layer.
26332638
@@ -2640,13 +2645,16 @@ def classification_cost(input, label, name=None,
26402645
:param cost: cost method.
26412646
:type cost: basestring
26422647
:param evaluator: Evaluator method.
2648+
:param layer_attr: layer's extra attribute.
2649+
:type layer_attr: ExtraLayerAttribute
26432650
:return: LayerOutput object.
26442651
:rtype: LayerOutput
26452652
"""
26462653
assert input.layer_type != LayerType.DATA
26472654
assert isinstance(input.activation, SoftmaxActivation)
26482655
assert label.layer_type == LayerType.DATA
2649-
Layer(name=name, type=cost, inputs=[Input(input.name), Input(label.name)])
2656+
Layer(name=name, type=cost, inputs=[Input(input.name), Input(label.name)],
2657+
**ExtraLayerAttribute.to_kwargs(layer_attr))
26502658

26512659
def __add_evaluator__(e):
26522660
assert callable(e)

0 commit comments

Comments
 (0)