forked from mindspore-Ecosystem/mindspore
[MSLITE] code check
This commit is contained in:
parent
111d1a9a61
commit
c6eb64f9f6
|
@ -38,6 +38,7 @@ constexpr static int kOutCurrentVarIdx = 4;
|
|||
void FusedBatchnormFp16CPUKernel::CalcMeanVar(float16_t *in, float16_t *scale, float16_t *offset, float16_t *save_mean,
|
||||
float16_t *save_variance) {
|
||||
auto param = reinterpret_cast<BatchNormParameter *>(op_parameter_);
|
||||
MS_ASSERT(param != nullptr);
|
||||
float16_t *current_mean = static_cast<float16_t *>(mean_);
|
||||
float16_t *current_var = static_cast<float16_t *>(variance_);
|
||||
|
||||
|
@ -45,21 +46,25 @@ void FusedBatchnormFp16CPUKernel::CalcMeanVar(float16_t *in, float16_t *scale, f
|
|||
std::fill(current_var, current_var + in_tensors_.at(kInCurrentVarIdx)->ElementsNum(), 0.f);
|
||||
FusedBatchNormFp16MeanVar(in, current_mean, current_var, param, save_mean, save_variance);
|
||||
|
||||
MS_ASSERT(out_tensors_.at(kOutScaleIdx)->data_c() != nullptr);
|
||||
MS_ASSERT(out_tensors_.at(kOutOffsetIdx)->data_c() != nullptr);
|
||||
MS_ASSERT(out_tensors_.at(kOutCurrentMeanIdx)->data_c() != nullptr);
|
||||
MS_ASSERT(out_tensors_.at(kOutCurrentVarIdx)->data_c() != nullptr);
|
||||
memcpy(out_tensors_.at(kOutScaleIdx)->data_c(), scale, out_tensors_.at(kOutScaleIdx)->Size());
|
||||
memcpy(out_tensors_.at(kOutOffsetIdx)->data_c(), offset, out_tensors_.at(kOutOffsetIdx)->Size());
|
||||
memcpy(out_tensors_.at(kOutCurrentMeanIdx)->data_c(), current_mean, out_tensors_.at(kOutCurrentMeanIdx)->Size());
|
||||
memcpy(out_tensors_.at(kOutCurrentVarIdx)->data_c(), current_var, out_tensors_.at(kOutCurrentVarIdx)->Size());
|
||||
|
||||
// Copy to local variables
|
||||
memcpy(scale_, scale, in_tensors_[kInScaleIdx]->Size());
|
||||
memcpy(offset_, offset, in_tensors_[kInOffsetIdx]->Size());
|
||||
memcpy(scale_, scale, in_tensors_.at(kInScaleIdx)->Size());
|
||||
memcpy(offset_, offset, in_tensors_.at(kInOffsetIdx)->Size());
|
||||
|
||||
trained_ = true; // trained at least once
|
||||
}
|
||||
|
||||
int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) {
|
||||
auto param = reinterpret_cast<BatchNormParameter *>(op_parameter_);
|
||||
MS_ASSERT(param);
|
||||
MS_ASSERT(param != nullptr);
|
||||
if (in_tensors_.at(0)->data_type() == kNumberTypeFloat32) {
|
||||
MS_ASSERT(in_tensors_.size() == kMaxInIdx);
|
||||
MS_ASSERT(out_tensors_.size() == 1);
|
||||
|
@ -86,11 +91,11 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) {
|
|||
ms_context_->allocator->Free(output_fp16);
|
||||
return RET_ERROR;
|
||||
}
|
||||
MS_ASSERT(input->data_c() != nullptr);
|
||||
MS_ASSERT(scale->data_c() != nullptr);
|
||||
MS_ASSERT(offset->data_c() != nullptr);
|
||||
MS_ASSERT(mean->data_c() != nullptr);
|
||||
MS_ASSERT(variance->data_c() != nullptr);
|
||||
CHECK_NULL_RETURN(input->data_c());
|
||||
CHECK_NULL_RETURN(scale->data_c());
|
||||
CHECK_NULL_RETURN(offset->data_c());
|
||||
CHECK_NULL_RETURN(mean->data_c());
|
||||
CHECK_NULL_RETURN(variance->data_c());
|
||||
Float32ToFloat16(reinterpret_cast<float *>(input->data_c()), reinterpret_cast<float16_t *>(input_fp16),
|
||||
input->ElementsNum());
|
||||
Float32ToFloat16(reinterpret_cast<float *>(scale->data_c()), reinterpret_cast<float16_t *>(scale_fp16),
|
||||
|
@ -121,8 +126,8 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) {
|
|||
ms_context_->allocator->Free(output_fp16);
|
||||
return RET_OK;
|
||||
}
|
||||
MS_ASSERT(in_tensors_.at(0)->data_c() != nullptr);
|
||||
MS_ASSERT(out_tensors_.at(0)->data_c() != nullptr);
|
||||
CHECK_NULL_RETURN(in_tensors_.at(0)->data_c());
|
||||
CHECK_NULL_RETURN(out_tensors_.at(0)->data_c());
|
||||
if (IsTrain() && IsTrainable() && in_tensors_.size() >= kMaxInIdx) {
|
||||
CalcMeanVar(static_cast<float16_t *>(in_tensors_.at(0)->data_c()),
|
||||
static_cast<float16_t *>(in_tensors_.at(kInScaleIdx)->data_c()),
|
||||
|
@ -142,6 +147,10 @@ int FusedBatchnormFp16CPUKernel::Eval() {
|
|||
float16_t *save_var = static_cast<float16_t *>(in_tensors_.at(kInCurrentVarIdx)->data_c());
|
||||
float16_t *scale = static_cast<float16_t *>(in_tensors_.at(kInScaleIdx)->data_c());
|
||||
float16_t *bias = static_cast<float16_t *>(in_tensors_.at(kInOffsetIdx)->data_c());
|
||||
CHECK_NULL_RETURN(save_mean);
|
||||
CHECK_NULL_RETURN(save_var);
|
||||
CHECK_NULL_RETURN(scale);
|
||||
CHECK_NULL_RETURN(bias);
|
||||
|
||||
// Copy to local variables
|
||||
memcpy(scale_, scale, in_tensors_.at(kInScaleIdx)->Size());
|
||||
|
|
|
@ -62,37 +62,51 @@ int FusedBatchnormCPUKernel::InitConstTensor() {
|
|||
MS_LOG(ERROR) << "Memory allocation failed";
|
||||
return RET_ERROR;
|
||||
}
|
||||
memcpy(scale_, scale->MutableData(), scale->Size());
|
||||
memcpy(offset_, offset->MutableData(), offset->Size());
|
||||
memcpy(mean_, mean->MutableData(), mean->Size());
|
||||
memcpy(variance_, variance->MutableData(), variance->Size());
|
||||
|
||||
MS_ASSERT(scale->data_c() != nullptr);
|
||||
MS_ASSERT(offset->data_c() != nullptr);
|
||||
MS_ASSERT(mean->data_c() != nullptr);
|
||||
MS_ASSERT(variance->data_c() != nullptr);
|
||||
memcpy(scale_, scale->data_c(), scale->Size());
|
||||
memcpy(offset_, offset->data_c(), offset->Size());
|
||||
memcpy(mean_, mean->data_c(), mean->Size());
|
||||
memcpy(variance_, variance->data_c(), variance->Size());
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int FusedBatchnormCPUKernel::Run() {
|
||||
auto param = reinterpret_cast<BatchNormParameter *>(op_parameter_);
|
||||
MS_ASSERT(param != nullptr);
|
||||
if (IsTrain() && IsTrainable() && in_tensors_.size() >= kNumInputSize) {
|
||||
float *in = static_cast<float *>(in_tensors_[0]->MutableData());
|
||||
float *scale = static_cast<float *>(in_tensors_[1]->MutableData());
|
||||
float *offset = static_cast<float *>(in_tensors_[2]->MutableData());
|
||||
float *in = static_cast<float *>(in_tensors_.at(0)->data_c());
|
||||
float *scale = static_cast<float *>(in_tensors_.at(1)->data_c());
|
||||
float *offset = static_cast<float *>(in_tensors_.at(2)->data_c());
|
||||
float *current_mean = static_cast<float *>(mean_);
|
||||
float *current_var = static_cast<float *>(variance_);
|
||||
float *save_mean = static_cast<float *>(in_tensors_[3]->MutableData());
|
||||
float *save_variance = static_cast<float *>(in_tensors_[4]->MutableData());
|
||||
|
||||
std::fill(current_mean, current_mean + in_tensors_[3]->ElementsNum(), 0.f);
|
||||
std::fill(current_var, current_var + in_tensors_[4]->ElementsNum(), 0.f);
|
||||
float *save_mean = static_cast<float *>(in_tensors_.at(3)->data_c());
|
||||
float *save_variance = static_cast<float *>(in_tensors_.at(4)->data_c());
|
||||
if (in == nullptr || scale == nullptr || offset == nullptr || current_mean == nullptr || current_var == nullptr ||
|
||||
save_mean == nullptr || save_variance == nullptr) {
|
||||
MS_LOG(ERROR) << "The input data is nullptr.";
|
||||
return RET_ERROR;
|
||||
}
|
||||
std::fill(current_mean, current_mean + in_tensors_.at(3)->ElementsNum(), 0.f);
|
||||
std::fill(current_var, current_var + in_tensors_.at(4)->ElementsNum(), 0.f);
|
||||
FusedBatchNormFp32MeanVar(in, current_mean, current_var, param, static_cast<float *>(save_mean),
|
||||
static_cast<float *>(save_variance));
|
||||
|
||||
memcpy(out_tensors_.at(1)->MutableData(), scale, out_tensors_.at(1)->Size());
|
||||
memcpy(out_tensors_.at(2)->MutableData(), offset, out_tensors_.at(2)->Size());
|
||||
memcpy(out_tensors_.at(3)->MutableData(), current_mean, out_tensors_.at(3)->Size());
|
||||
memcpy(out_tensors_.at(4)->MutableData(), current_var, out_tensors_.at(4)->Size());
|
||||
CHECK_NULL_RETURN(out_tensors_.at(1)->data_c());
|
||||
CHECK_NULL_RETURN(out_tensors_.at(2)->data_c());
|
||||
CHECK_NULL_RETURN(out_tensors_.at(3)->data_c());
|
||||
CHECK_NULL_RETURN(out_tensors_.at(4)->data_c());
|
||||
memcpy(out_tensors_.at(1)->data_c(), scale, out_tensors_.at(1)->Size());
|
||||
memcpy(out_tensors_.at(2)->data_c(), offset, out_tensors_.at(2)->Size());
|
||||
memcpy(out_tensors_.at(3)->data_c(), current_mean, out_tensors_.at(3)->Size());
|
||||
memcpy(out_tensors_.at(4)->data_c(), current_var, out_tensors_.at(4)->Size());
|
||||
|
||||
// Copy to local variables
|
||||
memcpy(scale_, scale, in_tensors_[1]->Size());
|
||||
memcpy(offset_, offset, in_tensors_[2]->Size());
|
||||
memcpy(scale_, scale, in_tensors_.at(1)->Size());
|
||||
memcpy(offset_, offset, in_tensors_.at(2)->Size());
|
||||
|
||||
trained_ = true; // trained at least once
|
||||
}
|
||||
|
@ -106,10 +120,14 @@ int FusedBatchnormCPUKernel::Run() {
|
|||
int FusedBatchnormCPUKernel::Eval() {
|
||||
InnerKernel::Eval();
|
||||
if (trained_) {
|
||||
float *save_mean = static_cast<float *>(in_tensors_.at(3)->MutableData());
|
||||
float *save_var = static_cast<float *>(in_tensors_.at(4)->MutableData());
|
||||
float *scale = static_cast<float *>(in_tensors_.at(1)->MutableData());
|
||||
float *bias = static_cast<float *>(in_tensors_.at(2)->MutableData());
|
||||
float *save_mean = static_cast<float *>(in_tensors_.at(3)->data_c());
|
||||
float *save_var = static_cast<float *>(in_tensors_.at(4)->data_c());
|
||||
float *scale = static_cast<float *>(in_tensors_.at(1)->data_c());
|
||||
float *bias = static_cast<float *>(in_tensors_.at(2)->data_c());
|
||||
CHECK_NULL_RETURN(save_mean);
|
||||
CHECK_NULL_RETURN(save_var);
|
||||
CHECK_NULL_RETURN(scale);
|
||||
CHECK_NULL_RETURN(bias);
|
||||
|
||||
// Copy to local variables
|
||||
memcpy(scale_, scale, in_tensors_.at(1)->Size());
|
||||
|
@ -122,8 +140,11 @@ int FusedBatchnormCPUKernel::Eval() {
|
|||
|
||||
int FusedBatchnormCPUKernel::DoExecute(int task_id) {
|
||||
auto param = reinterpret_cast<BatchNormParameter *>(op_parameter_);
|
||||
FusedBatchNormFp32(in_tensors_.at(0)->MutableData(), scale_, offset_, mean_, variance_, param, task_id,
|
||||
out_tensors_.at(0)->MutableData());
|
||||
auto in_data = in_tensors_.at(0)->data_c();
|
||||
auto out_data = out_tensors_.at(0)->data_c();
|
||||
CHECK_NULL_RETURN(in_data);
|
||||
CHECK_NULL_RETURN(out_data);
|
||||
FusedBatchNormFp32(in_data, scale_, offset_, mean_, variance_, param, task_id, out_data);
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue