Merge Lite and TrainSessions

This commit is contained in:
Emir Haleva 2021-05-30 11:11:49 +03:00
parent 303d4857e8
commit cf061c5ac5
37 changed files with 403 additions and 704 deletions

View File

@ -100,7 +100,7 @@ void NetRunner::InitAndFigureInputs() {
context.device_list_[0].device_type_ = mindspore::lite::DT_CPU;
context.thread_num_ = 2;
session_ = mindspore::session::TrainSession::CreateSession(ms_file_, &context, true);
session_ = mindspore::session::LiteSession::CreateTrainSession(ms_file_, &context, true);
MS_ASSERT(nullptr != session_);
loop_ = mindspore::session::TrainLoop::CreateTrainLoop(session_);

View File

@ -46,7 +46,7 @@ class NetRunner {
float GetLoss() const;
mindspore::tensor::MSTensor *SearchOutputsForSize(size_t size) const;
mindspore::session::TrainSession *session_ = nullptr;
mindspore::session::LiteSession *session_ = nullptr;
mindspore::session::TrainLoop *loop_ = nullptr;
std::shared_ptr<Dataset> train_ds_;

View File

@ -71,7 +71,7 @@ void NetRunner::InitAndFigureInputs() {
context.device_list_[0].device_info_.cpu_device_info_.cpu_bind_mode_ = mindspore::lite::NO_BIND;
context.thread_num_ = 1;
session_ = mindspore::session::TrainSession::CreateTransferSession(ms_backbone_file_, ms_head_file_, &context);
session_ = mindspore::session::LiteSession::CreateTransferSession(ms_backbone_file_, ms_head_file_, &context);
MS_ASSERT(nullptr != session_);
auto inputs = session_->GetInputs();

View File

@ -22,7 +22,7 @@
#include <map>
#include <vector>
#include <string>
#include "include/train/train_session.h"
#include "include/lite_session.h"
#include "include/ms_tensor.h"
#include "src/dataset.h"
@ -43,7 +43,7 @@ class NetRunner {
mindspore::tensor::MSTensor *SearchOutputsForSize(size_t size) const;
DataSet ds_;
mindspore::session::TrainSession *session_ = nullptr;
mindspore::session::LiteSession *session_ = nullptr;
std::string ms_backbone_file_ = "";
std::string ms_head_file_ = "";

View File

@ -20,11 +20,19 @@
#ifndef NOT_USE_STL
#include <unordered_map>
#endif // NOT_USE_STL
#include <vector>
#include <string>
#include "include/ms_tensor.h"
#include "include/model.h"
#include "include/context.h"
#include "include/errorcode.h"
#include "include/lite_types.h"
namespace mindspore {
namespace lite {
class TrainCfg;
}
namespace session {
/// \brief LiteSession defined session in MindSpore Lite for compiling Model and forwarding model.
class MS_API LiteSession {
@ -119,6 +127,89 @@ class MS_API LiteSession {
///
/// \return STATUS as an error code of resize inputs, STATUS is defined in errorcode.h.
virtual int Resize(const Vector<tensor::MSTensor *> &inputs, const Vector<Vector<int>> &dims) = 0;
/// \brief Static method to create a TrainSession object
///
/// \param[in] filename name of flatbuffer that holds the flatbuffer
/// \param[in] context Defines the context of the session to be created
/// \param[in] train_mode training mode to initialize Session with
/// \param[in] cfg training configuration, set to null for default configuration
///
/// \return Pointer of MindSpore LiteSession
static LiteSession *CreateTrainSession(const std::string &filename, const lite::Context *context,
bool train_mode = false, const lite::TrainCfg *cfg = nullptr);
/// \brief Static method to create a TransferSession object
///
/// \param[in] filename_backbone Filename to read backbone net flatbuffer from
/// \param[in] filename_head Filename to read head net flatbuffer from
/// \param[in] context Defines the context of the session to be created
/// \param[in] train_mode training mode to initialize Session with
///
/// \return Pointer of MindSpore LiteSession
static LiteSession *CreateTransferSession(const std::string &filename_backbone, const std::string &filename_head,
const lite::Context *context, bool train_mode = false,
const lite::TrainCfg *cfg = nullptr);
/// \brief Set model to train mode
/// \return STATUS as an error code of compiling graph, STATUS is defined in errorcode.h
virtual int Train() { return mindspore::lite::RET_ERROR; }
/// \brief Check mode of model
///
/// \return boolean indication if model is in train mode
virtual bool IsTrain() { return false; }
/// \brief Set model to eval mode
/// \return STATUS as an error code of compiling graph, STATUS is defined in errorcode.h
virtual int Eval() { return mindspore::lite::RET_OK; }
/// \brief Check mode of model
///
/// \return boolean indication if model is in eval mode
virtual bool IsEval() { return true; }
/// \brief Sets the Learning Rate of the training
///
/// \param[in] learning_rate to set
///
/// \return STATUS as an error code of the set operation, STATUS is defined in errorcode.h
virtual int SetLearningRate(float learning_rate) { return mindspore::lite::RET_ERROR; }
/// \brief Gets the Learning Rate of the training
///
/// \return learning rate. 0.0 if no optimizer was found
virtual float GetLearningRate() { return 0.0; }
/// \brief Setup training with virtual batches
///
/// \param[in] virtual_batch_multiplier - virtual batch multiplier, use any number < 1 to disable
/// \param[in] lr - learning rate to use for virtual batch, -1 for internal configuration
/// \param[in] momentum - batch norm momentum to use for virtual batch, -1 for internal configuration
/// \return STATUS as an error code of the set operation, STATUS is defined in errorcode.h
virtual int SetupVirtualBatch(int virtual_batch_multiplier, float lr = -1.0f, float momentum = -1.0f) {
return mindspore::lite::RET_ERROR;
}
/// \brief Get output MindSpore Lite MSTensors of Training model prediction
///
/// \return a vector of output tensors (MindSpore Lite MSTensor).
virtual std::vector<tensor::MSTensor *> GetPredictions() const {
std::vector<tensor::MSTensor *> outputs;
return outputs;
}
/// \brief Save model
/// \param[in] file_name pretrained model file name prefix. '.ms' extenension is added if does not exist
/// \param[in] model_type indication whether to save full model or only the inference part
/// \param[in] quant_type indication whether to quantize exported model
/// \param[in] format of exported file (currently only FT_FLATBUFFERS is supported)
/// \return STATUS as an error code of the set operation, STATUS is defined in errorcode.h
virtual int Export(const std::string &file_name, lite::ModelType model_type = lite::MT_TRAIN,
lite::QuantizationType quant_type = lite::QT_DEFAULT, lite::FormatType = lite::FT_FLATBUFFERS) {
return mindspore::lite::RET_ERROR;
}
};
} // namespace session
} // namespace mindspore

View File

@ -32,5 +32,21 @@ typedef enum {
DT_NPU /**< NPU device type */
} DeviceType;
typedef enum {
FT_FLATBUFFERS, /**< Flatbuffers format */
FT_PROTOBUF /**< Protobuf format */
} FormatType;
typedef enum {
QT_DEFAULT, /**< the quantization of the original model will apply */
QT_NONE, /**< apply no quantization */
QT_WEIGHT /**< apply weight quantization */
} QuantizationType;
typedef enum {
MT_TRAIN, /**< Both Train and Inference part of the compiled model are serialized */
MT_INFERENCE /**< Only the Inference part of the compiled model is serialized */
} ModelType;
} // namespace mindspore::lite
#endif // MINDSPORE_LITE_INCLUDE_LITE_TYPES_H_

View File

@ -65,22 +65,6 @@ class TrainCfg {
MixPrecisionCfg mix_precision_cfg_; /**< Mix precision configuration */
};
typedef enum {
FT_FLATBUFFER, // Flatbuffer format
FT_MIBDIR // MINDIR format
} FormatType;
typedef enum {
QT_DEFAULT, // the quantization of the original model will apply
QT_NONE, // apply no quantization
QT_WEIGHT // apply weight quantization
} QuantType;
typedef enum {
MT_TRAIN, // Both Train and Inference part of the compiled model are serialized
MT_INFERENCE // Only the Inference part of the compiled model is serialized
} ModelType;
} // namespace lite
} // namespace mindspore
#endif // MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_CFG_H_

View File

@ -22,7 +22,7 @@
#include <unordered_map>
#include "include/train/train_loop_callback.h"
#include "include/train/metrics.h"
#include "include/train/train_session.h"
#include "include/lite_session.h"
namespace mindspore {
class MSTensor;
@ -43,7 +43,7 @@ class TrainLoop {
/// \param[in] train_session Train session object as return from CreateSession\CreateTransferSession API
///
/// \return Pointer of MindSpore Lite TrainLoop
static TrainLoop *CreateTrainLoop(session::TrainSession *train_session);
static TrainLoop *CreateTrainLoop(session::LiteSession *train_session);
/// \brief Class destructor
virtual ~TrainLoop() = default;
@ -53,10 +53,10 @@ class TrainLoop {
/// \return 0 on success or -1 in case of error
virtual int Reset() = 0; // resets the epoch counter to 0.
/// \brief Accessor to the TrainSession
/// \brief Accessor to the LiteSession
///
/// \return pointer of the train_session
const virtual session::TrainSession *train_session() = 0;
const virtual session::LiteSession *train_session() = 0;
/// \brief Initialize object with metrics
///

View File

@ -23,17 +23,17 @@
namespace mindspore {
namespace session {
class TrainSession;
class LiteSession;
class TrainLoop;
struct TrainLoopCallBackData {
TrainLoopCallBackData(bool train_mode, int epoch, TrainSession *session, TrainLoop *loop)
TrainLoopCallBackData(bool train_mode, int epoch, LiteSession *session, TrainLoop *loop)
: train_mode_(train_mode), epoch_(epoch), session_(session), loop_(loop) {}
bool train_mode_; /**< training mode of TrainSession object */
bool train_mode_; /**< training mode of LiteSession object */
unsigned int epoch_; /**< the current training epoch (starts at 0) */
unsigned int step_ = 0; /**< the current step within the epoch */
TrainSession *session_; /**< pointer to the TrainSession */
LiteSession *session_; /**< pointer to the LiteSession */
TrainLoop *loop_;
};

View File

@ -1,118 +0,0 @@
/**
* Copyright 2020 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.
*/
#ifndef MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_SESSION_H_
#define MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_SESSION_H_
#include <vector>
#include <string>
#include <tuple>
#include "include/lite_session.h"
#include "include/errorcode.h"
#include "include/train/train_cfg.h"
namespace mindspore {
namespace session {
/// \brief TrainSession Defines a class that allows training a MindSpore model
class TrainSession : public session::LiteSession {
public:
/// \brief Class destructor
virtual ~TrainSession() = default;
/// \brief Static method to create a TrainSession object
///
/// \param[in] filename name of flatbuffer that holds the flatbuffer
/// \param[in] context Defines the context of the session to be created
/// \param[in] train_mode training mode to initialize Session with
/// \param[in] cfg training configuration, set to null for default configuration
///
/// \return Pointer of MindSpore Lite TrainSession
static TrainSession *CreateSession(const std::string &filename, const lite::Context *context, bool train_mode = false,
const lite::TrainCfg *cfg = nullptr);
/// \brief Static method to create a TrainSession object
///
/// \param[in] filename_backbone Filename to read backbone net flatbuffer from
/// \param[in] filename_head Filename to read head net flatbuffer from
/// \param[in] context Defines the context of the session to be created
/// \param[in] train_mode training mode to initialize Session with
///
/// \return Pointer of MindSpore Lite TrainSession
static TrainSession *CreateTransferSession(const std::string &filename_backbone, const std::string &filename_head,
const lite::Context *context, bool train_mode = false,
const lite::TrainCfg *cfg = nullptr);
/// \brief Set model to train mode
/// \return STATUS as an error code of compiling graph, STATUS is defined in errorcode.h
virtual int Train() = 0;
/// \brief Check mode of model
///
/// \return boolean indication if model is in train mode
bool IsTrain() { return train_mode_ == true; }
/// \brief Set model to eval mode
/// \return STATUS as an error code of compiling graph, STATUS is defined in errorcode.h
virtual int Eval() = 0;
/// \brief Check mode of model
///
/// \return boolean indication if model is in eval mode
bool IsEval() { return train_mode_ == false; }
/// \brief Sets the Learning Rate of the training
///
/// \param[in] learning_rate to set
///
/// \return STATUS as an error code of the set operation, STATUS is defined in errorcode.h
virtual int SetLearningRate(float learning_rate) = 0;
/// \brief Gets the Learning Rate of the training
///
/// \return learning rate. 0.0 if no optimizer was found
virtual float GetLearningRate() = 0;
/// \brief Setup training with virtual batches
///
/// \param[in] virtual_batch_multiplier - virtual batch multiplier, use any number < 1 to disable
/// \param[in] lr - learning rate to use for virtual batch, -1 for internal configuration
/// \param[in] momentum - batch norm momentum to use for virtual batch, -1 for internal configuration
/// \return STATUS as an error code of the set operation, STATUS is defined in errorcode.h
virtual int SetupVirtualBatch(int virtual_batch_multiplier, float lr = -1.0f, float momentum = -1.0f) = 0;
/// \brief Get output MindSpore Lite MSTensors of Training model prediction
///
/// \return a vector of output tensors (MindSpore Lite MSTensor).
virtual std::vector<tensor::MSTensor *> GetPredictions() const = 0;
/// \brief Save model
/// \param[in] file_name pretrained model file name prefix. '.ms' extenension is added if does not exist
/// \param[in] model_type indication whether to save full model or only the inference part
/// \param[in] quant_type indication whether to quantize exported model
/// \param[in] format of exported file (currently only FT_FLATBUFFER is supported)
/// \return STATUS as an error code of the set operation, STATUS is defined in errorcode.h
virtual int Export(const std::string &file_name, lite::ModelType model_type = lite::MT_TRAIN,
lite::QuantType quant_type = lite::QT_DEFAULT, lite::FormatType = lite::FT_FLATBUFFER) {
return mindspore::lite::RET_ERROR;
}
protected:
bool train_mode_ = false;
};
} // namespace session
} // namespace mindspore
#endif // MINDSPORE_LITE_INCLUDE_TRAIN_TRAIN_SESSION_H_

View File

@ -63,6 +63,16 @@ public class LiteSession {
}
}
public static LiteSession createTrainSession(String modelname, final MSConfig config, boolean train_mode) {
LiteSession liteSession = new LiteSession();
liteSession.sessionPtr = liteSession.createTrainSession(modelname, config.getMSConfigPtr(), train_mode, 0);
if (liteSession.sessionPtr == 0) {
return null;
} else {
return liteSession;
}
}
public long getSessionPtr() {
return sessionPtr;
}
@ -145,10 +155,40 @@ public class LiteSession {
return this.resize(this.sessionPtr, inputsArray, dims);
}
public boolean export(String modelFilename, int model_type, int quantization_type) {
return this.export(this.sessionPtr, modelFilename, model_type, quantization_type);
}
public boolean train() {
return this.train(this.sessionPtr);
}
public boolean eval() {
return this.eval(this.sessionPtr);
}
public boolean isTrain() {
return this.isTrain(this.sessionPtr);
}
public boolean isEval() {
return this.isEval(this.sessionPtr);
}
public boolean setLearningRate(float learning_rate) {
return this.setLearningRate(this.sessionPtr, learning_rate);
}
public boolean setupVirtualBatch(int virtualBatchMultiplier, float learningRate, float momentum) {
return this.setupVirtualBatch(this.sessionPtr, virtualBatchMultiplier, learningRate, momentum);
}
private native long createSession(long msConfigPtr);
private native long createSessionWithModel(MappedByteBuffer buffer, long msConfigPtr);
private native long createTrainSession(String filename, long msContextPtr, boolean train_mode, long msTrainCfgPtr);
private native boolean compileGraph(long sessionPtr, long modelPtr);
private native void bindThread(long sessionPtr, boolean if_bind);
@ -170,4 +210,19 @@ public class LiteSession {
private native void free(long sessionPtr);
private native boolean resize(long sessionPtr, long[] inputs, int[][] dims);
private native boolean export(long sessionPtr, String modelFilename, int model_type, int quantization_type);
private native boolean train(long sessionPtr);
private native boolean eval(long sessionPtr);
private native boolean isTrain(long sessionPtr);
private native boolean isEval(long sessionPtr);
private native boolean setLearningRate(long sessionPtr, float learning_rate);
private native boolean setupVirtualBatch(long sessionPtr, int virtualBatchMultiplier, float learningRate, float momentum);
}

View File

@ -1,189 +0,0 @@
/**
* Copyright 2020 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.
*/
package com.mindspore.lite;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.mindspore.lite.config.MSConfig;
public class TrainSession {
static {
System.loadLibrary("mindspore-lite-jni");
}
private long sessionPtr;
public TrainSession() {
this.sessionPtr = 0;
}
public boolean init(String modelFilename, MSConfig config) {
this.sessionPtr = createSession(modelFilename, config.getMSConfigPtr());
return this.sessionPtr != 0;
}
public long getSessionPtr() {
return sessionPtr;
}
public void bindThread(boolean if_bind) {
this.bindThread(this.sessionPtr, if_bind);
}
public boolean runGraph() {
return this.runGraph(this.sessionPtr);
}
public List<MSTensor> getInputs() {
List<Long> ret = this.getInputs(this.sessionPtr);
ArrayList<MSTensor> tensors = new ArrayList<MSTensor>();
for (Long msTensorAddr : ret) {
MSTensor msTensor = new MSTensor(msTensorAddr);
tensors.add(msTensor);
}
return tensors;
}
public MSTensor getInputsByTensorName(String tensorName) {
Long tensorAddr = this.getInputsByTensorName(this.sessionPtr, tensorName);
if (tensorAddr == null) {
return null;
}
MSTensor msTensor = new MSTensor(tensorAddr);
return msTensor;
}
public List<MSTensor> getOutputsByNodeName(String nodeName) {
List<Long> ret = this.getOutputsByNodeName(this.sessionPtr, nodeName);
ArrayList<MSTensor> tensors = new ArrayList<>();
for (Long msTensorAddr : ret) {
MSTensor msTensor = new MSTensor(msTensorAddr);
tensors.add(msTensor);
}
return tensors;
}
public Map<String, MSTensor> getOutputMapByTensor() {
Map<String, Long> ret = this.getOutputMapByTensor(this.sessionPtr);
Map<String, MSTensor> tensorMap = new HashMap<>();
Set<Map.Entry<String, Long>> entrySet = ret.entrySet();
for (Map.Entry<String, Long> entry : entrySet) {
String name = entry.getKey();
Long msTensorAddr = entry.getValue();
tensorMap.put(name, new MSTensor(msTensorAddr));
}
return tensorMap;
}
public List<String> getOutputTensorNames() {
return getOutputTensorNames(this.sessionPtr);
}
public MSTensor getOutputByTensorName(String tensorName) {
Long tensorAddr = getOutputByTensorName(this.sessionPtr, tensorName);
if (tensorAddr == null) {
return null;
}
return new MSTensor(tensorAddr);
}
public void free() {
this.free(this.sessionPtr);
this.sessionPtr = 0;
}
public boolean resize(List<MSTensor> inputs, int[][] dims) {
long[] inputsArray = new long[inputs.size()];
for (int i = 0; i < inputs.size(); i++) {
inputsArray[i] = inputs.get(i).getMSTensorPtr();
}
return this.resize(this.sessionPtr, inputsArray, dims);
}
public boolean saveToFile(String modelFilename) {
return this.saveToFile(this.sessionPtr, modelFilename);
}
public boolean train() {
return this.train(this.sessionPtr);
}
public boolean eval() {
return this.eval(this.sessionPtr);
}
public boolean isTrain() {
return this.isTrain(this.sessionPtr);
}
public boolean isEval() {
return this.isEval(this.sessionPtr);
}
public boolean setLearningRate(float learning_rate) {
return this.setLearningRate(this.sessionPtr, learning_rate);
}
public boolean setupVirtualBatch(int virtualBatchMultiplier, float learningRate, float momentum) {
return this.setupVirtualBatch(this.sessionPtr, virtualBatchMultiplier, learningRate, momentum);
}
public boolean setupVirtualBatch(int virtualBatchMultiplier) {
return this.setupVirtualBatch(this.sessionPtr, virtualBatchMultiplier, -1.0f, -1.0f);
}
private native long createSession(String modelFilename, long msConfigPtr);
private native void bindThread(long sessionPtr, boolean if_bind);
private native boolean runGraph(long sessionPtr);
private native List<Long> getInputs(long sessionPtr);
private native long getInputsByTensorName(long sessionPtr, String tensorName);
private native List<Long> getOutputsByNodeName(long sessionPtr, String nodeName);
private native Map<String, Long> getOutputMapByTensor(long sessionPtr);
private native List<String> getOutputTensorNames(long sessionPtr);
private native long getOutputByTensorName(long sessionPtr, String tensorName);
private native void free(long sessionPtr);
private native boolean resize(long sessionPtr, long[] inputs, int[][] dims);
private native boolean saveToFile(long sessionPtr, String modelFilename);
private native boolean train(long sessionPtr);
private native boolean eval(long sessionPtr);
private native boolean isTrain(long sessionPtr);
private native boolean isEval(long sessionPtr);
private native boolean setLearningRate(long sessionPtr, float learning_rate);
private native boolean setupVirtualBatch(long sessionPtr, int virtualBatchMultiplier, float learningRate, float momentum);
}

View File

@ -63,6 +63,16 @@ public class LiteSession {
}
}
public static LiteSession createTrainSession(String modelname, final MSConfig config, boolean train_mode) {
LiteSession liteSession = new LiteSession();
liteSession.sessionPtr = liteSession.createTrainSession(modelname, config.getMSConfigPtr(), train_mode, 0);
if (liteSession.sessionPtr == 0) {
return null;
} else {
return liteSession;
}
}
public long getSessionPtr() {
return sessionPtr;
}
@ -145,10 +155,40 @@ public class LiteSession {
return this.resize(this.sessionPtr, inputsArray, dims);
}
public boolean export(String modelFilename, int model_type, int quantization_type) {
return this.export(this.sessionPtr, modelFilename, model_type, quantization_type);
}
public boolean train() {
return this.train(this.sessionPtr);
}
public boolean eval() {
return this.eval(this.sessionPtr);
}
public boolean isTrain() {
return this.isTrain(this.sessionPtr);
}
public boolean isEval() {
return this.isEval(this.sessionPtr);
}
public boolean setLearningRate(float learning_rate) {
return this.setLearningRate(this.sessionPtr, learning_rate);
}
public boolean setupVirtualBatch(int virtualBatchMultiplier, float learningRate, float momentum) {
return this.setupVirtualBatch(this.sessionPtr, virtualBatchMultiplier, learningRate, momentum);
}
private native long createSession(long msConfigPtr);
private native long createSessionWithModel(MappedByteBuffer buffer, long msConfigPtr);
private native long createTrainSession(String filename, long msContextPtr, boolean train_mode, long msTrainCfgPtr);
private native boolean compileGraph(long sessionPtr, long modelPtr);
private native void bindThread(long sessionPtr, boolean if_bind);
@ -170,4 +210,18 @@ public class LiteSession {
private native void free(long sessionPtr);
private native boolean resize(long sessionPtr, long[] inputs, int[][] dims);
private native boolean export(long sessionPtr, String modelFilename, int model_type, int quantization_type);
private native boolean train(long sessionPtr);
private native boolean eval(long sessionPtr);
private native boolean isTrain(long sessionPtr);
private native boolean isEval(long sessionPtr);
private native boolean setLearningRate(long sessionPtr, float learning_rate);
private native boolean setupVirtualBatch(long sessionPtr, int virtualBatchMultiplier, float learningRate, float momentum);
}

View File

@ -63,9 +63,10 @@ set(LITE_SO_NAME mindspore-lite)
if(SUPPORT_TRAIN)
set(LITE_SO_NAME mindspore-lite-train)
set(JNI_SRC
${JNI_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/runtime/train_session.cpp
${JNI_SRC}
${CMAKE_CURRENT_SOURCE_DIR}/runtime/train_session.cpp
)
endif()
add_library(mindspore-lite-jni SHARED ${JNI_SRC})

View File

@ -274,3 +274,98 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_resize
int ret = lite_session_ptr->Resize(c_inputs, c_dims);
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_export(JNIEnv *env, jobject thiz,
jlong session_ptr,
jstring model_name,
jint model_type,
jint quantization_type) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *lite_session_ptr = static_cast<mindspore::session::LiteSession *>(session_pointer);
auto ret = lite_session_ptr->Export(env->GetStringUTFChars(model_name, JNI_FALSE),
static_cast<mindspore::lite::ModelType>(model_type),
static_cast<mindspore::lite::QuantizationType>(quantization_type));
return (jboolean)(ret == 0);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_train(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *lite_session_ptr = static_cast<mindspore::session::LiteSession *>(session_pointer);
auto ret = lite_session_ptr->Train();
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_eval(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *lite_session_ptr = static_cast<mindspore::session::LiteSession *>(session_pointer);
auto ret = lite_session_ptr->Eval();
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_isTrain(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *lite_session_ptr = static_cast<mindspore::session::LiteSession *>(session_pointer);
auto ret = lite_session_ptr->IsTrain();
return (jboolean)(ret);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_isEval(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *lite_session_ptr = static_cast<mindspore::session::LiteSession *>(session_pointer);
auto ret = lite_session_ptr->IsEval();
return (jboolean)(ret);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_setLearningRate(JNIEnv *env, jobject thiz,
jlong session_ptr,
jfloat learning_rate) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *lite_session_ptr = static_cast<mindspore::session::LiteSession *>(session_pointer);
auto ret = lite_session_ptr->SetLearningRate(learning_rate);
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_LiteSession_setupVirtualBatch(JNIEnv *env, jobject thiz,
jlong session_ptr,
jint virtualBatchMultiplier,
jfloat learningRate,
jfloat momentum) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *lite_session_ptr = static_cast<mindspore::session::LiteSession *>(session_pointer);
auto ret = lite_session_ptr->SetupVirtualBatch(virtualBatchMultiplier, learningRate, momentum);
return (jboolean)(ret == mindspore::lite::RET_OK);
}

View File

@ -17,19 +17,23 @@
#include <jni.h>
#include "common/ms_log.h"
#include "common/jni_utils.h"
#include "include/train/train_session.h"
#include "include/lite_session.h"
#include "include/train/train_cfg.h"
#include "include/errorcode.h"
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_TrainSession_createSession(JNIEnv *env, jobject thiz,
jstring model_file_name,
jlong ms_config_ptr) {
auto *pointer = reinterpret_cast<void *>(ms_config_ptr);
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_LiteSession_createTrainSession(JNIEnv *env, jobject thiz,
jstring file_name,
jlong ms_context_ptr,
jboolean train_mode) {
auto *pointer = reinterpret_cast<void *>(ms_context_ptr);
if (pointer == nullptr) {
MS_LOGE("Context pointer from java is nullptr");
return jlong(nullptr);
}
auto *lite_context_ptr = static_cast<mindspore::lite::Context *>(pointer);
auto session = mindspore::session::TrainSession::CreateSession(JstringToChar(env, model_file_name), lite_context_ptr);
auto session = mindspore::session::LiteSession::CreateTrainSession(env->GetStringUTFChars(file_name, JNI_FALSE),
lite_context_ptr, train_mode, nullptr);
if (session == nullptr) {
MS_LOGE("CreateSession failed");
return jlong(nullptr);
@ -37,284 +41,4 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_TrainSession_createSe
return jlong(session);
}
extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_TrainSession_bindThread(JNIEnv *env, jobject thiz,
jlong session_ptr, jboolean if_bind) {
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
train_session_ptr->BindThread(if_bind);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_runGraph(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
auto ret = train_session_ptr->RunGraph();
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jobject JNICALL Java_com_mindspore_lite_TrainSession_getInputs(JNIEnv *env, jobject thiz,
jlong session_ptr) {
jclass array_list = env->FindClass("java/util/ArrayList");
jmethodID array_list_construct = env->GetMethodID(array_list, "<init>", "()V");
jobject ret = env->NewObject(array_list, array_list_construct);
jmethodID array_list_add = env->GetMethodID(array_list, "add", "(Ljava/lang/Object;)Z");
jclass long_object = env->FindClass("java/lang/Long");
jmethodID long_object_construct = env->GetMethodID(long_object, "<init>", "(J)V");
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return ret;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
auto inputs = train_session_ptr->GetInputs();
for (auto input : inputs) {
jobject tensor_addr = env->NewObject(long_object, long_object_construct, jlong(input));
env->CallBooleanMethod(ret, array_list_add, tensor_addr);
}
return ret;
}
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_TrainSession_getInputsByTensorName(JNIEnv *env, jobject thiz,
jlong session_ptr,
jstring tensor_name) {
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return jlong(nullptr);
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
auto input = train_session_ptr->GetInputsByTensorName(JstringToChar(env, tensor_name));
return jlong(input);
}
extern "C" JNIEXPORT jobject JNICALL Java_com_mindspore_lite_TrainSession_getOutputsByNodeName(JNIEnv *env,
jobject thiz,
jlong session_ptr,
jstring node_name) {
jclass array_list = env->FindClass("java/util/ArrayList");
jmethodID array_list_construct = env->GetMethodID(array_list, "<init>", "()V");
jobject ret = env->NewObject(array_list, array_list_construct);
jmethodID array_list_add = env->GetMethodID(array_list, "add", "(Ljava/lang/Object;)Z");
jclass long_object = env->FindClass("java/lang/Long");
jmethodID long_object_construct = env->GetMethodID(long_object, "<init>", "(J)V");
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return ret;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
auto inputs = train_session_ptr->GetOutputsByNodeName(JstringToChar(env, node_name));
for (auto input : inputs) {
jobject tensor_addr = env->NewObject(long_object, long_object_construct, jlong(input));
env->CallBooleanMethod(ret, array_list_add, tensor_addr);
}
return ret;
}
extern "C" JNIEXPORT jobject JNICALL Java_com_mindspore_lite_TrainSession_getOutputMapByTensor(JNIEnv *env,
jobject thiz,
jlong session_ptr) {
jclass hash_map_clazz = env->FindClass("java/util/HashMap");
jmethodID hash_map_construct = env->GetMethodID(hash_map_clazz, "<init>", "()V");
jobject hash_map = env->NewObject(hash_map_clazz, hash_map_construct);
jmethodID hash_map_put =
env->GetMethodID(hash_map_clazz, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return hash_map;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
auto outputs = train_session_ptr->GetOutputs();
jclass long_object = env->FindClass("java/lang/Long");
jmethodID long_object_construct = env->GetMethodID(long_object, "<init>", "(J)V");
for (auto output_iter : outputs) {
auto node_name = output_iter.first;
auto ms_tensor = output_iter.second;
jobject tensor_addr = env->NewObject(long_object, long_object_construct, jlong(ms_tensor));
env->CallObjectMethod(hash_map, hash_map_put, env->NewStringUTF(node_name.c_str()), tensor_addr);
}
return hash_map;
}
extern "C" JNIEXPORT jobject JNICALL Java_com_mindspore_lite_TrainSession_getOutputTensorNames(JNIEnv *env,
jobject thiz,
jlong session_ptr) {
jclass array_list = env->FindClass("java/util/ArrayList");
jmethodID array_list_construct = env->GetMethodID(array_list, "<init>", "()V");
jobject ret = env->NewObject(array_list, array_list_construct);
jmethodID array_list_add = env->GetMethodID(array_list, "add", "(Ljava/lang/Object;)Z");
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return ret;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
auto output_names = train_session_ptr->GetOutputTensorNames();
for (auto output_name : output_names) {
env->CallBooleanMethod(ret, array_list_add, env->NewStringUTF(output_name.c_str()));
}
return ret;
}
extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_TrainSession_getOutputByTensorName(JNIEnv *env, jobject thiz,
jlong session_ptr,
jstring tensor_name) {
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return jlong(nullptr);
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
auto output = train_session_ptr->GetOutputByTensorName(JstringToChar(env, tensor_name));
return jlong(output);
}
extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_TrainSession_free(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
delete (train_session_ptr);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_resize(JNIEnv *env, jobject thiz,
jlong session_ptr, jlongArray inputs,
jobjectArray dims) {
std::vector<std::vector<int>> c_dims;
auto *pointer = reinterpret_cast<void *>(session_ptr);
if (pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(pointer);
jsize input_size = static_cast<int>(env->GetArrayLength(inputs));
jlong *input_data = env->GetLongArrayElements(inputs, nullptr);
std::vector<mindspore::tensor::MSTensor *> c_inputs;
for (int i = 0; i < input_size; i++) {
auto *tensor_pointer = reinterpret_cast<void *>(input_data[i]);
if (tensor_pointer == nullptr) {
MS_LOGE("Tensor pointer from java is nullptr");
return false;
}
auto *ms_tensor_ptr = static_cast<mindspore::tensor::MSTensor *>(tensor_pointer);
c_inputs.push_back(ms_tensor_ptr);
}
jsize tensor_size = static_cast<int>(env->GetArrayLength(dims));
for (int i = 0; i < tensor_size; i++) {
jintArray array = static_cast<jintArray>(env->GetObjectArrayElement(dims, i));
jsize dim_size = static_cast<int>(env->GetArrayLength(array));
jint *dim_data = env->GetIntArrayElements(array, nullptr);
std::vector<int> tensor_dims;
for (int j = 0; j < dim_size; j++) {
tensor_dims.push_back(dim_data[j]);
}
c_dims.push_back(tensor_dims);
}
int ret = train_session_ptr->Resize(c_inputs, c_dims);
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_saveToFile(JNIEnv *env, jobject thiz,
jlong session_ptr,
jstring model_file_name) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(session_pointer);
auto ret = train_session_ptr->Export(JstringToChar(env, model_file_name));
return (jboolean)(ret == 0);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_train(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(session_pointer);
auto ret = train_session_ptr->Train();
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_eval(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(session_pointer);
auto ret = train_session_ptr->Eval();
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_isTrain(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(session_pointer);
auto ret = train_session_ptr->IsTrain();
return (jboolean)(ret);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_isEval(JNIEnv *env, jobject thiz,
jlong session_ptr) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(session_pointer);
auto ret = train_session_ptr->IsEval();
return (jboolean)(ret);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_setLearningRate(JNIEnv *env, jobject thiz,
jlong session_ptr,
jfloat learning_rate) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(session_pointer);
auto ret = train_session_ptr->SetLearningRate(learning_rate);
return (jboolean)(ret == mindspore::lite::RET_OK);
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_TrainSession_setupVirtualBatch(JNIEnv *env, jobject thiz,
jlong session_ptr,
jint virtualBatchMultiplier,
jfloat learningRate,
jfloat momentum) {
auto *session_pointer = reinterpret_cast<void *>(session_ptr);
if (session_pointer == nullptr) {
MS_LOGE("Session pointer from java is nullptr");
return (jboolean) false;
}
auto *train_session_ptr = static_cast<mindspore::session::TrainSession *>(session_pointer);
auto ret = train_session_ptr->SetupVirtualBatch(virtualBatchMultiplier, learningRate, momentum);
return (jboolean)(ret == mindspore::lite::RET_OK);
}

View File

@ -25,7 +25,6 @@ namespace mindspore::kernel {
using mindspore::lite::RET_ERROR;
using mindspore::lite::RET_OK;
#ifdef SUPPORT_TRAIN
void *InnerKernel::workspace_ = nullptr;
void InnerKernel::AllocWorkspace(size_t size) {
@ -43,8 +42,6 @@ void InnerKernel::FreeWorkspace() {
workspace_ = nullptr;
}
#endif
int InnerKernel::PreProcess() {
if (!InferShapeDone()) {
auto ret = lite::KernelInferShape(in_tensors_, out_tensors_, op_parameter_);

View File

@ -185,13 +185,11 @@ class InnerKernel : public Kernel {
void set_registry_data_type(TypeId data_type) { registry_data_type_ = data_type; }
#ifdef SUPPORT_TRAIN
void set_workspace_size(size_t value) { workspace_size_ = value; }
size_t workspace_size() { return workspace_size_; }
static void AllocWorkspace(size_t size);
static void FreeWorkspace();
void *workspace() { return workspace_; }
#endif
protected:
OpParameter *op_parameter_ = nullptr;
@ -201,10 +199,8 @@ class InnerKernel : public Kernel {
bool train_mode_ = false;
bool trainable_ = false; // parameters of this Kernel are trained in Train Session
TypeId registry_data_type_ = kTypeUnknown;
#ifdef SUPPORT_TRAIN
size_t workspace_size_ = 0;
static void *workspace_;
#endif
};
} // namespace mindspore::kernel

View File

@ -464,10 +464,10 @@ int LiteSession::CompileGraph(Model *model) {
is_running_.store(false);
return ret;
}
#ifndef SUPPORT_TRAIN
// For reducing runtime RAM, free packop weight because packop will pack weight and will not access to origin weight
FreePackOpWeight(kernels_);
#endif
if (!is_train_session_) {
// For reducing runtime RAM, free packop weight because packop will pack weight and will not access to origin weight
FreePackOpWeight(kernels_);
}
is_running_.store(false);
return RET_OK;
} // namespace lite

View File

@ -36,13 +36,6 @@ OpParameter *PopulateTileParameter(const void *prim) {
}
memset(tile_param, 0, sizeof(TileParameter));
tile_param->op_parameter_.type_ = schema::PrimitiveType_TileFusion;
#ifdef SUPPORT_TRAIN
auto multiples = tile_prim->multiples();
tile_param->in_dim_ = multiples->size();
for (int i = 0; i < tile_param->in_dim_; ++i) {
tile_param->multiples_[i] = *(multiples->begin() + i);
}
#else
if (tile_prim->dims() != nullptr) {
auto dims = tile_prim->dims();
if (dims == nullptr) {
@ -57,7 +50,6 @@ OpParameter *PopulateTileParameter(const void *prim) {
tile_param->dims_size_ = dims->size();
}
#endif
return reinterpret_cast<OpParameter *>(tile_param);
}
} // namespace

View File

@ -139,7 +139,7 @@ int KernelInterfaceRegistry::Reg(const std::string &provider, int op_type, Kerne
MS_LOG(ERROR) << "malloc kernel dev delegate creator fail!";
return RET_ERROR;
}
memset(kernel_creators_[provider], 0, kMaxKernelNum * sizeof(KernelInterfaceCreator));
memset(reinterpret_cast<void *>(kernel_creators_[provider]), 0, kMaxKernelNum * sizeof(KernelInterfaceCreator));
}
kernel_creators_[provider][op_type] = creator;

View File

@ -52,7 +52,8 @@ int RegistryKernelImpl::RegCustomKernel(const std::string &arch, const std::stri
MS_LOG(ERROR) << "malloc custom kernel creator fail!provider: " << provider << ", arch: " << arch;
return RET_ERROR;
}
memset(custom_kernel_creators_[provider][arch][type], 0, data_type_length_ * sizeof(CreateKernel));
memset(reinterpret_cast<void *>(custom_kernel_creators_[provider][arch][type]), 0,
data_type_length_ * sizeof(CreateKernel));
}
int data_type_index = data_type - kNumberTypeBegin - 1;
@ -74,7 +75,7 @@ int RegistryKernelImpl::RegKernel(const std::string &arch, const std::string &pr
MS_LOG(ERROR) << "malloc kernel creator buffer fail! provider: " << provider << ",arch:" << arch;
return RET_ERROR;
}
memset(kernel_creators_[provider][arch], 0, kKernelMaxNum * sizeof(CreateKernel));
memset(reinterpret_cast<void *>(kernel_creators_[provider][arch]), 0, kKernelMaxNum * sizeof(CreateKernel));
} else {
auto iter_arch = iter->second.find(arch);
if (iter_arch == iter->second.end()) {
@ -83,7 +84,7 @@ int RegistryKernelImpl::RegKernel(const std::string &arch, const std::string &pr
MS_LOG(ERROR) << "malloc kernel creator buffer fail! provider: " << provider << ",arch:" << arch;
return RET_ERROR;
}
memset(iter->second[arch], 0, kKernelMaxNum * sizeof(CreateKernel));
memset(reinterpret_cast<void *>(iter->second[arch]), 0, kKernelMaxNum * sizeof(CreateKernel));
}
}

View File

@ -17,9 +17,8 @@
#include "include/train/classification_train_accuracy_monitor.h"
#include <sys/stat.h>
#include <vector>
#include "include/errorcode.h"
#include "src/common/log_adapter.h"
#include "include/train/train_session.h"
#include "include/lite_session.h"
#include "src/common/utils.h"
#include "src/train/train_utils.h"

View File

@ -20,7 +20,7 @@
#include <utility>
#include <vector>
#include <iostream>
#include "include/train/train_session.h"
#include "include/lite_session.h"
#include "src/common/utils.h"
#include "src/tensor.h"

View File

@ -23,7 +23,7 @@
#include <fstream>
#include <memory>
#include "include/errorcode.h"
#include "include/train/train_session.h"
#include "include/lite_session.h"
#include "src/common/utils.h"
#include "src/tensor.h"

View File

@ -274,7 +274,8 @@ int TrainExport::AddTransformNode() {
int TrainExport::ExportNet(const std::vector<mindspore::kernel::LiteKernel *> &kernels,
const std::vector<mindspore::lite::Tensor *> &tensors,
const std::vector<std::string> &output_names, const Model *model, QuantType quant_type) {
const std::vector<std::string> &output_names, const Model *model,
QuantizationType quant_type) {
std::vector<size_t> map_index;
std::set<size_t> out_set;
int offset = meta_graph_->allTensors.size();

View File

@ -41,7 +41,7 @@ class TrainExport {
virtual ~TrainExport();
int ExportNet(const std::vector<mindspore::kernel::LiteKernel *> &kernels,
const std::vector<mindspore::lite::Tensor *> &tensors, const std::vector<std::string> &output_names,
const Model *model, QuantType quant_type);
const Model *model, QuantizationType quant_type);
int ExportInit(const std::string model_name, std::string version);
int SaveToFile();
void set_connect(const std::unordered_map<size_t, size_t> &map) { connect_ = map; }
@ -71,7 +71,7 @@ class TrainExport {
virtual int QuantTensorData(schema::TensorT *dest_tensor, const mindspore::lite::Tensor *src_tensor);
mindspore::schema::QuantType GetNodeQuantType(const mindspore::kernel::LiteKernel *kernel);
void TagQuantizedNodes();
QuantType quant_type_;
QuantizationType quant_type_;
};
}; // namespace lite
} // namespace mindspore

View File

@ -20,7 +20,6 @@
#include <memory>
#include <algorithm>
#include "include/errorcode.h"
#include "include/train/train_session.h"
#include "include/dataset/iterator.h"
#include "src/common/log_adapter.h"
@ -168,7 +167,7 @@ int TrainLoop::LoadPartialData(std::vector<tensor::MSTensor *> inputs, dataset::
} // namespace lite
session::TrainLoop *session::TrainLoop::CreateTrainLoop(session::TrainSession *train_session) {
session::TrainLoop *session::TrainLoop::CreateTrainLoop(session::LiteSession *train_session) {
auto loop = new (std::nothrow) lite::TrainLoop(train_session);
return loop;
}

View File

@ -32,9 +32,9 @@ namespace lite {
class TrainLoop : virtual public session::TrainLoop {
public:
explicit TrainLoop(session::TrainSession *session) : train_session_(session) {}
explicit TrainLoop(session::LiteSession *session) : train_session_(session) {}
const session::TrainSession *train_session() override { return train_session_; }
const session::LiteSession *train_session() override { return train_session_; }
int Reset() override {
epoch_ = 0;
@ -65,7 +65,7 @@ class TrainLoop : virtual public session::TrainLoop {
static int LoadData(std::vector<tensor::MSTensor *> inputs, dataset::MSTensorVec *dataset_vec);
static int LoadPartialData(std::vector<tensor::MSTensor *> inputs, dataset::MSTensorVec *dataset_vec);
session::TrainSession *train_session_ = nullptr;
session::LiteSession *train_session_ = nullptr;
unsigned int epoch_ = 0;
KernelCallBack before_cb_ = nullptr;
KernelCallBack after_cb_ = nullptr;

View File

@ -449,8 +449,9 @@ bool TrainSession::IsBN(kernel::LiteKernel *kernel) const {
(kernel->type() == schema::PrimitiveType_FusedBatchNorm));
}
int TrainSession::Export(const std::string &file_name, ModelType model_type, QuantType quant_type, FormatType format) {
if (format != FT_FLATBUFFER) {
int TrainSession::Export(const std::string &file_name, ModelType model_type, QuantizationType quant_type,
FormatType format) {
if (format != FT_FLATBUFFERS) {
MS_LOG(ERROR) << "Currently only flatbuffer format is supported";
return RET_ERROR;
}
@ -481,8 +482,8 @@ int TrainSession::Export(const std::string &file_name, ModelType model_type, Qua
} // namespace lite
session::TrainSession *session::TrainSession::CreateSession(const std::string &fn, const lite::Context *context,
bool train_mode, const lite::TrainCfg *cfg) {
session::LiteSession *session::LiteSession::CreateTrainSession(const std::string &fn, const lite::Context *context,
bool train_mode, const lite::TrainCfg *cfg) {
auto session = new (std::nothrow) lite::TrainSession();
if (session == nullptr) {
MS_LOG(ERROR) << "create session failed";

View File

@ -21,7 +21,7 @@
#include <unordered_map>
#include <memory>
#include <map>
#include "include/train/train_session.h"
#include "include/train/train_cfg.h"
#include "src/lite_session.h"
/*
@ -44,7 +44,7 @@ namespace mindspore {
namespace lite {
std::unique_ptr<char[]> ReadFileToBuf(const std::string &filename, size_t *size);
using CreatorOp = std::tuple<mindspore::kernel::KernelKey, mindspore::kernel::KernelCreator>;
class TrainSession : virtual public session::TrainSession, virtual public lite::LiteSession {
class TrainSession : virtual public lite::LiteSession {
public:
TrainSession();
~TrainSession();
@ -89,7 +89,7 @@ class TrainSession : virtual public session::TrainSession, virtual public lite::
}
return outputs;
}
int Export(const std::string &fb_name, ModelType model_type, QuantType quant_type, FormatType) override;
int Export(const std::string &fb_name, ModelType model_type, QuantizationType quant_type, FormatType) override;
protected:
void AllocWorkSpace();
@ -138,6 +138,7 @@ class TrainSession : virtual public session::TrainSession, virtual public lite::
std::map<Tensor *, Tensor *> restored_origin_tensors_;
int virtual_batch_idx_ = 0;
int virtual_batch_multiplier_ = 0;
bool train_mode_ = false;
};
} // namespace lite

View File

@ -179,9 +179,9 @@ std::unordered_map<size_t, size_t> TransferSession::ConnectionMap() {
return map;
}
int TransferSession::Export(const std::string &filename, ModelType model_type, QuantType quant_type,
int TransferSession::Export(const std::string &filename, ModelType model_type, QuantizationType quant_type,
FormatType format) {
if (format != FT_FLATBUFFER) {
if (format != FT_FLATBUFFERS) {
MS_LOG(ERROR) << "Currently only flatbuffer format is supported";
return RET_ERROR;
}
@ -228,10 +228,10 @@ int TransferSession::Export(const std::string &filename, ModelType model_type, Q
} // namespace lite
static session::TrainSession *CreateTransferSessionInt(const char *model_buf_backbone, size_t size_backbone,
const char *model_buf_head, size_t size_head,
const lite::Context *context, bool train_mode,
const lite::TrainCfg *cfg) {
static session::LiteSession *CreateTransferSessionInt(const char *model_buf_backbone, size_t size_backbone,
const char *model_buf_head, size_t size_head,
const lite::Context *context, bool train_mode,
const lite::TrainCfg *cfg) {
auto ValidModelSize = [](size_t size) -> bool {
constexpr size_t MaxModelSize = 1024 * 1024 * 1024ULL; // 1G B
return size < MaxModelSize && size > 0;
@ -295,10 +295,10 @@ static session::TrainSession *CreateTransferSessionInt(const char *model_buf_bac
return session;
}
session::TrainSession *session::TrainSession::CreateTransferSession(const std::string &filename_backbone,
const std::string &filename_head,
const lite::Context *ctxt, bool train_mode,
const lite::TrainCfg *cfg) {
session::LiteSession *session::LiteSession::CreateTransferSession(const std::string &filename_backbone,
const std::string &filename_head,
const lite::Context *ctxt, bool train_mode,
const lite::TrainCfg *cfg) {
size_t size_head = 0;
size_t size_backbone = 0;
auto buf_head = lite::ReadFileToBuf(filename_head, &size_head);

View File

@ -61,7 +61,7 @@ class TransferSession : public lite::TrainSession {
mindspore::tensor::MSTensor *GetInputsByTensorName(const std::string &tensor_name) const override;
int CompileTransferGraph();
int Export(const std::string &fb_name, ModelType model_type, QuantType quant_type, FormatType) override;
int Export(const std::string &fb_name, ModelType model_type, QuantizationType quant_type, FormatType) override;
protected:
lite::LiteSession *backbone_session_ = nullptr;

View File

@ -24,9 +24,10 @@
#include "schema/inner/model_generated.h"
#include "common/common_test.h"
#include "include/train/train_session.h"
#include "include/lite_session.h"
#include "include/context.h"
#include "include/errorcode.h"
#include "include/train/train_cfg.h"
#include "src/common/log_adapter.h"
#include "src/common/file_utils.h"
#include "src/kernel_registry.h"
@ -42,8 +43,8 @@ class NetworkTest : public mindspore::CommonTest {
int32_t runNet(mindspore::session::LiteSession *session, const std::string &in, const std::string &out,
const char *tensor_name, bool debug = false);
int32_t fileIterator(mindspore::session::TrainSession *session, const std::string &path,
std::function<int32_t(mindspore::session::TrainSession *session, const std::string &)> cb) {
int32_t fileIterator(mindspore::session::LiteSession *session, const std::string &path,
std::function<int32_t(mindspore::session::LiteSession *session, const std::string &)> cb) {
int32_t res = 0;
if (auto dir = opendir(path.c_str())) {
while (auto f = readdir(dir)) {
@ -101,7 +102,7 @@ TEST_F(NetworkTest, efficient_net) {
context->thread_num_ = 1;
std::string net = "./test_data/nets/effnetb0_fwd_nofuse.ms";
auto session = session::TrainSession::CreateSession(net, context, false);
auto session = session::LiteSession::CreateTrainSession(net, context, false);
ASSERT_NE(session, nullptr);
std::string in = "./test_data/nets/effNet_input_x_1_3_224_224.bin";
@ -147,7 +148,7 @@ TEST_F(NetworkTest, noname) {
context.device_list_[0].device_info_.cpu_device_info_.cpu_bind_mode_ = lite::NO_BIND;
context.thread_num_ = 1;
auto session = mindspore::session::TrainSession::CreateSession(net, &context);
auto session = mindspore::session::LiteSession::CreateTrainSession(net, &context);
ASSERT_NE(session, nullptr);
auto tensors_map = session->GetOutputs();
@ -168,7 +169,7 @@ TEST_F(NetworkTest, setname) {
lite::TrainCfg train_cfg;
train_cfg.loss_name_ = "nhwc";
auto session = mindspore::session::TrainSession::CreateSession(net, &context, true, &train_cfg);
auto session = mindspore::session::LiteSession::CreateTrainSession(net, &context, true, &train_cfg);
ASSERT_NE(session, nullptr);
auto tensors_map = session->GetOutputs();

View File

@ -30,6 +30,7 @@
#include "src/runtime/runtime_api.h"
#include "include/version.h"
#include "include/model.h"
#include "include/train/train_cfg.h"
namespace mindspore {
namespace lite {
@ -221,7 +222,7 @@ int NetTrain::CompareOutput(const session::LiteSession &lite_session) {
}
}
int NetTrain::MarkPerformance(session::TrainSession *session) {
int NetTrain::MarkPerformance(session::LiteSession *session) {
MS_LOG(INFO) << "Running train loops...";
std::cout << "Running train loops..." << std::endl;
uint64_t time_min = 0xFFFFFFFFFFFFFFFF;
@ -331,21 +332,19 @@ int NetTrain::CreateAndRunNetwork(const std::string &filename, int train_session
}
session::LiteSession *session = nullptr;
session::TrainSession *t_session = nullptr;
if (train_session) {
MS_LOG(INFO) << "CreateSession from model file" << filename.c_str();
std::cout << "CreateSession from model file " << filename.c_str() << std::endl;
t_session = session::TrainSession::CreateSession(filename, &context, true, &train_cfg);
if (t_session == nullptr) {
MS_LOG(ERROR) << "RunNetTrain CreateSession failed while running " << model_name.c_str();
std::cout << "RunNetTrain CreateSession failed while running " << model_name.c_str() << std::endl;
MS_LOG(INFO) << "CreateTrainSession from model file" << filename.c_str();
std::cout << "CreateTrainSession from model file " << filename.c_str() << std::endl;
session = session::LiteSession::CreateTrainSession(filename, &context, true, &train_cfg);
if (session == nullptr) {
MS_LOG(ERROR) << "RunNetTrain CreateTrainSession failed while running " << model_name.c_str();
std::cout << "RunNetTrain CreateTrainSession failed while running " << model_name.c_str() << std::endl;
return RET_ERROR;
}
if (epochs > 0) {
t_session->Train();
session->Train();
}
session = t_session;
} else {
std::string filenamems = filename;
if (filenamems.substr(filenamems.find_last_of(".") + 1) != "ms") {
@ -386,19 +385,18 @@ int NetTrain::CreateAndRunNetwork(const std::string &filename, int train_session
return status;
}
if ((epochs > 0) && (t_session != nullptr)) {
status = MarkPerformance(t_session);
if ((epochs > 0) && train_session) {
status = MarkPerformance(session);
if (status != RET_OK) {
MS_LOG(ERROR) << "Run MarkPerformance error: " << status;
std::cout << "Run MarkPerformance error: " << status << std::endl;
return status;
}
SaveModels(t_session); // save file if flags are on
SaveModels(session); // save file if flags are on
}
if (!flags_->data_file_.empty()) {
if (t_session != nullptr) {
t_session->Eval();
}
session->Eval();
status = MarkAccuracy(session, check_accuracy);
if (status != RET_OK) {
MS_LOG(ERROR) << "Run MarkAccuracy error: " << status;
@ -426,7 +424,7 @@ int NetTrain::RunNetTrain() {
return RET_OK;
}
int NetTrain::SaveModels(session::TrainSession *session) {
int NetTrain::SaveModels(session::LiteSession *session) {
if (!flags_->export_file_.empty()) {
auto status = session->Export(flags_->export_file_ + "_qt", lite::MT_TRAIN, lite::QT_WEIGHT);
if (status != RET_OK) {

View File

@ -34,7 +34,7 @@
#include "tools/common/flag_parser.h"
#include "src/common/file_utils.h"
#include "src/common/utils.h"
#include "include/train/train_session.h"
#include "include/lite_session.h"
namespace mindspore::lite {
enum MS_API DataType { kImage = 0, kBinary = 1 };
@ -192,11 +192,11 @@ class MS_API NetTrain {
return meanError;
}
int MarkPerformance(session::TrainSession *session);
int MarkPerformance(session::LiteSession *session);
int MarkAccuracy(session::LiteSession *lite_session, bool enforce_accuracy = true);
int CompareOutput(const session::LiteSession &lite_session);
int SaveModels(session::TrainSession *session);
int SaveModels(session::LiteSession *session);
int CheckExecutionOfSavedModels();
NetTrainFlags *flags_;

View File

@ -72,7 +72,7 @@ class Flags : public virtual mindspore::lite::FlagParser {
TypeId outputDataType;
// used for quantization
std::string quantTypeStr;
QuantType quantType;
schema::QuantType quantType;
std::string inputDataTypeStr;
std::string outputDataTypeStr;
// used for post-trainning-weight