From 46dec604245b63dee2eb52d9b10428edab6c6434 Mon Sep 17 00:00:00 2001 From: Emir Haleva Date: Thu, 22 Jul 2021 17:37:47 +0300 Subject: [PATCH] fixed codex/coverity warnings --- .../models/densenet_train_export.py | 4 +- .../export_models/models/emoji_model.py | 2 + .../export_models/models/train_utils.py | 61 +++++------- .../examples/export_models/models_train.cfg | 1 - .../examples/train_lenet/src/net_runner.cc | 6 +- .../train_lenet_java/model/lenet_export.py | 4 +- .../train_lenet_java/model/train_utils.py | 5 +- .../transfer_learning/src/net_runner.cc | 2 +- .../examples/unified_api/src/net_runner.cc | 6 +- mindspore/lite/src/common/quant_utils.cc | 2 - .../lite/src/cxx_api/callback/ckpt_saver.cc | 2 - .../lite/src/cxx_api/callback/loss_monitor.cc | 2 - .../lite/src/cxx_api/callback/lr_scheduler.cc | 2 - .../src/cxx_api/callback/train_accuracy.cc | 2 - mindspore/lite/src/cxx_api/converters.cc | 6 +- .../lite/src/cxx_api/metrics/accuracy.cc | 2 - .../lite/src/cxx_api/model/model_impl.cc | 1 - .../lite/src/cxx_api/train/converters.cc | 2 - .../lite/src/cxx_api/train/model_impl.cc | 55 ----------- .../lite/src/cxx_api/train/train_support.cc | 92 +++++++++++++++++++ .../lite/src/registry/register_kernel_impl.cc | 4 +- .../src/runtime/kernel/arm/base/select.cc | 7 +- .../kernel/arm/fp16/convolution_1x1_fp16.cc | 3 +- .../kernel/arm/fp16/fused_batchnorm_fp16.cc | 64 +++++++------ .../arm/fp16_grad/arithmetic_fp16_grad.cc | 15 +-- .../kernel/arm/fp16_grad/bias_fp16_grad.cc | 3 +- mindspore/lite/src/train/train_export.cc | 10 +- .../src/train/train_populate_parameter.cc | 1 - mindspore/lite/src/train/train_session.cc | 7 +- mindspore/lite/src/train/train_utils.cc | 2 - mindspore/lite/src/train/transfer_session.cc | 11 +-- .../lite/tools/benchmark_train/net_train.cc | 66 +++++++------ .../converter/quantizer/quantize_util.cc | 7 +- 33 files changed, 252 insertions(+), 207 deletions(-) create mode 100644 mindspore/lite/src/cxx_api/train/train_support.cc diff --git a/mindspore/lite/examples/export_models/models/densenet_train_export.py b/mindspore/lite/examples/export_models/models/densenet_train_export.py index 3aa0649fa33..14c36475890 100644 --- a/mindspore/lite/examples/export_models/models/densenet_train_export.py +++ b/mindspore/lite/examples/export_models/models/densenet_train_export.py @@ -21,9 +21,9 @@ from train_utils import save_inout, train_wrap import mindspore.common.dtype as mstype from mindspore import context, Tensor, nn from mindspore.train.serialization import export -from src.network.densenet import DenseNet121 -sys.path.append(os.environ['CLOUD_MODEL_ZOO'] + 'official/cv/densenet121/') #pylint: disable=wrong-import-position +sys.path.append(os.environ['CLOUD_MODEL_ZOO'] + 'official/cv/densenet121/') +from src.network.densenet import DenseNet121 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU", save_graphs=False) diff --git a/mindspore/lite/examples/export_models/models/emoji_model.py b/mindspore/lite/examples/export_models/models/emoji_model.py index 05995d1f05e..0c733d2007c 100644 --- a/mindspore/lite/examples/export_models/models/emoji_model.py +++ b/mindspore/lite/examples/export_models/models/emoji_model.py @@ -15,6 +15,7 @@ """emoji_model.""" import mindspore as MS + class GlobalAvgPooling(MS.nn.Cell): """ Global avg pooling definition. @@ -33,6 +34,7 @@ class GlobalAvgPooling(MS.nn.Cell): x = self.mean(x, (2, 3)) return x + class EmojiModel(MS.nn.Cell): """emoji model""" def __init__(self, wayc, use_bb, use_head): diff --git a/mindspore/lite/examples/export_models/models/train_utils.py b/mindspore/lite/examples/export_models/models/train_utils.py index 1f284be38e8..037b999df00 100644 --- a/mindspore/lite/examples/export_models/models/train_utils.py +++ b/mindspore/lite/examples/export_models/models/train_utils.py @@ -39,22 +39,8 @@ def save_t(t, file): x.tofile(file) -def save_inout(name, x, l, net, net_train, sparse=False, epoch=1): - """save_inout""" - x_name = name + "_input1.bin" - if sparse: - x_name = name + "_input2.bin" - save_t(Tensor(x.asnumpy().transpose(0, 2, 3, 1)), x_name) - - l_name = name + "_input2.bin" - if sparse: - l_name = name + "_input1.bin" - save_t(l, l_name) - - net.set_train(False) - y = net(x) - - #train network +def train_and_save(name, net, net_train, x, l, epoch): + """train_and_save""" net.set_train(True) for i in range(epoch): net_train(x, l) @@ -72,7 +58,8 @@ def save_inout(name, x, l, net, net_train, sparse=False, epoch=1): y_name = name + "_output1.bin" save_t(y, y_name) -def save_inout_transfer(name, x, l, net_bb, net, net_train, sparse=False, epoch=1): + +def save_inout(name, x, l, net, net_train, sparse=False, epoch=1): """save_inout""" x_name = name + "_input1.bin" if sparse: @@ -84,25 +71,27 @@ def save_inout_transfer(name, x, l, net_bb, net, net_train, sparse=False, epoch= l_name = name + "_input1.bin" save_t(l, l_name) + net.set_train(False) + net(x) + + train_and_save(name, net, net_train, x, l, epoch) + + +def save_inout_transfer(name, x, l, net_bb, net, net_train, sparse=False, epoch=1): + """save_inout_transfer""" + x_name = name + "_input1.bin" + if sparse: + x_name = name + "_input2.bin" + save_t(Tensor(x.asnumpy().transpose(0, 2, 3, 1)), x_name) + + l_name = name + "_input2.bin" + if sparse: + l_name = name + "_input1.bin" + save_t(l, l_name) + net_bb.set_train(False) - net.set_train(False) x1 = net_bb(x) - y = net(x1) - - #train network - net.set_train(True) - for i in range(epoch): - net_train(x1, l) - net.set_train(False) - y = net(x1) - if isinstance(y, tuple): - i = 1 - for t in y: - with os.fdopen(name + "_output" + str(i) + ".bin", 'w') as f: - for j in t.asnumpy().flatten(): - f.write(str(j)+' ') - i = i + 1 - else: - y_name = name + "_output1.bin" - save_t(y, y_name) + net(x1) + + train_and_save(name, net, net_train, x1, l, epoch) diff --git a/mindspore/lite/examples/export_models/models_train.cfg b/mindspore/lite/examples/export_models/models_train.cfg index 5f295096b9d..24cb443e6e1 100644 --- a/mindspore/lite/examples/export_models/models_train.cfg +++ b/mindspore/lite/examples/export_models/models_train.cfg @@ -12,4 +12,3 @@ densenet shufflenetv2 vgg noarm32 xception -albert_mlm diff --git a/mindspore/lite/examples/train_lenet/src/net_runner.cc b/mindspore/lite/examples/train_lenet/src/net_runner.cc index 152c8e9dd37..86be84ec099 100644 --- a/mindspore/lite/examples/train_lenet/src/net_runner.cc +++ b/mindspore/lite/examples/train_lenet/src/net_runner.cc @@ -56,6 +56,8 @@ constexpr int kNCHWCDim = 2; constexpr int kPrintTimes = 100; constexpr int kSaveSteps = 1000; constexpr float kGammaFactor = 0.7f; +constexpr static int kElem2Print = 10; + class Rescaler : public mindspore::session::TrainLoopCallBack { public: explicit Rescaler(float scale) : scale_(scale) { @@ -126,7 +128,9 @@ bool after_callback(const std::vector &after_inpu auto d = reinterpret_cast(after_outputs.at(i)->MutableData()); int num2p = (after_outputs.at(i)->ElementsNum()); printf("ou%zu(%d): ", i, num2p); - if (num2p > 10) num2p = 10; + if (num2p > kElem2Print) { + num2p = kElem2Print; + } for (int j = 0; j < num2p; j++) printf("%f, ", d[j]); printf("\n"); } diff --git a/mindspore/lite/examples/train_lenet_java/model/lenet_export.py b/mindspore/lite/examples/train_lenet_java/model/lenet_export.py index 15bdd21fd47..6fc5e0ca9a4 100644 --- a/mindspore/lite/examples/train_lenet_java/model/lenet_export.py +++ b/mindspore/lite/examples/train_lenet_java/model/lenet_export.py @@ -19,7 +19,7 @@ from mindspore import context, Tensor import mindspore.common.dtype as mstype from mindspore.train.serialization import export from lenet import LeNet5 -from train_utils import TrainWrap +from train_utils import train_wrap n = LeNet5() n.set_train() @@ -28,7 +28,7 @@ context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU", save_graphs BATCH_SIZE = 4 x = Tensor(np.ones((BATCH_SIZE, 1, 32, 32)), mstype.float32) label = Tensor(np.zeros([BATCH_SIZE]).astype(np.int32)) -net = TrainWrap(n) +net = train_wrap(n) export(net, x, label, file_name="lenet_tod", file_format='MINDIR') print("finished exporting") diff --git a/mindspore/lite/examples/train_lenet_java/model/train_utils.py b/mindspore/lite/examples/train_lenet_java/model/train_utils.py index 0e422baac16..f09f982883f 100644 --- a/mindspore/lite/examples/train_lenet_java/model/train_utils.py +++ b/mindspore/lite/examples/train_lenet_java/model/train_utils.py @@ -17,9 +17,10 @@ import mindspore.nn as nn from mindspore.common.parameter import ParameterTuple -def TrainWrap(net, loss_fn=None, optimizer=None, weights=None): + +def train_wrap(net, loss_fn=None, optimizer=None, weights=None): """ - TrainWrap + train_wrap """ if loss_fn is None: loss_fn = nn.SoftmaxCrossEntropyWithLogits(reduction='mean', sparse=True) diff --git a/mindspore/lite/examples/transfer_learning/src/net_runner.cc b/mindspore/lite/examples/transfer_learning/src/net_runner.cc index 3cc408631ec..0d37f8f2f34 100644 --- a/mindspore/lite/examples/transfer_learning/src/net_runner.cc +++ b/mindspore/lite/examples/transfer_learning/src/net_runner.cc @@ -199,7 +199,7 @@ int NetRunner::TrainLoop() { session_->Export(cpkt_fn); } - std::cout << i + 1 << ": Loss is " << loss << " [min=" << min_loss << "]" << std::endl; + std::cout << (i + 1) << ": Loss is " << loss << " [min=" << min_loss << "]" << std::endl; if ((i + 1) % kBatchNum == 0) { session_->Eval(); float acc = CalculateAccuracy(ds_.test_data(), session_); diff --git a/mindspore/lite/examples/unified_api/src/net_runner.cc b/mindspore/lite/examples/unified_api/src/net_runner.cc index cbcda937fe4..238dfa8e9ac 100644 --- a/mindspore/lite/examples/unified_api/src/net_runner.cc +++ b/mindspore/lite/examples/unified_api/src/net_runner.cc @@ -57,6 +57,8 @@ constexpr int kNCHWCDim = 2; constexpr int kPrintTimes = 100; constexpr int kSaveEpochs = 3; constexpr float kGammaFactor = 0.7f; +constexpr static int kElem2Print = 10; + class Rescaler : public mindspore::TrainCallBack { public: explicit Rescaler(float scale) : scale_(scale) { @@ -128,7 +130,9 @@ bool after_callback(const std::vector &after_inpu auto d = reinterpret_cast(after_outputs.at(i)->MutableData()); int num2p = (after_outputs.at(i)->ElementsNum()); printf("ou%zu(%d): ", i, num2p); - if (num2p > 10) num2p = 10; + if (num2p > kElem2Print) { + num2p = kElem2Print; + } for (int j = 0; j < num2p; j++) printf("%f, ", d[j]); printf("\n"); } diff --git a/mindspore/lite/src/common/quant_utils.cc b/mindspore/lite/src/common/quant_utils.cc index 30a1a11e8ec..dbef61e86dc 100644 --- a/mindspore/lite/src/common/quant_utils.cc +++ b/mindspore/lite/src/common/quant_utils.cc @@ -20,7 +20,6 @@ namespace mindspore { namespace lite { - void GetMaxMinPerchannel(int channels, int one_filter_size, int i, int elem_count, const float *raw_datas, bool channel_at_first, float *desired_max, float *desired_min) { float min = FLT_MAX; @@ -99,6 +98,5 @@ STATUS CalQuantizationParams(schema::QuantParamT *quantParam, double mMin, doubl return RET_OK; } - } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/callback/ckpt_saver.cc b/mindspore/lite/src/cxx_api/callback/ckpt_saver.cc index 705688ab994..a1aed22d302 100644 --- a/mindspore/lite/src/cxx_api/callback/ckpt_saver.cc +++ b/mindspore/lite/src/cxx_api/callback/ckpt_saver.cc @@ -23,7 +23,6 @@ #include "src/common/log_adapter.h" namespace mindspore { - CkptSaver::CkptSaver(int save_every_n, const std::string &filename_prefix) { callback_impl_ = new CallbackImpl(new lite::CkptSaver(save_every_n, filename_prefix)); } @@ -37,5 +36,4 @@ CkptSaver::~CkptSaver() { delete internal_call_back; } } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/callback/loss_monitor.cc b/mindspore/lite/src/cxx_api/callback/loss_monitor.cc index c096879d49e..6807f9b3ab5 100644 --- a/mindspore/lite/src/cxx_api/callback/loss_monitor.cc +++ b/mindspore/lite/src/cxx_api/callback/loss_monitor.cc @@ -23,7 +23,6 @@ #include "src/common/log_adapter.h" namespace mindspore { - LossMonitor::LossMonitor(int print_every_n_steps) { callback_impl_ = new CallbackImpl(new lite::LossMonitor(print_every_n_steps)); } @@ -53,5 +52,4 @@ const std::vector &LossMonitor::GetLossPoints() { return (reinterpret_cast(internal_call_back))->GetLossPoints(); } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/callback/lr_scheduler.cc b/mindspore/lite/src/cxx_api/callback/lr_scheduler.cc index f6d61f7e14f..aa08a963b49 100644 --- a/mindspore/lite/src/cxx_api/callback/lr_scheduler.cc +++ b/mindspore/lite/src/cxx_api/callback/lr_scheduler.cc @@ -23,7 +23,6 @@ #include "src/common/log_adapter.h" namespace mindspore { - int StepLRLambda(float *lr, int epoch, void *lr_cb_data) { if ((lr == nullptr) || (lr_cb_data == nullptr)) { MS_LOG(ERROR) << "nullptr passed as input to MultiplicativeLRLambda"; @@ -51,5 +50,4 @@ LRScheduler::~LRScheduler() { delete internal_call_back; } } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/callback/train_accuracy.cc b/mindspore/lite/src/cxx_api/callback/train_accuracy.cc index 614ea972ad4..01e5ea98950 100644 --- a/mindspore/lite/src/cxx_api/callback/train_accuracy.cc +++ b/mindspore/lite/src/cxx_api/callback/train_accuracy.cc @@ -23,7 +23,6 @@ #include "src/common/log_adapter.h" namespace mindspore { - TrainAccuracy::TrainAccuracy(int print_every_n, int accuracy_metrics, const std::vector &input_indexes, const std::vector &output_indexes) { callback_impl_ = new CallbackImpl( @@ -55,5 +54,4 @@ const std::vector &TrainAccuracy::GetAccuracyPoints() { return (reinterpret_cast(internal_call_back))->GetAccuracyPoints(); } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/converters.cc b/mindspore/lite/src/cxx_api/converters.cc index 7fd3fa4d7fb..b72bd82dc55 100644 --- a/mindspore/lite/src/cxx_api/converters.cc +++ b/mindspore/lite/src/cxx_api/converters.cc @@ -24,6 +24,8 @@ #include "src/common/log_adapter.h" namespace mindspore { +constexpr static int kMaxNumOfDevices = 2; + Status A2L_ConvertContext(Context *a_context, lite::Context *l_context) { if ((a_context == nullptr) || (l_context == nullptr)) { MS_LOG(ERROR) << "Invalid context pointers."; @@ -35,7 +37,7 @@ Status A2L_ConvertContext(Context *a_context, lite::Context *l_context) { MS_LOG(ERROR) << "Invalid device list."; return kLiteInputParamInvalid; } - if (device_list.size() > 2) { + if (device_list.size() > kMaxNumOfDevices) { MS_LOG(ERROR) << "Only CPU/CPU & GPU/CPU & NPU mode is supported."; return kLiteInputParamInvalid; } @@ -71,7 +73,7 @@ Status A2L_ConvertContext(Context *a_context, lite::Context *l_context) { cpu_info.cpu_device_info_ = {cpu_context->GetEnableFP16(), mode}; l_context->device_list_.push_back({lite::DT_CPU, cpu_info, cpu_context->GetProvider(), cpu_context->GetProviderDevice(), cpu_context->GetAllocator()}); - if (device_list.size() == 2) { + if (device_list.size() == kMaxNumOfDevices) { lite::DeviceInfo device_info = {0}; if (device_list[1]->GetDeviceType() == kGPU) { auto gpu_context = device_list[1]->Cast(); diff --git a/mindspore/lite/src/cxx_api/metrics/accuracy.cc b/mindspore/lite/src/cxx_api/metrics/accuracy.cc index 283aac7ca0b..9ea6ed6d62d 100644 --- a/mindspore/lite/src/cxx_api/metrics/accuracy.cc +++ b/mindspore/lite/src/cxx_api/metrics/accuracy.cc @@ -23,7 +23,6 @@ #include "src/common/log_adapter.h" namespace mindspore { - AccuracyMetrics::AccuracyMetrics(int accuracy_metrics, const std::vector &input_indexes, const std::vector &output_indexes) { metrics_impl_ = new MetricsImpl(new lite::AccuracyMetrics(accuracy_metrics, input_indexes, output_indexes)); @@ -56,5 +55,4 @@ float AccuracyMetrics::Eval() { auto internal_metrics = metrics_impl_->GetInternalMetrics(); return (reinterpret_cast(internal_metrics))->Eval(); } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/model/model_impl.cc b/mindspore/lite/src/cxx_api/model/model_impl.cc index d0525e619f1..6dbddd672d7 100644 --- a/mindspore/lite/src/cxx_api/model/model_impl.cc +++ b/mindspore/lite/src/cxx_api/model/model_impl.cc @@ -437,5 +437,4 @@ Status ModelImpl::Resize(const std::vector &inputs, const std::vector< auto ret = session_->Resize(inner_input, truncated_shape); return static_cast(ret); } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/train/converters.cc b/mindspore/lite/src/cxx_api/train/converters.cc index 959d3e2871a..b8b0e92364c 100644 --- a/mindspore/lite/src/cxx_api/train/converters.cc +++ b/mindspore/lite/src/cxx_api/train/converters.cc @@ -24,7 +24,6 @@ #include "src/common/log_adapter.h" namespace mindspore { - Status A2L_ConvertConfig(const TrainCfg *a_train_cfg, lite::TrainCfg *l_train_cfg) { if ((a_train_cfg == nullptr) || (l_train_cfg == nullptr)) { MS_LOG(ERROR) << "Invalid train_cfg pointers"; @@ -39,5 +38,4 @@ Status A2L_ConvertConfig(const TrainCfg *a_train_cfg, lite::TrainCfg *l_train_cf return kSuccess; } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/train/model_impl.cc b/mindspore/lite/src/cxx_api/train/model_impl.cc index 7577b786c24..a40300b3248 100644 --- a/mindspore/lite/src/cxx_api/train/model_impl.cc +++ b/mindspore/lite/src/cxx_api/train/model_impl.cc @@ -40,60 +40,6 @@ #include "src/train/train_session.h" namespace mindspore { -using mindspore::lite::RET_ERROR; -using mindspore::lite::RET_OK; - -std::shared_ptr CreateTrainSession(std::shared_ptr graph_data, - std::shared_ptr cfg, lite::Context *context) { - bool is_train_session = graph_data->IsTrainModel(); - - if (is_train_session) { - auto model = graph_data->lite_model(); - if (model == nullptr || model->buf == nullptr) { - MS_LOG(ERROR) << "Lite model has been freed."; - return nullptr; - } - std::shared_ptr shared_session; - lite::TrainSession *session = new lite::TrainSession(); - if (session == nullptr) { - MS_LOG(ERROR) << "create session failed"; - return nullptr; - } - shared_session.reset(session); - - lite::TrainCfg train_cfg; - if (cfg != nullptr) { - auto status = A2L_ConvertConfig(cfg.get(), &train_cfg); - if (status != kSuccess) { - MS_LOG(ERROR) << "Failed to convert Config to Lite Config"; - return nullptr; - } - } - - auto ret = session->Init(context, &train_cfg); - if (ret != mindspore::lite::RET_OK) { - MS_LOG(ERROR) << "init session failed"; - return nullptr; - } - - ret = session->CompileTrainGraph(model); - if (ret != mindspore::lite::RET_OK) { - MS_LOG(ERROR) << "Compiling Train Graph session failed"; - return nullptr; - } - return shared_session; - } - MS_LOG(DEBUG) << "Session is not a train session."; - return nullptr; -} - -class UnifiedAPISupportTrain { - public: - UnifiedAPISupportTrain() { CreateTrainSessionCallbackHolder(CreateTrainSession); } -}; - -UnifiedAPISupportTrain support_train_api; - Status ModelImpl::PrepareMetrics(Model *model, std::vector *out_ms, std::vector *adapter_ms) { if (out_ms == nullptr || adapter_ms == nullptr) { @@ -157,5 +103,4 @@ Status ModelImpl::ConvertCallbacks(Model *model, std::vector *i } return kSuccess; } - } // namespace mindspore diff --git a/mindspore/lite/src/cxx_api/train/train_support.cc b/mindspore/lite/src/cxx_api/train/train_support.cc new file mode 100644 index 00000000000..afbe9adc32c --- /dev/null +++ b/mindspore/lite/src/cxx_api/train/train_support.cc @@ -0,0 +1,92 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "include/api/types.h" +#include "include/api/context.h" +#include "include/api/dual_abi_helper.h" +#include "include/lite_session.h" +#include "include/context.h" +#include "include/api/callback/callback.h" +#include "include/api/metrics/metrics.h" +#include "src/lite_model.h" +#include "src/runtime/inner_allocator.h" +#include "src/common/string_util.h" +#include "src/cxx_api/model/model_impl.h" +#include "src/cxx_api/converters.h" +#include "src/cxx_api/graph/graph_data.h" +#include "src/cxx_api/tensor/tensor_impl.h" +#include "src/cxx_api/tensor_utils.h" +#include "src/cxx_api/metrics/metrics_adapter.h" +#include "src/cxx_api/metrics/metrics_impl.h" +#include "src/cxx_api/callback/callback_adapter.h" +#include "src/cxx_api/callback/callback_impl.h" +#include "src/common/log_adapter.h" +#include "src/train/train_session.h" + +namespace mindspore { +std::shared_ptr CreateTrainSession(std::shared_ptr graph_data, + std::shared_ptr cfg, lite::Context *context) { + bool is_train_session = graph_data->IsTrainModel(); + if (is_train_session) { + auto model = graph_data->lite_model(); + if (model == nullptr || model->buf == nullptr) { + MS_LOG(ERROR) << "Lite model has been freed."; + return nullptr; + } + std::shared_ptr shared_session; + lite::TrainSession *session = new lite::TrainSession(); + if (session == nullptr) { + MS_LOG(ERROR) << "create session failed"; + return nullptr; + } + shared_session.reset(session); + + lite::TrainCfg train_cfg; + if (cfg != nullptr) { + auto status = A2L_ConvertConfig(cfg.get(), &train_cfg); + if (status != kSuccess) { + MS_LOG(ERROR) << "Failed to convert Config to Lite Config"; + return nullptr; + } + } + + auto ret = session->Init(context, &train_cfg); + if (ret != mindspore::lite::RET_OK) { + MS_LOG(ERROR) << "init session failed"; + return nullptr; + } + + ret = session->CompileTrainGraph(model); + if (ret != mindspore::lite::RET_OK) { + MS_LOG(ERROR) << "Compiling Train Graph session failed"; + return nullptr; + } + return shared_session; + } + MS_LOG(DEBUG) << "Session is not a train session."; + return nullptr; +} + +class TrainSupport { + public: + TrainSupport() { CreateTrainSessionCallbackHolder(CreateTrainSession); } +}; + +TrainSupport support_train_api; +} // namespace mindspore diff --git a/mindspore/lite/src/registry/register_kernel_impl.cc b/mindspore/lite/src/registry/register_kernel_impl.cc index f4f868a278d..b6885eee601 100644 --- a/mindspore/lite/src/registry/register_kernel_impl.cc +++ b/mindspore/lite/src/registry/register_kernel_impl.cc @@ -47,13 +47,11 @@ int RegistryKernelImpl::RegCustomKernel(const std::string &arch, const std::stri std::unique_lock lock(lock_); if (custom_kernel_creators_[provider][arch][type] == nullptr) { custom_kernel_creators_[provider][arch][type] = - reinterpret_cast(malloc(data_type_length_ * sizeof(CreateKernel))); + reinterpret_cast(calloc(data_type_length_, sizeof(CreateKernel))); if (custom_kernel_creators_[provider][arch][type] == nullptr) { MS_LOG(ERROR) << "malloc custom kernel creator fail!provider: " << provider << ", arch: " << arch; return RET_ERROR; } - memset(reinterpret_cast(custom_kernel_creators_[provider][arch][type]), 0, - data_type_length_ * sizeof(CreateKernel)); } int data_type_index = data_type - kNumberTypeBegin - 1; diff --git a/mindspore/lite/src/runtime/kernel/arm/base/select.cc b/mindspore/lite/src/runtime/kernel/arm/base/select.cc index 4ed0b896131..57256062ecb 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/select.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/select.cc @@ -24,6 +24,9 @@ using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_Select; namespace mindspore::kernel { +constexpr static int kFirstIdx = 1; +constexpr static int kSecondIdx = 2; + int SelectCPUKernel::Init() { return RET_OK; } int SelectCPUKernel::ReSize() { return RET_OK; } @@ -70,8 +73,8 @@ int SelectCPUKernel::Run() { MS_ASSERT(in_tensors_.at(1)->Size() == out_tensors_.at(0)->Size()); auto size = in_tensors_.at(1)->ElementsNum(); auto condition = static_cast(bool_tensor->data_c()); - auto input1 = static_cast(in_tensors_.at(1)->data_c()); - auto input2 = static_cast(in_tensors_.at(2)->data_c()); + auto input1 = static_cast(in_tensors_.at(kFirstIdx)->data_c()); + auto input2 = static_cast(in_tensors_.at(kSecondIdx)->data_c()); auto output = static_cast(out_tensors_.at(0)->data_c()); for (int i = 0; i < size; i++) { output[i] = condition[i] ? input1[i] : input2[i]; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc index dfcdad360e1..f3257b424a7 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc @@ -97,7 +97,7 @@ int Convolution1x1FP16CPUKernel::InitWeightBias() { return RET_ERROR; } } - void *bias_origin_tmp = IsTrainable() ? in_tensors_.at(2)->data_c() : origin_bias_; + void *bias_origin_tmp = IsTrainable() ? in_tensors_.at(kBiasIndex)->data_c() : origin_bias_; memcpy(bias_data_, bias_origin_tmp, output_channel * sizeof(float16_t)); memset(reinterpret_cast(bias_data_) + bias_size, 0, size - bias_size); } @@ -295,5 +295,4 @@ int Convolution1x1FP16CPUKernel::Eval() { } return InnerKernel::Eval(); } - } // namespace mindspore::kernel diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc index a67261527c8..9e544cd6e30 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc @@ -25,6 +25,15 @@ using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_FusedBatchNorm; namespace mindspore::kernel { +constexpr static int kInScaleIdx = 1; +constexpr static int kInOffsetIdx = 2; +constexpr static int kInCurrentMeanIdx = 3; +constexpr static int kInCurrentVarIdx = 4; +constexpr static int kMaxInIdx = 5; +constexpr static int kOutScaleIdx = 1; +constexpr static int kOutOffsetIdx = 2; +constexpr static int kOutCurrentMeanIdx = 3; +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) { @@ -32,18 +41,18 @@ void FusedBatchnormFp16CPUKernel::CalcMeanVar(float16_t *in, float16_t *scale, f float16_t *current_mean = static_cast(mean_); float16_t *current_var = static_cast(variance_); - 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); + std::fill(current_mean, current_mean + in_tensors_.at(kInCurrentMeanIdx)->ElementsNum(), 0.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); - 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()); + 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_[1]->Size()); - memcpy(offset_, offset, in_tensors_[2]->Size()); + memcpy(scale_, scale, in_tensors_[kInScaleIdx]->Size()); + memcpy(offset_, offset, in_tensors_[kInOffsetIdx]->Size()); trained_ = true; // trained at least once } @@ -52,13 +61,13 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) { auto param = reinterpret_cast(op_parameter_); MS_ASSERT(param); if (in_tensors_.at(0)->data_type() == kNumberTypeFloat32) { - MS_ASSERT(in_tensors_.size() == 5); + MS_ASSERT(in_tensors_.size() == kMaxInIdx); MS_ASSERT(out_tensors_.size() == 1); auto input = in_tensors_.at(0); - auto scale = in_tensors_.at(1); - auto offset = in_tensors_.at(2); - auto mean = in_tensors_.at(3); - auto variance = in_tensors_.at(4); + auto scale = in_tensors_.at(kInScaleIdx); + auto offset = in_tensors_.at(kInOffsetIdx); + auto mean = in_tensors_.at(kInCurrentMeanIdx); + auto variance = in_tensors_.at(kInCurrentVarIdx); auto output = out_tensors_.at(0); auto input_fp16 = ms_context_->allocator->Malloc(input->ElementsNum() * sizeof(float16_t)); @@ -88,7 +97,7 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) { Float32ToFloat16(reinterpret_cast(variance->data_c()), reinterpret_cast(variance_fp16), variance->ElementsNum()); - if (IsTrain() && IsTrainable() && in_tensors_.size() >= 5) { + if (IsTrain() && IsTrainable() && in_tensors_.size() >= kMaxInIdx) { CalcMeanVar(reinterpret_cast(input_fp16), reinterpret_cast(scale_fp16), reinterpret_cast(offset_fp16), reinterpret_cast(mean_fp16), reinterpret_cast(variance_fp16)); @@ -108,11 +117,12 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) { return RET_OK; } - if (IsTrain() && IsTrainable() && in_tensors_.size() >= 5) { - CalcMeanVar( - static_cast(in_tensors_.at(0)->data_c()), static_cast(in_tensors_.at(1)->data_c()), - static_cast(in_tensors_.at(2)->data_c()), static_cast(in_tensors_.at(3)->data_c()), - static_cast(in_tensors_.at(4)->data_c())); + if (IsTrain() && IsTrainable() && in_tensors_.size() >= kMaxInIdx) { + CalcMeanVar(static_cast(in_tensors_.at(0)->data_c()), + static_cast(in_tensors_.at(kInScaleIdx)->data_c()), + static_cast(in_tensors_.at(kInOffsetIdx)->data_c()), + static_cast(in_tensors_.at(kInCurrentMeanIdx)->data_c()), + static_cast(in_tensors_.at(kInCurrentVarIdx)->data_c())); } FusedBatchNormFp16(in_tensors_.at(0)->data_c(), scale_, offset_, mean_, variance_, param, task_id, out_tensors_.at(0)->data_c()); @@ -122,16 +132,16 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) { int FusedBatchnormFp16CPUKernel::Eval() { InnerKernel::Eval(); if (trained_) { - float16_t *save_mean = static_cast(in_tensors_.at(3)->data_c()); - float16_t *save_var = static_cast(in_tensors_.at(4)->data_c()); - float16_t *scale = static_cast(in_tensors_.at(1)->data_c()); - float16_t *bias = static_cast(in_tensors_.at(2)->data_c()); + float16_t *save_mean = static_cast(in_tensors_.at(kInCurrentMeanIdx)->data_c()); + float16_t *save_var = static_cast(in_tensors_.at(kInCurrentVarIdx)->data_c()); + float16_t *scale = static_cast(in_tensors_.at(kInScaleIdx)->data_c()); + float16_t *bias = static_cast(in_tensors_.at(kInOffsetIdx)->data_c()); // Copy to local variables - memcpy(scale_, scale, in_tensors_.at(1)->Size()); - memcpy(offset_, bias, in_tensors_.at(2)->Size()); - memcpy(mean_, save_mean, in_tensors_.at(3)->Size()); - memcpy(variance_, save_var, in_tensors_.at(4)->Size()); + memcpy(scale_, scale, in_tensors_.at(kInScaleIdx)->Size()); + memcpy(offset_, bias, in_tensors_.at(kInOffsetIdx)->Size()); + memcpy(mean_, save_mean, in_tensors_.at(kInCurrentMeanIdx)->Size()); + memcpy(variance_, save_var, in_tensors_.at(kInCurrentVarIdx)->Size()); } return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16_grad/arithmetic_fp16_grad.cc b/mindspore/lite/src/runtime/kernel/arm/fp16_grad/arithmetic_fp16_grad.cc index 09a4d004277..9b22fb394f0 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16_grad/arithmetic_fp16_grad.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16_grad/arithmetic_fp16_grad.cc @@ -26,14 +26,17 @@ using mindspore::lite::RET_ERROR; using mindspore::lite::RET_OK; namespace mindspore::kernel { +constexpr static int kX1Idx = 0; +constexpr static int kX2Idx = 1; +constexpr static int kDyIdx = 2; int ArithmeticGradCPUKernelFp16::Init() { return RET_OK; } void ArithmeticGradCPUKernelFp16::ArithmeticGradMaximum(float16_t *dy, int dy_size, float16_t *dx1, int dx1_size, float16_t *dx2, int dx2_size) { - auto x1 = reinterpret_cast(in_tensors_[0]->data_c()); - auto x2 = reinterpret_cast(in_tensors_[1]->data_c()); - dy = reinterpret_cast(in_tensors_[2]->data_c()); + auto x1 = reinterpret_cast(in_tensors_[kX1Idx]->data_c()); + auto x2 = reinterpret_cast(in_tensors_[kX2Idx]->data_c()); + dy = reinterpret_cast(in_tensors_[kDyIdx]->data_c()); MaximumByAxesFp16(x1, x2, dy, arithmeticParameter_->in_shape0_, arithmeticParameter_->in_shape1_, arithmeticParameter_->out_shape_, dx1, dx2, arithmeticParameter_->ndim_); @@ -41,9 +44,9 @@ void ArithmeticGradCPUKernelFp16::ArithmeticGradMaximum(float16_t *dy, int dy_si void ArithmeticGradCPUKernelFp16::ArithmeticGradMinimum(float16_t *dy, int dy_size, float16_t *dx1, int dx1_size, float16_t *dx2, int dx2_size) { - auto x1 = reinterpret_cast(in_tensors_[0]->data_c()); - auto x2 = reinterpret_cast(in_tensors_[1]->data_c()); - dy = reinterpret_cast(in_tensors_[2]->data_c()); + auto x1 = reinterpret_cast(in_tensors_[kX1Idx]->data_c()); + auto x2 = reinterpret_cast(in_tensors_[kX2Idx]->data_c()); + dy = reinterpret_cast(in_tensors_[kDyIdx]->data_c()); MinimumByAxesFp16(x1, x2, dy, arithmeticParameter_->in_shape0_, arithmeticParameter_->in_shape1_, arithmeticParameter_->out_shape_, dx1, dx2, arithmeticParameter_->ndim_); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16_grad/bias_fp16_grad.cc b/mindspore/lite/src/runtime/kernel/arm/fp16_grad/bias_fp16_grad.cc index 76521f55fec..9c8e3ae1417 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16_grad/bias_fp16_grad.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16_grad/bias_fp16_grad.cc @@ -27,6 +27,7 @@ using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_BiasAddGrad; namespace mindspore::kernel { +constexpr static int kMaxDim = 4; int BiasGradCPUKernelFp16::ReSize() { auto dims = in_tensors_[0]->shape(); @@ -36,7 +37,7 @@ int BiasGradCPUKernelFp16::ReSize() { bias_param->out_shape_[i] = 1; // 1 dimension for N,H,W, } bias_param->out_shape_[bias_param->ndim_ - 1] = dims[bias_param->ndim_ - 1]; - for (auto i = bias_param->ndim_; i < 4; i++) { + for (auto i = bias_param->ndim_; i < kMaxDim; i++) { bias_param->in_shape0_[i] = 0; bias_param->out_shape_[i] = 0; } diff --git a/mindspore/lite/src/train/train_export.cc b/mindspore/lite/src/train/train_export.cc index 99c0b0d59c8..6872ed0a1bb 100644 --- a/mindspore/lite/src/train/train_export.cc +++ b/mindspore/lite/src/train/train_export.cc @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define _STUB #include "src/train/train_export.h" #include #include @@ -29,6 +28,8 @@ namespace mindspore { namespace lite { +constexpr static int kFmkVal = 3; +constexpr static int kTransformTensorDim = 4; std::vector TrainExport::CreateData(const lite::Tensor *tensor) { uint8_t *tensor_data = reinterpret_cast(tensor->data_c()); @@ -209,7 +210,7 @@ std::unique_ptr TrainExport::CreateTransformTensor(size_t id) { tensorT->dataType = scTensor->dataType; std::vector dims; std::vector val = {0, 2, 3, 1}; - if (scTensor->dims.size() == 4) { + if (scTensor->dims.size() == kTransformTensorDim) { for (size_t i = 0; i < val.size(); i++) { dims.push_back(scTensor->dims.at(val[i])); } @@ -233,7 +234,7 @@ std::unique_ptr TrainExport::CreateTransformConst(size_t last_i } tensorT->nodeType = lite::NodeType_ValueNode; tensorT->dataType = TypeId::kNumberTypeInt32; - tensorT->dims = {4}; + tensorT->dims = {kTransformTensorDim}; tensorT->format = schema::Format_NCHW; tensorT->name = "const-" + std::to_string(last_id); tensorT->refCount = 0; @@ -406,7 +407,7 @@ int TrainExport::ExportInit(const std::string model_name, std::string version) { MS_LOG(ERROR) << "cannot allocate meta_graph"; return RET_ERROR; } - meta_graph_->fmkType = 3; + meta_graph_->fmkType = kFmkVal; meta_graph_->name = model_name; meta_graph_->version = version; return RET_OK; @@ -420,6 +421,5 @@ int TrainExport::IsInputTensor(const schema::TensorT &t) { } TrainExport::~TrainExport() { delete meta_graph_; } - } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/train/train_populate_parameter.cc b/mindspore/lite/src/train/train_populate_parameter.cc index 0d77cb05832..d67db142fff 100644 --- a/mindspore/lite/src/train/train_populate_parameter.cc +++ b/mindspore/lite/src/train/train_populate_parameter.cc @@ -31,7 +31,6 @@ #include "nnacl/fp32_grad/resize_grad.h" namespace mindspore { namespace kernel { - OpParameter *PopulateSmoothL1LossParameter(const void *prim) { SmoothL1LossParameter *p = reinterpret_cast(malloc(sizeof(SmoothL1LossParameter))); if (p == nullptr) { diff --git a/mindspore/lite/src/train/train_session.cc b/mindspore/lite/src/train/train_session.cc index 9da76cc5c8f..c3eb823273d 100644 --- a/mindspore/lite/src/train/train_session.cc +++ b/mindspore/lite/src/train/train_session.cc @@ -198,7 +198,7 @@ TrainSession::~TrainSession() { FreeWorkSpace(); } int TrainSession::ExecKernels(const KernelCallBack &before, const KernelCallBack &after, const std::vector &run_kernels) { for (auto *kernel : run_kernels) { - MS_ASSERT(nullptr != kernel); + MS_ASSERT(kernel != nullptr); auto ret = kernel->Execute(before, after); if (RET_OK != ret) { MS_LOG(ERROR) << "Execute kernel failed, name: " << kernel->name(); @@ -309,7 +309,7 @@ int TrainSession::MixPrecisionExecKernels(const KernelCallBack &before, const Ke const std::vector &run_kernels) { float scale = cfg_.mix_precision_cfg_.loss_scale_; for (auto *kernel : run_kernels) { - MS_ASSERT(nullptr != kernel); + MS_ASSERT(kernel != nullptr); MixPrecisionPreProcess(kernel, scale); auto ret = kernel->Execute(before, after); if (RET_OK != ret) { @@ -398,7 +398,7 @@ int TrainSession::Train() { train_mode_ = true; virtual_batch_idx_ = 0; for (auto &kernel : this->train_kernels_) { - MS_ASSERT(nullptr != kernel); + MS_ASSERT(kernel != nullptr); auto ret = kernel->Train(); if (ret != RET_OK) { MS_LOG(ERROR) << kernel->name() << " failed to set train mode"; @@ -791,5 +791,4 @@ session::LiteSession *session::TrainSession::CreateTrainSession(const std::strin } return session.release(); } - } // namespace mindspore diff --git a/mindspore/lite/src/train/train_utils.cc b/mindspore/lite/src/train/train_utils.cc index 0a325b94de5..b35f978e3dd 100644 --- a/mindspore/lite/src/train/train_utils.cc +++ b/mindspore/lite/src/train/train_utils.cc @@ -26,7 +26,6 @@ namespace mindspore { namespace lite { - size_t TSFindTensor(const std::vector &where, const lite::Tensor *searchParameter) { for (size_t i = 0; i < where.size(); i++) { if (where[i] == searchParameter) { @@ -199,6 +198,5 @@ int ScaleTensor(Tensor *tensor, float scale) { MS_LOG(DEBUG) << "Scale tensor: " << tensor->tensor_name() << " " << scale; return tensor->Scale(scale); } - } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/src/train/transfer_session.cc b/mindspore/lite/src/train/transfer_session.cc index 008073e5495..8eb1d1d2b4f 100644 --- a/mindspore/lite/src/train/transfer_session.cc +++ b/mindspore/lite/src/train/transfer_session.cc @@ -39,7 +39,6 @@ namespace mindspore { namespace lite { - TransferSession::TransferSession(const char *model_buf_backbone, size_t size_backbone, const lite::Context *context) : is_valid_(false) { lite_model_ = reinterpret_cast(malloc(size_backbone)); @@ -92,18 +91,18 @@ int TransferSession::CompileTransferGraph() { nchw2nhwc_ = CompileFormatTransform(output, input, nchw2nhwc_mask, 4); match = nchw2nhwc_; } - if (true == match) { + if (match) { break; } } } - if (true == match) { + if (match) { backbone_head_map_.push_back(std::make_pair(input, output)); } else { combined_inputs_.push_back(input); } } - if (0 == backbone_head_map_.size()) { + if (backbone_head_map_.size() == 0) { ret = RET_ERROR; } return ret; @@ -113,7 +112,7 @@ mindspore::tensor::MSTensor *TransferSession::GetInputsByTensorName(const std::s /* First look in backbone netwok */ auto ret = backbone_session_->GetInputsByTensorName(tensor_name); /* If not found look in head network */ - if (nullptr == ret) { + if (ret == nullptr) { ret = TrainSession::GetInputsByTensorName(tensor_name); } return ret; @@ -220,7 +219,6 @@ int TransferSession::Export(const std::string &filename, ModelType model_type, Q if (orig_train_state) Train(); return status; } - } // namespace lite static session::LiteSession *CreateTransferSessionInt(const char *model_buf_backbone, size_t size_backbone, @@ -316,5 +314,4 @@ session::LiteSession *session::TrainSession::CreateTransferSession(const std::st } return CreateTransferSessionInt(buf_backbone, size_backbone, buf_head, size_head, ctxt, train_mode, cfg); } - } // namespace mindspore diff --git a/mindspore/lite/tools/benchmark_train/net_train.cc b/mindspore/lite/tools/benchmark_train/net_train.cc index 0104e4b189f..43c2a6c8a0e 100644 --- a/mindspore/lite/tools/benchmark_train/net_train.cc +++ b/mindspore/lite/tools/benchmark_train/net_train.cc @@ -38,6 +38,15 @@ static const char *DELIM_SLASH = "/"; constexpr const char *DELIM_COLON = ":"; constexpr const char *DELIM_COMMA = ","; constexpr int RET_TOO_BIG = -9; +constexpr int kField0 = 0; +constexpr int kField1 = 1; +constexpr int kField2 = 2; +constexpr int kField3 = 3; +constexpr int kField4 = 4; +constexpr int kFieldsToPrint = 5; +constexpr int kPrintOffset = 4; +constexpr int kCPUBindFlag2 = 2; +constexpr int kCPUBindFlag1 = 1; namespace { float *ReadFileBuf(const char *file, size_t *size) { @@ -60,7 +69,7 @@ float *ReadFileBuf(const char *file, size_t *size) { ifs.seekg(0, std::ios::end); *size = ifs.tellg(); - std::unique_ptr buf((new (std::nothrow) float[*size / sizeof(float) + 1])); + std::unique_ptr buf = std::make_unique(*size / sizeof(float) + 1); if (buf == nullptr) { MS_LOG(ERROR) << "malloc buf failed, file: " << real_path; ifs.close(); @@ -136,7 +145,7 @@ int NetTrain::ReadInputFile(std::vector *ms_input MS_ASSERT(cur_tensor != nullptr); size_t size; std::string file_name = flags_->in_data_file_ + std::to_string(i + 1) + ".bin"; - char *bin_buf = ReadFile(file_name.c_str(), &size); + auto bin_buf = ReadFile(file_name.c_str(), &size); if (bin_buf == nullptr) { MS_LOG(ERROR) << "ReadFile return nullptr"; return RET_ERROR; @@ -312,10 +321,10 @@ int NetTrain::MarkAccuracy(const std::unique_ptr &session, } static CpuBindMode FlagToBindMode(int flag) { - if (flag == 2) { + if (flag == kCPUBindFlag2) { return MID_CPU; } - if (flag == 1) { + if (flag == kCPUBindFlag1) { return HIGHER_CPU; } return NO_BIND; @@ -337,7 +346,6 @@ std::unique_ptr NetTrain::CreateAndRunNetworkForTrain(cons std::cout << "RunNetTrain CreateTranferSession failed while running " << model_name.c_str() << std::endl; return nullptr; } - } else { MS_LOG(INFO) << "CreateTrainSession from model file" << filename.c_str(); std::cout << "CreateTrainSession from model file " << filename.c_str() << std::endl; @@ -428,8 +436,8 @@ int NetTrain::CreateAndRunNetwork(const std::string &filename, const std::string } auto end_prepare_time = GetTimeUs(); - MS_LOG(INFO) << "PrepareTime = " << (end_prepare_time - start_prepare_time) / 1000 << " ms"; - std::cout << "PrepareTime = " << (end_prepare_time - start_prepare_time) / 1000 << " ms" << std::endl; + MS_LOG(INFO) << "PrepareTime = " << ((end_prepare_time - start_prepare_time) / 1000) << " ms"; + std::cout << "PrepareTime = " << ((end_prepare_time - start_prepare_time) / 1000) << " ms" << std::endl; // Load input MS_LOG(INFO) << "Load input data"; auto ms_inputs = session->GetInputs(); @@ -719,43 +727,47 @@ int NetTrain::Init() { int NetTrain::PrintResult(const std::vector &title, const std::map> &result) { - std::vector columnLenMax(5); + std::vector columnLenMax(kFieldsToPrint); std::vector> rows; for (auto &iter : result) { - char stringBuf[5][100] = {}; + std::string stringBuf[kFieldsToPrint]; std::vector columns; size_t len; len = iter.first.size(); - if (len > columnLenMax.at(0)) { - columnLenMax.at(0) = len + 4; + if (len > columnLenMax.at(kField0)) { + columnLenMax.at(kField0) = len + kPrintOffset; } columns.push_back(iter.first); - len = snprintf(stringBuf[1], sizeof(stringBuf[1]), "%f", iter.second.second / flags_->epochs_); - if (len > columnLenMax.at(1)) { - columnLenMax.at(1) = len + 4; + stringBuf[kField1] = to_string(iter.second.second / flags_->epochs_); + len = stringBuf[kField1].length(); + if (len > columnLenMax.at(kField1)) { + columnLenMax.at(kField1) = len + kPrintOffset; } - columns.emplace_back(stringBuf[1]); + columns.emplace_back(stringBuf[kField1]); - len = snprintf(stringBuf[2], sizeof(stringBuf[2]), "%f", iter.second.second / op_cost_total_); - if (len > columnLenMax.at(2)) { - columnLenMax.at(2) = len + 4; + stringBuf[kField2] = to_string(iter.second.second / op_cost_total_); + len = stringBuf[kField2].length(); + if (len > columnLenMax.at(kField2)) { + columnLenMax.at(kField2) = len + kPrintOffset; } - columns.emplace_back(stringBuf[2]); + columns.emplace_back(stringBuf[kField2]); - len = snprintf(stringBuf[3], sizeof(stringBuf[3]), "%d", iter.second.first); - if (len > columnLenMax.at(3)) { - columnLenMax.at(3) = len + 4; + stringBuf[kField3] = to_string(iter.second.first); + len = stringBuf[kField3].length(); + if (len > columnLenMax.at(kField3)) { + columnLenMax.at(kField3) = len + kPrintOffset; } - columns.emplace_back(stringBuf[3]); + columns.emplace_back(stringBuf[kField3]); - len = snprintf(stringBuf[4], sizeof(stringBuf[4]), "%f", iter.second.second); - if (len > columnLenMax.at(4)) { - columnLenMax.at(4) = len + 4; + stringBuf[kField4] = to_string(iter.second.second); + len = stringBuf[kField4].length(); + if (len > columnLenMax.at(kField4)) { + columnLenMax.at(kField4) = len + kPrintOffset; } - columns.emplace_back(stringBuf[4]); + columns.emplace_back(stringBuf[kField4]); rows.push_back(columns); } diff --git a/mindspore/lite/tools/converter/quantizer/quantize_util.cc b/mindspore/lite/tools/converter/quantizer/quantize_util.cc index f841954d409..15669dbd3ec 100644 --- a/mindspore/lite/tools/converter/quantizer/quantize_util.cc +++ b/mindspore/lite/tools/converter/quantizer/quantize_util.cc @@ -55,6 +55,9 @@ using std::vector; namespace mindspore::lite::quant { const std::vector QuantStrategy::conv_types_ = {ops::kNameConv2DFusion, ops::kNameConv2dTransposeFusion}; const std::vector QuantStrategy::mul_types_ = {ops::kNameMatMul, ops::kNameFullConnection}; +constexpr int kDim2 = 2; +constexpr int kDim4 = 4; + QuantStrategy::QuantStrategy(size_t weight_size, size_t conv_weight_quant_channel_threshold) : m_weight_size_(weight_size), m_conv_weight_quant_channel_threshold_(conv_weight_quant_channel_threshold) {} @@ -209,7 +212,7 @@ bool QuantStrategy::CanTensorQuantized(const AnfNodePtr &inputNode) const { } auto weight_shape = utils::cast(abstract_base->GetShapeTrack())->shape(); - if (weight_shape.size() < 2) { // do not quant single dim tensors + if (weight_shape.size() < kDim2) { // do not quant single dim tensors return false; } @@ -222,7 +225,7 @@ bool QuantStrategy::CanTensorQuantized(const AnfNodePtr &inputNode) const { return false; } - if (weight_shape.size() == 4) { // assume Convolution + if (weight_shape.size() == kDim4) { // assume Convolution if (weight_shape[0] <= static_cast(m_conv_weight_quant_channel_threshold_)) { MS_LOG(INFO) << "channel less m_conv_weight_quant_channel_threshold_!" << weight_shape[0]; return false;