-
Notifications
You must be signed in to change notification settings - Fork 88
add some loss api #109
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
add some loss api #109
Changes from 2 commits
a6ff8c9
7321408
a2d50df
f9099b9
77485fd
5bf245b
f95a8f0
ae26cd1
e560dd6
7d0bc25
6035745
0bd2a86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4891,6 +4891,15 @@ | |
| "target": "label" | ||
| } | ||
| }, | ||
| "torch.nn.MSELoss": { | ||
| "Matcher": "MseLossMatcher", | ||
| "paddle_api": "paddle.nn.MSELoss", | ||
| "args_list": [ | ||
| "size_average", | ||
| "reduce", | ||
| "reduction" | ||
| ] | ||
| }, | ||
| "torch.nn.functional.margin_ranking_loss": { | ||
| "Matcher": "GenericMatcher", | ||
| "paddle_api": "paddle.nn.functional.margin_ranking_loss", | ||
|
|
@@ -8536,6 +8545,21 @@ | |
| "pos_weight" | ||
| ] | ||
| }, | ||
| "torch.nn.functional.binary_cross_entropy": { | ||
| "Matcher": "FunctionalBinaryCrossEntropyMatcher", | ||
| "paddle_api": "paddle.nn.functional.binary_cross_entropy", | ||
| "args_list": [ | ||
| "input", | ||
| "target", | ||
| "weight", | ||
| "size_average", | ||
| "reduce", | ||
| "reduction" | ||
| ], | ||
| "kwargs_change": { | ||
| "target": "label" | ||
| } | ||
| }, | ||
| "torch.nn.functional.max_pool2d": { | ||
| "Matcher": "FunctionalMaxPool2DMatcher", | ||
| "paddle_api": "paddle.nn.functional.max_pool2d", | ||
|
|
@@ -8612,6 +8636,16 @@ | |
| "pos_weight" | ||
| ] | ||
| }, | ||
| "torch.nn.BCELoss": { | ||
| "Matcher": "BCELossMatcher", | ||
| "paddle_api": "paddle.nn.BCELoss", | ||
| "args_list": [ | ||
| "weight", | ||
| "size_average", | ||
| "reduce", | ||
| "reduction" | ||
| ] | ||
| }, | ||
| "torch.utils.data.BatchSampler": { | ||
| "Matcher": "TorchUtilDataBatchSampler", | ||
| "args_list": [ | ||
|
|
@@ -8642,6 +8676,49 @@ | |
| "input": "x" | ||
| } | ||
| }, | ||
|
|
||
| "torch.nn.L1Loss": { | ||
| "Matcher": "L1LossMatcher", | ||
| "paddle_api": "paddle.nn.L1Loss", | ||
| "args_list": [ | ||
| "size_average", | ||
| "reduce", | ||
| "reduction" | ||
| ] | ||
| }, | ||
| "torch.nn.Unfold": { | ||
| "Matcher": "UnfoldMatcher", | ||
| "paddle_api": "paddle.nn.Unfold", | ||
| "args_list": [ | ||
| "kernel_size", | ||
| "dilation", | ||
| "padding", | ||
| "stride" | ||
| ], | ||
| "kwargs_change": { | ||
| "kernel_size": "kernel_sizes", | ||
| "dilation": "dilations", | ||
| "padding": "paddings", | ||
| "stride": "strides" | ||
| } | ||
| }, | ||
| "torch.nn.functional.unfold": { | ||
|
||
| "Matcher": "FunctionalUnfoldMatcher", | ||
| "paddle_api": "paddle.nn.functional.unfold", | ||
| "args_list": [ | ||
| "input", | ||
| "kernel_size", | ||
| "dilation", | ||
| "padding", | ||
| "stride" | ||
| ], | ||
| "kwargs_change": { | ||
| "input": "x", | ||
| "kernel_size": "kernel_sizes", | ||
| "dilation": "dilations", | ||
| "padding": "paddings", | ||
| "stride": "strides" | ||
|
|
||
| "torch.nn.modules.batchnorm._BatchNorm": { | ||
| "Matcher": "Modules_BatchNormBaseMatcher", | ||
| "paddle_api": "paddle.nn.layer.norm._BatchNormBase", | ||
|
|
@@ -8656,6 +8733,7 @@ | |
| ], | ||
| "kwargs_change": { | ||
| "eps": "epsilon" | ||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3594,6 +3594,55 @@ def generate_code(self, kwargs): | |
| return code | ||
|
|
||
|
|
||
| class MseLossMatcher(BaseMatcher): | ||
| def generate_code(self, kwargs): | ||
|
||
| if "size_average" in kwargs: | ||
| size_average = kwargs.pop("size_average") | ||
| if "True" in size_average: | ||
| size_average = True | ||
| elif "False" in size_average: | ||
| size_average = False | ||
| else: | ||
| size_average = None | ||
| else: | ||
| size_average = None | ||
|
|
||
| if "reduce" in kwargs: | ||
| reduce = kwargs.pop("reduce") | ||
| if "True" in reduce: | ||
| reduce = True | ||
| elif "False" in reduce: | ||
| reduce = False | ||
| else: | ||
| reduce = None | ||
| else: | ||
| reduce = None | ||
|
|
||
| if size_average is not None or reduce is not None: | ||
| 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"""' | ||
|
|
||
| kwargs["reduction"] = reduction | ||
|
|
||
| API_TEMPLATE = textwrap.dedent( | ||
| """ | ||
| paddle.nn.MSELoss({}) | ||
| """ | ||
| ) | ||
| code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) | ||
|
|
||
| return code | ||
|
|
||
|
|
||
| class TupleAssignMatcher(BaseMatcher): | ||
| def generate_code(self, kwargs): | ||
| kwargs_change = {} | ||
|
|
@@ -3728,6 +3777,210 @@ def generate_code(self, kwargs): | |
| return GenericMatcher.generate_code(self, kwargs) | ||
|
|
||
|
|
||
| class L1LossMatcher(BaseMatcher): | ||
| def generate_code(self, kwargs): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 逻辑可以写成对每个kwargs遍历,判断是否kwargs,每个分支里再判断是否list,一共4个分支。用new_kwargs来接收kwargs,不然参数顺序会改变,导致代码风格不太好
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| if "size_average" in kwargs: | ||
| size_average = kwargs.pop("size_average") | ||
| if "True" in size_average: | ||
| size_average = True | ||
| elif "False" in size_average: | ||
| size_average = False | ||
| else: | ||
| size_average = None | ||
| else: | ||
| size_average = None | ||
|
|
||
| if "reduce" in kwargs: | ||
| reduce = kwargs.pop("reduce") | ||
| if "True" in reduce: | ||
| reduce = True | ||
| elif "False" in reduce: | ||
| reduce = False | ||
| else: | ||
| reduce = None | ||
| else: | ||
| reduce = None | ||
|
|
||
| if size_average is not None or reduce is not None: | ||
| 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"""' | ||
|
|
||
| kwargs["reduction"] = reduction | ||
|
|
||
| API_TEMPLATE = textwrap.dedent( | ||
| """ | ||
| paddle.nn.L1Loss({}) | ||
| """ | ||
| ) | ||
|
|
||
| code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) | ||
|
|
||
| return code | ||
|
|
||
|
|
||
| class BCELossMatcher(BaseMatcher): | ||
| def generate_code(self, kwargs): | ||
|
|
||
| if "size_average" in kwargs: | ||
| size_average = kwargs.pop("size_average") | ||
| if "True" in size_average: | ||
| size_average = True | ||
| elif "False" in size_average: | ||
| size_average = False | ||
| else: | ||
| size_average = None | ||
| else: | ||
| size_average = None | ||
|
|
||
| if "reduce" in kwargs: | ||
| reduce = kwargs.pop("reduce") | ||
| if "True" in reduce: | ||
| reduce = True | ||
| elif "False" in reduce: | ||
| reduce = False | ||
| else: | ||
| reduce = None | ||
| else: | ||
| reduce = None | ||
|
|
||
| if size_average is not None or reduce is not None: | ||
| 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"""' | ||
|
|
||
| kwargs["reduction"] = reduction | ||
|
|
||
| API_TEMPLATE = textwrap.dedent( | ||
| """ | ||
| paddle.nn.BCELoss({}) | ||
| """ | ||
| ) | ||
|
|
||
| code = API_TEMPLATE.format(self.kwargs_to_str(kwargs)) | ||
|
|
||
| return code | ||
|
|
||
|
|
||
| class FunctionalBinaryCrossEntropyMatcher(BaseMatcher): | ||
| def generate_code(self, kwargs): | ||
| if "size_average" in kwargs: | ||
| size_average = kwargs.pop("size_average") | ||
| if "True" in size_average: | ||
| size_average = True | ||
| elif "False" in size_average: | ||
| size_average = False | ||
| else: | ||
| size_average = None | ||
| else: | ||
| size_average = None | ||
|
|
||
| if "reduce" in kwargs: | ||
| reduce = kwargs.pop("reduce") | ||
| if "True" in reduce: | ||
| reduce = True | ||
| elif "False" in reduce: | ||
| reduce = False | ||
| else: | ||
| reduce = None | ||
| else: | ||
| reduce = None | ||
|
|
||
| if size_average is not None or reduce is not None: | ||
| 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"""' | ||
|
|
||
| kwargs["reduction"] = reduction | ||
|
|
||
| if "kwargs_change" in self.api_mapping: | ||
| kwargs_change = self.api_mapping["kwargs_change"] | ||
| for key in list(kwargs_change.keys()): | ||
| if key in kwargs: | ||
| kwargs[kwargs_change[key]] = kwargs[key] | ||
| kwargs.pop(key) | ||
|
|
||
| API_TEMPLACE = textwrap.dedent( | ||
| """ | ||
| paddle.nn.functional.binary_cross_entropy({}) | ||
| """ | ||
| ) | ||
| code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) | ||
|
|
||
| return code | ||
|
|
||
|
|
||
| class UnfoldMatcher(BaseMatcher): | ||
|
||
| def generate_code(self, kwargs): | ||
| if "kwargs_change" in self.api_mapping: | ||
| kwargs_change = self.api_mapping["kwargs_change"] | ||
| for key in list(kwargs_change.keys()): | ||
| if key in kwargs: | ||
| if isinstance(ast.literal_eval(kwargs[key]), tuple): | ||
| kwargs[key] = list(ast.literal_eval(kwargs[key])) | ||
| kwargs[kwargs_change[key]] = kwargs[key] | ||
| kwargs.pop(key) | ||
|
|
||
| if "paddings" not in kwargs: | ||
|
||
| kwargs["paddings"] = 0 | ||
|
|
||
| API_TEMPLACE = textwrap.dedent( | ||
| """ | ||
| paddle.nn.Unfold({}) | ||
| """ | ||
| ) | ||
| code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) | ||
|
|
||
| return code | ||
|
|
||
|
|
||
| class FunctionalUnfoldMatcher(BaseMatcher): | ||
| def generate_code(self, kwargs): | ||
| if "kwargs_change" in self.api_mapping: | ||
| kwargs_change = self.api_mapping["kwargs_change"] | ||
| for key in list(kwargs_change.keys()): | ||
| if key in kwargs: | ||
| if "input" not in key: | ||
| if isinstance(ast.literal_eval(kwargs[key]), tuple): | ||
| kwargs[key] = list(ast.literal_eval(kwargs[key])) | ||
| kwargs[kwargs_change[key]] = kwargs[key] | ||
| kwargs.pop(key) | ||
|
|
||
| API_TEMPLACE = textwrap.dedent( | ||
| """ | ||
| paddle.nn.functional.unfold({}) | ||
| """ | ||
| ) | ||
|
|
||
| code = API_TEMPLACE.format(self.kwargs_to_str(kwargs)) | ||
|
|
||
| return code | ||
|
|
||
| class ParameterMatcher(BaseMatcher): | ||
| def get_paddle_nodes(self, args, kwargs): | ||
| kwargs = self.parse_args_and_kwargs(args, kwargs) | ||
|
|
@@ -3948,3 +4201,4 @@ def generate_code(self, kwargs): | |
| if "dim" not in kwargs: | ||
| return None | ||
| return GenericMatcher.generate_code(self, kwargs) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个可以用genericmatcher吧,改成那个吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kernel_size 参数 pytorch支持tuple,paddle不支持,改为genericmatcher遇到tuple会报错