已合并
[fix]fix_ones_like_bug_on_cpu_device #4388
cuiduo创建于 3月6日
[fix]fix_ones_like_bug_on_cpu_device #4388
已合并
共 3 个文件变更+10-2
| @@ -42,7 +42,7 @@ at::Tensor ones_like( | |||
| 42 | auto device = c10::device_or_default(device_opt); | 42 | auto device = c10::device_or_default(device_opt); |
| 43 | if (!torch_npu::utils::is_npu(device)) { | 43 | if (!torch_npu::utils::is_npu(device)) { |
| 44 | auto result = at::empty_like(self, dtype_opt, layout_opt, device_opt, pin_memory_opt, optional_memory_format); | 44 | auto result = at::empty_like(self, dtype_opt, layout_opt, device_opt, pin_memory_opt, optional_memory_format); |
| 45 | - return acl_op::fill_(result, 1.); | 45 | + return result.fill_(1); |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | c10::TensorOptions option = c10::TensorOptions().dtype(dtype_opt) | 48 | c10::TensorOptions option = c10::TensorOptions().dtype(dtype_opt) |
| @@ -32,7 +32,7 @@ at::Tensor ones_like(const at::Tensor &self, | |||
| 32 | auto device = c10::device_or_default(device_opt); | 32 | auto device = c10::device_or_default(device_opt); |
| 33 | if (!torch_npu::utils::is_npu(device)) { | 33 | if (!torch_npu::utils::is_npu(device)) { |
| 34 | auto result = at::empty_like(self, dtype_opt, layout_opt, device_opt, pin_memory_opt, optional_memory_format); | 34 | auto result = at::empty_like(self, dtype_opt, layout_opt, device_opt, pin_memory_opt, optional_memory_format); |
| 35 | - return op_api::fill_(result, 1.); | 35 | + return result.fill_(1); |
| 36 | } | 36 | } |
| 37 | c10::TensorOptions options_ = c10::TensorOptions().dtype(dtype_opt) | 37 | c10::TensorOptions options_ = c10::TensorOptions().dtype(dtype_opt) |
| 38 | .device(device_opt) | 38 | .device(device_opt) |
| @@ -77,6 +77,14 @@ class TestOnesLike(TestCase): | |||
| 77 | self.assertEqual(cpu_dtype, npu_dtype) | 77 | self.assertEqual(cpu_dtype, npu_dtype) |
| 78 | self.assertRtolEqual(cpu_output, npu_output) | 78 | self.assertRtolEqual(cpu_output, npu_output) |
| 79 | 79 | ||
| 80 | + def test_ones_cpu_cases(self): | ||
| 81 | + cpu_input = torch.tensor([3, 1], device="cpu", dtype=torch.int32) | ||
| 82 | + npu_input = torch.tensor([3, 1], device="npu", dtype=torch.int32) | ||
| 83 | + kwargs = {"device": "cpu"} | ||
| 84 | + cpu_output = torch.ones_like(cpu_input, **kwargs) | ||
| 85 | + npu_output = torch.ones_like(npu_input, **kwargs) | ||
| 86 | + self.assertRtolEqual(cpu_output, npu_output) | ||
| 87 | + | ||
| 80 | 88 | ||
| 81 | if __name__ == "__main__": | 89 | if __name__ == "__main__": |
| 82 | run_tests() | 90 | run_tests() |