diff --git a/src/exo/libs/externs.py b/src/exo/libs/externs.py index 4149d0ec6..a5b360a62 100644 --- a/src/exo/libs/externs.py +++ b/src/exo/libs/externs.py @@ -207,7 +207,12 @@ def typecheck(self, args): return atyp def globl(self, prim_type): - return "#include " + return f""" +#include +{prim_type} sigmoid({prim_type} x) {{ + return 1 / (1 + exp(-x)); +}} +""" def interpret(self, args): return math.sigmoid(args[0]) diff --git a/tests/golden/asplos25/test_gemmini_matmul_old/test_matmul.txt b/tests/golden/asplos25/test_gemmini_matmul_old/test_matmul.txt index c892fcf29..1bef2c116 100644 --- a/tests/golden/asplos25/test_gemmini_matmul_old/test_matmul.txt +++ b/tests/golden/asplos25/test_gemmini_matmul_old/test_matmul.txt @@ -99,7 +99,7 @@ void matmul_on_cpu( c_code_str_Context *ctxt, int_fast32_t N, int_fast32_t M, co #include "gemm_acc_malloc.h" #include #include "gemm_malloc.h" -double _relu_(double x) { +int8_t _relu_int8_t(int8_t x) { if (x > 0.0) return x; else return 0.0; } diff --git a/tests/golden/test_externs/test_expf.txt b/tests/golden/test_externs/test_expf.txt index c4ed9eacd..ebbab2553 100644 --- a/tests/golden/test_externs/test_expf.txt +++ b/tests/golden/test_externs/test_expf.txt @@ -14,3 +14,48 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : i8[16] @DRAM, +// y : i8[16] @DRAM +// ) +void foo( void *ctxt, const int8_t* x, int8_t* y ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_fmaxf.txt b/tests/golden/test_externs/test_fmaxf.txt index 48024dec1..af16a7798 100644 --- a/tests/golden/test_externs/test_fmaxf.txt +++ b/tests/golden/test_externs/test_fmaxf.txt @@ -14,3 +14,48 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : f32[16] @DRAM, +// y : f32[16] @DRAM +// ) +void foo( void *ctxt, const float* x, float* y ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_relu.txt b/tests/golden/test_externs/test_relu.txt index d3aa0e215..f2fd00d91 100644 --- a/tests/golden/test_externs/test_relu.txt +++ b/tests/golden/test_externs/test_relu.txt @@ -17,3 +17,47 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : f32[16] @DRAM +// ) +void foo( void *ctxt, float* x ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_relu2.txt b/tests/golden/test_externs/test_relu2.txt index 8e631fd15..8d5174c56 100644 --- a/tests/golden/test_externs/test_relu2.txt +++ b/tests/golden/test_externs/test_relu2.txt @@ -17,3 +17,47 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : f32[16] @DRAM +// ) +void foo( void *ctxt, float* x ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_relu3.txt b/tests/golden/test_externs/test_relu3.txt index cb506df82..d1b294fc3 100644 --- a/tests/golden/test_externs/test_relu3.txt +++ b/tests/golden/test_externs/test_relu3.txt @@ -19,3 +19,49 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : f32[16] @DRAM, +// y : f32[16] @DRAM, +// z : f32[16] @DRAM +// ) +void foo( void *ctxt, const float* x, const float* y, float* z ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_relu4.txt b/tests/golden/test_externs/test_relu4.txt index fbf4d0e07..e1d141c51 100644 --- a/tests/golden/test_externs/test_relu4.txt +++ b/tests/golden/test_externs/test_relu4.txt @@ -17,3 +17,47 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : i8[16] @DRAM +// ) +void foo( void *ctxt, int8_t* x ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_select.txt b/tests/golden/test_externs/test_select.txt index 5039a79fe..fa71ccbad 100644 --- a/tests/golden/test_externs/test_select.txt +++ b/tests/golden/test_externs/test_select.txt @@ -19,3 +19,49 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : i8[16] @DRAM, +// y : i8[16] @DRAM, +// z : i8[16] @DRAM +// ) +void foo( void *ctxt, const int8_t* x, const int8_t* y, int8_t* z ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_sigmoid.txt b/tests/golden/test_externs/test_sigmoid.txt index 441e22118..bc202a82b 100644 --- a/tests/golden/test_externs/test_sigmoid.txt +++ b/tests/golden/test_externs/test_sigmoid.txt @@ -3,7 +3,12 @@ #include #include + #include +float sigmoid(float x) { + return 1 / (1 + exp(-x)); +} + // foo( // x : f32[16] @DRAM, // y : f32[16] @DRAM @@ -14,3 +19,48 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : f32[16] @DRAM, +// y : f32[16] @DRAM +// ) +void foo( void *ctxt, const float* x, float* y ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_sin.txt b/tests/golden/test_externs/test_sin.txt index 28e5ef105..3c6784c39 100644 --- a/tests/golden/test_externs/test_sin.txt +++ b/tests/golden/test_externs/test_sin.txt @@ -13,3 +13,47 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : i8[16] @DRAM +// ) +void foo( void *ctxt, int8_t* x ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/golden/test_externs/test_sqrt.txt b/tests/golden/test_externs/test_sqrt.txt index 17349b7c0..d37ce59b5 100644 --- a/tests/golden/test_externs/test_sqrt.txt +++ b/tests/golden/test_externs/test_sqrt.txt @@ -14,3 +14,48 @@ for (int_fast32_t i = 0; i < 16; i++) { } } + +#pragma once +#ifndef TEST_H +#define TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + + +#include +#include + +// Compiler feature macros adapted from Hedley (public domain) +// https://github.com/nemequ/hedley + +#if defined(__has_builtin) +# define EXO_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else +# define EXO_HAS_BUILTIN(builtin) (0) +#endif + +#if EXO_HAS_BUILTIN(__builtin_assume) +# define EXO_ASSUME(expr) __builtin_assume(expr) +#elif EXO_HAS_BUILTIN(__builtin_unreachable) +# define EXO_ASSUME(expr) \ + ((void)((expr) ? 1 : (__builtin_unreachable(), 1))) +#else +# define EXO_ASSUME(expr) ((void)(expr)) +#endif + + + +// foo( +// x : f32[16] @DRAM, +// y : f32[16] @DRAM +// ) +void foo( void *ctxt, const float* x, float* y ); + + + +#ifdef __cplusplus +} +#endif +#endif // TEST_H diff --git a/tests/test_externs.py b/tests/test_externs.py index 32c9b94dd..b0cbe3472 100644 --- a/tests/test_externs.py +++ b/tests/test_externs.py @@ -6,111 +6,133 @@ from exo.libs.externs import * -def test_relu(golden): +def test_relu(golden, compiler): @proc def foo(x: f32[16]): for i in seq(0, 16): x[i] = relu(3.0) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + compiler.compile(foo) -def test_relu2(golden): + +def test_relu2(golden, compiler): @proc def foo(x: f32[16]): for i in seq(0, 16): x[i] = relu(x[i]) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + + compiler.compile(foo) -def test_relu3(golden): +def test_relu3(golden, compiler): @proc def foo(x: f32[16], y: f32[16], z: f32[16]): for i in seq(0, 16): z[i] = relu(x[i] + y[i]) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + + compiler.compile(foo) -def test_relu4(golden): +def test_relu4(golden, compiler): @proc def foo(x: i8[16]): for i in seq(0, 16): x[i] = relu(3.0) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + compiler.compile(foo) -def test_sin(golden): + +def test_sin(golden, compiler): @proc def foo(x: i8[16]): for i in seq(0, 16): x[i] = sin(x[i] * 2) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + + compiler.compile(foo) -def test_sin(golden): +def test_sin(golden, compiler): @proc def foo(x: i8[16]): for i in seq(0, 16): x[i] = sin(x[i] * 2) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + + compiler.compile(foo) -def test_select(golden): +def test_select(golden, compiler): @proc def foo(x: i8[16], y: i8[16], z: i8[16]): for i in seq(0, 16): z[i] = select(x[i] * 2, y[i], z[i] + y[i], -x[i]) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + compiler.compile(foo) -def test_expf(golden): + +def test_expf(golden, compiler): @proc def foo(x: i8[16], y: i8[16]): for i in seq(0, 16): y[i] = expf(x[i] + y[i]) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + + compiler.compile(foo) -def test_fmaxf(golden): +def test_fmaxf(golden, compiler): @proc def foo(x: f32[16], y: f32[16]): for i in seq(0, 16): y[i] = fmaxf(x[i], y[i] * 2) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + + compiler.compile(foo) -def test_sigmoid(golden): +def test_sigmoid(golden, compiler): @proc def foo(x: f32[16], y: f32[16]): for i in seq(0, 16): y[i] = sigmoid(x[i] + y[i]) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + compiler.compile(foo) -def test_sqrt(golden): + +def test_sqrt(golden, compiler): @proc def foo(x: f32[16], y: f32[16]): for i in seq(0, 16): y[i] = sqrt(x[i] + y[i]) - c_file, _ = compile_procs_to_strings([foo], "test.h") - assert c_file == golden + c_file, h_file = compile_procs_to_strings([foo], "test.h") + assert c_file + h_file == golden + + compiler.compile(foo)