class_docstr: Base class for all sparsifiers.
  
  Abstract methods that need to be implemented:
  
  - update_mask: Function to compute a new mask for all keys in the
      `groups`.
  
  Args:
      - model [nn.Module]: model to configure. The model itself is not saved
          but used for the state_dict saving / loading.
      - config [list]: configuration elements should be a dict map that includes
          `tensor_fqn` of tensors to sparsify
      - defaults [dict]: default configurations will be attached to the
          configuration. Only the keys that don't exist in the `config` will
          be updated.
  
  Example::
  
      >>> # xdoctest: +SKIP("Can't instantiate abstract class BaseSparsifier with abstract method update_mask")
      >>> config = [{'tensor_fqn': 'layer1.weight', 'tensor_fqn': 'linear2.weight2', 'sparsity_level': 0.5}]
      >>> defaults = {'sparsity_level': 0.7}
      >>> # model.layer1.weight will have `sparsity_level` = 0.7 (getting default)
      >>> sparsifier = BaseSparsifier(config, defaults)
class_docstr_tok: ["Base","class","for","all","sparsifiers",".","Abstract","methods","that","need","to","be","implemented",":","-","update_mask",":","Function","to","compute","a","new","mask","for","all","keys","in","the","`","groups","`",".","Args",":","-","model","[","nn.Module","]",":","model","to","configure",".","The","model","itself","is","not","saved","but","used","for","the","state_dict","saving","\/","loading",".","-","config","[","list","]",":","configuration","elements","should","be","a","dict","map","that","includes","`","tensor_fqn","`","of","tensors","to","sparsify","-","defaults","[","dict","]",":","default","configurations","will","be","attached","to","the","configuration",".","Only","the","keys","that","do","n't","exist","in","the","`","config","`","will","be","updated",".","Example",":",":",">",">",">","#","xdoctest",":","+SKIP","(","``","Ca","n't","instantiate","abstract","class","BaseSparsifier","with","abstract","method","update_mask","''",")",">",">",">","config","=","[","{","'tensor_fqn","'",":","'layer1.weight","'",",","'tensor_fqn","'",":","'linear2.weight2","'",",","'sparsity_level","'",":","0.5","}","]",">",">",">","defaults","=","{","'sparsity_level","'",":","0.7","}",">",">",">","#","model.layer1.weight","will","have","`","sparsity_level","`","=","0.7","(","getting","default",")",">",">",">","sparsifier","=","BaseSparsifier","(","config",",","defaults",")"]
docstr: Prepares a model, by adding the parametrizations.
  
  Note::
  
      The model is modified inplace. If you need to preserve the original
      model, use copy.deepcopy.
docstr_tok: ["Prepares","a","model",",","by","adding","the","parametrizations",".","Note",":",":","The","model","is","modified","inplace",".","If","you","need","to","preserve","the","original","model",",","use","copy.deepcopy","."]
code: def prepare(self, model, config):
      r"""Prepares a model, by adding the parametrizations.
  
      Note::
  
          The model is modified inplace. If you need to preserve the original
          model, use copy.deepcopy.
      """
      self.model = (
          model  # TODO: Need to figure out how to load without this.
      )
      self.config = config
  
      # If no config -- try getting all the supported layers
      if self.config is None:
          self.make_config_from_model(model)
  
      # TODO: Remove the configuration by reference ('module')
      for module_config in self.config:
          assert isinstance(module_config, dict), (
              "config elements should be dicts not modules i.e.:"
              "[{`tensor_fqn`: `foo.bar.weight`}, {`tensor_fqn`: ... }, ...]"
          )
  
          assert isinstance(self.defaults, Dict)  # for mypy
          local_args = copy.deepcopy(self.defaults)
          local_args.update(module_config)
  
          tensor_fqn = local_args.get("tensor_fqn", None)
          assert tensor_fqn is not None, (
              "tensor_fqn is a required argument in the sparsity config which"
              "replaces previous `module` and [module]`fqn` arguments"
          )
  
          # populate all information from tensor_fqn
          info_from_tensor_fqn = get_arg_info_from_tensor_fqn(
              model, tensor_fqn
          )
  
          # check that whatever was put into local_args agrees with what was obtained
          # from tensor_fqn
          for key in info_from_tensor_fqn.keys():
              if key in local_args:
                  assert (
                      info_from_tensor_fqn[key] == local_args[key]
                      or (
                          key == "tensor_fqn"
                          and "." + info_from_tensor_fqn[key]
                          == local_args[key]
                      )
                      # info_from_tensor_fqn will chop leading '.' from tensor_fqn so ignore that
                  ), f"Given both `{key}` and `tensor_fqn` in the config, it is expected them to agree!"
          local_args.update(info_from_tensor_fqn)
          self.groups.append(local_args)
      self._prepare()
  
imports: import abc
  import copy
  from collections import defaultdict
  from typing import Any, Dict, Optional, Set, Tuple, List, Type
  import torch
  from torch import nn
  from torch.nn.utils import parametrize
  from torch.nn.utils.parametrize import type_before_parametrizations
  from .utils import module_contains_param, swap_module, FakeSparsity, get_arg_info_from_tensor_fqn, module_to_fqn
label_desc: Use image node_id 8 for calling the BaseSparsifier obj's underlying member method code with example usage: obj.prepare(model, config) without return types