Skip to content

Commit

Permalink
FIX: fix simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
T-K-233 committed Oct 15, 2024
1 parent 88548c5 commit d317f37
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
50 changes: 26 additions & 24 deletions examples/simple/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "nn.h"

// load the weight data block from the model.bin file
INCLUDE_FILE(".rodata", "../model.bin", weights);
INCLUDE_FILE(".rodata", "./model.bin", weights);
extern uint8_t weights_data[];
extern size_t weights_start[];
extern size_t weights_end[];
Expand All @@ -21,36 +21,38 @@ extern size_t weights_end[];


// Tensors can be defined either globally or locally
Tensor A;
Tensor B;
Tensor C;
Tensor D;
Tensor2D_F32 A;
Tensor2D_F32 B;
Tensor2D_F32 C;
Tensor1D_F32 D;

/**
* Initialize the required tensors for the model
*/
void init(Tensor *A, Tensor *B, Tensor *C, Tensor *D) {
nn_init_tensor(A, 2, (size_t[]){3, 3}, DTYPE_F32, (float *)malloc(9 * sizeof(float)));
nn_init_tensor(B, 2, (size_t[]){3, 3}, DTYPE_F32, (float *)(weights_data + 3 * sizeof(float)));
nn_init_tensor(C, 2, (size_t[]){3, 3}, DTYPE_F32, (float *)malloc(9 * sizeof(float)));
nn_init_tensor(D, 1, (size_t[]){3}, DTYPE_F32, (float *)(weights_data + 0 * sizeof(float)));
void init(Tensor2D_F32 *A, Tensor2D_F32 *B, Tensor2D_F32 *C, Tensor1D_F32 *D) {
A->shape[0] = 3; A->shape[1] = 3;
A->data = (float *)malloc(9 * sizeof(float));
B->shape[0] = 3; B->shape[1] = 3;
B->data = (float *)(weights_data + 3 * sizeof(float));
C->shape[0] = 3; C->shape[1] = 3;
C->data = (float *)malloc(9 * sizeof(float));
D->shape[0] = 3;
D->data = (float *)(weights_data + 0 * sizeof(float));
}

/**
* Deinitialize the tensors used for the model
*/
void deinit(Tensor *A, Tensor *B, Tensor *C, Tensor *D) {
nn_freeTensor(A);
nn_freeTensor(B);
nn_freeTensor(C);
nn_freeTensor(D);
void deinit(Tensor2D_F32 *A, Tensor2D_F32 *B, Tensor2D_F32 *C, Tensor1D_F32 *D) {
free(A->data);
free(C->data);
}

/**
* Forward pass of the model
*/
void forward(Tensor *C, Tensor *A, Tensor *B, Tensor *D) {
nn_Linear_F32(C, A, B, D);
void forward(Tensor2D_F32 *C, Tensor2D_F32 *A, Tensor2D_F32 *B, Tensor1D_F32 *D) {
nn_addmm_f32(C, A, B, D);
}


Expand All @@ -59,25 +61,25 @@ int main() {

// load the input data to the tensor
float input_data[] = {
1., 2., 3.,
1., 2., 3.,
1., 2., 3.,
1.0, 2.0, 3.0,
1.0, 2.0, 3.0,
1.0, 2.0, 3.0,
};
memcpy(A.data, input_data, 9 * sizeof(float));

forward(&C, &A, &B, &D);

printf("A:\n");
nn_printf(&A);
nn_print_tensor2d_f32(&A);

printf("B:\n");
nn_printf(&B);
nn_print_tensor2d_f32(&B);

printf("C:\n");
nn_printf(&C);
nn_print_tensor2d_f32(&C);

printf("D:\n");
nn_printf(&D);
nn_print_tensor1d_f32(&D);

deinit(&A, &B, &C, &D);

Expand Down
2 changes: 1 addition & 1 deletion examples/simple/model.bin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
裍���>~K�ɋپ��c�S�>@k;�za�>B�Q��p>��2��k
�p>��2��k�裍���>~K�ɋپ��c�S�>@k;�za�>B�Q�
Binary file modified examples/simple/model.pth
Binary file not shown.
10 changes: 7 additions & 3 deletions examples/simple/scripts/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import torch
import torch.nn as nn


torch.manual_seed(0)

class Simple(nn.Module):
Expand All @@ -20,8 +21,10 @@ def forward(self, x: torch.Tensor):
# Create model
model = Simple(dim=3)

model.eval()

# Save model
torch.save(model, "model.pth")
# torch.save(model, "model.pth")

# Load model
# model = torch.load("model.pth")
Expand All @@ -39,9 +42,10 @@ def forward(self, x: torch.Tensor):
b1_flat = b1.astype(np.float32).flatten()

with open("model.bin", "wb") as f:
f.write(w1_flat.tobytes())
# data here is ordered as bias first and then weight,
# following the XNNPACK convention
f.write(b1_flat.tobytes())

f.write(w1_flat.tobytes())


# Test model
Expand Down

0 comments on commit d317f37

Please sign in to comment.