code: def detrend(data, axis=-1, type="linear", bp=0, overwrite_data=False):
      """
      Remove linear trend along axis from data.
  
      Parameters
      ----------
      data : array_like
          The input data.
      axis : int, optional
          The axis along which to detrend the data. By default this is the
          last axis (-1).
      type : {'linear', 'constant'}, optional
          The type of detrending. If ``type == 'linear'`` (default),
          the result of a linear least-squares fit to `data` is subtracted
          from `data`.
          If ``type == 'constant'``, only the mean of `data` is subtracted.
      bp : array_like of ints, optional
          A sequence of break points. If given, an individual linear fit is
          performed for each part of `data` between two break points.
          Break points are specified as indices into `data`.
      overwrite_data : bool, optional
          If True, perform in place detrending and avoid a copy. Default is False
  
      Returns
      -------
      ret : ndarray
          The detrended input data.
  
      Examples
      --------
      >>> import cusignal
      >>> import cupy as cp
      >>> randgen = cp.random.RandomState(9)
      >>> npoints = 1000
      >>> noise = randgen.randn(npoints)
      >>> x = 3 + 2*cp.linspace(0, 1, npoints) + noise
      >>> (cusignal.detrend(x) - noise).max() < 0.01
      True
      """
      if type not in ["linear", "l", "constant", "c"]:
          raise ValueError("Trend type must be 'linear' or 'constant'.")
      data = cp.asarray(data)
      dtype = data.dtype.char
      if dtype not in "dfDF":
          dtype = "d"
      if type in ["constant", "c"]:
          ret = data - cp.expand_dims(cp.mean(data, axis), axis)
          return ret
      else:
          dshape = data.shape
          N = dshape[axis]
          bp = np.sort(np.unique(np.r_[0, bp, N]))
          if np.any(bp > N):
              raise ValueError(
                  "Breakpoints must be less than length of \
                  data along given axis."
              )
  
          Nreg = len(bp) - 1
          # Restructure data so that axis is along first dimension and
          #  all other dimensions are collapsed into second dimension
          rnk = len(dshape)
          if axis < 0:
              axis = axis + rnk
  
          newdims = np.r_[axis, 0:axis, axis + 1 : rnk]
          newdata = cp.reshape(
              cp.transpose(data, tuple(newdims)),
              (N, _prod(dshape) // N),
          )
  
          if not overwrite_data:
              newdata = newdata.copy()  # make sure we have a copy
          if newdata.dtype.char not in "dfDF":
              newdata = newdata.astype(dtype)
  
          # Find leastsq fit and remove it for each piece
          for m in range(Nreg):
              Npts = int(bp[m + 1] - bp[m])
              A = _detrend_A_kernel(size=Npts * 2)
              A = cp.reshape(A, (Npts, 2))
              sl = slice(bp[m], bp[m + 1])
              coef, _, _, _ = cp.linalg.lstsq(A, newdata[sl])
              newdata[sl] = newdata[sl] - cp.dot(A, coef)
  
          # Put data back in original shape.
          tdshape = np.take(dshape, newdims, 0)
          ret = cp.reshape(newdata, tuple(tdshape))
          vals = list(range(1, rnk))
          olddims = vals[:axis] + [0] + vals[axis:]
          ret = cp.transpose(ret, tuple(olddims))
          return ret
  
imports: import cupy
  import numpy
  from cupyx.scipy import linalg
  from ..convolution.correlate import correlate
  from ..filter_design.filter_design_utils import _validate_sos
  from ..utils.arraytools import _axis_reverse, _axis_slice, _const_ext, _even_ext, _odd_ext
  from ..utils.helper_tools import _get_max_smem, _get_max_tpb
  from ._channelizer_cuda import _channelizer
  from ._sosfilt_cuda import _sosfilt
label_desc: Use image node_id 12 for calling a global function with example usage: detrend(data, axis, type, bp, overwrite_data) and returns: ret, ret