code: def _array_to_chainerx(array, device=None):
      # If device is None, appropriate device is chosen according to the input
      # arrays.
      assert device is None or isinstance(device, chainerx.Device)
  
      if array is None:
          return None
  
      if array.dtype not in chainerx.all_dtypes:
          raise TypeError(
              "Dtype {} is not supported in ChainerX.".format(
                  array.dtype.name
              )
          )
  
      if isinstance(array, chainerx.ndarray):
          if device is None:
              return array
          if device is array.device:
              return array
          return array.to_device(device)
      if isinstance(array, numpy.ndarray):
          if device is None:
              device = chainerx.get_device("native", 0)
          return chainerx.array(array, device=device, copy=False)
      if isinstance(array, cuda.ndarray):
          if device is None:
              device = chainerx.get_device("cuda", array.device.id)
          elif device.backend.name != "cuda":
              # cupy to non-cuda backend
              # TODO(niboshi): Remove conversion to numpy when both CuPy and
              # ChainerX support the array interface.
              array = _cpu._to_cpu(array)
              return chainerx.array(array, device=device, copy=False)
          elif device.index != array.device.id:
              # cupy to cuda backend but different device
              array = cuda.to_gpu(array, device=device.index)
          # cupy to cuda backend with the same device
          return chainerx._core._fromrawpointer(
              array.data.mem.ptr,
              array.shape,
              array.dtype,
              array.strides,
              device,
              array.data.ptr - array.data.mem.ptr,
              array,
          )
      if isinstance(array, intel64.mdarray):
          return _array_to_chainerx(numpy.array(array), device)
      if numpy.isscalar(array):
          return chainerx.asarray(array)
  
      raise TypeError(
          "Array cannot be converted into chainerx.ndarray"
          "\nActual type: {0}.".format(type(array))
      )
  
imports: import numpy
  import chainer
  from chainer import _backend
  from chainer.backends import _cpu
  from chainer.backends import cuda
  from chainer.backends import intel64
  import chainerx
label_desc: Use image node_id 4 for calling a global function with example usage: _array_to_chainerx(array, device) and returns: None, array, chainerx, chainerx, _array_to_chainerx, chainerx, array, array, chainerx