From 38e8adffbe9240c5d5b24d1a5f34e86f6046fec1 Mon Sep 17 00:00:00 2001 From: Philipp Wirth Date: Fri, 27 Dec 2024 15:04:38 +0100 Subject: [PATCH] Typecheck negative cosine similarity --- pyproject.toml | 2 -- ...rity.py => test_negative_cosine_similarity.py} | 15 +++++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) rename tests/loss/{test_NegativeCosineSimilarity.py => test_negative_cosine_similarity.py} (61%) diff --git a/pyproject.toml b/pyproject.toml index f1bc629d0..2b46ffa24 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -192,7 +192,6 @@ exclude = '''(?x)( lightly/cli/train_cli.py | lightly/cli/_cli_simclr.py | lightly/cli/_helpers.py | - lightly/loss/negative_cosine_similarity.py | lightly/loss/hypersphere_loss.py | lightly/loss/dino_loss.py | lightly/loss/sym_neg_cos_sim_loss.py | @@ -244,7 +243,6 @@ exclude = '''(?x)( tests/UNMOCKED_end2end_tests/delete_datasets_test_unmocked_cli.py | tests/UNMOCKED_end2end_tests/create_custom_metadata_from_input_dir.py | tests/UNMOCKED_end2end_tests/scripts_for_reproducing_problems/test_api_latency.py | - tests/loss/test_NegativeCosineSimilarity.py | tests/loss/test_DINOLoss.py | tests/loss/test_VICRegLLoss.py | tests/loss/test_CO2Regularizer.py | diff --git a/tests/loss/test_NegativeCosineSimilarity.py b/tests/loss/test_negative_cosine_similarity.py similarity index 61% rename from tests/loss/test_NegativeCosineSimilarity.py rename to tests/loss/test_negative_cosine_similarity.py index 44d29522a..2f8c58a23 100644 --- a/tests/loss/test_NegativeCosineSimilarity.py +++ b/tests/loss/test_negative_cosine_similarity.py @@ -1,12 +1,11 @@ -import unittest - +import pytest import torch from lightly.loss import NegativeCosineSimilarity -class TestNegativeCosineSimilarity(unittest.TestCase): - def test_forward_pass(self): +class TestNegativeCosineSimilarity: + def test_forward_pass(self) -> None: loss = NegativeCosineSimilarity() for bsz in range(1, 20): x0 = torch.randn((bsz, 32)) @@ -15,10 +14,10 @@ def test_forward_pass(self): # symmetry l1 = loss(x0, x1) l2 = loss(x1, x0) - self.assertAlmostEqual((l1 - l2).pow(2).item(), 0.0) + assert l1 == pytest.approx(l2, abs=1e-5) - @unittest.skipUnless(torch.cuda.is_available(), "Cuda not available") - def test_forward_pass_cuda(self): + @pytest.mark.skipif(not torch.cuda.is_available(), reason="No cuda") + def test_forward_pass_cuda(self) -> None: loss = NegativeCosineSimilarity() for bsz in range(1, 20): x0 = torch.randn((bsz, 32)).cuda() @@ -27,4 +26,4 @@ def test_forward_pass_cuda(self): # symmetry l1 = loss(x0, x1) l2 = loss(x1, x0) - self.assertAlmostEqual((l1 - l2).pow(2).item(), 0.0) + assert l1 == pytest.approx(l2, abs=1e-5)