()
| 308 | |
| 309 | |
| 310 | def main(): |
| 311 | torch.manual_seed(42) |
| 312 | |
| 313 | batch_size = 3 |
| 314 | seq_len = 64 |
| 315 | vocab_size = 1000 |
| 316 | embed_dim = 128 |
| 317 | num_heads = 4 |
| 318 | eos_id = 2 |
| 319 | num_docs = 3 |
| 320 | device = "cuda" |
| 321 | dtype = torch.bfloat16 |
| 322 | |
| 323 | model = SimpleVarlenTransformer(vocab_size, embed_dim, num_heads).to( |
| 324 | device=device, dtype=dtype |
| 325 | ) |
| 326 | |
| 327 | # create input_batch tokens |
| 328 | input_batch = torch.randint(0, vocab_size, (batch_size, seq_len), device=device) |
| 329 | |
| 330 | for b in range(batch_size): |
| 331 | # getting random positions to cut the input into multiple documents |
| 332 | doc_positions = torch.randint(10, seq_len - 1, (num_docs - 1,)) |
| 333 | for pos in doc_positions: |
| 334 | input_batch[b, pos] = eos_id # insert eos token to simulate end of sample |
| 335 | input_batch[b, -1] = eos_id |
| 336 | |
| 337 | cu_seq, max_len = create_varlen_metadata(input_batch, eos_id) |
| 338 | print( |
| 339 | f"cu_seq: {cu_seq}, max_len: {max_len}" |
| 340 | ) # cu_seq: tensor([0, 32, 47, 64, 92, 103, 128, 168, 177, 192]), max_len: 40 |
| 341 | |
| 342 | # fwd pass |
| 343 | output = model(input_batch, cu_seq, max_len) |
| 344 | print(f"output shape: {output.shape}") # (3, 64, 128) |
| 345 | |
| 346 | # bwd pass |
| 347 | loss = output.mean() |
| 348 | loss.backward() |
| 349 | |
| 350 | |
| 351 | if __name__ == "__main__": |
no test coverage detected