code: def cast_tensor_by_spec(
      _input: tf_ext.TensorLike, spec: tf_ext.TypeSpec
  ) -> tf_ext.TensorLike:
      """
      transform dtype & shape following spec
      """
      if not LazyType["tf_ext.TensorSpec"](
          "tensorflow.python.framework.tensor_spec.TensorSpec"
      ).isinstance(spec):
          return _input
  
      if LazyType["tf_ext.CastableTensorType"]("tf.Tensor").isinstance(
          _input
      ) or LazyType["tf_ext.CastableTensorType"](
          "tensorflow.python.framework.ops.EagerTensor"
      ).isinstance(
          _input
      ):
          # TensorFlow Issues #43038
          # pylint: disable=unexpected-keyword-arg, no-value-for-parameter
          return t.cast(
              "tf_ext.TensorLike",
              tf.cast(
                  _input, dtype=spec.dtype, name=t.cast(str, spec.name)
              ),
          )
      else:
          return t.cast(
              "tf_ext.TensorLike",
              tf.constant(
                  _input, dtype=spec.dtype, name=t.cast(str, spec.name)
              ),
          )
  
imports: from __future__ import annotations
  import importlib.metadata
  import importlib.util
  import logging
  import typing
  from typing import TYPE_CHECKING
  from bentoml.exceptions import BentoMLException
  from ...types import LazyType
  from ...utils.lazy_loader import LazyLoader
label_desc: Use image node_id 18 for calling a global function with example usage: cast_tensor_by_spec(_input, spec) and returns: _input, t, t