Skip to content

Commit 42c8fad

Browse files
authored
【映射文档】更新别名表格的维护与生成方式 (#6691)
* add macro ALIAS-REFERENCE-ITEM(<alias_name>, <api_name>) * update apply_reference script to support alias
1 parent 964fcac commit 42c8fad

File tree

3 files changed

+189
-127
lines changed

3 files changed

+189
-127
lines changed

docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def mapping_type_to_description(mapping_type):
6767
REFERENCE_PATTERN = re.compile(
6868
r"^\| *REFERENCE-MAPPING-ITEM\( *(?P<torch_api>[^,]+) *, *(?P<diff_url>.+) *\) *\|$"
6969
)
70+
ALIAS_PATTERN = re.compile(
71+
r"^\| *ALIAS-REFERENCE-ITEM\( *(?P<alias_name>[^,]+) *, *(?P<torch_api>[^,]+) *\) *\|$"
72+
)
7073
NOT_IMPLEMENTED_PATTERN = re.compile(
7174
r"^\| *NOT-IMPLEMENTED-ITEM\( *(?P<torch_api>[^,]+) *, *(?P<torch_api_url>.+) *\) *\|$"
7275
)
@@ -87,21 +90,24 @@ def docs_url_to_relative_page(url):
8790

8891
def apply_reference_to_row(line, metadata_dict, table_row_idx, line_idx):
8992
reference_match = REFERENCE_PATTERN.match(line)
93+
alias_match = ALIAS_PATTERN.match(line)
9094
not_implemented_match = NOT_IMPLEMENTED_PATTERN.match(line)
9195

96+
row_idx_s = str(table_row_idx)
97+
9298
if reference_match:
9399
torch_api = reference_match["torch_api"].strip("`").replace(r"\_", "_")
94100
diff_url = reference_match["diff_url"]
95101

96102
diff_page_url = docs_url_to_relative_page(diff_url)
97103

98-
row_idx_s = str(table_row_idx)
99-
100104
if torch_api not in metadata_dict:
101105
raise Exception(
102106
f"Cannot find torch_api: {torch_api} in line {line_idx}"
103107
)
104108

109+
meta_dict[torch_api]["diff_url"] = diff_page_url
110+
105111
reference_item = metadata_dict.get(torch_api, None)
106112
torch_api_url = reference_item["torch_api_url"]
107113
torch_api_column = f"[`{torch_api}`]({torch_api_url})"
@@ -137,14 +143,61 @@ def apply_reference_to_row(line, metadata_dict, table_row_idx, line_idx):
137143

138144
output = "| " + " | ".join(content) + " |\n"
139145
return output
146+
elif alias_match:
147+
alias_name = alias_match["alias_name"].strip("`").replace(r"\_", "_")
148+
torch_api = alias_match["torch_api"].strip("`").replace(r"\_", "_")
149+
150+
if torch_api not in metadata_dict:
151+
raise Exception(
152+
f"Cannot find torch_api: {torch_api} in line {line_idx}"
153+
)
154+
155+
diff_page_url = metadata_dict[torch_api].get("diff_url", "")
156+
157+
reference_item = metadata_dict.get(torch_api, None)
158+
torch_api_url = reference_item["torch_api_url"]
159+
alisa_name_column = f"[`{alias_name}`]({torch_api_url})"
160+
161+
mapping_type = reference_item["mapping_type"]
162+
mapping_type_column = mapping_type
163+
164+
_mapping_type_desc, show_diff_url = mapping_type_to_description(
165+
mapping_type
166+
)
167+
168+
desc_column = f"`{torch_api}` 别名"
169+
170+
if show_diff_url:
171+
desc_column += f",[详细对比]({diff_page_url})"
172+
173+
if "paddle_api" not in reference_item:
174+
if mapping_type not in ["组合替代实现", "可删除", "功能缺失"]:
175+
print(
176+
f"Cannot find paddle_api for torch_api: {torch_api} in line {line_idx}"
177+
)
178+
paddle_api_column = ""
179+
else:
180+
paddle_api = reference_item["paddle_api"]
181+
paddle_api_url = reference_item["paddle_api_url"]
182+
paddle_api_column = f"[`{paddle_api}`]({paddle_api_url})"
183+
184+
content = [
185+
row_idx_s,
186+
alisa_name_column,
187+
paddle_api_column,
188+
mapping_type_column,
189+
desc_column,
190+
]
191+
192+
output = "| " + " | ".join(content) + " |\n"
193+
return output
194+
140195
elif not_implemented_match:
141196
torch_api = (
142197
not_implemented_match["torch_api"].strip("`").replace(r"\_", "_")
143198
)
144199
torch_api_url = not_implemented_match["torch_api_url"].strip()
145200

146-
row_idx_s = str(table_row_idx)
147-
148201
torch_api_column = f"[`{torch_api}`]({torch_api_url})"
149202

150203
paddle_api_column = ""
@@ -165,7 +218,7 @@ def apply_reference_to_row(line, metadata_dict, table_row_idx, line_idx):
165218
raise ValueError(
166219
f"found manual-maintaining row at line [{line_idx}]: {line}"
167220
)
168-
# return line
221+
return line
169222

170223

171224
def reference_mapping_item_processer(line, line_idx, state, output, context):

0 commit comments

Comments
 (0)