Skip to content

Commit 4214d79

Browse files
allenlavoietensorflower-gardener
authored andcommitted
ParallelDevice: Integration test with tf.random.Generator
Fixes an error due to tf.zeros trying to jump back and forth to NumPy (the only integration issue I ran into when I was trying this). PiperOrigin-RevId: 370752803 Change-Id: I00bc12167b3cb333ad05c83a0f2cb53ba41fcf85
1 parent 3e47a82 commit 4214d79

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎tensorflow/python/distribute/parallel_device/parallel_device_test.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from tensorflow.python.ops import control_flow_ops
3636
from tensorflow.python.ops import gen_resource_variable_ops
3737
from tensorflow.python.ops import math_ops
38+
from tensorflow.python.ops import stateful_random_ops
3839
from tensorflow.python.ops import variables
3940
from tensorflow.python.platform import test
4041
from tensorflow.python.saved_model import load
@@ -158,6 +159,26 @@ def test_device_id(self):
158159
self.assertIn(self.device.components[0], device_ids[0].device)
159160
self.assertIn(self.device.components[1], device_ids[1].device)
160161

162+
def test_zeros(self):
163+
with self.device:
164+
x = array_ops.zeros([array_ops.identity(constant_op.constant(10))])
165+
for component in self.device.unpack(x):
166+
self.assertAllClose([0.] * 10, component)
167+
168+
def test_generator(self):
169+
with self.device:
170+
g_same = stateful_random_ops.Generator.from_seed(0)
171+
g_different = stateful_random_ops.Generator.from_seed(
172+
self.device.device_ids)
173+
same = g_same.normal([10])
174+
different = g_different.normal([10])
175+
same_unpacked = self.device.unpack(same)
176+
different_unpacked = self.device.unpack(different)
177+
for same_component, different_component in zip(same_unpacked[1:],
178+
different_unpacked[1:]):
179+
self.assertAllClose(same_component, same_unpacked[0])
180+
self.assertNotAllClose(different_component, different_unpacked[0])
181+
161182
def test_collective_reduce(self):
162183
if self.device_type == "TPU":
163184
self.skipTest("ParallelDevice collectives on TPUs need work")

‎tensorflow/python/ops/array_ops.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from tensorflow.python.framework import composite_tensor
2828
from tensorflow.python.framework import constant_op
2929
from tensorflow.python.framework import dtypes
30+
from tensorflow.python.framework import errors
3031
from tensorflow.python.framework import ops
3132
from tensorflow.python.framework import sparse_tensor
3233
from tensorflow.python.framework import tensor_shape
@@ -2964,7 +2965,7 @@ def zeros(shape, dtype=dtypes.float32, name=None):
29642965
# Go through tensor shapes to get int64-if-needed semantics
29652966
shape = constant_op._tensor_shape_tensor_conversion_function(
29662967
tensor_shape.TensorShape(shape))
2967-
except (TypeError, ValueError):
2968+
except (TypeError, ValueError, errors.UnimplementedError):
29682969
# Happens when shape is a list with tensor elements
29692970
shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)
29702971
if not shape._shape_tuple():

0 commit comments

Comments
 (0)