-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathtest_prompt_kernel.cpp
More file actions
49 lines (43 loc) · 2.13 KB
/
Copy pathtest_prompt_kernel.cpp
File metadata and controls
49 lines (43 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "model_loader.hpp"
#include "backend.hpp"
#include "prompt_kernel.hpp"
#include "parity.hpp"
#include <cstdio>
#include <cstdlib>
#include <vector>
// Parity: PromptKernel(encoder_out, prompt_index) must match NeMo's
// prompt_kernel(cat([encoded, onehot])). Skips (77) unless both the converted
// nemotron gguf and the prompt baseline are provided.
// PARAKEET_TEST_GGUF_NEMOTRON converted nemotron gguf
// PARAKEET_TEST_BASELINE_NEMOTRON prompt baseline gguf (encoder_out [D,T],
// prompt_kernel_out [T,D], baseline.prompt_index)
int main() {
const char* gguf = std::getenv("PARAKEET_TEST_GGUF_NEMOTRON");
const char* base = std::getenv("PARAKEET_TEST_BASELINE_NEMOTRON");
if (!gguf || !base) {
std::fprintf(stderr, "test_prompt_kernel: fixtures not set; skip\n");
return 77;
}
pk::ModelLoader ml;
if (!ml.load(gguf)) { std::fprintf(stderr, "load failed\n"); return 1; }
pk::ensure_weights_realized(ml);
// encoder_out [D,T] (channels-first) + reference prompt_kernel_out [T,D].
std::vector<float> enc; std::vector<int64_t> enc_shape;
std::vector<float> ref; std::vector<int64_t> ref_shape;
if (!pktest::load_baseline(base, "encoder_out", enc, enc_shape)) return 1;
if (!pktest::load_baseline(base, "prompt_kernel_out", ref, ref_shape)) return 1;
const int D = (int)enc_shape[0], T = (int)enc_shape[1];
const int prompt_index = (int)pktest::pktest_read_u32(base, "baseline.prompt_index");
std::fprintf(stderr, "[prompt_kernel] D=%d T=%d prompt_index=%d\n", D, T, prompt_index);
pk::PromptKernel pkmod(ml);
if (!pkmod.present()) { std::fprintf(stderr, "prompt not present in model\n"); return 1; }
std::vector<float> got; // channels-first [D, T]
pkmod.apply(enc, D, T, prompt_index, got);
// Transpose got [D,T] -> [T,D] to compare with ref [T,D].
std::vector<float> got_td((size_t)T * D);
for (int t = 0; t < T; ++t)
for (int c = 0; c < D; ++c)
got_td[(size_t)t * D + c] = got[(size_t)c * T + t];
bool ok = pktest::compare(got_td, ref, "prompt_kernel", 2e-3f, 2e-3f);
return ok ? 0 : 1;
}