Skip to content

Instantly share code, notes, and snippets.

@svaniksharma
Created May 31, 2025 03:17
Show Gist options
  • Save svaniksharma/15fa1b0dbd0aca853f6e6bef0011bdb0 to your computer and use it in GitHub Desktop.
Save svaniksharma/15fa1b0dbd0aca853f6e6bef0011bdb0 to your computer and use it in GitHub Desktop.
Using `ggml_cont`
#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;
}
@svaniksharma
Copy link
Author

I created this because I was confused on ggml_cont and why it (seemingly) was not working as intended. I wrote a short article accompanying this gist here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment