diff --git a/docs/OPS.md b/docs/OPS.md index a50cbc3f..5de54f48 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -1444,9 +1444,9 @@ bf16 tensor and keeps f32 where it computes in f32; the libref function says whi | op | params | operands | |---|---|---| -| `rms_mod` | `M`(range) `n` `eps`:f64 `gated`/opt `dtype` `wadd`:f64/opt `norm_once`/opt `norm`/opt | `y`?, `g`?, `s`, `w`w?, `cu_seqlens`, `x`:inout → `z` | -| `rms_mod_quant_fp8` | `M`(range) `n` `eps`:f64 `group` `gated` `dtype` `wadd`:f64/opt `norm_once`/opt `norm`/opt | `y`?, `g`?, `s`, `w`w?, `cu_seqlens`, `x`:inout → `q`, `scale`, `z`? | -| `rms_mod_quant_i8` | `M`(range) `n` `eps`:f64 `group` `gated` `dtype` `wadd`:f64/opt `norm_once`/opt `norm`/opt | `y`?, `g`?, `s`, `w`w?, `cu_seqlens`, `x`:inout → `q`, `scale`, `z`? | +| `rms_mod` | `M`(range) `n` `eps`:f64 `gated`/opt `dtype` `wadd`:f64/opt `norm_once`/opt `norm`/opt | `y`?, `g`?, `s`, `w`w?, `cu_seqlens`, `x`:inout → `z`, `colmap`? | +| `rms_mod_quant_fp8` | `M`(range) `n` `eps`:f64 `group` `gated` `dtype` `wadd`:f64/opt `norm_once`/opt `norm`/opt | `y`?, `g`?, `s`, `w`w?, `cu_seqlens`, `x`:inout → `q`, `scale`, `z`?, `colmap`? | +| `rms_mod_quant_i8` | `M`(range) `n` `eps`:f64 `group` `gated` `dtype` `wadd`:f64/opt `norm_once`/opt `norm`/opt | `y`?, `g`?, `s`, `w`w?, `cu_seqlens`, `x`:inout → `q`, `scale`, `z`?, `colmap`? | | `flow_x0_step` | `M`(range) `n` `dtype` | `v`, `t`, `sigma`, `sigma_next`, `x`:inout | | `flow_res_multistep` | `M`(range) `n` `dtype` | `v`, `x0`:inout, `sigma`, `r`, `hb1`, `hb2`, `order`, `x`:inout | | `patch_unfold` | `M`(range) `C` `pt` `ph` `pw` `mul` `add` `dtype` | `x`, `geom` → `y` | @@ -1474,6 +1474,10 @@ reads its entry's rows of `s` and `g`. `eps`. Without `w` the norm has no gain and rounds once, `n2 = bf16((x - mean) rsqrt(var + eps))` (FLUX.3's LayerNorm without an affine). `norm` `none` takes no norm and no gain, `n2 = x`, and refuses `w` (LTX-2's text keys and values, modulated as they are). + `colmap` (optional, trailing, i32 one per sequence) reads the tables through a column map: every + `[e]` above becomes `[colmap[e]]`, so groups that share a modulation cell address one table row + across non-contiguous boundaries; when it is bound the `s` and `g` row-count checks drop (the + table is sized by columns, not sequences) and the map's own bound is the issuer's. - `rms_mod_quant_fp8` is `rms_mod` with the norm chain in bf16, then `quant_act_fp8` of that bf16 row per `group` as `ln_mod_quant_fp8` takes it; `z` written when bound. - `rms_mod_quant_i8` is the same with `quant_act_i8g` in place of `quant_act_fp8`: symmetric int8 diff --git a/libavx/avx_gen.cpp b/libavx/avx_gen.cpp index ecb26454..bd5acacd 100644 --- a/libavx/avx_gen.cpp +++ b/libavx/avx_gen.cpp @@ -342,13 +342,18 @@ static int rms_mod_run(const RadArgs* a, bool quant) { const bool once = p_int(a, "norm_once", 0) != 0; const char* nk = p_str(a, "norm", "rms"); const bool layer = std::strcmp(nk, "layer") == 0, none = std::strcmp(nk, "none") == 0; + /* The optional trailing column map (slot 9 quant, slot 7 plain): each sequence reads the + * modulation table through its mapped COLUMN, so `s` and `g` may hold fewer rows than there + * are sequences -- the map's own bound is the issuer's, as in libr4d and libref. */ + const RadTensor* cm = rad_arg_in(a, quant ? 9 : 7); if ((!layer && !none && std::strcmp(nk, "rms") != 0) || (none && W)) return RAD_E_INVAL; if (n <= 0 || grp <= 0 || n % grp || (gated && (!Yv || !G))) return RAD_E_SHAPE; const int64_t rows = rows_of(quant ? Q : Z, n), n_seq = numel(cu) - 1, ngrp = n / grp; + if (cm && (cm->dtype != RAD_I32 || numel(cm) < n_seq)) return RAD_E_SHAPE; if (rows <= 0 || n_seq <= 0 || rows_of(X, n) < rows || (W && numel(W) < n) || - numel(S) < n_seq * 2 * n) + (!cm && numel(S) < n_seq * 2 * n)) return RAD_E_SHAPE; - if (gated && (rows_of(Yv, n) < rows || numel(G) < n_seq * n)) return RAD_E_SHAPE; + if (gated && (rows_of(Yv, n) < rows || (!cm && numel(G) < n_seq * n))) return RAD_E_SHAPE; if (quant && (numel(SC) < rows * ngrp || (Z && rows_of(Z, n) < rows))) return RAD_E_SHAPE; const uint32_t dt = quant ? (uint32_t)RAD_BF16 : Z->dtype; /* The tables from one scratch_raw call, [gain | shift | gate] a sequence and the weight row: @@ -358,13 +363,15 @@ static int rms_mod_run(const RadArgs* a, bool quant) { { const int64_t s_rs = row_stride(S, 2 * n), s_cs = laststride(S); const int64_t g_rs = gated ? row_stride(G, n) : 0, g_cs = gated ? laststride(G) : 0; - for (int64_t e = 0; e < n_seq; ++e) + for (int64_t e = 0; e < n_seq; ++e) { + const int64_t col = cm ? (int64_t)ldt(cm, e) : e; /* the sequence's table column */ for (int64_t i = 0; i < n; ++i) { float* row = tabs + e * tw; - row[i] = narrow1(dt, 1.0f + ldt(S, e * s_rs + (n + i) * s_cs)); - row[n + i] = ldt(S, e * s_rs + i * s_cs); - if (gated) row[2 * n + i] = ldt(G, e * g_rs + i * g_cs); + row[i] = narrow1(dt, 1.0f + ldt(S, col * s_rs + (n + i) * s_cs)); + row[n + i] = ldt(S, col * s_rs + i * s_cs); + if (gated) row[2 * n + i] = ldt(G, col * g_rs + i * g_cs); } + } for (int64_t i = 0; i < n; ++i) tabs[n_seq * tw + i] = W ? ldt(W, i * laststride(W)) + wadd : 1.0f; } diff --git a/libavx/avx_registry.cpp b/libavx/avx_registry.cpp index d992c776..fbec2593 100644 --- a/libavx/avx_registry.cpp +++ b/libavx/avx_registry.cpp @@ -562,13 +562,14 @@ static const RadParamSpec pRmsMod[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P P_STR("dtype"), P_F64O("wadd"), P_INTO("norm_once"), P_STRO("norm") }; static const RadOperandSpec oRmsMod[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"), - OPD("cu_seqlens"), INOUT("x"), OUT("z") }; + OPD("cu_seqlens"), INOUT("x"), OUT("z"), + OPD_O("colmap") }; static const RadParamSpec pRmsModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"), P_INT("gated"), P_STR("dtype"), P_F64O("wadd"), P_INTO("norm_once"), P_STRO("norm") }; static const RadOperandSpec oRmsModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"), OPD("cu_seqlens"), INOUT("x"), OUT("q"), OUT("scale"), - OUT_O("z") }; + OUT_O("z"), OPD_O("colmap") }; static const RadOperandSpec oFlowX0[] = { OPD("v"), OPD("t"), OPD("sigma"), OPD("sigma_next"), INOUT("x") }; static const RadOperandSpec oFlowRM[] = { OPD("v"), INOUT("x0"), OPD("sigma"), OPD("r"), diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 03ede0b6..2d1625a3 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -2620,7 +2620,7 @@ static const RadParamSpec pRmsMod[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_ static const RadOperandSpec oRmsMod[] = { { "y", RAD_OPD_IN, 1 }, { "g", RAD_OPD_IN, 1 }, { "s", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 1 }, { "cu_seqlens", RAD_OPD_IN, 0 }, { "x", RAD_OPD_INOUT, 0 }, - { "z", RAD_OPD_OUT, 0 }, + { "z", RAD_OPD_OUT, 0 }, { "colmap", RAD_OPD_IN, 1 }, }; static const RadParamSpec pRmsModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"), P_INT("gated"), P_STR("dtype"), P_F64O("wadd"), @@ -2629,6 +2629,7 @@ static const RadOperandSpec oRmsModQ[] = { { "y", RAD_OPD_IN, 1 }, { "g", RAD_OPD_IN, 1 }, { "s", RAD_OPD_IN, 0 }, { "w", RAD_OPD_WEIGHT, 1 }, { "cu_seqlens", RAD_OPD_IN, 0 }, { "x", RAD_OPD_INOUT, 0 }, { "q", RAD_OPD_OUT, 0 }, { "scale", RAD_OPD_OUT, 0 }, { "z", RAD_OPD_OUT, 1 }, + { "colmap", RAD_OPD_IN, 1 }, }; static const RadOperandSpec oFlowX0[] = { { "v", RAD_OPD_IN, 0 }, { "t", RAD_OPD_IN, 0 }, { "sigma", RAD_OPD_IN, 0 }, diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index 4cdf51e8..2c1da8fe 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -769,13 +769,14 @@ static const RadParamSpec pRmsMod[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P P_STR("dtype"), P_F64O("wadd"), P_INTO("norm_once"), P_STRO("norm") }; static const RadOperandSpec oRmsMod[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"), - OPD("cu_seqlens"), INOUT("x"), OUT("z") }; + OPD("cu_seqlens"), INOUT("x"), OUT("z"), + OPD_O("colmap") }; static const RadParamSpec pRmsModQ[] = { P_INT("M"), P_INT("n"), P_F64("eps"), P_INT("group"), P_INT("gated"), P_STR("dtype"), P_F64O("wadd"), P_INTO("norm_once"), P_STRO("norm") }; static const RadOperandSpec oRmsModQ[] = { OPD_O("y"), OPD_O("g"), OPD("s"), WGT_O("w"), OPD("cu_seqlens"), INOUT("x"), OUT("q"), OUT("scale"), - OUT_O("z") }; + OUT_O("z"), OPD_O("colmap") }; /* `t`, `sigma` and `sigma_next` are one f32 value each. */ static const RadOperandSpec oFlowX0[] = { OPD("v"), OPD("t"), OPD("sigma"), OPD("sigma_next"), INOUT("x") }; diff --git a/libref/ref_shapes.cpp b/libref/ref_shapes.cpp index 14fadb41..341cac90 100644 --- a/libref/ref_shapes.cpp +++ b/libref/ref_shapes.cpp @@ -2597,6 +2597,7 @@ int ref_rms_mod_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) o.t_idx({ 2 }, 0, -1, (uint32_t)RAD_OPD_F_IDX_CU); /* cu_seqlens */ o.t(D, { M, n }); /* x, INOUT */ o.t(D, { M, n }); /* z */ + o.skip(); /* colmap: the sweep passes none */ return o.finish("rms_mod", operand, out); } int ref_rms_mod_quant_fp8_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { @@ -2620,6 +2621,7 @@ int ref_rms_mod_quant_fp8_shape(const RadParam* p, int n_p, int operand, RadOpdD o.t((uint32_t)RAD_F8E4M3, { M, n }); /* q */ o.t((uint32_t)RAD_F32, { M, ng }); /* scale */ o.t(D, { M, n }); /* z */ + o.skip(); /* colmap: the sweep passes none */ return o.finish("rms_mod_quant_fp8", operand, out); } /* The sampler's three scalars in (0, 1), the state f32. */