class_docstr: A configuration wrapper that enables fine-grained control when compressing a model,
  Providing the following levels: `global`, `op type`, and `op name`.
  
  1. ``global_config``: The default configuration applied to all ops / consts.
  2. ``op_type_configs``: Configurations applied to specific op type. It overrides ``global_config``.
  3. ``op_name_configs``: Configurations applied to specific constant or op instance. It overrides ``global_config`` and ``op_type_configs``.
  
  The following is an example that constructs an optimization config for weight palettization.
  
      .. code-block:: python
  
          from coremltools.optimize.coreml import OpPalettizerConfig, OptimizationConfig
  
          # The default global configuration is 8 bits palettization with kmeans
          global_config = OpPalettizerConfig(mode="kmeans", nbits=8)
  
          # We use 2 bits palettization for convolution layers, and skip the compression for linear layers
          op_type_configs = {
              "conv": OpPalettizerConfig(mode="kmeans", nbits=2),
              "linear": None,
          }
  
          # We want a convolution layer named "conv_1" to have a 4 bits palettization with a different mode
          op_name_configs = {
              "conv_1": OpPalettizerConfig(mode="uniform", nbits=4),
          }
  
          # Now we can put all configuration across three levels to construct an OptimizationConfig object
          config = OptimizationConfig(
              global_config=global_config,
              op_type_configs=op_type_configs,
              op_name_configs=op_name_configs,
          )
  
  
  Parameters
  ----------
  
  global_config: OpCompressorConfig
      Config to be applied globally to all supported ops.
  
  op_type_configs: dict[str, OpCompressorConfig]
      Op type level configs applied to a specific op class.
  
      * The keys of the dictionary are the string of the op type, and the values are the corresponding :py:class:`OpCompressorConfig`.
      * An op type will not be compressed if the value is set to ``None``.
  
  op_name_configs: dict[str, OpCompressorConfig]
      Op instance level configs applied to a specific constant or op.
  
      * The keys of the dictionary are the name of a constant or an op instance, and the values are the corresponding :py:class:`OpCompressorConfig`.
      * An op instance will not be compressed if the value is set to ``None``.
      * You can use ``coremltools.optimize.coreml.get_weights_metadata`` to get the name of the constants / op instances in the model.
class_docstr_tok: ["A","configuration","wrapper","that","enables","fine-grained","control","when","compressing","a","model",",","Providing","the","following","levels",":","`","global","`",",","`","op","type","`",",","and","`","op","name","`",".","1",".","``","global_config","``",":","The","default","configuration","applied","to","all","ops","\/","consts",".","2",".","``","op_type_configs","``",":","Configurations","applied","to","specific","op","type",".","It","overrides","``","global_config","``",".","3",".","``","op_name_configs","``",":","Configurations","applied","to","specific","constant","or","op","instance",".","It","overrides","``","global_config","``","and","``","op_type_configs","``",".","The","following","is","an","example","that","constructs","an","optimization","config","for","weight","palettization",".","..","code-block",":",":","python","from","coremltools.optimize.coreml","import","OpPalettizerConfig",",","OptimizationConfig","#","The","default","global","configuration","is","8","bits","palettization","with","kmeans","global_config","=","OpPalettizerConfig","(","mode=","''","kmeans","''",",","nbits=8",")","#","We","use","2","bits","palettization","for","convolution","layers",",","and","skip","the","compression","for","linear","layers","op_type_configs","=","{","``","conv","''",":","OpPalettizerConfig","(","mode=","''","kmeans","''",",","nbits=2",")",",","``","linear","''",":","None",",","}","#","We","want","a","convolution","layer","named","``","conv_1","''","to","have","a","4","bits","palettization","with","a","different","mode","op_name_configs","=","{","``","conv_1","''",":","OpPalettizerConfig","(","mode=","''","uniform","''",",","nbits=4",")",",","}","#","Now","we","can","put","all","configuration","across","three","levels","to","construct","an","OptimizationConfig","object","config","=","OptimizationConfig","(","global_config=global_config",",","op_type_configs=op_type_configs",",","op_name_configs=op_name_configs",",",")","Parameters","--","--","--","--","--","global_config",":","OpCompressorConfig","Config","to","be","applied","globally","to","all","supported","ops",".","op_type_configs",":","dict","[","str",",","OpCompressorConfig","]","Op","type","level","configs","applied","to","a","specific","op","class",".","*","The","keys","of","the","dictionary","are","the","string","of","the","op","type",",","and","the","values","are","the","corresponding",":","py",":","class",":","`","OpCompressorConfig","`",".","*","An","op","type","will","not","be","compressed","if","the","value","is","set","to","``","None","``",".","op_name_configs",":","dict","[","str",",","OpCompressorConfig","]","Op","instance","level","configs","applied","to","a","specific","constant","or","op",".","*","The","keys","of","the","dictionary","are","the","name","of","a","constant","or","an","op","instance",",","and","the","values","are","the","corresponding",":","py",":","class",":","`","OpCompressorConfig","`",".","*","An","op","instance","will","not","be","compressed","if","the","value","is","set","to","``","None","``",".","*","You","can","use","``","coremltools.optimize.coreml.get_weights_metadata","``","to","get","the","name","of","the","constants","\/","op","instances","in","the","model","."]
docstr: Construct an ``OptimizationConfig`` instance from a nested dictionary.
  The dictionary should have the structure that only contains (if any) the following four ``str`` keys:
  
  * ``"config_type"``: Specify the configuration class type.
  * ``"global_config"``: Parameters for ``global_config``.
  * ``"op_type_configs"``: A nested dictionary for ``op_type_configs``.
  * ``"op_name_config"``: A nested dictionary for ``op_name_configs``.
  
  The following is a nested dictionary that creates an optimization config for weight palettization:
  
  .. code-block:: python
  
      config_dict = {
          "config_type": "OpPalettizerConfig",
          "global_config": {
              "mode": "kmeans",
              "nbits": 4,
          },
          "op_type_configs": {
              "conv": {
                  "mode": "uniform",
                  "nbits": 1,
              }
          },
          "op_name_configs": {
              "conv_1": {
                  "mode": "unique",
              }
          },
      }
  
  Note that you can override the ``config_type``. For instance, if you want to do threshold-based
  pruning to the model in addition to the convolution layers in which magnitude pruning is applied, the following is an
  example of the nested dictionary:
  
  .. code-block:: python
  
      config_dict = {
          "config_type": "OpThresholdPrunerConfig",
          "global_config": {
              "threshold": 0.01,
          },
          "op_type_configs": {
              "conv": {
                  "config_type": "OpMagnitudePrunerConfig",
                  "n_m_ratio": [3, 4],
              }
          },
      }
  
  Parameters
  ----------
  
  config_dict: dict[str, Any]
      A dictionary that represents the configuration structure.
code: def from_dict(
      cls, config_dict: Dict[str, Any]
  ) -> "OptimizationConfig":
      """
      Construct an ``OptimizationConfig`` instance from a nested dictionary.
      The dictionary should have the structure that only contains (if any) the following four ``str`` keys:
  
      * ``"config_type"``: Specify the configuration class type.
      * ``"global_config"``: Parameters for ``global_config``.
      * ``"op_type_configs"``: A nested dictionary for ``op_type_configs``.
      * ``"op_name_config"``: A nested dictionary for ``op_name_configs``.
  
      The following is a nested dictionary that creates an optimization config for weight palettization:
  
      .. code-block:: python
  
          config_dict = {
              "config_type": "OpPalettizerConfig",
              "global_config": {
                  "mode": "kmeans",
                  "nbits": 4,
              },
              "op_type_configs": {
                  "conv": {
                      "mode": "uniform",
                      "nbits": 1,
                  }
              },
              "op_name_configs": {
                  "conv_1": {
                      "mode": "unique",
                  }
              },
          }
  
      Note that you can override the ``config_type``. For instance, if you want to do threshold-based
      pruning to the model in addition to the convolution layers in which magnitude pruning is applied, the following is an
      example of the nested dictionary:
  
      .. code-block:: python
  
          config_dict = {
              "config_type": "OpThresholdPrunerConfig",
              "global_config": {
                  "threshold": 0.01,
              },
              "op_type_configs": {
                  "conv": {
                      "config_type": "OpMagnitudePrunerConfig",
                      "n_m_ratio": [3, 4],
                  }
              },
          }
  
      Parameters
      ----------
  
      config_dict: dict[str, Any]
          A dictionary that represents the configuration structure.
      """
  
      def _get_cls_instance(cls_type, cls_attrs):
          if cls_attrs is None:
              return None
          converter = cattrs.Converter(forbid_extra_keys=True)
          if "config_type" in cls_attrs:
              cls_type = cls_attrs["config_type"]
              del cls_attrs["config_type"]
          class_type = getattr(sys.modules[__name__], cls_type)
          return class_type._from_dict(cls_attrs)
  
      def _check_config_dict(config_dict):
          valid_keys = (
              "config_type",
              "global_config",
              "op_name_configs",
              "op_type_configs",
          )
          for k in config_dict:
              if k not in valid_keys:
                  raise ValueError(
                      f"Invalid key {k} to construct an OptimizationConfig object. Supported keys are {valid_keys}."
                  )
  
      _check_config_dict(config_dict)
  
      config_type = config_dict.get("config_type", None)
      if config_type is None or not isinstance(config_type, str):
          raise ValueError(
              "config_type must be provided with type of string."
          )
  
      cls_attrs = {}
      if config_dict.get("global_config", None) is not None:
          cls_attrs["global_config"] = _get_cls_instance(
              config_type, config_dict["global_config"]
          )
      for key in ["op_type_configs", "op_name_configs"]:
          if config_dict.get(key, None) is None:
              continue
          if not isinstance(config_dict[key], dict):
              raise ValueError(
                  f"{key} must be type of dict. Got {type(config_dict[key])}"
              )
          cls_attrs[key] = {
              k: _get_cls_instance(config_type, v)
              for k, v in config_dict[key].items()
          }
  
      return cls(**cls_attrs)
  
imports: import sys
  from abc import ABC, abstractmethod
  from collections import OrderedDict
  from typing import IO, Any, Callable, Dict, Optional, Tuple, Union
  import cattrs
  import numpy
  import yaml
  from attrs import define, field, validators
  from coremltools.converters.mil.mil import Operation, types
  from coremltools.converters.mil.mil.types.type_mapping import is_builtin, numpy_type_to_builtin_type
label_desc: Use image node_id 12 for calling the OptimizationConfig obj's underlying member method code with example usage: obj.from_dict(cls, config_dict) and returns: cls, class_type, None