forked from mindspore-Ecosystem/mindspore
!24524 Added GetReductionInt() for loss with reduction gpu kernels
Merge pull request !24524 from markuskunej/reduction_utility_method
This commit is contained in:
commit
b414db49b6
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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_;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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_;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue