code: def from_concrete_function(
      concrete_fn,
      specialized_flat_specs: Optional[
          List[tensor_spec.TensorSpec]
      ] = None,
  ):
      """Generate the Compiler Ir from tf concrete function with TensorSpec.
  
      Args:
        concrete_fn: returned by using get_concrete_function.
        specialized_flat_specs: specialized flat tf.TensorSpecs for function args.
  
      Returns:
        Function callable that generate the HLO text.
  
      Raises:
          ValueError: if concrete_fn is not "compilable" without concrete
          inputs.
      """
      context.ensure_initialized()
      fn_name = concrete_fn.name
      filtered_flat_specs = specialized_flat_specs or list(
          nest.flatten(concrete_fn.structured_input_signature)
      )
  
      if not all(
          s.shape.is_fully_defined() for s in filtered_flat_specs
      ):
          raise ValueError(
              f"Only support static input shape but got inputs = {concrete_fn.inputs}"
          )
  
      def compiler_ir_generator(
          stage="hlo", device_name=None, platform_name=None
      ):
          """Gets the compiler IR bytes.
  
          Args:
            stage: The exported stage for the given function.
            device_name: The name of the device with the form as
              "/job:localhost/replica:0/task:0/device:CPU:0", "/device:TPU:0" etc.
              When this is used, actual device is needed for getting the compiler IR.
            platform_name: The name of the platform, e.g. "TPU". When this is used, no
              actual device is needed but the compiler IR is obtained as if using that
              device. The scenarios supported are more limited.
  
          Returns:
            The compiler IR bytes.
          """
          if device_name is not None:
              if platform_name is not None:
                  raise ValueError(
                      "device_name and platform_name cannot be provided at the same time."
                  )
              warnings.warn(
                  "device_name is being deprecated. Use platform_name."
              )
          device_name = maybe_get_device_name(device_name)
          res_bytes = context.context().get_compiler_ir(
              device_name=device_name,
              platform_name=platform_name,
              function_name=fn_name,
              flat_args=filtered_flat_specs,
              captured_inputs=concrete_fn.captured_inputs,
              stage=stage,
          )
          if stage in (
              "hlo_serialized",
              "optimized_hlo_serialized",
              "optimized_hlo_proto_serialized",
          ):
              return res_bytes
          else:
              return res_bytes.decode("utf-8")
  
      return compiler_ir_generator
  
file_path: tensorflow-upstream/tensorflow/python/eager/polymorphic_function/compiler_ir.py
imports: from typing import List, Optional
  import warnings
  from tensorflow.core.function import trace_type
  from tensorflow.python.eager import context
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import tensor_spec
  from tensorflow.python.ops import random_ops
  from tensorflow.python.util import nest
label_desc: Use image node_id 3 for calling a global function with example usage: from_concrete_function(concrete_fn, specialized_flat_specs) and returns: compiler_ir_generator, res_bytes, res_bytes