MCPcopy
hub / github.com/huggingface/transformers / test_determinism

Method test_determinism

tests/test_modeling_common.py:1464–1494  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1462 check_equal(load_state_dict(pt_checkpoint_path))
1463
1464 def test_determinism(self):
1465 config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
1466
1467 def check_determinism(first, second):
1468 # Simply don't compare if both tensors only contain `nan` elements
1469 # See: https://github.com/huggingface/transformers/pull/40661
1470 if torch.all(torch.isnan(first)) and torch.all(torch.isnan(second)):
1471 return
1472
1473 out_1 = first.cpu().numpy()
1474 out_2 = second.cpu().numpy()
1475 out_1 = out_1[~np.isnan(out_1)]
1476 out_2 = out_2[~np.isnan(out_2)]
1477 out_1 = out_1[~np.isneginf(out_1)]
1478 out_2 = out_2[~np.isneginf(out_2)]
1479 max_diff = np.amax(np.abs(out_1 - out_2))
1480 self.assertLessEqual(max_diff, 1e-5)
1481
1482 for model_class in self.all_model_classes:
1483 model = model_class(copy.deepcopy(config))
1484 model.to(torch_device)
1485 model.eval()
1486 with torch.no_grad():
1487 first = model(**self._prepare_for_class(inputs_dict, model_class))[0]
1488 second = model(**self._prepare_for_class(inputs_dict, model_class))[0]
1489
1490 if isinstance(first, tuple) and isinstance(second, tuple):
1491 for tensor1, tensor2 in zip(first, second):
1492 check_determinism(tensor1, tensor2)
1493 else:
1494 check_determinism(first, second)
1495
1496 def test_batching_equivalence(self, atol=1e-5, rtol=1e-5):
1497 """

Callers

nothing calls this directly

Calls 4

_prepare_for_classMethod · 0.95
evalMethod · 0.80
toMethod · 0.45

Tested by

no test coverage detected