-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
385 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(diffuse-loco LANGUAGES C) | ||
|
||
|
||
add_executable(diffuse-loco main.c) | ||
target_include_directories(diffuse-loco PUBLIC inc) | ||
|
||
target_compile_features(diffuse-loco INTERFACE c_std_11) | ||
|
||
if (X86) | ||
message("diffuse-loco: building for x86") | ||
target_link_libraries(diffuse-loco PUBLIC target-x86) | ||
|
||
elseif (RISCV) | ||
message("diffuse-loco: building for RISC-V") | ||
# CMake toolchain definition for RISC-V GCC toolchain | ||
set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "") | ||
set(CMAKE_SYSTEM_PROCESSOR "riscv" CACHE STRING "") | ||
|
||
set(TOOLCHAIN_PREFIX "riscv64-unknown-elf-") | ||
|
||
set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc") | ||
set(CMAKE_ASM_COMPILER "${TOOLCHAIN_PREFIX}gcc") | ||
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++") | ||
set(CMAKE_AR "${TOOLCHAIN_PREFIX}ar") | ||
set(CMAKE_LINKER "{TOOLCHAIN_PREFIX}ld") | ||
set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy") | ||
set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size") | ||
set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}ld") | ||
|
||
target_link_libraries(diffuse-loco PUBLIC target-riscv) | ||
endif () | ||
|
||
add_compile_options(-O3 -Wall -Wextra) | ||
|
||
target_compile_options(diffuse-loco PRIVATE -u _printf_float) | ||
|
||
add_subdirectory(../../ ./build/) | ||
|
||
add_subdirectory(../../nn/ ./build/nn) | ||
target_link_libraries(diffuse-loco PUBLIC nn) | ||
|
||
target_link_libraries(diffuse-loco PUBLIC m) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# DiffuseLoco | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @file main.c | ||
* | ||
* A simple example demonstrating C = A * B + D | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "rv.h" | ||
#include "nn.h" | ||
#include "model.h" | ||
|
||
|
||
// static void enable_vector_operations() { | ||
// unsigned long mstatus; | ||
// asm volatile("csrr %0, mstatus" : "=r"(mstatus)); | ||
// mstatus |= 0x00000600 | 0x00006000 | 0x00018000; | ||
// asm volatile("csrw mstatus, %0"::"r"(mstatus)); | ||
// } | ||
|
||
int main() { | ||
|
||
// enable_vector_operations(); | ||
|
||
Model *model = malloc(sizeof(Model)); | ||
|
||
size_t cycles; | ||
|
||
printf("initalizing model...\n"); | ||
init(model); | ||
|
||
printf("setting input data...\n"); | ||
NN_fill(&model->input_1, 1.0); | ||
|
||
// cycles = READ_CSR("mcycle"); | ||
forward(model); | ||
// cycles = READ_CSR("mcycle") - cycles; | ||
|
||
printf("cycles: %lu\n", cycles); | ||
|
||
// output tensor([[ 0.0258, -0.0050, 0.0902, -0.0022, -0.0924, -0.0574, 0.0328, 0.0386, -0.0277, 0.0788, 0.0603, -0.0085]]) | ||
|
||
printf("output:\n"); | ||
NN_printf(&model->actor_6); | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#ifndef __MODEL_H | ||
#define __MODEL_H | ||
|
||
#include "nn.h" | ||
|
||
|
||
// load the weight data block from the model.bin file | ||
INCLUDE_FILE(".rodata", "../model.bin", model_weight); | ||
extern uint8_t model_weight_data[]; | ||
extern size_t model_weight_start[]; | ||
extern size_t model_weight_end[]; | ||
|
||
typedef struct { | ||
Tensor input_1; | ||
Tensor actor_0_weight; | ||
Tensor actor_0_bias; | ||
Tensor actor_0; | ||
Tensor actor_1; | ||
Tensor actor_2_weight; | ||
Tensor actor_2_bias; | ||
Tensor actor_2; | ||
Tensor actor_3; | ||
Tensor actor_4_weight; | ||
Tensor actor_4_bias; | ||
Tensor actor_4; | ||
Tensor actor_5; | ||
Tensor actor_6_weight; | ||
Tensor actor_6_bias; | ||
Tensor actor_6; | ||
|
||
} Model; | ||
|
||
|
||
void init(Model *model); | ||
|
||
void forward(Model *model); | ||
|
||
/** | ||
* Initialize the required tensors for the model | ||
*/ | ||
void init(Model *model) { | ||
float *array_pointer = (float *)model_weight_data; | ||
|
||
NN_initTensor(&model->input_1, 2, (size_t[]){1, 48}, DTYPE_F32, NULL); | ||
|
||
// <class 'torch.nn.modules.linear.Linear'>: actor_0 | ||
NN_initTensor(&model->actor_0_weight, 2, (size_t[]){512, 48}, DTYPE_F32, array_pointer); | ||
array_pointer += 24576; | ||
NN_initTensor(&model->actor_0_bias, 1, (size_t[]){512}, DTYPE_F32, array_pointer); | ||
array_pointer += 512; | ||
NN_initTensor(&model->actor_0, 2, (size_t[]){1, 512}, DTYPE_F32, NULL); | ||
|
||
// <class 'torch.nn.modules.activation.ELU'>: actor_1 | ||
NN_initTensor(&model->actor_1, 2, (size_t[]){1, 512}, DTYPE_F32, NULL); | ||
|
||
// <class 'torch.nn.modules.linear.Linear'>: actor_2 | ||
NN_initTensor(&model->actor_2_weight, 2, (size_t[]){256, 512}, DTYPE_F32, array_pointer); | ||
array_pointer += 131072; | ||
NN_initTensor(&model->actor_2_bias, 1, (size_t[]){256}, DTYPE_F32, array_pointer); | ||
array_pointer += 256; | ||
NN_initTensor(&model->actor_2, 2, (size_t[]){1, 256}, DTYPE_F32, NULL); | ||
|
||
// <class 'torch.nn.modules.activation.ELU'>: actor_3 | ||
NN_initTensor(&model->actor_3, 2, (size_t[]){1, 256}, DTYPE_F32, NULL); | ||
|
||
// <class 'torch.nn.modules.linear.Linear'>: actor_4 | ||
NN_initTensor(&model->actor_4_weight, 2, (size_t[]){128, 256}, DTYPE_F32, array_pointer); | ||
array_pointer += 32768; | ||
NN_initTensor(&model->actor_4_bias, 1, (size_t[]){128}, DTYPE_F32, array_pointer); | ||
array_pointer += 128; | ||
NN_initTensor(&model->actor_4, 2, (size_t[]){1, 128}, DTYPE_F32, NULL); | ||
|
||
// <class 'torch.nn.modules.activation.ELU'>: actor_5 | ||
NN_initTensor(&model->actor_5, 2, (size_t[]){1, 128}, DTYPE_F32, NULL); | ||
|
||
// <class 'torch.nn.modules.linear.Linear'>: actor_6 | ||
NN_initTensor(&model->actor_6_weight, 2, (size_t[]){12, 128}, DTYPE_F32, array_pointer); | ||
array_pointer += 1536; | ||
NN_initTensor(&model->actor_6_bias, 1, (size_t[]){12}, DTYPE_F32, array_pointer); | ||
array_pointer += 12; | ||
NN_initTensor(&model->actor_6, 2, (size_t[]){1, 12}, DTYPE_F32, NULL); | ||
|
||
} | ||
|
||
|
||
/** | ||
* Forward pass of the model | ||
*/ | ||
void forward(Model *model) { | ||
NN_Linear(&model->actor_0, &model->input_1, &model->actor_0_weight, &model->actor_0_bias); | ||
NN_ELU(&model->actor_1, &model->actor_0, 1.0); | ||
NN_Linear(&model->actor_2, &model->actor_1, &model->actor_2_weight, &model->actor_2_bias); | ||
NN_ELU(&model->actor_3, &model->actor_2, 1.0); | ||
NN_Linear(&model->actor_4, &model->actor_3, &model->actor_4_weight, &model->actor_4_bias); | ||
NN_ELU(&model->actor_5, &model->actor_4, 1.0); | ||
NN_Linear(&model->actor_6, &model->actor_5, &model->actor_6_weight, &model->actor_6_bias); | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import numpy as np | ||
import torch | ||
import torch.nn as nn | ||
|
||
import barstools | ||
|
||
|
||
torch.manual_seed(0) | ||
|
||
class Net(nn.Module): | ||
def __init__(self): | ||
super(Net, self).__init__() | ||
self.actor = nn.Sequential( | ||
nn.Linear(48, 512, bias=True), | ||
nn.ELU(alpha=1.0), | ||
nn.Linear(512, 256, bias=True), | ||
nn.ELU(alpha=1.0), | ||
nn.Linear(256, 128, bias=True), | ||
nn.ELU(alpha=1.0), | ||
nn.Linear(128, 12, bias=True), | ||
) | ||
|
||
def forward(self, input): | ||
output = self.actor.forward(input) | ||
return output | ||
|
||
# Tracing the module | ||
m = Net() | ||
|
||
# m.load_state_dict(torch.load("model.pth", map_location=torch.device("cpu"))) | ||
m.eval() | ||
|
||
test_input = torch.ones((48, )).unsqueeze(0) | ||
|
||
print(test_input) | ||
|
||
with torch.no_grad(): | ||
output = m.forward(test_input) | ||
print("output", output) | ||
|
||
output = barstools.TorchConverter(m).convert(test_input, output_dir=".") | ||
|
Oops, something went wrong.