forked from codeaudit/polyai-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_client_test.py
269 lines (220 loc) · 10.3 KB
/
encoder_client_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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Unit tests for encoder_client.py.
Copyright PolyAI Limited.
"""
import unittest
from unittest import mock
from unittest.mock import patch
import numpy as np
import tensorflow as tf
import encoder_client
def _random_encoding(examples):
"""A random encoding function used for tests."""
return np.random.normal(size=(len(examples), 7))
class CacheEncodingsTest(unittest.TestCase):
def test_cache_encodings(self):
"""Test that values are cached."""
cached_random_encoding = encoder_client.cache_encodings(
_random_encoding, cache_size=100)
encodings_1 = cached_random_encoding(
["hello", "hello world"]
)
self.assertEqual([2, 7], list(encodings_1.shape))
encodings_2 = cached_random_encoding(
["hello world", "new input"]
)
self.assertEqual([2, 7], list(encodings_2.shape))
# The encoding for "hello world" should be cached, and return the
# same value, even though it is generated randomly.
np.testing.assert_allclose(encodings_1[1], encodings_2[0])
self.assertEqual(1, cached_random_encoding.cache_hits())
def test_cache_duplicate_inputs(self):
"""Test inputs are deduplicated for the encoding function."""
random_encoding = mock.Mock(side_effect=_random_encoding)
cached_random_encoding = encoder_client.cache_encodings(
random_encoding, cache_size=100)
encodings = cached_random_encoding(["hello"] * 10)
self.assertEqual([10, 7], list(encodings.shape))
for i in range(1, 10):
np.testing.assert_allclose(encodings[0], encodings[i])
random_encoding.assert_called_once_with(["hello"])
def test_least_recently_used_forgotten(self):
"""Test the least recently used input is forgotten."""
cached_random_encoding = encoder_client.cache_encodings(
_random_encoding, cache_size=10)
encoding = cached_random_encoding(["to be forgotten"])
cached_random_encoding(list(range(10)))
encoding_1 = cached_random_encoding(["to be forgotten"])
# Check the two encodings are different, as the old one should have
# been forgotten.
np.testing.assert_raises(
AssertionError, np.testing.assert_allclose, encoding,
encoding_1)
self.assertEqual(0, cached_random_encoding.cache_hits())
def test_nested_lists(self):
"""Test that inputs with nested lists are cached correctly."""
cached_random_encoding = encoder_client.cache_encodings(
_random_encoding, cache_size=100)
example_1 = ["hello", ["context 1", "context 2"]]
example_2 = ["hi", ["context 1", "context 2", "context 3"]]
encodings_1 = cached_random_encoding([example_1, example_2])
encodings_2 = cached_random_encoding([example_2, example_1])
np.testing.assert_allclose(encodings_1[0], encodings_2[1])
np.testing.assert_allclose(encodings_1[1], encodings_2[0])
self.assertEqual(2, cached_random_encoding.cache_hits())
class EncoderClientTest(unittest.TestCase):
"""Test EncoderClient with a non-contextual encoder."""
@patch("tensorflow_hub.Module")
def test_encode_context(self, mock_module_cls):
def mock_fn(input, signature=None):
self.assertIn(
signature, {"encode_context", "encode_response", None})
self.assertIsInstance(input, tf.Tensor)
self.assertEqual(input.dtype, tf.string)
if signature == "encode_context":
return tf.ones([tf.shape(input)[0], 3])
mock_module_cls.return_value = mock_fn
client = encoder_client.EncoderClient("test_uri")
mock_module_cls.assert_called_with("test_uri")
encodings = client.encode_contexts(["hello"])
np.testing.assert_allclose([[1, 1, 1]], encodings)
@patch("tensorflow_hub.Module")
def test_encode_response(self, mock_module_cls):
def mock_fn(input, signature=None):
self.assertIn(
signature, {"encode_context", "encode_response", None})
self.assertIsInstance(input, tf.Tensor)
self.assertEqual(input.dtype, tf.string)
if signature == "encode_response":
return tf.ones([tf.shape(input)[0], 3])
mock_module_cls.return_value = mock_fn
client = encoder_client.EncoderClient("test_uri")
mock_module_cls.assert_called_with("test_uri")
encodings = client.encode_responses(["hello"])
np.testing.assert_allclose([[1, 1, 1]], encodings)
@patch("tensorflow_hub.Module")
def test_encode_sentences(self, mock_module_cls):
def mock_fn(input, signature=None):
self.assertIn(
signature, {"encode_context", "encode_response", None})
self.assertIsInstance(input, tf.Tensor)
self.assertEqual(input.dtype, tf.string)
if signature is None:
return tf.ones([tf.shape(input)[0], 3])
mock_module_cls.return_value = mock_fn
client = encoder_client.EncoderClient("test_uri")
mock_module_cls.assert_called_with("test_uri")
encodings = client.encode_sentences(["hello"])
np.testing.assert_allclose([[1, 1, 1]], encodings)
@patch("tensorflow_hub.Module")
def test_encode_sentences_batching_caching(self, mock_module_cls):
def mock_fn(input, signature=None):
self.assertIn(
signature, {"encode_context", "encode_response", None})
self.assertIsInstance(input, tf.Tensor)
self.assertEqual(input.dtype, tf.string)
if signature is None:
return tf.random_normal([tf.shape(input)[0], 3])
mock_module_cls.return_value = mock_fn
client = encoder_client.EncoderClient(
# force batching by setting batch size to 3
"test_uri", internal_batch_size=3, cache_size=100,
)
mock_module_cls.assert_called_with("test_uri")
encodings = client.encode_sentences(
["a", "a", "b", "c", "d", "e", "f", "g"]
)
# Test de-duplication:
np.testing.assert_allclose(encodings[0], encodings[1])
encodings_2 = client.encode_sentences(["a", "b", "c", "z"])
# Test caching
np.testing.assert_allclose(encodings[0], encodings_2[0])
np.testing.assert_allclose(encodings[2], encodings_2[1])
np.testing.assert_allclose(encodings[3], encodings_2[2])
class EncoderClientExtraContextsTest(unittest.TestCase):
"""Test EncoderClient with a contextual encoder."""
@patch("tensorflow_hub.Module")
def test_encode_context(self, mock_module_cls):
def mock_fn(input, signature=None):
self.assertIn(
signature, {"encode_context", "encode_response", None})
if signature == "encode_context":
self.assertIsInstance(input, dict)
self.assertEqual(2, len(input))
for input_t in input.values():
self.assertEqual(input_t.dtype, tf.string)
return tf.ones([tf.shape(input_t)[0], 3])
mock_module_cls.return_value = mock_fn
client = encoder_client.EncoderClient(
"test_uri", use_extra_context=True)
mock_module_cls.assert_called_with("test_uri")
encodings = client.encode_contexts(
["hello", "hi", "yo"],
extra_contexts=[
["a", "b", "c", "d"],
["A", "B", "C", "D", "E", "F"],
[],
],
)
np.testing.assert_allclose([[1., 1., 1.]] * 3, encodings)
@patch("tensorflow_hub.Module")
def test_encode_context_feature_values(self, mock_module_cls):
def mock_fn(input, signature=None):
self.assertIn(
signature, {"encode_context", "encode_response", None})
if signature == "encode_context":
self.assertIsInstance(input, dict)
self.assertEqual(2, len(input))
for input_t in input.values():
self.assertEqual(input_t.dtype, tf.string)
mock_module_cls.return_value = mock_fn
with mock.patch("encoder_client._batch_session_run") as f:
client = encoder_client.EncoderClient(
"test_uri", use_extra_context=True, max_extra_contexts=4,
cache_size=0)
mock_module_cls.assert_called_with("test_uri")
encodings = client.encode_contexts(
["hello", "hi", "yo"],
extra_contexts=[
["a", "b", "c", "d"],
["A", "B", "C", "D", "E", "F"],
[],
],
)
f.assert_called_once()
self.assertEqual(
["d c b a", "F E D C", ""],
list(f.call_args[0][1][client._fed_extra_contexts]),
)
self.assertEqual(f.return_value, encodings)
@patch("tensorflow_hub.Module")
def test_encode_context_feature_values_with_prefix(self, mock_module_cls):
def mock_fn(input, signature=None):
self.assertIn(
signature, {"encode_context", "encode_response", None})
if signature == "encode_context":
self.assertIsInstance(input, dict)
self.assertEqual(2, len(input))
for input_t in input.values():
self.assertEqual(input_t.dtype, tf.string)
mock_module_cls.return_value = mock_fn
with mock.patch("encoder_client._batch_session_run") as f:
client = encoder_client.EncoderClient(
"test_uri", use_extra_context=True, max_extra_contexts=3,
use_extra_context_prefixes=True, cache_size=0,)
mock_module_cls.assert_called_with("test_uri")
encodings = client.encode_contexts(
["hello", "hi", "yo"],
extra_contexts=[
["a", "b", "c", "d d"],
["A", "B", "C", "D", "E", "F"],
[],
],
)
f.assert_called_once()
self.assertEqual(
["0: d d 1: c 2: b", "0: F 1: E 2: D", ""],
list(f.call_args[0][1][client._fed_extra_contexts]),
)
self.assertEqual(f.return_value, encodings)
if __name__ == "__main__":
unittest.main()