diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.cc index 3a865e95cee..ec0a900036a 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.cc @@ -80,7 +80,7 @@ int CTCLossV2CpuKernelMod::Resize(const BaseOperatorPtr &base_operator, const st } template -void CTCLossV2CpuKernelMod::LossCompute(S *log_probs_p, S *log_alpha_p, T *tar_p, SoftParam params) { +void CTCLossV2CpuKernelMod::LossCompute(const S *log_probs_p, S *log_alpha_p, const T *tar_p, SoftParam params) const { constexpr S neg_inf = -std::numeric_limits::infinity(); int64_t input_length = params.input_length; int64_t target_length = params.target_length; @@ -112,7 +112,7 @@ void CTCLossV2CpuKernelMod::LossCompute(S *log_probs_p, S *log_alpha_p, T *tar_p } else { log_a3 = neg_inf; } - if (log_max == neg_inf) { + if (std::isinf(log_max) && std::signbit(log_max)) { log_max = 0; } S log_three_sum = std::log(std::exp(log_a1 - log_max) + std::exp(log_a2 - log_max) + std::exp(log_a3 - log_max)) + @@ -124,7 +124,8 @@ void CTCLossV2CpuKernelMod::LossCompute(S *log_probs_p, S *log_alpha_p, T *tar_p } template -bool CTCLossV2CpuKernelMod::IndexProcessing(T *in_len_p, T *tar_len_p, std::vector *target_offsets) { +bool CTCLossV2CpuKernelMod::IndexProcessing(const T *in_len_p, const T *tar_len_p, + std::vector *target_offsets) { const int64_t target_stride = max_target_length_; for (size_t i = 0; i < LongToSize(batch_sizes_); ++i) { if (tar_len_p[i] < 0 || tar_len_p[i] > target_stride) { @@ -155,12 +156,12 @@ template bool CTCLossV2CpuKernelMod::LaunchKernel(const std::vector &inputs, const std::vector &, const std::vector &outputs) { - auto log_probs_p = reinterpret_cast(inputs[kIndex0]->addr); - auto tar_p = reinterpret_cast(inputs[kIndex1]->addr); - auto in_len_p = reinterpret_cast(inputs[kIndex2]->addr); - auto tar_len_p = reinterpret_cast(inputs[kIndex3]->addr); - auto neg_log_p = reinterpret_cast(outputs[kIndex0]->addr); - auto log_alpha_p = reinterpret_cast(outputs[kIndex1]->addr); + auto log_probs_p = static_cast(inputs[kIndex0]->addr); + auto tar_p = static_cast(inputs[kIndex1]->addr); + auto in_len_p = static_cast(inputs[kIndex2]->addr); + auto tar_len_p = static_cast(inputs[kIndex3]->addr); + auto neg_log_p = static_cast(outputs[kIndex0]->addr); + auto log_alpha_p = static_cast(outputs[kIndex1]->addr); std::vector target_offsets(LongToSize(batch_sizes_)); if (!IndexProcessing(in_len_p, tar_len_p, &target_offsets)) { return false; @@ -184,7 +185,7 @@ bool CTCLossV2CpuKernelMod::LaunchKernel(const std::vector & S l1 = log_alpha_p[log_alpha_it(b, in_len - 1, tar_len * target_mul)]; S l2 = log_alpha_p[log_alpha_it(b, in_len - 1, tar_len * target_mul - 1)]; S m = std::max(l1, l2); - m = ((m == -std::numeric_limits::infinity()) ? 0 : m); + m = ((std::isinf(m) && std::signbit(m)) ? 0 : m); S log_likelihood = std::log(std::exp(l1 - m) + std::exp(l2 - m)) + m; neg_log_p[b] = -log_likelihood; } diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.h b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.h index 73b9cb7d37d..fcca1be032e 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_cpu_kernel.h @@ -17,6 +17,7 @@ #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_CTCLOSS_V2_CPU_KERNEL_H_ #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_CTCLOSS_V2_CPU_KERNEL_H_ +#include #include #include #include @@ -55,7 +56,7 @@ class CTCLossV2CpuKernelMod : public NativeCpuKernelMod, public MatchKernelHelpe int64_t batch; }; template - inline int64_t GetBlankPaddedTarget(const target_t *target, int64_t offset, int64_t idx) { + inline int64_t GetBlankPaddedTarget(const target_t *target, int64_t offset, int64_t idx) const { constexpr int64_t interval = 2; if (idx % interval == 0) { return blank_; @@ -64,9 +65,9 @@ class CTCLossV2CpuKernelMod : public NativeCpuKernelMod, public MatchKernelHelpe } } template - void LossCompute(S *log_probs_p, S *log_alpha_p, T *tar_p, SoftParam params); + void LossCompute(const S *log_probs_p, S *log_alpha_p, const T *tar_p, SoftParam params) const; template - bool IndexProcessing(T *in_len_p, T *tar_len_p, std::vector *target_offsets); + bool IndexProcessing(const T *in_len_p, const T *tar_len_p, std::vector *target_offsets); // Variables for the operator itself int64_t blank_{0}; // Stands for T diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.cc index ec879fe35b8..044991a5f1c 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.cc @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include "abstract/utils.h" #include "mindspore/core/ops/ctc_loss_v2_grad.h" namespace mindspore { @@ -37,7 +37,7 @@ struct SoftParam { }; template -static inline int64_t get_target_prime(target_t *target, int64_t offset, int64_t idx, int64_t BLANK) { +static inline int64_t get_target_prime(const target_t *target, int64_t offset, int64_t idx, int64_t BLANK) { constexpr int64_t even = 2; if (idx % even == 0) { return BLANK; @@ -88,8 +88,8 @@ int CTCLossV2GradCpuKernelMod::Resize(const BaseOperatorPtr &base_operator, cons } template -void ComputeGrad(scalar_t *log_probs, const NdTensorIterator &log_probs_it, SoftParam params, - scalar_t *log_alpha, const NdTensorIterator &log_alpha_it, scalar_t *log_beta, +void ComputeGrad(const scalar_t *log_probs, const NdTensorIterator &log_probs_it, SoftParam params, + const scalar_t *log_alpha, const NdTensorIterator &log_alpha_it, scalar_t *log_beta, const NdTensorIterator &log_beta_it, scalar_t *grad, const NdTensorIterator &grad_it) { int64_t blank_ = params.blank_; int64_t input_length = params.input_length; @@ -121,14 +121,14 @@ void ComputeGrad(scalar_t *log_probs, const NdTensorIterator &log_probs_i } else { lb3 = neginf; } - if (lbmax == neginf) { + if (std::isinf(lbmax) && std::signbit(lbmax)) { lbmax = 0; } log_beta[log_beta_it(b, t, s)] = std::log(std::exp(lb1 - lbmax) + std::exp(lb2 - lbmax) + std::exp(lb3 - lbmax)) + lbmax + log_probs[log_probs_it(t, b, current_target_prime)]; scalar_t log_alpha_beta = log_alpha[log_alpha_it(b, t, s)] + log_beta[log_beta_it(b, t, s)]; scalar_t &lcab = grad[grad_it(t, b, current_target_prime)]; - if (lcab == neginf) { + if (std::isinf(lcab) && std::signbit(lcab)) { lcab = log_alpha_beta; } else { scalar_t max = std::max(lcab, log_alpha_beta); @@ -140,16 +140,16 @@ void ComputeGrad(scalar_t *log_probs, const NdTensorIterator &log_probs_i template bool CTCLossV2GradCpuKernelMod::LaunchKernel(const std::vector &inputs, const std::vector &workspace, - const std::vector &outputs) { - auto grad_out = reinterpret_cast(inputs[kIndex0]->addr); - auto log_probs = reinterpret_cast(inputs[kIndex1]->addr); - auto targets = reinterpret_cast(inputs[kIndex2]->addr); - auto input_lengths = reinterpret_cast(inputs[kIndex3]->addr); - auto target_lengths = reinterpret_cast(inputs[kIndex4]->addr); - auto neg_log_likelihood = reinterpret_cast(inputs[kIndex5]->addr); - auto log_alpha = reinterpret_cast(inputs[kIndex6]->addr); - auto log_beta = reinterpret_cast(workspace[kIndex0]->addr); - auto grad = reinterpret_cast(outputs[kIndex0]->addr); + const std::vector &outputs) const { + auto grad_out = static_cast(inputs[kIndex0]->addr); + auto log_probs = static_cast(inputs[kIndex1]->addr); + auto targets = static_cast(inputs[kIndex2]->addr); + auto input_lengths = static_cast(inputs[kIndex3]->addr); + auto target_lengths = static_cast(inputs[kIndex4]->addr); + auto neg_log_likelihood = static_cast(inputs[kIndex5]->addr); + auto log_alpha = static_cast(inputs[kIndex6]->addr); + auto log_beta = static_cast(workspace[kIndex0]->addr); + auto grad = static_cast(outputs[kIndex0]->addr); constexpr scalar_t neginf = -std::numeric_limits::infinity(); std::fill(grad, grad + (T_ * batch_size_ * num_labels_), neginf); @@ -160,7 +160,7 @@ bool CTCLossV2GradCpuKernelMod::LaunchKernel(const std::vector log_beta_it(batch_size_, T_, target_mul * max_target_length_ + 1); for (int64_t b = 0; b < batch_size_; b++) { scalar_t nll = neg_log_likelihood[b]; - if (zero_infinity_ && nll == std::numeric_limits::infinity()) { + if (zero_infinity_ && std::isinf(nll) && !std::signbit(nll)) { for (int t = 0; t < T_; t++) { for (int c = 0; c < num_labels_; c++) { grad[grad_it(t, b, c)] = 0; @@ -189,7 +189,7 @@ bool CTCLossV2GradCpuKernelMod::LaunchKernel(const std::vector(targets)}; + SoftParam param = {blank_, input_length, target_length, tg_batch_offset, b, static_cast(targets)}; ComputeGrad(log_probs, log_probs_it, param, log_alpha, log_alpha_it, log_beta, log_beta_it, grad, grad_it); scalar_t gr = grad_out[b]; diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.h b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.h index e7db1a0019b..ebf9e9814cb 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.h +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/ctcloss_v2_grad_cpu_kernel.h @@ -58,7 +58,7 @@ class CTCLossV2GradCpuKernelMod : public NativeCpuKernelMod, public MatchKernelH // Dealing with multiple types template bool LaunchKernel(const std::vector &inputs, const std::vector &workspace, - const std::vector &outputs); + const std::vector &outputs) const; }; } // namespace kernel } // namespace mindspore diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/dense_to_dense_set_operation_cpu_kernel.cc b/mindspore/ccsrc/plugin/device/cpu/kernel/dense_to_dense_set_operation_cpu_kernel.cc index 716278e59f8..06223190cda 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/dense_to_dense_set_operation_cpu_kernel.cc +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/dense_to_dense_set_operation_cpu_kernel.cc @@ -121,7 +121,7 @@ void GetGroupSet(const kernel::AddressPtr input, const size_t last_dim, const st << "but got " << group_indices.size() << " and " << input_strides.size() << "."; } result->clear(); - auto data_ptr = reinterpret_cast(input->addr); + auto data_ptr = static_cast(input->addr); const auto start = std::inner_product(group_indices.begin(), group_indices.end(), input_strides.begin(), 0UL); const auto end = start + last_dim; for (size_t i = start; i < end; ++i) { @@ -199,9 +199,9 @@ bool DenseToDenseSetOperationCpuKernelMod::PopulateOutput(const std::vector &outputs, const ShapeVector &output_shape, const size_t num_values, const std::map, std::set> *sets) { - auto out_indices_ptr = reinterpret_cast(outputs[kOutput1]->addr); - auto out_values_ptr = reinterpret_cast(outputs[kOutput2]->addr); - auto out_shape_ptr = reinterpret_cast(outputs[kOutput3]->addr); + auto out_indices_ptr = static_cast(outputs[kOutput1]->addr); + auto out_values_ptr = static_cast(outputs[kOutput2]->addr); + auto out_shape_ptr = static_cast(outputs[kOutput3]->addr); size_t output_shape_size = output_shape.size(); auto num_values_signed = SizeToLong(num_values); auto output_shape_size_signed = SizeToLong(output_shape_size); @@ -230,14 +230,14 @@ bool DenseToDenseSetOperationCpuKernelMod::PopulateOutput(const std::vector &, const std::vector &outputs, const size_t shape_size, const int size) { - auto output_indices = reinterpret_cast(outputs[kOutputIndicesStart]->addr); - auto output_values = reinterpret_cast(outputs[kOutputValuesStart]->addr); - auto output_shape = reinterpret_cast(outputs[kOutputShapesStart]->addr); + auto output_indices = static_cast(outputs[kOutputIndicesStart]->addr); + auto output_values = static_cast(outputs[kOutputValuesStart]->addr); + auto output_shape = static_cast(outputs[kOutputShapesStart]->addr); auto input_coo_num = input_num_ / kCOOTensorNum; const auto &first_shape_ptr = reinterpret_cast(inputs[kSpInputShapesStart * input_coo_num]->addr); std::map dim_position_map = {}; @@ -147,9 +147,9 @@ bool SparseConcatCpuKernelMod::SparseConcat(const std::vector(inputs[kSpInputIndicesStart * input_coo_num + i]->addr); - const auto &values_ptr = reinterpret_cast(inputs[kSpInputValuesStart * input_coo_num + i]->addr); - const auto &shape_ptr = reinterpret_cast(inputs[kOutputShapesStart * input_coo_num + i]->addr); + const auto &indices_ptr = static_cast(inputs[kSpInputIndicesStart * input_coo_num + i]->addr); + const auto &values_ptr = static_cast(inputs[kSpInputValuesStart * input_coo_num + i]->addr); + const auto &shape_ptr = static_cast(inputs[kOutputShapesStart * input_coo_num + i]->addr); auto cur_axis_shape = *(shape_ptr + concat_dim_); for (unsigned int j = 0; j < inputs[kSpInputIndicesStart * input_coo_num + i]->size / sizeof(int64_t); j++) { if (static_cast(j % shape_size) == concat_dim_) { @@ -227,7 +227,7 @@ bool SparseConcatCpuKernelMod::LaunchKernel(const std::vector(inputs, workspace, outputs, shape_size, size); + (void)SparseConcat(inputs, workspace, outputs, shape_size, size); return true; } diff --git a/mindspore/core/ops/sparse_concat.cc b/mindspore/core/ops/sparse_concat.cc index a0093c6f9db..fccd1f5b081 100644 --- a/mindspore/core/ops/sparse_concat.cc +++ b/mindspore/core/ops/sparse_concat.cc @@ -91,8 +91,9 @@ std::vector SparseConcatInferType(const PrimitivePtr &primitive, auto ind_type = inputs_indices[i]->BuildType(); auto sha_type = inputs_shapes[i]->BuildType(); (void)values_types.emplace(elementi, inputs_values[i]->BuildType()); - CheckAndConvertUtils::CheckTensorTypeValid("indices" + std::to_string(i), ind_type, {kInt64}, prim_name); - CheckAndConvertUtils::CheckTensorTypeValid("shapes" + std::to_string(i), sha_type, {kInt64, kInt32}, prim_name); + (void)CheckAndConvertUtils::CheckTensorTypeValid("indices" + std::to_string(i), ind_type, {kInt64}, prim_name); + (void)CheckAndConvertUtils::CheckTensorTypeValid("shapes" + std::to_string(i), sha_type, {kInt64, kInt32}, + prim_name); } (void)CheckAndConvertUtils::CheckTensorTypeSame(values_types, common_valid_types_with_complex_and_bool, prim_name); std::vector out_type = {}; @@ -139,7 +140,7 @@ std::vector SparseConcatInferShape(const PrimitivePtr &primi << indices_element0_shape[0] << ", but the value element number is " << values_element0_shape[0] << "."; } - primitive->AddAttr("N", MakeValue(SizeToLong(inputs_indices.size()))); + (void)primitive->AddAttr("N", MakeValue(SizeToLong(inputs_indices.size()))); std::vector out_indices_shape = {}; out_indices_shape.push_back(0); out_indices_shape.push_back(indices_element0_shape[1]);