diff --git a/runtime/samples/CMakeLists.txt b/runtime/samples/CMakeLists.txt index 04527805..00391fed 100644 --- a/runtime/samples/CMakeLists.txt +++ b/runtime/samples/CMakeLists.txt @@ -4,3 +4,4 @@ add_subdirectory(big_matvec) add_subdirectory(nsnet2) add_subdirectory(util) add_subdirectory(vec_multiply) +add_subdirectory(tiny_matmul) diff --git a/runtime/samples/tiny_matmul/CMakeLists.txt b/runtime/samples/tiny_matmul/CMakeLists.txt new file mode 100644 index 00000000..5e14a445 --- /dev/null +++ b/runtime/samples/tiny_matmul/CMakeLists.txt @@ -0,0 +1,11 @@ +quidditch_module(SRC tiny_matmul.mlir ASSERT_XDSL) + +add_executable(my_tiny_matmul main.c) +target_link_libraries( + my_tiny_matmul + PRIVATE + samples_util + tiny_matmul + snRuntime + Quidditch::dispatch::dispatch +) diff --git a/runtime/samples/tiny_matmul/main.c b/runtime/samples/tiny_matmul/main.c new file mode 100644 index 00000000..990a3323 --- /dev/null +++ b/runtime/samples/tiny_matmul/main.c @@ -0,0 +1,59 @@ +#include + +#include +#include +#include +#include + +int main() { + iree_alignas(64) double res[9]; + iree_alignas(64) double data[9]; + iree_alignas(64) double correct[9] = {30,36,42,66,81,96,102,126,150}; + + if (!snrt_is_dm_core()) return quidditch_dispatch_enter_worker_loop(); + + // [1, 2, 3, 4, 5, 6, 7, 8, 9] + for (int i = 0; i < IREE_ARRAYSIZE(data); i++) { + data[i] = (i + 1); + res[i]=0; + } + + model_config_t config = { + .libraries = + (iree_hal_executable_library_query_fn_t[]){ + quidditch_tiny_matmul_dispatch_0_library_query, + }, + .num_libraries = 1, + .module_constructor = test_tiny_matmul_create, + .main_function = iree_make_cstring_view("test_tiny_matmul.tiny_matmul"), + + .element_type = IREE_HAL_ELEMENT_TYPE_FLOAT_64, + + .num_inputs = 3, + .input_data = (const void*[]){data, data, res}, + .input_sizes = (const iree_host_size_t[]){IREE_ARRAYSIZE(data), + IREE_ARRAYSIZE(data), + IREE_ARRAYSIZE(res)}, + .input_ranks = (const iree_host_size_t[]){2, 2, 2}, + .input_shapes = + (const iree_hal_dim_t*[]){(iree_hal_dim_t[]){3,3}, + (iree_hal_dim_t[]){3,3}, + (iree_hal_dim_t[]){3,3}}, + + .num_outputs = 1, + .output_data = (void*[]){res}, + .output_sizes = (const iree_host_size_t[]){IREE_ARRAYSIZE(res)}, + }; + + IREE_CHECK_OK(run_model(&config)); + + if (!snrt_is_dm_core()) return 0; + + // check correctness + for (int i = 0; i < IREE_ARRAYSIZE(data); i++) { + double value = data[i]; + printf("%f\n", value); + if (value != correct[i]) return 1; + } + return 0; +} diff --git a/runtime/samples/tiny_matmul/tiny_matmul.mlir b/runtime/samples/tiny_matmul/tiny_matmul.mlir new file mode 100644 index 00000000..08f9bf70 --- /dev/null +++ b/runtime/samples/tiny_matmul/tiny_matmul.mlir @@ -0,0 +1,9 @@ +builtin.module @test_tiny_matmul { + func.func @tiny_matmul(%lhs: tensor<3x3xf64>, %rhs: tensor<3x3xf64>, %acc: tensor<3x3xf64>) -> tensor<3x3xf64> { + %result = linalg.matmul // changing linalg.add to linalg.matmul, causes a RESOURCE_EXHAUSTED error + ins(%lhs, %rhs: tensor<3x3xf64>, tensor<3x3xf64>) + outs(%acc: tensor<3x3xf64>) + -> tensor<3x3xf64> + return %result: tensor<3x3xf64> + } +}