clean code

This commit is contained in:
NBUFabio 2022-09-26 13:45:53 +08:00
parent 4df38be546
commit 43f5b0e929
9 changed files with 48 additions and 55 deletions

View File

@ -22,8 +22,8 @@ namespace mindspore {
namespace kernel {
namespace {
// preset size of paddings
constexpr int MAX_PADDINGS = 5;
constexpr int PADDING_SIZE = 2;
constexpr int64_t MAX_PADDINGS = 5;
constexpr int64_t PADDING_SIZE = 2;
constexpr size_t kTwo = 2;
constexpr size_t kInputNum = 2;
} // namespace
@ -104,10 +104,10 @@ bool MirrorPadCpuKernelMod::LaunchKernel(const std::vector<kernel::AddressPtr> &
const int64_t padd_dim = num_paddings_;
const int64_t mode = mode_;
const int64_t element_size = sizeof(T1);
const int64_t element_size = int64_t(sizeof(T1));
int64_t paddings[MAX_PADDINGS * PADDING_SIZE]; // local and fixed size to keep in registers
for (int i = 0; i < MAX_PADDINGS * PADDING_SIZE; i++) {
for (int64_t i = 0; i < MAX_PADDINGS * PADDING_SIZE; i++) {
paddings[i] = 0;
}
extract_paddings(paddings_arg, padd_dim, paddings);
@ -179,7 +179,7 @@ bool MirrorPadCpuKernelMod::LaunchKernel(const std::vector<kernel::AddressPtr> &
}
output_pos.clear();
output_pos.resize(tmp_pos.size());
std::copy(tmp_pos.begin(), tmp_pos.end(), output_pos.begin());
(void)std::copy(tmp_pos.begin(), tmp_pos.end(), output_pos.begin());
tmp_pos.clear();
}
return true;

View File

@ -66,7 +66,7 @@ void PadV3CpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
template <typename S>
bool PadV3CpuKernelMod::GetPaddings(const std::vector<AddressPtr> &inputs) {
auto paddings_arg = reinterpret_cast<S *>(inputs[1]->addr);
auto paddings_arg = static_cast<S *>(inputs[1]->addr);
paddings_ = std::vector<int64_t>(input_dim_ * kNum2, 0);
for (int64_t i = 0; i < paddings_num_; ++i) {
paddings_[i] = int64_t(*(paddings_arg + i));
@ -179,7 +179,7 @@ void PadV3CpuKernelMod::ConstantModeCompute(T *input_ptr, T *output_ptr, T const
}
template <typename T>
void PadV3CpuKernelMod::OtherModeCompute(T *input_ptr, T *output_ptr, int64_t p) {
void PadV3CpuKernelMod::OtherModeCompute(T *input_ptr, T *output_ptr, int64_t p) const {
if (paddings_num_ == kPadding1D) {
OtherModeCompute1D<T>(input_ptr, output_ptr, p);
} else if (paddings_num_ == kPadding2D) {
@ -190,7 +190,7 @@ void PadV3CpuKernelMod::OtherModeCompute(T *input_ptr, T *output_ptr, int64_t p)
}
template <typename T>
void PadV3CpuKernelMod::OtherModeCompute1D(T *input_ptr, T *output_ptr, int64_t p) {
void PadV3CpuKernelMod::OtherModeCompute1D(T *input_ptr, T *output_ptr, int64_t p) const {
int64_t nplane = 0;
int64_t input_w = input_shape_[kNum2];
int64_t output_w = output_shape_.end()[-1];
@ -206,7 +206,7 @@ void PadV3CpuKernelMod::OtherModeCompute1D(T *input_ptr, T *output_ptr, int64_t
}
template <typename T>
void PadV3CpuKernelMod::OtherModeCompute2D(T *input_ptr, T *output_ptr, int64_t p) {
void PadV3CpuKernelMod::OtherModeCompute2D(T *input_ptr, T *output_ptr, int64_t p) const {
int64_t pad_l = paddings_[0];
int64_t pad_t = paddings_[kNum2];
int64_t nplane = 0;
@ -230,7 +230,7 @@ void PadV3CpuKernelMod::OtherModeCompute2D(T *input_ptr, T *output_ptr, int64_t
}
template <typename T>
void PadV3CpuKernelMod::OtherModeCompute3D(T *input_ptr, T *output_ptr, int64_t p) {
void PadV3CpuKernelMod::OtherModeCompute3D(T *input_ptr, T *output_ptr, int64_t p) const {
int64_t pad_l = paddings_[0];
int64_t pad_t = paddings_[kNum2];
int64_t pad_f = paddings_[kNum4];
@ -264,7 +264,7 @@ void PadV3CpuKernelMod::OtherModeCompute3D(T *input_ptr, T *output_ptr, int64_t
}
int64_t PadV3CpuKernelMod::IndexCalculate(int64_t pad_value, int64_t now, int64_t input_value, int64_t o_start,
int64_t i_start) {
int64_t i_start) const {
int64_t ip = 0;
if (now < pad_value) {
if (mode_ == "reflect") {
@ -297,10 +297,10 @@ bool PadV3CpuKernelMod::LaunchKernel(const std::vector<AddressPtr> &inputs, cons
if (!GetPaddings<S>(inputs)) {
MS_LOG(ERROR) << "For '" << kernel_name_ << "', get paddings failed";
}
auto input_ptr = reinterpret_cast<T *>(inputs[0]->addr);
auto output_ptr = reinterpret_cast<T *>(outputs[0]->addr);
auto input_ptr = static_cast<T *>(inputs[0]->addr);
auto output_ptr = static_cast<T *>(outputs[0]->addr);
if (mode_ == "constant") {
T constant_values = *(reinterpret_cast<T *>(inputs[2]->addr));
T constant_values = *(static_cast<T *>(inputs[2]->addr));
for (int64_t i = 0; i < input_dim_ / kNum2; ++i) {
int64_t u = paddings_[i * kNum2];
int64_t v = paddings_[i * kNum2 + 1];

View File

@ -35,13 +35,12 @@ class PadV3CpuKernelMod : public DeprecatedNativeCpuKernelMod {
public:
PadV3CpuKernelMod() = default;
~PadV3CpuKernelMod() override = default;
void InitKernel(const CNodePtr &kernel_node) override;
bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
const std::vector<AddressPtr> &outputs) override {
return kernel_func_(this, inputs, workspace, outputs);
};
}
protected:
std::vector<KernelAttr> GetOpSupport() override;
@ -58,18 +57,18 @@ class PadV3CpuKernelMod : public DeprecatedNativeCpuKernelMod {
void ConstantModeCompute(T *input_ptr, T *output_ptr, T constant_values);
template <typename T>
void OtherModeCompute(T *input_ptr, T *output_ptr, int64_t p);
void OtherModeCompute(T *input_ptr, T *output_ptr, int64_t p) const;
template <typename T>
void OtherModeCompute1D(T *input_ptr, T *output_ptr, int64_t p);
void OtherModeCompute1D(T *input_ptr, T *output_ptr, int64_t p) const;
template <typename T>
void OtherModeCompute2D(T *input_ptr, T *output_ptr, int64_t p);
void OtherModeCompute2D(T *input_ptr, T *output_ptr, int64_t p) const;
template <typename T>
void OtherModeCompute3D(T *input_ptr, T *output_ptr, int64_t p);
void OtherModeCompute3D(T *input_ptr, T *output_ptr, int64_t p) const;
int64_t IndexCalculate(int64_t pad_value, int64_t now, int64_t input_value, int64_t o_start, int64_t i_start);
int64_t IndexCalculate(int64_t pad_value, int64_t now, int64_t input_value, int64_t o_start, int64_t i_start) const;
using SelectFunc =
std::function<bool(PadV3CpuKernelMod *, const std::vector<kernel::AddressPtr> &,

View File

@ -44,6 +44,7 @@ const std::vector<std::string> mode_list = {"reflect", "edge"};
void PadV3GradCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
MS_EXCEPTION_IF_NULL(kernel_node);
mode_ = common::AnfAlgo::GetNodeAttr<std::string>(kernel_node, "mode");
kernel_name_ = common::AnfAlgo::GetCNodeName(kernel_node);
const bool is_mode_available = std::find(mode_list.begin(), mode_list.end(), mode_) != mode_list.end();
@ -75,7 +76,7 @@ void PadV3GradCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
template <typename S>
bool PadV3GradCpuKernelMod::GetPaddings(const std::vector<AddressPtr> &inputs) {
// get paddings
auto paddings_arg = reinterpret_cast<S *>(inputs[1]->addr);
auto paddings_arg = static_cast<S *>(inputs[1]->addr);
if (paddings_num_ == 1) {
paddings_num_ = k2Num * (input_dim_ - k2Num);
for (int64_t i = 0; i < paddings_num_; ++i) {
@ -113,7 +114,7 @@ bool PadV3GradCpuKernelMod::GetPaddings(const std::vector<AddressPtr> &inputs) {
}
template <typename T>
void PadV3GradCpuKernelMod::PadV3GradCompute(T *input, T *output, int64_t p) {
void PadV3GradCpuKernelMod::PadV3GradCompute(T *input, T *output, int64_t p) const {
if (paddings_num_ == k1DNum) {
PadV3GradCompute1D<T>(input, output, p);
} else if (paddings_num_ == k2DNum) {
@ -128,7 +129,7 @@ void PadV3GradCpuKernelMod::PadV3GradCompute(T *input, T *output, int64_t p) {
}
template <typename T>
void PadV3GradCpuKernelMod::PadV3GradCompute1D(T *input, T *output, int64_t p) {
void PadV3GradCpuKernelMod::PadV3GradCompute1D(T *input, T *output, int64_t p) const {
for (int j = 0; j < input_w_; j++) {
auto ip_x = IndexCalculate(pad_l_, j, output_w_, o_start_x_, i_start_x_);
T *src_p = input + p * input_w_ + j;
@ -138,7 +139,7 @@ void PadV3GradCpuKernelMod::PadV3GradCompute1D(T *input, T *output, int64_t p) {
}
template <typename T>
void PadV3GradCpuKernelMod::PadV3GradCompute2D(T *input, T *output, int64_t p, int64_t i) {
void PadV3GradCpuKernelMod::PadV3GradCompute2D(T *input, T *output, int64_t p, int64_t i) const {
for (int j = 0; j < input_w_; j++) {
auto ip_x = IndexCalculate(pad_l_, j, output_w_, o_start_x_, i_start_x_);
auto ip_y = IndexCalculate(pad_t_, i, output_h_, o_start_y_, i_start_y_);
@ -149,7 +150,7 @@ void PadV3GradCpuKernelMod::PadV3GradCompute2D(T *input, T *output, int64_t p, i
}
template <typename T>
void PadV3GradCpuKernelMod::PadV3GradCompute3D(T *input, T *output, int64_t p, int64_t z) {
void PadV3GradCpuKernelMod::PadV3GradCompute3D(T *input, T *output, int64_t p, int64_t z) const {
for (int i = 0; i < input_h_; i++) {
for (int j = 0; j < input_w_; j++) {
auto ip_x = IndexCalculate(pad_l_, j, output_w_, o_start_x_, i_start_x_);
@ -164,7 +165,7 @@ void PadV3GradCpuKernelMod::PadV3GradCompute3D(T *input, T *output, int64_t p, i
}
int64_t PadV3GradCpuKernelMod::IndexCalculate(int64_t pad_value, int64_t now, int64_t output_value, int64_t o_start,
int64_t i_start) {
int64_t i_start) const {
int64_t ip = 0;
if (now < pad_value) {
if (mode_ == "reflect") {
@ -218,8 +219,8 @@ bool PadV3GradCpuKernelMod::LaunchKernel(const std::vector<AddressPtr> &inputs,
output_num_ *= output_shape_[i];
}
auto input = reinterpret_cast<T *>(inputs[0]->addr);
auto output = reinterpret_cast<T *>(outputs[0]->addr);
auto input = static_cast<T *>(inputs[0]->addr);
auto output = static_cast<T *>(outputs[0]->addr);
if (dtype_ == kNumberTypeComplex64 || dtype_ == kNumberTypeComplex128) {
for (size_t i = 0; i < LongToSize(output_num_); ++i) {

View File

@ -57,18 +57,18 @@ class PadV3GradCpuKernelMod : public DeprecatedNativeCpuKernelMod {
bool GetPaddings(const std::vector<AddressPtr> &inputs);
template <typename T>
void PadV3GradCompute(T *input, T *output, int64_t p);
void PadV3GradCompute(T *input, T *output, int64_t p) const;
template <typename T>
void PadV3GradCompute1D(T *input, T *output, int64_t p);
void PadV3GradCompute1D(T *input, T *output, int64_t p) const;
template <typename T>
void PadV3GradCompute2D(T *input, T *output, int64_t p, int64_t i);
void PadV3GradCompute2D(T *input, T *output, int64_t p, int64_t i) const;
template <typename T>
void PadV3GradCompute3D(T *input, T *output, int64_t p, int64_t z);
void PadV3GradCompute3D(T *input, T *output, int64_t p, int64_t z) const;
int64_t IndexCalculate(int64_t pad_value, int64_t now, int64_t output_value, int64_t o_start, int64_t i_start);
int64_t IndexCalculate(int64_t pad_value, int64_t now, int64_t output_value, int64_t o_start, int64_t i_start) const;
using SelectFunc =
std::function<bool(PadV3GradCpuKernelMod *, const std::vector<kernel::AddressPtr> &,

View File

@ -29,14 +29,6 @@ namespace {
constexpr size_t kTransposeInputsNum = 1;
constexpr size_t kDynamicPermInputNum = 2;
constexpr size_t kTransposeOutputsNum = 1;
constexpr size_t kIndex0 = 0;
constexpr size_t kIndex1 = 1;
constexpr size_t kIndex2 = 2;
constexpr size_t kIndex3 = 3;
constexpr size_t kIndex4 = 4;
constexpr size_t kIndex5 = 5;
constexpr size_t kIndex6 = 6;
constexpr size_t kIndex7 = 7;
using complex64 = std::complex<float>;
using complex128 = std::complex<double>;
// kMaxTransposeSerialSize = 64 * 3 * 512 * 512

View File

@ -86,8 +86,8 @@ abstract::ShapePtr MirrorPadGradInferShape(const PrimitivePtr &primitive,
for (size_t i = 0; i < paddings_arg.size(); i = i + kPaddingsSecondDim) {
paddings_attr.push_back(std::make_pair(paddings_arg[i], paddings_arg[i + 1]));
}
(void)CheckAndConvertUtils::CheckInteger("paddings_size", paddings_attr.size(), kEqual, x_shape.size(), prim_name);
size_t size = x_shape.size();
(void)CheckAndConvertUtils::CheckInteger("paddings_size", paddings_attr.size(), kEqual, SizeToLong(size), prim_name);
if (size > kMaxPaddings) {
MS_EXCEPTION(ValueError) << "For '" << prim_name
<< "', the dimension of input only supports less than or equal to 5 dims, but got " << size

View File

@ -25,13 +25,13 @@ namespace mindspore {
namespace ops {
namespace {
constexpr int64_t nTwo = 2;
constexpr size_t kPaddingsSizeTwo = 2;
constexpr size_t kPaddingsSizeFour = 4;
constexpr int64_t kPaddingsSizeTwo = 2;
constexpr int64_t kPaddingsSizeFour = 4;
void PaddingsSizeCheck(const PrimitivePtr &primitive, const int64_t paddings_size, const int64_t size) {
constexpr size_t kPaddingsSizeSix = 6;
constexpr size_t nThree = 3;
constexpr size_t nFour = 4;
constexpr size_t nFive = 5;
constexpr int64_t kPaddingsSizeSix = 6;
constexpr int64_t nThree = 3;
constexpr int64_t nFour = 4;
constexpr int64_t nFive = 5;
auto prim_name = primitive->name();
auto mode = GetValue<std::string>(primitive->GetAttr("mode"));
if (mode == kConstant) {
@ -62,9 +62,9 @@ void PaddingsSizeCheck(const PrimitivePtr &primitive, const int64_t paddings_siz
}
void ReflectModeCheck(std::string prim_name, const int64_t paddings_size, std::vector<int64_t> x_shape,
std::vector<int64_t> paddings_arg, const int64_t size) {
constexpr size_t kReflectMaxDims = 4;
constexpr size_t padding_pos_2 = 2;
constexpr size_t padding_pos_3 = 3;
constexpr int64_t kReflectMaxDims = 4;
constexpr int64_t padding_pos_2 = 2;
constexpr int64_t padding_pos_3 = 3;
(void)CheckAndConvertUtils::CheckInteger("input dims for reflect mode", size, kLessEqual, kReflectMaxDims, prim_name);
if (paddings_size == kPaddingsSizeTwo) {
if (paddings_arg[0] >= x_shape[kInputIndex2] || paddings_arg[1] >= x_shape[kInputIndex2]) {
@ -91,8 +91,8 @@ void ReflectModeCheck(std::string prim_name, const int64_t paddings_size, std::v
}
}
abstract::ShapePtr PadV3InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
constexpr size_t kEdgeMaxDims = 5;
constexpr size_t kOtherMinDims = 3;
constexpr int64_t kEdgeMaxDims = 5;
constexpr int64_t kOtherMinDims = 3;
MS_EXCEPTION_IF_NULL(primitive);
auto prim_name = primitive->name();
auto paddings = input_args[1]->BuildValue();

View File

@ -166,7 +166,8 @@ AbstractBasePtr UnsortedSegmentArithmeticInfer(const abstract::AnalysisEnginePtr
for (const auto &item : input_args) {
MS_EXCEPTION_IF_NULL(item);
}
(void)CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, kInputIndex3, primitive->name());
const int64_t input_num = 3;
CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, primitive->name());
auto infer_type = UnsortedSegmentArithmeticInferType(primitive, input_args);
auto infer_shape = UnsortedSegmentArithmeticInferShape(primitive, input_args);
return abstract::MakeAbstract(infer_shape, infer_type);