code: def analyze(model: nn.Module, input_size, query_granularity: int):
      assert isinstance(model, nn.Module)
      assert isinstance(input_size, (list, tuple))
  
      pre_hooks, post_hooks = [], []
      stats: OrderedDict[str, ModuleStats] = OrderedDict()
  
      try:
          _for_leaf(
              model, _register_hooks, pre_hooks, post_hooks, stats
          )
  
          x = torch.rand(*input_size)  # add module duration time
          x = x.to(next(model.parameters()).device)
          model.eval()
          model(x)
  
          stat_tree = _convert_leaf_modules_to_stat_tree(stats)
  
          return stat_tree.get_collected_stat_nodes(query_granularity)
  
      finally:
          for stat in stats.values():
              stat.done = True
          for hook in itertools.chain(pre_hooks, post_hooks):
              hook.remove()
  
imports: import time
  from collections import OrderedDict
  from typing import Dict, Sequence
  import functools
  import itertools
  import numpy
  import torch
  import torch.nn
  from .compute_madd import compute_madd
  from .compute_flops import compute_flops
  from .compute_memory import compute_memory
  from .stat_tree import StatTree, StatNode
  from .reporter import report_format
label_desc: Use image node_id 2 for calling a global function with example usage: analyze(model, input_size, query_granularity) and returns: stat_tree