add free for context

This commit is contained in:
ningshanwutuobang 2023-06-06 22:57:04 +08:00
parent 9c6117cd8d
commit ba1f617d7d
4 changed files with 11 additions and 0 deletions

View file

@ -23,6 +23,7 @@ bool eval_id(struct MyModel* mymodel, int id);
bool eval_string(struct MyModel* mymodel, const char* str); bool eval_string(struct MyModel* mymodel, const char* str);
const char* sampling(struct MyModel* mymodel); const char* sampling(struct MyModel* mymodel);
llama_token sampling_id(struct MyModel* mymodel); llama_token sampling_id(struct MyModel* mymodel);
void free_mymodel(struct MyModel* mymodel);
} }

View file

@ -17,6 +17,8 @@ class MyModel:
args_c = (c_char_p * argc)(*c_str) args_c = (c_char_p * argc)(*c_str)
self.model = c_void_p(libc.create_mymodel(argc, args_c)) self.model = c_void_p(libc.create_mymodel(argc, args_c))
# print("self.model", self.model) # print("self.model", self.model)
def __del__(self):
libc.free_mymodel(self.model)
def eval_float(self, x): def eval_float(self, x):
libc.eval_float(self.model, x.astype(np.float32).ctypes.data_as(POINTER(c_float)), x.shape[0]) libc.eval_float(self.model, x.astype(np.float32).ctypes.data_as(POINTER(c_float)), x.shape[0])

View file

@ -101,6 +101,13 @@ struct MyModel* create_mymodel(int argc, char ** argv) {
return ret; return ret;
} }
void free_mymodel(struct MyModel* mymodel) {
llama_context* ctx = mymodel->ctx;
llama_print_timings(ctx);
llama_free(ctx);
delete mymodel;
}
bool eval_float(void* model, float* input, int N){ bool eval_float(void* model, float* input, int N){
MyModel* mymodel = (MyModel* )model; MyModel* mymodel = (MyModel* )model;

View file

@ -27,5 +27,6 @@ int main(int argc, char** argv) {
// eval_id(mymodel, id); // eval_id(mymodel, id);
} }
printf("\n"); printf("\n");
free_mymodel(mymodel);
return 0; return 0;
} }