|
2 | 2 | #include "common.h" |
3 | 3 | #include "llama.h" |
4 | 4 | #include "ggml.h" |
5 | | -#include "../vanilla_pca.hpp" |
| 5 | +#include "../pca.hpp" |
6 | 6 |
|
7 | 7 | #ifdef GGML_USE_CUDA |
8 | 8 | #include "ggml-cuda.h" |
|
15 | 15 | #include <cstdio> |
16 | 16 | #include <cstring> |
17 | 17 |
|
18 | | -// Function to initialize ggml with optional GPU backend support |
19 | | -struct ggml_context *initialize_ggml_context() { |
20 | | -#ifdef GGML_USE_CUDA |
21 | | - struct ggml_init_params params = { .mem_size = 1024 * 1024, .mem_buffer = NULL, .use_gpu = true }; |
22 | | - printf("Initializing with GPU backend...\n"); |
23 | | -#else |
24 | | - struct ggml_init_params params = { .mem_size = 1024 * 1024, .mem_buffer = NULL }; |
25 | | - printf("Initializing with CPU backend...\n"); |
26 | | -#endif |
27 | | - return ggml_init(params); |
28 | | -} |
29 | | - |
30 | | -// Helper function to create a tensor from a matrix |
31 | | -struct ggml_tensor *create_tensor(struct ggml_context *ctx, float *data, int rows, int cols) { |
32 | | - struct ggml_tensor *tensor = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, cols, rows); |
33 | | - memcpy(tensor->data, data, ggml_nbytes(tensor)); |
34 | | - return tensor; |
35 | | -} |
36 | | - |
37 | 18 | // Function to run PCA and print results |
38 | | -void run_pca_test(struct ggml_context *ctx, float *matrix, int rows, int cols) { |
39 | | - struct ggml_tensor *input_tensor = create_tensor(ctx, matrix, rows, cols); |
| 19 | +static void run_pca_test(struct ggml_context *ctx, float *matrix, int rows, int cols) { |
| 20 | + // struct ggml_tensor *input_tensor = create_tensor(ctx, matrix, rows, cols); |
| 21 | + struct ggml_tensor *input_tensor = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, rows, cols); |
| 22 | + memcpy(input_tensor->data, matrix, rows * cols * sizeof(float)); |
40 | 23 |
|
41 | 24 | PCA::pca_params pca_params; |
42 | 25 | pca_params.n_threads = 8; |
43 | 26 | pca_params.n_batch = 20; |
44 | 27 | pca_params.n_iterations = 1000; |
45 | 28 | pca_params.tolerance = 1e-5; |
46 | 29 |
|
47 | | - PCA::pca_result result; |
| 30 | + PCA::pca_result result = {NULL, 0}; |
48 | 31 | PCA::run_single_pca(pca_params, input_tensor, result); |
49 | 32 |
|
50 | | - printf("\nPrincipal components:\n"); |
51 | | - float *b = (float *)result.principal_component->data; |
52 | | - for (int i = 0; i < result.principal_component->ne[0]; i++) { |
53 | | - printf("%f ", b[i]); |
| 33 | + printf("Principal components:\n"); |
| 34 | + for (int i = 0; i < cols; i++) { |
| 35 | + printf("%f ", result.principal_component[i]); |
54 | 36 | } |
55 | 37 | printf("\nEigenvalue: %f\n", result.explained_variance); |
| 38 | + |
| 39 | + free(result.principal_component); |
56 | 40 | } |
57 | 41 |
|
58 | 42 | int main() { |
59 | 43 | // Initialize ggml context |
60 | | - struct ggml_context *ctx = initialize_ggml_context(); |
| 44 | + size_t ctx_size = 0; |
| 45 | + ctx_size += 4 * 4 * ggml_type_size(GGML_TYPE_F32); |
| 46 | + ctx_size += 10 * 10 * ggml_type_size(GGML_TYPE_F32); |
| 47 | + ctx_size += 3 * 3 * ggml_type_size(GGML_TYPE_F32); |
| 48 | + ctx_size += 3 * 3 * ggml_type_size(GGML_TYPE_F32); |
| 49 | + ctx_size += 4 * ggml_tensor_overhead(); |
| 50 | + ctx_size += 1024; |
| 51 | + |
| 52 | + // Step 2. Initialize GGML Context |
| 53 | + struct ggml_init_params ctx_params { |
| 54 | + ctx_size, // mem_size |
| 55 | + NULL, // mem_buffer |
| 56 | + false, // no_alloc |
| 57 | + }; |
| 58 | + struct ggml_context * ctx = ggml_init(ctx_params); |
| 59 | + |
| 60 | + |
61 | 61 | if (ctx == NULL) { |
62 | 62 | printf("Failed to initialize ggml context\n"); |
63 | 63 | return 1; |
|
0 commit comments