-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtpu_replicated_variable_test.py
84 lines (70 loc) · 3.32 KB
/
tpu_replicated_variable_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
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# 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 for TPUReplicatedVariable."""
from __future__ import division
from __future__ import print_function
from absl.testing import parameterized
import numpy as np
from tensorflow.python.distribute import tpu_replicated_variable
from tensorflow.python.eager import test
from tensorflow.python.framework import combinations
from tensorflow.python.framework import dtypes
from tensorflow.python.ops import variables as variables_lib
class TPUReplicatedVariableTest(test.TestCase, parameterized.TestCase):
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
def test_tpu_replicated_variable_simple(self):
v0 = variables_lib.Variable([0], name='v0')
v1 = variables_lib.Variable([0], name='v1')
r = tpu_replicated_variable.TPUReplicatedVariable([v0, v1])
self.evaluate(variables_lib.global_variables_initializer())
self.assertEqual(r.variables[0], v0)
self.assertEqual(r.variables[1], v1)
self.assertEqual(r.shape.as_list(), [1])
self.assertEqual(r.dtype, v0.dtype)
self.check_replicated_variables_all_the_same(r)
@combinations.generate(combinations.combine(mode=['graph', 'eager']))
def test_tpu_replicated_variable_update(self):
batch_size = 32
num_feature_in = 16
x = np.random.rand(batch_size, num_feature_in).astype(np.float32)
w_init = np.random.rand(batch_size, num_feature_in).astype(np.float32)
w0 = variables_lib.Variable(w_init, dtype=dtypes.float32, name='w0')
w1 = variables_lib.Variable(w_init, dtype=dtypes.float32, name='w1')
self.evaluate(variables_lib.global_variables_initializer())
w = tpu_replicated_variable.TPUReplicatedVariable([w0, w1])
# Make a copy of x so that `w` and `x` do not share the same buffer.
# See b/195972684.
self.evaluate(w.assign(x.copy()))
result = self.evaluate(w.read_value())
self.assertAllClose(result, x)
self.check_replicated_variables_all_the_same(w)
x1 = np.random.rand(batch_size, num_feature_in).astype(np.float32)
self.evaluate(w.assign_sub(x1))
result = self.evaluate(w.read_value())
self.assertAllClose(result, np.subtract(x, x1))
self.check_replicated_variables_all_the_same(w)
x2 = np.random.rand(batch_size, num_feature_in).astype(np.float32)
self.evaluate(w.assign(x.copy()))
self.evaluate(w.assign_add(x2))
result = self.evaluate(w.read_value())
self.assertAllClose(result, np.add(x, x2))
self.check_replicated_variables_all_the_same(w)
def check_replicated_variables_all_the_same(self, rv):
for v in rv.variables:
self.assertAllEqual(
self.evaluate(rv.variables[0].read_value()),
self.evaluate(v))
if __name__ == '__main__':
test.main()