Skip to content

Commit b6b7ab6

Browse files
committed
Fix calculations in gru_unit_op to be consistent with gru_op
1 parent f191c82 commit b6b7ab6

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

paddle/operators/gru_unit_op.h

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,27 @@ class GRUUnitGradKernel : public framework::OpKernel<T> {
146146
auto* weight_grad =
147147
context.Output<Tensor>(framework::GradVarName("Weight"));
148148
auto* bias_grad = context.Output<Tensor>(framework::GradVarName("Bias"));
149-
input_grad->mutable_data<T>(context.GetPlace());
150-
hidden_prev_grad->mutable_data<T>(context.GetPlace());
151-
weight_grad->mutable_data<T>(context.GetPlace());
152149
Tensor gate_grad;
153-
gate_grad.mutable_data<T>(input->dims(), context.GetPlace());
154150
Tensor reset_hidden_prev_grad;
155-
reset_hidden_prev_grad.mutable_data<T>(reset_hidden_prev->dims(),
156-
context.GetPlace());
157-
158-
int batch_size = input->dims()[0];
159-
int frame_size = hidden_prev->dims()[1];
160151

161152
const T* hidden_prev_data = hidden_prev->data<T>();
162-
T* hidden_prev_grad_data = hidden_prev_grad->data<T>();
163153
const T* weight_data = weight->data<T>();
164-
T* weight_grad_data = weight_grad->data<T>();
165-
T* gate_grad_data = gate_grad.data<T>();
154+
T* gate_grad_data =
155+
gate_grad.mutable_data<T>(input->dims(), context.GetPlace());
166156
const T* reset_hidden_prev_data = reset_hidden_prev->data<T>();
167-
T* reset_hidden_prev_grad_data = reset_hidden_prev_grad.data<T>();
157+
T* reset_hidden_prev_grad_data = reset_hidden_prev_grad.mutable_data<T>(
158+
reset_hidden_prev->dims(), context.GetPlace());
168159

169160
auto h_p = EigenMatrix<T>::From(*hidden_prev);
170161
auto g = EigenMatrix<T>::From(*gate);
171162
auto d_h = EigenMatrix<T>::From(*hidden_grad);
172-
auto d_x = EigenMatrix<T>::From(*input_grad);
173-
auto d_h_p = EigenMatrix<T>::From(*hidden_prev_grad);
174163
auto d_g = EigenMatrix<T>::From(gate_grad);
175164
auto d_r_h_p = EigenMatrix<T>::From(reset_hidden_prev_grad);
176165
auto place = context.GetEigenDevice<Place>();
177166

167+
int batch_size = input->dims()[0];
168+
int frame_size = hidden_prev->dims()[1];
169+
178170
Eigen::array<int, 2> extents({{batch_size, frame_size}});
179171
Eigen::array<int, 2> u_offsets({{0, 0}});
180172
auto u = g.slice(u_offsets, extents); // update gate
@@ -195,28 +187,42 @@ class GRUUnitGradKernel : public framework::OpKernel<T> {
195187
gate_grad_data + frame_size * 2, frame_size * 3,
196188
weight_data + frame_size * frame_size * 2, frame_size,
197189
0, reset_hidden_prev_grad_data, frame_size);
198-
// backward for state_weight
199-
math::gemm<Place, T>(
200-
context.device_context(), true, false, frame_size, frame_size,
201-
batch_size, 1, reset_hidden_prev_data, frame_size,
202-
gate_grad_data + frame_size * 2, frame_size * 3, 0,
203-
weight_grad_data + frame_size * frame_size * 2, frame_size);
204190
// backward for unactivated reset gate
205191
ActGradCompute(context.Attr<int>("gate_activation"), place, r, r,
206192
d_g.slice(r_offsets, extents), d_r_h_p * h_p);
207-
// backward for update_gate_weight and reset_gate_weight
208-
math::gemm<Place, T>(context.device_context(), true, false, frame_size,
209-
frame_size * 2, batch_size, 1, hidden_prev_data,
210-
frame_size, gate_grad_data, frame_size * 3, 0,
211-
weight_grad_data, frame_size * 2);
193+
// backward for weight
194+
if (weight_grad) {
195+
T* weight_grad_data = weight_grad->mutable_data<T>(context.GetPlace());
196+
// backward for state_weight
197+
math::gemm<Place, T>(
198+
context.device_context(), true, false, frame_size, frame_size,
199+
batch_size, 1, reset_hidden_prev_data, frame_size,
200+
gate_grad_data + frame_size * 2, frame_size * 3, 0,
201+
weight_grad_data + frame_size * frame_size * 2, frame_size);
202+
203+
// backward for update_gate_weight and reset_gate_weight
204+
math::gemm<Place, T>(context.device_context(), true, false, frame_size,
205+
frame_size * 2, batch_size, 1, hidden_prev_data,
206+
frame_size, gate_grad_data, frame_size * 3, 0,
207+
weight_grad_data, frame_size * 2);
208+
}
212209
// backward for hidden_prev
213-
d_h_p.device(place) = d_r_h_p * r + d_h * (u.constant(T(1)) - u);
214-
math::gemm<Place, T>(context.device_context(), false, true, batch_size,
215-
frame_size, frame_size * 2, 1, gate_grad_data,
216-
frame_size * 3, weight_data, frame_size * 2, 1,
217-
hidden_prev_grad_data, frame_size);
210+
if (hidden_prev_grad) {
211+
T* hidden_prev_grad_data =
212+
hidden_prev_grad->mutable_data<T>(context.GetPlace());
213+
auto d_h_p = EigenMatrix<T>::From(*hidden_prev_grad);
214+
d_h_p.device(place) = d_r_h_p * r + d_h * (u.constant(T(1)) - u);
215+
math::gemm<Place, T>(context.device_context(), false, true, batch_size,
216+
frame_size, frame_size * 2, 1, gate_grad_data,
217+
frame_size * 3, weight_data, frame_size * 2, 1,
218+
hidden_prev_grad_data, frame_size);
219+
}
218220
// backward for input
219-
d_x.device(place) = d_g;
221+
if (input_grad) {
222+
input_grad->mutable_data<T>(context.GetPlace());
223+
auto d_x = EigenMatrix<T>::From(*input_grad);
224+
d_x.device(place) = d_g;
225+
}
220226
// backward for bias
221227
if (bias_grad) {
222228
bias_grad->mutable_data<T>(context.GetPlace());

python/paddle/v2/fluid/tests/test_gru_unit_op.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def relu(x):
2828

2929

3030
class TestGRUUnitOp(OpTest):
31-
batch_size = 3
32-
frame_size = 5
31+
batch_size = 5
32+
frame_size = 10
3333
activate = {
3434
GRUActivationType.identity: identity,
3535
GRUActivationType.sigmoid: sigmoid,
@@ -92,9 +92,7 @@ def test_check_output(self):
9292
self.check_output()
9393

9494
def test_check_grad(self):
95-
self.check_grad(
96-
['Input', 'HiddenPrev', 'Weight'], ['Hidden'],
97-
max_relative_error=0.007)
95+
self.check_grad(['Input', 'HiddenPrev', 'Weight'], ['Hidden'])
9896

9997

10098
class TestGRUUnitOpWithBias(TestGRUUnitOp):
@@ -110,9 +108,12 @@ def set_inputs(self):
110108
}
111109

112110
def test_check_grad(self):
111+
self.check_grad(['Input', 'HiddenPrev', 'Weight', 'Bias'], ['Hidden'])
112+
113+
def test_check_grad_ingore_input(self):
113114
self.check_grad(
114-
['Input', 'HiddenPrev', 'Weight', 'Bias'], ['Hidden'],
115-
max_relative_error=0.007)
115+
['HiddenPrev', 'Weight', 'Bias'], ['Hidden'],
116+
no_grad_set=set('Input'))
116117

117118

118119
if __name__ == '__main__':

0 commit comments

Comments
 (0)