-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from naist-nlp/unified-metric
Support multiple scoring methods of UnifiedMetric
- Loading branch information
Showing
7 changed files
with
146 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,72 @@ | ||
import pytest | ||
import torch | ||
|
||
from .xcomet import to_device | ||
from .xcomet import MetricXCOMET | ||
|
||
SOURCE = "これはテストです" | ||
HYPOTHESES = [ | ||
"this is a test", | ||
"another test", | ||
"this is a fest", | ||
"Producția de zahăr primă va fi exprimată în ceea ce privește zahărul alb;", | ||
] | ||
REFERENCES = [ | ||
"ref", | ||
"this is a test", | ||
"producţia de zahăr brut se exprimă în zahăr alb;", | ||
] | ||
SCORES = torch.Tensor( | ||
[ | ||
[0.97671, 1.00000, 0.49054], | ||
[0.94399, 0.99120, 0.43007], | ||
[0.71786, 0.71210, 0.30775], | ||
[0.21788, 0.22079, 0.61004], | ||
] | ||
) | ||
|
||
@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is not available on this machine.") | ||
def test_to_device(): | ||
device = torch.device("cuda:0") | ||
|
||
for x in [1, 1.0, "a", True]: | ||
assert to_device(x, device) == x | ||
@pytest.mark.skipif( | ||
not torch.cuda.is_available(), reason="CUDA is not available on this machine." | ||
) | ||
class TestMetricXCOMET: | ||
def test_score(self, metric_xcomet: MetricXCOMET): | ||
for i, hyp in enumerate(HYPOTHESES): | ||
for j, ref in enumerate(REFERENCES): | ||
assert torch.isclose( | ||
SCORES[i, j], | ||
torch.tensor(metric_xcomet.score(hyp, ref, SOURCE)), | ||
atol=0.0005 / 100, | ||
) | ||
|
||
assert to_device(torch.ones(1), device).device == device | ||
assert to_device({"a": torch.ones(1)}, device)["a"].device == device | ||
assert to_device([torch.ones(1)], device)[0].device == device | ||
assert to_device((torch.ones(1),), device)[0].device == device | ||
def test_scores(self, metric_xcomet: MetricXCOMET): | ||
hyps = ["another test", "this is a test", "this is an test"] | ||
refs = ["another test", "this is a fest", "this is a test"] | ||
src = SOURCE | ||
|
||
torch.testing.assert_close( | ||
metric_xcomet.scores(hyps, refs, src).cpu().float(), | ||
torch.FloatTensor([1.00000, 0.90545, 1.00000]), | ||
atol=0.0005 / 100, | ||
rtol=1e-6, | ||
) | ||
torch.testing.assert_close( | ||
metric_xcomet.scores(hyps, source=src).cpu().float(), | ||
torch.FloatTensor([0.99120, 0.99120, 0.99120]), | ||
atol=0.0005 / 100, | ||
rtol=1e-6, | ||
) | ||
torch.testing.assert_close( | ||
metric_xcomet.scores(hyps, references=refs).cpu().float(), | ||
torch.FloatTensor([1.00000, 0.77420, 1.00000]), | ||
atol=0.0005 / 100, | ||
rtol=1e-6, | ||
) | ||
|
||
def test_expected_scores(self, metric_xcomet: MetricXCOMET): | ||
expected_scores = metric_xcomet.expected_scores(HYPOTHESES, REFERENCES, SOURCE) | ||
torch.testing.assert_close( | ||
expected_scores, | ||
SCORES.mean(dim=1).to(metric_xcomet.device), | ||
atol=0.0005 / 100, | ||
rtol=1e-6, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Any | ||
|
||
import torch | ||
|
||
|
||
def to_device(sample: Any, device: torch.device): | ||
def _to_device(x): | ||
if torch.is_tensor(x): | ||
return x.to(device=device, non_blocking=True) | ||
elif isinstance(x, dict): | ||
return {key: _to_device(value) for key, value in x.items()} | ||
elif isinstance(x, list): | ||
return [_to_device(x) for x in x] | ||
elif isinstance(x, tuple): | ||
return tuple(_to_device(x) for x in x) | ||
elif isinstance(x, set): | ||
return {_to_device(x) for x in x} | ||
else: | ||
return x | ||
|
||
return _to_device(sample) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
import torch | ||
|
||
from . import utils | ||
|
||
|
||
@pytest.mark.skipif( | ||
not torch.cuda.is_available(), reason="CUDA is not available on this machine." | ||
) | ||
def test_to_device(): | ||
device = torch.device("cuda:0") | ||
|
||
for x in [1, 1.0, "a", True]: | ||
assert utils.to_device(x, device) == x | ||
|
||
assert utils.to_device(torch.ones(1), device).device == device | ||
assert utils.to_device({"a": torch.ones(1)}, device)["a"].device == device | ||
assert utils.to_device([torch.ones(1)], device)[0].device == device | ||
assert utils.to_device((torch.ones(1),), device)[0].device == device |