Skip to content

Commit 690cd1f

Browse files
committed
refine gather and broadcast
1 parent 494c262 commit 690cd1f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

paddle/fluid/framework/details/broadcast_op_handle.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ void BroadcastOpHandle::RunImpl() {
9999
PADDLE_THROW("Var should be LoDTensor or SelectedRows.");
100100
}
101101

102-
Tensor *out_tensor = GetTensorFromVar(out_var);
103-
paddle::framework::TensorCopy(*in_tensor, out_p, *(dev_ctxes_[in_place]),
104-
out_tensor);
102+
auto dev_ctx = dev_ctxes_[out_p];
103+
RunAndRecordEvent(out_p, [in_tensor, out_var, dev_ctx, out_p] {
104+
Tensor *out_tensor = GetTensorFromVar(out_var);
105+
paddle::framework::TensorCopy(*in_tensor, out_p, *(dev_ctx), out_tensor);
106+
});
105107
}
106108
}
107109

paddle/fluid/framework/details/gather_op_handle.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void GatherOpHandle::RunImpl() {
8484
"The type of input is not consistent.");
8585
PADDLE_ENFORCE_EQ(pre_in.height(), in_sr.height(),
8686
"The height of inputs is not consistent.");
87-
PADDLE_ENFORCE_EQ(pre_in.GetCompleteDims(), in_sr.GetCompleteDims(), ,
87+
PADDLE_ENFORCE_EQ(pre_in.GetCompleteDims(), in_sr.GetCompleteDims(),
8888
"The dims of inputs is not consistent.");
8989

9090
auto in_sr_rows = in_sr.rows();
@@ -110,14 +110,17 @@ void GatherOpHandle::RunImpl() {
110110
Tensor *out_tensor = out->mutable_value();
111111

112112
// copy
113-
int s = 0, e = 0;
114-
for (size_t j = 0; j < in_tensors.size(); ++j) {
115-
e += in_tensors[j].dims()[0];
116-
auto sub_out = out_tensor->Slice(s, e);
117-
paddle::framework::TensorCopy(in_tensors[j], out_place,
118-
*(dev_ctxes_[in_places[j]]), &sub_out);
119-
s = e;
120-
}
113+
auto dev_ctx = dev_ctxes_[out_place];
114+
RunAndRecordEvent(out_place, [in_tensors, out_var, dev_ctx, out_place] {
115+
int s = 0, e = 0;
116+
for (size_t j = 0; j < in_tensors.size(); ++j) {
117+
e += in_tensors[j].dims()[0];
118+
auto sub_out = out_tensor->Slice(s, e);
119+
paddle::framework::TensorCopy(in_tensors[j], out_place, *(dev_ctx),
120+
&sub_out);
121+
s = e;
122+
}
123+
});
121124
}
122125

123126
std::string GatherOpHandle::Name() const { return "gather"; }

0 commit comments

Comments
 (0)