-
Notifications
You must be signed in to change notification settings - Fork 61
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 7 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 |
---|---|---|
|
@@ -4941,6 +4941,15 @@ | |
"target": "label" | ||
} | ||
}, | ||
"torch.nn.MSELoss": { | ||
"Matcher": "SizeAverageMatcher", | ||
"paddle_api": "paddle.nn.MSELoss", | ||
"args_list": [ | ||
"size_average", | ||
"reduce", | ||
"reduction" | ||
] | ||
}, | ||
"torch.nn.functional.margin_ranking_loss": { | ||
"Matcher": "SizeAverageMatcher", | ||
"paddle_api": "paddle.nn.functional.margin_ranking_loss", | ||
|
@@ -8593,6 +8602,21 @@ | |
"pos_weight" | ||
] | ||
}, | ||
"torch.nn.functional.binary_cross_entropy": { | ||
"Matcher": "SizeAverageMatcher", | ||
"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", | ||
|
@@ -8670,6 +8694,16 @@ | |
"pos_weight" | ||
] | ||
}, | ||
"torch.nn.BCELoss": { | ||
"Matcher": "SizeAverageMatcher", | ||
"paddle_api": "paddle.nn.BCELoss", | ||
"args_list": [ | ||
"weight", | ||
"size_average", | ||
"reduce", | ||
"reduction" | ||
] | ||
}, | ||
"torch.utils.data.BatchSampler": { | ||
"Matcher": "TorchUtilDataBatchSampler", | ||
"args_list": [ | ||
|
@@ -8700,6 +8734,49 @@ | |
"input": "x" | ||
} | ||
}, | ||
"torch.nn.L1Loss": { | ||
"Matcher": "SizeAverageMatcher", | ||
"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": { | ||
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. 这个可以用genericmatcher吧,改成那个吧 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. 同torch.nn.Unfold |
||
"Matcher": "UnfoldMatcher", | ||
"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", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3359,6 +3359,30 @@ def generate_code(self, kwargs): | |
return GenericMatcher.generate_code(self, kwargs) | ||
|
||
|
||
class UnfoldMatcher(BaseMatcher): | ||
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. 这个可以起一个通用的名字,这个主要功能是把tuple转成list: 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 |
||
def generate_code(self, kwargs): | ||
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,不然参数顺序会改变,导致代码风格不太好
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 "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: | ||
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. 这个由于不可能是tuple,也不用单独判断 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 "(" in kwargs[key]: | ||
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. 直接判断 if isinstance(kwargs[key] , ast.Tuple): 吧 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. 这个在generate_code直接这样判断似乎不起作用 |
||
value = ast.literal_eval(kwargs[key]) | ||
if isinstance(value, tuple): | ||
kwargs[key] = list(ast.literal_eval(kwargs[key])) | ||
else: | ||
kwargs[key] = "list({})".format(kwargs[key]) | ||
kwargs[kwargs_change[key]] = kwargs[key] | ||
kwargs.pop(key) | ||
|
||
if "paddings" not in kwargs: | ||
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. 这个是默认值就不用单独设置了 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 |
||
kwargs["paddings"] = 0 | ||
|
||
code = "{}({})".format(self.get_paddle_api(), 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import textwrap | ||
|
||
from apibase import APIBase | ||
|
||
obj = APIBase("torch.nn.BCELoss") | ||
|
||
|
||
def test_case_1(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
weight = torch.tensor([0.5,0.2,0.3]) | ||
loss = torch.nn.BCELoss(weight=weight,size_average=True) | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) | ||
|
||
|
||
def test_case_2(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
weight = torch.tensor([0.5,0.2,0.3]) | ||
loss = torch.nn.BCELoss(weight=weight,size_average=False) | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) | ||
|
||
|
||
def test_case_3(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
weight = torch.tensor([0.5,0.2,0.3]) | ||
loss = torch.nn.BCELoss(weight=weight,reduction='none') | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) | ||
|
||
|
||
def test_case_4(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
weight = torch.tensor([0.5,0.2,0.3]) | ||
loss = torch.nn.BCELoss(weight=weight,reduction='mean') | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) | ||
|
||
|
||
def test_case_5(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
weight = torch.tensor([0.5,0.2,0.3]) | ||
loss = torch.nn.BCELoss(weight=weight,reduction='sum') | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) | ||
|
||
|
||
def test_case_6(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
weight = torch.tensor([0.5,0.2,0.3]) | ||
loss = torch.nn.BCELoss(weight=weight,reduce=True) | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) | ||
|
||
|
||
def test_case_7(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
weight = torch.tensor([0.5,0.2,0.3]) | ||
loss = torch.nn.BCELoss(weight=weight,reduce=False) | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) | ||
|
||
|
||
def test_case_8(): | ||
pytorch_code = textwrap.dedent( | ||
""" | ||
import torch | ||
import torch.nn as nn | ||
input = torch.tensor([[0.2837, 0.0297, 0.0355], | ||
[ 0.9112, 0.7526, 0.4061]]) | ||
target = torch.tensor([[1.,0.,1.],[0.,1.,0.]]) | ||
loss = torch.nn.BCELoss() | ||
result = loss(input,target) | ||
""" | ||
) | ||
obj.run(pytorch_code, ["result"]) |
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会报错