code: def type_of_pattern(val, backend, preferred_type=None):
      """Returns the `instructions.Type` of `val`.
  
      Args:
        val: Pattern of backend-specific `Tensor`s or a Python or numpy constant.
        backend: Object implementing required backend operations.
        preferred_type: `instructions.Type` to prefer, if `t` is a constant.
  
      Returns:
        vm_type: Pattern of `instructions.TensorType` describing `t`
      """
  
      def at_leaf(preferred_leaf_type, obj):
          """Pattern match at a leaf of the preferred_type pattern."""
          if preferred_leaf_type is None:
              return instructions.pattern_map(backend.type_of, obj)
          if isinstance(preferred_leaf_type, instructions.TensorType):
              return backend.type_of(obj, preferred_leaf_type.dtype)
          # Otherwise, preferred_leaf_type must be a (nested) list or tuple of
          # TensorType, while obj is not a list or a tuple (of anything).  In this
          # case, pattern_map2 should have raised an error, but we can defensively
          # raise an error here as well.
          msg = "Type mismatch: Expected structured type {}, got object {}.".format(
              preferred_leaf_type, obj
          )
          raise ValueError(msg)
  
      if preferred_type is None:
          preferred_type = instructions.Type(None)
      return instructions.pattern_map2(
          at_leaf,
          preferred_type.tensors,
          val,
          leaf_type=instructions.TensorType,
      )
  
file_path: probability/tensorflow_probability/python/experimental/auto_batching/type_inference.py
imports: import contextlib
  import functools
  from absl import logging
  import six
  from tensorflow_probability.python.experimental.auto_batching import instructions
label_desc: Use image node_id 5 for calling a global function with example usage: type_of_pattern(val, backend, preferred_type) and returns: instructions, instructions, backend