提交 8ee17e96 编写于 作者: Y Yang Yang

pass sgd at first iter

上级 93132332
......@@ -185,18 +185,27 @@ class ParallelDoGradOp : public OperatorBase {
// merge grad
for (auto &s : Outputs(framework::GradVarName(kParameters))) {
LOG(INFO) << s;
// std::string s_buf = s + "@BUF";
// auto *t_buf = sub_scopes[0]->Var(s_buf)->GetMutable<LoDTensor>();
auto &t = sub_scopes[0]->FindVar(s)->Get<LoDTensor>();
LOG(INFO) << t;
std::string s_buf = s + "@BUF";
auto *t_buf = sub_scopes[0]->Var(s_buf)->GetMutable<LoDTensor>();
for (size_t place_idx = 1; place_idx < places.size(); ++place_idx) {
auto &tt = sub_scopes[place_idx]->FindVar(s)->Get<LoDTensor>();
LOG(INFO) << place_idx;
LOG(INFO) << sub_scopes[place_idx]->FindVar(s)->Get<LoDTensor>();
// Copy grad[i] to grad_buf[0]
LOG(INFO) << tt;
framework::CopyFrom(tt, places[0], t_buf);
// sum_op
auto sum_op = framework::OpRegistry::CreateOp(
"sum", {{"X", {s, s_buf}}}, {{"Out", {s}}},
framework::AttributeMap{});
sum_op->Run(*sub_scopes[0], place);
}
// Copy grad[0] to grad
// auto *t = scope.FindVar(s)->GetMutable<LoDTensor>();
LOG(INFO) << t;
framework::CopyFrom(t, place, scope.FindVar(s)->GetMutable<LoDTensor>());
}
}
};
......
......@@ -27,7 +27,8 @@ class ParallelOpTest(unittest.TestCase):
pd.write_output(hidden)
data = pd()
loss = layers.mean(x=data)
append_backward(loss)
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
sgd_optimizer.minimize(loss)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册