@ -33,8 +33,7 @@ const int kFileOpSuccess = 0;
namespace ge {
Status FileSaver : : OpenFile ( int32_t & fd , const std : : string & file_path ) {
if ( CheckPath ( file_path ) ! = SUCCESS ) {
GELOGE ( FAILED , " [Check][FilePath]Check output file failed, file_path:%s. " , file_path . c_str ( ) ) ;
REPORT_CALL_ERROR ( " E19999 " , " Check output file failed, file_path:%s. " , file_path . c_str ( ) ) ;
GELOGE ( FAILED , " Check output file failed. " ) ;
return FAILED ;
}
@ -46,8 +45,7 @@ Status FileSaver::OpenFile(int32_t &fd, const std::string &file_path) {
fd = mmOpen2 ( real_path , M_RDWR | M_CREAT | O_TRUNC , mode ) ;
if ( fd = = EN_INVALID_PARAM | | fd = = EN_ERROR ) {
// -1: Failed to open file; - 2: Illegal parameter
GELOGE ( FAILED , " [Open][File]Failed. mmpa_errno = %d, %s " , fd , strerror ( errno ) ) ;
REPORT_INNER_ERROR ( " E19999 " , " Open file failed, mmpa_errno = %d, error:%s. " , fd , strerror ( errno ) ) ;
GELOGE ( FAILED , " Open file failed. mmpa_errno = %d, %s " , fd , strerror ( errno ) ) ;
return FAILED ;
}
return SUCCESS ;
@ -64,9 +62,7 @@ Status FileSaver::WriteData(const void *data, uint32_t size, int32_t fd) {
while ( size > size_1g ) {
write_count = mmWrite ( fd , reinterpret_cast < void * > ( seek ) , size_1g ) ;
if ( write_count = = EN_INVALID_PARAM | | write_count = = EN_ERROR ) {
GELOGE ( FAILED , " [Write][Data]Failed, mmpa_errorno = %ld, error:%s " , write_count , strerror ( errno ) ) ;
REPORT_INNER_ERROR ( " E19999 " , " Write data failed, mmpa_errorno = %ld, error:%s. " ,
write_count , strerror ( errno ) ) ;
GELOGE ( FAILED , " Write data failed. mmpa_errorno = %ld, %s " , write_count , strerror ( errno ) ) ;
return FAILED ;
}
size - = size_1g ;
@ -79,9 +75,7 @@ Status FileSaver::WriteData(const void *data, uint32_t size, int32_t fd) {
// -1: Failed to write to file; - 2: Illegal parameter
if ( write_count = = EN_INVALID_PARAM | | write_count = = EN_ERROR ) {
GELOGE ( FAILED , " [Write][Data]Failed. mmpa_errorno = %ld, error:%s " , write_count , strerror ( errno ) ) ;
REPORT_INNER_ERROR ( " E19999 " , " Write data failed, mmpa_errorno = %ld, error:%s. " ,
write_count , strerror ( errno ) ) ;
GELOGE ( FAILED , " Write data failed. mmpa_errorno = %ld, %s " , write_count , strerror ( errno ) ) ;
return FAILED ;
}
@ -91,8 +85,7 @@ Status FileSaver::WriteData(const void *data, uint32_t size, int32_t fd) {
Status FileSaver : : SaveWithFileHeader ( const std : : string & file_path , const ModelFileHeader & file_header , const void * data ,
int len ) {
if ( data = = nullptr | | len < = 0 ) {
GELOGE ( FAILED , " [Check][Param]Failed, model_data is null or the length[%d] is less than 1. " , len ) ;
REPORT_INNER_ERROR ( " E19999 " , " Save file failed, model_data is null or the length:%d is less than 1. " , len ) ;
GELOGE ( FAILED , " Model_data is null or the length[%d] less than 1. " , len ) ;
return FAILED ;
}
@ -111,8 +104,7 @@ Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFi
} while ( 0 ) ;
// Close file
if ( mmClose ( fd ) ! = 0 ) { // mmClose 0: success
GELOGE ( FAILED , " [Close][File]Failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
REPORT_INNER_ERROR ( " E19999 " , " Close file failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
GELOGE ( FAILED , " Close file failed. " ) ;
ret = FAILED ;
}
return ret ;
@ -148,95 +140,60 @@ Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFi
}
} while ( 0 ) ;
// Close file
if ( mmClose ( fd ) ! = EN_OK ) {
GELOGE ( FAILED , " [Close][File]Failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
REPORT_CALL_ERROR ( " E19999 " , " Close file failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
ret = FAILED ;
}
GE_CHK_BOOL_RET_STATUS ( mmClose ( fd ) = = EN_OK , FAILED , " Close file failed. " ) ;
return ret ;
}
Status FileSaver : : SaveToBuffWithFileHeader ( const ModelFileHeader & file_header ,
ModelPartitionTable & model_partition_table ,
const std : : vector < ModelPartition > & partition_datas ,
ge : : ModelBufferData & model ) {
const vector < ModelPartitionTable * > model_partition_tables = { & model_partition_table } ;
const std : : vector < std : : vector < ModelPartition > > all_partition_datas = { partition_datas } ;
return SaveToBuffWithFileHeader ( file_header , model_partition_tables , all_partition_datas , model ) ;
}
Status FileSaver : : SaveToBuffWithFileHeader ( const ModelFileHeader & file_header ,
const vector < ModelPartitionTable * > & model_partition_tables ,
const std : : vector < std : : vector < ModelPartition > > & all_partition_datas ,
const std : : vector < ModelPartition > & partitionDatas ,
ge : : ModelBufferData & model ) {
GE_CHK_BOOL_RET_STATUS ( model_partition_tables . size ( ) = = all_partition_datas . size ( ) , PARAM_INVALID ,
" Model table size %zu does not match partition size %zu. " ,
model_partition_tables . size ( ) , all_partition_datas . size ( ) ) ;
for ( size_t index = 0 ; index < model_partition_tables . size ( ) ; + + index ) {
auto & cur_partiton_data = all_partition_datas [ index ] ;
auto & cur_model_partition_table = * model_partition_tables [ index ] ;
GE_CHK_BOOL_RET_STATUS ( ! cur_partiton_data . empty ( ) & & cur_model_partition_table . num ! = 0
& & cur_model_partition_table . num = = cur_partiton_data . size ( ) , FAILED ,
" Invalid param: partition data size is (%zu), model_partition_table.num is (%u). " ,
cur_partiton_data . size ( ) , cur_model_partition_table . num ) ;
GE_CHK_BOOL_RET_STATUS (
! partitionDatas . empty ( ) & & model_partition_table . num ! = 0 & & model_partition_table . num = = partitionDatas . size ( ) ,
FAILED , " Invalid param:partition data size is (%u), model_partition_table.num is (%zu). " ,
model_partition_table . num , partitionDatas . size ( ) ) ;
uint32_t model_header_size = sizeof ( ModelFileHeader ) ;
uint32_t table_size = static_cast < uint32_t > ( SIZE_OF_MODEL_PARTITION_TABLE ( model_partition_table ) ) ;
uint32_t total_size = model_header_size + table_size ;
for ( const auto & partitionData : partitionDatas ) {
auto ret = ge : : CheckUint32AddOverflow ( total_size , partitionData . size ) ;
GE_CHK_BOOL_RET_STATUS ( ret = = SUCCESS , FAILED , " add uint32 overflow! " ) ;
total_size = total_size + partitionData . size ;
}
uint64_t model_header_size = sizeof ( ModelFileHeader ) ;
uint64_t total_size = model_header_size ;
for ( size_t index = 0 ; index < model_partition_tables . size ( ) ; + + index ) {
auto & cur_model_partition_table = * model_partition_tables [ index ] ;
total_size + = static_cast < uint64_t > ( SIZE_OF_MODEL_PARTITION_TABLE ( cur_model_partition_table ) ) ;
auto & cur_partition_data = all_partition_datas [ index ] ;
for ( const auto & partition_data : cur_partition_data ) {
auto ret = ge : : CheckUint64AddOverflow ( total_size , partition_data . size ) ;
GE_CHK_BOOL_RET_STATUS ( ret = = SUCCESS , FAILED , " Add uint64 overflow! " ) ;
total_size + = partition_data . size ;
}
}
// save to buff
auto buff = reinterpret_cast < uint8_t * > ( malloc ( total_size ) ) ;
GE_CHK_BOOL_RET_STATUS ( buff ! = nullptr , FAILED , " M alloc failed!" ) ;
GE_PRINT_DYNAMIC_MEMORY ( malloc , " F ile buffer." , total_size )
GE_CHK_BOOL_RET_STATUS ( buff ! = nullptr , FAILED , " malloc failed! " ) ;
GE_PRINT_DYNAMIC_MEMORY ( malloc , " file buffer. " , total_size )
model . data . reset ( buff , [ ] ( uint8_t * buff ) {
GELOGD ( " Free online model memory. " ) ;
free ( buff ) ;
buff = nullptr ;
} ) ;
model . length = total_size ;
uint 64 _t left_space = total_size ;
auto ret_mem = memcpy_s ( buff , left_space , reinterpret_cast < void * > ( const_cast < ModelFileHeader * > ( & file_header ) ) ,
model_header_size ) ;
GE_CHK_BOOL_RET_STATUS ( ret_mem = = EOK , FAILED , " M emcpy_s failed!" ) ;
uint32_t left_space = total_size ;
auto ret_mem1 = memcpy_s ( buff , left_space , reinterpret_cast < void * > ( const_cast < ModelFileHeader * > ( & file_header ) ) ,
model_header_size ) ;
GE_CHK_BOOL_RET_STATUS ( ret_mem1 = = 0 , FAILED , " memcpy_s failed! " ) ;
buff + = model_header_size ;
left_space - = model_header_size ;
for ( size_t index = 0 ; index < model_partition_tables . size ( ) ; + + index ) {
auto & cur_tabel = * model_partition_tables [ index ] ;
uint64_t table_size = static_cast < uint64_t > ( SIZE_OF_MODEL_PARTITION_TABLE ( cur_tabel ) ) ;
ret_mem = memcpy_s ( buff , left_space , reinterpret_cast < void * > ( & cur_tabel ) , table_size ) ;
GE_CHK_BOOL_RET_STATUS ( ret_mem = = EOK , FAILED , " Memcpy_s failed! " ) ;
buff + = table_size ;
left_space - = table_size ;
auto & cur_partition_data = all_partition_datas [ index ] ;
for ( const auto & partition_data : cur_partition_data ) {
ret_mem = memcpy_s ( buff , left_space , reinterpret_cast < void * > ( const_cast < uint8_t * > ( partition_data . data ) ) ,
partition_data . size ) ;
GE_CHK_BOOL_RET_STATUS ( ret_mem = = EOK , FAILED , " Memcpy_s failed! " ) ;
buff + = partition_data . size ;
left_space - = partition_data . size ;
}
auto ret_mem2 = memcpy_s ( buff , left_space , reinterpret_cast < void * > ( & model_partition_table ) , table_size ) ;
GE_CHK_BOOL_RET_STATUS ( ret_mem2 = = 0 , FAILED , " memcpy_s failed! " ) ;
buff + = table_size ;
left_space - = table_size ;
for ( const auto & partitionData : partitionDatas ) {
auto ret_mem3 = memcpy_s ( buff , left_space , reinterpret_cast < void * > ( const_cast < uint8_t * > ( partitionData . data ) ) ,
partitionData . size ) ;
GE_CHK_BOOL_RET_STATUS ( ret_mem3 = = 0 , FAILED , " memcpy failed! " ) ;
buff + = partitionData . size ;
left_space - = partitionData . size ;
}
return SUCCESS ;
}
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver : : CheckPath ( const std : : string & file_path ) {
// Determine file path length
if ( file_path . size ( ) > = MMPA_MAX_PATH ) {
GELOGE ( FAILED , " [Check][FilePath]Failed, file path's length:%zu > mmpa_max_path:%d " ,
file_path . size ( ) , MMPA_MAX_PATH ) ;
REPORT_INNER_ERROR ( " E19999 " , " Check file path failed, file path's length:%zu > mmpa_max_path:%d " ,
file_path . size ( ) , MMPA_MAX_PATH ) ;
GELOGE ( FAILED , " Path is too long:%zu " , file_path . size ( ) ) ;
return FAILED ;
}
@ -255,7 +212,7 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver::CheckPath(con
// If there is a path before the file name, create the path
if ( path_split_pos ! = - 1 ) {
if ( CreateDirectory ( std : : string ( file_path ) . substr ( 0 , static_cast < size_t > ( path_split_pos ) ) ) ! = kFileOpSuccess ) {
GELOGE ( FAILED , " [Create][Directory]F ailed, file path:%s." , file_path . c_str ( ) ) ;
GELOGE ( FAILED , " CreateDirectory f ailed, file path:%s." , file_path . c_str ( ) ) ;
return FAILED ;
}
}
@ -266,8 +223,7 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver::CheckPath(con
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
FileSaver : : SaveToFile ( const string & file_path , const ge : : ModelData & model , const ModelFileHeader * model_file_header ) {
if ( file_path . empty ( ) | | model . model_data = = nullptr | | model . model_len = = 0 ) {
GELOGE ( FAILED , " [Save][File]Incorrect input param, file_path is empty or model_data is nullptr or model_len is 0 " ) ;
REPORT_INNER_ERROR ( " E19999 " , " Save file failed, at least one of the input parameters(file_path, model_data, model_len) is incorrect " ) ;
GELOGE ( FAILED , " Incorrected input param. file_path.empty() || model.model_data == nullptr || model.model_len == 0 " ) ;
return FAILED ;
}
@ -284,8 +240,7 @@ FileSaver::SaveToFile(const string &file_path, const ge::ModelData &model, const
const Status ret = SaveWithFileHeader ( file_path , file_header , model . model_data , file_header . length ) ;
if ( ret ! = SUCCESS ) {
GELOGE ( FAILED , " [Save][File]Failed, file_path:%s, file_header_len:%u, error_code:%u. " ,
file_path . c_str ( ) , file_header . length , ret ) ;
GELOGE ( FAILED , " Save file failed, file_path:%s, file header len:%u. " , file_path . c_str ( ) , file_header . length ) ;
return FAILED ;
}
@ -350,7 +305,7 @@ Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFi
// Write partition data
auto & cur_partition_datas = all_partition_datas [ index ] ;
for ( const auto & partition_data : cur_partition_datas ) {
GELOGI ( " part_ size[%u]" , partition_data . size ) ;
GELOGI ( " GC: size[%u]" , partition_data . size ) ;
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG (
WriteData ( static_cast < const void * > ( partition_data . data ) , partition_data . size , fd ) ! = SUCCESS , ret = FAILED ;
break ) ;
@ -358,19 +313,14 @@ Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFi
}
} while ( 0 ) ;
// Close file
if ( mmClose ( fd ) ! = 0 ) { // mmClose 0: success
GELOGE ( FAILED , " [Close][File]Failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
REPORT_CALL_ERROR ( " E19999 " , " Close file failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
ret = FAILED ;
}
GE_CHK_BOOL_RET_STATUS ( mmClose ( fd ) = = EN_OK , FAILED , " Close file failed. " ) ;
return ret ;
}
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver : : SaveToFile ( const string & file_path , const void * data ,
int len ) {
if ( data = = nullptr | | len < = 0 ) {
GELOGE ( FAILED , " [Check][Param]Failed, model_data is null or the length[%d] is less than 1. " , len ) ;
REPORT_INNER_ERROR ( " E19999 " , " Save file failed, the model_data is null or its length:%d is less than 1. " , len ) ;
GELOGE ( FAILED , " Model_data is null or the length[%d] less than 1. " , len ) ;
return FAILED ;
}
@ -385,8 +335,7 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver::SaveToFile(co
// Close file
if ( mmClose ( fd ) ! = 0 ) { // mmClose 0: success
GELOGE ( FAILED , " [Close][File]Failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
REPORT_CALL_ERROR ( " E19999 " , " Close file failed, error_code:%u errmsg:%s " , ret , strerror ( errno ) ) ;
GELOGE ( FAILED , " Close file failed. " ) ;
ret = FAILED ;
}
return ret ;