NVTabular Documentation

repository·main·Indexed 22 days ago

https://github.com/nvidia-merlin/nvtabular

A feature engineering and preprocessing library for tabular data designed for terabyte-scale datasets. Part of the NVIDIA Merlin framework, NVTabular accelerates computation on GPUs using RAPIDS Dask-cuDF. It provides a graph-based approach to pipelines using Workflow and WorkflowNode, along with a comprehensive suite of operators for categorical, continuous, missing value, and row manipulation, as well as specialized schema operators for recommender systems.

Tokens
15.4K
Snippets
43
Records
64
Agent score
28%

What's inside NVTabular

  1. Accelerated Training with NVIDIA Merlin

    main

    NVIDIA Merlin provides highly optimized dataloaders designed to overcome the unique challenges of recommendation system datasets, which are often terabytes in size with billions of examples. Standard PyTorch and TensorFlow dataloaders can be slow due to random sampling; Merlin's dataloaders can speed up TensorFlow pipelines by up to 9x.

    For extreme performance and large-scale embedding tables, Merlin offers three accelerated dataloading paths:

    1. TensorFlow: Optimized dataloaders for TensorFlow-based pipelines.
    2. PyTorch: Optimized dataloaders for PyTorch-based pipelines.
    3. HugeCTR: A dedicated deep learning framework for recommender systems that can achieve up to 13x speedups and supports model parallel scaling for embedding tables that exceed a single GPU's memory by distributing them across multiple GPUs or nodes.
  2. Accelerated Training with HugeCTR

    main

    HugeCTR is a highly optimized deep learning framework written in CUDA C++ for recommender systems. It is designed to handle massive embedding tables (100GB to 1TB) that exceed single GPU memory by scaling across multiple GPUs and nodes.

    Key features include:

    • Model oversubscription: Prefetches only required embeddings from a parameter server per batch, allowing training on single nodes where tables exceed GPU/CPU memory.
    • Optimized Data Pipeline: Asynchronous, multithreaded data pipelines and a highly optimized data loader.
    • Architectures: Built-in support for common architectures like Wide&Deep and DLRM.
    • Data Formats: Supports Parquet and binary formats.
    • Configuration: Configurable via JSON or the Python API.
  3. How to define a preprocessing pipeline using ColumnGroups and Operators

    main

    NVTabular pipelines are defined as directed acyclic graphs (DAGs) of operators applied to ColumnGroup objects. A ColumnGroup is essentially a list of column name strings.

    You define a pipeline by using the overloaded operator >> to chain operators to a ColumnGroup. When you chain an operator, it is applied to every column in that group, and the result is a new ColumnGroup that can be further chained.

    Example of chaining:

    CONT_COLUMNS = ['col1', 'col2']
    # Chaining multiple operators to a ColumnGroup
    cont_features = CONT_COLUMNS >> ops.FillMissing() >> ops.Normalize()
    CONT_COLUMNS = ['col1 name', 'col2 name', ...]
    cont_features = CONT_COLUMNS >> <op1> >> <op2> >> ...
  4. Shuffle Datasets during Creation

    main
    NVTabular allows shuffling during dataset creation. This creates a uniformly shuffled dataset, enabling the dataloader to read large contiguous chunks of data that are already randomized. This is particularly critical for datasets that exceed CPU memory, as it allows for efficient epoch-level shuffling without the massive performance penalty of a full shuffle during training.
  5. Understand Operator Base Classes

    main

    All operators in NVTabular inherit from a base class hierarchy in nvtabular.ops:

    • Operator: The base class for all transformations.
    • StatOperator: A specialized base class for operators that require computing statistics (like mean or median) from the data before applying the transformation.
  6. Handle Multi-Hot Encoding and Pre-Existing Embeddings

    main

    NVTabular supports processing datasets with multi-hot categorical columns and continuous vector features (like pre-trained embeddings) using list columns.

    Categorical Multi-Hot

    Operators like Categorify and HashBucket can map list columns to small contiguous integers suitable for embedding lookup tables. For example, [['comedy', 'horror'], ['comedy', 'sciencefiction']] can be transformed into [[0, 1], [0, 2]].

    Framework Support

    • TensorFlow: The KerasSequenceLoader transforms list columns into two tensors (values and offsets). These can be converted to RaggedTensors. Use the nvtabular.framework_utils.tensorflow.layers.DenseFeatures Keras layer to automatically handle these conversions.
    • PyTorch: The nvtabular.framework_utils.torch.models.Model class supports multi-hot columns by internally utilizing the PyTorch EmbeddingBag layer.
  7. How NVTabular Operations work

    main

    NVTabular operations (ops) are the building blocks of preprocessing and feature engineering pipelines. They are designed to handle large-scale GPU compute via the RAPIDS Dask-cuDF library.

    Operations are split into two distinct phases:

    1. Statistics Gathering: The first phase where operations that require global context (crossing row boundaries) occur. For example, a Normalize op must first calculate the mean and standard deviation across the entire dataset.
    2. Transform: The second phase where the calculated statistics are used to modify the dataset. This phase can be used during dataset modification or during dataloading and inference.

    There are two primary types of operators:

    • Base Operator: Transforms columns using a transform method that processes a cuDF dataframe and a list of columns. It declares its output via output_columns_names and its requirements via dependencies.
    • StatOperator: A subclass of Base Operator that includes a fit method to calculate statistics, a finalize method to aggregate statistics from different Dask workers, and methods for serialization (save/load).
  8. Manage documentation source files and TOC

    main

    NVTabular uses Sphinx, which expects all source files to reside within docs/source.

    • Directory Copying: Non-source directories (like notebooks) are copied into docs/source based on the copydirs_additional_dirs list in docs/source/conf.py.
    • READMEs: When directories are copied, README.md files are renamed to index.md to comply with HTML web server expectations.
    • Table of Contents: Adding or removing files is not automatic. You must manually update docs/source/toc.yaml to reflect changes in the file structure. When adding notebooks, remember that paths in toc.yaml are relative to the docs/source/ directory.
  9. Construct Workflow pipelines with Workflow and WorkflowNode

    main
    NVTabular uses a graph-based approach to define data processing pipelines. You can construct these pipelines using Workflow and WorkflowNode from nvtabular.workflow.workflow. A Workflow typically represents the entire execution graph, while WorkflowNode represents individual steps or transformations within that graph.
  10. Set up JupyterLab in an NVTabular Docker container

    main

    To run the example notebooks using a Docker container, follow these steps:

    1. Pull and start the container: Use the following command to start the container with GPU support and necessary port mappings. Replace <docker container> with your chosen image (e.g., merlin-pytorch).

    2. Start JupyterLab: Once inside the container shell, launch the JupyterLab server.

    3. Access JupyterLab: Use the 127.0.0.1 URL provided in the terminal output to open the interface in your browser.

    4. Locate Notebooks: Navigate to the /nvtabular directory within JupyterLab to find the examples.

    # 1. Start the container
    docker run --gpus all --rm -it \
      -p 8888:8888 -p 8797:8787 -p 8796:8786 --ipc=host \
      <docker container> /bin/bash
    
    # 2. Start JupyterLab inside the container
    jupyter-lab --allow-root --ip='0.0.0.0'
  11. Run NVTabular examples using Docker containers

    main

    You can run NVTabular examples using pre-configured Docker containers from the NVIDIA GPU Cloud (NGC). Choose a container based on your preferred deep learning framework:

    • merlin-hugectr: Includes NVTabular with HugeCTR.
    • merlin-tensorflow: Includes NVTabular with TensorFlow.
    • merlin-pytorch: Includes NVTabular with PyTorch.

    Note: Since the 22.06 release, these containers include software for both training models and performing inference.

  12. Run NVTabular on Amazon Web Services (AWS)

    main

    To run NVTabular on AWS, use an EC2 instance with NVIDIA GPU support (e.g., p4d.24xlarge with 8x NVIDIA A100 GPUs). For optimal performance, create a RAID volume using local NVMe storage to host your datasets.

    Steps:

    1. Launch Instance: Start an EC2 instance using the NVIDIA Deep Learning AMI via aws-cli.
    2. Configure RAID: Create a RAID 0 volume using mdadm with your local NVMe devices (e.g., /dev/nvme1n1 and /dev/nvme2n1) and mount it to /mnt/raid.
    3. Prepare Data: Copy your dataset into the RAID directory (/mnt/raid/data/).
    4. Launch Container: Run the NVTabular Docker container, mounting the RAID volume to /raid inside the container.
    5. Start Jupyter: Launch jupyter-lab within the container to access the environment.
    # 1. Start the EC2 instance
    aws ec2 run-instances --image-id ami-04c0416d6bd8e4b1f --count 1 --instance-type p4d.24xlarge --key-name <MyKeyPair> --security-groups <my-sg>
    
    # 2. Create RAID volume
    sudo mdadm --create --verbose /dev/md0 --level=0 --name=MY_RAID --raid-devices=2 /dev/nvme1n1 /dev/nvme2n1
    sudo mkfs.ext4 -L MY_RAID /dev/md0
    sudo mkdir -p /mnt/raid
    sudo mount LABEL=MY_RAID /mnt/raid
    sudo chmod -R 777 /mnt/raid
    
    # 3. Copy dataset
    cp -r data/ /mnt/raid/data/
    
    # 4. Launch NVTabular Docker container
    docker run --gpus all --rm -it -p 8888:8888 -p 8797:8787 -p 8796:8786 --ipc=host --cap-add SYS_PTRACE -v /mnt/raid:/raid nvcr.io/nvidia/nvtabular:0.3 /bin/bash
    
    # 5. Start jupyter-lab
    jupyter-lab --allow-root --ip='0.0.0.0' --NotebookApp.token='<password>'