clop-dit 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- clop_dit/__init__.py +22 -0
- clop_dit/architecture/__init__.py +5 -0
- clop_dit/architecture/cell2cell.py +355 -0
- clop_dit/architecture/clop/__init__.py +22 -0
- clop_dit/architecture/clop/aligner.py +488 -0
- clop_dit/architecture/clop/losses.py +748 -0
- clop_dit/architecture/decoder.py +388 -0
- clop_dit/architecture/dit.py +672 -0
- clop_dit/architecture/scgpt_embed.py +848 -0
- clop_dit/cli.py +72 -0
- clop_dit/data_pipeline/__init__.py +15 -0
- clop_dit/data_pipeline/cache_builder.py +554 -0
- clop_dit/data_pipeline/dataset.py +855 -0
- clop_dit/data_pipeline/embedding_preprocessor.py +426 -0
- clop_dit/data_pipeline/geo_fetcher.py +254 -0
- clop_dit/data_pipeline/group_aware_sampler.py +170 -0
- clop_dit/data_pipeline/subcluster_annotation.py +1102 -0
- clop_dit/data_pipeline/text_cleaner.py +339 -0
- clop_dit/evaluation/__init__.py +6 -0
- clop_dit/evaluation/baseline_registry.py +245 -0
- clop_dit/evaluation/biological_validation/__init__.py +4 -0
- clop_dit/evaluation/biological_validation/constants.py +38 -0
- clop_dit/evaluation/biological_validation/figures.py +613 -0
- clop_dit/evaluation/biological_validation/generation.py +97 -0
- clop_dit/evaluation/biological_validation/io.py +120 -0
- clop_dit/evaluation/biological_validation/metrics.py +173 -0
- clop_dit/evaluation/biological_validation/run.py +219 -0
- clop_dit/evaluation/downstream_biology.py +627 -0
- clop_dit/evaluation/embedding_quality.py +231 -0
- clop_dit/evaluation/generative_metrics.py +426 -0
- clop_dit/evaluation/metrics.py +443 -0
- clop_dit/evaluation/model_benchmarking.py +525 -0
- clop_dit/evaluation/run_metrics.py +328 -0
- clop_dit/evaluation/visualizer.py +255 -0
- clop_dit/experiments/__init__.py +19 -0
- clop_dit/experiments/ood_evaluation.py +715 -0
- clop_dit/experiments/rare_cell_augmentation.py +867 -0
- clop_dit/training/__init__.py +5 -0
- clop_dit/training/reproducibility.py +60 -0
- clop_dit/training/schedulers.py +55 -0
- clop_dit/training/train_cell2cell.py +552 -0
- clop_dit/training/train_clop.py +890 -0
- clop_dit/training/train_dit.py +619 -0
- clop_dit/utils/__init__.py +34 -0
- clop_dit/utils/constants.py +106 -0
- clop_dit/utils/experiment_tracker.py +214 -0
- clop_dit/utils/helpers.py +58 -0
- clop_dit/utils/logging_config.py +39 -0
- clop_dit/utils/paths.py +139 -0
- clop_dit/visualization/__init__.py +27 -0
- clop_dit/visualization/_plot_helpers.py +162 -0
- clop_dit/visualization/_utils.py +43 -0
- clop_dit/visualization/article_composition.py +162 -0
- clop_dit/visualization/article_delivery.py +383 -0
- clop_dit/visualization/baseline_panels.py +3 -0
- clop_dit/visualization/benchmark_panels.py +3 -0
- clop_dit/visualization/direct_layout.py +200 -0
- clop_dit/visualization/downstream_panels.py +22 -0
- clop_dit/visualization/experimental/__init__.py +10 -0
- clop_dit/visualization/experimental/fig19_pseudobulk.py +132 -0
- clop_dit/visualization/experimental/fig20_failure_analysis.py +149 -0
- clop_dit/visualization/experimental/fig21_ablation_heatmap.py +185 -0
- clop_dit/visualization/experimental/fig21_field_ablation.py +135 -0
- clop_dit/visualization/experimental/fig22_discriminator.py +151 -0
- clop_dit/visualization/experimental/fig22_multi_seed.py +141 -0
- clop_dit/visualization/experimental/fig23_ood_showcase.py +154 -0
- clop_dit/visualization/experimental/fig24_variance_deepdive.py +194 -0
- clop_dit/visualization/experimental/fig25_cross_dataset.py +167 -0
- clop_dit/visualization/experimental/fig26_expanded_de.py +231 -0
- clop_dit/visualization/experimental/fig27_ood_robustness.py +244 -0
- clop_dit/visualization/experimental/fig28_marker_completeness.py +215 -0
- clop_dit/visualization/experimental/fig29_embedding_augmentation.py +238 -0
- clop_dit/visualization/experimental/fig30_validation_summary.py +396 -0
- clop_dit/visualization/experimental/fig31_decoder_ablation.py +174 -0
- clop_dit/visualization/explicit_positioning.py +135 -0
- clop_dit/visualization/fig03_training.py +567 -0
- clop_dit/visualization/fig04_embedding.py +843 -0
- clop_dit/visualization/fig05_metrics.py +659 -0
- clop_dit/visualization/fig06_fidelity.py +291 -0
- clop_dit/visualization/fig07_alignment.py +387 -0
- clop_dit/visualization/fig08_markers.py +346 -0
- clop_dit/visualization/fig09_expression_corr.py +322 -0
- clop_dit/visualization/fig10_expression_analysis.py +301 -0
- clop_dit/visualization/fig11_conditioning.py +392 -0
- clop_dit/visualization/fig12_diversity.py +237 -0
- clop_dit/visualization/fig13_noise_tradeoff.py +154 -0
- clop_dit/visualization/fig14_expr_diversity.py +122 -0
- clop_dit/visualization/fig15_baselines.py +405 -0
- clop_dit/visualization/fig16_benchmark.py +333 -0
- clop_dit/visualization/fig17_downstream.py +704 -0
- clop_dit/visualization/fig18_de_concordance.py +294 -0
- clop_dit/visualization/figS01_supplementary_validation.py +281 -0
- clop_dit/visualization/figS02_expression_diagnostics.py +529 -0
- clop_dit/visualization/panel_geometry.py +76 -0
- clop_dit/visualization/panels_classifier.py +17 -0
- clop_dit/visualization/panels_clustering.py +9 -0
- clop_dit/visualization/panels_conditioning.py +14 -0
- clop_dit/visualization/panels_de_concordance.py +3 -0
- clop_dit/visualization/panels_diversity.py +14 -0
- clop_dit/visualization/panels_embedding.py +9 -0
- clop_dit/visualization/panels_expression.py +29 -0
- clop_dit/visualization/panels_heatmaps.py +20 -0
- clop_dit/visualization/panels_merged.py +9 -0
- clop_dit/visualization/panels_metrics.py +3 -0
- clop_dit/visualization/panels_quality.py +17 -0
- clop_dit/visualization/panels_training.py +3 -0
- clop_dit/visualization/panels_umap_quality.py +9 -0
- clop_dit/visualization/report.py +49 -0
- clop_dit/visualization/results_visualizer.py +1022 -0
- clop_dit/visualization/style.py +977 -0
- clop_dit/visualization/supplementary/__init__.py +11 -0
- clop_dit/visualization/supplementary/figS1_robustness_ablation.py +465 -0
- clop_dit/visualization/supplementary/figS2_downstream_validation.py +611 -0
- clop_dit/visualization/supplementary/figS3_expression_decoder.py +356 -0
- clop_dit-1.1.0.dist-info/METADATA +156 -0
- clop_dit-1.1.0.dist-info/RECORD +120 -0
- clop_dit-1.1.0.dist-info/WHEEL +5 -0
- clop_dit-1.1.0.dist-info/entry_points.txt +2 -0
- clop_dit-1.1.0.dist-info/licenses/LICENSE +21 -0
- clop_dit-1.1.0.dist-info/top_level.txt +1 -0
clop_dit/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""CLOP-DiT — text-conditioned single-cell latent generation.
|
|
2
|
+
|
|
3
|
+
A three-stage pipeline that generates single-cell expression embeddings from
|
|
4
|
+
a structured five-field text prompt (cell type, tissue, organism, marker
|
|
5
|
+
genes, disease context):
|
|
6
|
+
|
|
7
|
+
1. Data preparation — curate GEO datasets, build per-type captions,
|
|
8
|
+
and cache frozen scGPT cell embeddings and frozen
|
|
9
|
+
BiomedBERT text embeddings with ZCA whitening.
|
|
10
|
+
2. CLOP alignment — prototype-aware SigLIP contrastive alignment
|
|
11
|
+
between text and cell embeddings in a shared
|
|
12
|
+
512-dimensional latent space.
|
|
13
|
+
3. DiT generation — 1-D Diffusion Transformer trained by conditional
|
|
14
|
+
flow matching with classifier-free guidance;
|
|
15
|
+
decoded to gene expression through the frozen
|
|
16
|
+
scGPT decoder for downstream inspection.
|
|
17
|
+
|
|
18
|
+
See the top-level README for installation, usage, and citation.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
__version__ = "1.1.0"
|
|
22
|
+
__author__ = "Zeyu Fu, JianXu Zheng, Jiawei Fu"
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# cell2cell.py — Cell→Cell Latent Editing via Conditional Flow Matching (v2: AdaLN injection)
|
|
2
|
+
"""
|
|
3
|
+
Cell2Cell: Intra-modal conditional generation in scGPT latent space.
|
|
4
|
+
|
|
5
|
+
Analogy to Stable Diffusion img2img:
|
|
6
|
+
Image img2img: image → VAE encoder → latent → add noise → denoise with prompt → VAE decoder → edited image
|
|
7
|
+
Cell2Cell: cell → scGPT encoder → z_src → add noise → flow match with condition → scGPT decoder → edited cell
|
|
8
|
+
|
|
9
|
+
Architecture (v2 — AdaLN injection, NOT token concatenation):
|
|
10
|
+
v1 used [src_tokens; tgt_tokens] concatenation (32 tokens), causing 4× attention cost.
|
|
11
|
+
v2 injects z_src via AdaLN conditioning: combined_cond = t_emb + c_emb + s_emb + src_emb
|
|
12
|
+
This keeps the token count at 16 (same as DiT1D) and adds <5% compute overhead.
|
|
13
|
+
|
|
14
|
+
Source cell signal flows through the SAME AdaLN modulation pathway as timestep, text condition,
|
|
15
|
+
and edit strength — a single vector that modulates every transformer block.
|
|
16
|
+
|
|
17
|
+
Use cases:
|
|
18
|
+
- Perturbation prediction: control cell → drug-treated cell
|
|
19
|
+
- Batch/domain translation: dataset A → dataset B
|
|
20
|
+
- Trajectory editing: cell at time t₀ → cell at time t₁
|
|
21
|
+
- Cell state transition: naive → activated, healthy → diseased
|
|
22
|
+
|
|
23
|
+
Flow Matching formulation for cell2cell:
|
|
24
|
+
z_t = (1-t) * z_noise + t * z_tgt (t ∈ [0, 1])
|
|
25
|
+
Starting from z_src (noised) instead of pure noise:
|
|
26
|
+
z_start = (1-s) * z_src + s * ε (s = edit_strength ∈ [0, 1])
|
|
27
|
+
|
|
28
|
+
Small s → gentle edit (preserve cell identity)
|
|
29
|
+
Large s → strong edit (more freedom, more drift)
|
|
30
|
+
|
|
31
|
+
References:
|
|
32
|
+
- SDEdit (Meng et al., 2021)
|
|
33
|
+
- Flux img2img (Black Forest Labs, 2024)
|
|
34
|
+
- CellOT (Bunne et al., 2023)
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
import math
|
|
38
|
+
import torch
|
|
39
|
+
import torch.nn as nn
|
|
40
|
+
import torch.nn.functional as F
|
|
41
|
+
from typing import Optional, Tuple, Dict
|
|
42
|
+
|
|
43
|
+
from ..utils.constants import (
|
|
44
|
+
CFG_SCALE,
|
|
45
|
+
COND_DROP_PROB,
|
|
46
|
+
DEFAULT_ATTN_DROP,
|
|
47
|
+
DEFAULT_PROJ_DROP,
|
|
48
|
+
DIT_HIDDEN_DIM,
|
|
49
|
+
DIT_MLP_RATIO,
|
|
50
|
+
DIT_NUM_BLOCKS,
|
|
51
|
+
DIT_NUM_HEADS,
|
|
52
|
+
DIT_NUM_TOKENS,
|
|
53
|
+
LATENT_DIM,
|
|
54
|
+
PROJ_DIM,
|
|
55
|
+
)
|
|
56
|
+
from .dit import (
|
|
57
|
+
TimestepEmbedder,
|
|
58
|
+
ConditionEmbedder,
|
|
59
|
+
AdaLNZero,
|
|
60
|
+
DiTBlock,
|
|
61
|
+
DiTFinalLayer,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# ============================================================================
|
|
66
|
+
# Source Cell Embedder — compresses z_src to a conditioning vector
|
|
67
|
+
# ============================================================================
|
|
68
|
+
|
|
69
|
+
class SourceCellEmbedder(nn.Module):
|
|
70
|
+
"""Compresses z_src (512-d) into a conditioning vector (hidden_dim).
|
|
71
|
+
|
|
72
|
+
Unlike v1's SourceCellEncoder which tokenized z_src into 16 tokens
|
|
73
|
+
(doubling the sequence length to 32 and causing 4× attention cost),
|
|
74
|
+
this simply projects z_src into the same hidden_dim space as
|
|
75
|
+
timestep/text embeddings for AdaLN injection.
|
|
76
|
+
|
|
77
|
+
Compute overhead: O(latent_dim × hidden_dim) — negligible.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
def __init__(self, latent_dim: int = 512, hidden_dim: int = 384):
|
|
81
|
+
super().__init__()
|
|
82
|
+
self.mlp = nn.Sequential(
|
|
83
|
+
nn.Linear(latent_dim, hidden_dim),
|
|
84
|
+
nn.SiLU(),
|
|
85
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def forward(self, z_src: torch.Tensor) -> torch.Tensor:
|
|
89
|
+
"""(B, latent_dim) → (B, hidden_dim)"""
|
|
90
|
+
return self.mlp(z_src)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# ============================================================================
|
|
94
|
+
# Edit Strength Embedder
|
|
95
|
+
# ============================================================================
|
|
96
|
+
|
|
97
|
+
class EditStrengthEmbedder(nn.Module):
|
|
98
|
+
"""Embeds the edit strength parameter s ∈ [0, 1].
|
|
99
|
+
|
|
100
|
+
Small s → gentle edit; large s → aggressive edit.
|
|
101
|
+
Uses sinusoidal encoding (same as TimestepEmbedder).
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def __init__(self, hidden_dim: int, frequency_dim: int = 64):
|
|
105
|
+
super().__init__()
|
|
106
|
+
self.mlp = nn.Sequential(
|
|
107
|
+
nn.Linear(frequency_dim, hidden_dim),
|
|
108
|
+
nn.SiLU(),
|
|
109
|
+
nn.Linear(hidden_dim, hidden_dim),
|
|
110
|
+
)
|
|
111
|
+
self.frequency_dim = frequency_dim
|
|
112
|
+
|
|
113
|
+
def forward(self, s: torch.Tensor) -> torch.Tensor:
|
|
114
|
+
s_freq = TimestepEmbedder.sinusoidal_embedding(s, self.frequency_dim)
|
|
115
|
+
return self.mlp(s_freq)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# ============================================================================
|
|
119
|
+
# Cell2Cell DiT — v2: AdaLN Source Injection
|
|
120
|
+
# ============================================================================
|
|
121
|
+
|
|
122
|
+
class Cell2CellDiT(nn.Module):
|
|
123
|
+
"""Cell-to-Cell Latent Editing via Conditional Flow Matching.
|
|
124
|
+
|
|
125
|
+
v2: Source cell injected via AdaLN conditioning (NOT token concatenation).
|
|
126
|
+
|
|
127
|
+
combined_cond = t_emb + c_emb + s_emb + src_emb
|
|
128
|
+
^time ^text ^strength ^source
|
|
129
|
+
|
|
130
|
+
This maintains the SAME 16-token sequence as DiT1D → same attention cost.
|
|
131
|
+
Only overhead vs DiT1D: 3 extra small MLP embedders (~1% compute).
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
def __init__(
|
|
135
|
+
self,
|
|
136
|
+
latent_dim: int = LATENT_DIM,
|
|
137
|
+
hidden_dim: int = DIT_HIDDEN_DIM,
|
|
138
|
+
cond_dim: int = PROJ_DIM,
|
|
139
|
+
num_blocks: int = DIT_NUM_BLOCKS,
|
|
140
|
+
num_heads: int = DIT_NUM_HEADS,
|
|
141
|
+
mlp_ratio: float = DIT_MLP_RATIO,
|
|
142
|
+
num_tokens: int = DIT_NUM_TOKENS,
|
|
143
|
+
cond_drop_prob: float = COND_DROP_PROB,
|
|
144
|
+
src_drop_prob: float = COND_DROP_PROB,
|
|
145
|
+
attn_drop: float = DEFAULT_ATTN_DROP,
|
|
146
|
+
proj_drop: float = DEFAULT_PROJ_DROP,
|
|
147
|
+
):
|
|
148
|
+
super().__init__()
|
|
149
|
+
assert latent_dim % num_tokens == 0
|
|
150
|
+
|
|
151
|
+
self.latent_dim = latent_dim
|
|
152
|
+
self.hidden_dim = hidden_dim
|
|
153
|
+
self.num_tokens = num_tokens
|
|
154
|
+
self.token_dim = latent_dim // num_tokens
|
|
155
|
+
self.src_drop_prob = src_drop_prob
|
|
156
|
+
|
|
157
|
+
# --- Target input projection (IDENTICAL to DiT1D) ---
|
|
158
|
+
self.input_proj = nn.Linear(self.token_dim, hidden_dim)
|
|
159
|
+
self.pos_embed = nn.Parameter(torch.randn(1, num_tokens, hidden_dim) * 0.02)
|
|
160
|
+
|
|
161
|
+
# --- Source cell embedder (lightweight MLP, NOT tokenizer) ---
|
|
162
|
+
self.src_embedder = SourceCellEmbedder(latent_dim, hidden_dim)
|
|
163
|
+
self.null_source = nn.Parameter(torch.zeros(1, latent_dim))
|
|
164
|
+
|
|
165
|
+
# --- Timestep, condition, and edit strength embedders ---
|
|
166
|
+
self.t_embedder = TimestepEmbedder(hidden_dim)
|
|
167
|
+
self.c_embedder = ConditionEmbedder(cond_dim, hidden_dim, dropout_prob=cond_drop_prob)
|
|
168
|
+
self.s_embedder = EditStrengthEmbedder(hidden_dim)
|
|
169
|
+
|
|
170
|
+
# --- Transformer backbone (SAME blocks as DiT1D, SAME token count) ---
|
|
171
|
+
self.blocks = nn.ModuleList([
|
|
172
|
+
DiTBlock(hidden_dim, num_heads, mlp_ratio, attn_drop, proj_drop)
|
|
173
|
+
for _ in range(num_blocks)
|
|
174
|
+
])
|
|
175
|
+
|
|
176
|
+
# --- Output projection ---
|
|
177
|
+
self.final_layer = DiTFinalLayer(hidden_dim, self.token_dim)
|
|
178
|
+
|
|
179
|
+
self._init_weights()
|
|
180
|
+
|
|
181
|
+
def _init_weights(self):
|
|
182
|
+
def _init(module):
|
|
183
|
+
if isinstance(module, nn.Linear):
|
|
184
|
+
nn.init.xavier_uniform_(module.weight)
|
|
185
|
+
if module.bias is not None:
|
|
186
|
+
nn.init.zeros_(module.bias)
|
|
187
|
+
self.input_proj.apply(_init)
|
|
188
|
+
nn.init.normal_(self.pos_embed, std=0.02)
|
|
189
|
+
|
|
190
|
+
def forward(
|
|
191
|
+
self,
|
|
192
|
+
z_t: torch.Tensor,
|
|
193
|
+
t: torch.Tensor,
|
|
194
|
+
z_src: torch.Tensor,
|
|
195
|
+
cond: torch.Tensor,
|
|
196
|
+
edit_strength: Optional[torch.Tensor] = None,
|
|
197
|
+
force_drop_cond: bool = False,
|
|
198
|
+
force_drop_src: bool = False,
|
|
199
|
+
) -> torch.Tensor:
|
|
200
|
+
"""
|
|
201
|
+
Parameters
|
|
202
|
+
----------
|
|
203
|
+
z_t : (B, latent_dim) Noisy target latent.
|
|
204
|
+
t : (B,) Flow matching timestep ∈ [0, 1].
|
|
205
|
+
z_src : (B, latent_dim) Source cell embedding (anchor).
|
|
206
|
+
cond : (B, cond_dim) Edit condition vector.
|
|
207
|
+
edit_strength : (B,) Edit strength ∈ [0, 1]. Default 0.5.
|
|
208
|
+
force_drop_cond : bool Drop condition (for CFG).
|
|
209
|
+
force_drop_src : bool Drop source (for CFG).
|
|
210
|
+
|
|
211
|
+
Returns
|
|
212
|
+
-------
|
|
213
|
+
v_pred : (B, latent_dim) predicted velocity field
|
|
214
|
+
"""
|
|
215
|
+
B = z_t.shape[0]
|
|
216
|
+
device = z_t.device
|
|
217
|
+
|
|
218
|
+
# --- Source cell dropout ---
|
|
219
|
+
if force_drop_src:
|
|
220
|
+
z_src_input = self.null_source.expand(B, -1)
|
|
221
|
+
elif self.training and self.src_drop_prob > 0:
|
|
222
|
+
mask = torch.bernoulli(
|
|
223
|
+
torch.ones(B, 1, device=device) * self.src_drop_prob
|
|
224
|
+
)
|
|
225
|
+
z_src_input = z_src * (1 - mask) + self.null_source.expand(B, -1) * mask
|
|
226
|
+
else:
|
|
227
|
+
z_src_input = z_src
|
|
228
|
+
|
|
229
|
+
# --- Tokenize target ONLY (16 tokens, same as DiT1D) ---
|
|
230
|
+
x = z_t.view(B, self.num_tokens, self.token_dim)
|
|
231
|
+
x = self.input_proj(x) + self.pos_embed
|
|
232
|
+
# (B, num_tokens, hidden_dim) ← 16 tokens, NOT 32!
|
|
233
|
+
|
|
234
|
+
# --- Combined AdaLN conditioning (ALL signals as one vector) ---
|
|
235
|
+
t_emb = self.t_embedder(t) # (B, hidden_dim)
|
|
236
|
+
c_emb = self.c_embedder(cond, force_drop=force_drop_cond) # (B, hidden_dim)
|
|
237
|
+
src_emb = self.src_embedder(z_src_input) # (B, hidden_dim)
|
|
238
|
+
|
|
239
|
+
if edit_strength is None:
|
|
240
|
+
edit_strength = torch.full((B,), 0.5, device=device)
|
|
241
|
+
s_emb = self.s_embedder(edit_strength) # (B, hidden_dim)
|
|
242
|
+
|
|
243
|
+
combined_cond = t_emb + c_emb + s_emb + src_emb
|
|
244
|
+
# (B, hidden_dim)
|
|
245
|
+
|
|
246
|
+
# --- Transformer blocks (16 tokens, same cost as DiT1D) ---
|
|
247
|
+
for block in self.blocks:
|
|
248
|
+
x = block(x, combined_cond)
|
|
249
|
+
|
|
250
|
+
# --- Final projection ---
|
|
251
|
+
v_tokens = self.final_layer(x, combined_cond)
|
|
252
|
+
# (B, num_tokens, token_dim)
|
|
253
|
+
|
|
254
|
+
v_pred = v_tokens.reshape(B, self.latent_dim)
|
|
255
|
+
return v_pred
|
|
256
|
+
|
|
257
|
+
def forward_with_cfg(
|
|
258
|
+
self,
|
|
259
|
+
z_t: torch.Tensor,
|
|
260
|
+
t: torch.Tensor,
|
|
261
|
+
z_src: torch.Tensor,
|
|
262
|
+
cond: torch.Tensor,
|
|
263
|
+
edit_strength: Optional[torch.Tensor] = None,
|
|
264
|
+
cfg_scale: float = CFG_SCALE,
|
|
265
|
+
src_cfg_scale: float = 1.5,
|
|
266
|
+
) -> torch.Tensor:
|
|
267
|
+
"""Classifier-Free Guidance with dual conditioning.
|
|
268
|
+
|
|
269
|
+
v_guided = v_uncond
|
|
270
|
+
+ cfg_scale * (v_cond_src - v_uncond)
|
|
271
|
+
+ src_cfg_scale * (v_cond_src - v_nosrc)
|
|
272
|
+
"""
|
|
273
|
+
v_cond_src = self.forward(z_t, t, z_src, cond, edit_strength,
|
|
274
|
+
force_drop_cond=False, force_drop_src=False)
|
|
275
|
+
v_nocond_src = self.forward(z_t, t, z_src, cond, edit_strength,
|
|
276
|
+
force_drop_cond=True, force_drop_src=False)
|
|
277
|
+
v_cond_nosrc = self.forward(z_t, t, z_src, cond, edit_strength,
|
|
278
|
+
force_drop_cond=False, force_drop_src=True)
|
|
279
|
+
v_guided = (v_nocond_src
|
|
280
|
+
+ cfg_scale * (v_cond_src - v_nocond_src)
|
|
281
|
+
+ src_cfg_scale * (v_cond_src - v_cond_nosrc))
|
|
282
|
+
return v_guided
|
|
283
|
+
|
|
284
|
+
@torch.no_grad()
|
|
285
|
+
def edit(
|
|
286
|
+
self,
|
|
287
|
+
z_src: torch.Tensor,
|
|
288
|
+
cond: torch.Tensor,
|
|
289
|
+
edit_strength: float = 0.5,
|
|
290
|
+
num_steps: int = 4,
|
|
291
|
+
cfg_scale: float = CFG_SCALE,
|
|
292
|
+
src_cfg_scale: float = 1.5,
|
|
293
|
+
) -> torch.Tensor:
|
|
294
|
+
"""Edit cells: source cell + condition → edited cell.
|
|
295
|
+
|
|
296
|
+
1. Start from noised source: z_start = (1-s)*z_src + s*ε
|
|
297
|
+
2. Flow towards target condition in remaining steps
|
|
298
|
+
"""
|
|
299
|
+
B = z_src.shape[0]
|
|
300
|
+
device = z_src.device
|
|
301
|
+
|
|
302
|
+
noise = torch.randn_like(z_src)
|
|
303
|
+
z = (1 - edit_strength) * z_src + edit_strength * noise
|
|
304
|
+
|
|
305
|
+
t_start = 1.0 - edit_strength
|
|
306
|
+
dt = edit_strength / max(num_steps, 1)
|
|
307
|
+
s_tensor = torch.full((B,), edit_strength, device=device)
|
|
308
|
+
|
|
309
|
+
for i in range(num_steps):
|
|
310
|
+
t_val = t_start + i * dt
|
|
311
|
+
t = torch.full((B,), t_val, device=device)
|
|
312
|
+
v = self.forward_with_cfg(
|
|
313
|
+
z, t, z_src, cond,
|
|
314
|
+
edit_strength=s_tensor,
|
|
315
|
+
cfg_scale=cfg_scale,
|
|
316
|
+
src_cfg_scale=src_cfg_scale,
|
|
317
|
+
)
|
|
318
|
+
z = z + v * dt
|
|
319
|
+
return z
|
|
320
|
+
|
|
321
|
+
@torch.no_grad()
|
|
322
|
+
def sample_text2cell(
|
|
323
|
+
self,
|
|
324
|
+
cond: torch.Tensor,
|
|
325
|
+
num_steps: int = 4,
|
|
326
|
+
cfg_scale: float = CFG_SCALE,
|
|
327
|
+
) -> torch.Tensor:
|
|
328
|
+
"""Fallback: pure text-to-cell generation (no source cell)."""
|
|
329
|
+
B = cond.shape[0]
|
|
330
|
+
device = next(self.parameters()).device
|
|
331
|
+
|
|
332
|
+
z = torch.randn(B, self.latent_dim, device=device)
|
|
333
|
+
null_src = self.null_source.expand(B, -1)
|
|
334
|
+
dt = 1.0 / num_steps
|
|
335
|
+
|
|
336
|
+
for i in range(num_steps):
|
|
337
|
+
t_val = i / num_steps
|
|
338
|
+
t = torch.full((B,), t_val, device=device)
|
|
339
|
+
v_cond = self.forward(z, t, null_src, cond,
|
|
340
|
+
force_drop_cond=False, force_drop_src=True)
|
|
341
|
+
v_uncond = self.forward(z, t, null_src, cond,
|
|
342
|
+
force_drop_cond=True, force_drop_src=True)
|
|
343
|
+
v = v_uncond + cfg_scale * (v_cond - v_uncond)
|
|
344
|
+
z = z + v * dt
|
|
345
|
+
return z
|
|
346
|
+
|
|
347
|
+
def count_parameters(self) -> dict:
|
|
348
|
+
total = sum(p.numel() for p in self.parameters())
|
|
349
|
+
trainable = sum(p.numel() for p in self.parameters() if p.requires_grad)
|
|
350
|
+
return {
|
|
351
|
+
"total": total,
|
|
352
|
+
"trainable": trainable,
|
|
353
|
+
"total_M": f"{total / 1e6:.2f}M",
|
|
354
|
+
"trainable_M": f"{trainable / 1e6:.2f}M",
|
|
355
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# clop — Contrastive Language-Omics Pre-training (CLOP) Alignment
|
|
2
|
+
"""
|
|
3
|
+
CLOP Aligner and loss/transform components.
|
|
4
|
+
Re-export so "from clop_dit.architecture.clop import CLOPAligner" etc. still work.
|
|
5
|
+
"""
|
|
6
|
+
from .losses import (
|
|
7
|
+
TextWhiteningTransform,
|
|
8
|
+
ProjectionHead,
|
|
9
|
+
SigLIPLoss,
|
|
10
|
+
PrototypeSigLIPLoss,
|
|
11
|
+
InfoNCELoss,
|
|
12
|
+
)
|
|
13
|
+
from .aligner import CLOPAligner
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"CLOPAligner",
|
|
17
|
+
"TextWhiteningTransform",
|
|
18
|
+
"ProjectionHead",
|
|
19
|
+
"SigLIPLoss",
|
|
20
|
+
"PrototypeSigLIPLoss",
|
|
21
|
+
"InfoNCELoss",
|
|
22
|
+
]
|