code: def randn_tensor(
      shape: Union[Tuple, List],
      generator: Optional[
          Union[List[torch.Generator], torch.Generator]
      ] = None,
      device: Optional[torch.device] = None,
      dtype: Optional[torch.dtype] = None,
      layout: Optional[torch.layout] = None,
  ) -> torch.Tensor:
      """A helper function to create random tensors on the desired `device` with the desired `dtype`. When
      passing a list of generators, you can seed each batch size individually. If CPU generators are passed, the tensor
      is always created on the CPU.
      """
      # device on which tensor is created defaults to device
      rand_device = device
      batch_size = shape[0]
  
      layout = layout or torch.strided
      device = device or torch.device("cpu")
  
      if generator is not None:
          gen_device_type = (
              generator.device.type
              if not isinstance(generator, list)
              else generator[0].device.type
          )
          if (
              gen_device_type != device.type
              and gen_device_type == "cpu"
          ):
              rand_device = "cpu"
              if device != "mps":
                  warnings.warn(
                      f"The passed generator was created on 'cpu' even though a tensor on {device} was expected."
                      f" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"
                      f" slighly speed up this function by passing a generator that was created on the {device} device."
                  )
          elif (
              gen_device_type != device.type
              and gen_device_type == "cuda"
          ):
              raise ValueError(
                  f"Cannot generate a {device} tensor from a generator of type {gen_device_type}."
              )
  
      # make sure generator list of length 1 is treated like a non-list
      if isinstance(generator, list) and len(generator) == 1:
          generator = generator[0]
  
      if isinstance(generator, list):
          shape = (1,) + shape[1:]
          latents = [
              torch.randn(
                  shape,
                  generator=generator[i],
                  device=rand_device,
                  dtype=dtype,
                  layout=layout,
              )
              for i in range(batch_size)
          ]
          latents = torch.cat(latents, dim=0).to(device)
      else:
          latents = torch.randn(
              shape,
              generator=generator,
              device=rand_device,
              dtype=dtype,
              layout=layout,
          ).to(device)
  
      return latents
  
imports: import gc
  import random
  import warnings
  from contextlib import contextmanager
  from typing import Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.utils.rnn import pad_sequence
  from transformers import top_k_top_p_filtering
  from .import_utils import is_npu_available, is_xpu_available
label_desc: Use image node_id 17 for calling a global function with example usage: randn_tensor(shape, generator, device, dtype, layout) and returns: latents