def _remote_finalize_func(string_handle): return functional_ops.remote_call( target=self._source_device, args=[string_handle] + finalize_func_concrete.captured_inputs, Tout=[dtypes.int64], f=finalize_func_concrete)
def testRemoteFunction(self): worker_config = config_pb2.ConfigProto() worker_config.device_count["CPU"] = 2 worker, _ = test_util.create_local_cluster( 1, 1, worker_config=worker_config) @function.Defun(dtypes.int32, dtypes.int32) def _remote_fn(a, b): return math_ops.multiply(a, b) with ops.device("/job:ps/task:0"): a = variables.Variable(2, dtype=dtypes.int32) b = variables.Variable(3, dtype=dtypes.int32) with ops.device("/job:worker/replica:0/task:0/cpu:0"): remote_op = functional_ops.remote_call( args=[a, b], Tout=[dtypes.int32], f=_remote_fn, target="/job:worker/replica:0/task:0/cpu:1") with session.Session(worker[0].target) as sess: self.evaluate(variables.global_variables_initializer()) mul = self.evaluate(remote_op) self.assertEqual(mul, [6])
def _remote_next_func(string_handle): return functional_ops.remote_call( target=self._source_device, args=[string_handle] + next_func_concrete.captured_inputs, Tout=self._input_dataset._element_structure._flat_types, # pylint: disable=protected-access f=next_func_concrete)
def _remote_next_func(string_handle): return functional_ops.remote_call( target=self._source_device, args=[string_handle] + next_func_concrete.captured_inputs, Tout=self._flat_output_types, f=next_func_concrete)
def _testRemoteIteratorHelper(self, device0, device1, target): with ops.device(device1): dataset_3 = dataset_ops.Dataset.from_tensor_slices([1, 2, 3]) iterator_3 = dataset_3.make_one_shot_iterator() iterator_3_handle = iterator_3.string_handle() @function.Defun(dtypes.string) def _remote_fn(h): remote_iterator = iterator_ops.Iterator.from_string_handle( h, dataset_3.output_types, dataset_3.output_shapes) return remote_iterator.get_next() with ops.device(device0): target_placeholder = array_ops.placeholder(dtypes.string, shape=[]) remote_op = functional_ops.remote_call( args=[iterator_3_handle], Tout=[dtypes.int32], f=_remote_fn, target=target_placeholder) with session.Session(target) as sess: elem = sess.run(remote_op, feed_dict={target_placeholder: device1}) self.assertEqual(elem, [1]) # Fails when target is cpu:0 where the resource is not located. with self.assertRaises(errors.InvalidArgumentError): sess.run(remote_op, feed_dict={target_placeholder: device0}) elem = sess.run(iterator_3.get_next()) self.assertEqual(elem, [2]) elem = sess.run(remote_op, feed_dict={target_placeholder: device1}) self.assertEqual(elem, [3]) with self.assertRaises(errors.OutOfRangeError): sess.run(remote_op, feed_dict={target_placeholder: device1})
def testRemoteIteratorUsingRemoteCallOpDirectSession(self): worker_config = config_pb2.ConfigProto() worker_config.device_count["CPU"] = 3 with ops.device("/job:localhost/replica:0/task:0/cpu:1"): dataset_3 = dataset_ops.Dataset.from_tensor_slices([1, 2, 3]) iterator_3 = dataset_ops.make_one_shot_iterator(dataset_3) iterator_3_handle = iterator_3.string_handle() @function.Defun(dtypes.string) def _remote_fn(h): remote_iterator = iterator_ops.Iterator.from_string_handle( h, dataset_ops.get_legacy_output_types(dataset_3), dataset_ops.get_legacy_output_shapes(dataset_3)) return remote_iterator.get_next() with ops.device("/job:localhost/replica:0/task:0/cpu:0"): target_placeholder = array_ops.placeholder(dtypes.string, shape=[]) remote_op = functional_ops.remote_call( args=[iterator_3_handle], Tout=[dtypes.int32], f=_remote_fn, target=target_placeholder) with self.session(config=worker_config) as sess: elem = sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:1" }) self.assertEqual(elem, [1]) # Fails when target is cpu:2 where the resource is not located. with self.assertRaises(errors.InvalidArgumentError): sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:2" }) elem = sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:1" }) self.assertEqual(elem, [2]) elem = sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:1" }) self.assertEqual(elem, [3]) with self.assertRaises(errors.OutOfRangeError): sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:1" })
def testRemoteIteratorUsingRemoteCallOpDirectSessionGPUCPU(self): if not test_util.is_gpu_available(): self.skipTest("No GPU available") with ops.device("/job:localhost/replica:0/task:0/cpu:0"): dataset_3 = dataset_ops.Dataset.from_tensor_slices([1, 2, 3]) iterator_3 = dataset_ops.make_one_shot_iterator(dataset_3) iterator_3_handle = iterator_3.string_handle() def _encode_raw(byte_array): return bytes(bytearray(byte_array)) @function.Defun(dtypes.uint8) def _remote_fn(h): handle = script_ops.py_func(_encode_raw, [h], dtypes.string) remote_iterator = iterator_ops.Iterator.from_string_handle( handle, dataset_ops.get_legacy_output_types(dataset_3), dataset_ops.get_legacy_output_shapes(dataset_3)) return remote_iterator.get_next() with ops.device("/job:localhost/replica:0/task:0/device:GPU:0"): target_placeholder = array_ops.placeholder(dtypes.string, shape=[]) iterator_3_handle_uint8 = parsing_ops.decode_raw( bytes=iterator_3_handle, out_type=dtypes.uint8) remote_op = functional_ops.remote_call( args=[iterator_3_handle_uint8], Tout=[dtypes.int32], f=_remote_fn, target=target_placeholder) with self.cached_session() as sess: elem = sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:0" }) self.assertEqual(elem, [1]) elem = sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:0" }) self.assertEqual(elem, [2]) elem = sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:0" }) self.assertEqual(elem, [3]) with self.assertRaises(errors.OutOfRangeError): sess.run( remote_op, feed_dict={ target_placeholder: "/job:localhost/replica:0/task:0/cpu:0" })
def MapFn(unused_input): if isinstance(source_dataset.output_types, dtypes.DType): output_types = [source_dataset.output_types] elif isinstance(source_dataset.output_types, (list, tuple)): output_types = source_dataset.output_types else: raise ValueError('source dataset has invalid output types') remote_calls = functional_ops.remote_call( args=[source_handle], Tout=output_types, f=LoadingFunc, target='/job:%s/replica:0/task:0/cpu:0' % file_reader_job) if len(remote_calls) == 1: return remote_calls[0] else: return remote_calls
def testRemoteFunctionGPUCPUStrings(self): if not test_util.is_gpu_available(): self.skipTest("No GPU available") @function.Defun(dtypes.string) def _remote_fn(inp): return array_ops.identity(inp) a = array_ops.constant("a") with ops.device("/gpu:0"): remote_op = functional_ops.remote_call( args=[a], Tout=[dtypes.string], f=_remote_fn, target="/cpu:0") with self.cached_session() as sess: ret = self.evaluate(remote_op) self.assertAllEqual(ret, [b"a"])
def testRemoteFunctionSameDeviceDirectSession(self): @function.Defun(dtypes.int32, dtypes.int32) def _remote_fn(a, b): return math_ops.multiply(a, b) with ops.device("/cpu:0"): a = variables.Variable(2, dtype=dtypes.int32) b = variables.Variable(3, dtype=dtypes.int32) with ops.device("/cpu:0"): remote_op = functional_ops.remote_call( args=[a, b], Tout=[dtypes.int32], f=_remote_fn, target="/cpu:0") with self.cached_session() as sess: self.evaluate(variables.global_variables_initializer()) mul = self.evaluate(remote_op) self.assertEqual(mul, [6])
def testRemoteFunctionCrossProcess(self): workers, _ = test_util.create_local_cluster(2, 1) @function.Defun(dtypes.float32, dtypes.float32) def _remote_fn(a, b): return math_ops.multiply(a, b) with ops.device("/job:ps/task:0"): a = variables.Variable(2, dtype=dtypes.float32) b = variables.Variable(3, dtype=dtypes.float32) with ops.device("/job:worker/replica:0/task:0/cpu:0"): remote_op = functional_ops.remote_call( args=[a, b], Tout=[dtypes.float32], f=_remote_fn, target="/job:worker/replica:0/task:1/cpu:0")[0] + 3.0 with session.Session(workers[0].target) as sess: self.evaluate(variables.global_variables_initializer()) mul = self.evaluate(remote_op) self.assertEqual(mul, 9)
def testRemoteFunctionGPUCPU(self): if not test_util.is_gpu_available(): self.skipTest("No GPU available") @function.Defun(dtypes.float32, dtypes.float32) def _remote_fn(a, b): return math_ops.multiply(a, b) with ops.device("/job:localhost/replica:0/task:0/device:GPU:0"): a = variables.Variable(2, dtype=dtypes.float32) b = variables.Variable(3, dtype=dtypes.float32) with ops.device("/job:localhost/replica:0/task:0/device:GPU:0"): remote_op = functional_ops.remote_call( args=[a, b], Tout=[dtypes.float32], f=_remote_fn, target="/job:localhost/replica:0/task:0/cpu:0")[0] + 3.0 with self.cached_session() as sess: self.evaluate(variables.global_variables_initializer()) mul = self.evaluate(remote_op) self.assertEqual(mul, 9.0)
def testRemoteFunctionDirectSession(self): worker_config = config_pb2.ConfigProto() worker_config.device_count["CPU"] = 2 @function.Defun(dtypes.int32, dtypes.int32) def _remote_fn(a, b): return math_ops.multiply(a, b) with ops.device("/job:localhost/replica:0/task:0/cpu:0"): a = variables.Variable(2, dtype=dtypes.int32) b = variables.Variable(3, dtype=dtypes.int32) with ops.device("/job:localhost/replica:0/task:0/cpu:0"): remote_op = functional_ops.remote_call( args=[a, b], Tout=[dtypes.int32], f=_remote_fn, target="/job:localhost/replica:0/task:0/cpu:1") with self.test_session(config=worker_config) as sess: sess.run(variables.global_variables_initializer()) mul = sess.run(remote_op) self.assertEqual(mul, [6])
def _remote_next_func(string_handle): return functional_ops.remote_call( target=source_device, args=[string_handle] + next_func_concrete.captured_inputs, Tout=structure.get_flat_tensor_types(self._element_spec), f=next_func_concrete)
def func(x): return functional_ops.remote_call( args=[x], Tout=[dtypes.int32], f=remote_fn, target='/job:worker/task:0')
def map_fn(target, handle): return functional_ops.remote_call( args=[handle], Tout=[dtypes.string], f=loading_func, target=target)
def _remote_init_func(): return functional_ops.remote_call( target=self._source_device, args=init_func_concrete.captured_inputs, Tout=[dtypes.string], f=init_func_concrete)
def MapFn(unused_input): return functional_ops.remote_call( args=[source_handle], Tout=[dtypes.string], f=LoadingFunc, target='/job:%s/replica:0/task:0/cpu:0' % file_reader_job)[0]
def MapFn(unused_input): return functional_ops.remote_call( args=[source_handle], Tout=[dtypes.string], f=LoadingFunc, target='/job:%s/replica:0/task:0/cpu:0' % file_reader_job)
def _remote_next_func(string_handle): return functional_ops.remote_call( target=source_device, args=[string_handle] + next_func_concrete.captured_inputs, Tout=self._structure._flat_types, # pylint: disable=protected-access f=next_func_concrete)
def _remote_init_func(): return functional_ops.remote_call( target=source_device, args=init_func_concrete.captured_inputs, Tout=[dtypes.string], f=init_func_concrete)
def _remote_finalize_func(string_handle): return functional_ops.remote_call( target=source_device, args=[string_handle] + finalize_func_concrete.captured_inputs, Tout=[dtypes.int64], f=finalize_func_concrete)
def _remote_init_func(): return functional_ops.remote_call( target=self._source_device, args=_init_func.captured_inputs, Tout=[dtypes.string], f=_init_func)
def _remote_next_func(string_handle): return functional_ops.remote_call( target=source_device, args=[string_handle] + _next_func.captured_inputs, Tout=self._flat_output_types, f=_next_func)
def _remote_init_func(): return functional_ops.remote_call( target=source_device, args=_init_func.captured_inputs, Tout=[dtypes.string], f=_init_func)
def map_fn(target, handle): return functional_ops.remote_call(args=[handle], Tout=[dtypes.string], f=loading_func, target=target)