已合并
Fix tensor shape not equal in tanhbackward #1279
wang-guangbin创建于 2024年2月19日
Fix tensor shape not equal in tanhbackward #1279
已合并
从refs/pull/1279/head合入到master
共 2 个文件变更+38-19
| @@ -25,13 +25,13 @@ at::Tensor& tanh_backward_out_npu_nocheck( | |||
| 25 | at::Tensor& result, | 25 | at::Tensor& result, |
| 26 | const at::Tensor& grad_output, | 26 | const at::Tensor& grad_output, |
| 27 | const at::Tensor& self) { | 27 | const at::Tensor& self) { |
| 28 | - at_npu::native::OpCommand cmd; | 28 | + at_npu::native::OpCommand cmd; |
| 29 | - cmd.Name("TanhGrad") | 29 | + cmd.Name("TanhGrad") |
| 30 | - .Input(self) | 30 | + .Input(self) |
| 31 | - .Input(grad_output) | 31 | + .Input(grad_output) |
| 32 | - .Output(result) | 32 | + .Output(result) |
| 33 | - .Run(); | 33 | + .Run(); |
| 34 | - return result; | 34 | + return result; |
| 35 | } | 35 | } |
| 36 | } // namespace | 36 | } // namespace |
| 37 | 37 | ||
| @@ -39,21 +39,23 @@ at::Tensor& tanh_backward_out( | |||
| 39 | const at::Tensor& grad_output, | 39 | const at::Tensor& grad_output, |
| 40 | const at::Tensor& self, | 40 | const at::Tensor& self, |
| 41 | at::Tensor& result) { | 41 | at::Tensor& result) { |
| 42 | - npu_preparation::CheckOut({grad_output, self}, result, self); | 42 | + auto output_size = op_infer::broadcast_ops_npu_output_size(grad_output, self); |
| 43 | - if (!npu_utils::check_match(&result)) { | 43 | + npu_preparation::CheckOut({grad_output, self}, result, self, output_size); |
| 44 | - at::Tensor contiguous_result = npu_utils::format_contiguous(result); | 44 | + if (!npu_utils::check_match(&result)) { |
| 45 | - tanh_backward_out_npu_nocheck(contiguous_result, grad_output, self); | 45 | + at::Tensor contiguous_result = npu_utils::format_contiguous(result); |
| 46 | - npu_utils::format_fresh_view(result, contiguous_result); | 46 | + tanh_backward_out_npu_nocheck(contiguous_result, grad_output, self); |
| 47 | - } else { | 47 | + npu_utils::format_fresh_view(result, contiguous_result); |
| 48 | - tanh_backward_out_npu_nocheck(result, grad_output, self); | 48 | + } else { |
| 49 | - } | 49 | + tanh_backward_out_npu_nocheck(result, grad_output, self); |
| 50 | + } | ||
| 50 | 51 | ||
| 51 | - return result; | 52 | + return result; |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | at::Tensor tanh_backward(const at::Tensor& grad_output, const at::Tensor& self) { | 55 | at::Tensor tanh_backward(const at::Tensor& grad_output, const at::Tensor& self) { |
| 55 | - at::Tensor result = npu_preparation::apply_tensor(self); | 56 | + auto output_size = op_infer::broadcast_ops_npu_output_size(grad_output, self); |
| 56 | - tanh_backward_out_npu_nocheck(result, grad_output, self); | 57 | + at::Tensor result = npu_preparation::apply_tensor(self, output_size); |
| 57 | - return result; | 58 | + tanh_backward_out_npu_nocheck(result, grad_output, self); |
| 59 | + return result; | ||
| 58 | } | 60 | } |
| 59 | } // namespace acl_op | 61 | } // namespace acl_op |
| @@ -0,0 +1,17 @@ | |||
| 1 | +import torch | ||
W | |||
| 2 | + | ||
| 3 | +import torch_npu | ||
| 4 | +from torch_npu.testing.testcase import TestCase, run_tests | ||
| 5 | + | ||
| 6 | + | ||
| 7 | +class TestTanhBackward(TestCase): | ||
| 8 | + | ||
| 9 | + def test_tanh_backward_diff_shape(self): | ||
| 10 | + gradoutput = torch.randn(16, 16) | ||
| 11 | + a = torch.randn(16) | ||
| 12 | + cpu_result = torch.ops.aten.tanh_backward(gradoutput, a) | ||
| 13 | + npu_result = torch.ops.aten.tanh_backward(gradoutput.npu(), a.npu()) | ||
| 14 | + self.assertRtolEqual(cpu_result, npu_result) | ||
| 15 | + | ||
| 16 | +if __name__ == "__main__": | ||
| 17 | + run_tests() | ||
当前op仓test文件均没有copyright,有需要可以后面统一整改。