TorchEasyRec Documentation

repository·master·Indexed 19 days ago

https://github.com/alibaba/torcheasyrec

A PyTorch-based framework for building, training, and deploying large-scale deep learning recommendation models. It supports the full pipeline from candidate generation (matching) and scoring (ranking) to multi-task learning and generative recommendation. The framework includes over 20 models such as DSSM, DeepFM, and MMoE, and integrates with data sources like MaxCompute/ODPS, Parquet, and Kafka. It features distributed training via TorchRec, large embedding sharding, and deployment support for PAI-DLC, PAI-DSW, and EAS.

Tokens
78.2K
Snippets
168
Records
244
Agent score
65%

What's inside TorchEasyRec

  1. What is TorchEasyRec?

    master

    TorchEasyRec is a PyTorch-based recommendation system framework designed for production-ready deep learning models. It supports various recommendation tasks including:

    • Candidate Generation (Matching): Finding potential items for a user.
    • Scoring (Ranking): Ordering items based on predicted relevance.
    • Multi-Task Learning: Optimizing for multiple objectives simultaneously.
    • Generative Recommendation: Using generative models for recommendation tasks.

    The framework allows for efficient development through simple configuration and supports easy customization of models and features.

  2. What is SID RQVAE and how does it work?

    master

    RQ-VAE (Residual-Quantized Variational Auto-Encoder) is a Semantic ID (SID) generation model used as a tokenizer for generative recommendation. It quantizes item content or multi-modal embeddings into a sequence of discrete integer codes (code_0, code_1, ..., code_{n-1}).

    Core Architecture

    • Encoder MLP: Compresses input embeddings to an embed_dim latent vector.
    • Residual Vector Quantizer (RVQ): A multi-layer quantizer that maps residuals to the nearest entry in a learnable codebook (implemented as nn.Embedding) at each layer.
    • Decoder MLP: Reconstructs the original embedding using the sum of the quantized vectors from all layers.
    • Training: The codebook, encoder, and decoder are trained jointly. Gradients are passed through the non-differentiable quantization step using the Straight-Through Estimator (STE).

    Key Concepts

    • Semantic ID (SID): A tuple of the nearest neighbor indices from each residual codebook layer.
    • Contrastive Learning: Supports dual-view contrastive learning (similar to CLIP). By providing a paired embedding and an is_pair flag, the model applies an InfoNCE loss to ensure semantically similar views are mapped to similar SID spaces.
    • Comparison with RQKMeans: Unlike SID RQKMeans (which uses FAISS K-Means on CPU), RQVAE is a gradient-based model that supports multi-GPU training.
  3. What is UltraHSTU and its core optimizations

    master

    UltraHSTU is a long-sequence recommendation architecture that implements four orthogonal efficiency optimizations to reduce computational and memory overhead while maintaining or improving accuracy. It is built upon the DlrmHSTU architecture.

    Core Optimizations

    • Semi-Local Attention (SLA): Limits causal attention to a local window of sla_k1 tokens and a global prefix of sla_k2 tokens. This reduces complexity from $O(L^2)$ to $O(L \cdot K1) + O(L \cdot K2)$. Requires kernel: CUTLASS or kernel: PYTORCH.
    • Mid-stack Attention Truncation: Discards the UIH prefix after a specific layer (attn_truncation_split_layer) and keeps only the last attn_truncation_tail_len tokens for subsequent layers. This compresses KV cache and compute requirements.
    • Mixture of Transducers (MoT): Runs $N$ parallel HSTUTransducer channels (e.g., click stream, view stream). Each channel has its own STU stack and SLA/truncation settings. Outputs are concatenated across the channel dimension before being fed to a FusionMTLTower.
    • Selective Rematerialization: Reduces memory usage during backpropagation by recomputing intermediate tensors. Controlled by recompute_normed_x_in_backward and recompute_uvqk_in_backward (both default to true).
  4. What is SID RQKMeans and how does it work?

    master

    RQKMeans (Residual K-Means) is a Semantic ID (SID) generation model that quantizes item embeddings into a sequence of discrete codes (code_0, code_1, ..., code_{n-1}) for use in generative recommendation.

    Unlike RQVAE, RQKMeans uses FAISS K-Means to perform clustering on residuals layer-by-layer. The process is offline and one-time:

    1. Layer 0: Performs K-Means on the original embeddings to produce codebook_0 cluster centers.
    2. Subsequent Layers: Each sample subtracts its nearest center from the previous layer to get a residual. The next layer performs K-Means on these residuals.
    3. Result: The item's SID is the tuple of indices of the nearest centers at each layer.

    Key Advantages: Faster training than RQVAE and requires no gradient hyperparameter tuning. It is ideal for quickly producing codebooks when data is abundant.

  5. Understand the TorchEasyRec optimizer architecture

    master

    TorchEasyRec splits optimization into two distinct parts based on parameter types:

    1. sparse_optimizer: Responsible for optimizing sparse parameters, specifically those in the embedding layers.
    2. dense_optimizer: Responsible for optimizing dense parameters, specifically those in the neural network (nn) layers.

    Within the dense_optimizer, you can further specialize optimization for specific layers using part_optimizers via regular expression matching.

  6. Configure HSTU Match feature groups

    master

    HSTU Match relies on specific feature_groups identified by group_name. The user_tower and item_tower use these names to index their inputs.

    Required Groups:

    • uih: User interaction history sequence. Type: JAGGED_SEQUENCE. Mandatory.
    • candidate: Candidate item sequence (composed of positive and negative sampled items during training). Type: JAGGED_SEQUENCE. Mandatory.

    Optional/Conditional Groups:

    • contextual: User-side ID features (e.g., user_id). Type: DEEP.
    • uih_action: Sequence of user action events (e.g., click, buy). Type: JAGGED_SEQUENCE. Mandatory if uih_preprocessor.action_encoder is configured.
    • uih_watchtime: Sequence of user interaction durations. Type: JAGGED_SEQUENCE. Mandatory if action encoder requires watchtime.
    • uih_timestamp: Sequence of user interaction timestamps. Type: JAGGED_SEQUENCE. Mandatory if positional_encoder.use_time_encoding=true.
    • query_time: A scalar request time per row (must use same units as uih_timestamp). Type: DEEP. If configured, time encoding is calculated as ts_gap = query_time - behavior_timestamp. Otherwise, it defaults to the timestamp of the last UIH behavior.

    Note: group_name must remain unchanged as it is used for indexing by the towers.

  7. Use WIDE feature groups for Wide&Deep models

    master

    The WIDE group type is intended for models like Wide&Deep or DeepFM.

    Constraints:

    • feature_names must only contain non-sequential features (IdFeature, RawFeature, ComboFeature, etc.).
    • It cannot contain sequence_groups.
    • The embedding_dim is fixed at 4 and does not change based on the embedding_dim configuration in the feature group.

    Embeddings are retrieved from the EmbeddingGroup output dictionary using the group_name.

  8. Understand Feature Groups in TorchEasyRec

    master

    A feature_group is used to aggregate a set of features after Embedding Lookup. This allows models to retrieve a consolidated set of embeddings directly using a group_name from the EmbeddingGroup output dictionary.

    TorchEasyRec supports multiple feature groups, which can be configured using different group_type values: SEQUENCE, DEEP, and WIDE.

  9. Common configuration options for TorchEasyRec features

    master

    All feature types in TorchEasyRec share several common configuration parameters:

    • feature_name: The name of the feature or its output name.
    • embedding_dim: The dimension of the feature embedding. It should be a multiple of 4. A recommended heuristic is embedding_dim = 8 + x^{0.25}, where x is the number of unique values.
    • embedding_name: The name of the embedding. If multiple features need to share the same embedding parameters, set their embedding_name to the same value.
    • pooling: The pooling method for multi-value feature embeddings. Supported values: sum (default), mean.
    • init_fn: The initialization function for feature embeddings. Can be any standard torch.nn.init function (e.g., nn.init.uniform_, a=-0.01, b=0.01).
    • default_value: The default value for the feature. If set to "", no default is provided, and empty features will result in zero vectors. This value is applied before bucketize operations.
    • separator: The delimiter used for multi-value string inputs. Defaults to \x1d. Using ARRAY types is recommended for better performance.
    • fg_encoded_default_value: The default value for FG-encoded data when fg_mode=FG_NONE and data is not encoded using pai-fg.
    • trainable: Whether the Embedding Variable is trainable. Defaults to true.
    • stub_type: If true, the feature acts only as an intermediate result for FG and is not output as a feature. Note: Cannot be used when fg_mode=FG_NORMAL.
    • data_type: The data type for the EmbeddingTable. Supports FP32 (default) and FP16.
  10. Configure embedding_name_suffix for independent embedding tables

    master

    The embedding_name_suffix (optional) allows you to append a string to the embedding names within a feature group. This enables different feature groups to use the same underlying features while maintaining independent embedding tables, which is useful for multi-tower architectures.

    Suffix Behavior:

    • No suffix or empty: Features with the same embedding_name across different groups will share the same embedding table.
    • Same suffix across groups: Groups will still share the same embedding table.
    • Different suffixes across groups: Each group will use its own independent embedding table.
    • In WIDE groups: The final embedding table name follows the pattern <emb_name>_wide_<suffix>.
    • In DEEP groups with nested sequences: The suffix is automatically inherited by nested sequence_groups. However, a sequence_group can explicitly set its own embedding_name_suffix to override the parent's value.
  11. Use specialized base classes for Ranking, Multi-Task, or Matching

    master

    Instead of inheriting from the generic BaseModel, you can inherit from specialized classes to reduce boilerplate. For these classes, you often only need to override the predict function:

    • Ranking models: Inherit from tzrec.models.rank_model.RankModel.
    • Multi-task ranking models: Inherit from tzrec.models.multi_task_rank.MultiTaskRank.
    • Matching (Candidate Generation) models: Inherit from tzrec.models.match_model.MatchModel.