From f1d1a60af7917c94f8041d254ebe5d49a85406c8 Mon Sep 17 00:00:00 2001 From: jingguo-st Date: Fri, 19 Jul 2024 10:34:24 +0800 Subject: [PATCH 1/6] fix sub bug for ascend --- .../diopi_functions.yaml | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml index 2308ba7a3..7f2ef0c98 100755 --- a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml +++ b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml @@ -76,12 +76,27 @@ custom_code_at_the_beginning: | return dipu_add__tensor(self, other, -alpha); +- schema: sub.Scalar(Tensor self, Scalar other, Scalar alpha=1) -> Tensor + custom_code_at_the_beginning: | + at::Tensor out = UnaryOpInferrer().infer_out(self); + interface: diopiSubScalar(ctx, out, self, other, alpha) + - schema: "sub.Tensor(Tensor self, Tensor other, *, Scalar alpha=1) -> Tensor" - dummy_call_diopi: True + ins: [selfTmp] custom_code_at_the_beginning: | - at::native::sub_check(self, other); - auto out = BinaryOpInferrer().infer_out(self, other); - return dipu_add_out(self, other, -alpha, out); + if (is_scalar_on_cpu(other)) { + return dipu_sub_scalar(self, other.item(), alpha); + } + + at::Tensor selfTmp = self; + if (is_scalar_on_cpu(selfTmp)) { + selfTmp = selfTmp.to(other.device()); + } + + at::native::sub_check(selfTmp, other); + at::Tensor out = BinaryOpInferrer().infer_out(selfTmp, other); + + interface: diopiSub(ctx, out, selfTmp, other, alpha) - schema: "div.Scalar(Tensor self, Scalar other) -> Tensor" custom_code_at_the_beginning: | From 44d5c852f7ea37f7b62496fcd5680bdb879991b0 Mon Sep 17 00:00:00 2001 From: jingguo-st Date: Fri, 19 Jul 2024 11:10:53 +0800 Subject: [PATCH 2/6] replace sync call in sub --- dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml index 7f2ef0c98..a8e5b21fe 100755 --- a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml +++ b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml @@ -90,7 +90,8 @@ at::Tensor selfTmp = self; if (is_scalar_on_cpu(selfTmp)) { - selfTmp = selfTmp.to(other.device()); + selfTmp = nodispatch::empty({1}, self.options().device(other.device())); + dipu_fill__scalar(selfTmp, self.item()); } at::native::sub_check(selfTmp, other); From f593ec99cd62798c24d568afd44837d4a72f8e0c Mon Sep 17 00:00:00 2001 From: jingguo-st Date: Fri, 19 Jul 2024 12:29:32 +0800 Subject: [PATCH 3/6] fix bug --- dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml index a8e5b21fe..ad4cec00a 100755 --- a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml +++ b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml @@ -84,6 +84,8 @@ - schema: "sub.Tensor(Tensor self, Tensor other, *, Scalar alpha=1) -> Tensor" ins: [selfTmp] custom_code_at_the_beginning: | + at::native::sub_check(self, other); + if (is_scalar_on_cpu(other)) { return dipu_sub_scalar(self, other.item(), alpha); } @@ -94,7 +96,6 @@ dipu_fill__scalar(selfTmp, self.item()); } - at::native::sub_check(selfTmp, other); at::Tensor out = BinaryOpInferrer().infer_out(selfTmp, other); interface: diopiSub(ctx, out, selfTmp, other, alpha) From fad4275dd0726dce505d9fbde3c7945fb8b4a69e Mon Sep 17 00:00:00 2001 From: jingguo-st Date: Fri, 19 Jul 2024 16:29:40 +0800 Subject: [PATCH 4/6] fix sub check for alpha --- dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml index ad4cec00a..b1e99a808 100755 --- a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml +++ b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml @@ -87,6 +87,7 @@ at::native::sub_check(self, other); if (is_scalar_on_cpu(other)) { + at::native::alpha_check(self.scalar_type(), alpha); return dipu_sub_scalar(self, other.item(), alpha); } @@ -96,6 +97,7 @@ dipu_fill__scalar(selfTmp, self.item()); } + at::native::alpha_check(selfTmp.scalar_type(), alpha); at::Tensor out = BinaryOpInferrer().infer_out(selfTmp, other); interface: diopiSub(ctx, out, selfTmp, other, alpha) From 54e27273d839c07972281329785e29f84a63dbb9 Mon Sep 17 00:00:00 2001 From: jingguo-st Date: Sat, 20 Jul 2024 10:37:54 +0800 Subject: [PATCH 5/6] fix rsub unit test --- dipu/tests/python/unittests/test_rsub.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/dipu/tests/python/unittests/test_rsub.py b/dipu/tests/python/unittests/test_rsub.py index cbe46ff6a..e734739e1 100644 --- a/dipu/tests/python/unittests/test_rsub.py +++ b/dipu/tests/python/unittests/test_rsub.py @@ -23,8 +23,15 @@ def test_rsub(self): self._test_rsub(torch.ones(4, 5) * 1.1, torch.ones(4, 5) * 5, alpha=4) def test_rsub_scalar(self): - self._test_rsub_scalar(torch.ones(4, 5), 10) - self._test_rsub_scalar(torch.ones(4, 5), 10, 2.5) + # from torch: + # For integral input tensors, argument alpha must not be a floating point number + # Boolean alpha only supported for Boolean results + self._test_rsub_scalar(torch.ones(4, 5), 10, alpha=1) + self.assertRaisesRegex( + RuntimeError, + r"For integral input tensors, argument alpha must not be a floating point number\.", + lambda: self._test_rsub_scalar(torch.ones(4, 5), 10, 2.5), + ) if __name__ == "__main__": From 5e37727e468ec92dad653f317b2bfe1eefe7372f Mon Sep 17 00:00:00 2001 From: jingguo-st Date: Sat, 20 Jul 2024 17:56:54 +0800 Subject: [PATCH 6/6] fix rsub impl --- dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml index b1e99a808..9a972d52c 100755 --- a/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml +++ b/dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml @@ -1360,9 +1360,10 @@ - schema: rsub.Tensor(Tensor self, Tensor other, *, Scalar alpha=1) -> Tensor custom_code_at_the_beginning: | + if (is_scalar_on_cpu(self)) { + return dipu_sub_scalar(other, self.item(), alpha); + } auto out = nodispatch::empty_like(self); - // NOLINTNEXTLINE(readability-suspicious-call-argument) - return dipu_sub_out(other, self, alpha, out); interface: diopiSub(ctx, out, other, self, alpha) - schema: "unique_dim(Tensor self, int dim, bool sorted=True, bool return_inverse=False, bool return_counts=False) -> (Tensor out, Tensor indices, Tensor counts)"