code: def make_mirror_plan(sym, threshold, plan_info=None, **kwargs):
      """Memory allocation planner with a given threshold.
  
      The user can pass in a network configuration,
      a threshold that limits memory per block.
      And input shape configurations.
  
      Parameters
      ----------
      sym : symbol
          Input configuration of symbols.
          The user need to pre-mark the attribute "mirror_stage" on the nodes
          that can be book-kept as stage
  
          The algorithm will decide whether to disbale mirror on the stage nodes.
  
      threshold: integer
          A tuning parameter to tune the approximate size of each stage blocks
  
      plan_info: dict, optional
          Used to hold plan information.
  
      **kwargs:
          The arguments to infer shape.
  
      Returns
      -------
      alloc_sym: symbol
          A symbol with force mirror tagged on the nodes for better allocation.
      """
      threshold = threshold << 20
      sym = sym.__copy__()
      internals = sym.get_internals()
      _, out_shapes, _ = internals.infer_shape(**kwargs)
      shape_dict = list(zip(internals.list_outputs(), out_shapes))
      total_size = 0
      param_size = 0
      local_size = 0
      save_size = 0
      max_size = 0
      last_sb = None
      last_local = 0
      period = 1
      last_stage = ""
      stage_decision = ""
  
      for idx, item in enumerate(shape_dict):
          sb = internals[idx]
          name, shape = item
          if is_param(name):
              param_size += prod(shape) * 4
              continue
          else:
              total_size += prod(shape) * 4
              local_size += prod(shape) * 4
              sb._set_attr(force_mirroring="True")
  
          if sb.attr("mirror_stage") is not None:
              stage = sb.attr("mirror_stage")
              if stage == "True" or stage != last_stage:
                  if local_size > threshold:
                      save_size += prod(shape) * 4
                      max_size = max(max_size, local_size)
                      local_size = 0
                      stage_decision = "False"
                      sb._set_attr(force_mirroring=stage_decision)
                  else:
                      stage_decision = "True"
                      pass
                  last_stage = stage
              elif stage == last_stage and stage_decision == "False":
                  save_size += prod(shape) * 4
                  sb._set_attr(force_mirroring=stage_decision)
  
      if plan_info is not None:
          plan_info["max_size"] = max_size
          plan_info["save_size"] = save_size
      return sym
  
file_path: insightface/recognition/arcface_mxnet/symbol/memonger_v2.py
imports: import mxnet
  import math
label_desc: Use image node_id 3 for calling a global function with example usage: make_mirror_plan(sym, threshold, plan_info) and returns: sym