{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Using rmm with Dask\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import dask\nfrom dask.distributed import Client\nfrom dask_cuda import LocalCUDACluster\nfrom sklearn.datasets import make_classification\n\nimport xgboost as xgb\n\n\ndef main(client):\n    # Optionally force XGBoost to use RMM for all GPU memory allocation, see ./README.md\n    # xgb.set_config(use_rmm=True)\n\n    X, y = make_classification(n_samples=10000, n_informative=5, n_classes=3)\n    # In pratice one should prefer loading the data with dask collections instead of\n    # using `from_array`.\n    X = dask.array.from_array(X)\n    y = dask.array.from_array(y)\n    dtrain = xgb.dask.DaskDMatrix(client, X, label=y)\n\n    params = {\n        \"max_depth\": 8,\n        \"eta\": 0.01,\n        \"objective\": \"multi:softprob\",\n        \"num_class\": 3,\n        \"tree_method\": \"hist\",\n        \"eval_metric\": \"merror\",\n        \"device\": \"cuda\",\n    }\n    output = xgb.dask.train(\n        client, params, dtrain, num_boost_round=100, evals=[(dtrain, \"train\")]\n    )\n    bst = output[\"booster\"]\n    history = output[\"history\"]\n    for i, e in enumerate(history[\"train\"][\"merror\"]):\n        print(f\"[{i}] train-merror: {e}\")\n\n\nif __name__ == \"__main__\":\n    # To use RMM pool allocator with a GPU Dask cluster, just add rmm_pool_size option\n    # to LocalCUDACluster constructor.\n    with LocalCUDACluster(rmm_pool_size=\"2GB\") as cluster:\n        with Client(cluster) as client:\n            main(client)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.17"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}