已合并
Bugfix for multilabel_margin_loss. #2216
weixin_40805272创建于 2025年3月4日
Bugfix for multilabel_margin_loss. #2216
已合并
weixin_40805272创建于 2025年3月4日
refs/pull/2216/head合入到master
2 个文件变更+21-17
Mop_plugin/ops/aclops/LossKernelNpu.cpp+2-15
@@ -66,21 +66,8 @@ at::Tensor& multilabel_margin_loss_out(
66 const at::Tensor& target,66 const at::Tensor& target,
67 int64_t reduction,67 int64_t reduction,
68 at::Tensor& output) {68 at::Tensor& output) {
69 c10::SmallVector<int64_t, SIZE> output_size;69 at::Tensor is_target = npu_preparation::apply_tensor(target);
70 const auto ndims = self.dim();70 return std::get<0>(at::multilabel_margin_loss_forward_out(output, is_target, self, target, reduction));
71 int64_t nframe;
72 if (ndims <= 1) {
73 nframe = 1;
74 } else {
75 nframe = self.size(0);
76 }
77 
78 if (reduction == at::Reduction::None) {
79 output_size = {nframe};
80 }
81 output = npu_preparation::apply_tensor(output_size, self.options(), self);
82 at::Tensor is_target = npu_preparation::apply_tensor(target);
83 return std::get<0>(at::multilabel_margin_loss_forward_out(output, is_target, self, target, reduction));
84}71}
85 72 
86at::Tensor multilabel_margin_loss(73at::Tensor multilabel_margin_loss(
Mtest/test_base_ops/test_multilabel_margin_loss.py+19-2
@@ -1,7 +1,7 @@
1import torch
2import numpy as np1import numpy as np
3import torch_npu
4 2 
3import torch
4import torch_npu
5from torch_npu.testing.testcase import TestCase, run_tests5from torch_npu.testing.testcase import TestCase, run_tests
6 6 
7 7 
@@ -84,6 +84,23 @@ class TestMultilabelMarginLoss(TestCase):
84 npu_output = self.npu_op_exec_out(data_npu, target_npu, c_npu, reduction)84 npu_output = self.npu_op_exec_out(data_npu, target_npu, c_npu, reduction)
85 self.assertRtolEqual(cpu_output, npu_output)85 self.assertRtolEqual(cpu_output, npu_output)
86 86 
87 def test_multilabel_margin_loss_out_2(self):
88 a = np.random.uniform(-0.001, 0.0, (97, 128)).astype(np.float32)
89 b = np.random.randint(-1, 64, (97, 128)).astype(np.int32)
90 out = np.random.uniform(-0.001, 0.0, (97, 128)).astype(np.float32)
91 a1 = torch.from_numpy(a)
92 b1 = torch.from_numpy(b).to(torch.int64)
93 out1 = torch.from_numpy(out)
94 
95 a2 = torch.from_numpy(a).npu()
96 b2 = torch.from_numpy(b).npu()
97 out2 = torch.from_numpy(out).npu()
98 
99 reduction = 2
100 output_data = self.cpu_op_exec_out(a1, b1, out1, reduction)
101 output_data_npu = self.npu_op_exec_out(a2, b2, out2, reduction)
102 self.assertRtolEqual(output_data, output_data_npu)
103 
87 def test_multilabel_margin_loss_float16_1(self):104 def test_multilabel_margin_loss_float16_1(self):
88 data = torch.Tensor([[0.1, 0.2, 0.4, 0.8], [0.1, 0.2, 0.4, 0.8]]).to(torch.float32)105 data = torch.Tensor([[0.1, 0.2, 0.4, 0.8], [0.1, 0.2, 0.4, 0.8]]).to(torch.float32)
89 target = torch.Tensor([[3, 0, -1, 1], [0, 1, 3, -1]]).to(torch.int64)106 target = torch.Tensor([[3, 0, -1, 1], [0, 1, 3, -1]]).to(torch.int64)