code: def testAllocationHistory(self):
      if not test.is_gpu_available(cuda_only=True):
          return
  
      gpu_dev = test.gpu_device_name()
      ops.reset_default_graph()
      with ops.device(gpu_dev):
          _, run_meta = _run_model()
  
      mm = _extract_node(run_meta, "MatMul")["gpu:0"][0]
      mm_allocs = mm.memory[0].allocation_records
      # has allocation and deallocation.
      self.assertEqual(len(mm_allocs), 2)
      # first allocated.
      self.assertGreater(
          mm_allocs[1].alloc_micros, mm_allocs[0].alloc_micros
      )
      self.assertGreater(mm_allocs[0].alloc_bytes, 0)
      # Then deallocated.
      self.assertLess(mm_allocs[1].alloc_bytes, 0)
      # All memory deallocated.
      self.assertEqual(
          mm_allocs[0].alloc_bytes + mm_allocs[1].alloc_bytes, 0
      )
  
      rand = _extract_node(
          run_meta, "random_normal/RandomStandardNormal"
      )["gpu:0"][0]
      random_allocs = rand.memory[0].allocation_records
      # random normal must allocated first since matmul depends on it.
      self.assertLess(
          random_allocs[0].alloc_micros, mm.all_start_micros
      )
      # deallocates the memory after matmul started.
      self.assertGreater(
          random_allocs[1].alloc_micros, mm.all_start_micros
      )
  
imports: from collections import defaultdict
  from tensorflow.core.protobuf import config_pb2
  from tensorflow.core.protobuf import rewriter_config_pb2
  from tensorflow.python.client import session
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import test_util
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import random_ops
  from tensorflow.python.ops import variables
  from tensorflow.python.platform import test
  from tensorflow.python.profiler import option_builder
  from tensorflow.python.profiler import model_analyzer
  from tensorflow.python.profiler.internal import model_analyzer_testlib
label_desc: Use image node_id 2 for calling the RunMetadataTest obj's underlying member method code with example usage: obj.testAllocationHistory() without return types