Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions tester/paddle_to_torch/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,19 @@ def apply(self, paddle_api: str) -> ConvertResult:
defaults_code, map_code = self.apply_generic()
pre = """
shp = label.shape
if len(input.shape) > 2:
perm = [0] + [len(input.shape)-1]+ [i for i in range(1,len(input.shape)-1)]
input = input.permute(*perm)
axis = locals().get('axis',-1)
label = label.squeeze(-1)
axis = locals().get('axis', -1)
num_classes = input.shape[axis]
if soft_label:
# soft label: 维持原 channels-first permute 逻辑
if len(input.shape) > 2:
perm = [0] + [len(input.shape)-1]+ [i for i in range(1,len(input.shape)-1)]
input = input.permute(*perm)
label = label.squeeze(-1)
else:
# hard label: 展平成 2D [N, C], 让 torch 与 paddle 走相同的 cross_entropy kernel,
# 避免 [N, C, d...] 触发 torch spatial nll_loss 路径 (与 2D 路径差 ~1 ulp, 造成假 diff)
input = input.movedim(axis, -1).reshape(-1, num_classes)
label = label.reshape(-1)
if weight is not None:
weight.requires_grad = False
if label.dtype == torch.int32:
Expand Down
Loading