diff --git a/core/format/share.cpp b/core/format/share.cpp index aa75b5ea..8b400d07 100644 --- a/core/format/share.cpp +++ b/core/format/share.cpp @@ -200,7 +200,10 @@ int weight_share(const WeightInfo& w, const RadEncoding& enc, uint32_t rank, con return refuse(fmt("columns [%lld, %lld) of the %s '%s' plane do not fall on whole " "bytes", (long long)pq.col0, (long long)(pq.col0 + pq.cols), rad_dtype_name(p.dtype), p.role)); - if (!s.rects.empty() && pq.cols != s.cols) + /* A vector's stacked parts lie end to end as one row, so they may differ in width (a + * Mamba convolution's bias is [x | B | C]); a matrix's parts stack as rows. */ + const bool vec = rank == 1 && pq.rows == 1; + if (!vec && !s.rects.empty() && pq.cols != s.cols) return refuse("the share's parts differ in width"); PlaneShare::Rect r; r.row0 = pq.row0; @@ -208,11 +211,16 @@ int weight_share(const WeightInfo& w, const RadEncoding& enc, uint32_t rank, con r.byte0 = pq.col0 * bits / 8; r.bytes = rad_enc_row_bytes(p.dtype, pq.cols); s.rects.push_back(r); - s.rows += pq.rows; - s.cols = pq.cols; + if (vec) { + s.rows = 1; + s.cols += pq.cols; + } else { + s.rows += pq.rows; + s.cols = pq.cols; + } } - /* A vector's stacked parts come out as one piece a part, each a row of the share; laid - * end to end they are the declared vector, so only the element count has to agree. */ + /* Laid end to end, a vector's parts are the declared vector, so only the element count + * has to agree. */ const bool fits = rank == 1 ? s.rows * s.cols == w.sel_rows[k] * w.sel_cols[k] : s.rows == w.sel_rows[k] && s.cols == w.sel_cols[k]; if (!fits) diff --git a/tests/share_test.cpp b/tests/share_test.cpp index c7ad2b5b..09c00469 100644 --- a/tests/share_test.cpp +++ b/tests/share_test.cpp @@ -262,6 +262,38 @@ TEST(a_stacked_vector_is_cut_in_every_part) { CHECK(why.find("declared parts") != std::string::npos); } +/* A vector's parts may differ in width: a Mamba convolution's bias is [x | B | C], and a rank takes + * its heads' x and its groups' B and C. The parts lie end to end as one row of the share. */ +TEST(a_stacked_vector_may_have_parts_of_different_widths) { + const RadEncoding e = rad_enc_plain(RAD_BF16); + WeightInfo w; + w.enc = e; + w.enc_known = true; + w.decl.shard = RAD_SHARD_ROW; + w.decl.rank = 1; + w.decl.shape[0] = 12; + w.decl.n_row_parts = 3; + w.decl.row_parts[0] = 8; + w.decl.row_parts[1] = 2; + w.decl.row_parts[2] = 2; + w.n_sel = 1; + w.sel[0] = 0; + w.sel_rows[0] = 1; + w.sel_cols[0] = 12; + /* entry [24]: x 0..15, B 16..19, C 20..23; rank 1 owns x 8..15, B 18..19 and C 22..23 */ + const int64_t shape[1] = { 24 }; + PlaneShare s[RAD_ENC_MAX_PLANES]; + REQUIRE_EQ(weight_share(w, e, 1, shape, 2, 1, s, nullptr), RAD_OK); + REQUIRE_EQ(s[0].rects.size(), (size_t)3); + CHECK_EQ(s[0].rects[0].byte0, (int64_t)(8 * 2)); + CHECK_EQ(s[0].rects[0].bytes, (int64_t)(8 * 2)); + CHECK_EQ(s[0].rects[1].byte0, (int64_t)(18 * 2)); + CHECK_EQ(s[0].rects[1].bytes, (int64_t)(2 * 2)); + CHECK_EQ(s[0].rects[2].byte0, (int64_t)(22 * 2)); + CHECK_EQ(s[0].rects[2].bytes, (int64_t)(2 * 2)); + CHECK_EQ(s[0].dense_bytes(), (int64_t)(12 * 2)); +} + /* A reshape: a conv kernel [O, C, T, H, W] declared as the [O, C*T*H*W] matrix it is. The bytes * do not move, so the share is the whole plane in the declared view; a shard of a reshape would * be a different cut, so it is refused. */