def test_conv3d(self, kwargs, expected_output_shape=None, requires_gpu=False): kwargs['filters'] = kwargs.get('filters', 2) kwargs['kernel_size'] = (3, 3, 3) # train_on_batch currently fails with XLA enabled on GPUs test_training = 'groups' not in kwargs or not test_util.is_xla_enabled() if not requires_gpu or test.is_gpu_available(cuda_only=True): self._run_test(kwargs, expected_output_shape, test_training)
def testSequenceFormatWithUnknownDims(self): if context.executing_eagerly(): return if test_util.is_xla_enabled() and self.pivoting: # Pivoting is not supported by xla backends. return superdiag = array_ops.placeholder(dtypes.float64, shape=[None]) diag = array_ops.placeholder(dtypes.float64, shape=[None]) subdiag = array_ops.placeholder(dtypes.float64, shape=[None]) rhs = array_ops.placeholder(dtypes.float64, shape=[None]) x = linalg_impl.tridiagonal_solve((superdiag, diag, subdiag), rhs, diagonals_format="sequence", partial_pivoting=self.pivoting) with self.cached_session() as sess: result = sess.run( x, feed_dict={ subdiag: [20, 1, -1, 1], diag: [1, 3, 2, 2], superdiag: [2, 1, 4, 20], rhs: [1, 2, 3, 4] }) self.assertAllClose(result, [-9, 5, -4, 4])
def testMinimizeSparseResourceVariableCentered(self): for dtype in _DATA_TYPES: if test_util.is_xla_enabled() and dtype.is_complex: self.skipTest("b/143578550") var0 = resource_variable_ops.ResourceVariable([[1.0, 2.0]], dtype=dtype) x = constant_op.constant([[4.0], [5.0]], dtype=dtype) def loss(): pred = math_ops.matmul( embedding_ops.embedding_lookup([var0], [0]), x) # pylint: disable=cell-var-from-loop return pred * pred # loss = lambda: pred * pred # pylint: disable=cell-var-from-loop sgd_op = rmsprop.RMSprop(learning_rate=1.0, rho=0.0, momentum=0.0, epsilon=1.0, centered=True).minimize(loss, var_list=[var0]) self.evaluate(variables.global_variables_initializer()) # Fetch params to validate initial values self.assertAllCloseAccordingToType([[1.0, 2.0]], self.evaluate(var0)) # Run 1 step of sgd self.evaluate(sgd_op) # Validate updated params self.assertAllCloseAccordingToType([[-111, -138]], self.evaluate(var0), atol=0.01)
def testCompareGpuVsCpu(self): in_shape = [2, 4, 6, 3] out_shape = [2, 8, 16, 3] size = np.prod(in_shape) x = 1.0 / size * np.arange(0, size).reshape(in_shape).astype( np.float32) # Align corners will be deprecated for tf2.0 and the false version is not # supported by XLA. align_corner_options = [True] if test_util.is_xla_enabled() else [ True, False ] for align_corners in align_corner_options: grad = {} for use_gpu in [False, True]: with self.cached_session(use_gpu=use_gpu): input_tensor = constant_op.constant(x, shape=in_shape) resized_tensor = image_ops.resize_bilinear( input_tensor, out_shape[1:3], align_corners=align_corners) grad[use_gpu] = gradient_checker.compute_gradient( input_tensor, in_shape, resized_tensor, out_shape, x_init_value=x) self.assertAllClose(grad[False], grad[True], rtol=1e-4, atol=1e-4)
def testMixedPrecision(self, required_gpus): if test_util.is_xla_enabled(): return # Test gets NaNs with XLA with policy.policy_scope('mixed_float16'): self._run_between_graph_clients(self._test_mixed_precision, self._cluster_spec, num_gpus=required_gpus)
def _gradientTest( self, diags, rhs, y, # output = reduce_sum(y * tridiag_solve(diags, rhs)) expected_grad_diags, # expected gradient of output w.r.t. diags expected_grad_rhs, # expected gradient of output w.r.t. rhs diags_format="compact", transpose_rhs=False, conjugate_rhs=False, feed_dict=None): expected_grad_diags = _tfconst(expected_grad_diags) expected_grad_rhs = _tfconst(expected_grad_rhs) with backprop.GradientTape() as tape_diags: with backprop.GradientTape() as tape_rhs: tape_diags.watch(diags) tape_rhs.watch(rhs) if test_util.is_xla_enabled(): # Pivoting is not supported by xla backends. return x = linalg_impl.tridiagonal_solve( diags, rhs, diagonals_format=diags_format, transpose_rhs=transpose_rhs, conjugate_rhs=conjugate_rhs) res = math_ops.reduce_sum(x * y) with self.cached_session() as sess: actual_grad_diags = sess.run(tape_diags.gradient(res, diags), feed_dict=feed_dict) actual_rhs_diags = sess.run(tape_rhs.gradient(res, rhs), feed_dict=feed_dict) self.assertAllClose(expected_grad_diags, actual_grad_diags) self.assertAllClose(expected_grad_rhs, actual_rhs_diags)
def _test(self, diags, rhs, expected, diags_format="compact", transpose_rhs=False, conjugate_rhs=False): with self.cached_session(): pivoting = True if hasattr(self, "pivoting"): pivoting = self.pivoting if test_util.is_xla_enabled() and pivoting: # Pivoting is not supported by xla backends. return result = linalg_impl.tridiagonal_solve(diags, rhs, diags_format, transpose_rhs, conjugate_rhs, partial_pivoting=pivoting) result = self.evaluate(result) if expected is None: self.assertAllEqual(np.zeros_like(result, dtype=np.bool), np.isfinite(result)) else: self.assertAllClose(result, expected)
def testGradientGraphOptimization(self): @def_function.function def f(x, y): with backprop.GradientTape() as tape: z = math_ops.mul(x, array_ops.zeros_like(x)) l = math_ops.add(z, y) l = math_ops.reduce_sum(l) gx, gy = tape.gradient(l, [x, y]) x.assign_add(gx) y.assign_add(gy) return x + y # XLA completely optimizes away the variable reads and # assignments, so skip the test. if test_util.is_xla_enabled(): self.skipTest('Not relevant for XLA') with context.eager_mode(): x = resource_variable_ops.ResourceVariable( np.random.uniform(size=[2, 2]), dtype=dtypes.float32) y = resource_variable_ops.ResourceVariable( np.random.uniform(size=[2, 2]), dtype=dtypes.float32) with context.collect_graphs(optimized=True) as graphs: f(x, y).numpy() self.assertLen(graphs, 1) assign_count = 0 for node in graphs[0].node: if node.op == 'AssignAddVariableOp': self.assertEqual(node.input[0], 'y') assign_count += 1 # Make sure that the only variable update that remains after # grappler optimization is that of y. self.assertEqual(assign_count, 1) self.assertLen(graphs[0].node, 11)
def testAutoclusteringWithTfFunction(self): if 'tpu' in self.device.lower(): self.skipTest('Autoclustering does not run on TPU') with ops.device('device:{}:0'.format(self.device)): @def_function.function(experimental_compile=False) def outer(a, b, c): return a * inner(b, c) + c @def_function.function(experimental_compile=True) def inner(b, c): return b + c * b i1 = constant_op.constant([1.0, 2.0, 3.0, 4.0, 5.0]) i2 = constant_op.constant([1.0, 2.0, 3.0, 4.0, 5.0]) i3 = constant_op.constant([1.0, 2.0, 3.0, 4.0, 5.0]) with context.collect_graphs(optimized=True) as graphs: outer(i1, i2, i3) if test_util.is_xla_enabled(): self.assertIn('_XlaRun', [n.op for n in graphs[0].node]) else: self.assertNotIn('_XlaRun', [n.op for n in graphs[0].node])
def test2x2NotInvertible(self): if test_util.is_xla_enabled(): # XLA implementation does not check invertibility. return with self.assertRaises(errors_impl.InvalidArgumentError): self._testWithLists( diags=[[3, 0], [1, 3], [0, 1]], rhs=[1, 4], expected=[])
def testCheckpoint(self, delayed, restore_shards): if test_util.is_xla_enabled() and not delayed and restore_shards == 4: self.skipTest( "TODO(b/202760274): Would raise an error that is to be " "investigated.") def make_variable(name, shape, dtype, initializer): initial_value = functools.partial(initializer, shape, dtype=dtype) return variables.Variable(name=name, initial_value=initial_value, shape=shape, dtype=dtype) class Model(tracking.AutoTrackable): def build(self): self.w = self._add_variable_with_custom_getter( "w", shape=(4, ), initializer=init_ops_v2.Ones(), getter=make_variable) strategy = parameter_server_strategy_v2.ParameterServerStrategyV2( self.cluster_resolver, sharded_variable.FixedShardsPartitioner(2)) ckpt_dir = os.path.join(self.get_temp_dir(), "checkpoint") with strategy.scope(): model1 = Model() model1.build() self.assertIsInstance(model1.w, sharded_variable.ShardedVariable) self.assertLen(model1.w.variables, 2) model1.w.assign([1., 2., 3., 4.]) cp1 = tracking_util.Checkpoint(model=model1) cp1.write(ckpt_dir) strategy = parameter_server_strategy_v2.ParameterServerStrategyV2( self.cluster_resolver, sharded_variable.FixedShardsPartitioner(restore_shards)) with strategy.scope(): model2 = Model() cp2 = tracking_util.Checkpoint(model=model2) if delayed: cp2.restore(ckpt_dir) model2.build() else: model2.build() cp2.restore(ckpt_dir) self.assertIsInstance(model2.w, sharded_variable.ShardedVariable) self.assertLen(model2.w.variables, restore_shards) if restore_shards == 2: self.assertAllEqual(model2.w.variables[0], [1., 2.]) self.assertAllEqual(model2.w.variables[1], [3., 4.]) elif restore_shards == 4: self.assertAllEqual(model2.w.variables[0], [1.]) self.assertAllEqual(model2.w.variables[1], [2.]) self.assertAllEqual(model2.w.variables[2], [3.]) self.assertAllEqual(model2.w.variables[3], [4.])
def test0x0(self): if test_util.is_xla_enabled(): # The following test crashes with XLA due to slicing 0 length tensors. return self._test( diags=constant_op.constant(0, shape=(3, 0), dtype=dtypes.float32), rhs=constant_op.constant(0, shape=(0, 1), dtype=dtypes.float32), expected=constant_op.constant(0, shape=(0, 1), dtype=dtypes.float32))
def testDeterministicGradients(self, align_corners, half_pixel_centers, data_type): if not align_corners and test_util.is_xla_enabled(): # Align corners is deprecated in TF2.0, but align_corners==False is not # supported by XLA. self.skipTest('align_corners==False not currently supported by XLA') with self.session(force_gpu=True): seed = ( hash(align_corners) % 256 + hash(half_pixel_centers) % 256 + hash(data_type) % 256) np.random.seed(seed) input_shape = (1, 25, 12, 3) # NHWC output_shape = (1, 200, 250, 3) input_image = self._randomDataOp(input_shape, data_type) repeat_count = 3 if context.executing_eagerly(): def resize_bilinear_gradients(local_seed): np.random.seed(local_seed) upstream_gradients = self._randomDataOp(output_shape, dtypes.float32) with backprop.GradientTape(persistent=True) as tape: tape.watch(input_image) output_image = image_ops.resize_bilinear( input_image, output_shape[1:3], align_corners=align_corners, half_pixel_centers=half_pixel_centers) gradient_injector_output = output_image * upstream_gradients return tape.gradient(gradient_injector_output, input_image) for i in range(repeat_count): local_seed = seed + i # select different upstream gradients result_a = resize_bilinear_gradients(local_seed) result_b = resize_bilinear_gradients(local_seed) self.assertAllEqual(result_a, result_b) else: # graph mode upstream_gradients = array_ops.placeholder( dtypes.float32, shape=output_shape, name='upstream_gradients') output_image = image_ops.resize_bilinear( input_image, output_shape[1:3], align_corners=align_corners, half_pixel_centers=half_pixel_centers) gradient_injector_output = output_image * upstream_gradients # The gradient function behaves as if grad_ys is multiplied by the op # gradient result, not passing the upstram gradients through the op's # gradient generation graph. This is the reason for using the # gradient injector resize_bilinear_gradients = gradients_impl.gradients( gradient_injector_output, input_image, grad_ys=None, colocate_gradients_with_ops=True)[0] for i in range(repeat_count): feed_dict = {upstream_gradients: self._randomNDArray(output_shape)} result_a = resize_bilinear_gradients.eval(feed_dict=feed_dict) result_b = resize_bilinear_gradients.eval(feed_dict=feed_dict) self.assertAllEqual(result_a, result_b)
def testMixedPrecision(self, num_gpus): if context.num_gpus() < num_gpus: self.skipTest('Not enough GPUs') if test_util.is_xla_enabled(): self.skipTest('Test gets NaNs with XLA') with policy.policy_scope('mixed_float16'): self._run_between_graph_clients(self._test_mixed_precision, self._cluster_spec, num_gpus=num_gpus)
def should_execute_combination(self, kwargs): distributions = [ v for v in kwargs.values() if isinstance(v, NamedDistribution) ] if test_util.is_xla_enabled() and any(d.no_xla for d in distributions): return ( False, "n/a: skipping strategy combination with no_xla=True in XLA tests") return (True, None)
def testBatchFunctionOpWithLargeBatchSplitted(self): """Tests that the batch_function op works with large batch splitted.""" if test_util.is_xla_enabled(): self.skipTest("b/178649404") if context.executing_eagerly(): return with self.cached_session() as sess: @function.Defun(dtypes.int32) def computation(in_t): return in_t + 3 inp = array_ops.placeholder(dtype=dtypes.int32) result = gen_batch_ops.batch_function( [inp], num_batch_threads=2, # enable_large_batch_splitting is True, so it's valid as long as # max('allowed_batch_sizes') <= 'max_batch_size'. allowed_batch_sizes=[1, 2], max_batch_size=5, batch_timeout_micros=100000, # 100ms Tout=[dtypes.int32], enable_large_batch_splitting=True, f=computation, captured_tensors=computation.captured_inputs) thread1_results = [] thread2_results = [] # Input sizes of worker1 and main thread are larger than # max(allowed_batch_sizes), while input size of worker2 is smaller. def worker1(): thread1_results.extend( sess.run([result], feed_dict={inp: [5, 6, 7, 8, 9]})) worker_thread1 = threading.Thread(target=worker1) worker_thread1.start() def worker2(): thread2_results.extend(sess.run([result], feed_dict={inp: [10]})) worker_thread2 = threading.Thread(target=worker2) worker_thread2.start() main_results = sess.run([result], feed_dict={inp: [2, 3, 4]}) worker_thread1.join() worker_thread2.join() self.assertTrue( np.all(np.equal(thread2_results[0], np.array([13], dtype=np.int32)))) self.assertTrue( np.all( np.equal(thread1_results[0], np.array([8, 9, 10, 11, 12], dtype=np.int32)))) self.assertTrue( np.all( np.equal(main_results[0], np.array([5, 6, 7], dtype=np.int32))))
def _assertRaises(self, diags, rhs, diags_format="compact"): pivoting = True if hasattr(self, "pivoting"): pivoting = self.pivoting if test_util.is_xla_enabled() and pivoting: # Pivoting is not supported by xla backends. return with self.assertRaises(ValueError): linalg_impl.tridiagonal_solve( diags, rhs, diags_format, partial_pivoting=pivoting)
def testLargeTensor(self): # Test case for GItHub issue 46911. if test_util.is_xla_enabled(): # The following test fails with XLA enabled. return with self.assertRaises(errors_impl.InternalError): with self.cached_session(): tiled = array_ops.tile(np.ones((1, 1, 1)), [100000000, 100000000, 100000000]) self.evaluate(tiled)
def test_xla_fails_on_unsupported(self): if not test_util.is_xla_enabled() or not test_util.is_gpu_available(): return with self.assertRaises(errors.UnimplementedError): with self.session(force_gpu=True) as sess: graph, placeholder = self._makeGraphWithUnsupportedOp() res = sess.run(graph, feed_dict={placeholder: True}) # We should never reach the below line but if we do also assert the # value is correct. self.assertEqual(True, res)
def _testBothArg(self, method, x, axis, expected_values, expected_err_re=None): self._testArg(method, x, axis, expected_values, True, expected_err_re) # Compilation time is too large with XLA/CPU autojit. if not test_util.is_xla_enabled(): self._testArg(method, x, axis, expected_values, False, expected_err_re)
def testNotInvertible(self): if test.is_gpu_available(cuda_only=True) or test_util.is_xla_enabled(): # CuSparse gtsv routines don't raise errors for non-invertible # matrices. return with self.assertRaises(errors_impl.InvalidArgumentError): self._testWithLists( diags=[[2, -1, 1, 0], [1, 4, 1, -1], [0, 2, 0, 3]], rhs=[1, 2, 3, 4], expected=[8, -3.5, 0, -4])
def testEqualityBroadcast(self): default = ops.Tensor._USE_EQUALITY try: tf_a = constant_op.constant([1, 1]) tf_b = constant_op.constant([1, 1]) tf_c = constant_op.constant([[1, 1], [1, 1]]) tf_d = constant_op.constant([[1, 2], [1, 2]]) tf_e = constant_op.constant([1, 1, 1]) np_a = np.array([1, 1]) np_b = np.array([1, 1]) np_c = np.array([[1, 1], [1, 1]]) np_d = np.array([[1, 2], [1, 2]]) np_e = np.array([1, 1, 1]) ops.disable_tensor_equality() # We don't do element-wise comparison self.assertNotEqual(tf_a, tf_b) self.assertNotEqual(tf_a, tf_c) self.assertNotEqual(tf_a, tf_d) ops.enable_tensor_equality() # We do element-wise comparison but can't convert results array to bool with self.assertRaises(ValueError): bool(tf_a == tf_b) self.assertAllEqual(tf_a == tf_b, [True, True]) with self.assertRaises(ValueError): bool(tf_a == tf_c) self.assertAllEqual(tf_a == tf_c, [[True, True], [True, True]]) with self.assertRaises(ValueError): bool(tf_a == tf_d) self.assertAllEqual(tf_a == tf_d, [[True, False], [True, False]]) # TODO(b/207402791): re-enable once incompatible shapes supported by XLA. if not test_util.is_xla_enabled(): self.assertFalse(bool(tf_a == tf_e)) self.assertTrue(bool(tf_a != tf_e)) self.assertNotAllEqual(tf_a, tf_e) with self.assertRaises(ValueError): bool(np_a == np_b) self.assertAllEqual(np_a == np_b, [True, True]) with self.assertRaises(ValueError): bool(np_a == np_c) self.assertAllEqual(np_a == np_c, [[True, True], [True, True]]) self.assertAllEqual(np_a == np_d, [[True, False], [True, False]]) self.assertFalse(bool(np_a == np_e)) self.assertTrue(bool(np_a != np_e)) self.assertNotAllEqual(np_a, np_e) finally: if default: ops.enable_tensor_equality() else: ops.disable_tensor_equality()
def _create_parameter_server(): if framework_test_util.is_xla_enabled(): # To address test failures resulting in XLA with MultiProcessRunner, # continue to use in-process cluster for XLA tests. cluster_def = multi_worker_test_base.create_in_process_cluster( num_workers=num_workers, num_ps=num_ps, rpc_layer="grpc") resolver = cluster_resolver.SimpleClusterResolver( server_lib.ClusterSpec(cluster_def), num_accelerators={"GPU": required_gpus}, rpc_layer="grpc") return _create_ps_strategy(resolver, variable_partitioner) else: tf_config = cluster_resolver.TFConfigClusterResolver() cluster_def = tf_config.cluster_spec().as_dict() if not cluster_def: # When MultiProcessRunner cluster is used, the cluster is not created # initially when the decorator is called. When the test runs, initially # this method is invoked via decorator before setting up the # MultiProcessRunner with worker and ps in the combinations.py. After # setup is done, the subprocess invokes this method again to get # strategy object. We return None strategy when the main thread invokes # this method before setting up cluster. # Returning None is fine here, since this thread will proceed to create # MultiProcessRunner and invoke tests with decorator inside # subprocesses. return None # MultiProcessRunner is already setup and this method is invoked from a # subprocess running the actual test. resolver = cluster_resolver.SimpleClusterResolver( server_lib.ClusterSpec(cluster_def), num_accelerators={"GPU": required_gpus}, task_type=tf_config.task_type, task_id=tf_config.task_id, environment=tf_config.environment, rpc_layer=tf_config.rpc_layer or "grpc") if tf_config.task_type in ("worker", "ps"): worker_config = config_pb2.ConfigProto() worker_config.inter_op_parallelism_threads = 4 # max num_workers + 1 try: server = server_lib.Server(cluster_def, job_name=tf_config.task_type, task_index=tf_config.task_id, protocol="grpc", config=worker_config) except errors.UnknownError as e: if "Could not start gRPC server" in e.message: raise unittest.SkipTest("Cannot start std servers.") else: raise # Blocking the process that starts a server from exiting. server.join() return _create_ps_strategy(resolver, variable_partitioner)
def testExperimentalCompileRaisesExceptionWhenXlaIsUnsupported(self): if test.is_built_with_rocm() or test_util.is_xla_enabled(): return with self.assertRaisesRegexp(ValueError, 'XLA support is not'): @def_function.function(experimental_compile=True) def fn(x): return array_ops.unique(x).y fn([1, 1, 2, 3])
def test_dnn_correctness_minus_tpus(self, distribution, optimizer_fn, iteration_type, inside_func, sync_batchnorm): # TODO(anjs): Identify why this particular V1 optimizer needs a higher tol. if 'FtrlV1' in optimizer_fn._name and 'TPU' in type(distribution).__name__: self.skipTest('Reduced tolerance of the order of 1e-1 required.') if ('CollectiveAllReduce' in type(distribution).__name__ and test_util.is_xla_enabled()): self.skipTest('XLA tests fail with MWMS.') self.dnn_correctness(distribution, optimizer_fn, iteration_type, inside_func, sync_batchnorm)
def testJitCompileRaisesExceptionWhenXlaIsUnsupported(self): if test.is_built_with_rocm() or test_util.is_xla_enabled(): return with self.assertRaisesRegex(errors.UnimplementedError, 'support for that platform linked in'): @def_function.function(jit_compile=True) def fn(x): return x + x fn([1, 1, 2, 3])
def testExperimentalCompileRaisesExceptionWhenXlaIsUnsupported(self): if test.is_built_with_rocm() or test_util.is_xla_enabled(): return with self.assertRaisesRegexp(errors.UnimplementedError, 'check target linkage'): @def_function.function(experimental_compile=True) def fn(x): return x + x fn([1, 1, 2, 3])
def _is_unimplemented(self): unimplemented = False pivoting = True if hasattr(self, "pivoting"): pivoting = self.pivoting perturb_singular = False if hasattr(self, "perturb_singular"): perturb_singular = self.perturb_singular if (test_util.is_xla_enabled() or test.is_gpu_available(cuda_only=True)) and perturb_singular: # Perturbed solve not supported on XLA or GPU. unimplemented = True return unimplemented, pivoting, perturb_singular
def _itGen(self, smaller_shape, larger_shape): up_sample = (smaller_shape, larger_shape) down_sample = (larger_shape, smaller_shape) pass_through = (larger_shape, larger_shape) shape_pairs = (up_sample, down_sample, pass_through) # Align corners is deprecated in TF2.0, but align_corners==False is not # supported by XLA. options = [(True, False)] if not test_util.is_xla_enabled(): options += [(False, True), (False, False)] for align_corners, half_pixel_centers in options: for in_shape, out_shape in shape_pairs: yield in_shape, out_shape, align_corners, half_pixel_centers
def test_conv3d(self, kwargs, expected_output_shape=None, requires_gpu=False): kwargs["filters"] = kwargs.get("filters", 2) kwargs["kernel_size"] = (3, 3, 3) # train_on_batch currently fails with XLA enabled on GPUs test_training = ("groups" not in kwargs or not tf_test_utils.is_xla_enabled()) if not requires_gpu or tf.test.is_gpu_available(cuda_only=True): self._run_test(kwargs, expected_output_shape, test_training) self._run_test_extra_batch_dim(kwargs, expected_output_shape, test_training)
def testCompareGpuVsCpu(self): in_shape = [2, 4, 6, 3] out_shape = [2, 8, 16, 3] size = np.prod(in_shape) x = 1.0 / size * np.arange(0, size).reshape(in_shape).astype(np.float32) # Align corners will be deprecated for tf2.0 and the false version is not # supported by XLA. align_corner_options = [True ] if test_util.is_xla_enabled() else [True, False] for align_corners in align_corner_options: grad = {} for use_gpu in [False, True]: with self.cached_session(use_gpu=use_gpu): input_tensor = constant_op.constant(x, shape=in_shape) resized_tensor = image_ops.resize_bilinear( input_tensor, out_shape[1:3], align_corners=align_corners) grad[use_gpu] = gradient_checker.compute_gradient( input_tensor, in_shape, resized_tensor, out_shape, x_init_value=x) self.assertAllClose(grad[False], grad[True], rtol=1e-4, atol=1e-4)
def testGradient(self): with self.session(use_gpu=True): # Input: [batch, height, width, input_depth] x_shape = [2, 5, 6, 2] # Filter: [kernel_height, kernel_width, input_depth, output_depth] f_shape = [3, 3, 2, 2] # Output: [batch, height, width, output_depth] y_shape = [2, 5, 6, 2] np.random.seed(1) # Make it reproducible. x_val = np.random.random_sample(x_shape).astype(np.float32) f_val = np.random.random_sample(f_shape).astype(np.float32) x = constant_op.constant(x_val, name="x", dtype=dtypes.float32) f = constant_op.constant(f_val, name="f", dtype=dtypes.float32) for rate in range(1, 4): output = nn_ops.atrous_conv2d(x, f, rate=rate, padding="SAME") err = gradient_checker.compute_gradient_error([x, f], [x_shape, f_shape], output, y_shape) print("atrous_conv2d gradient err = %g " % err) err_tolerance = 4e-3 if test_util.is_xla_enabled() else 1e-3 self.assertLess(err, err_tolerance)
def test_binary_cwise_ops(self): logical_ops = [ math_ops.logical_and, math_ops.logical_or, math_ops.logical_xor ] # Wrapper functions restricting the range of inputs of zeta and polygamma. def safe_polygamma(x, y): return math_ops.polygamma( math_ops.round(clip_ops.clip_by_value(y, 1, 10)), x * x + 1) def safe_zeta(x, y): return math_ops.zeta(x * x + 1, y * y) float_ops = [ math_ops.add, math_ops.add_v2, math_ops.atan2, math_ops.complex, math_ops.div, math_ops.divide, math_ops.div_no_nan, math_ops.equal, math_ops.floor_mod, math_ops.greater, math_ops.greater_equal, math_ops.igamma, math_ops.igammac, math_ops.igamma_grad_a, math_ops.less, math_ops.less_equal, math_ops.maximum, math_ops.minimum, math_ops.mod, math_ops.multiply, math_ops.not_equal, math_ops.pow, math_ops.squared_difference, math_ops.subtract, math_ops.truncate_mod, safe_polygamma, safe_zeta, ] # FloorDiv fails on XLA due floor's discontinuities exacerbating small # division differences. if not test_util.is_xla_enabled(): float_ops += [math_ops.floor_div] for op in logical_ops + float_ops: x = random_ops.random_uniform([7, 3, 5]) y = random_ops.random_uniform([3, 5]) if op in logical_ops: x = x > 0 y = y > 0 output_dtypes = [] # pylint: disable=cell-var-from-loop def loop_fn(i): x1 = array_ops.gather(x, i) y1 = array_ops.gather(y, i) outputs = [op(x, y), op(x1, y), op(x, y1), op(x1, y1), op(x1, x1)] del output_dtypes[:] output_dtypes.extend([t.dtype for t in outputs]) return outputs # pylint: enable=cell-var-from-loop self._test_loop_fn(loop_fn, 3, loop_fn_dtypes=output_dtypes)