-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsorting_nets_test.py
89 lines (73 loc) · 3.05 KB
/
sorting_nets_test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2022 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests sorting nets."""
from absl.testing import absltest
from absl.testing import parameterized
import chex
import jax
import jax.numpy as jnp
import sorting_nets
import variational_sorting_net
class SortingNetsTest(parameterized.TestCase):
@parameterized.parameters([
[4],
])
def test_create_comms(self, length):
comms = sorting_nets.comm_pattern_bitonic(2)
chex.assert_equal(comms["alg"], "bitonic")
comms = sorting_nets.comm_pattern_batcher(length, make_parallel=True)
chex.assert_equal(comms["alg"], "batcher-bitonic")
comms = sorting_nets.comm_pattern_batcher(length, make_parallel=False)
chex.assert_equal(comms["alg"], "batcher-bitonic")
@parameterized.parameters([
[[[[0, 1]], [[1, 2]], [[0, 2]]], 3]
])
def test_comm_pattern_from_list(self, snet_list, num_stages):
comms = sorting_nets.comm_pattern_from_list(snet_list)
chex.assert_equal(comms["alg"], "fixed")
chex.assert_equal(comms["num_stages"], num_stages)
@parameterized.parameters([
[[[[0, 1]], [[2, 3]], [[0, 2]]], 2]
])
def test_parallelize(self, snet_list, final_len):
snet_par = sorting_nets.parallelize(snet_list)
chex.assert_equal(len(snet_par), final_len)
comms = sorting_nets.comm_pattern_from_list(snet_par)
chex.assert_equal(comms["alg"], "fixed")
chex.assert_equal(comms["num_wires"], 4)
chex.assert_equal(comms["num_stages"], 2)
chex.assert_equal(comms["num_comparators"], 3)
def test_prune(self):
snet_list = sorting_nets.SNET_10
snet_pruned = sorting_nets.prune(snet_list, keep=[9])
comms = sorting_nets.comm_pattern_from_list(snet_pruned, make_parallel=True)
chex.assert_equal(comms["alg"], "fixed")
chex.assert_equal(comms["num_wires"], 10)
chex.assert_equal(comms["num_stages"], 4)
chex.assert_equal(comms["num_comparators"], 9)
k_top = 2
length = comms["num_wires"]
keep = list(range(length-1, length -1 - k_top - 1, -1))
pruned_list = sorting_nets.prune(snet_list, keep=keep)
comms = sorting_nets.comm_pattern_from_list(pruned_list, make_parallel=True)
bs = variational_sorting_net.VariationalSortingNet(comms)
prng_key = jax.random.PRNGKey(1)
x = jax.random.uniform(prng_key, [length])
xh, _ = bs.sort_tester(x, dispersion=0.1)
x_sort = jnp.sort(x)
chex.assert_equal(xh[-1], x_sort[-1])
chex.assert_equal(xh[-2], x_sort[-2])
if __name__ == "__main__":
absltest.main()