未验证 提交 d73eb38c 编写于 作者: A Aganlengzi 提交者: GitHub

[NPU] support model PPO (#42484)

上级 1588e7e7
...@@ -118,20 +118,45 @@ class ElementwiseDivGradNPUKernel : public framework::OpKernel<T> { ...@@ -118,20 +118,45 @@ class ElementwiseDivGradNPUKernel : public framework::OpKernel<T> {
if (dy) { if (dy) {
dy->mutable_data<T>(place); dy->mutable_data<T>(place);
Tensor neg_out(y->type()); Tensor neg_out(out->type());
neg_out.mutable_data<T>(y->dims(), place); neg_out.mutable_data<T>(out->dims(), place);
const auto& runner_neg_out = NpuOpRunner("Neg", {*out}, {neg_out}, {}); const auto& runner_neg_out = NpuOpRunner("Neg", {*out}, {neg_out}, {});
runner_neg_out.Run(stream); runner_neg_out.Run(stream);
Tensor y_grad_w(y->type()); Tensor tmp_mul(out->type());
y_grad_w.mutable_data<T>(y->dims(), place); tmp_mul.mutable_data<T>(out->dims(), place);
const auto& runner_y_grad_w = const auto& runner_mul =
NpuOpRunner("Div", {neg_out, *y}, {y_grad_w}, {}); NpuOpRunner("Mul", {neg_out, *dout}, {tmp_mul}, {});
runner_y_grad_w.Run(stream); runner_mul.Run(stream);
const auto& runner_y_grad = if (dy->dims() != dout->dims()) {
NpuOpRunner("Mul", {y_grad_w, *dout}, {*dy}, {}); Tensor reduced_tmp_mul(y->type());
runner_y_grad.Run(stream); reduced_tmp_mul.mutable_data<T>(y->dims(), place);
std::vector<int64_t> axes;
int64_t diff = dout->dims().size() - dy->dims().size();
for (int64_t i = 0; i < dout->dims().size(); ++i) {
if (i < diff) {
axes.push_back(i);
continue;
}
if (dout->dims()[i] > dy->dims()[i - diff]) {
axes.push_back(i);
}
}
const auto& runner_reduce =
NpuOpRunner("ReduceSumD", {tmp_mul}, {reduced_tmp_mul},
{{"axes", axes}, {"keep_dims", false}});
runner_reduce.Run(stream);
const auto& runner_y_grad =
NpuOpRunner("Div", {reduced_tmp_mul, *y}, {*dy}, {});
runner_y_grad.Run(stream);
} else {
const auto& runner_y_grad =
NpuOpRunner("Div", {tmp_mul, *y}, {*dy}, {});
runner_y_grad.Run(stream);
}
} }
} }
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册