已合并
[fix][ops]MatmulReduceScatter算子修复string_view悬空指针 #5383
chenjl创建于 7月6日
[fix][ops]MatmulReduceScatter算子修复string_view悬空指针 #5383
已合并
共 2 个文件变更+13-7
| @@ -32,8 +32,11 @@ at::Tensor npu_mm_reduce_scatter_base(const at::Tensor & self, const at::Tensor | |||
| 32 | self.size(1), " and the K-axis of x2 is ", x2.size(0), "." + OPS_ERROR(ErrCode::PARAM)); | 32 | self.size(1), " and the K-axis of x2 is ", x2.size(0), "." + OPS_ERROR(ErrCode::PARAM)); |
| 33 | TORCH_CHECK(self.size(0) % world_size == 0, "The M-axis in input of Matmul should be be divisible by world_size." | 33 | TORCH_CHECK(self.size(0) % world_size == 0, "The M-axis in input of Matmul should be be divisible by world_size." |
| 34 | + OPS_ERROR(ErrCode::PARAM)); | 34 | + OPS_ERROR(ErrCode::PARAM)); |
| 35 | + std::string hcom_str = std::string(hcom); | ||
| 36 | + std::string reduce_op_str = std::string(reduce_op); | ||
| 35 | bool isSocBelowAscend950 = (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend950); | 37 | bool isSocBelowAscend950 = (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend950); |
| 36 | c10::string_view comm_mode_value = comm_mode.value_or("ai_cpu"); | 38 | c10::string_view comm_mode_value = comm_mode.value_or("ai_cpu"); |
| 39 | + std::string comm_mode_str = std::string(comm_mode_value); | ||
| 37 | auto output_size = {self.size(0) / world_size, x2.size(1)}; | 40 | auto output_size = {self.size(0) / world_size, x2.size(1)}; |
| 38 | auto result_dtype = self.scalar_type(); | 41 | auto result_dtype = self.scalar_type(); |
| 39 | bool has_quant = x2_scale.has_value(); | 42 | bool has_quant = x2_scale.has_value(); |
| @@ -41,9 +44,9 @@ at::Tensor npu_mm_reduce_scatter_base(const at::Tensor & self, const at::Tensor | |||
| 41 | result_dtype = x2_scale.value().scalar_type() == at::kLong ? at::kHalf: output_dtype.value_or(at::kBFloat16); | 44 | result_dtype = x2_scale.value().scalar_type() == at::kLong ? at::kHalf: output_dtype.value_or(at::kBFloat16); |
| 42 | } | 45 | } |
| 43 | auto result = at_npu::native::OpPreparation::apply_tensor_without_format(output_size, self.options().dtype(result_dtype)); | 46 | auto result = at_npu::native::OpPreparation::apply_tensor_without_format(output_size, self.options().dtype(result_dtype)); |
| 44 | - char *reduce_op_ptr = const_cast<char *>(reduce_op.data()); | 47 | + char *reduce_op_ptr = const_cast<char*>(reduce_op_str.data()); |
| 45 | - char *hcom_ptr = const_cast<char *>(hcom.data()); | 48 | + char *hcom_ptr = const_cast<char*>(hcom_str.data()); |
| 46 | - char *comm_mode_ptr = const_cast<char *>(comm_mode_value.data()); | 49 | + char *comm_mode_ptr = const_cast<char*>(comm_mode_str.data()); |
| 47 | const at::Tensor &bias_real = bias.value_or(at::Tensor()); | 50 | const at::Tensor &bias_real = bias.value_or(at::Tensor()); |
| 48 | int64_t stream_mode = ACL_STOP_ON_FAILURE; | 51 | int64_t stream_mode = ACL_STOP_ON_FAILURE; |
| 49 | int64_t block_size = 0; | 52 | int64_t block_size = 0; |
| @@ -51,7 +54,7 @@ at::Tensor npu_mm_reduce_scatter_base(const at::Tensor & self, const at::Tensor | |||
| 51 | at::Tensor quant_scale; | 54 | at::Tensor quant_scale; |
| 52 | at::Tensor amax_out; | 55 | at::Tensor amax_out; |
| 53 | if (isSocBelowAscend950) { | 56 | if (isSocBelowAscend950) { |
| 54 | - if (comm_mode_value == "ai_cpu") { | 57 | + if (comm_mode_str == "ai_cpu") { |
| 55 | TORCH_CHECK(!has_quant, "When comm_mode is ai_cpu, quantization not supported." + OPS_ERROR(ErrCode::PARAM)); | 58 | TORCH_CHECK(!has_quant, "When comm_mode is ai_cpu, quantization not supported." + OPS_ERROR(ErrCode::PARAM)); |
| 56 | EXEC_NPU_CMD(aclnnMatmulReduceScatter, self, x2, bias_real, hcom_ptr, reduce_op_ptr, comm_turn, stream_mode, result); | 59 | EXEC_NPU_CMD(aclnnMatmulReduceScatter, self, x2, bias_real, hcom_ptr, reduce_op_ptr, comm_turn, stream_mode, result); |
| 57 | } else { | 60 | } else { |
| @@ -45,6 +45,9 @@ std::tuple<at::Tensor, at::Tensor> npu_quant_mm_reduce_scatter( | |||
| 45 | bool isSocBelowAscend950 = (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend950); | 45 | bool isSocBelowAscend950 = (c10_npu::GetSocVersion() < c10_npu::SocVersion::Ascend950); |
| 46 | c10::string_view default_comm_mode = isSocBelowAscend950 ? "aiv" : "ai_cpu"; | 46 | c10::string_view default_comm_mode = isSocBelowAscend950 ? "aiv" : "ai_cpu"; |
| 47 | c10::string_view comm_mode_value = comm_mode.value_or(default_comm_mode); | 47 | c10::string_view comm_mode_value = comm_mode.value_or(default_comm_mode); |
| 48 | + std::string hcom_str = std::string(hcom); | ||
| 49 | + std::string reduce_op_str = std::string(reduce_op); | ||
| 50 | + std::string comm_mode_str = std::string(comm_mode_value); | ||
| 48 | at::IntArrayRef group_size_list = group_sizes.value_or(at::IntArrayRef{}); | 51 | at::IntArrayRef group_size_list = group_sizes.value_or(at::IntArrayRef{}); |
| 49 | int64_t group_size = op_plugin::utils::check_and_get_group_size(group_size_list); | 52 | int64_t group_size = op_plugin::utils::check_and_get_group_size(group_size_list); |
| 50 | TORCH_CHECK(group_size != -1, "Invalid group_sizes.", OPS_ERROR(ErrCode::PARAM)); | 53 | TORCH_CHECK(group_size != -1, "Invalid group_sizes.", OPS_ERROR(ErrCode::PARAM)); |
| @@ -69,9 +72,9 @@ std::tuple<at::Tensor, at::Tensor> npu_quant_mm_reduce_scatter( | |||
| 69 | } | 72 | } |
| 70 | c10::TensorOptions options = self.options().dtype(output_scalar_type); | 73 | c10::TensorOptions options = self.options().dtype(output_scalar_type); |
| 71 | auto result = npu_preparation::apply_tensor_without_format(output_size, options); | 74 | auto result = npu_preparation::apply_tensor_without_format(output_size, options); |
| 72 | - char* reduce_op_ptr = const_cast<char*>(reduce_op.data()); | 75 | + char *reduce_op_ptr = const_cast<char*>(reduce_op_str.data()); |
| 73 | - char* hcom_ptr = const_cast<char*>(hcom.data()); | 76 | + char *hcom_ptr = const_cast<char*>(hcom_str.data()); |
| 74 | - char *comm_mode_ptr = const_cast<char *>(comm_mode_value.data()); | 77 | + char *comm_mode_ptr = const_cast<char*>(comm_mode_str.data()); |
| 75 | const at::Tensor& bias_real = bias.value_or(at::Tensor()); | 78 | const at::Tensor& bias_real = bias.value_or(at::Tensor()); |
| 76 | const at::Tensor& quant_scale_real = quant_scale.value_or(at::Tensor()); | 79 | const at::Tensor& quant_scale_real = quant_scale.value_or(at::Tensor()); |
| 77 | int64_t stream_mode = ACL_STOP_ON_FAILURE; | 80 | int64_t stream_mode = ACL_STOP_ON_FAILURE; |