File size: 1,083 Bytes
9627ce0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import pytest
import torch
from src.data.esm.utils.sampling import sample_logits
def test_sample_logits():
# batched input. temperature != 0.0.
sampled = sample_logits(
logits=torch.randn((64, 8, 4096)), temperature=0.8, valid_ids=list(range(4096))
)
assert sampled.shape == (64, 8)
# batched input. temperature == 0.0.
sampled = sample_logits(
logits=torch.randn((64, 8, 4096)), temperature=0.0, valid_ids=list(range(4096))
)
assert sampled.shape == (64, 8)
# non-batched input. temperature != 0.0.
sampled = sample_logits(
logits=torch.randn((8, 4096)), temperature=0.8, valid_ids=list(range(4096))
)
assert sampled.shape == (8,)
# non-batched input. temperature == 0.0.
sampled = sample_logits(
logits=torch.randn((8, 4096)), temperature=0.0, valid_ids=list(range(4096))
)
assert sampled.shape == (8,)
with pytest.raises(ValueError):
sampled = sample_logits(
logits=torch.randn((8, 4096)), temperature=0.0, valid_ids=[]
)
test_sample_logits()
|