Archived
nemotron 3.5 implementation
This commit is contained in:
1 parent
33108e488b
commit
94c90b17ad
59 files changed
+6046
-76
No files matched your search
@@ -2341,4 +2341,525 @@ TEST(an_unconverted_qwen_image_pipeline_serves_the_containers_picture) {
|
||||
CHECK(direct.png == conv.png);
|
||||
}
|
||||
|
||||
namespace {
|
||||
json nemotron_fixture(const char* name) {
|
||||
return json::parse(tiny::read_text(std::string(RAD_GOLDEN_FIXTURES) + "/" + name));
|
||||
}
|
||||
|
||||
/* The same BF16 parameters as the component oracle, now read by the real checkpoint loader.
|
||||
* Extra vocabulary rows only make the byte tokenizer usable for HTTP input; the oracle cases
|
||||
* use the original 32 rows. No test executor or media adapter participates in these tests. */
|
||||
bool write_nemotron(const std::string& dir) {
|
||||
json weights = nemotron_fixture("nemotron35.json")["weights"];
|
||||
weights.update(nemotron_fixture("nemotron35_vision.json")["weights"]);
|
||||
std::vector<tiny::Tensor> ts;
|
||||
for (auto it = weights.begin(); it != weights.end(); ++it) {
|
||||
auto shape = it.value()["shape"].get<std::vector<int64_t>>();
|
||||
auto bits = it.value()["bf16"].get<std::vector<uint16_t>>();
|
||||
if (it.key() == "language_model.embed_tokens.weight") {
|
||||
shape[0] = 320;
|
||||
const auto original = bits;
|
||||
for (int row = 32; row < 320; ++row)
|
||||
bits.insert(bits.end(), original.begin() + row % 32 * 16,
|
||||
original.begin() + (row % 32 + 1) * 16);
|
||||
}
|
||||
tiny::Tensor t{it.key(), "BF16", shape, {}};
|
||||
t.bytes.resize(bits.size() * 2);
|
||||
std::memcpy(t.bytes.data(), bits.data(), t.bytes.size());
|
||||
ts.push_back(std::move(t));
|
||||
}
|
||||
json rope = {{"rope_type", "yarn"},
|
||||
{"rope_theta", 1e6},
|
||||
{"factor", 16.0},
|
||||
{"original_max_position_embeddings", 16384},
|
||||
{"beta_fast", 32.0},
|
||||
{"beta_slow", 1.0},
|
||||
{"mscale", 1.0},
|
||||
{"mscale_all_dim", 1.0},
|
||||
{"llama_4_scaling_beta", 0.1}};
|
||||
json tc = {{"model_type", "ministral3"},
|
||||
{"hidden_size", 16},
|
||||
{"intermediate_size", 32},
|
||||
{"num_hidden_layers", 2},
|
||||
{"num_attention_heads", 4},
|
||||
{"num_key_value_heads", 1},
|
||||
{"head_dim", 128},
|
||||
{"vocab_size", 320},
|
||||
{"max_position_embeddings", 262144},
|
||||
{"rms_norm_eps", 1e-5},
|
||||
{"hidden_act", "silu"},
|
||||
{"is_causal", false},
|
||||
{"use_cache", false},
|
||||
{"sliding_window", nullptr},
|
||||
{"rope_parameters", rope},
|
||||
{"bos_token_id", 1},
|
||||
{"eos_token_id", 2},
|
||||
{"pad_token_id", 11}};
|
||||
json vc = {{"model_type", "pixtral"},
|
||||
{"hidden_size", 64},
|
||||
{"intermediate_size", 96},
|
||||
{"num_hidden_layers", 2},
|
||||
{"num_attention_heads", 1},
|
||||
{"head_dim", 64},
|
||||
{"patch_size", 14},
|
||||
{"image_size", 1540},
|
||||
{"num_channels", 3},
|
||||
{"hidden_act", "silu"},
|
||||
{"rope_parameters", {{"rope_type", "default"}, {"rope_theta", 10000.0}}}};
|
||||
json config = {{"architectures", {"Mistral3Model"}},
|
||||
{"model_type", "mistral3"},
|
||||
{"is_causal", false},
|
||||
{"text_config", tc},
|
||||
{"vision_config", vc},
|
||||
{"image_token_index", 10},
|
||||
{"spatial_merge_size", 2},
|
||||
{"multimodal_projector_bias", false},
|
||||
{"projector_hidden_act", "gelu"},
|
||||
{"vision_feature_layer", -1}};
|
||||
json tok = json::parse(tiny::tokenizer_json());
|
||||
json vocab = json::object();
|
||||
for (int c = 0; c < 256; ++c)
|
||||
vocab[tiny::byte_symbol(c)] = 32 + c;
|
||||
vocab[tiny::byte_symbol(' ')] = 5;
|
||||
const int letter_ids[] = {3, 4, 7, 8, 9, 6};
|
||||
for (int i = 0; i < 6; ++i)
|
||||
vocab[std::string(1, 'A' + i)] = letter_ids[i];
|
||||
tok["added_tokens"] = json::array();
|
||||
for (const auto& [s, id] : std::vector<std::pair<std::string, int>>{{"<s>", 1},
|
||||
{"</s>", 2},
|
||||
{"<pad>", 11},
|
||||
{"[IMG]", 10},
|
||||
{"[IMG_BREAK]", 12},
|
||||
{"[IMG_END]", 13}}) {
|
||||
vocab[s] = id;
|
||||
tok["added_tokens"].push_back({{"id", id},
|
||||
{"content", s},
|
||||
{"special", true},
|
||||
{"single_word", false},
|
||||
{"lstrip", false},
|
||||
{"rstrip", false},
|
||||
{"normalized", false}});
|
||||
}
|
||||
tok["model"]["vocab"] = std::move(vocab);
|
||||
return tiny::write_text(dir + "/config.json", config.dump()) &&
|
||||
tiny::write_safetensors(dir + "/model.safetensors", ts) &&
|
||||
tiny::write_text(dir + "/tokenizer.json", tok.dump()) &&
|
||||
tiny::write_text(dir + "/config_sentence_transformers.json",
|
||||
R"({"prompts":{"query":"AB","document":"CE"}})");
|
||||
}
|
||||
|
||||
// Opt-in hardware checks reuse the same endpoint fixtures and gates. The default suite stays
|
||||
// on CPU; no unrelated test sees this switch.
|
||||
bool nemotron_device() {
|
||||
const char* e = std::getenv("RAD_NEMOTRON_DEVICE");
|
||||
return e && *e && *e != '0';
|
||||
}
|
||||
std::vector<const char*> nemotron_kernels() {
|
||||
return nemotron_device() ? std::vector<const char*>{"libr4d,libavx"}
|
||||
: std::vector<const char*>{"libref", "libavx"};
|
||||
}
|
||||
std::string nemotron_device_env() {
|
||||
const char* index = std::getenv("RAD_NEMOTRON_DEVICE_INDEX");
|
||||
return std::string("ROCR_VISIBLE_DEVICES=") + (index && *index ? index : "0");
|
||||
}
|
||||
std::vector<std::string> nemotron_env() {
|
||||
return nemotron_device() ? std::vector<std::string>{nemotron_device_env()}
|
||||
: std::vector<std::string>{};
|
||||
}
|
||||
|
||||
const std::vector<std::string> nemotron_args = {
|
||||
"--max-num-seqs", "4", "--max-num-batched-tokens", "64",
|
||||
"--max-model-len", "64", "--mm-max-patches", "64"};
|
||||
|
||||
std::vector<float> embedding_reply(int port, const json& body, int row = 0) {
|
||||
int status = 0;
|
||||
const auto reply = post(port, "/v1/embeddings", body, &status);
|
||||
if (status != 200) fprintf(stderr, " embedding HTTP %d: %s\n", status, reply.dump().c_str());
|
||||
CHECK_EQ(status, 200);
|
||||
if (status != 200 || !reply.contains("data") || row >= (int)reply["data"].size()) return {};
|
||||
return reply["data"][row]["embedding"].get<std::vector<float>>();
|
||||
}
|
||||
|
||||
void check_embedding(const std::vector<float>& got, std::vector<float> want, int dims = 16,
|
||||
double max_l2 = 0.01) {
|
||||
REQUIRE_EQ(got.size(), (size_t)dims);
|
||||
want.resize((size_t)dims);
|
||||
double n = 0, dot = 0, err = 0, gn = 0;
|
||||
for (float v : want)
|
||||
n += (double)v * v;
|
||||
for (int i = 0; i < dims; ++i) {
|
||||
want[(size_t)i] /= (float)std::sqrt(n);
|
||||
dot += (double)got[(size_t)i] * want[(size_t)i];
|
||||
const double d = got[(size_t)i] - want[(size_t)i];
|
||||
err += d * d;
|
||||
gn += (double)got[(size_t)i] * got[(size_t)i];
|
||||
}
|
||||
CHECK(std::abs(gn - 1) < 1e-5);
|
||||
if (dot / std::sqrt(gn) < 0.9995 || std::sqrt(err) > max_l2)
|
||||
fprintf(stderr, " embedding cosine %.9g, normalized L2 error %.9g\n",
|
||||
dot / std::sqrt(gn), std::sqrt(err));
|
||||
CHECK(dot / std::sqrt(gn) >= 0.9995);
|
||||
CHECK(std::sqrt(err) <= max_l2);
|
||||
}
|
||||
|
||||
json nemotron_image(const json& spec) {
|
||||
const int f = spec["f"], h = spec["h"], w = spec["w"];
|
||||
std::vector<uint8_t> rgb((size_t)h * w * 3);
|
||||
for (int y = 0; y < h; ++y)
|
||||
for (int x = 0; x < w; ++x)
|
||||
for (int c = 0; c < 3; ++c) {
|
||||
uint64_t z = (uint64_t)f * 0x9E3779B97F4A7C15ull +
|
||||
(uint64_t)y * 0xBF58476D1CE4E5B9ull +
|
||||
(uint64_t)x * 0x94D049BB133111EBull +
|
||||
(uint64_t)c * 0x2545F4914F6CDD1Dull + 12345ull;
|
||||
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
|
||||
z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
|
||||
rgb[((size_t)y * w + x) * 3 + c] = (uint8_t)((z ^ (z >> 31)) & 255);
|
||||
}
|
||||
std::vector<uint8_t> png;
|
||||
std::string why;
|
||||
if (rad::mm::encode_image(rgb.data(), w, h, 3, "png", 0, &png, &why) < 0) return nullptr;
|
||||
return {
|
||||
{"type", "image_url"},
|
||||
{"image_url",
|
||||
{{"url", "data:image/png;base64," + rad::server::base64_encode(png.data(), png.size())}}}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
TEST(nemotron35_endpoint_refuses_custom_template_overrides) {
|
||||
tiny::TempDir d("e2e_nemotron_template");
|
||||
REQUIRE(d.ok());
|
||||
REQUIRE(write_nemotron(d.path));
|
||||
const std::string path = d.path + "/override.jinja";
|
||||
REQUIRE(tiny::write_text(path, "{{ messages[0]['content'] }}"));
|
||||
for (const char* kernels : nemotron_kernels()) {
|
||||
#if !defined(__x86_64__)
|
||||
if (std::string(kernels) == "libavx") continue;
|
||||
#endif
|
||||
Server s;
|
||||
s.quiet = true;
|
||||
auto args = nemotron_args;
|
||||
args.insert(args.end(), {"--kernels", kernels, "--override-chat-template", path});
|
||||
CHECK(!s.start(d.path, args, nemotron_env()));
|
||||
CHECK(s.output().find("--override-chat-template is not supported for this model") !=
|
||||
std::string::npos);
|
||||
CHECK(s.stop() != 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(nemotron35_endpoint_loads_the_oracle_weights_and_pools_ragged_inputs) {
|
||||
tiny::TempDir d("e2e_nemotron");
|
||||
REQUIRE(d.ok());
|
||||
REQUIRE(write_nemotron(d.path));
|
||||
const auto fixture = nemotron_fixture("nemotron35.json");
|
||||
for (const char* kernels : nemotron_kernels()) {
|
||||
#if !defined(__x86_64__)
|
||||
if (std::string(kernels) == "libavx") continue;
|
||||
#endif
|
||||
Server s;
|
||||
auto args = nemotron_args;
|
||||
args.insert(args.end(), {"--kernels", kernels});
|
||||
if (!s.start(d.path, args, nemotron_env())) {
|
||||
fprintf(stderr, " complete Nemotron startup log:\n%s\n", s.output().c_str());
|
||||
REQUIRE(false);
|
||||
}
|
||||
for (const auto& c : fixture["cases"]) {
|
||||
if (c["name"] == "q_scale_boundary")
|
||||
continue; // HTTP starts each input at position zero
|
||||
const json req = {{"input", c["inputs"]}};
|
||||
for (int row = 0; row < (int)c["inputs"].size(); ++row)
|
||||
check_embedding(embedding_reply(s.port, req, row), c["pooled"][row]);
|
||||
}
|
||||
const auto raw = embedding_reply(s.port, {{"input", {3, 4, 5, 7}}});
|
||||
check_embedding(embedding_reply(s.port, {{"input", "C"}, {"prompt_name", "query"}}), raw);
|
||||
const json messages = {{{"role", "system"}, {"content", "AB "}},
|
||||
{{"role", "user"}, {"content", "C"}}};
|
||||
check_embedding(embedding_reply(s.port, {{"messages", messages}}), raw);
|
||||
check_embedding(embedding_reply(s.port, {{"input", {3, 4, 5, 7}}, {"dimensions", 8}}), raw,
|
||||
8);
|
||||
int status = 0;
|
||||
// Whole-input encoders need no generation slot after the last encoded token.
|
||||
const auto boundary =
|
||||
post(s.port, "/v1/embeddings", {{"input", std::vector<int>(64, 3)}}, &status);
|
||||
REQUIRE_EQ(status, 200);
|
||||
const auto values = boundary["data"][0]["embedding"].get<std::vector<float>>();
|
||||
CHECK_EQ(values.size(), (size_t)16);
|
||||
double norm = 0;
|
||||
for (float v : values) {
|
||||
CHECK(std::isfinite(v));
|
||||
norm += (double)v * v;
|
||||
}
|
||||
CHECK_NEAR(norm, 1.0, 1e-5);
|
||||
post(s.port, "/v1/embeddings", {{"input", std::vector<int>(65, 3)}}, &status);
|
||||
CHECK_EQ(status, 400);
|
||||
post(s.port, "/v1/chat/completions", {{"messages", {{{"role", "user"}, {"content", "A"}}}}},
|
||||
&status);
|
||||
CHECK_EQ(status, 501);
|
||||
REQUIRE_EQ(s.stop(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Small model dimensions isolate configured engine capacity from the available model-weight VRAM.
|
||||
TEST(nemotron35_configured_combined_capacity_exceeds_old_gemm_ceiling) {
|
||||
if (!nemotron_device()) SKIP("explicit RAD_NEMOTRON_DEVICE capacity validation only");
|
||||
tiny::TempDir d("e2e_nemotron_capacity");
|
||||
REQUIRE(d.ok());
|
||||
REQUIRE(write_nemotron(d.path));
|
||||
Server s;
|
||||
const std::vector<std::string> args = {"--kernels",
|
||||
"libr4d,libavx",
|
||||
"--max-num-seqs",
|
||||
"512",
|
||||
"--max-num-batched-tokens",
|
||||
"131073",
|
||||
"--max-model-len",
|
||||
"257",
|
||||
"--mm-max-patches",
|
||||
"64"};
|
||||
if (!s.start(d.path, args, nemotron_env())) {
|
||||
fprintf(stderr, " capacity startup log:\n%s\n", s.output().c_str());
|
||||
REQUIRE(false);
|
||||
}
|
||||
std::vector<int> a(256), b(257);
|
||||
for (size_t i = 0; i < a.size(); ++i)
|
||||
a[i] = 3 + i % 6;
|
||||
for (size_t i = 0; i < b.size(); ++i)
|
||||
b[i] = 8 - i % 6;
|
||||
const auto alone_a = embedding_reply(s.port, {{"input", a}});
|
||||
const auto alone_b = embedding_reply(s.port, {{"input", b}});
|
||||
json inputs = json::array();
|
||||
for (int i = 0; i < 511; ++i)
|
||||
inputs.push_back(a);
|
||||
inputs.push_back(b);
|
||||
auto stats = [&]() {
|
||||
Call c;
|
||||
std::string body;
|
||||
if (!c.send(s.port, "GET", "/stats", "") || c.read(&body) != 200) return json();
|
||||
return json::parse(body);
|
||||
};
|
||||
const auto before = stats();
|
||||
int status = 0;
|
||||
const auto reply = post(s.port, "/v1/embeddings", {{"input", inputs}}, &status);
|
||||
REQUIRE_EQ(status, 200);
|
||||
REQUIRE_EQ(reply["data"].size(), (size_t)512);
|
||||
const auto after = stats();
|
||||
CHECK_EQ(after["steps"].get<int64_t>() - before["steps"].get<int64_t>(), 1);
|
||||
CHECK_EQ(reply["usage"]["prompt_tokens"].get<int64_t>(), 131073);
|
||||
double worst = 0;
|
||||
for (int r = 0; r < 512; ++r) {
|
||||
const auto got = reply["data"][r]["embedding"].get<std::vector<float>>();
|
||||
const auto& expected = r == 511 ? alone_b : alone_a;
|
||||
REQUIRE_EQ(got.size(), expected.size());
|
||||
double err = 0;
|
||||
for (size_t j = 0; j < got.size(); ++j)
|
||||
err += std::pow((double)got[j] - expected[j], 2);
|
||||
worst = std::max(worst, std::sqrt(err));
|
||||
}
|
||||
fprintf(
|
||||
stderr,
|
||||
" 131073 combined tokens / 512 sequences / one completed step; worst individual L2 %.9g\n",
|
||||
worst);
|
||||
CHECK(worst <= 1e-5);
|
||||
post(s.port, "/v1/embeddings", {{"input", std::vector<int>(258, 3)}}, &status);
|
||||
CHECK_EQ(status, 400);
|
||||
if (const char* path = std::getenv("RAD_NEMOTRON_CAPACITY_SERVER_LOG"))
|
||||
tiny::write_text(path, s.output());
|
||||
REQUIRE_EQ(s.stop(), 0);
|
||||
}
|
||||
|
||||
TEST(nemotron35_endpoint_decodes_images_and_matches_the_multimodal_oracle) {
|
||||
if (!rad::mm::decoder_available()) SKIP("this build has no image codec");
|
||||
tiny::TempDir d("e2e_nemotron_mm");
|
||||
REQUIRE(d.ok());
|
||||
REQUIRE(write_nemotron(d.path));
|
||||
const auto vision = nemotron_fixture("nemotron35_vision.json");
|
||||
const auto mm = nemotron_fixture("nemotron35_multimodal.json");
|
||||
const json im1 = nemotron_image(vision["images"][1]);
|
||||
REQUIRE(im1.is_object());
|
||||
const json im2 = nemotron_image(vision["images"][2]);
|
||||
REQUIRE(im2.is_object());
|
||||
const json im3 = nemotron_image(vision["images"][3]);
|
||||
REQUIRE(im3.is_object());
|
||||
for (const char* kernels : nemotron_kernels()) {
|
||||
#if !defined(__x86_64__)
|
||||
if (std::string(kernels) == "libavx") continue;
|
||||
#endif
|
||||
Server s;
|
||||
auto args = nemotron_args;
|
||||
args.insert(args.end(), {"--kernels", kernels});
|
||||
if (!s.start(d.path, args, nemotron_env())) {
|
||||
fprintf(stderr, " complete Nemotron startup log:\n%s\n", s.output().c_str());
|
||||
REQUIRE(false);
|
||||
}
|
||||
const json only = {{"input", json::array({im1})}};
|
||||
// Text originally before its image must reach the reference's image-before-text order.
|
||||
const json combined = {{"input", json::array({{{"type", "text"}, {"text", "CDF"}}, im2})}};
|
||||
const json messages = {
|
||||
{{"role", "system"}, {"content", "AB"}},
|
||||
{{"role", "user"},
|
||||
{"content", json::array({{{"type", "text"}, {"text", "CE"}}, im1, im3})}}};
|
||||
for (const auto& c : mm["cases"]) {
|
||||
json req;
|
||||
if (c["name"] == "image_only")
|
||||
req = only;
|
||||
else if (c["name"] == "http_image_text")
|
||||
req = combined;
|
||||
else if (c["name"] == "http_two_images")
|
||||
req = {{"messages", messages}};
|
||||
else
|
||||
continue;
|
||||
check_embedding(embedding_reply(s.port, req), c["pooled"][0]);
|
||||
}
|
||||
const auto alone = embedding_reply(s.port, only);
|
||||
const json mixed = {{"input", json::array({"ABC", json::array({im1})})}};
|
||||
check_embedding(embedding_reply(s.port, mixed, 1), alone);
|
||||
REQUIRE_EQ(s.stop(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* Opt-in for native 1B/8B checkpoints; weights remain external assets. The offline Python oracle writes this JSON;
|
||||
* neither the ordinary suite nor the engine downloads models or invokes Python. */
|
||||
TEST(nemotron35_real_weights_match_the_offline_transformers_reference) {
|
||||
const char* fixture = std::getenv("RAD_NEMOTRON_REAL_FIXTURE");
|
||||
if (!fixture || !*fixture)
|
||||
SKIP("set RAD_NEMOTRON_REAL_FIXTURE to the offline real-weight oracle JSON");
|
||||
#if !defined(__x86_64__)
|
||||
SKIP("this real-weight check uses libavx on x86-64");
|
||||
#endif
|
||||
const auto reference = json::parse(tiny::read_text(fixture));
|
||||
const int dims = reference.value("dimensions", (int)reference["cases"][0]["embedding"].size());
|
||||
REQUIRE(dims == 2048 || dims == 4096);
|
||||
const auto validation = reference.value("validation", json::object());
|
||||
REQUIRE(rad::mm::decoder_available());
|
||||
Server s;
|
||||
REQUIRE(
|
||||
s.start(reference["checkpoint"],
|
||||
{"--kernels", nemotron_device() ? "libr4d,libavx" : "libavx", "--max-num-seqs", "4",
|
||||
"--max-model-len", std::to_string(validation.value("context", 128)),
|
||||
"--max-num-batched-tokens", std::to_string(validation.value("batch_tokens", 256)),
|
||||
"--mm-max-patches", std::to_string(validation.value("patches", 64))},
|
||||
{"RADIANCE_HOST_VRAM_MIB=24576", "OMP_NUM_THREADS=8",
|
||||
nemotron_device() ? nemotron_device_env() : "ROCR_VISIBLE_DEVICES=99"}));
|
||||
json actual = json::array();
|
||||
for (const auto& c : reference["cases"]) {
|
||||
int status = 0;
|
||||
const auto reply = post(s.port, "/v1/embeddings", c["request"], &status);
|
||||
if (status != 200)
|
||||
fprintf(stderr, " %s HTTP %d: %s\n", c["name"].get<std::string>().c_str(), status,
|
||||
reply.dump().c_str());
|
||||
REQUIRE_EQ(status, 200);
|
||||
const auto got = reply["data"][0]["embedding"].get<std::vector<float>>();
|
||||
actual.push_back({{"name", c["name"]}, {"embedding", got}, {"usage", reply["usage"]}});
|
||||
const auto want = c["embedding"].get<std::vector<float>>();
|
||||
double dot = 0, err = 0, n = 0;
|
||||
REQUIRE_EQ(want.size(), (size_t)dims);
|
||||
REQUIRE_EQ(got.size(), want.size());
|
||||
for (size_t i = 0; i < got.size(); ++i) {
|
||||
dot += (double)got[i] * want[i];
|
||||
n += (double)got[i] * got[i];
|
||||
const double d = got[i] - want[i];
|
||||
err += d * d;
|
||||
}
|
||||
fprintf(stderr, " %s: %zu tokens, cosine %.9g, normalized L2 %.9g\n",
|
||||
c["name"].get<std::string>().c_str(), c["tokens"].size(), dot / std::sqrt(n),
|
||||
std::sqrt(err));
|
||||
CHECK_EQ(reply["usage"]["prompt_tokens"].get<size_t>(), c["tokens"].size());
|
||||
// Full-model gates differ from tiny fixtures: PyTorch eager vs SDPA alone reaches
|
||||
// 2.8% normalized L2 on these weights. 3.2% corresponds to the 0.9995 cosine gate.
|
||||
check_embedding(got, want, (int)want.size(), 0.032);
|
||||
}
|
||||
auto cosine = [](const std::vector<float>& x, const std::vector<float>& y) {
|
||||
double dot = 0, xn = 0, yn = 0;
|
||||
for (size_t i = 0; i < x.size(); ++i) {
|
||||
dot += (double)x[i] * y[i];
|
||||
xn += (double)x[i] * x[i];
|
||||
yn += (double)y[i] * y[i];
|
||||
}
|
||||
return dot / std::sqrt(xn * yn);
|
||||
};
|
||||
double score_error = 0;
|
||||
for (size_t i = 0; i < actual.size(); ++i)
|
||||
for (size_t j = 0; j < i; ++j) {
|
||||
const double got = cosine(actual[i]["embedding"], actual[j]["embedding"]);
|
||||
const double want =
|
||||
cosine(reference["cases"][i]["embedding"], reference["cases"][j]["embedding"]);
|
||||
score_error = std::max(score_error, std::abs(got - want));
|
||||
}
|
||||
fprintf(stderr, " maximum pairwise similarity score error %.9g\n", score_error);
|
||||
CHECK(score_error <= 0.005);
|
||||
for (const auto& group : reference.value("ranking_groups", json::array())) {
|
||||
const int q = group["query"], a = group["documents"][0], b = group["documents"][1];
|
||||
const double native =
|
||||
cosine(reference["cases"][q]["embedding"], reference["cases"][a]["embedding"]) -
|
||||
cosine(reference["cases"][q]["embedding"], reference["cases"][b]["embedding"]);
|
||||
const double candidate = cosine(actual[q]["embedding"], actual[a]["embedding"]) -
|
||||
cosine(actual[q]["embedding"], actual[b]["embedding"]);
|
||||
fprintf(stderr, " retrieval %s native margin %.9g candidate %.9g\n",
|
||||
group["language"].get<std::string>().c_str(), native, candidate);
|
||||
if (std::abs(native) > .01) CHECK(native * candidate > 0);
|
||||
}
|
||||
|
||||
// The real scheduler must preserve a ragged mixed text/image batch's row identities.
|
||||
const auto& cases = reference["cases"];
|
||||
const json mixed = {
|
||||
{"input", json::array({cases[1]["request"]["input"], cases[3]["request"]["input"]})}};
|
||||
check_embedding(embedding_reply(s.port, mixed, 0), cases[1]["embedding"], dims, 0.032);
|
||||
check_embedding(embedding_reply(s.port, mixed, 1), cases[3]["embedding"], dims, 0.032);
|
||||
if (nemotron_device()) {
|
||||
auto stats = [&]() {
|
||||
Call c;
|
||||
std::string body;
|
||||
if (!c.send(s.port, "GET", "/stats", "") || c.read(&body) != 200) return json();
|
||||
return json::parse(body);
|
||||
};
|
||||
const auto before = stats();
|
||||
int status = 0;
|
||||
const auto reply =
|
||||
post(s.port, "/v1/embeddings",
|
||||
{{"input", json::array({cases[0]["tokens"], cases[2]["tokens"]})}}, &status);
|
||||
REQUIRE_EQ(status, 200);
|
||||
check_embedding(reply["data"][0]["embedding"], cases[0]["embedding"], dims, 0.032);
|
||||
check_embedding(reply["data"][1]["embedding"], cases[2]["embedding"], dims, 0.032);
|
||||
const auto after = stats();
|
||||
REQUIRE(before.is_object() && after.is_object());
|
||||
fprintf(stderr, " whole-input batch: steps %lld -> %lld, prompt_tokens %zu\n",
|
||||
before["steps"].get<long long>(), after["steps"].get<long long>(),
|
||||
reply["usage"]["prompt_tokens"].get<size_t>());
|
||||
CHECK_EQ(after["steps"].get<long long>() - before["steps"].get<long long>(), 1);
|
||||
CHECK_EQ(reply["usage"]["prompt_tokens"].get<size_t>(),
|
||||
cases[0]["tokens"].size() + cases[2]["tokens"].size());
|
||||
// A disconnected client must not poison the next embedding, including pooled-row reuse.
|
||||
Call abandoned;
|
||||
REQUIRE(abandoned.send(s.port, "POST", "/v1/embeddings",
|
||||
json({{"input", std::vector<int>(120, 3)}}).dump()));
|
||||
::close(abandoned.fd);
|
||||
abandoned.fd = -1;
|
||||
check_embedding(embedding_reply(s.port, cases[0]["request"]), cases[0]["embedding"], dims,
|
||||
0.032);
|
||||
}
|
||||
// Repeated identical geometry reaches cold, recording, audit and playback paths. Changing
|
||||
// input contents at that geometry catches a tape retaining stale metadata or image rows.
|
||||
if (nemotron_device()) {
|
||||
for (int repeat = 0; repeat < 6; ++repeat)
|
||||
for (int i : {0, 1, 3, 4})
|
||||
check_embedding(embedding_reply(s.port, cases[i]["request"]), cases[i]["embedding"],
|
||||
dims, 0.032);
|
||||
}
|
||||
if (nemotron_device()) {
|
||||
Call c;
|
||||
std::string stats;
|
||||
REQUIRE(c.send(s.port, "GET", "/stats", ""));
|
||||
REQUIRE_EQ(c.read(&stats), 200);
|
||||
const auto state = json::parse(stats);
|
||||
fprintf(stderr, " device stats: %s\n", stats.c_str());
|
||||
if (const char* output = std::getenv("RAD_NEMOTRON_STATS_OUTPUT"))
|
||||
REQUIRE(tiny::write_text(output, state.dump(2) + "\n"));
|
||||
}
|
||||
if (const char* output = std::getenv("RAD_NEMOTRON_REAL_OUTPUT"))
|
||||
REQUIRE(tiny::write_text(output, actual.dump(2) + "\n"));
|
||||
REQUIRE_EQ(s.stop(), 0);
|
||||
if (const char* output = std::getenv("RAD_NEMOTRON_SERVER_LOG"))
|
||||
REQUIRE(tiny::write_text(output, s.output()));
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
Reference in new issue
Block a user