From d317f37a76d6654d9de500c8afce1f0b37aefa8b Mon Sep 17 00:00:00 2001 From: "-T.K.-" Date: Tue, 15 Oct 2024 15:24:13 -0700 Subject: [PATCH] FIX: fix simple example --- examples/simple/main.c | 50 +++++++++++++++++---------------- examples/simple/model.bin | 2 +- examples/simple/model.pth | Bin 2316 -> 2316 bytes examples/simple/scripts/run.py | 10 +++++-- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/examples/simple/main.c b/examples/simple/main.c index 1c72f82..6ec230d 100644 --- a/examples/simple/main.c +++ b/examples/simple/main.c @@ -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[]; @@ -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); } @@ -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); diff --git a/examples/simple/model.bin b/examples/simple/model.bin index 6797f84..223968e 100644 --- a/examples/simple/model.bin +++ b/examples/simple/model.bin @@ -1 +1 @@ -裍>~KɋپcS>@k;za>BQp>2k \ No newline at end of file +p>2k裍>~KɋپcS>@k;za>BQ \ No newline at end of file diff --git a/examples/simple/model.pth b/examples/simple/model.pth index 35b668828ecab5e03ea0d250a21386b5d1c7c7f6..8f23055024fda254fcfd1b9627fec60e79f2f2ce 100644 GIT binary patch delta 78 zcmeAX>Jgfd!`QhoHV0K85sn4 bvvXM9|95k;0lNf5oe#STBMVrlgkuH(pz#<5 delta 78 zcmeAX>Jgfd!`QJgH&mX!&cXR*{WNtzoO8JU=w7+4sYn;V!Ln;Qjq bvvY8sxDq_sfL#Kj&WBxvkp(PN!Z8B?x@s4p diff --git a/examples/simple/scripts/run.py b/examples/simple/scripts/run.py index 927a595..6fd36d4 100644 --- a/examples/simple/scripts/run.py +++ b/examples/simple/scripts/run.py @@ -2,6 +2,7 @@ import torch import torch.nn as nn + torch.manual_seed(0) class Simple(nn.Module): @@ -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") @@ -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