class_docstr: This function prune the model based on the sensitivity
  for each layer.
  
  Parameters
  ----------
  model: torch.nn.Module
      model to be compressed
  evaluator: function
      validation function for the model. This function should return the accuracy
      of the validation dataset. The input parameters of evaluator can be specified
      in the parameter `eval_args` and 'eval_kwargs' of the compress function if needed.
      Example:
      >>> def evaluator(model):
      >>>     device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
      >>>     val_loader = ...
      >>>     model.eval()
      >>>     correct = 0
      >>>     with torch.no_grad():
      >>>         for data, target in val_loader:
      >>>             data, target = data.to(device), target.to(device)
      >>>             output = model(data)
      >>>             # get the index of the max log-probability
      >>>             pred = output.argmax(dim=1, keepdim=True)
      >>>             correct += pred.eq(target.view_as(pred)).sum().item()
      >>>     accuracy = correct / len(val_loader.dataset)
      >>>     return accuracy
  finetuner: function
      finetune function for the model. This parameter is not essential, if is not None,
      the sensitivity pruner will finetune the model after pruning in each iteration.
      The input parameters of finetuner can be specified in the parameter of compress
      called `finetune_args` and `finetune_kwargs` if needed.
      Example:
      >>> def finetuner(model, epoch=3):
      >>>     device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
      >>>     train_loader = ...
      >>>     criterion = torch.nn.CrossEntropyLoss()
      >>>     optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
      >>>     model.train()
      >>>     for _ in range(epoch):
      >>>         for _, (data, target) in enumerate(train_loader):
      >>>             data, target = data.to(device), target.to(device)
      >>>             optimizer.zero_grad()
      >>>             output = model(data)
      >>>             loss = criterion(output, target)
      >>>             loss.backward()
      >>>             optimizer.step()
  base_algo: str
      base pruning algorithm. `level`, `l1`, `l2` or `fpgm`, by default `l1`.
  sparsity_proportion_calc: function
      This function generate the sparsity proportion between the conv layers according to the
      sensitivity analysis results. We provide a default function to quantify the sparsity
      proportion according to the sensitivity analysis results. Users can also customize
      this function according to their needs. The input of this function is a dict,
      for example : {'conv1' : {0.1: 0.9, 0.2 : 0.8}, 'conv2' : {0.1: 0.9, 0.2 : 0.8}},
      in which, 'conv1' and is the name of the conv layer, and 0.1:0.9 means when the
      sparsity of conv1 is 0.1 (10%), the model's val accuracy equals to 0.9.
  sparsity_per_iter: float
      The sparsity of the model that the pruner try to prune in each iteration.
  acc_drop_threshold : float
      The hyperparameter used to quantifiy the sensitivity for each layer.
  checkpoint_dir: str
      The dir path to save the checkpoints during the pruning.
class_docstr_tok: ["This","function","prune","the","model","based","on","the","sensitivity","for","each","layer",".","Parameters","--","--","--","--","--","model",":","torch.nn.Module","model","to","be","compressed","evaluator",":","function","validation","function","for","the","model",".","This","function","should","return","the","accuracy","of","the","validation","dataset",".","The","input","parameters","of","evaluator","can","be","specified","in","the","parameter","`","eval_args","`","and","'eval_kwargs","'","of","the","compress","function","if","needed",".","Example",":",">",">",">","def","evaluator","(","model",")",":",">",">",">","device","=","torch.device","(","``","cuda","''","if","torch.cuda.is_available","(",")","else","``","cpu","''",")",">",">",">","val_loader","=","...",">",">",">","model.eval","(",")",">",">",">","correct","=","0",">",">",">","with","torch.no_grad","(",")",":",">",">",">","for","data",",","target","in","val_loader",":",">",">",">","data",",","target","=","data.to","(","device",")",",","target.to","(","device",")",">",">",">","output","=","model","(","data",")",">",">",">","#","get","the","index","of","the","max","log-probability",">",">",">","pred","=","output.argmax","(","dim=1",",","keepdim=True",")",">",">",">","correct","+=","pred.eq","(","target.view_as","(","pred",")",")",".sum","(",")",".item","(",")",">",">",">","accuracy","=","correct","\/","len","(","val_loader.dataset",")",">",">",">","return","accuracy","finetuner",":","function","finetune","function","for","the","model",".","This","parameter","is","not","essential",",","if","is","not","None",",","the","sensitivity","pruner","will","finetune","the","model","after","pruning","in","each","iteration",".","The","input","parameters","of","finetuner","can","be","specified","in","the","parameter","of","compress","called","`","finetune_args","`","and","`","finetune_kwargs","`","if","needed",".","Example",":",">",">",">","def","finetuner","(","model",",","epoch=3",")",":",">",">",">","device","=","torch.device","(","``","cuda","''","if","torch.cuda.is_available","(",")","else","``","cpu","''",")",">",">",">","train_loader","=","...",">",">",">","criterion","=","torch.nn.CrossEntropyLoss","(",")",">",">",">","optimizer","=","torch.optim.SGD","(","model.parameters","(",")",",","lr=0.01",")",">",">",">","model.train","(",")",">",">",">","for","_","in","range","(","epoch",")",":",">",">",">","for","_",",","(","data",",","target",")","in","enumerate","(","train_loader",")",":",">",">",">","data",",","target","=","data.to","(","device",")",",","target.to","(","device",")",">",">",">","optimizer.zero_grad","(",")",">",">",">","output","=","model","(","data",")",">",">",">","loss","=","criterion","(","output",",","target",")",">",">",">","loss.backward","(",")",">",">",">","optimizer.step","(",")","base_algo",":","str","base","pruning","algorithm",".","`","level","`",",","`","l1","`",",","`","l2","`","or","`","fpgm","`",",","by","default","`","l1","`",".","sparsity_proportion_calc",":","function","This","function","generate","the","sparsity","proportion","between","the","conv","layers","according","to","the","sensitivity","analysis","results",".","We","provide","a","default","function","to","quantify","the","sparsity","proportion","according","to","the","sensitivity","analysis","results",".","Users","can","also","customize","this","function","according","to","their","needs",".","The","input","of","this","function","is","a","dict",",","for","example",":","{","'conv1","'",":","{","0.1",":","0.9",",","0.2",":","0.8","}",",","'conv2","'",":","{","0.1",":","0.9",",","0.2",":","0.8","}","}",",","in","which",",","'conv1","'","and","is","the","name","of","the","conv","layer",",","and","0.1:0.9","means","when","the","sparsity","of","conv1","is","0.1","(","10","%",")",",","the","model","'s","val","accuracy","equals","to","0.9.","sparsity_per_iter",":","float","The","sparsity","of","the","model","that","the","pruner","try","to","prune","in","each","iteration",".","acc_drop_threshold",":","float","The","hyperparameter","used","to","quantifiy","the","sensitivity","for","each","layer",".","checkpoint_dir",":","str","The","dir","path","to","save","the","checkpoints","during","the","pruning","."]
docstr: Generate the cfg_list for the pruner according to the prune ratios.
  
  Parameters
  ---------
      ratios:
          For example: {'conv1' : 0.2}
  
  Returns
  -------
      cfg_list:
          For example: [{'sparsity':0.2, 'op_names':['conv1'], 'op_types':['Conv2d']}]
docstr_tok: ["Generate","the","cfg_list","for","the","pruner","according","to","the","prune","ratios",".","Parameters","--","--","--","--","-","ratios",":","For","example",":","{","'conv1","'",":","0.2","}","Returns","--","--","--","-","cfg_list",":","For","example",":","[","{","'sparsity':0.2",",","'op_names","'",":","[","'conv1","'","]",",","'op_types","'",":","[","'Conv2d","'","]","}","]"]
code: def create_cfg(self, ratios):
      """
      Generate the cfg_list for the pruner according to the prune ratios.
  
      Parameters
      ---------
          ratios:
              For example: {'conv1' : 0.2}
  
      Returns
      -------
          cfg_list:
              For example: [{'sparsity':0.2, 'op_names':['conv1'], 'op_types':['Conv2d']}]
      """
      cfg_list = []
      for layername in ratios:
          prune_ratio = ratios[layername]
          remain = 1 - self.analyzer.already_pruned[layername]
          sparsity = (
              remain * prune_ratio
              + self.analyzer.already_pruned[layername]
          )
          if sparsity > 0:
              # Pruner does not allow the prune ratio to be zero
              cfg = {
                  "sparsity": sparsity,
                  "op_names": [layername],
                  "op_types": ["Conv2d"],
              }
              cfg_list.append(cfg)
      return cfg_list
  
imports: import os
  import csv
  import copy
  import json
  import logging
  import torch
  from schema import And, Optional
  from ..compressor import Pruner
  from ..utils.config_validation import CompressorSchema
  from .constants_pruner import PRUNER_DICT
  from ..utils.sensitivity_analysis import SensitivityAnalysis
label_desc: Use image node_id 6 for calling the SensitivityPruner obj's underlying member method code with example usage: obj.create_cfg(ratios) and returns: cfg_list