(N, E_q, E_k, E_v, device)
| 269 | ###################################################################### |
| 270 | # Create nested tensor batch inputs |
| 271 | def gen_batch(N, E_q, E_k, E_v, device): |
| 272 | # generate semi-realistic data using Zipf distribution for sentence lengths |
| 273 | sentence_lengths = zipf_sentence_lengths(alpha=1.2, batch_size=N) |
| 274 | |
| 275 | # Note: the torch.jagged layout is a nested tensor layout that supports a single ragged |
| 276 | # dimension and works with torch.compile. The batch items each have shape (B, S*, D) |
| 277 | # where B = batch size, S* = ragged sequence length, and D = embedding dimension. |
| 278 | query = torch.nested.nested_tensor([ |
| 279 | torch.randn(l.item(), E_q, device=device) |
| 280 | for l in sentence_lengths |
| 281 | ], layout=torch.jagged) |
| 282 | |
| 283 | key = torch.nested.nested_tensor([ |
| 284 | torch.randn(s.item(), E_k, device=device) |
| 285 | for s in sentence_lengths |
| 286 | ], layout=torch.jagged) |
| 287 | |
| 288 | value = torch.nested.nested_tensor([ |
| 289 | torch.randn(s.item(), E_v, device=device) |
| 290 | for s in sentence_lengths |
| 291 | ], layout=torch.jagged) |
| 292 | |
| 293 | return query, key, value, sentence_lengths |
| 294 | |
| 295 | query, key, value, sentence_lengths = gen_batch(N, E_q, E_k, E_v, device) |
| 296 |
no test coverage detected