未验证 提交 f54078e8 编写于 作者: K kangguangli 提交者: GitHub

translate grad_add to elementwise_add (#56180)

上级 052e7b07
......@@ -1379,6 +1379,19 @@ struct ElementwiseTranscriber : public OpTranscriber {
}
};
struct GradAddOpTranscriber : public ElementwiseTranscriber {
ir::OpInfo LoopkUpOpInfo(ir::IrContext* ctx, const OpDesc& op_desc) override {
const std::string& target_op_name = "pd.add";
const auto& op_info = ctx->GetRegisteredOpInfo(target_op_name);
if (!op_info) {
IR_THROW(
"Op assign_value should have corresponding OpInfo pd.assign_value_");
}
return op_info;
}
};
struct ElementwiseGradTranscriber : public OpTranscriber {
void RecordOpResultMapping(ir::IrContext* ctx,
TranslationContext* param_map,
......@@ -1450,6 +1463,7 @@ OpTranslator::OpTranslator() {
special_handlers["feed"] = FeedOpTranscriber();
special_handlers["data"] = DataOpTranscriber();
special_handlers["fetch_v2"] = FetchOpTranscriber();
special_handlers["grad_add"] = GradAddOpTranscriber();
special_handlers["increment"] = IncrementOpTranscriber();
special_handlers["lookup_table_v2"] = EmbeddingOpTranscriber();
special_handlers["lookup_table_v2_grad"] = EmbeddingGradOpTranscriber();
......
......@@ -1230,6 +1230,10 @@
{pre_nms_top_n : pre_nms_topN, post_nms_top_n : post_nms_topN}
- op : grad_add
inputs :
{x : X, y : Y}
outputs :
{out : Out}
extra :
attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32",
bool use_quantizer = false, float Scale_x = 1.0f, float Scale_y = 1.0f, float Scale_out = 1.0f]
......
......@@ -274,5 +274,31 @@ class TestIndexPutOpTranscriber(unittest.TestCase):
_ = ir.translate_to_new_ir(main_program.desc)
class TestGradAddOpTranscriber(unittest.TestCase):
def test_op(self):
place = core.Place()
place.set_place(paddle.CPUPlace())
new_scope = paddle.static.Scope()
main_program = paddle.static.Program()
with paddle.static.scope_guard(new_scope):
with paddle.static.program_guard(main_program):
x_data = np.random.rand(100, 2, 3)
y_data = np.random.rand(100, 1, 1)
x = paddle.to_tensor(x_data, dtype='float32')
x.stop_gradient = False
y = paddle.to_tensor(y_data, dtype='float32')
helper = LayerHelper('grad_add')
out = helper.create_variable_for_type_inference("float")
helper.append_op(
type="grad_add",
inputs={"X": x, "Y": y},
outputs={"Out": out},
attrs={"axis": -1},
)
_ = ir.translate_to_new_ir(main_program.desc)
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册