File size: 1,176 Bytes
d7228c8 | 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 40 41 42 43 44 45 46 47 48 49 | """
Unit tests for SLMForCausalLM model architecture.
"""
import torch
import pytest
from slm.config.model_config import ModelConfig
from slm.model.transformer_lm import SLMForCausalLM
def test_slm_model_forward_and_loss():
config = ModelConfig(
vocab_size=500,
d_model=64,
n_heads=4,
n_layers=2,
d_ff=256,
max_seq_len=128,
norm_type="rmsnorm",
activation="swiglu",
pos_encoding_type="rope"
)
model = SLMForCausalLM(config)
batch, seq = 2, 16
input_ids = torch.randint(0, config.vocab_size, (batch, seq))
targets = torch.randint(0, config.vocab_size, (batch, seq))
logits, loss = model(input_ids, targets=targets)
assert logits.shape == (batch, seq, config.vocab_size)
assert loss is not None
assert isinstance(loss.item(), float)
assert loss.item() > 0.0
def test_weight_tying():
config = ModelConfig(
vocab_size=100,
d_model=32,
n_heads=2,
n_layers=1,
tie_word_embeddings=True
)
model = SLMForCausalLM(config)
assert torch.equal(model.lm_head.weight, model.token_embeddings.embedding.weight)
|