Run a graph: build win_part -> win_unpart on the given backend. Returns the final output as a flat float vector.
| 19 | // Run a graph: build win_part -> win_unpart on the given backend. |
| 20 | // Returns the final output as a flat float vector. |
| 21 | static std::vector<float> run_win_roundtrip(ggml_backend_t backend, |
| 22 | const std::vector<float>& input, |
| 23 | int C, int W, int H, int WS) { |
| 24 | // Graph context |
| 25 | size_t ctx_size = ggml_tensor_overhead() * 16 + ggml_graph_overhead(); |
| 26 | struct ggml_init_params params = {ctx_size, nullptr, true}; |
| 27 | struct ggml_context* ctx = ggml_init(params); |
| 28 | |
| 29 | // Input tensor: [C, W, H, 1] |
| 30 | auto* inp = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, C, W, H); |
| 31 | ggml_set_name(inp, "input"); |
| 32 | ggml_set_input(inp); |
| 33 | |
| 34 | // win_part: [C, W, H, 1] -> [C, WS, WS, np] |
| 35 | auto* parted = ggml_win_part(ctx, inp, WS); |
| 36 | ggml_set_name(parted, "parted"); |
| 37 | |
| 38 | // win_unpart: [C, WS, WS, np] -> [C, W, H, 1] |
| 39 | auto* unparted = ggml_win_unpart(ctx, parted, W, H, WS); |
| 40 | ggml_set_name(unparted, "unparted"); |
| 41 | ggml_set_output(unparted); |
| 42 | |
| 43 | // Build graph |
| 44 | auto* graph = ggml_new_graph(ctx); |
| 45 | ggml_build_forward_expand(graph, unparted); |
| 46 | |
| 47 | // Allocate |
| 48 | auto* galloc = ggml_gallocr_new(ggml_backend_get_default_buffer_type(backend)); |
| 49 | if (!ggml_gallocr_reserve(galloc, graph) || !ggml_gallocr_alloc_graph(galloc, graph)) { |
| 50 | fprintf(stderr, "FAIL: gallocr_alloc_graph failed\n"); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | // Set input data |
| 55 | ggml_backend_tensor_set(inp, input.data(), 0, input.size() * sizeof(float)); |
| 56 | |
| 57 | // Compute |
| 58 | ggml_backend_graph_compute(backend, graph); |
| 59 | |
| 60 | // Read output |
| 61 | std::vector<float> output(C * W * H); |
| 62 | ggml_backend_tensor_get(unparted, output.data(), 0, output.size() * sizeof(float)); |
| 63 | |
| 64 | ggml_gallocr_free(galloc); |
| 65 | ggml_free(ctx); |
| 66 | return output; |
| 67 | } |
| 68 | |
| 69 | // Run win_part only (no roundtrip) — compare intermediate output. |
| 70 | static std::vector<float> run_win_part_only(ggml_backend_t backend, |