Created
May 31, 2025 03:17
-
-
Save svaniksharma/15fa1b0dbd0aca853f6e6bef0011bdb0 to your computer and use it in GitHub Desktop.
Revisions
-
svaniksharma created this gist
May 31, 2025 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ #include "ggml-cpu.h" #include "ggml.h" #include <iostream> void print_tensor(struct ggml_tensor *tensor) { for (size_t i = 0; i < tensor->ne[3]; i++) { for (size_t k = 0; k < tensor->ne[2]; k++) { for (size_t j = 0; j < tensor->ne[1]; j++) { for (size_t l = 0; l < tensor->ne[0]; l++) { std::cout << ggml_get_i32_nd(tensor, l, j, k, i) << " "; } std::cout << "\n"; } std::cout << "\n"; } std::cout << "\n"; } } int main (int argc, char *argv[]) { struct ggml_init_params params = { 1024 * ggml_tensor_overhead(), nullptr, false }; struct ggml_context *ctx = ggml_init(params); // Creates an array with values 0, 1, 2, ..., 15 const int N = 16; int values[N] = { 0 }; for (int i = 0; i < N; i++) values[i] = i; struct ggml_tensor *tensor = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, N); for (int i = 0; i < N; i++) ggml_set_i32_1d(tensor, i, values[i]); struct ggml_tensor *t = ggml_reshape_4d(ctx, tensor, 2, 2, 4, 1); std::cout << "Original tensor\n--------------\n"; print_tensor(t); // 0 -> 1, 1 -> 2, 2 -> 0, 3 -> 3 struct ggml_tensor *permuted_t = ggml_permute(ctx, t, 1, 2, 0, 3); std::cout << "Permuted tensor\n--------------\n"; std::cout << "New Shape: " << permuted_t->ne[0] << " x " << permuted_t->ne[1] << " x " << permuted_t->ne[2] << " x " << permuted_t->ne[3] << "\n"; GGML_ASSERT(permuted_t->ne[0] == 4); GGML_ASSERT(permuted_t->ne[1] == 2); GGML_ASSERT(permuted_t->ne[2] == 2); GGML_ASSERT(permuted_t->ne[3] == 1); print_tensor(permuted_t); struct ggml_tensor *cont_permuted_t = ggml_cont(ctx, permuted_t); struct ggml_tensor *a = ggml_reshape_2d(ctx, cont_permuted_t, 2, 8); GGML_ASSERT(a->ne[0] == 2); GGML_ASSERT(a->ne[1] == 8); GGML_ASSERT(a->ne[2] == 1); GGML_ASSERT(a->ne[3] == 1); std::cout << "All zeros since we haven't computed anything\n"; print_tensor(a); // NOTE: Should get all zeros! // Build computational graph struct ggml_cgraph *gf = ggml_new_graph(ctx); ggml_build_forward_expand(gf, a); ggml_graph_compute_with_ctx(ctx, gf, 1); // Try printing the tensor std::cout << "Now-contiguous tensor after we perform the computation\n"; print_tensor(a); ggml_free(ctx); return 0; }