code: def _verify_cancellable_transposes(self):
      # invert "transpose_op_to_cancel_ops"
      transpose_cancel_ops_to_starting_transpose_set = defaultdict(
          lambda: set()
      )
      for (
          op,
          cancel_ops_list,
      ) in self.transpose_op_to_cancel_ops.items():
          for cancel_op in cancel_ops_list:
              transpose_cancel_ops_to_starting_transpose_set[
                  cancel_op
              ].update(set([op]))
  
      for op in transpose_cancel_ops_to_starting_transpose_set:
          assert (
              op not in self.transpose_op_to_cancel_ops
          ), "transpose reduction optimization: transpose op '{}' cannot be both a starting and cancel op".format(
              op.name
          )
  
      # invert "transpose_op_to_materialize_ops"
      materizalize_ops_to_starting_transpose_set = defaultdict(
          lambda: set()
      )
      for (
          op,
          materialize_ops,
      ) in self.transpose_op_to_materialize_ops.items():
          for materialize_op, edge in materialize_ops:
              materizalize_ops_to_starting_transpose_set[
                  materialize_op
              ].update(set([op]))
  
              # the starting transpose op may not be in "transpose_op_to_cancel_ops"
              # but it needs to be removed if it materializes later, hence we need to add it
              # to the "transpose_op_to_cancel_ops", with an empty value, i.e. no other ops to cancel because of it
              if op not in self.transpose_op_to_cancel_ops:
                  self.transpose_op_to_cancel_ops[op] = []
  
      # (starting transpose ops) and (transpose cancel ops + materialize ops) form a bipartite graph.
      # Find the connected components of this graph, by doing a BFS traversal
      connected_components = []  # List[(Set(op), Set(op)), Set(op)]
      visited = {}
      for op in list(self.transpose_op_to_cancel_ops.keys()):
          if op in visited:
              continue
          visited[op] = 1
          set_a = set([op])  # set of starting transpose ops
          set_b1 = (
              set()
          )  # set of transpose cancel ops connected to set_a
          set_b2 = set()  # set of materialize ops connected to set_a
          queue = []
          queue.extend(self.transpose_op_to_cancel_ops[op])
          if op in self.transpose_op_to_materialize_ops:
              materialize_ops_list = list(
                  list(zip(*self.transpose_op_to_materialize_ops[op]))[
                      0
                  ]
              )
              queue.extend(materialize_ops_list)
          while len(queue) > 0:
              o = queue.pop(0)
              visited[o] = 1
              # enque nodes connected to o
              if o in self.transpose_op_to_cancel_ops:
                  set_a.update(set([o]))
                  for neighbor_op in self.transpose_op_to_cancel_ops[o]:
                      if neighbor_op not in visited:
                          queue.append(neighbor_op)
                  if o in self.transpose_op_to_materialize_ops:
                      materialize_ops_list = list(
                          list(
                              zip(
                                  *self.transpose_op_to_materialize_ops[
                                      o
                                  ]
                              )
                          )[0]
                      )
                      for neighbor_op in materialize_ops_list:
                          if neighbor_op not in visited:
                              queue.append(neighbor_op)
              elif o in transpose_cancel_ops_to_starting_transpose_set:
                  set_b1.update(set([o]))
                  for (
                      neighbor_op
                  ) in transpose_cancel_ops_to_starting_transpose_set[
                      o
                  ]:
                      if neighbor_op not in visited:
                          queue.append(neighbor_op)
              else:
                  set_b2.update(set([o]))
                  for (
                      neighbor_op
                  ) in materizalize_ops_to_starting_transpose_set[o]:
                      if neighbor_op not in visited:
                          queue.append(neighbor_op)
          connected_components.append((set_a, set_b1, set_b2))
  
      starting_ops_to_remove = (
          set()
      )  # starting ops to remove from the optimization list
  
      # now for each connected component, make a decision whether to cancel it or not
      # (either all transpose ops in a set get cancelled or they don't)
      for (
          op_set,
          op_cancel_set,
          materialize_op_set,
      ) in connected_components:
          block_output = False
          # check that output is not directly connected to a starting transpose op
          for op in op_set:
              if op.outputs[0] in self.block.outputs:
                  starting_ops_to_remove.update(op_set)
                  block_output = True
                  break
          if block_output:
              continue
  
          materizalize_set = set(list(materialize_op_set))
          if len(materizalize_set) >= len(op_set) + len(op_cancel_set):
              starting_ops_to_remove.update(op_set)
  
      # remove ops
      for op in starting_ops_to_remove:
          self.transpose_op_to_cancel_ops.pop(op, None)
  
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  from coremltools.converters.mil.mil.passes.pass_registry import register_pass
  from coremltools.converters.mil.mil import Builder
  from coremltools.converters.mil.mil.var import Var
  import logging
  import numpy
  import copy
  from collections import defaultdict
label_desc: Use image node_id 9 for calling the TransposeOptimization obj's underlying member method code with example usage: obj._verify_cancellable_transposes() without return types