module wrapper
Browse files- aoti_attention.py +18 -0
aoti_attention.py
CHANGED
|
@@ -64,6 +64,24 @@ def _ns() -> torch.library.Library:
|
|
| 64 |
return lib
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
def sparse_attention_functional(
|
| 68 |
query: torch.Tensor,
|
| 69 |
key: torch.Tensor,
|
|
|
|
| 64 |
return lib
|
| 65 |
|
| 66 |
|
| 67 |
+
def sparse_module(topk: int, num_prefix_tiles: int) -> torch.nn.Module:
|
| 68 |
+
"""`torch.export.export` requires an `nn.Module`; wrap the functional with baked scalars."""
|
| 69 |
+
|
| 70 |
+
class _SparseModule(torch.nn.Module):
|
| 71 |
+
def __init__(self):
|
| 72 |
+
super().__init__()
|
| 73 |
+
self.topk = topk
|
| 74 |
+
self.num_prefix_tiles = num_prefix_tiles
|
| 75 |
+
|
| 76 |
+
def forward(self, q, k, v, gate, untile_index, variable_block_sizes, tile_divisor):
|
| 77 |
+
return sparse_attention_functional(
|
| 78 |
+
q, k, v, gate, untile_index, variable_block_sizes, tile_divisor,
|
| 79 |
+
self.topk, self.num_prefix_tiles,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
return _SparseModule()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
def sparse_attention_functional(
|
| 86 |
query: torch.Tensor,
|
| 87 |
key: torch.Tensor,
|