Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

use pointer when load ffm model and release model with Explicit method #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion ffm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,22 @@ bool check_same_txt_bin(string txt_path, string bin_path) {

} // unnamed namespace

ffm_model::~ffm_model() {
//ffm_model::~ffm_model() {
// if(W != nullptr) {
//#ifndef USESSE
// free(W);
//#else
// #ifdef _WIN32
// _aligned_free(W);
// #else
// free(W);
// #endif
//#endif
// W = nullptr;
// }
//}

void ffm_model::release() {
if(W != nullptr) {
#ifndef USESSE
free(W);
Expand Down Expand Up @@ -682,6 +697,31 @@ ffm_model ffm_load_model(string path) {
return model;
}

ffm_model* ffm_load_model_ptr(string path) {
ifstream f_in(path, ios::in | ios::binary);

ffm_model* model;
model = (ffm_model*)malloc(sizeof(struct ffm_model));
f_in.read(reinterpret_cast<char*>(&(model->n)), sizeof(ffm_int));
f_in.read(reinterpret_cast<char*>(&(model->m)), sizeof(ffm_int));
f_in.read(reinterpret_cast<char*>(&(model->k)), sizeof(ffm_int));
f_in.read(reinterpret_cast<char*>(&(model->normalization)), sizeof(bool));

ffm_long w_size = get_w_size(*model);
model->W = malloc_aligned_float(w_size);
// f_in.read(reinterpret_cast<char*>(model.W), sizeof(ffm_float) * w_size);
// Need to write chunk by chunk because some compiler use int32 and will overflow when w_size * 4 > MAX_INT

for(ffm_long offset = 0; offset < w_size; ) {
ffm_long next_offset = min(w_size, offset + (ffm_long) sizeof(ffm_float) * kCHUNK_SIZE);
ffm_long size = next_offset - offset;
f_in.read(reinterpret_cast<char*>(model->W+offset), sizeof(ffm_float) * size);
offset = next_offset;
}

return model;
}

ffm_float ffm_predict(ffm_node *begin, ffm_node *end, ffm_model &model) {
ffm_float r = 1;
if(model.normalization) {
Expand Down
5 changes: 4 additions & 1 deletion ffm.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ struct ffm_model {
ffm_int k; // number of latent factors
ffm_float *W = nullptr;
bool normalization;
~ffm_model();
//~ffm_model();
void release();
};

struct ffm_parameter {
Expand All @@ -42,6 +43,8 @@ void ffm_save_model(ffm_model &model, string path);

ffm_model ffm_load_model(string path);

ffm_model* ffm_load_model_ptr(string path);

ffm_model ffm_train_on_disk(string Tr_path, string Va_path, ffm_parameter param);

ffm_float ffm_predict(ffm_node *begin, ffm_node *end, ffm_model &model);
Expand Down