code: def test_replace_pattern_with_filters(self):
      class M(torch.nn.Module):
          def forward(self, x, scale, zero_point):
              # Match, second input to add is a scalar
              x = x.dequantize()
              x = torch.add(x, 2)
              x = x.relu()
              x = torch.quantize_per_tensor(
                  x, scale, zero_point, torch.quint8
              )
  
              y = x + 1
              # NOT a match, second input to add is NOT a scalar
              x = x.dequantize()
              x = torch.add(x, y)
              x = x.relu()
              x = torch.quantize_per_tensor(
                  x, scale, zero_point, torch.quint8
              )
  
              return x
  
      def BinaryOpScalarReLUPattern(x, num, scale, zero_point):
          x = x.dequantize()
          x = torch.add(x, num)
          x = x.relu()
          x = torch.quantize_per_tensor(
              x, scale, zero_point, torch.quint8
          )
          return x
  
      def BinaryOpScalarReLUReplacement(x, num, scale, zero_point):
          x = torch.mul(x, num)
          return x
  
      def second_input_is_scalar(match, original_graph, pattern_graph):
          """check the node that's matched to the second input of the pattern graph
          is a scalar number
          """
          input_idx = 0
          for node in pattern_graph.nodes:
              if node.op == "placeholder":
                  if input_idx == 1:
                      num_node = node
                  input_idx += 1
          if not isinstance(match.nodes_map[num_node], (int, float)):
              return False
          return True
  
      def check_replacement_nodes(self, traced, matches):
          replacement_nodes_in_graph = [
              node
              for node in traced.graph.nodes
              if node.target == torch.mul
          ]
          replacement_nodes_in_res = [
              r for m in matches for r in m.replacements
          ]
          self.assertEqual(
              len(replacement_nodes_in_graph),
              len(replacement_nodes_in_res),
          )
          self.assertEqual(
              replacement_nodes_in_graph, replacement_nodes_in_res
          )
          return len(replacement_nodes_in_graph)
  
      # match without filter, should find 2 match
      traced = symbolic_trace(M())
      matches = subgraph_rewriter.replace_pattern_with_filters(
          traced,
          BinaryOpScalarReLUPattern,
          BinaryOpScalarReLUReplacement,
          None,
      )
      self.assertEqual(len(matches), 2)
      self.assertEqual(
          check_replacement_nodes(self, traced, matches), 2
      )
  
      # match with filter, should find 1 match
      traced = symbolic_trace(M())
      matches = subgraph_rewriter.replace_pattern_with_filters(
          traced,
          BinaryOpScalarReLUPattern,
          BinaryOpScalarReLUReplacement,
          [second_input_is_scalar],
      )
      self.assertEqual(len(matches), 1)
      self.assertEqual(
          check_replacement_nodes(self, traced, matches), 1
      )
  
imports: import os
  import sys
  import torch
  from torch.fx import symbolic_trace, subgraph_rewriter
  from torch.fx.annotate import annotate
  from torch.fx.experimental.rewriter import RewritingTracer
  from torch.testing._internal.jit_utils import JitTestCase
label_desc: Use image node_id 23 for calling the TestSubgraphRewriter obj's underlying member method code with example usage: obj.test_replace_pattern_with_filters() and returns: x, x, True, len, x, False