class_docstr: Extension of PyTorch's native pruning infra for seamless
  model export and progressive sparsity schedules
  
  This class works by registering itself as a forward pre-hook
  into each prune-able `nn.Module` to apply the pruning mask
code: def _remove_impl(
      self, module: _torch.nn.Module, fuse_pruning_mask: bool
  ) -> None:
      assert self._tensor_name is not None
  
      # Restore the (pruned) tensor under its original name
      orig = module._parameters[self._tensor_name + "_orig"]
      assert orig is not None
  
      if fuse_pruning_mask:
          pruned_orig = None
          if self.scheduled:
              current_mask = module._buffers[
                  self._tensor_name + "_mask"
              ]
              assert current_mask is not None
              current_amount = self.infer_sparsity_amount_from_external_mask(
                  current_mask
              )  # may have been loaded from ckpt and current_amount != self.amount:  # self.amount may be
              # out-of-sync with the ckpt
  
              if hasattr(self, "amount") and not _np.isclose(
                  current_amount, self.amount, rtol=1 / orig.numel()
              ):
                  _logger.warning(
                      f"Pruning method {self.__class__}'s sparsity schedule state ({self.amount}) is inconsistent "
                      f"with pruning mask's current state ({current_amount}). This is probably harmless "
                      f"if you are exporting a pruned model"
                  )
                  # We have detected an inconsistent state so we correct for this by updating the
                  # pruning method's schedule. This correction will ensure the following `self._apply_mask_impl`
                  # call to use the correct self.amount
                  self.update_mask(module, current_amount)
                  pruned_orig = current_mask.to(orig.dtype) * orig
  
          if pruned_orig is None:
              pruned_orig = self._apply_mask_impl(module)
  
          orig.data = pruned_orig.data
  
      setattr(module, self._tensor_name, orig)
      del module._parameters[self._tensor_name + "_orig"]
      del module._buffers[self._tensor_name + "_mask"]
  
imports: import logging
  import types
  from typing import Any
  from typing import Dict
  from typing import NamedTuple
  from typing import Optional
  from typing import cast
  import numpy
  import torch
  import torch.nn.utils.prune
  import torch.utils.hooks
  from coremltools.optimize.torch._typing import ParamsDict
  from coremltools.optimize.torch._utils.state_dict_utils import LoadStateDictPostHook
  from coremltools.optimize.torch.pruning._utils import block2_sparsity
  from coremltools.optimize.torch.pruning._utils import structured_sparsity
  from coremltools.optimize.torch.pruning._utils import unstructured_sparsity
label_desc: Use image node_id 4 for calling the BaseDynamicPruningMethod obj's underlying member method code with example usage: obj._remove_impl(module, fuse_pruning_mask) without return types