File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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+
150163def test_predict (mock_trained_pipeline : StaticModelForClassification ) -> None :
151164 """Test the predict function."""
152165 result = mock_trained_pipeline .predict (["dog cat" , "dog" ]).tolist ()
You can’t perform that action at this time.
0 commit comments