提交 5d33ef61 编写于 作者: Q qiaolongfei

change op_register and grad_op_builder

上级 b2e3824e
...@@ -13,22 +13,22 @@ express or implied. See the License for the specific language governing ...@@ -13,22 +13,22 @@ express or implied. See the License for the specific language governing
permissions and limitations under the License. */ permissions and limitations under the License. */
#include "paddle/framework/grad_op_builder.h" #include "paddle/framework/grad_op_builder.h"
#include "paddle/framework/framework.pb.h"
#include "paddle/framework/op_registry.h" #include "paddle/framework/op_registry.h"
namespace paddle { namespace paddle {
namespace framework { namespace framework {
enum class OpArgType { IN, OUT }; enum class OpArgType { IN, OUT };
static void TransOpArg(const OperatorBase* src_op, OperatorBase* dst_op, using VarNameMap = OperatorBase::VarNameMap;
const OpArgType& src_type, const OpArgType& dst_type,
bool is_grad) { static VarNameMap TransOpArg(const OperatorBase* src_op,
const OpArgType& src_type,
const OpArgType& dst_type, bool is_grad) {
const auto& src_inout = const auto& src_inout =
src_type == OpArgType::IN ? src_op->inputs_ : src_op->outputs_; src_type == OpArgType::IN ? src_op->Inputs() : src_op->Outputs();
auto& dst_inout = VarNameMap dst_inout;
dst_type == OpArgType::IN ? dst_op->inputs_ : dst_op->outputs_;
const OpProto& proto = OpProtos().at(src_op->type_); const OpProto& proto = OpProtos().at(src_op->Type());
const auto& src_arg_list = const auto& src_arg_list =
src_type == OpArgType::IN ? proto.inputs() : proto.outputs(); src_type == OpArgType::IN ? proto.inputs() : proto.outputs();
for (const auto& arg : src_arg_list) { for (const auto& arg : src_arg_list) {
...@@ -41,17 +41,23 @@ static void TransOpArg(const OperatorBase* src_op, OperatorBase* dst_op, ...@@ -41,17 +41,23 @@ static void TransOpArg(const OperatorBase* src_op, OperatorBase* dst_op,
dst_inout[dst_name].emplace_back(s); dst_inout[dst_name].emplace_back(s);
} }
} }
return dst_inout;
} }
OperatorBase* BuildGradOp(const OperatorBase* op) { OperatorBase* BuildGradOp(const OperatorBase* op) {
std::string grad_op_type = OpRegistry::grad_ops().at(op->type_); std::string grad_op_type = OpRegistry::grad_ops().at(op->Type());
OperatorBase* grad_op = OpRegistry::op_creators().at(grad_op_type)(); auto I = TransOpArg(op, OpArgType::IN, OpArgType::IN, false); // I
grad_op->type_ = grad_op_type; auto O = TransOpArg(op, OpArgType::OUT, OpArgType::IN, false); // O
grad_op->attrs_ = op->attrs_; auto OG = TransOpArg(op, OpArgType::OUT, OpArgType::IN, true); // OG
TransOpArg(op, grad_op, OpArgType::IN, OpArgType::IN, false); // I auto IG = TransOpArg(op, OpArgType::IN, OpArgType::OUT, true); // IG
TransOpArg(op, grad_op, OpArgType::OUT, OpArgType::IN, false); // O // TODO(merge I/O/OG)
TransOpArg(op, grad_op, OpArgType::OUT, OpArgType::IN, true); // OG VarNameMap GradIn;
TransOpArg(op, grad_op, OpArgType::IN, OpArgType::OUT, true); // IG GradIn.insert(I.begin(), I.end());
GradIn.insert(O.begin(), O.end());
GradIn.insert(OG.begin(), OG.end());
OperatorBase* grad_op = OpRegistry::op_creators().at(grad_op_type)(
grad_op_type, GradIn, IG, op->Attrs());
return grad_op; return grad_op;
} }
......
...@@ -128,7 +128,11 @@ class OpRegistry { ...@@ -128,7 +128,11 @@ class OpRegistry {
public: public:
template <typename OpType, typename ProtoMakerType> template <typename OpType, typename ProtoMakerType>
static void RegisterOp(const std::string& op_type) { static void RegisterOp(const std::string& op_type) {
op_creators()[op_type] = [] { return new OpType; }; op_creators()[op_type] = [](
const std::string& type, const VarNameMap& inputs,
const VarNameMap& outputs, const AttributeMap& attrs) {
return new OpType(type, inputs, outputs, attrs);
};
OpAttrChecker& op_checker = op_checkers()[op_type]; OpAttrChecker& op_checker = op_checkers()[op_type];
OpProto& op_proto = OpProtos()[op_type]; OpProto& op_proto = OpProtos()[op_type];
auto maker = ProtoMakerType(&op_proto, &op_checker); auto maker = ProtoMakerType(&op_proto, &op_checker);
...@@ -143,7 +147,11 @@ class OpRegistry { ...@@ -143,7 +147,11 @@ class OpRegistry {
template <typename GradOpType> template <typename GradOpType>
static void RegisterGradOp(const std::string& op_type, static void RegisterGradOp(const std::string& op_type,
const std::string& grad_op_type) { const std::string& grad_op_type) {
op_creators()[grad_op_type] = [] { return new GradOpType; }; op_creators()[grad_op_type] = [](
const std::string& type, const VarNameMap& inputs,
const VarNameMap& outputs, const AttributeMap& attrs) {
return new GradOpType(type, inputs, outputs, attrs);
};
grad_ops()[op_type] = grad_op_type; grad_ops()[op_type] = grad_op_type;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册