code: def safe_normalize(x, *, p=2):
      """
      Safely project a vector onto the sphere wrt the ``p``-norm. This avoids the
      singularity at zero by mapping zero to the vector ``[1, 0, 0, ..., 0]``.
  
      :param torch.Tensor x: A vector
      :param float p: The norm exponent, defaults to 2 i.e. the Euclidean norm.
      :returns: A normalized version ``x / ||x||_p``.
      :rtype: Tensor
      """
      assert isinstance(p, (float, int))
      assert p >= 0
      norm = torch.linalg.norm(x, dim=-1, ord=p, keepdim=True)
      x = x / norm.clamp(min=torch.finfo(x.dtype).tiny)
      x.data[..., 0][
          x.data.eq(0).all(dim=-1)
      ] = 1  # Avoid the singularity.
      return x
  
imports: import math
  import torch
  from torch.fft import irfft, rfft
  from ..None import settings
label_desc: Use image node_id 21 for calling a global function with example usage: safe_normalize(x) and returns: x