code: def _sorted_patterns_dict(
      patterns_dict: Dict[Pattern, QuantizeHandler]
  ) -> Dict[Pattern, QuantizeHandler]:
      """
      Return a sorted version of the patterns dictionary such that longer patterns are matched first,
      e.g. match (F.relu, F.linear) before F.relu.
      This works for current use cases, but we may need to have a more clever way to sort
      things to address more complex patterns
      """
  
      def get_len(pattern):
          """this will calculate the length of the pattern by counting all the entries
          in the pattern.
          this will make sure (nn.ReLU, (nn.BatchNorm, nn.Conv2d)) comes before
          (nn.BatchNorm, nn.Conv2d) so that we can match the former first
          """
          len = 0
          if isinstance(pattern, tuple):
              for item in pattern:
                  len += get_len(item)
          else:
              len += 1
          return len
  
      return OrderedDict(
          sorted(
              patterns_dict.items(),
              key=lambda kv: -get_len(kv[0])
              if isinstance(kv[0], tuple)
              else 1,
          )
      )
  
file_path: pytorch/torch/ao/quantization/fx/pattern_utils.py
imports: from collections import OrderedDict
  from typing import Dict, Any
  from torch.ao.quantization.utils import Pattern
  from ..fake_quantize import FixedQParamsFakeQuantize
  from ..observer import ObserverBase
  import copy
label_desc: Use image node_id 6 for calling a global function with example usage: _sorted_patterns_dict(patterns_dict) and returns: OrderedDict, len