MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / testGradient

Method testGradient

tensorflow/compiler/tests/jit_test.py:429–478  ·  view source on GitHub ↗

Tests that the backprop function is properly compiled.

(self)

Source from the content-addressed store, hash-verified

427 self.assertAllClose(result, np.float32(63), rtol=1e-1)
428
429 def testGradient(self):
430 """Tests that the backprop function is properly compiled."""
431
432 def _Run(compiled):
433
434 @function.Defun(compiled=compiled)
435 def Forward(x):
436 return math_ops.log(x)
437
438 g = ops.Graph()
439 with g.as_default():
440 x = array_ops.placeholder(dtypes.float32)
441 y = Forward(x)
442 dx, = gradients_impl.gradients(y, [x], 1.0)
443
444 cfg = NoRewriteSessionConfig()
445 cfg.graph_options.optimizer_options.opt_level = (
446 config_pb2.OptimizerOptions.L1)
447 cfg.graph_options.optimizer_options.do_function_inlining = True
448 with session_lib.Session(graph=g, config=cfg) as sess:
449 run_metadata = config_pb2.RunMetadata()
450 dx_val = test_utils.RunWithWarmup(
451 sess,
452 dx,
453 feed_dict={x: 100.},
454 run_metadata=run_metadata,
455 options=config_pb2.RunOptions(
456 trace_level=config_pb2.RunOptions.FULL_TRACE))
457 self.assertAllClose(dx_val, 0.01)
458 return RunMetadataLabels(run_metadata)
459
460 # SymGrad[f=log(x)](x, dy) = 1/x * dy
461 #
462 # Note: we don't need to compute log(x) for dx due to graph pruning.
463
464 # Do not compile the backprop. We should see one Reciprocal and one Mul.
465 labels = _Run(compiled=False)
466 self.assertFalse(InLabels(labels, "Log"))
467 self.assertTrue(InLabels(labels, "Reciprocal"))
468 self.assertTrue(InLabels(labels, "Mul"))
469 self.assertFalse(InLabels(labels, "XlaCompile"))
470 self.assertFalse(InLabels(labels, "XlaRun"))
471
472 # Compile the backprop. One XlaCompile/XlaRun pair.
473 labels = _Run(compiled=True)
474 self.assertFalse(InLabels(labels, "Log"))
475 self.assertFalse(InLabels(labels, "Reciprocal"))
476 self.assertFalse(InLabels(labels, "Mul"))
477 self.assertTrue(InLabels(labels, "XlaCompile"))
478 self.assertTrue(InLabels(labels, "XlaRun"))
479
480
481class ElementWiseFusionTest(test.TestCase):

Callers

nothing calls this directly

Calls 1

InLabelsFunction · 0.70

Tested by

no test coverage detected