Compare C++ ggml tensor (dumped as [C, W, H]) vs Python NCHW [1, C, H, W] ggml stores: flat[c + w*C + h*C*W] Python NCHW: flat[c*H*W + h*W + w] These differ! Need transposition.
| 32 | // Python NCHW: flat[c*H*W + h*W + w] |
| 33 | // These differ! Need transposition. |
| 34 | static compare_result compare_ggml_vs_nchw(const ref_tensor_f32& cpp, |
| 35 | const ref_tensor_f32& ref) { |
| 36 | if (cpp.shape.size() < 3 || ref.data.empty()) { |
| 37 | compare_result r; |
| 38 | r.max_diff = 999.0f; |
| 39 | return r; |
| 40 | } |
| 41 | int C = cpp.shape[0], W = cpp.shape[1], H = cpp.shape[2]; |
| 42 | |
| 43 | // Transpose ggml → NCHW |
| 44 | std::vector<float> transposed(C * W * H); |
| 45 | for (int c = 0; c < C; ++c) |
| 46 | for (int h = 0; h < H; ++h) |
| 47 | for (int w = 0; w < W; ++w) |
| 48 | transposed[c * H * W + h * W + w] = cpp.data[c + w * C + h * C * W]; |
| 49 | |
| 50 | return compare_tensors(transposed.data(), ref.data.data(), |
| 51 | std::min((int)transposed.size(), ref.numel())); |
| 52 | } |
| 53 | |
| 54 | static void print_result(const char* name, const compare_result& r, float atol) { |
| 55 | const char* status = (r.max_diff < atol) ? "[PASS]" : "[FAIL]"; |
no test coverage detected