Skip to content

feat: add RASCCrossEncoder model and ValueSetPairClassification task (RASC reproduction) - #1608

Open
QiWenchang wants to merge 1 commit into
sunlabuiuc:masterfrom
QiWenchang:feat/rasc-cross-encoder
Open

QiWenchang wants to merge 1 commit into
sunlabuiuc:masterfrom
QiWenchang:feat/rasc-cross-encoder

Conversation

@QiWenchang

Copy link
Copy Markdown

Contributor: Wenchang Qi (qi14@illinois.edu, NetID: qi14)

Contribution Type: New Model + Task (reproduction, CS 598 DL4H Option 4: model and task)

Paper: Mukherjee et al., 2026. Retrieve, Then Classify: Corpus-Grounded Automation of Clinical Value Set Authoring. https://arxiv.org/abs/2604.14616 (code: https://github.com/mukhes3/RASC)

Description: Adds the paper's best classifier, the cross-encoder, and the task that feeds it. PyHealth already has the VSAC dataset and the embedding form of the task for the paper's MLP (#1418). The paper's headline result comes from the cross-encoder, which reads the value set title and a candidate code's display name together as one sentence pair. PyHealth had no model for that and no task that emits the two texts.

  • RASCCrossEncoder (BaseModel) tokenizes the two text inputs as [CLS] title [SEP] display [SEP], runs a BERT encoder (SapBERT by default, as in the paper) and scores the pair with a linear head. It follows model_training/train_cross_encoder.py in the authors' repository and loads their released weights (RELEASED_WEIGHTS), head included. The loss is BCE with the label processor's pos_weight, the paper's class-balanced BCE.
  • Late fusion (not in the paper): the other inputs of the dataset, such as the retrieval score, pass through a bias-free linear layer whose output is added to the text logit. Its weights start at zero, so an untrained fused model scores exactly as the text-only one and the released weights stay valid.
  • mixed_precision=True runs the encoder under bfloat16 autocast on a GPU. gradient_checkpointing and dropout are passed to the encoder as in the authors' script.
  • ValueSetPairClassification (BaseTask) emits one sample per (target value set, candidate code): title_text, candidate_text, a binary label, and on request score and code_system for fusion. Candidate pooling, the 1,000-code cap and the labels match ValueSetCompletion; a test checks that both tasks produce the same pool, labels and scores. It reads only the index's text and neighbor tables, so workers load no embedding matrix.
  • build_hybrid_retrieval_index (not in the paper) merges the title-embedding ranking with a BM25 ranking of the titles by reciprocal rank fusion, at the same K. The paper names retrieval coverage as its binding constraint; this tests whether lexical matching helps.

How the new code relates to existing components (no existing component is changed; the only edits to existing files are two exports and two docs index lines):

  • value_set_pair_classification.py imports CODE_SYSTEM_VOCAB, normalize_code_system and the Encoder type from value_set_completion.py, on purpose: the code_system one-hot must use the same vocabulary and URI mapping as the existing task and as VSACDataset, which imports the same function.
  • The new task reads the on-disk index written by the existing build_retrieval_index, and build_hybrid_retrieval_index writes the same files, so one task reads both. This is a shared file layout, not shared code: the hybrid builder is self-contained, and value_set_completion.py stays as merged.
  • I kept this as a separate task instead of adding options to ValueSetCompletion, so that the existing task and its cache keys are untouched.
  • The example imports build_retrieval_index and sapbert_encoder to build the paper's dense index.
  • tests/core/test_value_set_pair_classification.py imports the synthetic VSAC fixture (write_fixture, stub_encoder, ICD10, _THREADED_DASK) from tests/core/test_vsac.py, so both tasks are tested on the same five value sets, and write_tiny_bert from tests/core/test_rasc.py.
  • No new dependencies. BM25 uses scikit-learn's CountVectorizer and scipy.sparse; the encoder loads through transformers.

Reproduction (real VSAC: 11,799 of the release manifest's 11,803 value sets were still downloadable; first 1,000 codes each as in the paper; K=10; 4.00M training, 0.92M validation and 2.25M test pairs; one A100, about 3 hours; single runs):

Model Pair AUROC Pair AP VS precision VS recall VS F1
Paper cross-encoder 0.852 0.374 – – 0.298
Authors' released weights, this pipeline 0.850 0.353 0.256 0.393 0.260
Trained here with PyHealth Trainer (main) 0.850 0.386 0.305 0.342 0.274
Retrieval only – – – 0.555 0.115
  • Pair-level ranking reproduces: both the authors' weights and a model trained here are within 0.002 AUROC of the paper.
  • Value-set F1 is 0.02–0.04 below the paper's 0.298. The authors' own weights show the same gap, so it comes from the evaluation setup and not from training. I did not test which choice differs. Here the threshold is tuned on validation pairs pooled over value sets, and true codes that retrieval never surfaced count against recall.
  • Retrieval recall is 0.555 against the paper's 0.553.

Ablations (examples/vsac_value_set_completion_rasc.py; 20% of the value sets of every split, hashed by OID, 755 test value sets, 0.81M training pairs; none of these are in the paper):

Config Change Test pairs Pair AUROC Pair AP VS F1 Retrieval recall
main paper's setup 474,857 0.731 0.258 0.183 0.538
fused + score and code system on the logit 474,857 0.702 0.243 0.180 0.538
fused_score + score only 474,857 0.728 0.263 0.186 0.538
len32 32 tokens per pair 474,857 0.721 0.248 0.184 0.538
len64 64 tokens per pair 474,857 0.712 0.232 0.179 0.538
lr5e-5 learning rate 5e-5 474,857 0.659 0.206 0.176 0.538
lr1e-5 learning rate 1e-5 474,857 0.731 0.272 0.181 0.538
hybrid SapBERT + BM25 retrieval 434,775 0.698 0.250 0.195 0.522
  • Data volume matters most: main reaches AUROC 0.731 on 20% of the training value sets and 0.850 on all of them.
  • A learning rate of 5e-5 is clearly worse (−0.07 AUROC). 2e-5 and 1e-5 are indistinguishable.
  • Late fusion does not help the cross-encoder, although the retrieval score is the MLP's most useful input in feat: add VSAC dataset and ValueSetCompletion task (RASC reproduction) #1418.
  • 32 tokens per pair cost at most 0.01 AUROC, a quarter of the paper's length.
  • Hybrid retrieval gives the highest value-set F1 (0.195 against 0.183) on 8% fewer candidates, with slightly lower retrieval recall. One run each, so this is within noise.
  • Noise is about 0.01–0.02 AUROC (len64 is below both len32 and main). Treat smaller differences as nothing.

A finding about data loading: get_dataloader(shuffle=True) on a patient-level split shuffles only within each value set's run of samples. A batch of 64 held 1.3 value sets on average, and with that loader the cross-encoder stayed at validation AUROC 0.500 for three epochs. The example therefore reads the training split once and permutes it in memory (shuffled_dataloader); with that change alone it reaches the numbers above. This PR does not change the shared loader.

Differences from the authors' training script: batch size 64 replaces 16 with 4 accumulation steps; bfloat16 autocast replaces fp16 with a gradient scaler; the linear warmup (6%) and decay run inside an optimizer subclass in the example (WarmupAdamW), because Trainer takes no scheduler; early stopping watches validation PR-AUC instead of validation loss.

Limitations:

  • VSAC terms forbid redistribution, so users download their own copy with a free UMLS key (VSACDataset.download_expansions). Tests use synthetic value sets only.
  • Every number is a single run with seed 42.
  • The in-memory training loader needs about 8 GB of RAM for the full training split.

Files to Review:

  • pyhealth/models/rasc.py: RASCCrossEncoder
  • pyhealth/tasks/value_set_pair_classification.py: ValueSetPairClassification, build_hybrid_retrieval_index
  • tests/core/test_rasc.py: 19 model tests on a random two-layer BERT built in a temporary directory and four in-memory pairs (no network, no download); module setup about 0.1 s
  • tests/core/test_value_set_pair_classification.py: 16 task and retrieval tests on five synthetic value sets with stub encoders, including task-to-model end to end; one shared module setup of about 1.5 s (PyHealth's dataset cache build), each test under 0.3 s
  • examples/vsac_value_set_completion_rasc.py: reproduction and ablations; --synthetic runs all eight trained configurations on CPU in about a minute
  • docs/api/models/pyhealth.models.RASCCrossEncoder.rst, docs/api/tasks/pyhealth.tasks.ValueSetPairClassification.rst, docs/api/models.rst, docs/api/tasks.rst: API docs
  • pyhealth/models/__init__.py, pyhealth/tasks/__init__.py: exports

Test coverage of the two new modules is 98% (rasc.py 99%, value_set_pair_classification.py 98%).

…(RASC reproduction)

Adds the cross-encoder of Retrieve, Then Classify (Mukherjee et al., 2026,
https://arxiv.org/abs/2604.14616) as a PyHealth model, a text-pair task that
feeds it from VSACDataset, and a hybrid SapBERT + BM25 retrieval index
builder, with tests, API docs and a reproduction and ablation example.
@QiWenchang QiWenchang changed the title feat: add RASCCrossEncoder model and ValueSetPairClassification task … feat: add RASCCrossEncoder model and ValueSetPairClassification task (RASC reproduction) Nov 25, 2026
@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 86.42% (46568 / 53887 lines)

Modified file Coverage Lines
pyhealth/models/__init__.py 100.00% 92 / 92
pyhealth/models/rasc.py 98.59% 70 / 71
pyhealth/tasks/__init__.py 98.08% 102 / 104
pyhealth/tasks/value_set_pair_classification.py 98.41% 186 / 189

Workflow run · 2ae5bab

This branch has not been deployed

No deployments
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