code: def generate_efficient_write_read_instruction(instruction_array):
      """Statically generate a memory efficient write/read instruction."""
      nsteps_within_tree = np.max(instruction_array) + 1
      instruction_mat = np.zeros(
          (nsteps_within_tree, nsteps_within_tree)
      )
      for previous_step, current_step in instruction_array:
          instruction_mat[previous_step, current_step] = 1
  
      # Generate a sparse matrix that represents the memory footprint:
      #   -1 : no need to save to memory (these are odd steps)
      #    1 : needed for check u turn (either already in memory or will be saved)
      #    0 : still in memory but not needed for check u turn
      for i in range(nsteps_within_tree):
          temp = instruction_mat[i]
          endpoint = np.where(temp == 1)[0]
          if endpoint.size > 0:
              temp[:i] = -1
              temp[endpoint[-1] + 1 :] = -1
              instruction_mat[i] = temp
          else:
              instruction_mat[i] = -1
  
      # In the classical U-turn check, the writing is only at odd step and the
      # instruction follows squence A000120 (https://oeis.org/A000120)
      to_write_temp = np.sum(instruction_mat > -1, axis=0)
      write_instruction = to_write_temp - 1
      write_instruction[np.diag(instruction_mat) == -1] = max(
          to_write_temp
      )
  
      read_instruction = []
      for i in range(nsteps_within_tree):
          temp_instruction = instruction_mat[:, i]
          if np.sum(temp_instruction == 1) > 0:
              r = np.where(
                  temp_instruction[temp_instruction >= 0] == 1
              )[0][0]
              read_instruction.append(
                  [r, r + np.sum(temp_instruction == 1)]
              )
          else:
              # If there is no instruction to do U turn check (e.g., odd step in the
              # original U turn check scheme), we append a pair of 0s as instruction.
              # In the inner most while loop of build tree, we loop through the read
              # instruction to check U turn - looping from 0 to 0 works with the
              # existing code while no computation happens.
              read_instruction.append([0, 0])
      return write_instruction, np.asarray(read_instruction)
  
imports: import collections
  import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.experimental.mcmc import preconditioning_utils
  from tensorflow_probability.python.internal import broadcast_util
  from tensorflow_probability.python.internal import distribute_lib
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import samplers
  from tensorflow_probability.python.internal import tensorshape_util
  from tensorflow_probability.python.math.generic import log_add_exp
  from tensorflow_probability.python.mcmc.internal import leapfrog_integrator
  from tensorflow_probability.python.mcmc.internal import util
  from tensorflow_probability.python.mcmc.kernel import TransitionKernel
label_desc: Use image node_id 4 for calling a global function with example usage: generate_efficient_write_read_instruction(instruction_array) and returns: write_instruction,  np