@@ -149,6 +149,51 @@ def test_predict(self):
149149 self .assertTrue (np .array_equal (labels [0 ], trainer .eval_dataset .ys [0 ]))
150150 self .assertTrue (np .array_equal (labels [1 ], trainer .eval_dataset .ys [1 ]))
151151
152+ def test_predict_preprocessed_float8_logits (self ):
153+ dtypes = (
154+ torch .float8_e4m3fn ,
155+ torch .float8_e4m3fnuz ,
156+ torch .float8_e5m2 ,
157+ torch .float8_e5m2fnuz ,
158+ torch .bfloat16 ,
159+ torch .float32 ,
160+ torch .float64 ,
161+ )
162+ for dtype in dtypes :
163+ for accumulation_steps in (None , 1 ):
164+ with self .subTest (dtype = dtype , accumulation_steps = accumulation_steps ):
165+ with tempfile .TemporaryDirectory () as tmp_dir :
166+ trainer = get_regression_trainer (
167+ a = 1.5 ,
168+ b = 2.5 ,
169+ double_output = True ,
170+ eval_len = 7 ,
171+ per_device_eval_batch_size = 3 ,
172+ eval_accumulation_steps = accumulation_steps ,
173+ preprocess_logits_for_metrics = lambda logits , labels : tuple (t .to (dtype ) for t in logits ),
174+ compute_metrics = lambda output : {"prediction_mean" : output .predictions [0 ].mean ().item ()},
175+ output_dir = tmp_dir ,
176+ report_to = [],
177+ )
178+ raw = torch .from_numpy (1.5 * trainer .eval_dataset .x + 2.5 )
179+ expected = raw .to (dtype )
180+ if dtype != torch .float64 :
181+ expected = expected .float ()
182+ expected = expected .numpy ()
183+
184+ output = trainer .predict (trainer .eval_dataset )
185+ self .assertIsInstance (output .predictions , tuple )
186+ self .assertEqual (len (output .predictions ), 2 )
187+ for predictions in output .predictions :
188+ self .assertEqual (predictions .dtype , expected .dtype )
189+ np .testing .assert_array_equal (predictions , expected )
190+ np .testing .assert_array_equal (output .label_ids , trainer .eval_dataset .ys [0 ])
191+ self .assertEqual (output .metrics ["test_prediction_mean" ], expected .mean ().item ())
192+
193+ metrics = trainer .evaluate ()
194+ self .assertEqual (metrics ["eval_prediction_mean" ], expected .mean ().item ())
195+ self .assertAlmostEqual (metrics ["eval_loss" ], output .metrics ["test_loss" ], places = 6 )
196+
152197 def test_train_and_predict_loss_parity (self ):
153198 """
154199 Tests that the loss computed during a training_step is the same as the one computed during prediction_step.
0 commit comments