Skip to content

Commit

Permalink
load data for the model
Browse files Browse the repository at this point in the history
  • Loading branch information
datduonguva committed Oct 4, 2023
1 parent 36d67a6 commit d076862
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 3 deletions.
12 changes: 9 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
OBJ=./objects


all: mkdir simple_rnn test3
# TODO: I am here
all: mkdir simple_rnn test3 rnn_generation

simple_rnn: ${OBJ}/ggml.o ${OBJ}/simple_rnn.o
g++ -o simple_rnn_main ${OBJ}/simple_rnn.o ${OBJ}/ggml.o -lm -lpthread
rnn_generation: ${OBJ}/ggml.o ${OBJ}/rnn_text_gen.o
g++ -o rnn_generation_main ${OBJ}/rnn_text_gen.o ${OBJ}/ggml.o -lm -lpthread

test3: mkdir ${OBJ}/ggml.o ${OBJ}/test3.o
g++ -o test3_main ${OBJ}/test3.o ${OBJ}/ggml.o -lm -pthread
Expand All @@ -21,7 +22,12 @@ ${OBJ}/simple_rnn.o: simple_rnn/simple_rnn.cpp
${OBJ}/test3.o: tests/test-mul-mat3.cpp
g++ -I. -o ${OBJ}/test3.o -c tests/test-mul-mat3.cpp

${OBJ}/rnn_text_gen.o: rnn_text_gen/rnn_text_generation.cpp
g++ -I. -o ${OBJ}/rnn_text_gen.o -c rnn_text_gen/rnn_text_generation.cpp


.PHONY: clean


clean:
rm -f ${OBJ}/*.o
114 changes: 114 additions & 0 deletions rnn_text_gen/rnn_text_generation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <ggml.h>
#include <vector>
#include <string>
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;
/**
Layer: my_model/embedding/embeddings:0 with shape (66, 256)
Layer: my_model/gru/gru_cell/kernel:0 with shape (256, 3072)
Layer: my_model/gru/gru_cell/recurrent_kernel:0 with shape (1024, 3072)
Layer: my_model/gru/gru_cell/bias:0 with shape (2, 3072)
Layer: my_model/dense/kernel:0 with shape (1024, 66)
Layer: my_model/dense/bias:0 with shape (66,)
**/

void print_2d(ggml_tensor * ts){
for (int i1 = 0; i1 < ts->ne[1]; i1++){
for (int i0 = 0; i0 < ts->ne[0]; i0++){
cout << ggml_get_f32_1d(ts, i1*ts->ne[0] + i0) << " ";
}
cout << endl;
}
}


struct rnn_generator {
struct ggml_context * ctx;
struct ggml_tensor * embeddings;
struct ggml_tensor * cell_kernel;
struct ggml_tensor * cell_recurrent_kernel;
struct ggml_tensor * cell_bias;
struct ggml_tensor * dense_kernel;
struct ggml_tensor * dense_bias;
};

struct rnn_generator load_model(){
struct ggml_init_params params = {
.mem_size = 128 * 1024 * 1024,
.mem_buffer = NULL,
.no_alloc = false
};
struct ggml_context *ctx = ggml_init(params);
struct rnn_generator model;

model.ctx = ctx;

model.embeddings = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, 256, 66);
model.cell_kernel = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, 3072, 256);
model.cell_recurrent_kernel = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, 3072, 1024);
model.cell_bias = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, 3072, 2);
model.dense_kernel = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, 66, 1024);
model.dense_bias = ggml_new_tensor_2d(model.ctx, GGML_TYPE_F32, 66, 1);

auto fin = ifstream("rnn_text_gen/gru.bin", ios::binary);

if (! fin) cout << "Error reading file" << endl;
int dummy;

// Read the embeddings
for (int i = 0; i < 3; i++) {
fin.read((char*)&dummy, sizeof(dummy));
cout << dummy << endl;
}
fin.read((char*)model.embeddings->data, 256*66*sizeof(float));

// Read the cell kernel
for (int i = 0; i < 3; i++) {
fin.read((char*)&dummy, sizeof(dummy));
cout << dummy << endl;
}
fin.read((char*)model.cell_kernel->data, 3072*256*sizeof(float));

// Read the cell recurrent kernel
for (int i = 0; i < 3; i++) {
fin.read((char*)&dummy, sizeof(dummy));
cout << dummy << endl;
}
fin.read((char*)model.cell_recurrent_kernel->data, 3072*1024*sizeof(float));

// Read the cell bias
for (int i = 0; i < 3; i++) {
fin.read((char*)&dummy, sizeof(dummy));
cout << dummy << endl;
}
fin.read((char*)model.cell_bias->data, 3072*2*sizeof(float));

// Read the dense kernel
for (int i = 0; i < 3; i++) {
fin.read((char*)&dummy, sizeof(dummy));
cout << dummy << endl;
}
fin.read((char*)model.dense_kernel->data, 66*1024*sizeof(float));


// Read the dense kernel
for (int i = 0; i < 2; i++){
fin.read((char*)&dummy, sizeof(dummy));
cout << dummy << endl;
}
fin.read((char*)model.dense_bias->data, 66*1*sizeof(float));

print_2d(model.dense_bias);

fin.close();

return model;
}

int main(){

struct rnn_generator my_model = load_model();
}

0 comments on commit d076862

Please sign in to comment.