Skip to content

Commit bd680f1

Browse files
committed
fix compiling warning.
1 parent bcc0dad commit bd680f1

File tree

3 files changed

+23
-34
lines changed

3 files changed

+23
-34
lines changed

paddle/operators/lstm_op.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class LSTMGradKernel : public framework::OpKernel<T> {
155155
auto* batch_cell_pre_act = ctx.Input<LoDTensor>("BatchCellPreAct");
156156

157157
auto* hidden_g = ctx.Input<LoDTensor>(framework::GradVarName("Hidden"));
158-
auto* cell_g = ctx.Input<LoDTensor>(framework::GradVarName("Cell"));
158+
// auto* cell_g = ctx.Input<LoDTensor>(framework::GradVarName("Cell"));
159159

160160
auto* in_g = ctx.Output<LoDTensor>(framework::GradVarName("Input"));
161161
auto* weight_g = ctx.Output<Tensor>(framework::GradVarName("Weight"));
@@ -219,8 +219,8 @@ class LSTMGradKernel : public framework::OpKernel<T> {
219219
LoDTensor batch_cell_g;
220220
batch_cell_g.mutable_data<T>(out_dims, ctx.GetPlace());
221221
batch_cell_g.set_lod(batch_gate->lod());
222-
to_batch(device_ctx, *cell_g, batch_cell_g, false);
223222
// TODO(qingqing) support the case output cell has gradient.
223+
// to_batch(device_ctx, *cell_g, batch_cell_g, false);
224224
zero(device_ctx, &batch_cell_g, static_cast<T>(0.0));
225225

226226
LoDTensor batch_gate_g;

paddle/operators/math/sequence2batch.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class LoDTensor2BatchFunctor {
5858
if (!is_cal_batch_lod) {
5959
auto lods = batch.lod();
6060
PADDLE_ENFORCE_EQ(lods.size(), 2UL);
61-
PADDLE_ENFORCE_EQ(lods[1].size(), lod_tensor.dims()[0]);
61+
PADDLE_ENFORCE_EQ(lods[1].size(),
62+
static_cast<size_t>(lod_tensor.dims()[0]));
6263
CopyMatrixRowsFunctor<Place, T> to_batch;
6364
to_batch(context, lod_tensor, lods[1].data(), batch, true);
6465
return;
@@ -111,10 +112,10 @@ class LoDTensor2BatchFunctor {
111112
size_t* batch_starts = batch_lods[0].data();
112113
size_t* seq2batch_idx = batch_lods[1].data();
113114
batch_starts[0] = 0;
114-
for (size_t n = 0; n < num_batch; n++) {
115+
for (int n = 0; n < num_batch; n++) {
115116
auto batch_id = static_cast<int>(batch_starts[n]);
116117
for (size_t i = 0; i < seq_info.size(); ++i) {
117-
size_t seq_len = seq_info[i].length;
118+
int seq_len = seq_info[i].length;
118119
int start = seq_info[i].start;
119120
if (n < seq_len) {
120121
seq2batch_idx[batch_id] =

python/paddle/v2/framework/tests/test_lstm_op.py

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,23 @@ def _step(x, w_h, w_c, h_pre, c_pre, act_gate, act_cell, act_cand):
5252
g = np.dot(h_pre, w_h) # 1 x 4D
5353
g = g + x
5454
g = np.reshape(g, (1, g.size))
55-
c_tmp, g_i, g_f, g_o = np.split(g, 4, axis=1)
55+
c, g_i, g_f, g_o = np.split(g, 4, axis=1)
5656
if w_c is None:
5757
g_i = act_gate(g_i) # 1 x D
5858
g_f = act_gate(g_f) # 1 x D
5959
else:
6060
w_ic, w_fc, w_oc = np.split(w_c, 3, axis=1)
6161
g_i = act_gate(g_i + w_ic * c_pre) # 1 x D
6262
g_f = act_gate(g_f + w_fc * c_pre) # 1 x D
63-
c = g_f * c_pre + g_i * act_cand(c_tmp) # 1 x D
63+
c = g_f * c_pre + g_i * act_cand(c) # 1 x D
6464

6565
if w_c is None:
6666
g_o = act_gate(g_o) # 1 x D
6767
else:
6868
_, _, w_oc = np.split(w_c, 3, axis=1)
6969
g_o = act_gate(g_o + w_oc * c) # 1 x D
7070
h = g_o * act_cell(c)
71-
bg = np.concatenate((act_cand(c_tmp), g_i, g_f, g_o), axis=1)
72-
return h, c, bg
71+
return h, c
7372

7473
def _reverse(x, lod):
7574
y = np.zeros_like(x)
@@ -82,7 +81,6 @@ def _reverse(x, lod):
8281
batch_size = len(offset) - 1
8382
hidden = []
8483
cell = []
85-
gate = []
8684
input = _reverse(input, offset) if is_reverse else input
8785
if w_b is not None:
8886
input = input + np.tile(w_b, (offset[-1], 1))
@@ -94,30 +92,26 @@ def _reverse(x, lod):
9492
c_pre = c0[i] # 1 x D
9593
for j in range(seq_len):
9694
# compute one step
97-
h_pre, c_pre, g_pre = _step(x[j], w_h, w_c, h_pre, c_pre, act_gate,
98-
act_cell, act_cand)
95+
h_pre, c_pre = _step(x[j], w_h, w_c, h_pre, c_pre, act_gate,
96+
act_cell, act_cand)
9997
hidden.append(h_pre.flatten())
10098
cell.append(c_pre.flatten())
101-
gate.append(g_pre.flatten())
10299

103100
hidden = np.array(hidden).astype('float64')
104101
cell = np.array(cell).astype('float64')
105-
gate = np.array(gate).astype('float64')
106102

107103
hidden = _reverse(hidden, offset) if is_reverse else hidden
108104
cell = _reverse(cell, offset) if is_reverse else cell
109105

110-
assert gate.shape == input.shape
111106
assert hidden.shape == (input.shape[0], input.shape[1] / 4)
112107
assert cell.shape == (input.shape[0], input.shape[1] / 4)
113-
return hidden, cell, gate
108+
return hidden, cell
114109

115110

116111
class TestLstmOp(OpTest):
117112
def set_argument(self):
118-
self.lod = [[0, 2, 6, 9]]
113+
self.lod = [[0, 2, 6]]
119114
self.D = 16
120-
self.sort_idx = [2, 6, 0, 3, 7, 1, 4, 8, 5]
121115

122116
self.act_gate = 'sigmoid'
123117
self.act_cell = 'tanh'
@@ -141,22 +135,18 @@ def setUp(self):
141135

142136
w_b = b[:, 0:4 * self.D]
143137
w_c = b[:, 4 * self.D:]
144-
h, c, g = lstm(x, self.lod, h0, c0, w, w_b, w_c, self.is_reverse,
145-
ACTVATION[self.act_gate], ACTVATION[self.act_cell],
146-
ACTVATION[self.act_cand])
147-
148-
g_sort = np.zeros_like(x)
149-
for i, j in enumerate(self.sort_idx):
150-
g_sort[i, :] = g[j, :]
138+
h, c = lstm(x, self.lod, h0, c0, w, w_b, w_c, self.is_reverse,
139+
ACTVATION[self.act_gate], ACTVATION[self.act_cell],
140+
ACTVATION[self.act_cand])
151141

152142
self.inputs = {'Input': (x, self.lod), 'Weight': w, 'Bias': b}
153-
self.inputs['H0'] = h0
154-
self.inputs['C0'] = c0
143+
if self.has_initial_state:
144+
self.inputs['H0'] = h0
145+
self.inputs['C0'] = c0
155146

156147
self.outputs = {
157148
'Hidden': (h, self.lod),
158149
'Cell': (c, self.lod),
159-
'BatchGate': g_sort,
160150
}
161151
self.attrs = {
162152
'usePeepholes': True,
@@ -179,9 +169,8 @@ def test_check_grad(self):
179169

180170
class TestLstmOpHasNoInitial(TestLstmOp):
181171
def set_argument(self):
182-
self.lod = [[0, 2, 6, 9]]
183-
self.D = 64
184-
self.sort_idx = [2, 6, 0, 3, 7, 1, 4, 8, 5]
172+
self.lod = [[0, 2, 6]]
173+
self.D = 16
185174

186175
self.act_gate = 'sigmoid'
187176
self.act_cell = 'tanh'
@@ -193,9 +182,8 @@ def set_argument(self):
193182

194183
class TestLstmOpRerverse(TestLstmOp):
195184
def set_argument(self):
196-
self.lod = [[0, 2, 6, 9]]
197-
self.D = 64
198-
self.sort_idx = [2, 6, 0, 3, 7, 1, 4, 8, 5]
185+
self.lod = [[0, 2, 6]]
186+
self.D = 16
199187

200188
self.act_gate = 'sigmoid'
201189
self.act_cell = 'tanh'

0 commit comments

Comments
 (0)