code: def checkpoint_diff(
      state_dict_a: Dict[str, torch.Tensor],
      state_dict_b: Dict[str, torch.Tensor],
      scale: float,
      threshold: float,
  ) -> List[Union[Keep, Insert, Remove, Modify]]:
      """
      Uses a modified version of the Myers diff algorithm to compute a representation
      of the diff between two model state dictionaries.
  
      The only difference is that in addition to the `Keep`, `Insert`, and `Remove`
      operations, we add `Modify`. This corresponds to keeping a parameter
      but changing its weights (not the shape).
  
      Adapted from [this gist]
      (https://gist.github.com/adamnew123456/37923cf53f51d6b9af32a539cdfa7cc4).
      """
      param_list_a = [
          (k, tuple(v.shape)) for k, v in state_dict_a.items()
      ]
      param_list_b = [
          (k, tuple(v.shape)) for k, v in state_dict_b.items()
      ]
  
      # This marks the farthest-right point along each diagonal in the edit
      # graph, along with the history that got it there
      frontier: Dict[int, _Frontier] = {1: _Frontier(0, [])}
  
      def one(idx):
          """
          The algorithm Myers presents is 1-indexed; since Python isn't, we
          need a conversion.
          """
          return idx - 1
  
      a_max = len(param_list_a)
      b_max = len(param_list_b)
      for d in range(0, a_max + b_max + 1):
          for k in range(-d, d + 1, 2):
              # This determines whether our next search point will be going down
              # in the edit graph, or to the right.
              #
              # The intuition for this is that we should go down if we're on the
              # left edge (k == -d) to make sure that the left edge is fully
              # explored.
              #
              # If we aren't on the top (k != d), then only go down if going down
              # would take us to territory that hasn't sufficiently been explored
              # yet.
              go_down = k == -d or (
                  k != d and frontier[k - 1].x < frontier[k + 1].x
              )
  
              # Figure out the starting point of this iteration. The diagonal
              # offsets come from the geometry of the edit grid - if you're going
              # down, your diagonal is lower, and if you're going right, your
              # diagonal is higher.
              if go_down:
                  old_x, history = frontier[k + 1]
                  x = old_x
              else:
                  old_x, history = frontier[k - 1]
                  x = old_x + 1
  
              # We want to avoid modifying the old history, since some other step
              # may decide to use it.
              history = history[:]
              y = x - k
  
              # We start at the invalid point (0, 0) - we should only start building
              # up history when we move off of it.
              if 1 <= y <= b_max and go_down:
                  history.append(Insert(*param_list_b[one(y)]))
              elif 1 <= x <= a_max:
                  history.append(Remove(*param_list_a[one(x)]))
  
              # Chew up as many diagonal moves as we can - these correspond to common lines,
              # and they're considered "free" by the algorithm because we want to maximize
              # the number of these in the output.
              while (
                  x < a_max
                  and y < b_max
                  and param_list_a[one(x + 1)]
                  == param_list_b[one(y + 1)]
              ):
                  x += 1
                  y += 1
                  history.append(Keep(*param_list_a[one(x)]))
  
              if x >= a_max and y >= b_max:
                  # If we're here, then we've traversed through the bottom-left corner,
                  # and are done.
                  return _finalize(
                      history,
                      state_dict_a,
                      state_dict_b,
                      scale,
                      threshold,
                  )
              else:
                  frontier[k] = _Frontier(x, history)
  
      assert False, "Could not find edit script"
  
imports: import argparse
  import logging
  from typing import Union, Dict, List, Tuple, NamedTuple, cast
  import termcolor
  import torch
  from allennlp.commands.subcommand import Subcommand
  from allennlp.common.file_utils import cached_path
  from allennlp.nn.util import read_state_dict
label_desc: Use image node_id 2 for calling a global function with example usage: checkpoint_diff(state_dict_a, state_dict_b, scale, threshold) and returns: unknown, _finalize