KuaiFlow is a reproducible, multi-stage short-video recommendation project built on the KuaiRand dataset. The five-week offline prototype includes implicit-feedback baselines, two-tower retrieval, multi-task ranking, an exposure calibration audit, diversity-aware reranking, and a local recommendation API. ItemCF remains the strongest measured click-ranking baseline; the full neural path is also runnable.
Read the final five-week project report for the consolidated architecture, experimental findings, serving verification, limitations, and next steps.
Many portfolio recommenders stop at a MovieLens notebook. KuaiFlow is organized around the components and evaluation questions of a production recommender:
- candidate retrieval;
- multi-objective ranking;
- diversity-aware reranking;
- robustness under a different exposure policy.
Week one establishes the data and evaluation foundation with Popularity, ItemCF, and Bayesian Personalized Ranking (BPR) baselines.
- Strict chronological train/validation/test split.
- Random-exposure calibration and a future, time-aligned exposure-robustness audit.
- Popularity and ItemCF baselines.
- BPR matrix factorization implemented from scratch in NumPy.
- Feature- and history-aware two-tower retrieval.
- Exact and FAISS approximate-nearest-neighbor retrieval evaluation.
- Single-task DeepFM click ranking over fixed retrieved candidates.
- DeepFM + MMoE ranking with eight action targets, watch time, and completion.
- DIN and DIN + MMoE with causal target-aware click histories and a four-model comparison.
- Frozen-model probability calibration, reliability diagrams, exposure diagnostics, and paired user-bootstrap intervals on standard and randomized test impressions.
- Recall@K, HitRate@K, NDCG@K, and catalog coverage.
- Explicit novel-item, warm-start evaluation rather than mixing in impossible cold-start targets.
- Validation-selected author/tag MMR reranking with measured relevance costs.
- Local HTTP serving for ItemCF and hybrid retrieval → DIN attention → MMR, with verified artifacts, request validation, and exact offline/HTTP replay.
- Deterministic toy demo and unit tests.
configs/ Experiment configuration
data/raw/ Downloaded KuaiRand-Pure files (not tracked)
data/processed/ Chronological data splits (not tracked)
artifacts/ Benchmark results (not tracked)
src/kuaiflow/ Data, models, metrics, and CLI
tests/ Unit and smoke tests
The work is organized as a sequence of milestones. Week 1 establishes the data, evaluation, and baseline foundation. Week 2 builds learned retrieval and scalable nearest-neighbor search. Week 3 reaches the first major end-to-end milestone by connecting those candidates to trained ranking models.
- Week 1 — Recommendation baselines: chronological data preparation, warm-start evaluation, Popularity, ItemCF, and BPR.
- Week 2 — Two-tower retrieval: feature and history ablations, followed by exact-versus-FAISS latency and fidelity evaluation.
- Week 3 — DeepFM, DIN, and MMoE ranking: four ranking models, ten jointly trained objectives, and a measured fixed-candidate retrieval-to-ranking handoff. See the full Week 3 experimental report.
- Week 4 — Exposure robustness and calibration: frozen Week 3 checkpoints, standard versus early-random Platt calibration, and aligned future tests. See the Week 4 experimental report.
- Week 5 — Diversity reranking and serving: fixed-candidate MMR, local JSON API, replay and latency checks, and the five-week completion report.
Week 1 creates the reproducible evaluation pipeline and establishes three implicit-feedback baselines. See the Week 1 implementation plan and full experimental report.
All models are evaluated at (K=20) on 5,000 users. The evaluation uses novel warm-start positives and a training catalog containing 7,538 videos. Users with no positive training interactions are retained and reported separately as the zero_positive group.
Higher values are better for every metric.
| Split | Model | Recall@20 ↑ | HitRate@20 ↑ | NDCG@20 ↑ | Coverage@20 ↑ | Unique Items |
|---|---|---|---|---|---|---|
| Validation | Popularity | 7.95% | 20.18% | 4.45% | 0.56% | 42 |
| Validation | BPR | 10.19% | 25.78% | 5.78% | 16.26% | 1,226 |
| Validation | ItemCF | 11.58% | 28.26% | 6.76% | 47.05% | 3,547 |
| Test | Popularity | 7.20% | 20.02% | 4.15% | 0.53% | 40 |
| Test | BPR | 9.99% | 25.52% | 5.45% | 18.16% | 1,369 |
| Test | ItemCF | 10.78% | 27.78% | 6.14% | 45.52% | 3,431 |
See the full Week 1 experimental report or continue to the Week 2 two-tower retrieval report.
- ItemCF performs best across all ranking and coverage metrics.
- On the test set, ItemCF improves Recall@20 by 49.7% and HitRate@20 by 38.8% relative to the popularity baseline.
- BPR consistently improves over popularity, but remains behind ItemCF under the initial untuned configuration.
- Popularity recommends only 40 distinct test items, while ItemCF reaches 3,431, demonstrating the importance of personalization for catalog coverage.
- Validation and test results are similar, suggesting that the model comparison is reasonably stable across the two future time periods.
Create a Python 3.10+ environment and install the package:
python -m venv .venv
source .venv/bin/activate
python -m pip install -e .Run the synthetic end-to-end demo and test suite:
kuaiflow demo
python -m unittest discover -s tests -vDownload and verify KuaiRand-Pure from the dataset's official Zenodo record:
kuaiflow download --config configs/week1.yamlPrepare the strict chronological splits:
kuaiflow prepare --config configs/week1.yamlRun every week-one baseline:
kuaiflow benchmark --config configs/week1.yamlResults are written to artifacts/week1_results.json and
artifacts/week1_results.csv.
To iterate more quickly, run only selected models:
kuaiflow benchmark --config configs/week1.yaml --models popularity itemcfWeek 2 follows the same chronological warm-start protocol established in Week 1, replacing hand-designed retrieval scores with a learned two-tower model. It then compares exact retrieval with FAISS Flat, IVF, and HNSW indexes. See the Week 2 implementation plan and full experimental report.
Install the optional CPU FAISS dependency before running a FAISS experiment:
python -m pip install -e ".[faiss]"Run the final feature- and history-aware model with the selected IVF index:
kuaiflow retrieval --config configs/week2_faiss_ivf.yaml --mode faissRun the exact, FAISS Flat, IVF, and HNSW latency/accuracy comparison. The model is trained once and reused by every retrieval backend:
OMP_NUM_THREADS=1 kuaiflow retrieval \
--config configs/week2_feature_history.yaml --mode tradeoffThe comparison is written to artifacts/faiss_tradeoff_analysis.json and
artifacts/faiss_tradeoff_comparison.csv.
In addition to downstream Recall/NDCG, the comparison directly measures ANN fidelity against exact retrieval: candidate Recall@K, rank-sensitive candidate NDCG@K, final-list overlap after filtering, and complete ordered-list match rate. The selected IVF-100/10 configuration recovers 86.49% of exact top-100 candidates while making candidate search about 2.8x faster.
The selected Week 2 run also persists its final ordered candidate lists as
data/processed/candidates/week2_feature_history_faiss_ivf_top100.csv.gz.
The path is declared by data.candidates_path in the selected configuration.
Each row contains the split, user ID, video ID, and one-based retrieval rank.
This generated dataset is the explicit retrieval-to-ranking handoff for Week 3;
the downstream ranker learns its own score rather than taking the retrieval
score as an input feature.
The latency comparison uses one warmup followed by five measured runs. It reports median and p95 latency at three matching boundaries for every backend:
search: precomputed user embeddings through candidate search;pipeline: candidate search plus seen-item filtering and fallback;end_to_end: user embedding generation through search and post-processing.
Index construction is reported separately because it is an offline operation.
Change latency_warmup_runs and latency_measured_runs under evaluation in
the YAML configuration to control the benchmark. OMP_NUM_THREADS=1 makes CPU
threading explicit and reproducible; use the same value whenever comparing
results from different runs.
KuaiFlow now runs one measured offline path from chronological impressions, through Week 2 two-tower/FAISS top-100 retrieval, into DeepFM + MMoE scoring and a final top-100 order using DeepFM, DIN, DeepFM + MMoE, or DIN + MMoE. The ranker preserves candidate membership: it improves the order of the 100 retrieved videos but cannot recover a video the retriever missed.
See the full Week 3 experimental report for the workflow, evaluation protocol, pointwise and candidate-ranking results, multi-objective analysis, coverage trade-off, and limitations.
Week 3 starts with a standalone DeepFM click model. It trains on logged impressions, then scores and reorders the selected Week 2 top-100 candidates without changing candidate membership. Its linear, FM, and deep branches are optimized jointly with one binary click loss. See the DeepFM guide for the model equation, leakage rules, and evaluation protocol.
OMP_NUM_THREADS=1 kuaiflow deepfm --config configs/week3_deepfm.yamlThe first full run reached 0.7195 test click AUC. Over the fixed candidates it improved test NDCG@20 by 19.2% and HitRate@20 by 40.2%, while reducing Coverage@20 from 84.1% to 38.7%; the full trade-off is reported in the guide.
The next saved baseline combines DeepFM's linear/FM branches with an MMoE deep branch. It jointly learns click, like, follow, comment, forward, long-view, profile-entry, hate, watch-time, and completion heads, then applies an explicit configurable utility policy to the same Week 2 top 100. See the DeepFM + MMoE guide for the architecture and exact losses, or the Week 3 results report for the full comparison, leakage constraints, experiment results, and milestone conclusions.
OMP_NUM_THREADS=1 kuaiflow mmoe --config configs/week3_mmoe.yamlDIN adds attention over the latest 30 strictly prior training clicks. DIN + MMoE feeds the same attended history into the ten-task expert network. Both use history frozen at the training cutoff for validation, test, and candidate scoring. See the DIN implementation guide and full four-model comparison.
In the seed-2026 comparison, DIN reaches test click AUC 0.7233 and NDCG@20 2.015%, versus DeepFM's 0.7195 and 1.687%. DIN + MMoE raises composite NDCG@20 from 1.799% to 1.862% over DeepFM + MMoE. DIN leads NDCG@20 on both validation and test; the multi-task models retain broader objectives and higher catalog coverage. These are single-seed offline results.
OMP_NUM_THREADS=1 kuaiflow din --config configs/week3_din.yaml
OMP_NUM_THREADS=1 kuaiflow din-mmoe --config configs/week3_din_mmoe.yaml
make week3-comparisonRebuild the editable SVG and website-ready PNG figures directly from the saved experiment artifacts:
make week3-figuresCollection is deliberately absent: KuaiRand-Pure has no per-impression collection label, and its month-aggregated collection statistics are not safe training targets or request-time features.
CLI modes are explicit: standard rejects configurations containing faiss,
faiss requires a non-empty faiss block, and tradeoff uses its own fixed
index matrix and therefore rejects a single-index faiss block.
Week 4 freezes all four Week 3 models and compares their probabilities on standard and randomized future impressions. It fits one Platt map on the standard validation period and another on earlier randomized impressions, then evaluates both maps on later, time-aligned final cohorts. Random labels adapt the calibration map; model weights, feature encoders, and histories stay fixed. The standard validation period has already selected training epochs, and neither final test cohort fits calibration parameters.
The audit reports click discrimination, log loss, Brier score, ECE, reliability diagrams, eight binary MMoE heads, shared-scenario sensitivity, exposure mix, and paired user-bootstrap intervals. It measures probability quality rather than ranking lift or off-policy value. See the implementation notes and generated experimental report.
OMP_NUM_THREADS=1 make week4
make week4-reportThe first command scores the saved checkpoints and runs the full audit. The
second rebuilds its report and figures from artifacts/week4_results.json.
The raw standard-policy files contain a small timestamp overlap at their boundary. Preprocessing conservatively removes 47 future rows whose timestamps are not later than the final training timestamp, preserving a strictly chronological evaluation.
The default positive signal is is_click. In KuaiRand this represents a click
for the two-column interface and a valid play for the single-column interface.
The randomized-exposure log is excluded from base-model training and epoch
selection. Week 4 uses its early post-training portion for calibration and
reserves its later portion for final evaluation.
The main benchmark removes training-seen positives from each user's ground truth and restricts evaluation to items present in the training catalog. This protocol measures novel-item recommendation under a warm-start setting; cold-start items will be evaluated separately after content features are added.
Randomized exposures are used for calibration adaptation and a future robustness audit. This pipeline does not use logging propensities and makes no unbiased off-policy evaluation claim.
- Week 1 — Complete: data pipeline, chronological evaluation, Popularity, ItemCF, and BPR.
- Week 2 — Complete: feature/history-aware two-tower retrieval, controlled ablations, and matched exact-versus-FAISS latency evaluation.
- Week 3 — Major milestone reached: the first measured end-to-end offline retrieval-to-ranking pipeline is complete. DeepFM, DIN, DeepFM + MMoE, and DIN + MMoE rerank the fixed Week 2 top 100 with a measured comparison. The three-seed matched pooling ablation is complete; MMoE scoring comparison and validation-based source/neural blending are complete.
- Week 4 — Implemented: exposure-robustness audit and calibrated evaluation of the four frozen models, with time-aligned final holdouts, reliability diagrams, and paired uncertainty. Measured outputs are in the Week 4 report.
- Week 5 — Complete as an offline prototype: diversity-aware reranking, FAISS/DIN and ItemCF serving profiles, HTTP replay, local latency, and final report. Online deployment and A/B testing remain outside scope.
KuaiRand is released by Gao et al. under CC BY-SA 4.0. This repository does not redistribute the dataset. Download instructions, field definitions, and citation information are available from the official KuaiRand repository.
Code in this repository is released under the MIT License. The KuaiRand dataset has its own CC BY-SA 4.0 license and attribution requirements.
The single-task DIN pooling comparison and reproducible three-seed protocol are described in the ablation report. It preserves existing Week 3 and Week 4 artifacts. Measured results show attention has the highest mean validation NDCG@20, while its test NDCG@20 is very close to mean pooling.
The three-seed MMoE experiment compares mean and attention pooling, click-only and fixed composite scoring, and the existing single-task mean baseline. Protocol. The single-task baseline leads average click NDCG@20; combining outcomes trades click/long-view ranking for like/profile-entry ranking relative to MMoE click-only scoring. No utility weights were tuned.
The candidate-budget experiment evaluates 100/200/500 candidates with a frozen mean-pooling ranker and a small validation-only retrieval search. Its audit found and fixed same-timestamp history leakage in two-tower training. Earlier Week 2 results and candidate-based Week 3 comparisons remain historical snapshots of the earlier retrieval implementation; new experiments use strict timestamp exclusion.
The corrected retrieval results retain K=100: larger budgets improve recall but hurt final NDCG@20 with the frozen ranker. A validation-selected two-tower/ItemCF mixture improves final test NDCG@20 from 1.924% to 3.253%. This is a single-seed offline comparison, not online lift.
The whole-pipeline comparison includes the previously missing ItemCF-only control, source-rank fusion and neural-score blending. Tuning selected ItemCF-only with no neural reranking; it reached test NDCG@20 6.141%, versus 3.885% for the mixed retrieval order and 3.253% for mixed candidates plus the neural ranker. The selection was confirmed on reserved validation users. This restores a stronger existing baseline rather than claiming a new model improvement over ItemCF. Catalog coverage is lower than mixed retrieval.
Run python -m kuaiflow.recommend --users 1 2 --k 20 from the repository root with local artifacts available, or use RecommendationPipeline.load("artifacts/pipeline_improvement/selected_pipeline.json").recommend(user_ids). The inference path uses training-only ItemCF, excludes training-clicked items, and skips unused neural models. User IDs in the command are examples.
The operations guide covers prerequisites, API requests, Python usage, and reproduction. The final report includes the validation search, confirmation/test tradeoffs, exact HTTP replay, and latency.
make week5
OMP_NUM_THREADS=1 .venv/bin/python -m kuaiflow.serving --profile itemcfThe default preserves ItemCF ordering. To exercise the full neural pipeline,
run python -m kuaiflow.serving --profile hybrid_din --diversity --port 8001.
Diversity reranking is opt-in: its selected ItemCF setting improves tag diversity
but exceeds the relevance-loss budget on confirmation and test. Frozen choices
remain recorded; no test-based retuning is presented as validation selection.
This completes the planned offline architecture, not a production deployment or a claim that the neural models beat ItemCF. See the commit guide to preserve the preceding experiment checkpoint separately from Week 5.
