code: def _make_scalar_valued_func(func, inputs, modules):
      outputs = func(inputs, modules)
      output_size = (
          outputs.numel()
          if torch.is_tensor(outputs)
          else sum(o.numel() for o in outputs)
      )
  
      if output_size > 1:
          # Define this outside `func_scalar_valued` so that random tensors are generated only once.
          grad_outputs = tuple(torch.randn_like(o) for o in outputs)
  
          def func_scalar_valued(inputs, modules):
              outputs = func(inputs, modules)
              return sum(
                  (output * grad_output).sum()
                  for output, grad_output, in zip(outputs, grad_outputs)
              )
  
          return func_scalar_valued
  
      return func
  
imports: import copy
  import torch
  from torch import nn
  from torchsde.types import Callable, ModuleOrModules, Optional, TensorOrTensors
label_desc: Use image node_id 3 for calling a global function with example usage: _make_scalar_valued_func(func, inputs, modules) and returns: func, func_scalar_valued, sum