code: def _rgb2hsv(img: Tensor) -> Tensor:
      r, g, b = img.unbind(axis=-3)
  
      # Implementation is based on https://github.com/python-pillow/Pillow/blob/4174d4267616897df3746d315d5a2d0f82c656ee/
      # src/libImaging/Convert.c#L330
      maxc = paddle.max(img, axis=-3)
      minc = paddle.min(img, axis=-3)
  
      # The algorithm erases S and H channel where `maxc = minc`. This avoids NaN
      # from happening in the results, because
      #   + S channel has division by `maxc`, which is zero only if `maxc = minc`
      #   + H channel has division by `(maxc - minc)`.
      #
      # Instead of overwriting NaN afterwards, we just prevent it from occuring so
      # we don't need to deal with it in case we save the NaN in a buffer in
      # backprop, if it is ever supported, but it doesn't hurt to do so.
      eqc = maxc == minc
  
      cr = maxc - minc
      # Since `eqc => cr = 0`, replacing denominator with 1 when `eqc` is fine.
      ones = paddle.ones_like(maxc)
      s = cr / paddle.where(eqc, ones, maxc)
      # Note that `eqc => maxc = minc = r = g = b`. So the following calculation
      # of `h` would reduce to `bc - gc + 2 + rc - bc + 4 + rc - bc = 6` so it
      # would not matter what values `rc`, `gc`, and `bc` have here, and thus
      # replacing denominator with 1 when `eqc` is fine.
      cr_divisor = paddle.where(eqc, ones, cr)
      rc = (maxc - r) / cr_divisor
      gc = (maxc - g) / cr_divisor
      bc = (maxc - b) / cr_divisor
  
      hr = (maxc == r).cast("float32") * (bc - gc)
      hg = ((maxc == g) & (maxc != r)).cast("float32") * (2.0 + rc - bc)
      hb = ((maxc != g) & (maxc != r)).cast("float32") * (4.0 + gc - rc)
      h = hr + hg + hb
      h = fmod((h / 6.0 + 1.0), paddle.to_tensor(1.0))
      return paddle.stack((h, s, maxc), axis=-3)
  
imports: import math
  import numbers
  import warnings
  from enum import Enum
  from typing import Any
  from typing import Dict
  from typing import List
  from typing import Optional
  from typing import Sequence
  from typing import Tuple
  from typing import Union
  import paddle
  import paddle.nn
  from paddle import Tensor
  from paddle.nn import functional
  from paddle.nn.functional import grid_sample
  from paddle.vision import transforms
label_desc: Use image node_id 4 for calling a global function with example usage: _rgb2hsv(img) and returns: paddle