code: def pack_sum_xgbmatrix(xs, ys, gids=None, weights=None):
      """Convert (feature, label) pairs into a xgb matrix with pack-sum format
      Parameters
      ----------
      xs: np.ndarray
          The feature vector
      ys: np.ndarray
          The normaizlied throughput
      gids: Optional[List[int]]
          Group id (task id)
      weights: Optional[np.ndarray]
          The weight of samples
      Returns
      -------
      dmatrix: xgb.DMatrix
          The DMatrix with pack-sum information
      """
      if gids is not None:
          # sort by group
          indices = gids.argsort()
          xs, ys = xs[indices], ys[indices]
          group_sizes = np.bincount(gids)
          if weights is not None:
              weights = weights[indices]
      else:
          # assume it has only one group
          group_sizes = [len(xs)]
  
      x_flatten = []
      y_flatten = []
      weights_flatten = []
      pack_ids = []
  
      if weights is not None:
          for ct, (x, y, w) in enumerate(zip(xs, ys, weights)):
              for row in x:
                  x_flatten.append(row)
                  y_flatten.append(y)
                  weights_flatten.append(w)
                  pack_ids.append(ct)
      else:
          for ct, (x, y) in enumerate(zip(xs, ys)):
              for row in x:
                  x_flatten.append(row)
                  y_flatten.append(y)
                  pack_ids.append(ct)
  
      ret = xgb.DMatrix(np.array(x_flatten), y_flatten)
      if weights is not None:
          ret.set_weight(weights_flatten)
      dmatrix_context.set("pack_ids", ret, np.array(pack_ids))
      dmatrix_context.set("group_sizes", ret, group_sizes)
      return ret
  
imports: import multiprocessing
  import logging
  from typing import Dict
  from collections import defaultdict
  import numpy
  from tvm.autotvm.tuner.metric import max_curve
  from .cost_model import PythonBasedModel
  from ..feature import get_per_store_features_from_measure_pairs, get_per_store_features_from_states
  from ..measure_record import RecordReader
label_desc: Use image node_id 2 for calling a global function with example usage: pack_sum_xgbmatrix(xs, ys, gids, weights) and returns: ret