From 5ef88c774d7c7319c291ca50af2f250884413890 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Sun, 3 Mar 2024 01:49:43 +0800 Subject: [PATCH 1/5] merge process logic --- .../apply_reference_from_api_difference.py | 256 ++++-------------- .../validate_mapping_in_api_difference.py | 187 ++++++++----- 2 files changed, 168 insertions(+), 275 deletions(-) diff --git a/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py b/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py index 98b8426b965..339821965ac 100644 --- a/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py +++ b/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py @@ -1,89 +1,16 @@ import os import re import sys -import typing +script_path = os.path.abspath(__file__) +script_dir = os.path.dirname(__file__) +sys.path.append(script_dir) +print(script_dir) -class DiffMeta(typing.TypedDict): - torch_api: str - torch_api_url: typing.Optional[str] - paddle_api: typing.Optional[str] - paddle_api_url: typing.Optional[str] - mapping_type: str - source_file: str - - -def unescape_api(api): - return api.replace(r"\_", "_") - - -def get_meta_from_diff_file(filepath): - meta_data: DiffMeta = {"source_file": filepath} - state = 0 - # 0: wait for title - # 1: wait for torch api - # 2: wait for paddle api - # 3: end - title_pattern = re.compile(r"^## +\[(?P[^\]]+)\] *(?P.+)$") - torch_pattern = re.compile( - r"^### +\[ *(?Ptorch.[^\]]+)\](?P\([^\)]*\))?$" - ) - paddle_pattern = re.compile( - r"^### +\[ *(?Ppaddle.[^\]]+)\](?P\([^\)]*\))$" - ) - - with open(filepath, "r") as f: - for line in f.readlines(): - if not line.startswith("##"): - continue - - if state == 0: - title_match = title_pattern.match(line) - if title_match: - mapping_type = title_match["type"].strip() - torch_api = title_match["torch_api"].strip() - - meta_data["torch_api"] = unescape_api(torch_api) - meta_data["mapping_type"] = mapping_type - state = 1 - else: - raise Exception(f"Cannot parse title: {line} in {filepath}") - elif state == 1: - torch_match = torch_pattern.match(line) - - if torch_match: - torch_api = torch_match["torch_api"].strip() - torch_url = torch_match["url"] if torch_match["url"] else "" - real_url = torch_url.lstrip("(").rstrip(")") - if meta_data["torch_api"] != unescape_api(torch_api): - raise Exception( - f"torch api not match: {line} != {meta_data['torch_api']} in {filepath}" - ) - meta_data["torch_api_url"] = real_url - state = 2 - else: - raise Exception( - f"Cannot parse torch api: {line} in {filepath}" - ) - elif state == 2: - paddle_match = paddle_pattern.match(line) - - if paddle_match: - paddle_api = paddle_match["paddle_api"].strip() - paddle_url = paddle_match["url"].strip() - real_url = paddle_url.lstrip("(").rstrip(")") - meta_data["paddle_api"] = paddle_api - meta_data["paddle_api_url"] = real_url - state = 3 - else: - pass - - if state < 2: - raise Exception( - f"Unexpected End State at {state} in parsing file: {filepath}, current meta: {meta_data}" - ) - - return meta_data +from validate_mapping_in_api_difference import ( + get_meta_from_diff_file, + process_mapping_index as reference_mapping_item, +) def mapping_type_to_description(mapping_type): @@ -222,133 +149,40 @@ def apply_reference_to_row(line, metadata_dict, table_row_idx, line_idx): # return line -def reference_mapping_item(index_path, metadata_dict): - if not os.path.exists(index_path): - raise Exception(f"Cannot find pytorch_api_mapping_cn.md: {index_path}") - - with open(mapping_index_file, "r", encoding="utf-8") as f: - lines = f.readlines() - - ret_code = 0 - state = 0 - # -1: error - # 0: wait for table header - - # 1: wait for ignore table seperator - # 2: wait for expect table content - - # 5: wait for ignore table content - # 6: wait for expect table content - - column_names = [] - column_count = -1 - table_seperator_pattern = re.compile(r"^ *\|(?P *-+ *\|)+ *$") - - expect_column_names = [ - "序号", - "PyTorch-2.1", - "PaddlePaddle-dev", - "映射关系分类", - "详细对比", - ] - - table_row_idx = -1 - output = [] - - for i, line in enumerate(lines): - if state < 0: - break - - content = line.strip() - if not content.startswith("|"): - output.append(line) - state = 0 - continue - - columns = [c.strip() for c in content.split("|")] - if len(columns) <= 2: - raise Exception( - f"Table column count must > 0, but found {len(columns) - 2} at line {i+1}: {line}" - ) - columns = columns[1:-1] - - if state == 0: - column_names.clear() - column_names.extend([c.strip() for c in columns]) - column_count = len(column_names) - if column_names == expect_column_names: - state = 2 - table_row_idx = 1 - # print(f'process mapping table at line {i+1}.') - else: - state = 1 - print(f"ignore table with {column_names} at line {i+1}.") - output.append(line) - elif state == 1: - if ( - not table_seperator_pattern.match(line) - or len(columns) != column_count - ): - raise Exception( - f"Table seperator not match at line {i+1}: {line}" - ) - state = 5 - output.append(line) - elif state == 2: - if ( - not table_seperator_pattern.match(line) - or len(columns) != column_count - ): - raise Exception( - f"Table seperator not match at line {i+1}: {line}" - ) - state = 6 - output.append(line) - elif state == 5: - # if len(columns) != column_count: - # raise Exception( - # f"Table content not match at line {i+1}: {line}" - # ) - output.append(line) - # state = 5 - elif state == 6: - # if len(columns) != column_count: - # raise Exception( - # f"Table content not match at line {i+1}: {line}" - # ) - try: - referenced_row = apply_reference_to_row( - line, metadata_dict, table_row_idx, i + 1 - ) - table_row_idx += 1 - - output.append(referenced_row) - except Exception as e: - print(e) - print(f"Error at line {i+1}: {line}") - output.append(line) - ret_code = 1 - - # state = 6 - else: - raise Exception( - f"Unexpected State at {state} in processing file: {index_path}" - ) - ret_code = 2 - - if state == 5 or state == 6: - state = 0 - - if state != 0: - raise Exception( - f"Unexpected End State at {state} in parsing file: {index_path}" +def reference_mapping_item_processer(line, line_idx, state, output, context): + if not line.startswith("|"): + output.append(line) + return True + + metadata_dict = context.get("metadata_dict", {}) + + if state == 0: + # check column names in common process + output.append(line) + return True + elif state == 1: + # check seperator of table to ignore in common process + output.append(line) + return True + elif state == 2: + # check seperator of table to process in common process + output.append(line) + return True + elif state == 5: + # check content of table to ignore in common process + output.append(line) + return True + elif state == 6: + # check content of table to process in common process + referenced_row = apply_reference_to_row( + line, metadata_dict, context["table_row_idx"], line_idx + 1 ) - if ret_code != 0: - sys.exit(ret_code) + output.append(referenced_row) + return True - with open(mapping_index_file, "w", encoding="utf-8") as f: - f.writelines(output) + print(state) + return False if __name__ == "__main__": @@ -377,6 +211,16 @@ def reference_mapping_item(index_path, metadata_dict): meta_dict = {m["torch_api"].replace(r"\_", "_"): m for m in metas} - reference_mapping_item(mapping_index_file, meta_dict) + reference_context = { + "metadata_dict": meta_dict, + "ret_code": 0, + "output": [], + } + ret_code = reference_mapping_item( + mapping_index_file, reference_mapping_item_processer, reference_context + ) + + with open(mapping_index_file, "w", encoding="utf-8") as f: + f.writelines(reference_context["output"]) # 映射关系文件的保存流程移动至 `validate_mapping_in_api_difference.py` diff --git a/docs/guides/model_convert/convert_from_pytorch/validate_mapping_in_api_difference.py b/docs/guides/model_convert/convert_from_pytorch/validate_mapping_in_api_difference.py index 64744a22e09..0b967227351 100644 --- a/docs/guides/model_convert/convert_from_pytorch/validate_mapping_in_api_difference.py +++ b/docs/guides/model_convert/convert_from_pytorch/validate_mapping_in_api_difference.py @@ -340,7 +340,24 @@ def validate_mapping_table_row(columns, row_idx, line_idx): } -def process_mapping_index(filename): +def collect_mapping_item_processor(item, line_idx, state, output, context): + if state == 6: + table_row_idx = context["table_row_idx"] + columns = context["columns"] + item = validate_mapping_table_row(columns, table_row_idx, line_idx + 1) + output.append(item) + return True + + return False + + +def process_mapping_index(index_path, item_processer, context={}): + if not os.path.exists(index_path): + raise Exception(f"Cannot find pytorch_api_mapping_cn.md: {index_path}") + + with open(index_path, "r", encoding="utf-8") as f: + lines = f.readlines() + state = 0 # -1: error # 0: wait for table header @@ -355,90 +372,116 @@ def process_mapping_index(filename): column_count = -1 table_seperator_pattern = re.compile(r"^ *\|(?P *-+ *\|)+ *$") - expect_column_names = ["序号", "PyTorch API", "PaddlePaddle API", "备注"] + expect_column_names = [ + "序号", + "PyTorch-2.1", + "PaddlePaddle-dev", + "映射关系分类", + "详细对比", + ] + + context["table_row_idx"] = context.get("table_row_idx", -1) + output = context.get("output", []) - table_row_idx = -1 + for i, line in enumerate(lines): + if state < 0: + break - output = [] + content = line.strip() + if not content.startswith("|"): + output.append(line) + state = 0 + continue - with open(filename, "r") as f: - for i, line in enumerate(f.readlines()): - if state < 0: - break + columns = [c.strip() for c in content.split("|")] + if len(columns) <= 2: + raise Exception( + f"Table column count must > 0, but found {len(columns) - 2} at line {i+1}: {line}" + ) + columns = columns[1:-1] + + if state == 0: + column_names.clear() + column_names.extend([c.strip() for c in columns]) + column_count = len(column_names) - content = line.strip() - if len(content) == 0 or content[0] != "|": - state = 0 - continue + if not item_processer(line, i, state, output, context): + break - columns = [c.strip() for c in content.split("|")] - if len(columns) <= 2: + if column_names == expect_column_names: + state = 2 + context["table_row_idx"] = 1 + # print(f'process mapping table at line {i+1}.') + else: + state = 1 + print(f"ignore table with {column_names} at line {i+1}.") + + elif state == 1: + if ( + not table_seperator_pattern.match(line) + or len(columns) != column_count + ): raise Exception( - f"Table column count must > 0, but found {len(columns) - 2} at line {i+1}: {line}" + f"Table seperator not match at line {i+1}: {line}" ) - columns = columns[1:-1] - - if state == 0: - column_names.clear() - column_names.extend([c.strip() for c in columns]) - column_count = len(column_names) - if column_names == expect_column_names: - state = 2 - table_row_idx = 1 - # print(f'process mapping table at line {i+1}.') - else: - state = 1 - print(f"ignore table with {column_names} at line {i+1}.") - elif state == 1: - if ( - not table_seperator_pattern.match(line) - or len(columns) != column_count - ): - raise Exception( - f"Table seperator not match at line {i+1}: {line}" - ) - state = 5 - elif state == 2: - if ( - not table_seperator_pattern.match(line) - or len(columns) != column_count - ): - raise Exception( - f"Table seperator not match at line {i+1}: {line}" - ) - state = 6 - elif state == 5: - if len(columns) != column_count: - raise Exception( - f"Table content not match at line {i+1}: {line}" - ) - # state = 5 - elif state == 6: - if len(columns) != column_count: - raise Exception( - f"Table content not match at line {i+1}: {line}" - ) - - item = validate_mapping_table_row(columns, table_row_idx, i + 1) - table_row_idx += 1 - - output.append(item) - - # state = 6 - else: + if not item_processer(line, i, state, output, context): + break + state = 5 + elif state == 2: + if ( + not table_seperator_pattern.match(line) + or len(columns) != column_count + ): raise Exception( - f"Unexpected State at {state} in parsing file: {filename}" + f"Table seperator not match at line {i+1}: {line}" ) + if not item_processer(line, i, state, output, context): + break + state = 6 + elif state == 5: + # if len(columns) != column_count: + # raise Exception( + # f"Table content not match at line {i+1}: {line}" + # ) + if not item_processer(line, i, state, output, context): + break + # state = 5 + elif state == 6: + # if len(columns) != column_count: + # raise Exception( + # f"Table content not match at line {i+1}: {line}" + # ) + try: + if not item_processer(line, i, state, output, context): + break + context["table_row_idx"] += 1 + except Exception as e: + print(e) + print(f"Error at line {i+1}: {line}") + ret_code = 1 + + # state = 6 + else: + ret_code = 2 + raise Exception( + f"Unexpected State at {state} in processing file: {index_path}" + ) if state == 5 or state == 6: state = 0 if state != 0: raise Exception( - f"Unexpected End State at {state} in parsing file: {filename}" + f"Unexpected End State at {state} in parsing file: {index_path}" ) - return output + ret_code = context.get("ret_code", 0xCC) + if ret_code != 0: + return ret_code + + context["output"] = output + + return 0 if __name__ == "__main__": @@ -450,7 +493,13 @@ def process_mapping_index(filename): if not os.path.exists(mapping_index_file): raise Exception(f"Cannot find mapping index file: {mapping_index_file}") - # index_data = process_mapping_index(mapping_index_file) + index_data = process_mapping_index( + mapping_index_file, + collect_mapping_item_processor, + { + "ret_code": 0, + }, + ) # index_data_dict = {i['torch_api'].replace('\_', '_'): i for i in index_data} api_difference_basedir = os.path.join(cfp_basedir, "api_difference") From b60fafa63dbb132e7db32b87656ec0ff8116a4cf Mon Sep 17 00:00:00 2001 From: RedContritio Date: Tue, 5 Mar 2024 11:12:43 +0800 Subject: [PATCH 2/5] add some docs --- ...torch.nn.functional._Reduction.get_enum.md | 35 ++++++++++++++ .../torch.nn.functional.alpha_dropout.md | 24 ++++++++++ ...orch.nn.functional.binary_cross_entropy.md | 44 +++++++++++++++++ .../functional/torch.nn.functional.dropout.md | 24 ++++++++++ .../torch.nn.functional.embedding.md | 27 +++++++++++ .../torch.nn.functional.hardsigmoid.md | 22 +++++++++ .../torch.nn.functional.multi_margin_loss.md | 46 ++++++++++++++++++ ....functional.multilabel_soft_margin_loss.md | 44 +++++++++++++++++ .../torch.nn.functional.pairwise_distance.md | 25 ++++++++++ .../torch.nn.functional.poisson_nll_loss.md | 46 ++++++++++++++++++ .../functional/torch.nn.functional.rrelu_.md | 24 ++++++++++ .../functional/torch.nn.functional.silu.md | 22 +++++++++ .../functional/torch.nn.functional.tanh.md | 21 ++++++++ .../api_difference/ops/torch.mode.md | 35 ++++++++++++++ .../ops/torch.nn.Module.modules.md | 15 ++++++ .../torch.nn.modules.batchnorm._BatchNorm.md | 48 +++++++++++++++++++ 16 files changed, 502 insertions(+) create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.pairwise_distance.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.tanh.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md create mode 100644 docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.modules.batchnorm._BatchNorm.md diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md new file mode 100644 index 00000000000..77d9b892b0f --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md @@ -0,0 +1,35 @@ +## [组合替代实现]torch.nn.functional._Reduction.get_enum + +### [torch.nn.functional._Reduction.get_enum](https://github.com/pytorch/pytorch/blob/3045b16488f14c9d941d33d63417e6ea52fb2544/torch/nn/_reduction.py#L7) + +```python +torch.nn.functional._Reduction.get_enum(reduction) +``` + +Paddle 无此 API,需要组合实现。 + +### 转写示例 + +```python +# PyTorch 写法 +torch.nn.functional._Reduction.get_enum(reduction) + +# Paddle 写法 +## paddle_aux +def get_enum(reduction: str) -> int: + if reduction == 'none': + ret = 0 + elif reduction == 'mean': + ret = 1 + elif reduction == 'elementwise_mean': + warnings.warn("reduction='elementwise_mean' is deprecated, please use reduction='mean' instead.") + ret = 1 + elif reduction == 'sum': + ret = 2 + else: + ret = -1 + raise ValueError("{} is not a valid value for reduction".format(reduction)) + return ret + +paddle_aux.get_enum(reduction) +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md new file mode 100644 index 00000000000..f2a77595b7f --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md @@ -0,0 +1,24 @@ +## [ 仅参数名不一致 ]torch.nn.functional.alpha_dropout + +### [torch.nn.functional.alpha\_dropout](https://pytorch.org/docs/stable/generated/torch.nn.functional.alpha_dropout.html) + +```python +torch.nn.functional.alpha_dropout(input, p=0.5, training=False, inplace=False) +``` + +### [paddle.nn.functional.alpha\_dropout](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/alpha_dropout_cn.html#alpha-dropout) + +```python +paddle.nn.functional.alpha_dropout(x, p=0.5, training=True, name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| -------- | ------------ | -- | +| input | x | 输入的多维 Tensor,仅参数名不一致。 | +| p | p | 将输入节点置 0 的概率。 | +| training | training | 标记是否为训练阶段,PyTorch 默认值为 False,paddle 默认值为 True。 | +| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md new file mode 100644 index 00000000000..1f8ed5c8fd3 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md @@ -0,0 +1,44 @@ +## [ 参数不一致 ]torch.nn.functional.binary_cross_entropy + +### [torch.nn.functional.binary\_cross\_entropy](https://pytorch.org/docs/stable/generated/torch.nn.functional.binary_cross_entropy.html) + +```python +torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=None, reduce=None, reduction='mean') +``` + +### [paddle.nn.functional.binary\_cross\_entropy](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/binary_cross_entropy_cn.html#binary-cross-entropy) + +```python +paddle.nn.functional.binary_cross_entropy(input, label, weight=None, reduction='mean', name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------------ | ------------ | -- | +| input | input | 输入 Tensor。 | +| target | label | 标签 Tensor。 | +| weight | weight | 手动指定每个 batch 二值交叉熵的权重。 | +| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| reduction | reduction | 指定应用于输出结果的计算方式。 | + +### 转写示例 + +#### size_average:做 reduce 的方式 +```python +# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 +if size_average is None: + size_average = True +if reduce is None: + reduce = True + +if size_average and reduce: + reduction = 'mean' +elif reduce: + reduction = 'sum' +else: + reduction = 'none' +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md new file mode 100644 index 00000000000..bdf4afe4f07 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md @@ -0,0 +1,24 @@ +## [ 仅参数名不一致 ]torch.nn.functional.dropout + +### [torch.nn.functional.dropout](https://pytorch.org/docs/stable/generated/torch.nn.functional.dropout.html) + +```python +torch.nn.functional.dropout(input, p=0.5, training=True, inplace=False) +``` + +### [paddle.nn.functional.dropout](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/dropout_cn.html#dropout) + +```python +paddle.nn.functional.dropout(x, p=0.5, axis=None, training=True, mode='upscale_in_train', name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| -------- | ------------ | -- | +| input | x | 输入 Tensor,仅参数名不一致。 | +| p | p | 将输入节点置 0 的概率。 | +| training | training | 标记是否为训练阶段。 | +| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md new file mode 100644 index 00000000000..05da077a0c5 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md @@ -0,0 +1,27 @@ +## [ torch 参数更多 ]torch.nn.functional.embedding + +### [torch.nn.functional.embedding](https://pytorch.org/docs/stable/generated/torch.nn.functional.embedding.html) + +```python +torch.nn.functional.embedding(input, weight, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False) +``` + +### [paddle.nn.functional.embedding](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/embedding_cn.html#embedding) + +```python +paddle.nn.functional.embedding(x, weight, padding_idx=None, sparse=False, name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------------------ | ------------------ | -- | +| input | x | 输入 Tensor,仅参数名不同。 | +| weight | weight | 嵌入矩阵权重。 | +| padding_idx | padding_idx | 视为填充的下标,参数完全一致。 | +| max_norm | - | 重新归一化的最大范数, | +| norm_type | - | Paddle 无此参数,需要转写。 | +| scale_grad_by_freq | - | 按词频进行梯度缩放的比例,需要转写。 | +| sparse | sparse | 是否使用稀疏更新。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md new file mode 100644 index 00000000000..f1bb4537cf6 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md @@ -0,0 +1,22 @@ +## [ 仅参数名不一致 ]torch.nn.functional.hardsigmoid + +### [torch.nn.functional.hardsigmoid](https://pytorch.org/docs/stable/generated/torch.nn.functional.hardsigmoid.html) + +```python +torch.nn.functional.hardsigmoid(input, inplace=False) +``` + +### [paddle.nn.functional.hardsigmoid](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/hardsigmoid_cn.html#hardsigmoid) + +```python +paddle.nn.functional.hardsigmoid(x, slope=0.1666667, offset=0.5, name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------- | ------------ | -- | +| input | x | 输入 Tensor,仅参数名不一致。 | +| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md new file mode 100644 index 00000000000..c4b1659ecc5 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md @@ -0,0 +1,46 @@ +## [ torch 参数更多 ]torch.nn.functional.multi_margin_loss + +### [torch.nn.functional.multi\_margin\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.multi_margin_loss.html) + +```python +torch.nn.functional.multi_margin_loss(input, target, p=1, margin=1, weight=None, size_average=None, reduce=None, reduction='mean') +``` + +### [paddle.nn.functional.multi\_margin\_loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/multi_margin_loss_cn.html#multi-margin-loss) + +```python +paddle.nn.functional.multi_margin_loss(input, label, p=1, margin=1.0, weight=None, reduction='mean', name=None) +``` + +PyTorch 相比 Paddle 支持更多其他参数,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------------ | ------------ | -- | +| input | input | 输入 Tensor,仅参数名不一致。 | +| target | label | 标签 Tensor,仅参数名不一致。 | +| p | p | 手动指定范数。| +| margin | margin | 手动指定间距。 | +| weight | weight | 手动指定每个 batch 二值交叉熵的权重。 | +| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| reduction | reduction | 指定应用于输出结果的计算方式。 | + +### 转写示例 + +#### size_average:做 reduce 的方式 +```python +# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 +if size_average is None: + size_average = True +if reduce is None: + reduce = True + +if size_average and reduce: + reduction = 'mean' +elif reduce: + reduction = 'sum' +else: + reduction = 'none' +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md new file mode 100644 index 00000000000..3d4d3799d2f --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md @@ -0,0 +1,44 @@ +## [ 仅参数名不一致 ]torch.nn.functional.multilabel_soft_margin_loss + +### [torch.nn.functional.multilabel\_soft\_margin\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.multilabel_soft_margin_loss.html) + +```python +torch.nn.functional.multilabel_soft_margin_loss(input, target, weight=None, size_average=None, reduce=None, reduction='mean') +``` + +### [paddle.nn.functional.multi\_label\_soft\_margin\_loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/multi_label_soft_margin_loss_cn.html#multi-label-soft-margin-loss) + +```python +paddle.nn.functional.multi_label_soft_margin_loss(input, label, weight=None, reduction='mean', name=None) +``` + +PyTorch 相比 Paddle 支持更多其他参数,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------------ | ------------ | -- | +| input | input | 输入 Tensor,仅参数名不一致。 | +| target | label | 标签 Tensor,仅参数名不一致。 | +| weight | weight | 手动指定每个 batch 二值交叉熵的权重。 | +| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| reduction | reduction | 指定应用于输出结果的计算方式。 | + +### 转写示例 + +#### size_average:做 reduce 的方式 +```python +# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 +if size_average is None: + size_average = True +if reduce is None: + reduce = True + +if size_average and reduce: + reduction = 'mean' +elif reduce: + reduction = 'sum' +else: + reduction = 'none' +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.pairwise_distance.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.pairwise_distance.md new file mode 100644 index 00000000000..22fe3877572 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.pairwise_distance.md @@ -0,0 +1,25 @@ +## [ 仅参数名不一致 ]torch.nn.functional.pairwise_distance + +### [torch.nn.functional.pairwise\_distance](https://pytorch.org/docs/stable/generated/torch.nn.functional.pairwise_distance.html) + +```python +torch.nn.functional.pairwise_distance(x1, x2, p=2.0, eps=1e-6, keepdim=False) +``` + +### [paddle.nn.functional.pairwise\_distance](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/pairwise_distance_cn.html#pairwise-distance) + +```python +paddle.nn.functional.pairwise_distance(x, y, p=2., epsilon=1e-6, keepdim=False, name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------- | ------------ | -- | +| x1 | x | 输入 Tensor,仅参数名不一致。 | +| x2 | y | 输入 Tensor,仅参数名不一致。 | +| p | p | 指定 p 阶的范数。 | +| eps | epsilon | 添加到分母的一个很小值,避免发生除零错误。仅参数名不一致。 | +| keepdim | keepdim | 是否保留输出 Tensor 减少的维度。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md new file mode 100644 index 00000000000..6e3cfaf4bb2 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md @@ -0,0 +1,46 @@ +## [ 参数不一致 ]torch.nn.functional.poisson_nll_loss + +### [torch.nn.functional.poisson\_nll\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.poisson_nll_loss.html) + +```python +torch.nn.functional.poisson_nll_loss(input, target, log_input=True, full=False, size_average=None, eps=1e-08, reduce=None, reduction='mean') +``` + +### [paddle.nn.functional.poisson\_nll\_loss](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/functional/poisson_nll_loss_cn.html#poisson-nll-loss) + +```python +paddle.nn.functional.poisson_nll_loss(input, label, log_input=True, full=False, epsilon=1e-8, reduction='mean', name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------------ | ------------ | -- | +| input | input | 输入 Tensor。 | +| target | label | 标签 Tensor,仅参数名不一致。 | +| log_input | log_input | 输入是否为对数函数映射后结果。 | +| full | full | 是否在损失计算中包括 Stirling 近似项。 | +| size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| eps | epsilon | 在 log_input 为 True 时使用的常数小量,仅参数名不一致。 | +| reduce | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | +| reduction | reduction | 指定应用于输出结果的计算方式。 | + +### 转写示例 + +#### size_average:做 reduce 的方式 +```python +# PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 +if size_average is None: + size_average = True +if reduce is None: + reduce = True + +if size_average and reduce: + reduction = 'mean' +elif reduce: + reduction = 'sum' +else: + reduction = 'none' +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md new file mode 100644 index 00000000000..18de9c3adbd --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md @@ -0,0 +1,24 @@ +## [ 仅参数名不一致 ]torch.nn.functional.rrelu_ + +### [torch.nn.functional.rrelu\_](https://pytorch.org/docs/stable/generated/torch.nn.functional.rrelu_.html) + +```python +torch.nn.functional.rrelu_(input, lower=1./8, upper=1./3, training=False) +``` + +### [paddle.nn.functional.rrelu](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/functional/rrelu_cn.html) + +```python +paddle.nn.functional.rrelu(x, lower=1./8., upper=1./3., training=True, name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| -------- | ------------ | -- | +| input | x | 输入的 Tensor,仅参数名不一致。 | +| lower | lower | 负值斜率的随机值范围下限。 | +| upper | upper | 负值斜率的随机值范围上限。 | +| training | training | 标记是否为训练阶段。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md new file mode 100644 index 00000000000..0166314afed --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md @@ -0,0 +1,22 @@ +## [ 仅参数名不一致 ]torch.nn.functional.silu + +### [torch.nn.functional.silu](https://pytorch.org/docs/stable/generated/torch.nn.functional.silu.html) + +```python +torch.nn.functional.silu(input, inplace=False) +``` + +### [paddle.nn.functional.silu](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/functional/silu_cn.html#silu) + +```python +paddle.nn.functional.silu(x, name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------- | ------------ | -- | +| input | x | 输入 Tensor,仅参数名不一致。 | +| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.tanh.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.tanh.md new file mode 100644 index 00000000000..ddb30ea1cca --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.tanh.md @@ -0,0 +1,21 @@ +## [ 仅参数名不一致 ]torch.nn.functional.tanh + +### [torch.nn.functional.tanh](https://pytorch.org/docs/stable/generated/torch.nn.functional.tanh.html) + +```python +torch.nn.functional.tanh(input) +``` + +### [paddle.nn.functional.tanh](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/tanh_cn.html#tanh) + +```python +paddle.nn.functional.tanh(x) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------- | ------------ | -- | +| input | x | 输入 Tensor,仅参数名不一致。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md new file mode 100644 index 00000000000..8d79c5de9d7 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md @@ -0,0 +1,35 @@ +## [ torch 参数更多 ]torch.mode + +### [torch.mode](https://pytorch.org/docs/stable/generated/torch.mode.html) + +```python +torch.mode(input, dim=-1, keepdim=False, *, out=None) +``` + +### [paddle.mode](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/mode_cn.html#mode) + +```python +paddle.mode(x, axis=-1, keepdim=False, name=None) +``` + +其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------- | ------------ | -- | +| input | x | 输入的多维 Tensor。 | +| dim | axis | 指定对输入 Tensor 进行运算的轴,仅参数名不一致。 | +| keepdim | keepdim | 是否保留指定的轴。 | +| out | - | 表示输出的 Tensor , Paddle 无此参数,需要转写。 | + +### 转写示例 +#### out:指定输出 +```python +# PyTorch 写法 +torch.mode(x, dim, False, out=(a, b)) + +# Paddle 写法 +out1, out2 = paddle.mode(x, dim, False) +paddle.assign(out1, (a, b)[0]), paddle.assign(out2, (a, b)[1]) +``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md new file mode 100644 index 00000000000..a33eb4f481d --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md @@ -0,0 +1,15 @@ +## [ 无参数 ]torch.nn.Module.modules + +### [torch.nn.Module.modules](https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.modules) + +```python +torch.nn.Module.modules() +``` + +### [paddle.nn.Layer.sublayers](https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/Layer_cn.html#sublayers-include-self-false) + +```python +paddle.nn.Layer.sublayers(include_self=False) +``` + +两者功能一致,无参数。 diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.modules.batchnorm._BatchNorm.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.modules.batchnorm._BatchNorm.md new file mode 100644 index 00000000000..d7c1b8b1a47 --- /dev/null +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.modules.batchnorm._BatchNorm.md @@ -0,0 +1,48 @@ +## [ torch 参数更多 ]torch.nn.modules.batchnorm._BatchNorm + +### [torch.nn.modules.batchnorm.\_BatchNorm](https://pytorch.org/docs/stable/_modules/torch/nn/modules/batchnorm.html) + +```python +torch.nn.modules.batchnorm._BatchNorm(num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True, device=None, dtype=None) +``` + +### [paddle.nn.layer.norm.\_BatchNormBase](https://github.com/PaddlePaddle/Paddle/blob/b51d50bc9ee9eaa5cefa18507195b239e4513194/python/paddle/nn/layer/norm.py#L701) + +```python +paddle.nn.layer.norm._BatchNormBase(num_features, momentum=0.9, epsilon=1e-05, weight_attr=None, bias_attr=None, data_format='NCHW', use_global_stats=None, name=None) +``` + +PyTorch 相比 Paddle 支持更多其他参数,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------------------- | ---------------------------- | --------- | +| num_features | num_features | 特征数量。 | +| eps | epsilon | 一个极小正数,仅参数名不一致。 | +| momentum | momentum | 动量因子,参数默认值不一致。 | +| affine | - | 是否进行仿射变换,Paddle 无此参数,需要转写。 +| track_running_stats | - | 是否跟踪运行时的 mean 和 var, Paddle 无此参数。暂无转写方式。 | +| device | - | 表示 Tensor 存放设备位置,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | +| dtype | - | 参数类型,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | +| - | weight_attr | 指定权重参数属性的对象,PyTorch 无此参数,Paddle 保持默认即可。 | +| - | bias_attr | 指定偏置参数属性的对象,PyTorch 无此参数,Paddle 保持默认即可。 | +| - | data_format | 指定输入数据格式,PyTorch 无此参数,Paddle 保持默认即可。 | + +### 转写示例 + +#### affine:是否进行仿射变换 + +```python +# 当 PyTorch 的 affine 为`False`,表示 weight 和 bias 不进行更新,torch 写法 +torch.nn.modules.batchnorm._BatchNorm(num_features, affine=False) + +# paddle 写法 +paddle.nn.layer.norm._BatchNormBase(num_features, weight_attr=False, bias_attr=False) + +# 当 PyTorch 的 affine 为`True`,torch 写法 +torch.nn.modules.batchnorm._BatchNorm(num_features, affine=True) + +# paddle 写法 +paddle.nn.layer.norm._BatchNormBase(num_features) +``` From 6ef6fb1ab3057ec6dc64bf345d9b6fcff67c3627 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Tue, 5 Mar 2024 11:13:10 +0800 Subject: [PATCH 3/5] add strict check for mapping type --- .../apply_reference_from_api_difference.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py b/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py index 339821965ac..d9c13766ed6 100644 --- a/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py +++ b/docs/guides/model_convert/convert_from_pytorch/apply_reference_from_api_difference.py @@ -57,6 +57,9 @@ def mapping_type_to_description(mapping_type): if mapping_type in mapping_type_delete: return "无对应 API,可以直接删除,对网络一般无影响", False + raise ValueError( + f"Unexpected pyTorch-PaddlePaddle api mapping type {mapping_type}, please check " + ) return "【未知类型】", False From 29cdf858c80cb22f361954573f50fd615b447b13 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Wed, 6 Mar 2024 15:07:54 +0800 Subject: [PATCH 4/5] update docs --- .../torch.nn.functional._Reduction.get_enum.md | 3 +-- .../functional/torch.nn.functional.alpha_dropout.md | 4 ++-- .../torch.nn.functional.binary_cross_entropy.md | 6 +++--- .../functional/torch.nn.functional.dropout.md | 6 ++++-- .../functional/torch.nn.functional.embedding.md | 8 ++++---- .../functional/torch.nn.functional.hardsigmoid.md | 8 +++++--- .../torch.nn.functional.multi_margin_loss.md | 2 +- .../torch.nn.functional.multilabel_soft_margin_loss.md | 4 ++-- .../functional/torch.nn.functional.poisson_nll_loss.md | 6 +++--- .../functional/torch.nn.functional.rrelu_.md | 6 +++--- .../functional/torch.nn.functional.silu.md | 2 +- .../api_difference/ops/torch.nn.Module.modules.md | 10 ++++++++-- 12 files changed, 37 insertions(+), 28 deletions(-) diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md index 77d9b892b0f..c208f1d7ff9 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional._Reduction.get_enum.md @@ -15,7 +15,6 @@ Paddle 无此 API,需要组合实现。 torch.nn.functional._Reduction.get_enum(reduction) # Paddle 写法 -## paddle_aux def get_enum(reduction: str) -> int: if reduction == 'none': ret = 0 @@ -31,5 +30,5 @@ def get_enum(reduction: str) -> int: raise ValueError("{} is not a valid value for reduction".format(reduction)) return ret -paddle_aux.get_enum(reduction) +get_enum(reduction) ``` diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md index f2a77595b7f..eff8a4be8d2 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.alpha_dropout.md @@ -1,4 +1,4 @@ -## [ 仅参数名不一致 ]torch.nn.functional.alpha_dropout +## [ torch 参数更多 ]torch.nn.functional.alpha_dropout ### [torch.nn.functional.alpha\_dropout](https://pytorch.org/docs/stable/generated/torch.nn.functional.alpha_dropout.html) @@ -12,7 +12,7 @@ torch.nn.functional.alpha_dropout(input, p=0.5, training=False, inplace=False) paddle.nn.functional.alpha_dropout(x, p=0.5, training=True, name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: +其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 参数映射 diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md index 1f8ed5c8fd3..9416b07e298 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.binary_cross_entropy.md @@ -1,4 +1,4 @@ -## [ 参数不一致 ]torch.nn.functional.binary_cross_entropy +## [ torch 参数更多 ]torch.nn.functional.binary_cross_entropy ### [torch.nn.functional.binary\_cross\_entropy](https://pytorch.org/docs/stable/generated/torch.nn.functional.binary_cross_entropy.html) @@ -12,7 +12,7 @@ torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_averag paddle.nn.functional.binary_cross_entropy(input, label, weight=None, reduction='mean', name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数不一致,具体如下: +其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 参数映射 @@ -27,7 +27,7 @@ paddle.nn.functional.binary_cross_entropy(input, label, weight=None, reduction=' ### 转写示例 -#### size_average:做 reduce 的方式 +#### size_average、reduce ```python # PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 if size_average is None: diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md index bdf4afe4f07..b527971fc4b 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.dropout.md @@ -1,4 +1,4 @@ -## [ 仅参数名不一致 ]torch.nn.functional.dropout +## [ torch 参数更多 ]torch.nn.functional.dropout ### [torch.nn.functional.dropout](https://pytorch.org/docs/stable/generated/torch.nn.functional.dropout.html) @@ -12,7 +12,7 @@ torch.nn.functional.dropout(input, p=0.5, training=True, inplace=False) paddle.nn.functional.dropout(x, p=0.5, axis=None, training=True, mode='upscale_in_train', name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: +其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 参数映射 @@ -20,5 +20,7 @@ paddle.nn.functional.dropout(x, p=0.5, axis=None, training=True, mode='upscale_i | -------- | ------------ | -- | | input | x | 输入 Tensor,仅参数名不一致。 | | p | p | 将输入节点置 0 的概率。 | +| - | axis | 指定对输入 Tensor 进行 dropout 操作的轴,PyTorch 无此参数,Paddle 保持默认即可。 | | training | training | 标记是否为训练阶段。 | +| - | mode | 丢弃单元的方式,PyTorch 无此参数,Paddle 保持默认即可。 | | inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md index 05da077a0c5..0b816528924 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md @@ -1,4 +1,4 @@ -## [ torch 参数更多 ]torch.nn.functional.embedding +## [ 参数不一致 ]torch.nn.functional.embedding ### [torch.nn.functional.embedding](https://pytorch.org/docs/stable/generated/torch.nn.functional.embedding.html) @@ -21,7 +21,7 @@ paddle.nn.functional.embedding(x, weight, padding_idx=None, sparse=False, name=N | input | x | 输入 Tensor,仅参数名不同。 | | weight | weight | 嵌入矩阵权重。 | | padding_idx | padding_idx | 视为填充的下标,参数完全一致。 | -| max_norm | - | 重新归一化的最大范数, | -| norm_type | - | Paddle 无此参数,需要转写。 | -| scale_grad_by_freq | - | 按词频进行梯度缩放的比例,需要转写。 | +| max_norm | - | 重新归一化的最大范数,参数不一致,暂无转写方式。 | +| norm_type | - | Paddle 无此参数,参数不一致,暂无转写方式。 | +| scale_grad_by_freq | - | 按词频进行梯度缩放的比例,参数不一致,暂无转写方式。 | | sparse | sparse | 是否使用稀疏更新。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md index f1bb4537cf6..88360ab6825 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.hardsigmoid.md @@ -1,4 +1,4 @@ -## [ 仅参数名不一致 ]torch.nn.functional.hardsigmoid +## [ 仅 paddle 参数更多 ]torch.nn.functional.hardsigmoid ### [torch.nn.functional.hardsigmoid](https://pytorch.org/docs/stable/generated/torch.nn.functional.hardsigmoid.html) @@ -12,11 +12,13 @@ torch.nn.functional.hardsigmoid(input, inplace=False) paddle.nn.functional.hardsigmoid(x, slope=0.1666667, offset=0.5, name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: +其中 PyTorch 和 Paddle 功能一致,仅 paddle 参数更多,具体如下: ### 参数映射 | PyTorch | PaddlePaddle | 备注 | | ------- | ------------ | -- | | input | x | 输入 Tensor,仅参数名不一致。 | -| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | +| - | slope | hardsigmoid 的斜率,PyTorch 无此参数,Paddle 保持默认即可。| +| - | offset | hardsigmoid 的截距,PyTorch 无此参数,Paddle 保持默认即可。| +| inplace | - | 表示在不更改变量的内存地址的情况下,直接修改变量的值,Paddle 无此参数,一般对网络训练结果影响不大,可直接删除。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md index c4b1659ecc5..448f318db7b 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md @@ -29,7 +29,7 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 -#### size_average:做 reduce 的方式 +#### size_average、reduce ```python # PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 if size_average is None: diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md index 3d4d3799d2f..6e0758a8e69 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md @@ -1,4 +1,4 @@ -## [ 仅参数名不一致 ]torch.nn.functional.multilabel_soft_margin_loss +## [ torch 参数更多 ]torch.nn.functional.multilabel_soft_margin_loss ### [torch.nn.functional.multilabel\_soft\_margin\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.multilabel_soft_margin_loss.html) @@ -27,7 +27,7 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 转写示例 -#### size_average:做 reduce 的方式 +#### size_average、reduce ```python # PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 if size_average is None: diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md index 6e3cfaf4bb2..9a39d396355 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.poisson_nll_loss.md @@ -1,4 +1,4 @@ -## [ 参数不一致 ]torch.nn.functional.poisson_nll_loss +## [ torch 参数更多 ]torch.nn.functional.poisson_nll_loss ### [torch.nn.functional.poisson\_nll\_loss](https://pytorch.org/docs/stable/generated/torch.nn.functional.poisson_nll_loss.html) @@ -12,7 +12,7 @@ torch.nn.functional.poisson_nll_loss(input, target, log_input=True, full=False, paddle.nn.functional.poisson_nll_loss(input, label, log_input=True, full=False, epsilon=1e-8, reduction='mean', name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: +其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 参数映射 @@ -29,7 +29,7 @@ paddle.nn.functional.poisson_nll_loss(input, label, log_input=True, full=False, ### 转写示例 -#### size_average:做 reduce 的方式 +#### size_average、reduce ```python # PyTorch 的 size_average、reduce 参数转为 Paddle 的 reduction 参数 if size_average is None: diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md index 18de9c3adbd..eaa2664fba4 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md @@ -1,4 +1,4 @@ -## [ 仅参数名不一致 ]torch.nn.functional.rrelu_ +## [ 仅参数默认值不一致 ]torch.nn.functional.rrelu_ ### [torch.nn.functional.rrelu\_](https://pytorch.org/docs/stable/generated/torch.nn.functional.rrelu_.html) @@ -12,7 +12,7 @@ torch.nn.functional.rrelu_(input, lower=1./8, upper=1./3, training=False) paddle.nn.functional.rrelu(x, lower=1./8., upper=1./3., training=True, name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: +其中 PyTorch 和 Paddle 功能一致,参数名与参数默认值不一致,具体如下: ### 参数映射 @@ -21,4 +21,4 @@ paddle.nn.functional.rrelu(x, lower=1./8., upper=1./3., training=True, name=None | input | x | 输入的 Tensor,仅参数名不一致。 | | lower | lower | 负值斜率的随机值范围下限。 | | upper | upper | 负值斜率的随机值范围上限。 | -| training | training | 标记是否为训练阶段。 | +| training | training | 标记是否为训练阶段。参数默认值不一致, PyTorch 默认值为 `False`,Paddle 默认值为 `True`,Paddle 需保持与 PyTorch 一致。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md index 0166314afed..a1a78855b8b 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.silu.md @@ -12,7 +12,7 @@ torch.nn.functional.silu(input, inplace=False) paddle.nn.functional.silu(x, name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: +其中 PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 参数映射 diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md index a33eb4f481d..b030a127f3b 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.nn.Module.modules.md @@ -1,4 +1,4 @@ -## [ 无参数 ]torch.nn.Module.modules +## [ 仅 paddle 参数更多 ]torch.nn.Module.modules ### [torch.nn.Module.modules](https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.modules) @@ -12,4 +12,10 @@ torch.nn.Module.modules() paddle.nn.Layer.sublayers(include_self=False) ``` -两者功能一致,无参数。 +其中 Paddle 相比 PyTorch 支持更多其他参数,具体如下: + +### 参数映射 + +| PyTorch | PaddlePaddle | 备注 | +| ------- | ------------ | -- | +| - | include_self | 是否包含本层。PyTorch 无此参数,Paddle 保持默认即可。 | From a3419ce0733f2509ec8049fa02123204a65539e2 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Fri, 8 Mar 2024 15:40:16 +0800 Subject: [PATCH 5/5] update incorrect desc --- .../functional/torch.nn.functional.embedding.md | 4 ++-- .../functional/torch.nn.functional.multi_margin_loss.md | 2 +- .../torch.nn.functional.multilabel_soft_margin_loss.md | 2 +- .../api_difference/functional/torch.nn.functional.rrelu_.md | 2 +- .../convert_from_pytorch/api_difference/ops/torch.mode.md | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md index 0b816528924..4e3a22d1de6 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.embedding.md @@ -1,4 +1,4 @@ -## [ 参数不一致 ]torch.nn.functional.embedding +## [torch 参数]torch.nn.functional.embedding ### [torch.nn.functional.embedding](https://pytorch.org/docs/stable/generated/torch.nn.functional.embedding.html) @@ -12,7 +12,7 @@ torch.nn.functional.embedding(input, weight, padding_idx=None, max_norm=None, no paddle.nn.functional.embedding(x, weight, padding_idx=None, sparse=False, name=None) ``` -其中 PyTorch 和 Paddle 功能一致,仅参数名不一致,具体如下: +PyTorch 相比 Paddle 支持更多其他参数,具体如下: ### 参数映射 diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md index 448f318db7b..d2a370a9917 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multi_margin_loss.md @@ -18,7 +18,7 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | PyTorch | PaddlePaddle | 备注 | | ------------ | ------------ | -- | -| input | input | 输入 Tensor,仅参数名不一致。 | +| input | input | 输入 Tensor。 | | target | label | 标签 Tensor,仅参数名不一致。 | | p | p | 手动指定范数。| | margin | margin | 手动指定间距。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md index 6e0758a8e69..57889db47ad 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.multilabel_soft_margin_loss.md @@ -18,7 +18,7 @@ PyTorch 相比 Paddle 支持更多其他参数,具体如下: | PyTorch | PaddlePaddle | 备注 | | ------------ | ------------ | -- | -| input | input | 输入 Tensor,仅参数名不一致。 | +| input | input | 输入 Tensor。 | | target | label | 标签 Tensor,仅参数名不一致。 | | weight | weight | 手动指定每个 batch 二值交叉熵的权重。 | | size_average | - | PyTorch 已弃用, Paddle 无此参数,需要转写。 | diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md index eaa2664fba4..182d7554cec 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/functional/torch.nn.functional.rrelu_.md @@ -12,7 +12,7 @@ torch.nn.functional.rrelu_(input, lower=1./8, upper=1./3, training=False) paddle.nn.functional.rrelu(x, lower=1./8., upper=1./3., training=True, name=None) ``` -其中 PyTorch 和 Paddle 功能一致,参数名与参数默认值不一致,具体如下: +其中 PyTorch 和 Paddle 功能基本一致,前者会在不变更变量内存地址的情况下,直接改变变量的值,一般对网络训练结果影响不大。参数名与参数默认值不一致,具体如下: ### 参数映射 diff --git a/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md index 8d79c5de9d7..47888fd80a8 100644 --- a/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md +++ b/docs/guides/model_convert/convert_from_pytorch/api_difference/ops/torch.mode.md @@ -18,7 +18,7 @@ paddle.mode(x, axis=-1, keepdim=False, name=None) | PyTorch | PaddlePaddle | 备注 | | ------- | ------------ | -- | -| input | x | 输入的多维 Tensor。 | +| input | x | 输入的多维 Tensor,仅参数名不一致。 | | dim | axis | 指定对输入 Tensor 进行运算的轴,仅参数名不一致。 | | keepdim | keepdim | 是否保留指定的轴。 | | out | - | 表示输出的 Tensor , Paddle 无此参数,需要转写。 | @@ -31,5 +31,5 @@ torch.mode(x, dim, False, out=(a, b)) # Paddle 写法 out1, out2 = paddle.mode(x, dim, False) -paddle.assign(out1, (a, b)[0]), paddle.assign(out2, (a, b)[1]) +paddle.assign(out1, a), paddle.assign(out2, b) ```