diff --git a/npu/forward/include/q4nx_forward.h b/npu/forward/include/q4nx_forward.h index be495fe27..8b7fd09db 100644 --- a/npu/forward/include/q4nx_forward.h +++ b/npu/forward/include/q4nx_forward.h @@ -70,6 +70,10 @@ struct I8Ctx; // Q4nxForwardConfig now lives with the model that owns it: npu/model/npu_model.h (engine#803 A3). +// engine#1740 W2: the rope_type "dynamic" reader, exposed so a host test can pin it against the exact block the +// Hunyuan 1.9 packs carry ({"type": "dynamic", "factor": 1.0} with max_position_embeddings 262144) without a device. +Q4nxRopeDynamic read_rope_dynamic(const std::string& cfg_path); + class Q4nxNpuForward { public: Q4nxNpuForward(); diff --git a/npu/forward/src/q4nx_forward.cpp b/npu/forward/src/q4nx_forward.cpp index aa8179083..aaccf6ee2 100644 --- a/npu/forward/src/q4nx_forward.cpp +++ b/npu/forward/src/q4nx_forward.cpp @@ -196,7 +196,7 @@ static int dequant_any(const void* data, size_t nbytes, int rows, int cols, floa // Dynamic NTK: `rope_scaling: {type: "dynamic"|"dynamic_ntk", factor: F}` and `max_position_embeddings`. Hunyuan's // 1.9 packs declare factor 1.0 with a 262,144 window, i.e. a no-op until a prompt passes that window -- which is why // accepting it is safe rather than a guess: below the window the base IS theta, so the served bytes are plain rope. -static Q4nxRopeDynamic read_rope_dynamic(const std::string& cfg_path) { +Q4nxRopeDynamic read_rope_dynamic(const std::string& cfg_path) { Q4nxRopeDynamic d{}; std::ifstream f(cfg_path); if (!f) return d; @@ -4266,7 +4266,11 @@ bool Q4nxNpuForward::attention(const std::vector& qkv, int pos, const int // Dynamic NTK: the base moves with the position, so it is one pow per position rather than one per dim pair. // Below the model's original window this returns theta unchanged, which is what makes a short prompt // bit-identical to the same model served without the scaling. - const double rope_theta = q4nx_rope_dynamic_base((double)theta, &rope_dynamic_, (double)pos, (int)HD); + // engine#1792 review: the exponent is HF's `dim / (dim - 2)` over the ROTARY width (`dim = int(head_dim * + // partial_rotary_factor)`), which is `rot` here -- passing `HD` was wrong for any model that declares dynamic + // scaling together with partial_rotary_factor < 1. Unreachable in today's catalog, so this is a correctness + // constant rather than a behaviour change for any pack that exists. + const double rope_theta = q4nx_rope_dynamic_base((double)theta, &rope_dynamic_, (double)pos, rot); for (int i = 0; i < half; i++) { double inv; if (proportional) { diff --git a/tests/npu_rope_llama3_test.c b/tests/npu_rope_llama3_test.c index 8f57734c1..dd261d2df 100644 --- a/tests/npu_rope_llama3_test.c +++ b/tests/npu_rope_llama3_test.c @@ -195,6 +195,21 @@ static void test_declared_type(void) { q4nx_rope_declared_type((const char*)0, none, (int)sizeof none); check(none[0] == '\0', "null config declares no type"); check(q4nx_rope_type_supported(none, (const Q4nxRopeLlama3*)0, 0, 0) == 1, "no type is supported"); + /* engine#1740 W2: the declared-type extraction must SEE Hunyuan's block, because everything downstream (the + * reader, the predicate, the base rule) is gated on this string. A config that declares dynamic must report it, + * and one that declares nothing must report nothing -- the second half is what keeps every other model on plain + * rope. */ + { + const char* hy = "{\"rope_scaling\": {\"type\": \"dynamic\", \"factor\": 1.0}, \"max_position_embeddings\": 262144}"; + char t2[64] = {0}; + q4nx_rope_declared_type(hy, t2, (int)sizeof t2); + check(!strcmp(t2, "dynamic"), "a config declaring rope_scaling dynamic is read as dynamic"); + const char* none = "{\"max_position_embeddings\": 32768}"; + char t3[64] = {1}; + q4nx_rope_declared_type(none, t3, (int)sizeof t3); + check(t3[0] == 0, "and a config with no rope_scaling block reports nothing"); + } + /* engine#1740 W2: dynamic NTK. The support predicate accepts it ONLY when the numbers were read, and the base * rule is the plain theta until a position passes the model's own window -- which is the property that makes * accepting Hunyuan's factor-1.0 block safe rather than a guess. */ @@ -221,6 +236,27 @@ static void test_declared_type(void) { check(q4nx_rope_dynamic_base(1e4, &bad2, 1e6, 128) == 1e4, "an unusable window leaves the base alone"); Q4nxRopeDynamic off = {0, 4.0, 8192.0}; check(q4nx_rope_dynamic_base(1e4, &off, 1e6, 128) == 1e4, "and off is a no-op"); + /* THE EXPONENT, against a KNOWN VALUE (review of #1792). Every arm above asserts a property -- "below the window + * the base is theta, past it not below theta" -- and every one of them is satisfied by ANY exponent, which is how + * a wrong rotary width (the caller passing head_dim instead of the rotary width) survived a green suite. With + * factor 1.0 the rule collapses to theta * (pos/original)^(dim/(dim-2)), so one printed constant pins it: + * pos 524288, original 262144 -> ratio 2; dim 64 -> 1e6 * 2^(64/62) = 2.0435e6 + * and dim 128 -> 1e6 * 2^(128/126) = 2.0219e6, i.e. the arm distinguishes the two widths the bug confused. */ + { + Q4nxRopeDynamic k = {1, 1.0, 262144.0}; + const double want64 = 1e6 * pow(2.0, 64.0 / 62.0); + const double want128 = 1e6 * pow(2.0, 128.0 / 126.0); + const double got64 = q4nx_rope_dynamic_base(1e6, &k, 524288.0, 64); + const double got128 = q4nx_rope_dynamic_base(1e6, &k, 524288.0, 128); + check(fabs(got64 - want64) < 1e-6 * want64, + "the base at twice the window with dim 64 is theta * 2^(64/62), to 1e-6 relative"); + check(fabs(got128 - want128) < 1e-6 * want128, + "... and with dim 128 it is theta * 2^(128/62)"); + /* the pair is the point: if the exponent were ignored, or the width were not passed through, these would be + * equal -- so this asserts the arms CAN tell the rotary width from the head dim. */ + check(fabs(got64 - got128) > 1e3, + "and the two widths differ by more than 1e3, so this arm cannot pass with the widths confused"); + } } }