code: def tensor(
      data=None,
      dtype=None,
      order="K",
      chunk_size=None,
      gpu=None,
      sparse=False,
  ) -> Tensor:
      order = order or "K"
      if isinstance(data, TENSOR_TYPE):
          if isinstance(data, TensorData):
              data = Tensor(data)
          return data.astype(
              dtype or data.dtype, order=order, copy=False
          )
      elif (
          isinstance(data, (tuple, list))
          and len(data) > 0
          and all(isinstance(d, TENSOR_TYPE) for d in data)
      ):
          from ..merge import stack
  
          data = stack(data)
          return data.astype(
              dtype or data.dtype, order=order, copy=False
          )
      elif np.isscalar(data):
          return scalar(data, dtype=dtype)
      elif issparse(data):
          return _from_spmatrix(
              data, dtype=dtype, chunk_size=chunk_size, gpu=gpu
          )
      elif hasattr(data, "__mars_tensor__"):
          return data.__mars_tensor__(dtype=dtype, order=order)
      else:
          m = get_array_module(data)
          try:
              data = m.asarray(data, dtype=dtype, order=order)
          except ValueError:
              arr = data.__array__(dtype=dtype)
              if isinstance(arr, TENSOR_TYPE):
                  return arr.astype(arr.dtype, order=order, copy=False)
              raise
          if gpu is None:
              if cp is not None and m is cp:
                  gpu = True
  
      if is_array(data):
          if data.ndim == 0:
              return scalar(data.item(), dtype=dtype)
          tensor_order = (
              TensorOrder.C_ORDER
              if data.flags["C_CONTIGUOUS"]
              else TensorOrder.F_ORDER
          )
          op = ArrayDataSource(
              data, dtype=dtype, gpu=gpu, chunk_size=chunk_size
          )
          t = op(data.shape, chunk_size=chunk_size, order=tensor_order)
          if sparse and not t.issparse():
              return t.tosparse()
          return t
      else:
          raise ValueError(
              f"Cannot create tensor by given data: {data}"
          )
  
imports: import numpy
  from ...None import opcodes
  from ...lib.sparse.core import issparse, get_array_module, cp, cps, sps
  from ...lib.sparse import SparseNDArray
  from ...serialization.serializables import FieldTypes, AnyField, NDArrayField, TupleField
  from ...utils import on_serialize_shape, on_deserialize_shape
  from ..core import TENSOR_TYPE, TensorOrder, TensorData, Tensor
  from ..utils import get_chunk_slices
  from ..array_utils import array_module, is_array, is_cupy
  from .core import TensorNoInput
  from .scalar import scalar
label_desc: Use image node_id 2 for calling a global function with example usage: tensor(data, dtype, order, chunk_size, gpu, sparse) and returns: data, t, data, scalar, t, scalar, _from_spmatrix, data, arr