Xgb gpu batch iterator

From wenmin-wu/ds-skills · View interactive version

Xgb gpu batch iterator is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Use XGBoost DeviceQuantileDMatrix with a custom batch iterator to train on large datasets without exhausting GPU memory

How to use this skill

Skill file

XGBoost GPU Batch Iterator

Overview

When training XGBoost on GPU with data too large to fit in GPU memory, use DeviceQuantileDMatrix with a custom DataIter that streams batches. Each batch is loaded, quantized, then freed — only the quantile sketch stays in memory. Cuts GPU memory usage proportionally to batch count with minimal speed penalty.

Quick Start

import xgboost as xgb
import numpy as np

class BatchIterator(xgb.core.DataIter):
    def __init__(self, df, features, target, batch_size=256*1024):
        self.df = df
        self.features = features
        self.target = target
        self.batch_size = batch_size
        self.it = 0
        self.n_batches = int(np.ceil(len(df) / batch_size))
        super().__init__()
    
    def reset(self):
        self.it = 0
    
    def next(self, input_data):
        if self.it == self.n_batches:
            return 0
        a = self.it * self.batch_size
        b = min(a + self.batch_size, len(self.df))
        batch = self.df.iloc[a:b]
        input_data(data=batch[self.features], label=batch[self.target])
        self.it += 1
        return 1

# Usage
it = BatchIterator(train_df, features, 'target', batch_size=256*1024)
dtrain = xgb.DeviceQuantileDMatrix(it, max_bin=256)
model = xgb.train(params, dtrain, num_boost_round=2000)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.