Skip to content

HPC + MDR optimization & updated defaults - #61

Merged
epbrenner merged 7 commits into
hpc-parallelizationfrom
hpc-optimization
Nov 24, 2026
Merged

epbrenner merged 7 commits into
hpc-parallelizationfrom
hpc-optimization

Conversation

@epbrenner

Copy link
Copy Markdown
Contributor

MDR fits on big bugs can spend hours per individual model. This adds optional parallelism across CV folds for all model families, alongside modeling defaults and correctness fixes. This is intended to be used with MDR or struct models and may not have benefits for most models, so it is disabled by default.

What's new?

  • Add parallel_cv = TRUE to runModelingWorkflow() and fitModels(). Uses existing worker allocation for folds, processing one model at a time without nested task/fold parallelism. Supported in prepared array workflows.
  • Keep parallel_cv = FALSE by default because each fold needs lots of additional memory.
  • Exclude tuning candidates whose requested penalty falls outside the fitted glmnet path. Verify that the final fit reaches its selected penalty, extending the path when necessary.
  • Make "vanilla" select drug and drug-class models; add "mdr" for MDR models and "complete" for all supported modes.
  • Expand model_types documentation, including cross-test, leave-one-out, and shuffled-control options.
  • Changed to include DefenseCas in "standard" features. "full" adds only struct now.
  • Default to quiet console output. verbose = TRUE enables per-group, heap, and worker diagnostics. Progress and saved diagnostic logs remain available.

Try it out!

Drug and drug-class models

runModelingWorkflow(
orb_dir,
model_types = "vanilla",
threads = 8
)

MDR with parallel CV enabled

runModelingWorkflow(
orb_dir,
model_types = "mdr",
threads = 8,
parallel_cv = TRUE,
verbose = TRUE
)

Use model_types = "complete" for all supported modes.

Primarily updates HPC performance with Slurm arrays. Now spins out jobs more robustly into very large-scale arrays, and when OOM errors pop up (they will), this logs and auto-retries with additional memory for two more retries. Benchmarking with S. sonnei shows great improvements in runtime when scaled up with large arrays.

Also some MDR scoring changes, which aren't final and deserve scrutiny.
Wasn't catching false positives for some MDR classes, bug has been relocated outside and removed from the premises.
Updating CV handling so folds can be distributed to individual CPU cores, which benefits some very very very slow MDR models. Adding verbosity handling, changing what "vanilla" refers to and adding a lot more documentation to help users figure out what different parameters do.
@epbrenner epbrenner self-assigned this Nov 11, 2026
@epbrenner
epbrenner marked this pull request as ready for review November 13, 2026 01:13
@AbhirupaGhosh
AbhirupaGhosh added this pull request to stack #58 November 13, 2026 17:04
@AbhirupaGhosh AbhirupaGhosh added enhancement New feature or request rewrite These PRs are relevant to the ongoing rewrite and merging will be to amrml-rewrite branch labels Nov 16, 2026
@amcim

amcim commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

There's a lot of useful machinery here (OOM handling and retries, the SLURM runner, MDR label diagnostics), which also means a lot to check. Unlike #53 and #59, this PR changes fitting results, so I tested it on a subset of S. flexneri: 40 tasks from gene features, covering 3 vanilla drugs (binary), 1 drug (counts), binary MDR, the MDR combination model and a 4-target cross-drug group, each with seeds 123 and 456 and real and shuffled labels. Results were identical() across serial, socket and forked backends, array chunks, resumed runs, and parallel_cv with 3 and 5 workers.

I'll support merging once we've made explicit decisions on these points:

1. Penalty-path exclusion (now on by default). Tuning tries 60 candidates, 10 penalties × 6 mixtures (.tuningGrid(), fit-models.R:114-121). For each mixture, glmnet fits a "path", a sequence of penalties from the strongest downward. Until now, all 60 candidates were eligible, in old amRml and the current rewrite alike. This PR excludes "tuning candidates whose requested penalty falls outside the fitted glmnet path", rather than letting glmnet "silently substitute endpoint coefficients" (fit-cv.R:50-52).

I checked how much that matters. With fewer genomes than features, glmnet's default path stops at 1/100 of its largest penalty (lambda.min.ratio = 0.01), so most of our 1e-4 to 0.1 penalties fall below it. On 4 S. flexneri matrices, only 17–25 of the 60 candidates are inside the path. Below the path, glmnet clamps to the path's end, so those candidates are all the same model. On CIP, mixture 0.2, the 6 penalties below the path all return the identical 354-feature model.

Pros:

  • The reported penalty is the one actually used. Before, "1e-4" really meant the path's end (0.009 for CIP at mixture 0.2), and ridge at "1e-4" was really ridge at 1.8.
  • Cross-validation compares like with like. Every fold and the final fit now use exactly the same penalty. Before, a below-path candidate's score averaged each fold's own path end, and the final model used yet another.

Cons:

  • It fixes the labels by removing options rather than fitting them, and not evenly. The loose end of the grid, where models keep the most features, is what goes.
  • Ridge goes entirely. On CIP the ridge path runs from 180 to 1.8, so all 10 ridge penalties fall below it. I think that's because glmnet computes the ridge path's start as if mixture were 0.001: on CIP it starts exactly 200× higher than the mixture-0.2 path.
  • That pushes tuning toward sparser models. On the subset, 19 of 40 tasks pick a different model, and CIP keeps 152 features instead of 313. Those lists are what rescoring uses, and the shuffled baselines change too.
  • No gain in prediction on this subset. On real labels, every genome gets the same predicted phenotype as before, and MCC is unchanged. These are mostly easy targets, though, so harder targets or other species may differ.
  • It departs from both old amRml and the current rewrite.

I also tried the obvious alternative: giving glmnet the grid's penalties as its path (path_values, which this PR already uses to extend the final fit), so every candidate is fitted at its real penalty. On 8 AZM and CIP tasks it excluded nothing, took the same time, and real-label predictions were again unchanged. But it moves the feature lists the other way. Once the low penalties are really fitted, ridge at a true 1e-4 was chosen on 4 of the 8 tasks, and it keeps almost every feature:

Features kept before this PR full path
AZM, seed 123 189 174 6,569
CIP, seed 456 361 191 555

So on this subset the three behaviors make the same predictions but report very different feature lists. Which one we want is the real decision, and since those lists are what MAGIC works from, I think we should make it explicitly.

Also worth noting that on more complex bugs, predictions may not necessarily be the same.

2. Rescoring on MDR. The description covers the new "mdr" mode, but this PR also turns on MAGIC rescoring for MDR models: binary MDR is scored like a drug, and the combination model per label. That reverses your earlier commit in #53, "Preserve MDR coefficient labels and exclude MDR from binary rescoring", and your commit message here says these MDR scoring changes "aren't final and deserve scrutiny".

It also changes what "shared" means. In #53, runFeatureDyadDiscovery() counts n_drug_or_class = n_distinct(drug_or_class) and keeps features with n_drug_or_class >= min_models_shared (feature-rescoring.R:1096, :1137). Here it counts n_outcomes = n_distinct(drug_or_class, label) and filters on that (:1106, :1148, and n_outcomes == 1 for unique features at :1166; the same again for dyads at :1249, :1290, :1308). Sharedness is still computed separately for drugs, classes and MDR, and I checked that drug results are identical with and without MDR. But within MDR, a feature found in two labels of the one combination model now counts as shared, though only one model found it. On the S. flexneri subset this made no difference: only 10 MDR features passed rescoring's filters, and none was shared under either rule. So this is about what the definition should be, not a measured effect.

Pros:

  • MDR models are fitted anyway, so this gives them feature results instead of leaving them unanalysed.
  • Scoring per label shows which features go with each resistance combination, and the new label diagnostics show how well each label is predicted.

Cons:

  • "Shared" now mixes two different things: found by two models, or found in two labels of one model.
  • Binary and multinomial signs mean different things ("POS" points toward susceptibility for binary, toward the label for multinomial), which makes MDR rows easy to misread next to drug rows.
  • It's a new scientific output bundled into a performance PR, which makes both harder to review.

We may want to make this its own PR, so it gets its own review and we can make sure reversing the earlier exclusion is intentional.

3. Merging with #53. I merged #53 into #59 because the conflicts there were simple. The ones here are harder. Merging hpc-parallelization into this branch conflicts in R/rescore.R, R/workflow.R, man/runModelingWorkflow.Rd and R/task-chunks.R. All but rescore.R are keep-both. The real question is rescore.R: this branch reads native results so MDR labels survive, while #53 builds the MAGIC-ready tables and Parquet outputs from the merged exports. That needs one agreed input path. Also after a merge, one of this PR's tests errors: test-fit-models.R:114 calls .rescoreScopes(fit), which relied on the min_seeds default that #53 removed.

Question

4. MDR in grouped modes. This removes MDR from every stratified, cross and leave-one-out mode. That reverses design/mdr.md, whose Goal was to run both MDR models "in the rewrite's modes, not only on the whole dataset", with agreed rules for cross and leave-one-out test sets. The PR also deletes that doc (including its record that MDR matches old amRml on S. flexneri) and the test for the agreed cross-mode rule, and the description doesn't mention it. If the reason is cost on big datasets, that's fair, but it's worth keeping the doc and updating this decision in it explicitly.

@eboyer221

Copy link
Copy Markdown
Contributor

I went through the MDR parts since that's what I worked on originally. Giving MDR its own mode does seem better than having it inside "vanilla", and the per-label diagnostics are really helpful and avoids overall MCC hiding labels the model never gets right.

I see MDR is no longer running in the year/country/cross modes -
Is this because MDR takes too long on big datasets? Old amRml only ran MDR on the whole dataset too, and when I checked S. flexneri, S. argenteus and E. faecium, only E. faecium had enough genomes across years and countries for those modes to do anything.

But could you:

  • Bring back design/mdr.md and add a short note about this decision and why. It's the only place we wrote down that the new MDR model matches old amRml on S. flexneri, and it explains the label rules that are still in the code.

  • Mention this change in the PR description, since it changes what "vanilla" and the grouped modes return.

  • Keep the deleted cross-mode test and mark it as skipped instead of removing it. The code that handles test sets for MDR is still there, it just can't run right now. If parallel_cv makes MDR fast enough, we might want those modes back.

Running MAGIC on MDR:
I agree with Alex that this should be its own PR.

  • I'd keep "shared" meaning "found by two or more different models." Two labels from the same MDR model aren't really two separate pieces of evidence. You already added a separate table that shows features shared within MDR, so we wouldn't lose that information.
  • The direction of "POS" means opposite things for regular MDR (toward susceptible) and the combination model (toward that label). Could we add a column that says which way it points? Otherwise it's going to be easy to misread, especially once it shows up in whatever visualization is down the line.

The label bug fix in ad62a92 looks at both the true and predicted labels, but .networkOutcomeLabels() in network.R still only looks at the true ones. Is that on purpose? I haven't run it to check, just noticed it.

On the penalty changes - I agree with Alex that we should decide this on purpose, since it changes which features get picked and everything after that uses those lists.

@epbrenner

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review, Alex and Emily! Going to separate a few changes out here. First, the path exclusion leaving Ridge out isn't great and wasn't intended. I'm going to say I/we think about this in more detail and can revert to prior tuning behavior until we finalize it. Same with the MDR feature scoring -- I'm okay with that being its own PR, and doesn't need to be a v1 feature.

For MDR not running cross-test models, that is intentional -- Abhirupa and I had a conversation about this last week, and the long and short of it is that so many nested assumptions going into a leave-one-geography-out multiclass MDR model means an extraordinarily complex model (that takes hours to run) but provides fairly limited scientific information. We could allow these to be manually enabled, perhaps, but I don't think they should be a part of a default MDR run.

I'll try to clean things up quickly so we can keep it moving.

epbrenner and others added 4 commits November 17, 2026 15:37
Oops.

Co-Authored-By: Emily Boyer <130874527+eboyer221@users.noreply.github.com>
Updated version can return for a later PR
Falling back to previous MDR scoring and tuning behavior. These changes deserve greater scrutiny and should be made in a future PR, and these updates are not necessary for v1. Tossing them for now to keep this PR cleaner!
…imization

# Conflicts:
#	R/rescore.R
#	R/task-chunks.R
#	R/workflow.R
#	man/rescoreFeatures.Rd
#	man/runModelingWorkflow.Rd
#	tests/testthat/test-workflow-presets.R
@amcim

amcim commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

@epbrenner Things look good here! Thanks for the parallelization work. Once we're on 2.0, it'd be worth revisiting speed for MDR in the grouped modes, and I can look at applying ideas like those in my PR #60 elsewhere for more speedup.

Because PR #57 was merged early and threw the stack off, I merged #53 into #59 and then #59 into this branch, resolving the conflicts along the way. Each branch now contains the one below it, so merging this into #59, #59 into #53 and #53 into main should go through without conflicts.

Also noting that n_feats_returned now counts distinct features rather than one row per feature per label. That's correct and an improvment

@epbrenner
epbrenner removed this pull request from stack #58 November 24, 2026 19:52
@epbrenner
epbrenner merged commit 7754e3f into hpc-parallelization Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request rewrite These PRs are relevant to the ongoing rewrite and merging will be to amrml-rewrite branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants