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
1 parent
7b28076
commit 0b5dc66
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
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
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,6 @@ | ||
# --- | ||
|
||
set(TEST_TARGET test_ggml_basics_work) | ||
add_executable(${TEST_TARGET} ${TEST_TARGET}.c) | ||
target_link_libraries(${TEST_TARGET} PRIVATE ggml) | ||
add_test(NAME ${TEST_TARGET} COMMAND $<TARGET_FILE:${TEST_TARGET}>) |
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,62 @@ | ||
// Tests that ggml basics work. | ||
|
||
#include "ggml.h" | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <math.h> | ||
|
||
#define GGML_TEST_SET_ELEMENT_F32(tensor, i, value) ((float *) tensor->data)[i] = value | ||
|
||
#define GGML_TEST_ASSERT(x, ...) {\ | ||
if (!(x)) {\ | ||
fprintf(stderr, "*** Assertion failed ***\n");\ | ||
fprintf(stderr, __VA_ARGS__);\ | ||
fprintf(stderr, "\n%s:%d\n", __FILE__, __LINE__);\ | ||
abort();\ | ||
}\ | ||
} | ||
|
||
#define GGML_TEST_ASSERT_ELEMENT_F32(tensor, i, expected_value) {\ | ||
float actual = ((float *) tensor->data)[i];\ | ||
GGML_TEST_ASSERT(fabsf(actual - expected_value) <= 0.0000001F, "At %s[%d]: expected %f, actual %f", #tensor, i, expected_value, actual);\ | ||
} | ||
|
||
int main(int argc, const char ** argv) { | ||
struct ggml_init_params params = { | ||
.mem_size = 16 * 1024, | ||
.mem_buffer = NULL, | ||
.no_alloc = false, | ||
}; | ||
|
||
struct ggml_context * ctx = ggml_init(params); | ||
|
||
struct ggml_tensor * x = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 4); | ||
GGML_TEST_SET_ELEMENT_F32(x, 0, -10.0F); | ||
GGML_TEST_SET_ELEMENT_F32(x, 1, 0.0F); | ||
GGML_TEST_SET_ELEMENT_F32(x, 2, 2.5F); | ||
GGML_TEST_SET_ELEMENT_F32(x, 3, 5.0F); | ||
|
||
struct ggml_tensor * y = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 4); | ||
GGML_TEST_SET_ELEMENT_F32(y, 0, 1.0F); | ||
GGML_TEST_SET_ELEMENT_F32(y, 1, 2.0F); | ||
GGML_TEST_SET_ELEMENT_F32(y, 2, 3.0F); | ||
GGML_TEST_SET_ELEMENT_F32(y, 3, 4.0F); | ||
|
||
struct ggml_tensor * sum = ggml_add(ctx, x, y); | ||
|
||
struct ggml_cgraph graph = ggml_build_forward(sum); | ||
graph.n_threads = 2; | ||
ggml_graph_compute(ctx, &graph); | ||
|
||
GGML_TEST_ASSERT_ELEMENT_F32(sum, 0, -9.0F); | ||
GGML_TEST_ASSERT_ELEMENT_F32(sum, 1, 2.0F); | ||
GGML_TEST_ASSERT_ELEMENT_F32(sum, 2, 5.5F); | ||
GGML_TEST_ASSERT_ELEMENT_F32(sum, 3, 9.0F); | ||
|
||
ggml_print_objects(ctx); | ||
|
||
ggml_free(ctx); | ||
|
||
return 0; | ||
} |