Repository: huggingface/transformers
Pull request: Update `check_determinism` inside `test_determinism`

# What does this PR do?

Flaky at least for

> FAILED tests/models/evolla/test_modeling_evolla.py::EvollaModelTest::test_determinism - ValueError: zero-size array to reduction operation maximum which has no identity

where tensors on both sides are all `nan`.

Let's simply not compare in this case.

### Failed job run page

https://app.circleci.com/pipelines/github/huggingface/transformers/144111/workflows/032ff3b8-2782-4519-b836-49b52975a197/jobs/1904923

### Full log
```
_______________________ EvollaModelTest.test_determinism _______________________
[gw1] linux -- Python 3.9.23 /usr/local/bin/python3

self = <tests.models.evolla.test_modeling_evolla.EvollaModelTest testMethod=test_determinism>

    def test_determinism(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
    
        def check_determinism(first, second):
            out_1 = first.cpu().numpy()
            out_2 = second.cpu().numpy()
            out_1 = out_1[~np.isnan(out_1)]
            out_2 = out_2[~np.isnan(out_2)]
            out_1 = out_1[~np.isneginf(out_1)]
            out_2 = out_2[~np.isneginf(out_2)]
            max_diff = np.amax(np.abs(out_1 - out_2))
            self.assertLessEqual(max_diff, 1e-5)
    
        for model_class in self.all_model_classes:
            model = model_class(copy.deepcopy(config))
            model.to(torch_device)
            model.eval()
            with torch.no_grad():
                first = model(**self._prepare_for_class(inputs_dict, model_class))[0]
                second = model(**self._prepare_for_class(inputs_dict, model_class))[0]
    
            if isinstance(first, tuple) and isinstance(second, tuple):
                for tensor1, tensor2 in zip(first, second):
                    check_determinism(tensor1, tensor2)
            else:
>               check_determinism(first, second)

tests/test_modeling_common.py:1015: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/test_modeling_common.py:1000: in check_determinism
    max_diff = np.amax(np.abs(out_1 - out_2))
/usr/local/lib/python3.9/site-packages/numpy/core/fromnumeric.py:2827: in amax
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

obj = array([], dtype=float32), ufunc = <ufunc 'maximum'>, method = 'max'
axis = None, dtype = None, out = None
kwargs = {'initial': <no value>, 'keepdims': <no value>, 'where': <no value>}
passkwargs = {}

    def _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs):
        passkwargs = {k: v for k, v in kwargs.items()
                      if v is not np._NoValue}
    
        if type(obj) is not mu.ndarray:
            try:
                reduction = getattr(obj, method)
            except AttributeError:
                pass
            else:
                # This branch is needed for reductions like any which don't
                # support a dtype.
                if dtype is not None:
                    return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
                else:
                    return reduction(axis=axis, out=out, **passkwargs)
    
>       return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
E       ValueError: zero-size array to reduction operation maximum which has no identity

/usr/local/lib/python3.9/site-packages/numpy/core/fromnumeric.py:88: ValueError
```


Changed files:
filename: tests/test_modeling_common.py
patch: @@ -1001,6 +1001,11 @@ def test_determinism(self):
         config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
 
         def check_determinism(first, second):
+            # Simply don't compare if both tensors only contain `nan` elements
+            # See: https://github.com/huggingface/transformers/pull/40661
+            if torch.all(torch.isnan(first)) and torch.all(torch.isnan(second)):
+                return
+
             out_1 = first.cpu().numpy()
             out_2 = second.cpu().numpy()
             out_1 = out_1[~np.isnan(out_1)]