Skip to content

Commit

Permalink
perfect code (#58542)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanRisheng authored Nov 2, 2023
1 parent 752705f commit 5cce0a5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 48 deletions.
28 changes: 5 additions & 23 deletions paddle/fluid/pir/dialect/op_generator/api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
PD_MANUAL_OP_LIST,
OpCompatParser,
OpInfoParser,
check_need_update_ops,
to_pascal_case,
update_ops,
)

H_FILE_TEMPLATE = """
Expand Down Expand Up @@ -152,29 +154,9 @@ class CodeGen:
def __init__(self) -> None:
pass

def _check_need_update_ops(self, op_yaml_files):
need_update_ops = False
for yaml_file in op_yaml_files:
if yaml_file.find("update_ops.parsed.yaml") != -1:
need_update_ops = True
update_yaml_file = yaml_file
break
return need_update_ops, update_yaml_file

def _update_ops(self, op_yaml_items, update_yaml_file):
with open(update_yaml_file, "r") as f:
update_ops = yaml.safe_load(f)
for i in range(len(op_yaml_items)):
for update_op in update_ops:
if op_yaml_items[i]['name'] == update_op['name']:
op_yaml_items[i] = update_op
break

def _parse_yaml(self, op_yaml_files, op_compat_yaml_file):
op_compat_parser = OpCompatParser(op_compat_yaml_file)
need_update_ops, update_yaml_file = self._check_need_update_ops(
op_yaml_files
)
need_update_ops, update_yaml_file = check_need_update_ops(op_yaml_files)

op_yaml_items = []
for yaml_file in op_yaml_files:
Expand All @@ -185,7 +167,7 @@ def _parse_yaml(self, op_yaml_files, op_compat_yaml_file):
op_yaml_items = op_yaml_items + ops
# replace old ir ops with pir ops
if need_update_ops:
self._update_ops(op_yaml_items, update_yaml_file)
update_ops(op_yaml_items, update_yaml_file)
op_info_items = []
for op in op_yaml_items:
op_compat_item = op_compat_parser.get_compat(op['name'])
Expand All @@ -204,7 +186,7 @@ def _parse_yaml(self, op_yaml_files, op_compat_yaml_file):
and 'scalar' in op_compat_item
):
op_compat_item = op_compat_item.pop('scalar')
if op['support_tensor'] != []:
if 'support_tensor' in op.keys() and op['support_tensor']:
(
scalar_item,
int_array_item,
Expand Down
33 changes: 9 additions & 24 deletions paddle/fluid/pir/dialect/op_generator/op_creator_drr_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
import argparse

import yaml
from op_gen import OpCompatParser, OpInfoParser, to_pascal_case
from op_gen import (
OpCompatParser,
OpInfoParser,
check_need_update_ops,
to_pascal_case,
update_ops,
)

CPP_FILE_TEMPLATE = """
#include "paddle/fluid/pir/drr/ir_operation_factory.h"
Expand Down Expand Up @@ -75,30 +81,9 @@ def __init__(self, op_yaml_files, op_compat_yaml_file, dialect_name):
self.op_info_items = self.parse_yaml(op_yaml_files, op_compat_yaml_file)
self.dialect_name = dialect_name

def _check_need_update_ops(self, op_yaml_files):
need_update_ops = False
update_yaml_file = None
for yaml_file in op_yaml_files:
if yaml_file.find("update_ops.parsed.yaml") != -1:
need_update_ops = True
update_yaml_file = yaml_file
break
return need_update_ops, update_yaml_file

def _update_ops(self, op_yaml_items, update_yaml_file):
with open(update_yaml_file, "r") as f:
update_ops = yaml.safe_load(f)
for i in range(len(op_yaml_items)):
for update_op in update_ops:
if op_yaml_items[i]['name'] == update_op['name']:
op_yaml_items[i] = update_op
break

def parse_yaml(self, op_yaml_files, op_compat_yaml_file):
op_compat_parser = OpCompatParser(op_compat_yaml_file)
need_update_ops, update_yaml_file = self._check_need_update_ops(
op_yaml_files
)
need_update_ops, update_yaml_file = check_need_update_ops(op_yaml_files)

op_yaml_items = []
for yaml_file in op_yaml_files:
Expand All @@ -109,7 +94,7 @@ def parse_yaml(self, op_yaml_files, op_compat_yaml_file):
op_yaml_items = op_yaml_items + ops
# replace old ir ops with pir ops
if need_update_ops:
self._update_ops(op_yaml_items, update_yaml_file)
update_ops(op_yaml_items, update_yaml_file)

op_info_items = []
for op in op_yaml_items:
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/pir/dialect/op_generator/op_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ def OpGenerator(
):
op_compat_item = op_compat_item.pop('scalar')

if op['support_tensor'] != []:
if 'support_tensor' in op.keys() and op['support_tensor']:
scalar_item, int_array_item = op_compat_parser.parse_support_tensor(
op
)
Expand Down

0 comments on commit 5cce0a5

Please sign in to comment.