RecML: High-Performance Recommender Library

repository·main·Indexed 42 days ago

https://github.com/ai-hypercomputer/recml

A high-performance library for large-scale deep learning recommendation systems optimized for Google Cloud TPUs (using SparseCore acceleration) and GPUs. RecML provides reference implementations for state-of-the-art models including SASRec, BERT4Rec, Mamba4Rec, HSTU, and DLRM v2, featuring a unified trainer for Keras and JAX and specialized Embedding APIs for massive datasets.

Tokens
3.3K
Snippets
7
Records
13
Agent score
53%

What's inside RecML

  1. Overview of RecML: High-Performance Recommender Library

    main

    RecML is a high-performance, large-scale deep learning recommender system library. It is specifically optimized for Cloud TPUs (leveraging SparseCore acceleration) and Cloud GPUs, making it suitable for training and serving massive models with large embedding tables on datasets containing millions or billions of items/users.

    Key capabilities include:

    • Scalable Architecture: Support for massive datasets via efficient data loading (e.g., tf.data) and sharding/SPMD.
    • State-of-the-Art Models: Reference implementations for ranking, retrieval, and sequential recommendation tasks.
    • Reusable Building Blocks: Access to common recommendation layers (e.g., DCN, BERT4Rec), specialized Embedding APIs (JAX Embedding API for SparseCore), standardized metrics (AUC, NDCG@K, etc.), and common loss functions.
    • Unified Trainer: A high-level abstraction for training and evaluation that targets different hardware (TPU/GPU) and frameworks (primarily Keras and JAX).
  2. BERT4Rec: Bidirectional Transformer for Sequential Recommendation

    main

    BERT4Rec uses a transformer-based approach to model user preferences by looking at action sequences in both directions (bidirectional). It is trained using a mask prediction objective where random items are masked and the model learns to predict them based on context.

    Architecture Components:

    • Embedding Layer: Converts item IDs to dense vectors with optional type embeddings and absolute positional embeddings. It uses a separate embedding for masked features to prevent other tokens from attending to them.
    • Multi-Head Self-Attention Layer: Computes attention scores using a separate embedding for masked features.
    • Feed-Forward Network: Two linear layers with GeLU activation.
    • Residual Connections and Post-Layernorm: Applied around self-attention and feed-forward sub-layers.
    • Masked Prediction Head: Projects masked sequence embeddings and decodes them using the item embedding layer, computing categorical cross entropy loss against masked item IDs.
  3. SASRec: Self-Attentive Sequential Recommendation

    main

    SASRec uses self-attention to predict a user's next action based on past activities. It is designed to balance long-term user interests with recent actions by adapting focus based on history length. It is built with efficient attention blocks, avoiding RNN or CNN structures for faster training.

    Architecture Components:

    • Embedding Layer: Converts item IDs to dense vectors with learnable absolute positional embeddings and dropout.
    • Multi-Head Self-Attention Layer: Computes attention scores within a sequence window using causal masking to prevent information leakage.
    • Feed-Forward Network: Two linear layers with GeLU activation applied to each embedding.
    • Residual Connections and Pre-Layernorm: Applied around self-attention and feed-forward sub-layers for stability.
    • Prediction Head: Decodes sequence embeddings into logits using the input item embedding table and computes causal categorical cross entropy loss.
  4. HSTU: Hierarchical Sequential Transducer Unit

    main

    HSTU is a generative modeling architecture designed for high-cardinality, non-stationary streaming data. It treats recommendation as a sequential transduction task and is highly scalable.

    Architecture Components:

    • Embedding Layer: Converts action tokens into dense vectors; optionally includes learnable absolute positional embeddings.
    • Gated Pointwise Aggregated Attention: Uses a multi-head gated pointwise attention mechanism with Layernorm on outputs to capture interaction intensity.
    • Relative Attention Bias: Uses T5-style relative attention bias based on positions and timestamps.
    • Residual Connections and Pre-Layernorm: Applied around pointwise attention blocks.
    • No Feedforward Network: Unlike other models, the standard feedforward network is removed.
    • Prediction Head: Decodes sequence embeddings into logits using separately learned weights and computes causal categorical cross entropy loss.
  5. Mamba4Rec: Linear Recurrent Mamba 2 for Recommendation

    main

    Mamba4Rec implements a linear recurrent Mamba 2 architecture. It scales better on long sequences than attention-based methods due to its linear complexity (vs quadratic) and matches the quality of attention models while being more efficient in training and inference.

    Architecture Components:

    • Embedding Layer: Converts item IDs to dense vectors. No positional embedding is used as the recurrent nature of Mamba provides positional inductive bias.
    • Mamba SSD: Computes causal interactions using the Mamba state space duality algorithm.
    • Feedforward Network: Two linear layers with GeLU activation applied to each embedding.
    • Residual Connections and Post-Layernorm: Applied around the Mamba and feed-forward sub-layers.
    • Prediction Head: Decodes sequence embeddings into logits using the input item embedding table and computes causal categorical cross entropy loss.
  6. Build a custom Docker image for RecML training

    main

    You can build a custom Docker image using the Dockerfile provided in the repository root. This is useful if you want to manage your own environment while using the RecML configuration.

    Note: You must have the jax-tpu-embedding wheel available locally to include it in your build. You can obtain the wheel from the jax-ml/jax-tpu-embedding repository.

    docker build -t recml-training .
  7. Set up a development environment using a Virtual Environment

    main

    For development on a TPU VM, it is recommended to use a Python virtual environment to prevent conflicts with system-level packages.

    Prerequisites:

    • Python 3.12 or higher.

    Steps:

    1. Create the virtual environment from the repository root.
    2. Activate the environment.
    3. Install the required dependencies and the specific jax-tpu-embedding wheel.
    4. Crucial: Force a specific version of protobuf using the --no-deps flag to ensure compatibility with the TPU stack and prevent accidental downgrades by other libraries.
    # 1. Create and Activate
    python3 -m venv venv
    source venv/bin/activate
    
    # 2. Install Dependencies
    pip install ./jax_tpu_embedding-0.1.0.dev20260226-cp312-cp312-manylinux_2_31_x86_64.whl
    pip install -r requirements.txt
    
    # 3. Force Protobuf version (Required)
    pip install "protobuf>=6.31.1" --no-deps
    
    # 4. Run DLRM training
    python dlrm_experiment_test.py
  8. Run RecML training using the official Docker image

    main

    For production or simplified deployment, use the pre-built image from Dockerhub. This image is optimized for running RecML algorithms on V6 and V7 TPUs and includes all necessary dependencies.

    Image Name: docker.io/recsyscmcs/recml-tpu:v1.0.0

    Execution: Run the container with --privileged, --net=host, and --ipc=host flags to ensure the container has the necessary access to TPU hardware and host networking.

    docker run --rm --privileged \
      --net=host \
      --ipc=host \
      --name recml-experiment \
      docker.io/recsyscmcs/recml-tpu:v1.0.0
  9. Launch a TPU VM for RecML benchmarks

    main

    Use the gcloud CLI to create a queued TPU resource. Ensure you have exported the required environment variables (TPU_NAME, QR_NODE_NAME, PROJECT, ZONE, ACCELERATOR_TYPE, and RUNTIME_VERSION) before running this command.

    gcloud alpha compute tpus queued-resources create ${TPU_NAME} --node-id ${QR_NODE_NAME}$ --project ${PROJECT} --zone ${ZONE} --accelerator-type ${ACCELERATOR_TYPE} --runtime-version ${RUNTIME_VERSION}
  10. Setup environment for RecML Inference benchmarks

    main

    Before launching a TPU VM or running benchmarks, you must export the following environment variables to configure your Google Cloud project and TPU resource details:

    • TPU_NAME: The name of the TPU resource.
    • QR_NODE_NAME: The node ID for the queued resource.
    • PROJECT: Your Google Cloud project ID.
    • ZONE: The zone where the TPU will be located.
    • ACCELERATOR_TYPE: The type of TPU accelerator.
    • RUNTIME_VERSION: The runtime version for the TPU VM.
    export TPU_NAME=
    export QR_NODE_NAME=
    export PROJECT=
    export ZONE=
    export ACCELERATOR_TYPE=
    export RUNTIME_VERSION=
  11. Install dependencies on TPU VM

    main

    Follow these steps via gcloud alpha compute tpus tpu-vm ssh to prepare the TPU environment for RecML benchmarks. These commands are executed on all workers simultaneously.

    1. Clone the RecML repository
    2. Install requirements from requirements.txt
    3. Install JAX and JAXLib nightly (including libtpu and requests)
    4. Install JAX Sparsecore (jax-tpu-embedding)
    5. Install additional dependencies (tensorflow, dm-tree, flax, google-metrax)
    # Clone the RecML repository
    gcloud alpha compute tpus tpu-vm ssh ${TPU_NAME} --project ${PROJECT} --zone ${ZONE} --worker=all  --command="git clone https://github.com/AI-Hypercomputer/RecML.git"
    
    # Install requirements
    gcloud alpha compute tpus tpu-vm ssh ${TPU_NAME} --project ${PROJECT} --zone ${ZONE} --worker=all  --command="cd RecML && pip install -r requirements.txt" 
    
    # Install jax and jaxlib nightly
    gcloud alpha compute tpus tpu-vm ssh ${TPU_NAME} --project ${PROJECT} --zone ${ZONE} --worker=all  --command="pip install -U --pre jax jaxlib libtpu requests -f https://storage.googleapis.com/jax-releases/jax_nightly_releases.html -f https://storage.googleapis.com/jax-releases/libtpu_releases.html --force"
    
    # Install JAX Sparsecore (jax-tpu-embedding)
    gcloud alpha compute tpus tpu-vm ssh ${TPU_NAME} --project ${PROJECT} --zone ${ZONE} --worker=all  --command="pip install -U https://storage.googleapis.com/jax-tpu-embedding-whls/20250604/jax_tpu_embedding-0.1.0.dev20250604-cp310-cp310-manylinux_2_35_x86_64.whl --force"
    
    # Install other dependencies
    gcloud alpha compute tpus tpu-vm ssh ${TPU_NAME} --project ${PROJECT} --zone ${ZONE} --worker=all  --command="pip install -U tensorflow  dm-tree flax google-metrax"
  12. Run RecML Inference benchmark workload

    main

    To execute a specific benchmark, you must replace <MODEL_NAME> and <TASK_NAME> with the appropriate directory and script names within the recml/inference/benchmarks/ path. The command makes the script executable and runs it while passing the TPU_NAME environment variable.

    # Replace <MODEL_NAME> and <TASK_NAME> with actual values
    gcloud alpha compute tpus tpu-vm ssh ${TPU_NAME} --project ${PROJECT} --zone ${ZONE} --worker=all  --command="cd RecML && chmod +x ./recml/inference/benchmarks/<MODEL_NAME>/<TASK_NAME> && TPU_NAME=${TPU_NAME} ./recml/inference/benchmarks/<MODEL_NAME>/<TASK_NAME>"