code: def replace_instancenorm2d(norm, masks):
      """
      Parameters
      ----------
      norm : torch.nn.InstanceNorm2d
          The instancenorm module to be replace
      masks : Tuple of the input masks, output masks and weight masks
          Tuple of the masks, for example
          ([input_m1, input_m2], [output_m], {'weight':weight_m})
  
      Returns
      -------
      torch.nn.InstanceNorm2d
          The new instancenorm module
      """
      in_masks, output_mask, _ = masks
      assert isinstance(norm, nn.InstanceNorm2d)
      in_mask = in_masks[0]
  
      # N, C, H, W
      _, remained_in = convert_to_coarse_mask(in_mask, 1)
      _, remained_out = convert_to_coarse_mask(output_mask, 1)
      if remained_in.size(0) != remained_out.size(0):
          raise ShapeMisMatchError()
  
      num_features = remained_in.size(0)
      _logger.info(
          "replace instancenorm2d with num_features: %d", num_features
      )
      new_norm = torch.nn.InstanceNorm2d(
          num_features=num_features,
          eps=norm.eps,
          momentum=norm.momentum,
          affine=norm.affine,
          track_running_stats=norm.track_running_stats,
      )
      # assign weights
      if norm.affine:
          new_norm.weight.data = torch.index_select(
              norm.weight.data, 0, remained_in
          )
          new_norm.bias.data = torch.index_select(
              norm.bias.data, 0, remained_in
          )
  
      if norm.track_running_stats:
          new_norm.running_mean.data = torch.index_select(
              norm.running_mean.data, 0, remained_in
          )
          new_norm.running_var.data = torch.index_select(
              norm.running_var.data, 0, remained_in
          )
      return new_norm
  
file_path: nni/nni/compression/speedup/replacement.py
imports: import logging
  import math
  import torch
  import torch.nn
  from .error_code import EmptyLayerError, ShapeMisMatchError, InputsNumberError, OutputTypeError, UnBalancedGroupError
label_desc: Use image node_id 9 for calling a global function with example usage: replace_instancenorm2d(norm, masks) and returns: new_norm