-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_forward.py
More file actions
36 lines (28 loc) · 1.26 KB
/
test_forward.py
File metadata and controls
36 lines (28 loc) · 1.26 KB
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
import pytest
import torch
from modernvbert.models.modernvbert.modeling_modernvbert import ModernVBertForMaskedLM
@pytest.mark.parametrize("dt", [torch.float32, torch.float16])
def test_forward_pass(dt):
model_id = "ModernVBERT/modernvbert"
try:
model = ModernVBertForMaskedLM.from_pretrained(model_id, torch_dtype=dt, trust_remote_code=True)
except Exception as e:
pytest.skip(f"Could not load pretrained model: {e}")
device = torch.device("cpu")
model.to(device)
model.eval()
batch_size = 1
seq_len = 8
vocab_size = model.config.vocab_size
input_ids = torch.randint(low=0, high=vocab_size, size=(batch_size, seq_len), dtype=torch.long, device=device)
attention_mask = torch.ones((batch_size, seq_len), dtype=torch.long, device=device)
try:
with torch.no_grad():
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
except Exception as e:
pytest.fail(f"Forward pass failed: {e}")
logits = getattr(outputs, "logits", outputs[0])
expected_vocab = vocab_size + getattr(model, "out_additional_features", 0)
assert logits.shape == (batch_size, seq_len, expected_vocab)
# model returns logits as float32 for stability
assert logits.dtype == torch.float32