> ## Documentation Index
> Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XGBoost

> Logging and Deploying XGBoost Models in TrueFoundry

We will need to know some information about the model you are logging to generate a deployment package.

* To load the model:
  * The serialization format (`joblib`,`cloudpickle`, `pickle` or`json`) and the model file name.
* To generate the inference script and wrap it around a model server:
  * The input and output schema of the inference method.\
    **NOTE** For XGBoost models we only support `predict` inference method name as of now.
* To deploy and run:
  * Python version along with pip package (numpy, xgboost) dependencies.

***

### **Log a deployable XGBoost Model**

Below is an example of logging a model trained using XGBoost:

<CodeGroup>
  ```python Python lines theme={"dark"}
  from truefoundry.ml import get_client, XGBoostFramework, xgboost_infer_schema

  import joblib
  import os
  import numpy as np
  from xgboost import XGBClassifier


  X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
  y = np.array([0, 0, 1, 1])
  clf = XGBClassifier()
  clf.fit(X, y)


  name = "my-xgboost-model"
  LOCAL_MODEL_DIR = f"{name}/"
  model_file_name = "xgboost-model.joblib"
  model_file_path = f"{name}/{model_file_name}"

  os.makedirs(LOCAL_MODEL_DIR, exist_ok=True)
  joblib.dump(clf, model_file_path)

  client = get_client()
  model = joblib.load(model_file_path)
  model_schema = xgboost_infer_schema(
      model_input=X, model=model,
  )
  model_version = client.log_model(
      ml_repo="project-classification",
      name="my-xgboost-model",
      description="A simple xgboost model",
      model_file_or_folder=model_file_path,
      framework=XGBoostFramework(
          model_filepath=model_file_name,
          serialization_format="joblib",
          model_schema=model_schema,
      ),
  )
  ```
</CodeGroup>

* View and manage recently logged models in the ML Repos.

<Frame caption="">
  <img src="https://mintcdn.com/truefoundry/jw406UAsc7ErYUq8/images/a886a541-81c3542d1264f8fa538303208155f4497517d370783d1f73fa0331d0f2c21846-image.png?fit=max&auto=format&n=jw406UAsc7ErYUq8&q=85&s=208cefcd2ec184ec3f2b35b1ca4b348b" width="3014" height="936" data-path="images/a886a541-81c3542d1264f8fa538303208155f4497517d370783d1f73fa0331d0f2c21846-image.png" />
</Frame>

* Access framework details like serialization format, model schema, and inference method.

<Frame caption="">
  <img src="https://mintcdn.com/truefoundry/4MAaF__cLD4iud16/images/4f383b9b-64ddb6d1e92c93da3b528d060f5dca095a162fb77146952155a00d4d6946407f-image.png?fit=max&auto=format&n=4MAaF__cLD4iud16&q=85&s=04822a7bb3cebf4f09b118fd50de5d67" width="3012" height="1536" data-path="images/4f383b9b-64ddb6d1e92c93da3b528d060f5dca095a162fb77146952155a00d4d6946407f-image.png" />
</Frame>

* Access environment details like the Python version and pip packages list required for a specific model version.

<Frame caption="">
  <img src="https://mintcdn.com/truefoundry/JFTbQOWMkMfvFjDC/images/b65d7662-bd9055b6ddc16dd1f35e1732a9ee1e216b1804723b1083638ded0d9983dda171-image.png?fit=max&auto=format&n=JFTbQOWMkMfvFjDC&q=85&s=cb30ccea5d0ba12ae9d5733257418712" width="3012" height="1536" data-path="images/b65d7662-bd9055b6ddc16dd1f35e1732a9ee1e216b1804723b1083638ded0d9983dda171-image.png" />
</Frame>

***

### **Deploy the model**

Once the model is deployable, you can start the deployment flow directly using the CLI.

**Navigate to the Model Registry**

* Locate the desired model in the list and click on the **Deploy** button

<Frame caption="">
  <img src="https://mintcdn.com/truefoundry/DdP_2rhue4AQQlob/images/3ea9a98d-72d799915248b761330b0a2559491e473676755621f807dc67b334b22802bec8-image.png?fit=max&auto=format&n=DdP_2rhue4AQQlob&q=85&s=ce611ff89f51e51355d0c4a667799442" width="3012" height="580" data-path="images/3ea9a98d-72d799915248b761330b0a2559491e473676755621f807dc67b334b22802bec8-image.png" />
</Frame>

* Select the **workspace** for deployment, then click the **copy** icon to use the generated CLI command and initialize the model deployment package.

<Frame caption="">
  <img src="https://mintcdn.com/truefoundry/PSBc0bX31_cIC7pm/images/da01fbbe-057e8afe5728521fa3c46d2b5af232ebc8221fa29047752491e75132e87e1477-image.png?fit=max&auto=format&n=PSBc0bX31_cIC7pm&q=85&s=23d10d082621b4c11cfea1834f19a2c2" width="2996" height="918" data-path="images/da01fbbe-057e8afe5728521fa3c46d2b5af232ebc8221fa29047752491e75132e87e1477-image.png" />
</Frame>

***

### Common Model Deployment Issues and Troubleshooting Guide

* **Fix for Incomplete Model Manifest and make an existing logged model deployable**

  <Info>
    ### ❕Deploying a logged model may fail due to an incomplete model manifest, causing errors like:

    -`Model framework is not supported for deployment`\
    -`Model filename not found, please save model filename while logging the model`\
    -`Model schema not found, please save schema while logging the model`\
    -`Serialization format not found, please save serialization format while logging the model`
  </Info>

  Here’s an example code snippet to resolve the Incomplete Model Manifest by adding the required fields and updating the model version:

  <CodeGroup>
    ```python Python lines theme={"dark"}
    from truefoundry.ml import get_client, ModelVersionEnvironment, XGBoostFramework, xgboost_infer_schema
    import joblib
    import numpy as np

    # Replace with your model version FQN
    model_version_fqn = "model:truefoundry/project-classification/my-xgboost-model:1"

    client = get_client()
    model_version = client.get_model_version_by_fqn(model_version_fqn)
    model_version.download(path=".")

    # Replace with your model file path
    model_file_path = "./xgboost-model.joblib"
    model = joblib.load(model_file_path)

    # Update the model input example as per your model
    X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
    model_schema = xgboost_infer_schema(model_input=X, model=model)

    # To make the model deployable and generate the inference script, model file, and schema(with the method name) are required.
    model_version.framework = XGBoostFramework(
        model_filepath="xgboost-model.joblib",
        serialization_format="joblib",
        model_schema=model_schema,
    )
    model_version.environment = ModelVersionEnvironment(
        python_version="3.11",
        pip_packages=[
            "joblib==1.4.2",
            "numpy==1.26.4",
            "pandas==2.1.4",
            "xgboost==2.1.3",
        ],
    )
    model_version.update()
    ```
  </CodeGroup>

***

* `Python version < 3.8 and > 3.12 is not supported for Triton deployment`

  The Triton deployment depends on the nvidia-pytriton library ([https://pypi.org/project/nvidia-pytriton/](https://pypi.org/project/nvidia-pytriton/)) which supports Python versions >=3.8 and \<=3.12. If you need to use a version outside this range, consider using FastAPI as an alternative framework for serving the model.

***

* `Numpy version must be specified for Triton deployment`,`Numpy version must be less than 2.0.0 for Triton deployment`

  The nvidia-pytriton library specifies in its `pyproject.toml` file that it does not support numpy versions >=2.0. This limitation has been confirmed through practical experience. If you need to use a version outside this range, consider using FastAPI as an alternative framework for serving the model.

***
