已合并
[fix] Refine error message of linalg_qr. #4278
maoyuanpeng1创建于 2月14日
[fix] Refine error message of linalg_qr. #4278
已合并
共 2 个文件变更+37-14
| @@ -42,14 +42,26 @@ std::tuple<c10::SmallVector<int64_t, N>, c10::SmallVector<int64_t, N>> qr_npu_ou | |||
| 42 | return std::tie(q_size, r_size); | 42 | return std::tie(q_size, r_size); |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | -inline void qr_check( | 45 | +inline bool mode_valid(c10::string_view mode) |
| 46 | - const at::Tensor& self) | ||
| 47 | { | 46 | { |
| 47 | + return (mode == "reduced" || mode == "complete" || mode == "r"); | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +void check_linalg_qr_input(const at::Tensor& self, c10::string_view mode) | ||
| 51 | +{ | ||
| 52 | + constexpr int MATRIX_DIM = 2; | ||
| 48 | TORCH_CHECK( | 53 | TORCH_CHECK( |
| 49 | - self.ndimension() >= 2, | 54 | + self.dim() >= MATRIX_DIM, |
| 50 | - "The input tensor must have at least 2 dimensions.", | 55 | + "linalg_qr: The input tensor must have at least 2 dimensions, but got ", |
| 51 | self.dim(), | 56 | self.dim(), |
| 52 | OPS_ERROR(ErrCode::PARAM)); | 57 | OPS_ERROR(ErrCode::PARAM)); |
| 58 | + | ||
| 59 | + TORCH_CHECK( | ||
| 60 | + mode_valid(mode), | ||
| 61 | + "linalg_qr: received unrecognized mode '", | ||
| 62 | + mode, | ||
| 63 | + "', expected one of 'reduced'(default), 'r', or 'complete'", | ||
| 64 | + OPS_ERROR(ErrCode::PARAM)); | ||
| 53 | } | 65 | } |
| 54 | 66 | ||
| 55 | std::tuple<at::Tensor&, at::Tensor&> qr_out_npu_nocheck( | 67 | std::tuple<at::Tensor&, at::Tensor&> qr_out_npu_nocheck( |
| @@ -76,8 +88,8 @@ std::tuple<at::Tensor&, at::Tensor&> linalg_qr_out( | |||
| 76 | at::Tensor& Q, | 88 | at::Tensor& Q, |
| 77 | at::Tensor& R) | 89 | at::Tensor& R) |
| 78 | { | 90 | { |
| 91 | + check_linalg_qr_input(self, mode); | ||
| 79 | bool some = (mode == "complete") ? false : true; | 92 | bool some = (mode == "complete") ? false : true; |
| 80 | - qr_check(self); | ||
| 81 | auto sizes = qr_npu_output_size(self, some); | 93 | auto sizes = qr_npu_output_size(self, some); |
| 82 | npu_preparation::CheckOut( | 94 | npu_preparation::CheckOut( |
| 83 | {self}, | 95 | {self}, |
| @@ -115,8 +127,8 @@ std::tuple<at::Tensor, at::Tensor> linalg_qr( | |||
| 115 | const at::Tensor& self, | 127 | const at::Tensor& self, |
| 116 | c10::string_view mode) | 128 | c10::string_view mode) |
| 117 | { | 129 | { |
| 130 | + check_linalg_qr_input(self, mode); | ||
| 118 | bool some = (mode == "complete") ? false : true; | 131 | bool some = (mode == "complete") ? false : true; |
| 119 | - qr_check(self); | ||
| 120 | auto sizes = qr_npu_output_size(self, some); | 132 | auto sizes = qr_npu_output_size(self, some); |
| 121 | at::Tensor Q = npu_preparation::apply_tensor(self, std::get<0>(sizes)); | 133 | at::Tensor Q = npu_preparation::apply_tensor(self, std::get<0>(sizes)); |
| 122 | at::Tensor R = npu_preparation::apply_tensor(self, std::get<1>(sizes)); | 134 | at::Tensor R = npu_preparation::apply_tensor(self, std::get<1>(sizes)); |
| @@ -27,6 +27,23 @@ static inline bool mode_valid(c10::string_view mode) | |||
| 27 | return (mode == "reduced" || mode == "complete" || mode == "r"); | 27 | return (mode == "reduced" || mode == "complete" || mode == "r"); |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | +static void check_linalg_qr_input(const at::Tensor& self, c10::string_view mode) | ||
| 31 | +{ | ||
| 32 | + constexpr int MATRIX_DIM = 2; | ||
| 33 | + TORCH_CHECK( | ||
| 34 | + self.dim() >= MATRIX_DIM, | ||
| 35 | + "linalg_qr: The input tensor must have at least 2 dimensions, but got ", | ||
| 36 | + self.dim(), | ||
| 37 | + OPS_ERROR(ErrCode::PARAM)); | ||
| 38 | + | ||
| 39 | + TORCH_CHECK( | ||
| 40 | + mode_valid(mode), | ||
| 41 | + "linalg_qr: received unrecognized mode '", | ||
| 42 | + mode, | ||
| 43 | + "', expected one of 'reduced'(default), 'r', or 'complete'", | ||
| 44 | + OPS_ERROR(ErrCode::PARAM)); | ||
| 45 | +} | ||
| 46 | + | ||
| 30 | static inline int64_t get_mode(c10::string_view mode) | 47 | static inline int64_t get_mode(c10::string_view mode) |
| 31 | { | 48 | { |
| 32 | if (mode == "complete") { | 49 | if (mode == "complete") { |
| @@ -67,11 +84,7 @@ std::tuple<at::Tensor &, at::Tensor &> linalg_qr_out(const at::Tensor &self, c10 | |||
| 67 | { | 84 | { |
| 68 | DO_COMPATIBILITY(aclnnLinalgQr, acl_op::linalg_qr_out(self, mode, Q, R)); | 85 | DO_COMPATIBILITY(aclnnLinalgQr, acl_op::linalg_qr_out(self, mode, Q, R)); |
| 69 | // 输入至少为2维tensor | 86 | // 输入至少为2维tensor |
| 70 | - TORCH_CHECK(self.ndimension() >= 2, "Expected nonempty least 2D tensor, but got a tensor with sizes ", | 87 | + check_linalg_qr_input(self, mode); |
| 71 | - self.dim(), OPS_ERROR(ErrCode::TYPE)); | ||
| 72 | - TORCH_CHECK(mode_valid(mode), | ||
| 73 | - "qr received unrecognized mode but expected one of 'reduced'(default), 'r', or 'complete'", | ||
| 74 | - OPS_ERROR(ErrCode::TYPE)); | ||
| 75 | auto sizes = linalg_qr_infer_shape(self, mode); | 88 | auto sizes = linalg_qr_infer_shape(self, mode); |
| 76 | npu_preparation::check_tensor({self}, Q, self, std::get<0>(sizes)); | 89 | npu_preparation::check_tensor({self}, Q, self, std::get<0>(sizes)); |
| 77 | npu_preparation::check_tensor({self}, R, self, std::get<1>(sizes)); | 90 | npu_preparation::check_tensor({self}, R, self, std::get<1>(sizes)); |
| @@ -83,9 +96,7 @@ std::tuple<at::Tensor &, at::Tensor &> linalg_qr_out(const at::Tensor &self, c10 | |||
| 83 | std::tuple<at::Tensor, at::Tensor> linalg_qr(const at::Tensor &self, c10::string_view mode) | 96 | std::tuple<at::Tensor, at::Tensor> linalg_qr(const at::Tensor &self, c10::string_view mode) |
| 84 | { | 97 | { |
| 85 | DO_COMPATIBILITY(aclnnLinalgQr, acl_op::linalg_qr(self, mode)); | 98 | DO_COMPATIBILITY(aclnnLinalgQr, acl_op::linalg_qr(self, mode)); |
| 86 | - TORCH_CHECK(self.ndimension() >= 2, "Expected nonempty least 2D tensor, but got a tensor with sizes ", | 99 | + check_linalg_qr_input(self, mode); |
| 87 | - self.dim(), OPS_ERROR(ErrCode::TYPE)); | ||
| 88 | - TORCH_CHECK(mode_valid(mode), "Mode should be reduced, complete or r", OPS_ERROR(ErrCode::TYPE)); | ||
| 89 | auto sizes = linalg_qr_infer_shape(self, mode); | 100 | auto sizes = linalg_qr_infer_shape(self, mode); |
| 90 | at::Tensor Q = npu_preparation::apply_tensor_without_format(std::get<0>(sizes), self.options()); | 101 | at::Tensor Q = npu_preparation::apply_tensor_without_format(std::get<0>(sizes), self.options()); |
| 91 | at::Tensor R = npu_preparation::apply_tensor_without_format(std::get<1>(sizes), self.options()); | 102 | at::Tensor R = npu_preparation::apply_tensor_without_format(std::get<1>(sizes), self.options()); |