"benchmarks/kernels/cpu/benchmark_cpu_fused_moe.py" did not exist on "22970c16266746971f80fbd8e1fd505a5f35511d"
test_mamba_ssm_ssd.py 19.8 KB
Newer Older
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
3
4
5
6
7
8
9

import pytest
import torch
import torch.nn.functional as F
from einops import rearrange, repeat

from vllm.model_executor.layers.mamba.ops.ssd_combined import (
10
11
    mamba_chunk_scan_combined_varlen,
)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
12
from vllm.platforms import current_platform
13
from vllm.v1.attention.backends.mamba2_attn import compute_varlen_chunk_metadata
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
14
15
16
17
18
19
20
21
22
23
24

# Added by the IBM Team, 2024

# Adapted from https://github.com/state-spaces/mamba/blob/v2.2.4/mamba_ssm/modules/ssd_minimal.py


# this is the segsum implementation taken from above
def segsum(x):
    """Calculates segment sum."""
    T = x.size(-1)
    x = repeat(x, "... d -> ... d e", e=T)
25
    mask = torch.tril(torch.ones(T, T, device=x.device, dtype=bool), diagonal=-1)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
26
27
    x = x.masked_fill(~mask, 0)
    x_segsum = torch.cumsum(x, dim=-2)
28
    mask = torch.tril(torch.ones(T, T, device=x.device, dtype=bool), diagonal=0)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    x_segsum = x_segsum.masked_fill(~mask, -torch.inf)
    return x_segsum


def ssd_minimal_discrete(X, A, B, C, block_len, initial_states=None):
    """
    Arguments:
        X: (batch, length, n_heads, d_head)
        A: (batch, length, n_heads)
        B: (batch, length, n_heads, d_state)
        C: (batch, length, n_heads, d_state)
    Return:
        Y: (batch, length, n_heads, d_head)
    """
    assert X.dtype == A.dtype == B.dtype == C.dtype
    assert X.shape[1] % block_len == 0

    # Rearrange into blocks/chunks
47
48
49
    X, A, B, C = (
        rearrange(x, "b (c l) ... -> b c l ...", l=block_len) for x in (X, A, B, C)
    )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

    A = rearrange(A, "b c l h -> b h c l")
    A_cumsum = torch.cumsum(A, dim=-1)

    # 1. Compute the output for each intra-chunk (diagonal blocks)
    L = torch.exp(segsum(A))
    Y_diag = torch.einsum("bclhn,bcshn,bhcls,bcshp->bclhp", C, B, L, X)

    # 2. Compute the state for each intra-chunk
    # (right term of low-rank factorization of off-diagonal blocks; B terms)
    decay_states = torch.exp(A_cumsum[:, :, :, -1:] - A_cumsum)
    states = torch.einsum("bclhn,bhcl,bclhp->bchpn", B, decay_states, X)

    # 3. Compute the inter-chunk SSM recurrence; produces correct SSM states at
    #    chunk boundaries
    # (middle term of factorization of off-diag blocks; A terms)
    if initial_states is None:
        initial_states = torch.zeros_like(states[:, :1])
    states = torch.cat([initial_states, states], dim=1)
    decay_chunk = torch.exp(segsum(F.pad(A_cumsum[:, :, :, -1], (1, 0))))
    new_states = torch.einsum("bhzc,bchpn->bzhpn", decay_chunk, states)
    states, final_state = new_states[:, :-1], new_states[:, -1]

    # 4. Compute state -> output conversion per chunk
    # (left term of low-rank factorization of off-diagonal blocks; C terms)
    state_decay_out = torch.exp(A_cumsum)
76
    Y_off = torch.einsum("bclhn,bchpn,bhcl->bclhp", C, states, state_decay_out)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
77
78
79
80
81
82
83

    # Add output of intra-chunk and inter-chunk terms
    # (diagonal and off-diagonal blocks)
    Y = rearrange(Y_diag + Y_off, "b c l h p -> b (c l) h p")
    return Y, final_state


84
def generate_random_inputs(batch_size, seqlen, n_heads, d_head, itype, device="cuda"):
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
85
    current_platform.seed_everything(0)
86
    A = -torch.exp(torch.rand(n_heads, dtype=itype, device=device))
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
87
    dt = F.softplus(
88
89
90
91
92
        torch.randn(batch_size, seqlen, n_heads, dtype=itype, device=device) - 4
    )
    X = torch.randn((batch_size, seqlen, n_heads, d_head), dtype=itype, device=device)
    B = torch.randn((batch_size, seqlen, n_heads, d_head), dtype=itype, device=device)
    C = torch.randn((batch_size, seqlen, n_heads, d_head), dtype=itype, device=device)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
93
94
95
96

    return A, dt, X, B, C


97
98
99
100
101
102
103
104
105
106
107
108
def generate_continuous_batched_examples(
    example_lens_by_batch,
    num_examples,
    full_length,
    last_taken,
    exhausted,
    n_heads,
    d_head,
    itype,
    device="cuda",
    return_naive_ref=True,
):
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
109
110
    # this function generates a random examples of certain length
    # and then cut according to "example_lens_by_batch" and feed
111
112
113
114
    # them in continuous batches to the kernels.
    # If if return_naive_ref=True, the naive torch implementation
    # ssd_minimal_discrete will be used to compute and return
    # reference output.
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
115
116

    # generate the full-length example
117
118
119
    A, dt, X, B, C = generate_random_inputs(
        num_examples, full_length, n_heads, d_head, itype
    )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
120

121
    if return_naive_ref:
122
123
124
        Y_min, final_state_min = ssd_minimal_discrete(
            X * dt.unsqueeze(-1), A * dt, B, C, block_len=full_length // 4
        )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
125
126
127
128
129

    # internal function that outputs a cont batch of examples
    # given a tuple of lengths for each example in the batch
    # e.g., example_lens=(8, 4) means take 8 samples from first eg,
    #       4 examples from second eg, etc
130
    def get_continuous_batch(example_lens: tuple[int, ...]):
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
131
132
133
134
135
136
137
        indices = []
        for i, x in enumerate(example_lens):
            c = last_taken.get(i, 0)
            indices.append((c, c + x))
            last_taken[i] = (c + x) % full_length
            exhausted[i] = last_taken[i] == 0

138
139
140
141
        return (
            torch.concat([x[i, s:e] for i, (s, e) in enumerate(indices)]).unsqueeze(0)
            for x in (dt, X, B, C)
        )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    # internal function that maps "n" to the appropriate right boundary
    # value when forming continuous batches from examples of length given
    # by "full_length".
    # - e.g., when n > full_length, returns n % full_length
    #         when n == full_length, returns full_length
    def end_boundary(n: int):
        return n - ((n - 1) // full_length) * full_length

    IND_E = None
    for spec in example_lens_by_batch:
        # get the (maybe partial) example seen in this cont batch
        dt2, X2, B2, C2 = get_continuous_batch(spec)

        # get the metadata
157
158
159
160
161
162
        cu_seqlens = torch.tensor((0,) + spec, device=device).cumsum(dim=0)
        seq_idx = torch.zeros(
            cu_seqlens[-1], dtype=torch.int32, device=cu_seqlens.device
        )
        for i, (srt, end) in enumerate(
            zip(
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
163
164
                cu_seqlens,
                cu_seqlens[1:],
165
166
            )
        ):
167
            seq_idx[srt:end] = i
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
168
169
170
171
172
173
174
175

        # for cont batch
        if IND_E is None:
            IND_S = [0 for _ in range(len(spec))]
        else:
            IND_S = [x % full_length for x in IND_E]
        IND_E = [end_boundary(x + y) for x, y in zip(IND_S, spec)]

176
177
178
179
180
        # varlen has implicit batch=1
        dt2 = dt2.squeeze(0)
        X2 = X2.squeeze(0)
        B2 = B2.squeeze(0)
        C2 = C2.squeeze(0)
181
182
183
184
185
186
187
188
        yield (
            [Y_min[s, IND_S[s] : IND_E[s]] for s in range(num_examples)]
            if return_naive_ref
            else None,
            cu_seqlens,
            seq_idx,
            (A, dt2, X2, B2, C2),
        )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
189
190


191
@pytest.mark.parametrize("itype", [torch.float32, torch.float16, torch.bfloat16])
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
192
193
@pytest.mark.parametrize("n_heads", [3, 4, 11, 16, 32])
@pytest.mark.parametrize("d_head", [5, 8, 19, 32, 128])
194
@pytest.mark.parametrize("seq_len_chunk_size", [(112, 16), (128, 32)])
195
def test_mamba_chunk_scan_single_example(d_head, n_heads, seq_len_chunk_size, itype):
196
    # this tests the kernels on a single example (bs=1)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
197

198
199
200
201
202
203
204
    # TODO: the bfloat16 case requires higher thresholds. To be investigated

    if itype == torch.bfloat16:
        atol, rtol = 5e-2, 5e-2
    else:
        atol, rtol = 8e-3, 5e-3

Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
205
206
207
208
209
210
211
    # set seed
    batch_size = 1  # batch_size
    # ssd_minimal_discrete requires chunk_size divide seqlen
    # - this is only required for generating the reference seqs,
    #   it is not an operational limitation.
    seqlen, chunk_size = seq_len_chunk_size

212
    A, dt, X, B, C = generate_random_inputs(batch_size, seqlen, n_heads, d_head, itype)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
213

214
215
216
    Y_min, final_state_min = ssd_minimal_discrete(
        X * dt.unsqueeze(-1), A * dt, B, C, chunk_size
    )
217

218
219
    cu_seqlens = torch.tensor((0, seqlen), device="cuda").cumsum(dim=0)
    cu_chunk_seqlens, last_chunk_indices, seq_idx_chunks = (
220
221
        compute_varlen_chunk_metadata(cu_seqlens, chunk_size)
    )
222
223
224
225
226
227
    # varlen has implicit batch=1
    X = X.squeeze(0)
    dt = dt.squeeze(0)
    A = A.squeeze(0)
    B = B.squeeze(0)
    C = C.squeeze(0)
228
    Y = torch.empty_like(X)
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    final_state = mamba_chunk_scan_combined_varlen(
        X,
        dt,
        A,
        B,
        C,
        chunk_size,
        cu_seqlens=cu_seqlens.to(torch.int32),
        cu_chunk_seqlens=cu_chunk_seqlens,
        last_chunk_indices=last_chunk_indices,
        seq_idx=seq_idx_chunks,
        out=Y,
        D=None,
    )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
243
244

    # just test the last in sequence
245
    torch.testing.assert_close(Y[-1], Y_min[0, -1], atol=atol, rtol=rtol)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
246
247
248

    # just test the last head
    # NOTE, in the kernel we always cast states to fp32
249
250
251
252
253
254
    torch.testing.assert_close(
        final_state[:, -1].to(torch.float32),
        final_state_min[:, -1].to(torch.float32),
        atol=atol,
        rtol=rtol,
    )
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
255
256
257
258
259
260
261
262
263
264
265
266


@pytest.mark.parametrize("itype", [torch.float32, torch.float16])
@pytest.mark.parametrize("n_heads", [4, 8, 13])
@pytest.mark.parametrize("d_head", [5, 16, 21, 32])
@pytest.mark.parametrize(
    "seq_len_chunk_size_cases",
    [
        # small-ish chunk_size (8)
        (64, 8, 2, [(64, 32), (64, 32)]),
        (64, 8, 2, [(32, 32), (32, 32), (32, 32)]),
        (64, 8, 2, [(8, 8), (8, 8), (8, 8)]),  # chunk size boundary
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
        (
            64,
            8,
            2,
            [(4, 4), (4, 4), (4, 4), (4, 4)],
        ),  # chunk_size larger than cont batches
        (
            64,
            8,
            5,
            [
                (64, 32, 16, 8, 8),
                (8, 16, 32, 16, 8),
                (8, 8, 16, 32, 16),
            ],
        ),  # mode examples with varied lengths
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
283
        # large-ish chunk_size (256)
284
285
286
287
288
289
290
        (64, 256, 1, [(5,), (1,), (1,), (1,)]),  # irregular sizes with small sequences
        (
            64,
            256,
            2,
            [(5, 30), (1, 2), (1, 2), (1, 2)],
        ),  # irregular sizes with small sequences
291
292
293
        # we also need to test some large seqlen
        # to catch errors with init states decay
        (768, 128, 2, [(138, 225), (138, 225)]),
294
295
296
    ],
)
def test_mamba_chunk_scan_cont_batch(d_head, n_heads, seq_len_chunk_size_cases, itype):
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
297
298
299
300
301
    # this test with multiple examples in a continuous batch
    # (i.e. chunked prefill)

    seqlen, chunk_size, num_examples, cases = seq_len_chunk_size_cases

302
303
304
    # This test can have larger error for longer sequences
    if seqlen > 256:
        atol, rtol = 1e-2, 5e-3
305
306
307
    else:
        atol, rtol = 5e-3, 5e-3

Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
308
309
    # hold state during the cutting process so we know if an
    # example has been exhausted and needs to cycle
310
311
    last_taken: dict = {}  # map: eg -> pointer to last taken sample
    exhausted: dict = {}  # map: eg -> boolean indicating example is exhausted
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
312
313

    states = None
314
    for Y_min, cu_seqlens, _token_seq_idx, (
315
316
317
318
319
320
321
322
        A,
        dt,
        X,
        B,
        C,
    ) in generate_continuous_batched_examples(
        cases, num_examples, seqlen, last_taken, exhausted, n_heads, d_head, itype
    ):
323
        cu_chunk_seqlens, last_chunk_indices, seq_idx_chunks = (
324
325
            compute_varlen_chunk_metadata(cu_seqlens, chunk_size)
        )
326

327
        Y = torch.empty_like(X)
328
        new_states = mamba_chunk_scan_combined_varlen(
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
329
330
331
332
333
334
            X,
            dt,
            A,
            B,
            C,
            chunk_size,
335
336
337
338
339
            cu_seqlens=cu_seqlens.to(torch.int32),
            cu_chunk_seqlens=cu_chunk_seqlens,
            last_chunk_indices=last_chunk_indices,
            seq_idx=seq_idx_chunks,
            out=Y,
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
340
341
342
343
344
345
346
            D=None,
            initial_states=states,
        )

        # just test the last in sequence
        for i in range(num_examples):
            # just test one dim and dstate
347
            Y_eg = Y[cu_seqlens[i] : cu_seqlens[i + 1], 0, 0]
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
348
            Y_min_eg = Y_min[i][:, 0, 0]
349
            torch.testing.assert_close(Y_eg, Y_min_eg, atol=atol, rtol=rtol)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
350
351
352
353
354

        # update states
        states = new_states
        for i, clear in exhausted.items():
            if clear:
355
                states[i].fill_(0.0)
Yu Chin Fabian Lim's avatar
Yu Chin Fabian Lim committed
356
                exhausted[i] = False
357
358
359


@pytest.mark.parametrize("chunk_size", [8, 256])
360
361
362
363
364
365
366
367
@pytest.mark.parametrize(
    "seqlens",
    [
        (16, 2, 8, 13),
        (270, 88, 212, 203),
        (16, 20),
    ],
)
368
369
370
371
372
def test_mamba_chunk_scan_cont_batch_prefill_chunking(chunk_size, seqlens):
    # This test verifies the correctness of the chunked prefill implementation
    # in the mamba2 ssd kernels, by comparing concatenation (in the sequence
    # dimension) of chunked results with the full sequence result.
    # It is different from test_mamba_chunk_scan_cont_batch by:
co63oc's avatar
co63oc committed
373
    # 1. Not using the naive torch implementation (ssd_minimal_discrete) to get
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
    #    reference outputs. Instead, it compares chunked kernel outputs to full
    #    sequence kernel outputs. This is the most straightforward way to
    #    assert chunked prefill correctness.
    # 2. It focuses on cases where sequences change in the middle of mamba
    #    chunks, and not necessarily on chunk boundaries.

    max_seqlen = max(seqlens)
    # This test can have larger error for longer sequences
    if max_seqlen > 256:
        atol, rtol = 1e-2, 5e-3
    else:
        atol, rtol = 5e-3, 5e-3

    num_sequences = len(seqlens)
    n_heads = 16
    d_head = 64
    itype = torch.float32

    # hold state during the cutting process so we know if an
    # example has been exhausted and needs to cycle
    last_taken: dict = {}  # map: eg -> pointer to last taken sample
    exhausted: dict = {}  # map: eg -> boolean indicating example is exhausted
    _, cu_seqlens, seq_idx, (A, dt, X, B, C) = next(
397
398
399
400
401
402
403
404
405
406
407
408
        generate_continuous_batched_examples(
            [seqlens],
            num_sequences,
            max_seqlen,
            last_taken,
            exhausted,
            n_heads,
            d_head,
            itype,
            return_naive_ref=False,
        )
    )
409
410
411
412
    seqlens = torch.tensor(seqlens, dtype=torch.int32, device=X.device)
    device = X.device

    ## full seqlen computation
413
    cu_chunk_seqlens, last_chunk_indices, seq_idx_chunks = (
414
415
        compute_varlen_chunk_metadata(cu_seqlens, chunk_size)
    )
416
    Y_ref = torch.empty_like(X)
417
    state_ref = mamba_chunk_scan_combined_varlen(
418
419
420
421
422
423
        X,
        dt,
        A,
        B,
        C,
        chunk_size,
424
425
426
427
428
        cu_seqlens=cu_seqlens.to(torch.int32),
        cu_chunk_seqlens=cu_chunk_seqlens,
        last_chunk_indices=last_chunk_indices,
        seq_idx=seq_idx_chunks,
        out=Y_ref,
429
430
431
432
433
434
435
        D=None,
        initial_states=None,
    )

    ## chunked seqlen computation
    # first chunk
    chunked_seqlens = seqlens // 2
436
437
438
    chunked_cu_seqlens = torch.cat(
        [torch.tensor([0], device=device), torch.cumsum(chunked_seqlens, dim=0)], dim=0
    )
439
    chunked_input_seq_len = chunked_cu_seqlens[-1]
440
441
442
443
    X_chunked = torch.zeros_like(X)[:chunked_input_seq_len, ...]
    dt_chunked = torch.zeros_like(dt)[:chunked_input_seq_len, ...]
    B_chunked = torch.zeros_like(B)[:chunked_input_seq_len, ...]
    C_chunked = torch.zeros_like(C)[:chunked_input_seq_len, ...]
444
445
    for i in range(num_sequences):
        # fmt: off
446
        chunk_f = lambda x, i: x[cu_seqlens[i]:cu_seqlens[i] + chunked_seqlens[i], ...]  # noqa: E501
447

448
449
450
451
        X_chunked[chunked_cu_seqlens[i]:chunked_cu_seqlens[i+1], ...] = chunk_f(X, i)  # noqa: E501
        dt_chunked[chunked_cu_seqlens[i]:chunked_cu_seqlens[i+1], ...] = chunk_f(dt, i)  # noqa: E501
        B_chunked[chunked_cu_seqlens[i]:chunked_cu_seqlens[i+1], ...] = chunk_f(B, i)  # noqa: E501
        C_chunked[chunked_cu_seqlens[i]:chunked_cu_seqlens[i+1], ...] = chunk_f(C, i)  # noqa: E501
452
453
        # fmt: on

454
    cu_chunk_seqlens, last_chunk_indices, seq_idx_chunks = (
455
456
        compute_varlen_chunk_metadata(chunked_cu_seqlens, chunk_size)
    )
457
    Y_partial = torch.empty_like(X_chunked)
458
    partial_state = mamba_chunk_scan_combined_varlen(
459
460
461
462
463
464
        X_chunked,
        dt_chunked,
        A,
        B_chunked,
        C_chunked,
        chunk_size,
465
466
467
468
469
        cu_seqlens=chunked_cu_seqlens.to(torch.int32),
        cu_chunk_seqlens=cu_chunk_seqlens,
        last_chunk_indices=last_chunk_indices,
        seq_idx=seq_idx_chunks,
        out=Y_partial,
470
471
472
473
474
475
        D=None,
        initial_states=None,
    )

    # remaining chunk
    remaining_chunked_seqlens = seqlens - chunked_seqlens
476
477
478
479
480
481
482
    remaining_chunked_cu_seqlens = torch.cat(
        [
            torch.tensor([0], device=device),
            torch.cumsum(remaining_chunked_seqlens, dim=0),
        ],
        dim=0,
    )
483
484
    remaining_chunked_input_seq_len = remaining_chunked_cu_seqlens[-1]
    # fmt: off
485
486
487
488
    remaining_X_chunked = torch.zeros_like(X)[:remaining_chunked_input_seq_len, ...]  # noqa: E501
    remaining_dt_chunked = torch.zeros_like(dt)[:remaining_chunked_input_seq_len, ...]  # noqa: E501
    remaining_B_chunked = torch.zeros_like(B)[:remaining_chunked_input_seq_len, ...]  # noqa: E501
    remaining_C_chunked = torch.zeros_like(C)[:remaining_chunked_input_seq_len, ...]  # noqa: E501
489
    for i in range(num_sequences):
490
        remaining_chunk_f = lambda x, i: x[cu_seqlens[i] + chunked_seqlens[i]:cu_seqlens[i+1], ...]  # noqa: E501
491

492
493
494
495
        remaining_X_chunked[remaining_chunked_cu_seqlens[i]:remaining_chunked_cu_seqlens[i+1], ...] = remaining_chunk_f(X, i)  # noqa: E501
        remaining_dt_chunked[remaining_chunked_cu_seqlens[i]:remaining_chunked_cu_seqlens[i+1], ...] = remaining_chunk_f(dt, i)  # noqa: E501
        remaining_B_chunked[remaining_chunked_cu_seqlens[i]:remaining_chunked_cu_seqlens[i+1], ...] = remaining_chunk_f(B, i)  # noqa: E501
        remaining_C_chunked[remaining_chunked_cu_seqlens[i]:remaining_chunked_cu_seqlens[i+1], ...] = remaining_chunk_f(C, i)  # noqa: E501
496
497
498

    # assert input chunking is correct
    concat_chunk_f = lambda pt1, pt2, i: torch.cat([
499
500
        pt1[chunked_cu_seqlens[i]:chunked_cu_seqlens[i+1],...],
        pt2[remaining_chunked_cu_seqlens[i]:remaining_chunked_cu_seqlens[i+1],...],
501
        ],
502
503
        dim=0)
    concat_batch_f = lambda pt1, pt2: torch.cat([concat_chunk_f(pt1, pt2, i) for i in range(num_sequences)], dim=0)  # noqa: E501
504
505
506
507
508
509
510
    # fmt: on

    assert concat_batch_f(X_chunked, remaining_X_chunked).equal(X)
    assert concat_batch_f(dt_chunked, remaining_dt_chunked).equal(dt)
    assert concat_batch_f(B_chunked, remaining_B_chunked).equal(B)
    assert concat_batch_f(C_chunked, remaining_C_chunked).equal(C)

511
    cu_chunk_seqlens, last_chunk_indices, seq_idx_chunks = (
512
513
        compute_varlen_chunk_metadata(remaining_chunked_cu_seqlens, chunk_size)
    )
514
515

    Y_chunked = torch.empty_like(remaining_X_chunked)
516
    state_chunked = mamba_chunk_scan_combined_varlen(
517
518
519
520
521
522
        remaining_X_chunked,
        remaining_dt_chunked,
        A,
        remaining_B_chunked,
        remaining_C_chunked,
        chunk_size,
523
524
525
526
527
        cu_seqlens=remaining_chunked_cu_seqlens.to(torch.int32),
        cu_chunk_seqlens=cu_chunk_seqlens,
        last_chunk_indices=last_chunk_indices,
        seq_idx=seq_idx_chunks,
        out=Y_chunked,
528
529
530
531
532
533
534
        D=None,
        initial_states=partial_state,
    )
    Y = concat_batch_f(Y_partial, Y_chunked)

    # kernel chunked is same as kernel overall
    for i in range(num_sequences):
535
536
        Y_seq = Y[cu_seqlens[i] : cu_seqlens[i + 1], ...]
        Y_ref_seq = Y_ref[cu_seqlens[i] : cu_seqlens[i + 1], ...]
537
        torch.testing.assert_close(
538
539
            Y_seq[: chunked_seqlens[i], ...],
            Y_ref_seq[: chunked_seqlens[i], ...],
540
541
            atol=atol,
            rtol=rtol,
542
543
            msg=lambda x: f"seq{i} output part1 " + x,
        )  # noqa: B023
544
        torch.testing.assert_close(
545
546
            Y_seq[chunked_seqlens[i] :, ...],
            Y_ref_seq[chunked_seqlens[i] :, ...],
547
548
            atol=atol,
            rtol=rtol,
549
550
            msg=lambda x: f"seq{i} output part2 " + x,
        )  # noqa: B023
551
552
553
554
555
556
557
558

        state_seq = state_chunked[i]
        state_seq_ref = state_ref[i]
        torch.testing.assert_close(
            state_seq,
            state_seq_ref,
            atol=atol,
            rtol=rtol,
559
560
            msg=lambda x: f"seq{i} state " + x,
        )  # noqa: B023