class_docstr: Test the basic error handling / validation in pymil.
class_docstr_tok: ["Test","the","basic","error","handling","\/","validation","in","pymil","."]
docstr: The following program:
  
  constexpr -> transpose -> linear
  
  will not error out during the front end conversion, even though the weight of
  linear op needs to be const / constexpr directly.
  
  It is going to error out after all the optimization graph passes are finished,
  and transpose remains.
  
  However, if transpose can be removed, the conversion goes through.
docstr_tok: ["The","following","program",":","constexpr","-",">","transpose","-",">","linear","will","not","error","out","during","the","front","end","conversion",",","even","though","the","weight","of","linear","op","needs","to","be","const","\/","constexpr","directly",".","It","is","going","to","error","out","after","all","the","optimization","graph","passes","are","finished",",","and","transpose","remains",".","However",",","if","transpose","can","be","removed",",","the","conversion","goes","through","."]
code: def test_invalid_const_input_early_error_out():
      """
      The following program:
  
      constexpr -> transpose -> linear
  
      will not error out during the front end conversion, even though the weight of
      linear op needs to be const / constexpr directly.
  
      It is going to error out after all the optimization graph passes are finished,
      and transpose remains.
  
      However, if transpose can be removed, the conversion goes through.
      """
  
      # Test a simple constexpr op fed into linear
      @mb.program(input_specs=[mb.TensorSpec(shape=(2, 3))])
      def prog(x):
          constexpr = CONSTEXPR_FUNCS["constexpr_affine_dequantize"](
              (4, 3)
          )
          return mb.linear(x=x, weight=constexpr)
  
      for compute_precision in [
          ct.precision.FLOAT32,
          ct.precision.FLOAT16,
      ]:
          mlmodel = ct.convert(
              prog,
              convert_to="mlprogram",
              minimum_deployment_target=ct.target.iOS16,
              compute_units=ct.ComputeUnit.CPU_ONLY,
              compute_precision=compute_precision,
          )
  
      # Additional pattern (transpose) after constexpr will cause an early error out
      @mb.program(input_specs=[mb.TensorSpec(shape=(2, 3))])
      def prog(x):
          constexpr = CONSTEXPR_FUNCS["constexpr_affine_dequantize"](
              (3, 4)
          )
          constexpr = mb.transpose(x=constexpr, perm=[1, 0])
          return mb.linear(x=x, weight=constexpr)
  
      for compute_precision in [
          ct.precision.FLOAT32,
          ct.precision.FLOAT16,
      ]:
          with pytest.raises(
              ValueError, match="must be const or constexpr ops"
          ):
              mlmodel = ct.convert(
                  prog,
                  convert_to="mlprogram",
                  minimum_deployment_target=ct.target.iOS16,
                  compute_units=ct.ComputeUnit.CPU_ONLY,
                  compute_precision=compute_precision,
              )
  
      # If the transpose is removed by optimization passes, the conversion goes through
      @mb.program(input_specs=[mb.TensorSpec(shape=(2, 3))])
      def prog(x):
          constexpr = CONSTEXPR_FUNCS["constexpr_affine_dequantize"](
              (4, 3)
          )
          constexpr = mb.transpose(x=constexpr, perm=[0, 1])
          return mb.linear(x=x, weight=constexpr)
  
          mlmodel = ct.convert(
              prog,
              convert_to="mlprogram",
              minimum_deployment_target=ct.target.iOS16,
              compute_units=ct.ComputeUnit.CPU_ONLY,
              compute_precision=compute_precision,
          )
  
imports: import numpy
  import pytest
  import coremltools
  from coremltools import _logger
  from coremltools.converters.mil.mil import Builder
  from coremltools.converters.mil.mil import Function, Program, types
  from coremltools.converters.mil.mil.passes.tests.test_passes import CONSTEXPR_FUNCS
label_desc: Use image node_id 6 for calling the TestMILBasic obj's underlying member method code with example usage: obj.test_invalid_const_input_early_error_out() and returns: mb, mb, mb