code: def to_dtype_image(
      image: torch.Tensor,
      dtype: torch.dtype = torch.float,
      scale: bool = False,
  ) -> torch.Tensor:
      if image.dtype == dtype:
          return image
      elif not scale:
          return image.to(dtype)
  
      float_input = image.is_floating_point()
      if torch.jit.is_scripting():
          # TODO: remove this branch as soon as `dtype.is_floating_point` is supported by JIT
          float_output = torch.tensor(
              0, dtype=dtype
          ).is_floating_point()
      else:
          float_output = dtype.is_floating_point
  
      if float_input:
          # float to float
          if float_output:
              return image.to(dtype)
  
          # float to int
          if (
              image.dtype == torch.float32
              and dtype in (torch.int32, torch.int64)
          ) or (image.dtype == torch.float64 and dtype == torch.int64):
              raise RuntimeError(
                  f"The conversion from {image.dtype} to {dtype} cannot be performed safely."
              )
  
          # For data in the range `[0.0, 1.0]`, just multiplying by the maximum value of the integer range and converting
          # to the integer dtype  is not sufficient. For example, `torch.rand(...).mul(255).to(torch.uint8)` will only
          # be `255` if the input is exactly `1.0`. See https://github.com/pytorch/vision/pull/2078#issuecomment-612045321
          # for a detailed analysis.
          # To mitigate this, we could round before we convert to the integer dtype, but this is an extra operation.
          # Instead, we can also multiply by the maximum value plus something close to `1`. See
          # https://github.com/pytorch/vision/pull/2078#issuecomment-613524965 for details.
          eps = 1e-3
          max_value = float(_max_value(dtype))
          # We need to scale first since the conversion would otherwise turn the input range `[0.0, 1.0]` into the
          # discrete set `{0, 1}`.
          return image.mul(max_value + 1.0 - eps).to(dtype)
      else:
          # int to float
          if float_output:
              return image.to(dtype).mul_(1.0 / _max_value(image.dtype))
  
          # int to int
          num_value_bits_input = _num_value_bits(image.dtype)
          num_value_bits_output = _num_value_bits(dtype)
  
          if num_value_bits_input > num_value_bits_output:
              return image.bitwise_right_shift(
                  num_value_bits_input - num_value_bits_output
              ).to(dtype)
          else:
              return image.to(dtype).bitwise_left_shift_(
                  num_value_bits_output - num_value_bits_input
              )
  
imports: import math
  from typing import List, Optional
  import PIL.Image
  import torch
  from torch.nn.functional import conv2d, pad
  from torchvision import tv_tensors
  from torchvision.transforms._functional_tensor import _max_value
  from torchvision.transforms.functional import pil_to_tensor, to_pil_image
  from torchvision.utils import _log_api_usage_once
  from ._utils import _get_kernel, _register_kernel_internal
label_desc: Use image node_id 12 for calling a global function with example usage: to_dtype_image(image, dtype, scale) and returns: image, image, image, image, image, image, image