clean code warning

This commit is contained in:
fandawei 2022-08-02 21:49:19 +08:00
parent 8db46cbd17
commit 5d5a8a36b5
9 changed files with 43 additions and 26 deletions

View File

@ -19,6 +19,7 @@
#include <limits>
#include <utility>
#include <vector>
#include <cmath>
#include "plugin/device/cpu/hal/device/cpu_device_address.h"
#include "plugin/device/cpu/kernel/arithmetic_cpu_kernel.h"
@ -160,12 +161,22 @@ void AddcdivCpuKernelMod::AddcdivAdd(const T *input1, const T *input2, T *output
}
}
template <typename T>
T abs(T num) {
if (num >= static_cast<T>(0.0)) {
return num;
} else {
return -num;
}
}
template <typename T>
void AddcdivCpuKernelMod::AddcdivDiv(const T *input1, const T *input2, T *output) {
if (inputx_shape_size_ == 0 && inputy_shape_size_ == 0) {
auto zero = (T)0;
if (input2[0] == zero) {
if (input1[0] == zero) {
const auto eps_if_zero = static_cast<T>(1e-6);
auto zero = static_cast<T>(0);
if (abs(input2[0] - zero) <= eps_if_zero) {
if (abs(input1[0] - zero) <= eps_if_zero) {
output[0] = std::numeric_limits<T>::quiet_NaN();
return;
}
@ -178,14 +189,15 @@ void AddcdivCpuKernelMod::AddcdivDiv(const T *input1, const T *input2, T *output
} else {
BroadcastIterator div_iter(output_shape_, input_shape2_, output_shape_);
auto div_task = [&input1, &input2, &output, &div_iter](int64_t div_start, int64_t div_end) {
const auto eps_if_zero = static_cast<T>(1e-6);
auto iter = div_iter;
iter.SetPos(div_start);
for (int64_t i = div_start; i < div_end; i++) {
auto zero = (T)0;
auto zero = static_cast<T>(0);
auto addcdiv_dividend = input1[iter.GetInputPosA()];
auto addcdiv_divisor = input2[iter.GetInputPosB()];
if (addcdiv_divisor == zero) {
if (addcdiv_dividend == zero) {
if (abs(addcdiv_divisor - zero) <= eps_if_zero) {
if (abs(addcdiv_dividend - zero) <= eps_if_zero) {
output[i] = std::numeric_limits<T>::quiet_NaN();
continue;
}
@ -210,7 +222,7 @@ void AddcdivCpuKernelMod::AddcdivDiv(const T *input1, const T *input2, T *output
}
std::vector<KernelAttr> AddcdivCpuKernelMod::GetOpSupport() {
static std::vector<KernelAttr> kernel_attr_list = {
static const std::vector<KernelAttr> kernel_attr_list = {
ADD_KERNEL(Float32, Float32, Float32, Float16, Float32), ADD_KERNEL(Float32, Float32, Float32, Float32, Float32),
ADD_KERNEL(Float32, Float32, Float32, Float64, Float32), ADD_KERNEL(Float32, Float32, Float32, Int32, Float32),
ADD_KERNEL(Float32, Float32, Float32, Int64, Float32), ADD_KERNEL(Float64, Float64, Float64, Float16, Float64),

View File

@ -171,7 +171,7 @@ bool AddcmulCpuKernelMod::Launch(const std::vector<AddressPtr> &inputs, const st
}
std::vector<KernelAttr> AddcmulCpuKernelMod::GetOpSupport() {
static std::vector<KernelAttr> kernel_attr_list = {
static const std::vector<KernelAttr> kernel_attr_list = {
KernelAttr().AddInputAttr(F32).AddInputAttr(F32).AddInputAttr(F32).AddInputAttr(F16).AddOutputAttr(F32),
KernelAttr().AddInputAttr(F32).AddInputAttr(F32).AddInputAttr(F32).AddInputAttr(F32).AddOutputAttr(F32),
KernelAttr().AddInputAttr(F32).AddInputAttr(F32).AddInputAttr(F32).AddInputAttr(I8).AddOutputAttr(F32),

View File

@ -30,7 +30,7 @@ const int64_t kAdjustContrastv2ParallelNum = 64 * 1024;
} // namespace
template <typename T>
void AdjustContrastv2(T *image, T *image_out, std::float_t contrast_factor, std::int64_t channel_count,
void AdjustContrastv2(const T *image, T *image_out, std::float_t contrast_factor, std::int64_t channel_count,
std::int64_t per_batch_elements) {
if (channel_count == 0) {
return;
@ -108,7 +108,7 @@ bool AdjustContrastv2CpuKernelMod::Launch(const std::vector<kernel::AddressPtr>
}
std::vector<KernelAttr> AdjustContrastv2CpuKernelMod::GetOpSupport() {
static std::vector<KernelAttr> support_list = {
static const std::vector<KernelAttr> support_list = {
KernelAttr().AddInputAttr(kNumberTypeFloat32).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32)};
return support_list;
}

View File

@ -17,6 +17,7 @@
#include "plugin/device/cpu/kernel/adjust_hue_cpu_kernel.h"
#include <Eigen/Dense>
#include <algorithm>
#include <cmath>
#include "plugin/device/cpu/hal/device/cpu_device_address.h"
#include "utils/ms_utils.h"
@ -38,6 +39,7 @@ namespace detail {
static void rgb_to_hv_range(float r, float g, float b, float *h, float *v_min, float *v_max) {
float v_mid;
int h_category;
const float eps = 1e-6;
// According to the figures in:
// https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma
// For the conditions, we don't care about the case where two components are
@ -84,7 +86,7 @@ static void rgb_to_hv_range(float r, float g, float b, float *h, float *v_min, f
h_category = kAdjustHueFive;
}
}
if (*v_max == *v_min) {
if (std::fabs(*v_max - *v_min) <= eps) {
*h = 0;
return;
}
@ -144,14 +146,15 @@ HsvTuple rgb2hsv(const float r, const float g, const float b) {
const float M = fmaxf(r, fmaxf(g, b));
const float m = fminf(r, fminf(g, b));
const float chroma = M - m;
const float eps = 1e-6;
float h = 0.0f, s = 0.0f;
// hue
if (chroma > 0.0f) {
if (M == r) {
if (std::fabs(M - r) <= eps) {
const float num = (g - b) / chroma;
const float sign = copysignf(1.0f, num);
h = (static_cast<float>(sign < 0.0f) * 6.0f + sign * fmodf(sign * num, 6.0f)) / 6.0f;
} else if (M == g) {
} else if (std::fabs(M - g) <= eps) {
h = ((b - r) / chroma + 2.0f) / 6.0f;
} else {
h = ((r - g) / chroma + 4.0f) / 6.0f;
@ -307,7 +310,7 @@ bool AdjustHueCpuKernelMod::Launch(const std::vector<kernel::AddressPtr> &inputs
}
std::vector<KernelAttr> AdjustHueCpuKernelMod::GetOpSupport() {
static std::vector<KernelAttr> support_list = {
static const std::vector<KernelAttr> support_list = {
KernelAttr().AddInputAttr(kNumberTypeFloat32).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32)};
return support_list;
}

View File

@ -18,6 +18,7 @@
#include <Eigen/Dense>
#include <algorithm>
#include <iostream>
#include <cmath>
#include "plugin/device/cpu/hal/device/cpu_device_address.h"
#include "utils/ms_utils.h"
@ -38,6 +39,7 @@ namespace detail {
static void rgb_to_hsv(float r, float g, float b, float *h, float *s, float *v) {
float vv = std::max(r, std::max(g, b));
float range = vv - std::min(r, std::min(g, b));
const float eps = 1e-6;
if (vv > 0) {
*s = range / vv;
} else {
@ -45,9 +47,9 @@ static void rgb_to_hsv(float r, float g, float b, float *h, float *s, float *v)
}
float norm = kAdjustSaturationOne / (kAdjustSaturationSix * range);
float hh;
if (r == vv) {
if (std::fabs(r - vv) <= eps) {
hh = norm * (g - b);
} else if (g == vv) {
} else if (std::fabs(g - vv) <= eps) {
hh = norm * (b - r) + kAdjustSaturationTwo / kAdjustSaturationSix;
} else {
hh = norm * (r - g) + kAdjustSaturationFour / kAdjustSaturationSix;
@ -172,7 +174,7 @@ bool AdjustSaturationCpuKernelMod::Launch(const std::vector<kernel::AddressPtr>
return true;
}
std::vector<KernelAttr> AdjustSaturationCpuKernelMod::GetOpSupport() {
static std::vector<KernelAttr> support_list = {
static const std::vector<KernelAttr> support_list = {
KernelAttr().AddInputAttr(kNumberTypeFloat32).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32)};
return support_list;
}

View File

@ -73,7 +73,7 @@ bool AngleCpuKernelMod::LaunchKernel(const std::vector<kernel::AddressPtr> &inpu
}
std::vector<KernelAttr> AngleCpuKernelMod::GetOpSupport() {
static std::vector<KernelAttr> support_list = {
static const std::vector<KernelAttr> support_list = {
KernelAttr().AddInputAttr(kNumberTypeComplex64).AddOutputAttr(kNumberTypeFloat32),
KernelAttr().AddInputAttr(kNumberTypeComplex128).AddOutputAttr(kNumberTypeFloat64)};

View File

@ -120,7 +120,7 @@ bool ApplyAdagradDACpuKernelMod::Launch(const std::vector<AddressPtr> &inputs, c
}
void ApplyAdagradDACpuKernelMod::CheckShapeAndDtypeEqual(int64_t size_a, int64_t size_b, const char *name_a,
const char *name_b) {
const char *name_b) const {
if (size_a != size_b) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', the shape and dtype of '" << name_a << "' and '" << name_b
<< "' must be the same, "

View File

@ -47,7 +47,7 @@ class ApplyAdagradDACpuKernelMod : public NativeCpuKernelMod {
private:
void CheckParam(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs);
void CheckShapeAndDtypeEqual(int64_t size_a, int64_t size_b, const char *name_a, const char *name_b);
void CheckShapeAndDtypeEqual(int64_t size_a, int64_t size_b, const char *name_a, const char *name_b) const;
void CheckDType(const std::vector<KernelTensorPtr> &inputs) const;

View File

@ -84,7 +84,7 @@ template <typename T>
void ElementRealDivComplex(const T *input1, const T *input2, T *out, size_t size, size_t delta_1, size_t delta_2) {
size_t idx_1 = 0;
size_t idx_2 = 0;
auto zero = (T)0;
auto zero = static_cast<T>(0);
for (size_t i = 0; i < size; ++i) {
auto dividend = input1[idx_1];
auto divisor = input2[idx_2];
@ -397,7 +397,7 @@ void ArithmeticCpuTypeFunc<T>::RealDiv(const T *input1, const T *input2, T *out)
auto dividend = input1[iter.GetInputPosA()];
auto divisor = input2[iter.GetInputPosB()];
iter.GenNextPos();
auto zero = (T)0;
auto zero = static_cast<T>(0);
if (divisor == zero) {
if (dividend == zero) {
out[i] = std::numeric_limits<T>::quiet_NaN();
@ -448,7 +448,7 @@ void ArithmeticCpuTypeFunc<T>::RealDivComplex(const T *input1, const T *input2,
auto dividend = input1[iter.GetInputPosA()];
auto divisor = input2[iter.GetInputPosB()];
iter.GenNextPos();
auto zero = (T)0;
auto zero = static_cast<T>(0);
if (divisor == zero) {
out[i] = std::numeric_limits<T>::quiet_NaN();
continue;
@ -469,7 +469,7 @@ void ArithmeticCpuTypeFunc<T>::Div(const T *input1, const T *input2, T *out) {
auto dividend = input1[iter.GetInputPosA()];
auto divisor = input2[iter.GetInputPosB()];
iter.GenNextPos();
auto zero = (T)0;
auto zero = static_cast<T>(0);
if (divisor == zero) {
if (dividend == zero) {
out[i] = std::numeric_limits<T>::quiet_NaN();
@ -498,7 +498,7 @@ void ArithmeticCpuTypeFunc<T>::DivComplex(const T *input1, const T *input2, T *o
auto dividend = input1[iter.GetInputPosA()];
auto divisor = input2[iter.GetInputPosB()];
iter.GenNextPos();
auto zero = (T)0;
auto zero = static_cast<T>(0);
if (divisor == zero) {
if (dividend == zero) {
out[i] = std::numeric_limits<T>::quiet_NaN();
@ -522,7 +522,7 @@ void ArithmeticCpuTypeFunc<T>::DivNoNan(const T *input1, const T *input2, T *out
auto dividend = input1[iter.GetInputPosA()];
auto divisor = input2[iter.GetInputPosB()];
iter.GenNextPos();
auto zero = (T)0;
auto zero = static_cast<T>(0);
if (divisor == zero) {
out[i] = zero;
continue;