-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathtest_neuron_utils.py
57 lines (51 loc) · 2.11 KB
/
test_neuron_utils.py
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
import os
import pytest
import unittest
from torch_xla._internal.neuron_utils import *
class NeuronUtilsTest(unittest.TestCase):
def test_get_visible_cores_list(self):
os.environ["NEURON_RT_VISIBLE_CORES"] = "1"
assert (get_visible_cores_list() == [1])
os.environ["NEURON_RT_VISIBLE_CORES"] = "1,2,3"
assert (get_visible_cores_list() == [1, 2, 3])
os.environ["NEURON_RT_VISIBLE_CORES"] = "1-3"
assert (get_visible_cores_list() == [1, 2, 3])
os.environ["NEURON_RT_VISIBLE_CORES"] = "1-3,5-8"
assert (get_visible_cores_list() == [1, 2, 3, 5, 6, 7, 8])
os.environ["NEURON_RT_VISIBLE_CORES"] = "1,3,5-8"
assert (get_visible_cores_list() == [1, 3, 5, 6, 7, 8])
os.environ["NEURON_RT_VISIBLE_CORES"] = "1-3,5-8,3-5"
with pytest.raises(ValueError):
get_visible_cores_list()
os.environ["NEURON_RT_VISIBLE_CORES"] = "1-3,5-8-5"
with pytest.raises(ValueError):
get_visible_cores_list()
os.environ["NEURON_RT_VISIBLE_CORES"] = "a-b,5-8-5"
with pytest.raises(Exception):
get_visible_cores_list()
os.environ["NEURON_RT_VISIBLE_CORES"] = "a"
with pytest.raises(Exception):
get_visible_cores_list()
def test_remap_visible_cores(self):
os.environ["NEURON_RT_VISIBLE_CORES"] = "1"
remap_visible_cores(0, 1)
assert (os.environ['NEURON_RT_VISIBLE_CORES'] == "1")
os.environ["NEURON_RT_VISIBLE_CORES"] = "1,2,3"
remap_visible_cores(1, 3)
assert (os.environ['NEURON_RT_VISIBLE_CORES'] == "2")
os.environ["NEURON_RT_VISIBLE_CORES"] = "1-3"
remap_visible_cores(2, 3)
assert (os.environ['NEURON_RT_VISIBLE_CORES'] == "3")
os.environ["NEURON_RT_VISIBLE_CORES"] = "1-3,5-8"
remap_visible_cores(5, 7)
assert (os.environ['NEURON_RT_VISIBLE_CORES'] == "7")
os.environ["NEURON_RT_VISIBLE_CORES"] = "1,3,5-8"
remap_visible_cores(5, 6)
assert (os.environ['NEURON_RT_VISIBLE_CORES'] == "8")
with pytest.raises(ValueError):
remap_visible_cores(5, 9)
with pytest.raises(ValueError):
remap_visible_cores(6, 6)
if __name__ == "__main__":
test = unittest.main()
sys.exit(0 if test.result.wasSuccessful() else 1)