Skip to content

Commit 1840df1

Browse files
Lucas NogueiraLucas Nogueira
authored andcommitted
Apply suggestions from the PR: employ CPU buffers to copy results, use correct ctx_size and add GGML_ASSERT to check v_output
1 parent 5c1d117 commit 1840df1

File tree

3 files changed

+288
-271
lines changed

3 files changed

+288
-271
lines changed

examples/cvector-generator/cvector-generator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#include "common.h"
33
#include "llama.h"
44
#include "ggml.h"
5-
#include "vanilla_pca.hpp"
5+
6+
#include "mean.hpp"
7+
#include "pca.hpp"
68

79
#ifdef GGML_USE_CUDA
810
#include "ggml-cuda.h"

examples/cvector-generator/mini-tests/test-vanilla-pca.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "common.h"
33
#include "llama.h"
44
#include "ggml.h"
5-
#include "../vanilla_pca.hpp"
5+
#include "../pca.hpp"
66

77
#ifdef GGML_USE_CUDA
88
#include "ggml-cuda.h"
@@ -15,49 +15,49 @@
1515
#include <cstdio>
1616
#include <cstring>
1717

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-
3718
// 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));
4023

4124
PCA::pca_params pca_params;
4225
pca_params.n_threads = 8;
4326
pca_params.n_batch = 20;
4427
pca_params.n_iterations = 1000;
4528
pca_params.tolerance = 1e-5;
4629

47-
PCA::pca_result result;
30+
PCA::pca_result result = {NULL, 0};
4831
PCA::run_single_pca(pca_params, input_tensor, result);
4932

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]);
5436
}
5537
printf("\nEigenvalue: %f\n", result.explained_variance);
38+
39+
free(result.principal_component);
5640
}
5741

5842
int main() {
5943
// 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+
6161
if (ctx == NULL) {
6262
printf("Failed to initialize ggml context\n");
6363
return 1;

0 commit comments

Comments
 (0)