Skip to content

Commit aa681dd

Browse files
committed
test(train): assert training batches keep padding masked
1 parent 2e3968c commit aa681dd

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

tests/test_trainable.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,19 @@ def test_textdataset_init_incorrect() -> None:
147147
TextDataset([[0]], torch.arange(2))
148148

149149

150+
def test_training_batch_padding_is_masked(mock_vectors: np.ndarray, mock_tokenizer: Tokenizer) -> None:
151+
"""Training batches should pad with the model's pad id, so padding stays masked and out of the mean."""
152+
s = StaticModelForClassification(vectors=torch.from_numpy(mock_vectors).float(), tokenizer=mock_tokenizer, pad_id=1)
153+
texts = ["word2", "word2 word3"]
154+
155+
dataset = s._prepare_dataset(texts, torch.arange(2), max_length=None)
156+
batch, _ = next(iter(dataset.to_dataloader(shuffle=False, batch_size=2)))
157+
158+
assert torch.equal(batch, s.tokenize(texts))
159+
with torch.no_grad():
160+
assert torch.allclose(s._encode(batch)[0], s._encode(s.tokenize(texts[:1]))[0])
161+
162+
150163
def test_predict(mock_trained_pipeline: StaticModelForClassification) -> None:
151164
"""Test the predict function."""
152165
result = mock_trained_pipeline.predict(["dog cat", "dog"]).tolist()

0 commit comments

Comments
 (0)