Archived
Add BF16 Spark 2.5 checkpoint and decoder
This commit is contained in:
1 parent
a0f9eb29c0
commit
e56d948889
5 files changed
+527
-3
No files matched your search
@@ -15,7 +15,7 @@
|
||||
# builder. With the archive linked, a new ABI call missing from the fake would pull in
|
||||
# rad_builder.cpp.o and collide on every rad_decl_* symbol; without it the error is a single
|
||||
# undefined reference.
|
||||
set(RAD_TESTS_NO_CORE arch_test glm5next_test nemotron35_test)
|
||||
set(RAD_TESTS_NO_CORE arch_test glm5next_test nemotron35_test spark25_arch_test)
|
||||
|
||||
# fixture_test tests tools/kfixture.cpp and tools/opshapes.cpp (the seeded draw, the reduction and
|
||||
# the on-disk recording) directly, kbench_test tools/kbench_core.cpp (rad-kbench's rules), and
|
||||
@@ -324,12 +324,14 @@ set(RAD_E2E_DEPS radiance rad-convert libref libquant nemotron35_embed qwen35_bf
|
||||
if(TARGET libavx)
|
||||
list(APPEND RAD_E2E_DEPS libavx)
|
||||
endif()
|
||||
foreach(t engine_e2e_test convert_test clef_decision_test laya_decision_test)
|
||||
foreach(t engine_e2e_test convert_test clef_decision_test laya_decision_test spark25_convert_test)
|
||||
if(TARGET ${t})
|
||||
target_compile_definitions(${t} PRIVATE
|
||||
RAD_E2E_SERVER="$<TARGET_FILE:radiance>" RAD_E2E_CONVERT="$<TARGET_FILE:rad-convert>"
|
||||
RAD_E2E_HOME="${RAD_PLUGIN_OUT}" RAD_E2E_RECIPES="${CMAKE_SOURCE_DIR}/data/recipes")
|
||||
if(t STREQUAL "clef_decision_test")
|
||||
if(t STREQUAL "spark25_convert_test")
|
||||
add_dependencies(${t} rad-convert libref spark25_bf16)
|
||||
elseif(t STREQUAL "clef_decision_test")
|
||||
# The tiny clef's golden (tests/mkclefgolden.py) and the one plugin a clef container selects.
|
||||
target_compile_definitions(${t} PRIVATE
|
||||
RAD_CLEF_GOLDEN="${CMAKE_CURRENT_SOURCE_DIR}/data/golden/clef_tiny.json")
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/* Spark's declaration and step, without a GPU or checkpoint. */
|
||||
#define RAD_ARCH_NO_EXPORTS 1
|
||||
#include "arch_fakes.h"
|
||||
#include "../arch/spark25_bf16/spark25_bf16.cpp"
|
||||
|
||||
using namespace rad_fake;
|
||||
|
||||
static MetaKV spark_meta() {
|
||||
RadModelMeta m{};
|
||||
m.arch_id = "spark25";
|
||||
m.n_layers = 4;
|
||||
m.n_embd = 2560;
|
||||
m.n_head = 16;
|
||||
m.n_head_kv = 4;
|
||||
m.head_dim = 256;
|
||||
m.n_ff = 10240;
|
||||
m.n_vocab = 131072;
|
||||
m.n_ctx_train = 1048576;
|
||||
m.rms_eps = 1e-6f;
|
||||
m.rope_theta = 10000;
|
||||
MetaKV k(m);
|
||||
k.set("tie_word_embeddings", "1").set("headwise_attn_output_gate", "1")
|
||||
.set("gate_attn_act_mode", "sigmoid").set("hidden_act", "gelu")
|
||||
.set("layer_types", "sliding_attention sliding_attention sliding_attention full_attention")
|
||||
.set("sliding_window", "512")
|
||||
.set("rope_parameters.sliding_attention.rope_theta", "10000")
|
||||
.set("rope_parameters.sliding_attention.partial_rotary_factor", "1")
|
||||
.set("rope_parameters.full_attention.rope_theta", "5000000")
|
||||
.set("rope_parameters.full_attention.partial_rotary_factor", "0.25");
|
||||
return k;
|
||||
}
|
||||
|
||||
TEST(layer_schedule_rope_and_window_follow_the_checkpoint) {
|
||||
MetaKV k = spark_meta();
|
||||
RadBuilder b;
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
REQUIRE_EQ(spark25_bf16::declare(&b, k.meta(), &ctx), RAD_OK);
|
||||
REQUIRE_EQ(count_op(b, "attn_paged"), 4);
|
||||
REQUIRE_EQ(count_op(b, "rope"), 8);
|
||||
REQUIRE_EQ(b.binds.size(), (size_t)4);
|
||||
for (int l = 0; l < 4; ++l) {
|
||||
const RecOp* a = find_op(b, "attn_paged", l);
|
||||
const RecOp* q = find_op(b, "rope", 2*l);
|
||||
const RecOp* key = find_op(b, "rope", 2*l+1);
|
||||
REQUIRE(a && q && key);
|
||||
CHECK_EQ(pint(a, "window"), l == 3 ? 0 : 512);
|
||||
CHECK_EQ(pint(a, "gqa"), 4);
|
||||
CHECK_EQ(pint(q, "rotary_dim"), l == 3 ? 64 : 256);
|
||||
CHECK_EQ(pint(key, "rotary_dim"), l == 3 ? 64 : 256);
|
||||
CHECK_EQ(pflt(q, "theta"), l == 3 ? 5000000.0 : 10000.0);
|
||||
CHECK_EQ(pflt(key, "theta"), pflt(q, "theta"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uses_fp32_residual_exact_gelu_and_one_sigmoid_per_head) {
|
||||
MetaKV k = spark_meta();
|
||||
RadBuilder b;
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
REQUIRE_EQ(spark25_bf16::declare(&b, k.meta(), &ctx), RAD_OK);
|
||||
const RecBuf* x = find_b(b, "x");
|
||||
REQUIRE(x);
|
||||
CHECK_EQ(x->d.dtype, (uint32_t)RAD_F32);
|
||||
CHECK_EQ(count_op(b, "sigmoid"), 4);
|
||||
CHECK_EQ(count_op(b, "scale_rows"), 4);
|
||||
CHECK_EQ(count_op(b, "act_mul"), 4);
|
||||
for (int l = 0; l < 4; ++l) {
|
||||
CHECK_EQ(pstr(find_op(b, "act_mul", l), "act"), std::string("gelu"));
|
||||
CHECK_EQ(pint(find_op(b, "scale_rows", l), "n"), 256);
|
||||
CHECK_EQ(pstr(find_op(b, "scale_rows", l), "act"), std::string("none"));
|
||||
}
|
||||
for (const auto& o : b.ops) if (o.resolved) CHECK(o.uses_declared);
|
||||
for (const auto& w : b.weights) if (!w.d.source) CHECK(find_map(b, w.name) != nullptr);
|
||||
}
|
||||
|
||||
TEST(prefill_and_decode_issue_a_liveness_safe_graph) {
|
||||
MetaKV k = spark_meta();
|
||||
RadBuilder b;
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
REQUIRE_EQ(spark25_bf16::declare(&b, k.meta(), &ctx), RAD_OK);
|
||||
for (int n : {1, 8}) {
|
||||
RadBatch batch = decode_batch();
|
||||
batch.n_tok = n;
|
||||
batch.max_q_len = n;
|
||||
RadCtx c{.b = &b, .batch = &batch};
|
||||
spark25_bf16::step(&c, &batch);
|
||||
CHECK_LINT(b, &c);
|
||||
CHECK(c.step_fail.empty());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(refuses_unimplemented_variants) {
|
||||
RadBuildCtx ctx = build_ctx();
|
||||
for (const auto& change : {std::pair{"tie_word_embeddings", "0"},
|
||||
std::pair{"headwise_attn_output_gate", "0"},
|
||||
std::pair{"gate_attn_act_mode", "silu"},
|
||||
std::pair{"hidden_act", "silu"},
|
||||
std::pair{"attention_bias", "1"},
|
||||
std::pair{"layer_types", "sliding_attention"},
|
||||
std::pair{"rope_parameters.full_attention.partial_rotary_factor", "0.3"}}) {
|
||||
MetaKV k = spark_meta();
|
||||
k.set(change.first, change.second);
|
||||
RadBuilder b;
|
||||
CHECK(stderr_of_rc([&] { return spark25_bf16::declare(&b, k.meta(), &ctx); }) != RAD_OK);
|
||||
}
|
||||
MetaKV k = spark_meta();
|
||||
RadBuilder b;
|
||||
ctx.world_size = 2;
|
||||
CHECK(stderr_of_rc([&] { return spark25_bf16::declare(&b, k.meta(), &ctx); }) == RAD_E_UNSUPPORTED);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -0,0 +1,81 @@
|
||||
/* A small Spark checkpoint converted with the production name map. */
|
||||
#include "rad_test.h"
|
||||
#include "tiny_model.h"
|
||||
#include "format/radfile.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
using namespace rad;
|
||||
|
||||
TEST(bf16_conversion_preserves_qkv_rows_gate_up_and_tied_head) {
|
||||
tiny::TempDir d("spark25_convert");
|
||||
REQUIRE(d.ok());
|
||||
const std::string ckpt = d / "model";
|
||||
REQUIRE(std::filesystem::create_directory(ckpt));
|
||||
constexpr int H = 32, Q = 32, K = 16, F = 64, V = 320;
|
||||
nlohmann::ordered_json config = {
|
||||
{"model_type", "spark2_5"}, {"architectures", {"Spark2_5ForCausalLM"}},
|
||||
{"hidden_size", H}, {"num_hidden_layers", 2}, {"num_attention_heads", 4},
|
||||
{"num_key_value_heads", 2}, {"head_dim", 8}, {"intermediate_size", F},
|
||||
{"vocab_size", V}, {"max_position_embeddings", 64}, {"rms_norm_eps", 1e-6},
|
||||
{"tie_word_embeddings", true}, {"headwise_attn_output_gate", true},
|
||||
{"gate_attn_act_mode", "sigmoid"}, {"hidden_act", "gelu"}, {"sliding_window", 4},
|
||||
{"layer_types", {"sliding_attention", "full_attention"}},
|
||||
{"rope_parameters", {{"sliding_attention", {{"rope_theta", 10000}, {"partial_rotary_factor", 1.0}}},
|
||||
{"full_attention", {{"rope_theta", 5000000}, {"partial_rotary_factor", 0.5}}}}}
|
||||
};
|
||||
tiny::Rng r(123);
|
||||
std::vector<tiny::Tensor> ts;
|
||||
ts.push_back(tiny::noise("model.embedding.weight", {V, H}, r, 0.2f));
|
||||
ts.push_back(tiny::noise("model.norm.weight", {H}, r, 0.1f, 1.0f));
|
||||
for (int l = 0; l < 2; ++l) {
|
||||
const std::string p = "model.layers." + std::to_string(l) + ".";
|
||||
ts.push_back(tiny::noise(p + "input_layernorm.weight", {H}, r, 0.1f, 1.0f));
|
||||
ts.push_back(tiny::noise(p + "post_attention_layernorm.weight", {H}, r, 0.1f, 1.0f));
|
||||
ts.push_back(tiny::linear(p + "self_attn.q_k_v_proj.weight", Q + 2*K, H, r));
|
||||
ts.push_back(tiny::linear(p + "self_attn.g_proj.weight", 4, H, r));
|
||||
ts.push_back(tiny::linear(p + "self_attn.out_proj.weight", H, Q, r));
|
||||
ts.push_back(tiny::linear(p + "mlp.gate_proj.weight", F, H, r));
|
||||
ts.push_back(tiny::linear(p + "mlp.up_proj.weight", F, H, r));
|
||||
ts.push_back(tiny::linear(p + "mlp.down_proj.weight", H, F, r));
|
||||
}
|
||||
REQUIRE(tiny::write_checkpoint(ckpt, config, ts));
|
||||
std::map<std::string, std::string> source;
|
||||
for (const auto& t : ts) source[t.name] = std::string(t.bytes.begin(), t.bytes.end());
|
||||
std::string log;
|
||||
const int st = tiny::run({RAD_E2E_CONVERT, ckpt, "-o", d / "model.rad", "--max-ctx", "64",
|
||||
"--max-tok", "16"},
|
||||
{std::string("RADIANCE_HOME=") + RAD_E2E_HOME,
|
||||
"ROCR_VISIBLE_DEVICES=99", "OMP_NUM_THREADS=2", "OMP_WAIT_POLICY=passive"},
|
||||
&log);
|
||||
if (st) fprintf(stderr, "%s\n", log.c_str());
|
||||
REQUIRE_EQ(st, 0);
|
||||
RadFile f;
|
||||
REQUIRE_EQ(f.open((d / "model.rad").c_str()), RAD_OK);
|
||||
CHECK_EQ(std::string(f.str(f.header().arch_id)), std::string("spark25"));
|
||||
CHECK_EQ(f.entry_count(), (int64_t)21);
|
||||
auto check = [&](const std::string& name, const std::string& bytes) {
|
||||
const RadFileEntry* e = f.find(name);
|
||||
if (!e) { CHECK(e != nullptr); return; }
|
||||
CHECK_EQ(e->n_planes, (uint32_t)1);
|
||||
const RadFilePlane& p = f.plane(*e, 0);
|
||||
CHECK_EQ(p.bytes, (uint64_t)bytes.size());
|
||||
if (p.bytes == bytes.size()) CHECK(std::memcmp(f.data(p), bytes.data(), bytes.size()) == 0);
|
||||
};
|
||||
check("token_embd.weight", source.at("model.embedding.weight"));
|
||||
check("output.weight", source.at("model.embedding.weight"));
|
||||
for (int l = 0; l < 2; ++l) {
|
||||
const std::string p = "model.layers." + std::to_string(l) + ".";
|
||||
const std::string name = "blk." + std::to_string(l) + ".";
|
||||
const std::string& qkv = source.at(p + "self_attn.q_k_v_proj.weight");
|
||||
const size_t row = H * 2;
|
||||
check(name + "attn_q.weight", qkv.substr(0, Q*row));
|
||||
check(name + "attn_k.weight", qkv.substr(Q*row, K*row));
|
||||
check(name + "attn_v.weight", qkv.substr((Q+K)*row, K*row));
|
||||
check(name + "ffn_gate_up.weight", source.at(p + "mlp.gate_proj.weight") +
|
||||
source.at(p + "mlp.up_proj.weight"));
|
||||
check(name + "attn_gate.weight", source.at(p + "self_attn.g_proj.weight"));
|
||||
}
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
Reference in new issue
Block a user