Skip to content

Commit 48cabd1

Browse files
committed
fix code
1 parent 53c5cbc commit 48cabd1

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

python/paddle/nn/layer/loss.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from paddle.base.framework import in_dynamic_or_pir_mode
2222
from paddle.utils.decorator_utils import (
2323
ParamAliasDecorator,
24-
legacy_reduction_guard,
25-
legacy_reduction_special_guard,
24+
legacy_reduction_decorator,
25+
legacy_reduction_special_decorator,
2626
)
2727

2828
from .. import functional as F
@@ -125,7 +125,7 @@ class BCEWithLogitsLoss(Layer):
125125
pos_weight: Tensor | None
126126
name: str | None
127127

128-
@legacy_reduction_guard
128+
@legacy_reduction_decorator
129129
def __init__(
130130
self,
131131
weight: Tensor | None = None,
@@ -423,7 +423,7 @@ class CrossEntropyLoss(Layer):
423423
label_smoothing: float
424424
name: str | None
425425

426-
@legacy_reduction_special_guard
426+
@legacy_reduction_special_decorator
427427
def __init__(
428428
self,
429429
weight: Tensor | None = None,
@@ -662,7 +662,7 @@ class MSELoss(Layer):
662662

663663
reduction: _ReduceMode
664664

665-
@legacy_reduction_guard
665+
@legacy_reduction_decorator
666666
def __init__(self, reduction: _ReduceMode = 'mean'):
667667
super().__init__()
668668
if reduction not in ['sum', 'mean', 'none']:
@@ -766,7 +766,7 @@ class L1Loss(Layer):
766766
reduction: _ReduceMode
767767
name: str | None
768768

769-
@legacy_reduction_guard
769+
@legacy_reduction_decorator
770770
def __init__(
771771
self, reduction: _ReduceMode = 'mean', name: str | None = None
772772
) -> None:
@@ -857,7 +857,7 @@ class BCELoss(Layer):
857857
reduction: _ReduceMode
858858
name: str | None
859859

860-
@legacy_reduction_guard
860+
@legacy_reduction_decorator
861861
def __init__(
862862
self,
863863
weight: Tensor | None = None,
@@ -970,7 +970,7 @@ class NLLLoss(Layer):
970970
971971
"""
972972

973-
@legacy_reduction_guard
973+
@legacy_reduction_decorator
974974
def __init__(
975975
self,
976976
weight: Tensor | None = None,
@@ -1059,7 +1059,7 @@ class PoissonNLLLoss(Layer):
10591059
10601060
"""
10611061

1062-
@legacy_reduction_guard
1062+
@legacy_reduction_decorator
10631063
def __init__(
10641064
self,
10651065
log_input: bool = True,
@@ -1191,7 +1191,7 @@ class KLDivLoss(Layer):
11911191
reduction: _ReduceMode
11921192
log_target: bool
11931193

1194-
@legacy_reduction_special_guard
1194+
@legacy_reduction_special_decorator
11951195
def __init__(
11961196
self, reduction: _ReduceMode = 'mean', log_target: bool = False
11971197
) -> None:
@@ -1264,7 +1264,7 @@ class MarginRankingLoss(Layer):
12641264
reduction: _ReduceMode
12651265
name: str | None
12661266

1267-
@legacy_reduction_guard
1267+
@legacy_reduction_decorator
12681268
def __init__(
12691269
self,
12701270
margin: float = 0.0,
@@ -1538,7 +1538,7 @@ class SmoothL1Loss(Layer):
15381538
delta: float
15391539
name: str | None
15401540

1541-
@legacy_reduction_guard
1541+
@legacy_reduction_decorator
15421542
def __init__(
15431543
self,
15441544
reduction: _ReduceMode = 'mean',
@@ -1628,7 +1628,7 @@ class MultiLabelSoftMarginLoss(Layer):
16281628
reduction: _ReduceMode
16291629
name: str | None
16301630

1631-
@legacy_reduction_guard
1631+
@legacy_reduction_decorator
16321632
def __init__(
16331633
self,
16341634
weight: Tensor | None = None,
@@ -1741,7 +1741,7 @@ class HingeEmbeddingLoss(Layer):
17411741
reduction: _ReduceMode
17421742
name: str | None
17431743

1744-
@legacy_reduction_guard
1744+
@legacy_reduction_decorator
17451745
def __init__(
17461746
self,
17471747
margin: float = 1.0,
@@ -1840,7 +1840,7 @@ class CosineEmbeddingLoss(Layer):
18401840
reduction: _ReduceMode
18411841
name: str | None
18421842

1843-
@legacy_reduction_guard
1843+
@legacy_reduction_decorator
18441844
def __init__(
18451845
self,
18461846
margin: float = 0,
@@ -2078,7 +2078,7 @@ class TripletMarginLoss(Layer):
20782078
reduction: _ReduceMode
20792079
name: str | None
20802080

2081-
@legacy_reduction_guard
2081+
@legacy_reduction_decorator
20822082
def __init__(
20832083
self,
20842084
margin: float = 1.0,
@@ -2195,7 +2195,7 @@ class MultiMarginLoss(Layer):
21952195
reduction: _ReduceMode
21962196
name: str | None
21972197

2198-
@legacy_reduction_guard
2198+
@legacy_reduction_decorator
21992199
def __init__(
22002200
self,
22012201
p: int = 1,
@@ -2291,7 +2291,7 @@ class MultiLabelMarginLoss(Layer):
22912291
reduction: _ReduceMode
22922292
name: str | None
22932293

2294-
@legacy_reduction_guard
2294+
@legacy_reduction_decorator
22952295
def __init__(
22962296
self,
22972297
reduction: _ReduceMode = 'mean',
@@ -2380,7 +2380,7 @@ class SoftMarginLoss(Layer):
23802380
reduction: _ReduceMode
23812381
name: str | None
23822382

2383-
@legacy_reduction_guard
2383+
@legacy_reduction_decorator
23842384
def __init__(
23852385
self, reduction: _ReduceMode = 'mean', name: str | None = None
23862386
) -> None:

python/paddle/utils/decorator_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,6 @@ def wrapper(*args: _InputT.args, **kwargs: _InputT.kwargs) -> _RetT:
778778

779779

780780
def compute_legacy_reduction(reduce_val, size_average_val):
781-
reduce_val = reduce_val if reduce_val != '' else None
782-
size_average_val = size_average_val if size_average_val != '' else None
783781
if reduce_val is False:
784782
return 'none'
785783
if reduce_val is True:
@@ -810,14 +808,16 @@ def get_legacy_reduce_and_size_average(cls_name, args, kwargs):
810808

811809
def raise_deprecated_error(cls_name, reduce_val, size_avg_val):
812810
suggested = compute_legacy_reduction(reduce_val, size_avg_val)
811+
reduce_val = None if reduce_val == '' else reduce_val
812+
size_avg_val = None if size_avg_val == '' else size_avg_val
813813
raise ValueError(
814814
f"[Deprecated] '{cls_name}' no longer supports 'reduce' or 'size_average'."
815815
f"\nDetected: reduce={reduce_val}, size_average={size_avg_val}"
816816
f"\nPlease use: reduction='{suggested}' instead."
817817
)
818818

819819

820-
def legacy_reduction_guard(init_func):
820+
def legacy_reduction_decorator(init_func):
821821
"""
822822
Function decorator for __init__: intercept deprecated 'reduce' and 'size_average'.
823823
"""
@@ -836,7 +836,7 @@ def wrapper(self, *args, **kwargs):
836836
return wrapper
837837

838838

839-
def legacy_reduction_special_guard(init_func):
839+
def legacy_reduction_special_decorator(init_func):
840840
"""
841841
Specialized decorator: add CrossEntropyLoss / KLDivLoss special case judgment based on general logic.
842842
"""

0 commit comments

Comments
 (0)