File size: 2,512 Bytes
addfd4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Regression cases for the task policies used by both validation runners."""

import sys
import unittest
from pathlib import Path

import torch

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
from task_acceptance import compare_output


class TaskAcceptanceTests(unittest.TestCase):
    def test_contest_or_is_not_additive_tolerance(self):
        expected = torch.tensor([100.0])
        actual = torch.tensor([115.0])
        additive = dict(atol=10.0, rtol=0.1)
        compare_output(actual, expected, additive)
        with self.assertRaises(AssertionError):
            compare_output(
                actual, expected, dict(additive, comparison="absolute_or_relative")
            )

    def test_strict_gdn_diagnostic_is_distinct_from_contest(self):
        expected, actual = torch.tensor([0.05]), torch.tensor([0.05151])
        with self.assertRaises(AssertionError):
            compare_output(actual, expected, dict(atol=2e-5, rtol=1e-4))
        compare_output(
            actual,
            expected,
            dict(atol=0.01, rtol=0.01, comparison="absolute_or_relative"),
        )

    def test_matched_ratio_boundary_uses_integer_counts(self):
        expected = torch.zeros(10000)
        actual = expected.clone()
        actual[0] = 1.0
        metrics = compare_output(
            actual, expected, dict(atol=0.0, rtol=0.0, required_matched_ratio=0.9999)
        )
        self.assertEqual(metrics["mismatched_elements"], 1)
        actual[1] = 1.0
        with self.assertRaises(AssertionError):
            compare_output(
                actual,
                expected,
                dict(atol=0.0, rtol=0.0, required_matched_ratio=0.9999),
            )
        metrics = compare_output(expected, expected, dict(atol=0.0, rtol=0.0))
        self.assertEqual(metrics["matched_ratio"], 1.0)

    def test_nonfinite_values_and_integer_mismatches_fail(self):
        for value in [float("nan"), float("inf")]:
            with self.assertRaises(AssertionError):
                compare_output(
                    torch.tensor([value]),
                    torch.tensor([value]),
                    dict(atol=1.0, rtol=1.0),
                )
        with self.assertRaises(AssertionError):
            compare_output(
                torch.tensor([1]), torch.tensor([2]), dict(atol=100.0, rtol=100.0)
            )
        compare_output(torch.tensor([1]), torch.tensor([1]), dict(atol=0.0, rtol=0.0))


if __name__ == "__main__":
    unittest.main()