Skip to content

Adding UniSRec model implemented on lightweight class hierarchy with pytorch preprocessing #306

Open
TOPAPEC wants to merge 7 commits intoMTSWebServices:mainfrom
TOPAPEC:feat/unisrec-model
Open

Adding UniSRec model implemented on lightweight class hierarchy with pytorch preprocessing #306
TOPAPEC wants to merge 7 commits intoMTSWebServices:mainfrom
TOPAPEC:feat/unisrec-model

Conversation

@TOPAPEC
Copy link
Copy Markdown

@TOPAPEC TOPAPEC commented Apr 24, 2026

New rectools.fast_transformers module — standalone transformer sequential recommenders that work with raw torch tensors, without going through Dataset/pandas.

  • GPU-native preprocessing. build_sequences() builds left-padded interaction sequences entirely in torch (argsort + scatter). On ML-20M (20M interactions) this takes 0.5s vs 14.6s for the pandas-based SASRecDataPreparator — roughly 30x faster.

  • FlatSASRec. Pre-norm SASRec encoder with plain id-embeddings, no ItemNet hierarchy. Wraps into FlatSASRecModel (inherits ModelBase) so it plugs into standard RecTools fit/recommend.

  • UniSRec. Three-phase sequential recommender with pretrained text embeddings and a learnable PCA adaptor:

    1. SASRec warm-up on ID embeddings (transformer + item_emb)
    2. Adaptor-only training (transformer frozen, pretrained embeddings)
    3. Full fine-tune (adaptor + transformer on pretrained embeddings)

    UniSRecModel.fit(user_ids, item_ids, timestamps) takes raw tensors end-to-end. Supports softmax/BCE/gBCE/sampled_softmax losses, Adam/AdamW, cosine warmup scheduler, gradient clipping, early stopping, checkpoint save/load. FFN blocks are configurable (conv1d, linear_gelu, linear_relu).

  • rank_topk() — batched top-k with CSR viewed-item filtering and whitelist support.

Benchmark (ML-20M, 10 epochs, softmax, Adam, n_factors=256)

SASRec UniSRec ID
Preprocessing 14.6s 0.5s
Training 911.8s 639.5s
Evaluation (138K users) 175.6s 28.0s
Total 1102s 668s
HR@10 NDCG@10 MRR@10
SASRec 0.2417 0.1410 0.1103
UniSRec ID 0.2528 0.1495 0.1179

UniSRec ID: +4.6% HR@10, +6.0% NDCG@10, 1.65x faster overall.

New files

Source (9 modules, 1683 lines):

  • rectools/fast_transformers/gpu_data.pybuild_sequences, align_embeddings, GPUBatchDataset, make_dataloader
  • rectools/fast_transformers/net.pyFlatSASRec, SASRecBlock
  • rectools/fast_transformers/lightning_wrap.pyFlatSASRecLightning
  • rectools/fast_transformers/model.pyFlatSASRecModel, FlatSASRecConfig
  • rectools/fast_transformers/ranking.pyrank_topk
  • rectools/fast_transformers/unisrec_net.pyUniSRec, FeedForward, make_ffn
  • rectools/fast_transformers/unisrec_lightning.pyUniSRecLightning, loss/optimizer/scheduler dispatch
  • rectools/fast_transformers/unisrec_model.pyUniSRecModel (three-phase fit, checkpoint)

Tests (143 tests, 1920 lines):

  • tests/fast_transformers/test_gpu_data.py — sequence building, alignment, dataset/dataloader
  • tests/fast_transformers/test_net.py, test_lightning_wrap.py, test_model.py — FlatSASRec stack
  • tests/fast_transformers/test_unisrec_net.py, test_unisrec_lightning.py, test_unisrec_model.py — UniSRec stack
  • tests/fast_transformers/test_ranking.py — top-k, filtering, edge cases

Scripts:

  • scripts/compare_sasrec_unisrec.py — full benchmark with markdown report generation
  • scripts/comparison_report.md — benchmark results

Test plan

  • All 143 tests pass (pytest tests/fast_transformers/ -q)
  • Run on GPU to confirm CUDA path works
  • Verify FlatSASRecModel fit/recommend through the standard RecTools API on a small dataset

TOPAPEC and others added 4 commits April 22, 2026 18:28
Standalone sequential recommender package, mimics ModelBase interface
without touching existing rectools code.

FlatSASRec - plain ID-embedding SASRec encoder.
UniSRec - pretrained text embeddings + PCA/BN adaptor, 3-phase training
(ID emb -> adaptor only -> full finetune).

Uses lightweight rank_topk instead of TorchRanker, reuses
SASRecDataPreparator for the data pipeline.

30 tests, smoke scripts for both models.

Fix: NaN*0=NaN in IEEE 754 breaks attention padding masking via
multiplication, switched to masked_fill.
New config options:
- ffn_type: conv1d / linear_gelu / linear_relu + ffn_expansion
- optimizer: adam / adamw
- scheduler: cosine_warmup (with warmup_ratio, min_lr_ratio)
- loss: softmax / BCE / gBCE / sampled_softmax (with gbce_t)
- patience: early stopping via EarlyStopping callback + val split
- data_preparator: accept custom preparator instance

31 tests passing.
@TOPAPEC TOPAPEC changed the title Feat/unisrec model Adding UniSRec model implemented on lightweight class hierarchy with pytorch preprocessing Apr 24, 2026
TOPAPEC added 3 commits April 24, 2026 22:17
- Add hash-based ID mapping (splitmix64) as alternative to dense
  torch.unique mapping in build_sequences and align_embeddings.
- Add UniSRecModel.export_to_onnx() for native ONNX export of
  encoder and item embeddings (project_all).
- Add UniSRecModel.map_item_ids() for external→internal ID conversion
  at inference time (works for both dense and hash modes).
- Remove FlatSASRecModel/FlatSASRecLightning (RecTools-coupled wrappers
  that duplicated UniSRecModel functionality).
- Add tests: hash mapping (including string-derived IDs),
  ONNX export roundtrip, map_item_ids for both modes.
@TOPAPEC TOPAPEC force-pushed the feat/unisrec-model branch from 2e923df to d68834f Compare April 24, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant