-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#0: Support any sequence length in Mamba prefill demo
- Loading branch information
Showing
8 changed files
with
136 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from loguru import logger | ||
import pytest | ||
import torch | ||
import ttnn | ||
|
||
from tests.tt_eager.python_api_testing.sweep_tests.comparison_funcs import ( | ||
comp_allclose, | ||
comp_pcc, | ||
) | ||
from models.demos.mamba.tt.preprocessing import split_sequence_length | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"layout", | ||
[ttnn.ROW_MAJOR_LAYOUT], | ||
) | ||
@pytest.mark.parametrize( | ||
"B, L, chunk_size, num_chunks", | ||
( | ||
(32, 32, 32, 1), | ||
(32, 64, 32, 2), | ||
(32, 128, 32, 4), | ||
(32, 128, 32, 2), | ||
(32, 1024, 32, 8), | ||
), | ||
) | ||
def test_splitting_sequence_length( | ||
B: int, L: int, chunk_size: int, num_chunks: int, layout: ttnn.Layout, device: ttnn.Device | ||
): | ||
expected = torch.randint(0, 255, (1, 1, B, L), dtype=torch.int32) | ||
|
||
x = ttnn.from_torch(expected, dtype=ttnn.int32, device=device, layout=layout) | ||
|
||
result = [] | ||
for batch_idx in range(B): | ||
chunks = [] | ||
for chunk in split_sequence_length(x, batch=batch_idx, chunk_size=chunk_size): | ||
assert list(chunk.shape) == [1, 1, 1, chunk_size] | ||
chunks.append(chunk) | ||
result.append(ttnn.to_torch(ttnn.concat(chunks, dim=-1))) | ||
|
||
actual = torch.concat(result, dim=-2) | ||
assert actual.shape == x.shape, "Expected input shape to match output shape" | ||
|
||
does_pass, output_pcc = comp_pcc(expected, actual, 1.0) | ||
logger.info(f"PCC value: {output_pcc}") | ||
assert does_pass, f"PCC value ({output_pcc}) is lower than 1.0" | ||
|
||
does_pass, output_allclose = comp_allclose(expected, actual) | ||
assert does_pass, "Allclose check failed: {output_allclose}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc. | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import ttnn | ||
|
||
|
||
def split_sequence_length(x, batch: int = 0, chunk_size: int = 32): | ||
""" | ||
Generator function to yield chunks of a tensor of shape (1, 1, B, L) into (1, 1, 1, chunk_size). | ||
Parameters: | ||
tensor (torch.Tensor): The input tensor of shape (1, 1, B, L). | ||
batch (int): The batch dimension to select. Default is 0. | ||
chunk_size (int): The size of each chunk along the third dimension. Default is 32. | ||
Yields: | ||
torch.Tensor: Chunks of the input tensor of shape (1, 1, 1, chunk_size). | ||
""" | ||
|
||
assert x.layout == ttnn.ROW_MAJOR_LAYOUT, f"Expected input to be row-major layout (was {x.layout})" | ||
assert len(x.shape) == 4, f"Expected input to be rank 4 (was {x.shape})" | ||
assert x.shape[3] % 32 == 0, "Sequence length size must be multiple of 32" | ||
|
||
assert chunk_size % 32 == 0, "Chunk size must be multiple of 32" | ||
|
||
_, _, B, L = x.shape | ||
|
||
assert batch < B, f"Expected batch index (was {batch}) to be less than the size of batch dimension (was {B})" | ||
|
||
for i in range(0, L, chunk_size): | ||
slice_start = (0, 0, batch, i) | ||
slice_end = (0, 0, batch, i + chunk_size - 1) | ||
yield ttnn.slice(x, ttnn.Shape(slice_start), ttnn.Shape(slice_end)) |
48 changes: 0 additions & 48 deletions
48
tests/nightly/wh_b0_only_eth/models/demos/mamba/tests/test_cache.py
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/nightly/wh_b0_only_eth/models/demos/mamba/tests/test_cache.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../../../models/demos/mamba/tests/test_cache.py |
1 change: 1 addition & 0 deletions
1
tests/nightly/wh_b0_only_eth/models/demos/mamba/tests/test_preprocessing.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../../../models/demos/mamba/tests/test_preprocessing.py |