!24524 Added GetReductionInt() for loss with reduction gpu kernels

Merge pull request !24524 from markuskunej/reduction_utility_method
This commit is contained in:
i-robot 2021-10-06 15:54:31 +00:00 committed by Gitee
commit b414db49b6
8 changed files with 27 additions and 41 deletions

View File

@ -571,6 +571,17 @@ int Sign(float x) {
return 0;
}
int GetReductionInt(const std::string &reduction) {
if (reduction == "none") {
return 0;
} else if (reduction == "sum") {
return 2;
} else {
// reduction = 'mean'
return 1;
}
}
std::pair<AnfNodePtr, size_t> GetKernelInput(const AnfNodePtr &anf_node, size_t index) {
MS_EXCEPTION_IF_NULL(anf_node);

View File

@ -89,6 +89,7 @@ std::string GetProcessor(const AnfNodePtr &anf_node);
Processor GetProcessor(const string &processor);
bool IsSameShape(const std::vector<size_t> &shape_a, const std::vector<size_t> &shape_b);
int Sign(float x);
int GetReductionInt(const std::string &reduction);
std::pair<AnfNodePtr, size_t> GetKernelInput(const AnfNodePtr &anf_node, size_t index);
std::vector<std::pair<AnfNodePtr, std::pair<size_t, size_t>>> GetInputIndex(const std::vector<AnfNodePtr> &node_list,
const std::vector<AnfNodePtr> &input_list);

View File

@ -22,6 +22,7 @@
#include "backend/kernel_compiler/gpu/gpu_kernel.h"
#include "backend/kernel_compiler/gpu/gpu_kernel_factory.h"
#include "backend/kernel_compiler/gpu/cuda_impl/loss_with_reduction_impl.cuh"
#include "backend/kernel_compiler/common_utils.h"
namespace mindspore {
namespace kernel {
@ -67,11 +68,7 @@ class BinaryCrossEntropyGpuKernel : public GpuKernel {
input_size_ *= input_shape[i];
}
string reduction = GetAttr<string>(kernel_node, "reduction");
if (reduction == "none") {
reduction_ = 0;
} else if (reduction == "sum") {
reduction_ = 2;
}
reduction_ = GetReductionInt(reduction);
workspace_size_ = sizeof(T);
if (reduction_ != 0) {
workspace_size_ *= input_size_;

View File

@ -22,6 +22,7 @@
#include "backend/kernel_compiler/gpu/gpu_kernel.h"
#include "backend/kernel_compiler/gpu/gpu_kernel_factory.h"
#include "backend/kernel_compiler/gpu/cuda_impl/loss_with_reduction_impl.cuh"
#include "backend/kernel_compiler/common_utils.h"
namespace mindspore {
namespace kernel {
@ -69,11 +70,7 @@ class BinaryCrossEntropyGradGpuKernel : public GpuKernel {
input_size_ *= input_shape[i];
}
string reduction = GetAttr<string>(kernel_node, "reduction");
if (reduction == "none") {
reduction_ = 0;
} else if (reduction == "sum") {
reduction_ = 2;
}
reduction_ = GetReductionInt(reduction);
InitSizeLists();
return true;
}

View File

@ -22,6 +22,7 @@
#include "backend/kernel_compiler/gpu/gpu_kernel.h"
#include "backend/kernel_compiler/gpu/gpu_kernel_factory.h"
#include "backend/kernel_compiler/gpu/cuda_impl/loss_with_reduction_impl.cuh"
#include "backend/kernel_compiler/common_utils.h"
namespace mindspore {
namespace kernel {
@ -60,11 +61,7 @@ class KLDivLossGpuKernel : public GpuKernel {
input_size_ *= input_shape[i];
}
string reduction = GetAttr<string>(kernel_node, "reduction");
if (reduction == "none") {
reduction_ = 0;
} else if (reduction == "sum") {
reduction_ = 2;
}
reduction_ = GetReductionInt(reduction);
workspace_size_ = sizeof(T);
if (reduction_ == 0) {
workspace_size_ *= input_size_;

View File

@ -22,6 +22,7 @@
#include "backend/kernel_compiler/gpu/gpu_kernel.h"
#include "backend/kernel_compiler/gpu/gpu_kernel_factory.h"
#include "backend/kernel_compiler/gpu/cuda_impl/loss_with_reduction_impl.cuh"
#include "backend/kernel_compiler/common_utils.h"
namespace mindspore {
namespace kernel {
@ -61,11 +62,7 @@ class KLDivLossGradGpuKernel : public GpuKernel {
input_size_ *= input_shape[i];
}
string reduction = GetAttr<string>(kernel_node, "reduction");
if (reduction == "none") {
reduction_ = 0;
} else if (reduction == "sum") {
reduction_ = 2;
}
reduction_ = GetReductionInt(reduction);
InitSizeLists();
return true;
}

View File

@ -22,6 +22,7 @@
#include "backend/kernel_compiler/gpu/gpu_kernel.h"
#include "backend/kernel_compiler/gpu/gpu_kernel_factory.h"
#include "backend/kernel_compiler/gpu/cuda_impl/loss_with_reduction_impl.cuh"
#include "backend/kernel_compiler/common_utils.h"
namespace mindspore {
namespace kernel {
@ -60,19 +61,10 @@ class NLLLossGpuKernel : public GpuKernel {
input_size_ *= input_shape[i];
}
string reduction = GetAttr<string>(kernel_node, "reduction");
// if reduction is not 'none', tmp_nll is (N,) size
if (reduction == "none") {
reduction_ = 0;
} else if (reduction == "sum") {
reduction_ = 2;
tmp_loss_size_ = sizeof(T) * n_;
} else {
// reduction = 'mean'
reduction_ = 1;
reduction_ = GetReductionInt(reduction);
if ((reduction_ == 2) || (reduction_ == 1)) {
tmp_loss_size_ = sizeof(T) * n_;
}
tmp_target_weight_size_ = n_ * sizeof(S);
InitSizeLists();

View File

@ -22,6 +22,7 @@
#include "backend/kernel_compiler/gpu/gpu_kernel.h"
#include "backend/kernel_compiler/gpu/gpu_kernel_factory.h"
#include "backend/kernel_compiler/gpu/cuda_impl/loss_with_reduction_impl.cuh"
#include "backend/kernel_compiler/common_utils.h"
namespace mindspore {
namespace kernel {
@ -59,16 +60,9 @@ class NLLLossGradGpuKernel : public GpuKernel {
input_size_ *= input_shape[i];
}
string reduction = GetAttr<string>(kernel_node, "reduction");
// if reduction is not 'none', tmp_nll is (N,) size
if (reduction == "none") {
reduction_ = 0;
num_dloss_ = n_; // dloss is a vector
} else if (reduction == "sum") {
reduction_ = 2;
} else {
// reduction = 'mean'
reduction_ = 1;
reduction_ = GetReductionInt(reduction);
if (reduction_ == 0) {
num_dloss_ = n_;
}
InitSizeLists();