class_docstr: Prune the weight with a constant sparsity percentile, which can be specified by either ``target_sparsity`` or ``n_m_ratio``.
  
  If ``target_sparsity`` is set, where ``n = floor(size_of_weight_tensor * target_sparsity)``, the ``n`` lowest
  absolute weight values are changed to ``0``. For example, given the following:
  
     * ``weight = [0.3, -0.2, -0.01, 0.05]``
     * ``target_sparsity = 0.75``
  
  The sparsified weight would be ``[0.3, 0, 0, 0]``.
  
  If ``block_size`` is set, then weights are pruned in a block structured manner; that is, chunks of weight values, as big as the ``block_size``, will be set to ``0``.
  Block sparsity can only be applied to ``linear`` and ``conv`` layers.
  For example:
  
      .. code-block:: python
  
          # Given a 4 x 2 weight with the following value, and block_size = 2, dim = 0.
          [
              [1, 3],
              [-6, -7],
              [0, 3],
              [-9, 2],
          ]
  
          # We first flatten the matrix along axis = 0.
          [1, -6, 0, -9, 3, -7, 3, 2]
  
          # For block size 2, the L2 norm will be compute of first 2 elements, then the second and 3rd element and so on.
          [6.08, 9.00, 7.62, 3.61]
  
          # Then the smallest values will be picked to prune. So if target_sparsity = 0.5, then the blocks that will be
          # pruned will be with ones with L2 norm value of 6.08 and 3.61. And hence, the elements in the first and third
          # block are pruned. Resulting in the following flatten pruned tensor:
          [0, 0, 0, -9, 3, -7, 0, 0]
  
          # The final pruned tensor is:
          [
              [0, 3],
              [0, -7],
              [0, 0],
              [-9, 0],
          ]
  
  The ``n_m_ratio`` triggers ``n:m`` pruning along the ``dim`` axis. In ``n:m`` pruning,
  out of every ``m`` elements, ``n`` with lowest magnitude are set to ``0``.
  For more information, see
  `Learning N:M Fine-Grained Structured Sparse Neural Networks From Scratch <https://arxiv.org/abs/2102.04010>`_.
  
  ``n:m`` pruning can be applied only to ``linear`` and ``conv`` layers.
  
  Example:
  
      .. code-block:: python
  
          # Given a 4 x 4 weight of
          [
              [3, 4, 7, 6],
              [1, 8, -3, -8],
              [-2, -3, -4, 0],
              [5, 4, -3, -2],
          ]
  
          # For n_m_ratio = (1, 2) with axis = 1 (default), the resulting pruned weight is
          [
              [0, 4, 7, 0],
              [0, 8, 0, -8],
              [0, -3, -4, 0],
              [5, 0, -3, 0],
          ]
  
          # For axis = 0, we get
          [
              [3, 0, 7, 0],
              [0, 8, 0, -8],
              [0, 0, -4, 0],
              [5, 4, 0, -2],
          ]
  
  Parameters
  ----------
  
  target_sparsity: float
      The percentage of sparsity for compression, which needs to be in the range ``[0, 1]``. When ``0``, no sparsification
      occurs. For ``1``, all weights become ``0``.
  
  block_size: int
      Block size for inducing block sparsity.
      This is applied on the ``dim`` dimension of the parameter.
      Having the zeros aligned in the parameter helps gain latency/memory performance on-device.
  
      * If set, must be greater than ``1`` to enable block sparsity.
      * Block sparsity can be applied only to ``linear`` and ``conv`` layers.
      * The channel will be padded with ``0`` if it is not divisible by ``block_size``.
  
  n_m_ratio: tuple[int]
      A tuple of two integers which specify the ratio for ``n:m`` pruning.
  
      * ``n`` must be smaller or equal to ``m``.
      * The channel would be padded with ``0`` if it is not divisible by ``m``.
  
  dim: int
      Dimension where the block sparsity or ``n:m`` sparsity is applied.
  
      * Must be either ``0`` or ``1``.
      * The default value for block sparsity is ``0`` (output channel).
      * The default value for ``n:m`` sparsity is ``1`` (input channel).
  
  weight_threshold: int
      The size threshold, above which weights are pruned.
      That is, a weight tensor is pruned only if its total number of elements is greater than ``weight_threshold``.
  
      For example, if ``weight_threshold = 1024`` and a weight tensor is of shape ``[10, 20, 1, 1]``, hence ``200``
      elements, it will not be pruned.
  
      * If not provided, it will be set to ``2048``, in which weights bigger than ``2048`` elements are compressed.
class_docstr_tok: ["Prune","the","weight","with","a","constant","sparsity","percentile",",","which","can","be","specified","by","either","``","target_sparsity","``","or","``","n_m_ratio","``",".","If","``","target_sparsity","``","is","set",",","where","``","n","=","floor","(","size_of_weight_tensor","*","target_sparsity",")","``",",","the","``","n","``","lowest","absolute","weight","values","are","changed","to","``","0","``",".","For","example",",","given","the","following",":","*","``","weight","=","[","0.3",",","-0.2",",","-0.01",",","0.05","]","``","*","``","target_sparsity","=","0.75","``","The","sparsified","weight","would","be","``","[","0.3",",","0",",","0",",","0","]","``",".","If","``","block_size","``","is","set",",","then","weights","are","pruned","in","a","block","structured","manner",";","that","is",",","chunks","of","weight","values",",","as","big","as","the","``","block_size","``",",","will","be","set","to","``","0","``",".","Block","sparsity","can","only","be","applied","to","``","linear","``","and","``","conv","``","layers",".","For","example",":","..","code-block",":",":","python","#","Given","a","4","x","2","weight","with","the","following","value",",","and","block_size","=","2",",","dim","=","0",".","[","[","1",",","3","]",",","[","-6",",","-7","]",",","[","0",",","3","]",",","[","-9",",","2","]",",","]","#","We","first","flatten","the","matrix","along","axis","=","0",".","[","1",",","-6",",","0",",","-9",",","3",",","-7",",","3",",","2","]","#","For","block","size","2",",","the","L2","norm","will","be","compute","of","first","2","elements",",","then","the","second","and","3rd","element","and","so","on",".","[","6.08",",","9.00",",","7.62",",","3.61","]","#","Then","the","smallest","values","will","be","picked","to","prune",".","So","if","target_sparsity","=","0.5",",","then","the","blocks","that","will","be","#","pruned","will","be","with","ones","with","L2","norm","value","of","6.08","and","3.61",".","And","hence",",","the","elements","in","the","first","and","third","#","block","are","pruned",".","Resulting","in","the","following","flatten","pruned","tensor",":","[","0",",","0",",","0",",","-9",",","3",",","-7",",","0",",","0","]","#","The","final","pruned","tensor","is",":","[","[","0",",","3","]",",","[","0",",","-7","]",",","[","0",",","0","]",",","[","-9",",","0","]",",","]","The","``","n_m_ratio","``","triggers","``","n",":","m","``","pruning","along","the","``","dim","``","axis",".","In","``","n",":","m","``","pruning",",","out","of","every","``","m","``","elements",",","``","n","``","with","lowest","magnitude","are","set","to","``","0","``",".","For","more","information",",","see","`","Learning","N",":","M","Fine-Grained","Structured","Sparse","Neural","Networks","From","Scratch","<","https",":","\/\/arxiv.org\/abs\/2102.04010",">","`","_",".","``","n",":","m","``","pruning","can","be","applied","only","to","``","linear","``","and","``","conv","``","layers",".","Example",":","..","code-block",":",":","python","#","Given","a","4","x","4","weight","of","[","[","3",",","4",",","7",",","6","]",",","[","1",",","8",",","-3",",","-8","]",",","[","-2",",","-3",",","-4",",","0","]",",","[","5",",","4",",","-3",",","-2","]",",","]","#","For","n_m_ratio","=","(","1",",","2",")","with","axis","=","1","(","default",")",",","the","resulting","pruned","weight","is","[","[","0",",","4",",","7",",","0","]",",","[","0",",","8",",","0",",","-8","]",",","[","0",",","-3",",","-4",",","0","]",",","[","5",",","0",",","-3",",","0","]",",","]","#","For","axis","=","0",",","we","get","[","[","3",",","0",",","7",",","0","]",",","[","0",",","8",",","0",",","-8","]",",","[","0",",","0",",","-4",",","0","]",",","[","5",",","4",",","0",",","-2","]",",","]","Parameters","--","--","--","--","--","target_sparsity",":","float","The","percentage","of","sparsity","for","compression",",","which","needs","to","be","in","the","range","``","[","0",",","1","]","``",".","When","``","0","``",",","no","sparsification","occurs",".","For","``","1","``",",","all","weights","become","``","0","``",".","block_size",":","int","Block","size","for","inducing","block","sparsity",".","This","is","applied","on","the","``","dim","``","dimension","of","the","parameter",".","Having","the","zeros","aligned","in","the","parameter","helps","gain","latency\/memory","performance","on-device",".","*","If","set",",","must","be","greater","than","``","1","``","to","enable","block","sparsity",".","*","Block","sparsity","can","be","applied","only","to","``","linear","``","and","``","conv","``","layers",".","*","The","channel","will","be","padded","with","``","0","``","if","it","is","not","divisible","by","``","block_size","``",".","n_m_ratio",":","tuple","[","int","]","A","tuple","of","two","integers","which","specify","the","ratio","for","``","n",":","m","``","pruning",".","*","``","n","``","must","be","smaller","or","equal","to","``","m","``",".","*","The","channel","would","be","padded","with","``","0","``","if","it","is","not","divisible","by","``","m","``",".","dim",":","int","Dimension","where","the","block","sparsity","or","``","n",":","m","``","sparsity","is","applied",".","*","Must","be","either","``","0","``","or","``","1","``",".","*","The","default","value","for","block","sparsity","is","``","0","``","(","output","channel",")",".","*","The","default","value","for","``","n",":","m","``","sparsity","is","``","1","``","(","input","channel",")",".","weight_threshold",":","int","The","size","threshold",",","above","which","weights","are","pruned",".","That","is",",","a","weight","tensor","is","pruned","only","if","its","total","number","of","elements","is","greater","than","``","weight_threshold","``",".","For","example",",","if","``","weight_threshold","=","1024","``","and","a","weight","tensor","is","of","shape","``","[","10",",","20",",","1",",","1","]","``",",","hence","``","200","``","elements",",","it","will","not","be","pruned",".","*","If","not","provided",",","it","will","be","set","to","``","2048","``",",","in","which","weights","bigger","than","``","2048","``","elements","are","compressed","."]
code: def check_dim(self, attr, dim):
      if dim is None:
          return
      if self.block_size is None and self.n_m_ratio is None:
          raise ValueError(
              '"dim" can only be set along with "block_size" or "n_m_ratio".'
          )
      if dim not in [0, 1]:
          raise ValueError(f'"dim" must be 1 or 0. Got {dim}.')
  
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 7 for calling the OpMagnitudePrunerConfig obj's underlying member method code with example usage: obj.check_dim(attr, dim) without return types