MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / test_mea

Function test_mea

SwissArmyTransformer/tests/test_mea.py:4–18  ·  view source on GitHub ↗
(seq_len, hidden_size, num_heads, batch_size=8, qkv=None)

Source from the content-addressed store, hash-verified

2import torch
3
4def test_mea(seq_len, hidden_size, num_heads, batch_size=8, qkv=None):
5 torch.cuda.manual_seed(0)
6 q, k, v = qkv.clone().cuda().requires_grad_(True)
7 # profile time and memory
8 torch.cuda.reset_peak_memory_stats()
9 with torch.autograd.profiler.profile(use_cuda=True, profile_memory=True) as prof:
10 out = memory_efficient_attention(q, k, v)
11 out.sum().backward()
12 print(prof.key_averages().table(sort_by="self_cuda_memory_usage", row_limit=10))
13
14 print(torch.cuda.max_memory_allocated() / 1024 ** 3)
15 # clean cuda cache
16 del q, k, v
17 torch.cuda.empty_cache()
18 return out
19
20def attention(query, key, value):
21 scale = 1 / query.shape[-1] ** 0.5

Callers

nothing calls this directly

Calls 3

printFunction · 0.50
backwardMethod · 0.45

Tested by

no test coverage detected