Add porting functionality for drvi-py to scvi-tools migration#130
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #130 +/- ##
==========================================
+ Coverage 82.00% 82.62% +0.61%
==========================================
Files 46 47 +1
Lines 2823 3061 +238
==========================================
+ Hits 2315 2529 +214
- Misses 508 532 +24
🚀 New features to boost your workflow:
|
Collaborator
Author
|
This will resolve @cvduffy 's comment on scverse/scvi-tools#3866 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
DRVI's PyTorch model has moved out of drvi-py into scvi-tools (
scvi.external.DRVI). Users who trained models with drvi-py need a way to keep using them once drvi-py drops its module code (version >= 0.3.0). This PR adds a checkpoint-migration utility that converts a saved drvi-py model (model.pt) into one loadable byscvi.external.DRVI.load(...).How to port
Minimal code:
model.ptdict, rewritesmodel_state_dict,init_params_andregistry_, and writes a newmodel.pt. It never instantiates annn.Moduleand imports neither drvi-py's nor scvi-tools' model classes — so it keeps working even after drvi-py removes its module (onlytorch+packagingare needed).destis never overwritten unlessoverwrite=True.Additional information (🤖 Generated with Claude Code)
Key transformations
StackedLinearLayer/split_transformation_weightlayout(n_split, in, out)→ scvi's(n_split, out, in)(transpose of every per-split weight); encoder mean/var un-nested fromFCLayersto plainLinear;px_shared_decoder → px_decoder,params_nets.mean → px_scale_decoder; synthesized zeroedl_encoder.*(scvi's VAE always has a library encoder; strict load requires the keys, but they're unused since DRVI uses the observed library size).r = 1.0 + paramin drvi vspx_rused directly in scvi): folded correctly forgene,gene-batch, andgene-cell, matching drvi's runtime add-order for bit-exactness where possible.drvi_py_0.2.1 → 0.2.2gene-likelihood renames /prior_init_obsremoval, and thedrvi_py → scvi_toolssplit-value renames). Adding a future breaking change is one function + one table entry.setup_anndataaccepts it on load (dropsis_count_data, addssize_factor_key, setsmodel_name="DRVI").DRVIPortErrorfor features scvi-tools DRVI cannot reproduce (learned covariate embeddings, non-normal prior, unsupported split methods/aggregations, non-uniform layer widths, decoder dropout, dropped gene-likelihoods); warns for training-only knobs that don't affect a trained model's forward pass. Every error message links users to open an issue if they need an unsupported case.Verification
Validated end-to-end by loading each model in both frameworks and comparing the deterministic forward pass (latent,
px.mean,px.log_prob, reconstruction) across 7 models — three real trained checkpoints (immune, HLCA, and a 261 MB / 40k-gene Parse-Bio model withmean_activation) plus trained models coveringdecoder_reuse_weights="nowhere",gene-batch, andgene-cell:Δ = 0.0) for allgeneandgene-batchmodels.gene-cellmatches to~1e-6— an inherent last-ULP difference (drvi adds its+1outside a nonlinearlogsumexp, which checkpoint surgery can only fold inside); numerically identical distributions.Unit tests in
tests/utils/test_porting.py(6 tests) cover the key remaps, transposes, all three dispersion modes, no-overwrite behavior, and capability errors.