From 0285fe45e7ad24fe5d9404248dbaa2f445649267 Mon Sep 17 00:00:00 2001 From: Paul Scotti Date: Sat, 24 Oct 2026 15:14:42 +0000 Subject: [PATCH 1/2] Add Midnight-12k-SPECTRA encoder --- README.md | 3 +- src/thunder/benchmark.py | 2 +- .../pretrained_model/midnight12k_spectra.yaml | 6 ++ src/thunder/models/download.py | 11 +++- src/thunder/models/pretrained_models.py | 5 +- src/thunder/utils/constants.py | 1 + tests/test_midnight12k_spectra.py | 64 +++++++++++++++++++ 7 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 src/thunder/config/pretrained_model/midnight12k_spectra.yaml create mode 100644 tests/test_midnight12k_spectra.py diff --git a/README.md b/README.md index 588f3ae..f5e1337 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ To learn more about how to use `thunder`, please visit our [documentation](https An API and command line interface (CLI) are provided to allow users to download datasets, models, and run benchmarks. The API is designed to be user-friendly and allows for easy integration into existing workflows. The CLI provides a convenient way to access the same functionality from the command line. > [!IMPORTANT] -> **Downloading supported foundation models**: you will have to visit the Huggingface URL of supported models you wish to use in order to accept usage conditions. +> **Downloading supported foundation models**: some models require accepting their usage conditions on Hugging Face before downloading.
List of Huggingface URLs @@ -65,6 +65,7 @@ An API and command line interface (CLI) are provided to allow users to download * Virchow2: https://huggingface.co/paige-ai/Virchow2 * H-optimus-0: https://huggingface.co/bioptimus/H-optimus-0 * H-optimus-1: https://huggingface.co/bioptimus/H-optimus-1 +* Midnight-12k-SPECTRA: https://huggingface.co/medarc/Midnight-12k-SPECTRA * GenBio-PathFM: https://huggingface.co/genbio-ai/genbio-pathfm * CONCH: https://huggingface.co/MahmoodLab/CONCH * TITAN/CONCHv1.5: https://huggingface.co/MahmoodLab/TITAN diff --git a/src/thunder/benchmark.py b/src/thunder/benchmark.py index 01f0e58..a0a2d1e 100644 --- a/src/thunder/benchmark.py +++ b/src/thunder/benchmark.py @@ -21,7 +21,7 @@ def benchmark( where options are: - dataset: *bach*, *bracs*, *break_his*, *ccrcc*, *crc*, *esca*, *mhist*, *ocelot*, *pannuke*, *patch_camelyon*, *segpath_epithelial*, *segpath_lymphocytes*, *tcga_crc_msi*, *tcga_tils*, *tcga_uniform*, *wilds* - - model: *hiboub*, *hiboul*, *hoptimus0*, *hoptimus1*, *genbio-pathfm*, *midnight*, *phikon*, *phikon2*, *uni*, *uni2h*, *virchow*, *virchow2*, *conch*, *titan*, *keep*, *musk*, *plip*, *quiltnetb32*, *dinov2base*, *dinov2large*, *vitbasepatch16224in21k*, *vitlargepatch16224in21k*, *clipvitbasepatch32*, *clipvitlargepatch14* + - model: *hiboub*, *hiboul*, *hoptimus0*, *hoptimus1*, *genbio-pathfm*, *midnight12k_spectra*, *midnight*, *phikon*, *phikon2*, *uni*, *uni2h*, *virchow*, *virchow2*, *conch*, *titan*, *keep*, *musk*, *plip*, *quiltnetb32*, *dinov2base*, *dinov2large*, *vitbasepatch16224in21k*, *vitlargepatch16224in21k*, *clipvitbasepatch32*, *clipvitlargepatch14* - task: *adversarial_attack*, *alignment_scoring*, *image_retrieval*, *knn*, *linear_probing*, *pre_computing_embeddings*, *segmentation*, *simple_shot*, *transformation_invariance*, *zero_shot_vlm* - loading_mode: *online_loading*, *image_pre_loading*, *embedding_pre_loading* diff --git a/src/thunder/config/pretrained_model/midnight12k_spectra.yaml b/src/thunder/config/pretrained_model/midnight12k_spectra.yaml new file mode 100644 index 0000000..1bea3ca --- /dev/null +++ b/src/thunder/config/pretrained_model/midnight12k_spectra.yaml @@ -0,0 +1,6 @@ +model_name: midnight12k_spectra +type: safetensors +vlm: false +emb_dim: 3072 +emb_dim_seg: 1536 +ckpt_path: ${oc.env:THUNDER_BASE_DATA_FOLDER}/pretrained_ckpts/midnight12kspectra/ diff --git a/src/thunder/models/download.py b/src/thunder/models/download.py index db7e381..526bc89 100644 --- a/src/thunder/models/download.py +++ b/src/thunder/models/download.py @@ -7,8 +7,8 @@ ) # Models -# Importantly, you will have to visit the Huggingface URL of all models to accept -# usage conditions to be able to download their associated checkpoints. +# Some models require accepting their usage conditions on Hugging Face +# before downloading their checkpoints. TAGS_FILENAMES = { "uni": ( "MahmoodLab/uni", @@ -86,6 +86,10 @@ "histai/hibou-L", "model.safetensors", ), # Hibou-L (https://huggingface.co/histai/hibou-L) + "midnight12k_spectra": ( + "medarc/Midnight-12k-SPECTRA", + "model.safetensors", + ), # Midnight-12k-SPECTRA (https://huggingface.co/medarc/Midnight-12k-SPECTRA) "midnight": ( "kaiko-ai/midnight", "model.safetensors", @@ -169,6 +173,7 @@ def download_models(models: Union[List[str], str]) -> None: * hiboub * hiboul * midnight + * midnight12k_spectra * openmidnight * kaiko_vits8 * kaiko_vits16 @@ -262,7 +267,7 @@ def download_model(model: str) -> None: ] if local_dir_tag != "plip": extra_files.extend(["merges.txt", "vocab.json"]) - elif local_dir_tag == "midnight": + elif local_dir_tag in ["midnight", "midnight12kspectra"]: extra_files = ["config.json"] elif local_dir_tag == "titan": extra_files = [ diff --git a/src/thunder/models/pretrained_models.py b/src/thunder/models/pretrained_models.py index f39af21..db147b0 100644 --- a/src/thunder/models/pretrained_models.py +++ b/src/thunder/models/pretrained_models.py @@ -124,7 +124,7 @@ def get_model(model_cfg: dict, device: str): model, transform, tokenizer = get_keep(model_cfg.ckpt_path) elif model_cfg.model_name == "titan": model, transform = get_titan(model_cfg.ckpt_path) - elif model_cfg.model_name == "midnight": + elif model_cfg.model_name in ["midnight", "midnight12k_spectra"]: model, transform = get_midnight(model_cfg.ckpt_path) elif model_cfg.model_name == "genbio-pathfm": model, transform = get_genbio_pathfm(model_cfg.ckpt_path) @@ -346,7 +346,7 @@ def extract_embedding(src, pretrained_model, task_type): else: return patch_tokens - elif model_cfg.model_name == "midnight": + elif model_cfg.model_name in ["midnight", "midnight12k_spectra"]: def extract_embedding(src, pretrained_model, task_type): out = pretrained_model(src).last_hidden_state @@ -407,6 +407,7 @@ def get_model_from_name(model_name: str, device: str): * hiboub * hiboul * midnight + * midnight12k_spectra * openmidnight * kaiko_vits8 * kaiko_vits16 diff --git a/src/thunder/utils/constants.py b/src/thunder/utils/constants.py index 5a71411..fe30d5c 100644 --- a/src/thunder/utils/constants.py +++ b/src/thunder/utils/constants.py @@ -15,6 +15,7 @@ class ModelConstants(Enum): "kaiko_vitb8", "kaiko_vitb16", "midnight", + "midnight12k_spectra", "openmidnight", "phikon", "phikon2", diff --git a/tests/test_midnight12k_spectra.py b/tests/test_midnight12k_spectra.py new file mode 100644 index 0000000..a053723 --- /dev/null +++ b/tests/test_midnight12k_spectra.py @@ -0,0 +1,64 @@ +from pathlib import Path +from unittest.mock import patch + +import torch +from omegaconf import OmegaConf +from PIL import Image + +from thunder.models import pretrained_models +from thunder.models.download import download_models + + +def test_midnight12k_spectra_download(tmp_path, monkeypatch): + monkeypatch.setenv("THUNDER_BASE_DATA_FOLDER", str(tmp_path)) + with patch("huggingface_hub.hf_hub_download") as download: + download_models("midnight12k_spectra") + assert [ + (call.args[0], call.kwargs["filename"]) for call in download.call_args_list + ] == [ + ("medarc/Midnight-12k-SPECTRA", "model.safetensors"), + ("medarc/Midnight-12k-SPECTRA", "config.json"), + ] + assert all( + Path(call.kwargs["local_dir"]) + == tmp_path / "pretrained_ckpts/midnight12kspectra" + for call in download.call_args_list + ) + + +def test_midnight12k_spectra_features(tmp_path, monkeypatch): + monkeypatch.setenv("THUNDER_BASE_DATA_FOLDER", str(tmp_path)) + monkeypatch.setenv("HF_HUB_OFFLINE", "1") + cfg = OmegaConf.load( + Path(pretrained_models.__file__).parents[1] + / "config/pretrained_model/midnight12k_spectra.yaml" + ) + from transformers import Dinov2Config, Dinov2Model + + reference = Dinov2Model( + Dinov2Config( + hidden_size=32, + num_hidden_layers=1, + num_attention_heads=4, + intermediate_size=64, + image_size=224, + patch_size=14, + ) + ).eval() + reference.save_pretrained(tmp_path) + cfg.ckpt_path = str(tmp_path) + model, transform, extract = pretrained_models.get_model(cfg, "cpu") + model.eval().requires_grad_(False) + x = transform(Image.new("RGB", (257, 231), color=(127, 91, 164))).unsqueeze(0) + assert x.shape == (1, 3, 224, 224) + with torch.no_grad(): + tokens = reference(x).last_hidden_state + patches = tokens[:, 1:] + pooled = torch.cat([tokens[:, 0], patches.mean(1)], dim=-1) + torch.testing.assert_close(extract(x, model, "linear_probing"), pooled) + dense = extract(x, model, "segmentation") + assert dense.shape == (1, 256, 32) + torch.testing.assert_close(dense, patches) + x.requires_grad_() + extract(x, model, "linear_probing")[0, 0].backward() + assert torch.isfinite(x.grad).all() and x.grad.abs().sum() > 0 From 6bc6ad3db152deb007ee8f7f8f82f3e95e7eaae7 Mon Sep 17 00:00:00 2001 From: Paul Scotti Date: Sat, 24 Oct 2026 16:31:50 +0000 Subject: [PATCH 2/2] Keep submission focused on model integration --- README.md | 2 +- src/thunder/models/download.py | 4 +- tests/test_midnight12k_spectra.py | 64 ------------------------------- 3 files changed, 3 insertions(+), 67 deletions(-) delete mode 100644 tests/test_midnight12k_spectra.py diff --git a/README.md b/README.md index f5e1337..661d21d 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ To learn more about how to use `thunder`, please visit our [documentation](https An API and command line interface (CLI) are provided to allow users to download datasets, models, and run benchmarks. The API is designed to be user-friendly and allows for easy integration into existing workflows. The CLI provides a convenient way to access the same functionality from the command line. > [!IMPORTANT] -> **Downloading supported foundation models**: some models require accepting their usage conditions on Hugging Face before downloading. +> **Downloading supported foundation models**: you will have to visit the Huggingface URL of supported models you wish to use in order to accept usage conditions.
List of Huggingface URLs diff --git a/src/thunder/models/download.py b/src/thunder/models/download.py index 526bc89..37fdf49 100644 --- a/src/thunder/models/download.py +++ b/src/thunder/models/download.py @@ -7,8 +7,8 @@ ) # Models -# Some models require accepting their usage conditions on Hugging Face -# before downloading their checkpoints. +# Importantly, you will have to visit the Huggingface URL of all models to accept +# usage conditions to be able to download their associated checkpoints. TAGS_FILENAMES = { "uni": ( "MahmoodLab/uni", diff --git a/tests/test_midnight12k_spectra.py b/tests/test_midnight12k_spectra.py deleted file mode 100644 index a053723..0000000 --- a/tests/test_midnight12k_spectra.py +++ /dev/null @@ -1,64 +0,0 @@ -from pathlib import Path -from unittest.mock import patch - -import torch -from omegaconf import OmegaConf -from PIL import Image - -from thunder.models import pretrained_models -from thunder.models.download import download_models - - -def test_midnight12k_spectra_download(tmp_path, monkeypatch): - monkeypatch.setenv("THUNDER_BASE_DATA_FOLDER", str(tmp_path)) - with patch("huggingface_hub.hf_hub_download") as download: - download_models("midnight12k_spectra") - assert [ - (call.args[0], call.kwargs["filename"]) for call in download.call_args_list - ] == [ - ("medarc/Midnight-12k-SPECTRA", "model.safetensors"), - ("medarc/Midnight-12k-SPECTRA", "config.json"), - ] - assert all( - Path(call.kwargs["local_dir"]) - == tmp_path / "pretrained_ckpts/midnight12kspectra" - for call in download.call_args_list - ) - - -def test_midnight12k_spectra_features(tmp_path, monkeypatch): - monkeypatch.setenv("THUNDER_BASE_DATA_FOLDER", str(tmp_path)) - monkeypatch.setenv("HF_HUB_OFFLINE", "1") - cfg = OmegaConf.load( - Path(pretrained_models.__file__).parents[1] - / "config/pretrained_model/midnight12k_spectra.yaml" - ) - from transformers import Dinov2Config, Dinov2Model - - reference = Dinov2Model( - Dinov2Config( - hidden_size=32, - num_hidden_layers=1, - num_attention_heads=4, - intermediate_size=64, - image_size=224, - patch_size=14, - ) - ).eval() - reference.save_pretrained(tmp_path) - cfg.ckpt_path = str(tmp_path) - model, transform, extract = pretrained_models.get_model(cfg, "cpu") - model.eval().requires_grad_(False) - x = transform(Image.new("RGB", (257, 231), color=(127, 91, 164))).unsqueeze(0) - assert x.shape == (1, 3, 224, 224) - with torch.no_grad(): - tokens = reference(x).last_hidden_state - patches = tokens[:, 1:] - pooled = torch.cat([tokens[:, 0], patches.mean(1)], dim=-1) - torch.testing.assert_close(extract(x, model, "linear_probing"), pooled) - dense = extract(x, model, "segmentation") - assert dense.shape == (1, 256, 32) - torch.testing.assert_close(dense, patches) - x.requires_grad_() - extract(x, model, "linear_probing")[0, 0].backward() - assert torch.isfinite(x.grad).all() and x.grad.abs().sum() > 0