MolecularDiffusion.modules.models.chefnmr.layers¶
DiT blocks for ChefNMR (MIT, (c) 2025 Ziyu Xiong).
Upstream: src/model/modules/layers.py, modified from DiT
(facebookresearch/DiT) and timm’s VisionTransformer.
Kept in modules/models/chefnmr/ rather than promoted to
modules/layers/: these blocks carry an explicit atom mask through
attention and use adaLN-zero conditioning on a fused
time + spectrum vector. Nothing else in the tree consumes that shape
today, and a shared block would have to grow the mask argument anyway.
Attributes¶
Classes¶
Masked multi-head self-attention over the atom axis. |
|
DiT block with adaptive layer norm zero (adaLN-Zero) conditioning. |
|
adaLN-zero output head: hidden -> coordinate update. |
|
timm's ViT MLP. |
|
Sinusoidal noise-level embedding + 2-layer MLP. |
Functions¶
|
Module Contents¶
- class MolecularDiffusion.modules.models.chefnmr.layers.Attention(dim: int, num_heads: int = 8, qkv_bias: bool = False, qk_norm: bool = False, proj_bias: bool = True, attn_drop: float = 0.0, proj_drop: float = 0.0, norm_layer: Type[torch.nn.Module] = nn.LayerNorm)¶
Bases:
torch.nn.ModuleMasked multi-head self-attention over the atom axis.
fused_attnis hard-off, exactly as upstream: the released weights were trained through the explicit softmax path, andscaled_dot_product_attentiondiffers in how it handles a fully masked row.- forward(x: torch.Tensor, mask: torch.Tensor) torch.Tensor¶
- attn_drop¶
- fused_attn = False¶
- head_dim¶
- k_norm¶
- num_heads = 8¶
- proj¶
- proj_drop¶
- q_norm¶
- qkv¶
- scale¶
- class MolecularDiffusion.modules.models.chefnmr.layers.DiTBlock(hidden_size: int, n_heads: int, mlp_ratio: float = 4, **block_kwargs)¶
Bases:
torch.nn.ModuleDiT block with adaptive layer norm zero (adaLN-Zero) conditioning.
- forward(x: torch.Tensor, c: torch.Tensor, atom_mask: torch.Tensor) torch.Tensor¶
- adaLN_modulation¶
- attn¶
- mlp¶
- norm1¶
- norm2¶
- class MolecularDiffusion.modules.models.chefnmr.layers.FinalLayer(hidden_size: int, out_atom_coords_size: int)¶
Bases:
torch.nn.ModuleadaLN-zero output head: hidden -> coordinate update.
- forward(x: torch.Tensor, c: torch.Tensor) torch.Tensor¶
- adaLN_modulation¶
- linear¶
- norm_final¶
- class MolecularDiffusion.modules.models.chefnmr.layers.Mlp(in_features: int, hidden_features: int = None, out_features: int = None, act_layer=nn.GELU, norm_layer=None, bias: bool = True, drop: float = 0.0, use_conv: bool = False)¶
Bases:
torch.nn.Moduletimm’s ViT MLP.
- forward(x: torch.Tensor) torch.Tensor¶
- act¶
- drop1¶
- drop2¶
- fc1¶
- fc2¶
- norm¶
- class MolecularDiffusion.modules.models.chefnmr.layers.TimestepEmbedder(hidden_size: int, frequency_embedding_size: int = 256)¶
Bases:
torch.nn.ModuleSinusoidal noise-level embedding + 2-layer MLP.
- forward(t: torch.Tensor) torch.Tensor¶
- static timestep_embedding(t: torch.Tensor, dim: int, max_period: int = 10000) torch.Tensor¶
- frequency_embedding_size = 256¶
- mlp¶
- MolecularDiffusion.modules.models.chefnmr.layers.modulate(x: torch.Tensor, shift: torch.Tensor, scale: torch.Tensor) torch.Tensor¶
- MolecularDiffusion.modules.models.chefnmr.layers.to_2tuple¶