Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NewIR] 在op_translator.cc支持mul算子的映射处理 #56521

Open
Aurelius84 opened this issue Aug 22, 2023 · 0 comments
Open

[NewIR] 在op_translator.cc支持mul算子的映射处理 #56521

Aurelius84 opened this issue Aug 22, 2023 · 0 comments
Assignees
Labels
type/new-feature 确认的新需求

Comments

@Aurelius84
Copy link
Contributor

Aurelius84 commented Aug 22, 2023

一、需求描述

为了兼容支持旧的 Program 与新 IR Dialect 之间的用户模型,框架在 op_translator.cc 添加了 OpTranscriber 组件,来支持OpDesc → New IR Op 的映射转换。

在旧的 Program 体系下,有 mul 算子,已经被标记为「废弃状态」,其功能可以通过 matmul 算子来替换。但目前在 op_compat.yaml 中,是将其映射到 matmul_with_flatten 上的:

- op : matmul_with_flatten (mul)
  backward : matmul_with_flatten_grad (mul_grad)
  extra :
    attrs : [bool use_mkldnn = false, float scale_x = 1.0f, 'float[] scale_y = {1.0f}',
             float scale_out = 1.0f, bool force_fp32_output = false]

这导致在执行框架部分单测时,会在「转换」模块报错,比如在 test/legacy_test目录下执行:
FLAGS_enable_new_ir_in_executor=1 python test_mul_op.py 会报如下错误:

    new_exe = _StandaloneExecutor(place, plan, scope)
  File "/workspace/env3.8/lib/python3.8/site-packages/paddle/fluid/executor.py", line 673, in __init__
    self._new_exe = self._create_new_executor()
  File "/workspace/env3.8/lib/python3.8/site-packages/paddle/fluid/executor.py", line 697, in _create_new_executor
    new_exe = core.StandaloneExecutor(self._place, self._plan, self._scope)
RuntimeError: Error occured at: /workspace/paddle-fork/paddle/fluid/ir_adaptor/translator/op_translator.cc:307 :
Op mul should have corresponding OpInfo pd.matmul_with_flatten

对应的报错代码在 op_translator.cc:307 :

ir::OpInfo OpTranscriber::LoopkUpOpInfo(ir::IrContext* ctx,
                                        const OpDesc& op_desc) {
  std::string target_op_name =
      kTargetDialectPrefix + OpNameCompatibleMapping(op_desc.Type());
  if (IsInplace(op_desc) && *target_op_name.rbegin() != '_') {
    target_op_name += "_";
  }
  VLOG(6) << "[op name normalizing]: " << op_desc.Type() << " to "
          << target_op_name;
  auto op_info = ctx->GetRegisteredOpInfo(target_op_name);
  if (!op_info) {
    IR_THROW("Op %d should have corresponding OpInfo %d",
             op_desc.Type(),
             target_op_name);
  }

  return op_info;
}

二、 一些思路

可以像op_translator.cc里的类似 TrilAndTriuOpTranscriber 其他子类一样,派生一个MulOpTranscriber,来单独处理mul→matmul 的映射,也需要处理MulGradOpTranscriber,重写LoopkUpOpInfo 等方法。

三、 验证修复

FLAGS_enable_new_ir_in_executor=1 python test_mul_op.py 执行成功

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/new-feature 确认的新需求
Projects
None yet
Development

No branches or pull requests

4 participants