Skip to content

Commit 3bba44d

Browse files
authored
[WIP ] Proposal to address precision issues in CI (#4775)
* proposal for flaky tests * clean up
1 parent b1290d3 commit 3bba44d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/diffusers/utils/testing_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import PIL.Image
2020
import PIL.ImageOps
2121
import requests
22+
from numpy.linalg import norm
2223
from packaging import version
2324

2425
from .import_utils import (
@@ -72,6 +73,13 @@ def torch_all_close(a, b, *args, **kwargs):
7273
return True
7374

7475

76+
def numpy_cosine_similarity_distance(a, b):
77+
similarity = np.dot(a, b) / (norm(a) * norm(b))
78+
distance = 1.0 - similarity.mean()
79+
80+
return distance
81+
82+
7583
def print_tensor_test(tensor, filename="test_corrections.txt", expected_tensor_name="expected_slice"):
7684
test_name = os.environ.get("PYTEST_CURRENT_TEST")
7785
if not torch.is_tensor(tensor):

tests/pipelines/test_pipelines_common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
from diffusers.schedulers import KarrasDiffusionSchedulers
2323
from diffusers.utils import logging
2424
from diffusers.utils.import_utils import is_accelerate_available, is_accelerate_version, is_xformers_available
25-
from diffusers.utils.testing_utils import CaptureLogger, require_torch, torch_device
25+
from diffusers.utils.testing_utils import (
26+
CaptureLogger,
27+
numpy_cosine_similarity_distance,
28+
require_torch,
29+
torch_device,
30+
)
2631

2732
from ..others.test_utils import TOKEN, USER, is_staging_test
2833

@@ -543,7 +548,7 @@ def test_float16_inference(self, expected_max_diff=1e-2):
543548
output = pipe(**self.get_dummy_inputs(torch_device))[0]
544549
output_fp16 = pipe_fp16(**self.get_dummy_inputs(torch_device))[0]
545550

546-
max_diff = np.abs(to_np(output) - to_np(output_fp16)).max()
551+
max_diff = numpy_cosine_similarity_distance(to_np(output).flatten(), to_np(output_fp16).flatten())
547552
self.assertLess(max_diff, expected_max_diff, "The outputs of the fp16 and fp32 pipelines are too different.")
548553

549554
@unittest.skipIf(torch_device != "cuda", reason="float16 requires CUDA")

0 commit comments

Comments
 (0)