code: def _detect_functional_mode():
      from torch._ops import _get_dispatch_mode_pre_dispatch
  
      pre_dispatch_functional_mode = _get_dispatch_mode_pre_dispatch(
          torch._C._TorchDispatchModeKey.FUNCTIONAL
      )
      post_dispatch_functional_mode = torch._C._get_dispatch_mode(
          torch._C._TorchDispatchModeKey.FUNCTIONAL
      )
  
      assert (pre_dispatch_functional_mode is None) or (
          post_dispatch_functional_mode is None
      )
  
      if pre_dispatch_functional_mode is None:
          return post_dispatch_functional_mode
  
      return pre_dispatch_functional_mode
  
imports: import contextlib
  from typing import Optional, Union, List, Set, Dict, Any
  import warnings
  from dataclasses import dataclass
  import torch
  import torchgen
  from torch._C import _len_torch_dispatch_stack, _get_dispatch_stack_at, _pop_torch_dispatch_stack, _push_on_torch_dispatch_stack, DispatchKey
label_desc: Use image node_id 2 for calling a global function with example usage: _detect_functional_mode() and returns: pre_dispatch_functional_mode, post_dispatch_functional_mode