code: def generate_weights_table(
      module,
      table_name,
      metrics,
      dataset,
      include_patterns=None,
      exclude_patterns=None,
  ):
      weights_endswith = (
          "_QuantizedWeights"
          if module.__name__.split(".")[-1] == "quantization"
          else "_Weights"
      )
      weight_enums = [
          getattr(module, name)
          for name in dir(module)
          if name.endswith(weights_endswith)
      ]
      weights = [w for weight_enum in weight_enums for w in weight_enum]
  
      if include_patterns is not None:
          weights = [
              w
              for w in weights
              if any(p in str(w) for p in include_patterns)
          ]
      if exclude_patterns is not None:
          weights = [
              w
              for w in weights
              if all(p not in str(w) for p in exclude_patterns)
          ]
  
      ops_name = (
          "GIPS" if "QuantizedWeights" in weights_endswith else "GFLOPS"
      )
  
      metrics_keys, metrics_names = zip(*metrics)
      column_names = (
          ["Weight"]
          + list(metrics_names)
          + ["Params"]
          + [ops_name, "Recipe"]
      )  # Final column order
      column_names = [
          f"**{name}**" for name in column_names
      ]  # Add bold
  
      content = []
      for w in weights:
          row = [
              f":class:`{w} <{type(w).__name__}>`",
              *(
                  w.meta["_metrics"][dataset][metric]
                  for metric in metrics_keys
              ),
              f"{w.meta['num_params']/1e6:.1f}M",
              f"{w.meta['_ops']:.2f}",
              f"`link <{w.meta['recipe']}>`__",
          ]
  
          content.append(row)
  
      column_widths = (
          ["110"] + ["18"] * len(metrics_names) + ["18"] * 2 + ["10"]
      )
      widths_table = " ".join(column_widths)
  
      table = tabulate(content, headers=column_names, tablefmt="rst")
  
      generated_dir = Path("generated")
      generated_dir.mkdir(exist_ok=True)
      with open(
          generated_dir / f"{table_name}_table.rst", "w+"
      ) as table_file:
          table_file.write(
              ".. rst-class:: table-weights\n"
          )  # Custom CSS class, see custom_torchvision.css
          table_file.write(".. table::\n")
          table_file.write(f"    :widths: {widths_table} \n\n")
          table_file.write(f"{textwrap.indent(table, ' ' * 4)}\n\n")
  
imports: import os
  import sys
  import textwrap
  from copy import copy
  from pathlib import Path
  import pytorch_sphinx_theme
  import torchvision
  import torchvision.models
  from sphinx_gallery.sorting import ExplicitOrder
  from tabulate import tabulate
  from sphinx_gallery import gen_rst
  from docutils import nodes
  from sphinx import addnodes
  from sphinx.util.docfields import TypedField
label_desc: Use image node_id 4 for calling a global function with example usage: generate_weights_table(module, table_name, metrics, dataset, include_patterns, exclude_patterns) without return types