forked from dmlc/gluon-nlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sampler.py
139 lines (124 loc) · 6.13 KB
/
test_sampler.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import warnings
import numpy as np
import pytest
from mxnet.gluon import data
import gluonnlp as nlp
from gluonnlp.data import sampler as s
N = 1000
def test_sorted_sampler():
dataset = data.SimpleDataset([np.random.normal(0, 1, (np.random.randint(10, 100), 1, 1))
for _ in range(N)])
gt_sample_id = sorted(range(len(dataset)), key=lambda i: dataset[i].shape, reverse=True)
sample_ret = list(s.SortedSampler([ele.shape[0] for ele in dataset]))
for lhs, rhs in zip(gt_sample_id, sample_ret):
assert lhs == rhs
@pytest.mark.parametrize('seq_lengths', [[np.random.randint(10, 100) for _ in range(N)],
[(np.random.randint(10, 100), np.random.randint(10, 100))
for _ in range(N)]])
@pytest.mark.parametrize('ratio', [0.0, 0.5])
@pytest.mark.parametrize('shuffle', [False, True])
@pytest.mark.parametrize('num_buckets', [1, 10, 100, 5000])
@pytest.mark.parametrize('bucket_scheme', [s.ConstWidthBucket(),
s.LinearWidthBucket(),
s.ExpWidthBucket()])
@pytest.mark.parametrize('use_average_length', [False, True])
@pytest.mark.parametrize('num_shards', range(4))
def test_fixed_bucket_sampler(seq_lengths, ratio, shuffle, num_buckets, bucket_scheme,
use_average_length, num_shards):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
sampler = s.FixedBucketSampler(seq_lengths, batch_size=8, num_buckets=num_buckets,
ratio=ratio, shuffle=shuffle,
use_average_length=use_average_length,
bucket_scheme=bucket_scheme, num_shards=num_shards)
print(sampler.stats())
total_sampled_ids = []
for batch_sample_ids in sampler:
if num_shards > 0:
assert len(batch_sample_ids) == num_shards
else:
total_sampled_ids.extend(batch_sample_ids)
if num_shards == 0:
assert len(set(total_sampled_ids)) == len(total_sampled_ids) == N
@pytest.mark.parametrize('bucket_keys', [[1, 5, 10, 100], [10, 100], [200]])
@pytest.mark.parametrize('ratio', [0.0, 0.5])
@pytest.mark.parametrize('shuffle', [False, True])
def test_fixed_bucket_sampler_with_single_key(bucket_keys, ratio, shuffle):
seq_lengths = [np.random.randint(10, 100) for _ in range(N)]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
sampler = s.FixedBucketSampler(seq_lengths, batch_size=8, num_buckets=None,
bucket_keys=bucket_keys, ratio=ratio, shuffle=shuffle)
print(sampler.stats())
total_sampled_ids = []
for batch_sample_ids in sampler:
total_sampled_ids.extend(batch_sample_ids)
assert len(set(total_sampled_ids)) == len(total_sampled_ids) == N
@pytest.mark.parametrize('bucket_keys', [[(1, 1), (5, 10), (10, 20), (20, 10), (100, 100)],
[(20, 20), (30, 15), (100, 100)],
[(100, 200)]])
@pytest.mark.parametrize('ratio', [0.0, 0.5])
@pytest.mark.parametrize('shuffle', [False, True])
def test_fixed_bucket_sampler_with_single_key(bucket_keys, ratio, shuffle):
seq_lengths = [(np.random.randint(10, 100), np.random.randint(10, 100)) for _ in range(N)]
with warnings.catch_warnings():
warnings.simplefilter("ignore")
sampler = s.FixedBucketSampler(seq_lengths, batch_size=8, num_buckets=None,
bucket_keys=bucket_keys, ratio=ratio, shuffle=shuffle)
print(sampler.stats())
total_sampled_ids = []
for batch_sample_ids in sampler:
total_sampled_ids.extend(batch_sample_ids)
assert len(set(total_sampled_ids)) == len(total_sampled_ids) == N
def test_fixed_bucket_sampler_compactness():
samples = list(
s.FixedBucketSampler(
np.arange(16, 32), 8, num_buckets=2,
bucket_scheme=nlp.data.ConstWidthBucket()))
assert len(samples) == 2
@pytest.mark.parametrize('seq_lengths', [[np.random.randint(10, 100) for _ in range(N)],
[(np.random.randint(10, 100), np.random.randint(10, 100))
for _ in range(N)]])
@pytest.mark.parametrize('mult', [10, 100])
@pytest.mark.parametrize('batch_size', [5, 7])
@pytest.mark.parametrize('shuffle', [False, True])
def test_sorted_bucket_sampler(seq_lengths, mult, batch_size, shuffle):
sampler = s.SortedBucketSampler(sort_keys=seq_lengths,
batch_size=batch_size,
mult=mult, shuffle=shuffle)
total_sampled_ids = []
for batch_sample_ids in sampler:
total_sampled_ids.extend(batch_sample_ids)
assert len(set(total_sampled_ids)) == len(total_sampled_ids) == N
@pytest.mark.parametrize('num_samples', [30])
@pytest.mark.parametrize('num_parts', [3, 7])
@pytest.mark.parametrize('repeat', [1, 3])
def test_split_sampler(num_samples, num_parts, repeat):
total_count = 0
indices = []
for part_idx in range(num_parts):
sampler = s.SplitSampler(num_samples, num_parts, part_idx, repeat=repeat)
count = 0
for i in sampler:
count += 1
indices.append(i)
total_count += count
assert count == len(sampler)
assert total_count == num_samples * repeat
assert np.allclose(sorted(indices), np.repeat(list(range(num_samples)), repeat))
@pytest.mark.parametrize('num_samples', [30])
@pytest.mark.parametrize('num_parts', [3, 7])
def test_split_sampler_even_size(num_samples, num_parts):
total_count = 0
indices = []
for part_idx in range(num_parts):
sampler = s.SplitSampler(num_samples, num_parts, part_idx, even_size=True)
count = 0
for i in sampler:
count += 1
indices.append(i)
total_count += count
assert count == len(sampler)
print(count)
expected_count = int(num_samples + num_parts - 1) // num_parts * num_parts
assert total_count == expected_count, (total_count, expected_count)