AIMv2
AIMv2 is a vision transformer trained autoregressively on image-text pairs. Rather than a contrastive or reconstruction objective, it predicts image tokens in a causal left-to-right sequence conditioned on a multimodal prefix, producing strong general-purpose patch-level representations.
Paper: "Multimodal Autoregressive Pre-training of Large Vision Encoders" (El-Nouby et al., CVPR 2025) Code: github.com/apple/ml-aim
The jimm implementation returns all patch token embeddings after the final RMSNorm with shape (batch, n_patches, hidden_size). AIMv2 has no CLS token; downstream tasks pool over patches or select specific tokens.
Key architectural differences from standard ViT:
| Feature | ViT | AIMv2 |
|---|---|---|
| Normalization | LayerNorm | RMSNorm |
| MLP | Standard GELU | SwiGLU |
| Pre-norm placement | After patch + pos | On patches only, before pos |
| CLS token | ✅ | ❌ |
| Output | CLS embedding (B, D) |
All patches (B, N, D) |
| Attention / MLP biases | Yes | No¹ |
¹ Attention and MLP projections have no bias. The patch embedding convolution does have a bias (use_patch_bias=True), consistent with the HuggingFace checkpoint.
Supported models
All AIMv2 models use patch size 14 and 24 transformer layers regardless of scale. The four architecture sizes scale width only.
Large - 307M params
| HuggingFace ID | hidden_size |
Heads | intermediate_size |
Image size |
|---|---|---|---|---|
apple/aimv2-large-patch14-224 |
1024 | 8 | 2816 | 224 |
apple/aimv2-large-patch14-336 |
1024 | 8 | 2816 | 336 |
apple/aimv2-large-patch14-448 |
1024 | 8 | 2816 | 448 |
apple/aimv2-large-patch14-224-distilled |
1024 | 8 | 2816 | 224 |
apple/aimv2-large-patch14-224-lit |
1024 | 8 | 2816 | 224 |
Huge - 680M params
| HuggingFace ID | hidden_size |
Heads | intermediate_size |
Image size |
|---|---|---|---|---|
apple/aimv2-huge-patch14-224 |
1536 | 12 | 4096 | 224 |
apple/aimv2-huge-patch14-336 |
1536 | 12 | 4096 | 336 |
apple/aimv2-huge-patch14-448 |
1536 | 12 | 4096 | 448 |
1B - 1.2B params
| HuggingFace ID | hidden_size |
Heads | intermediate_size |
Image size |
|---|---|---|---|---|
apple/aimv2-1B-patch14-224 |
2048 | 16 | 5632 | 224 |
apple/aimv2-1B-patch14-336 |
2048 | 16 | 5632 | 336 |
apple/aimv2-1B-patch14-448 |
2048 | 16 | 5632 | 448 |
3B - 2.7B params
| HuggingFace ID | hidden_size |
Heads | intermediate_size |
Image size |
|---|---|---|---|---|
apple/aimv2-3B-patch14-224 |
3072 | 24 | 8192 | 224 |
apple/aimv2-3B-patch14-336 |
3072 | 24 | 8192 | 336 |
apple/aimv2-3B-patch14-448 |
3072 | 24 | 8192 | 448 |
Note:
apple/aimv2-large-patch14-nativeuses sinusoidal position embeddings for variable resolution and is not currently supported. The lit model is a full multimodal checkpoint (vision + text encoder + projectors);AIMv2Model.from_pretrainedautomatically extracts just the vision encoder from it.
Basic usage
import jimm
import numpy as np
model = jimm.AIMv2Model.from_pretrained("apple/aimv2-large-patch14-224")
images = np.random.rand(4, 224, 224, 3).astype(np.float32)
patch_embeddings = model(images) # shape: (4, 256, 1024)
The model returns all 256 patch tokens (16×16 grid) for a 224×224 image with patch size 14. To get a single image embedding, pool over patches:
# Mean pool across patch dimension
image_embeddings = patch_embeddings.mean(axis=1) # shape: (4, 1024)
Flash / Splash Attention
AIMv2 supports hardware-accelerated attention via Tokamax:
| Backend | Hardware | Notes |
|---|---|---|
"mosaic" |
NVIDIA H100 (SM90) / B100 (SM100) | Pallas Mosaic GPU kernel |
"triton" |
Any NVIDIA GPU | Pallas Triton kernel |
"cudnn" |
NVIDIA GPU | Via JAX-NN / cuDNN |
"mosaic_tpu" |
TPU v5+ (all generations) | Splash attention (block-sparse) |
"xla_chunked" |
GPU / TPU | Flash-style chunked XLA |
"xla" |
Any | Standard XLA fallback |
import jimm
# GPU: try H100 Mosaic kernel, fall back to Triton, then XLA
model = jimm.AIMv2Model.from_pretrained("apple/aimv2-large-patch14-448",
attention_fn=jimm.make_tokamax_attention(["mosaic", "triton", "xla"]))
# TPU: try Splash attention, fall back to chunked XLA
model = jimm.AIMv2Model.from_pretrained("apple/aimv2-large-patch14-448",
attention_fn=jimm.make_tokamax_attention(["mosaic_tpu", "xla_chunked"]))
Note: AIMv2-Large produces 256 patch tokens at 224×224, 576 at 336×336, and 1024 at 448×448. Flash attention is most beneficial at the larger image sizes.
FSDP / Explicit Sharding
AIMv2 supports JAX explicit sharding (FSDP-style) via AIMv2Sharding, which inherits ViTSharding with no overrides needed (AIMv2 has no LayerScale, no register tokens, and no MLP bias tensors to shard).
from jax.experimental import mesh_utils
from jax.sharding import AxisType, Mesh
import jax
n_devices = jax.device_count()
mesh = Mesh(
mesh_utils.create_device_mesh((1, n_devices)),
("data", "fsdp"),
axis_types=(AxisType.Explicit, AxisType.Explicit),
)
jax.set_mesh(mesh)
model = jimm.AIMv2Model.from_pretrained("apple/aimv2-large-patch14-224")
# model params are automatically sharded across fsdp axis
To disable sharding, pass sharding=NoSharding() (import: from jimm.common.sharding import NoSharding).
jimm.models.aimv2.AIMv2Model
Bases: Module
AIMv2 vision encoder.
Implements the AIMv2 vision transformer with RMSNorm, SwiGLU MLP, no attention/MLP biases, and absolute learnable positional embeddings. Returns all patch token embeddings (no CLS token).
Source code in src/jimm/models/aimv2/aimv2_model.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
__call__(x)
Run AIMv2 forward pass and return all patch token embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Array, 'batch height width channels']
|
Batch of input images in BHWC format. Height and width must each be divisible by patch_size. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, 'batch n_patches hidden_size']
|
Float[Array, "batch n_patches hidden_size"]: Patch token embeddings after final RMSNorm. |
Source code in src/jimm/models/aimv2/aimv2_model.py
95 96 97 98 99 100 101 102 103 104 105 | |
__init__(img_size=224, patch_size=14, in_channels=3, hidden_size=1024, num_layers=24, num_heads=8, mlp_dim=2816, rms_norm_eps=1e-05, use_gradient_checkpointing=False, attention_fn=None, rngs=None, dtype=jnp.float32, param_dtype=jnp.float32, sharding=None)
Initialize AIMv2Model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_size
|
int
|
Input image size. Defaults to 224. |
224
|
patch_size
|
int
|
Patch size. Defaults to 14. |
14
|
in_channels
|
int
|
Number of input channels. Defaults to 3. |
3
|
hidden_size
|
int
|
Hidden dimension. Defaults to 1024 (Large). |
1024
|
num_layers
|
int
|
Number of transformer layers. Defaults to 24 (Large). |
24
|
num_heads
|
int
|
Number of attention heads. Defaults to 8 (Large). |
8
|
mlp_dim
|
int
|
MLP intermediate dimension. Defaults to 2816 (Large). |
2816
|
rms_norm_eps
|
float
|
RMSNorm epsilon. Defaults to 1e-5. |
1e-05
|
use_gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
attention_fn
|
Callable[..., Any] | None
|
Custom attention function. Defaults to None. |
None
|
rngs
|
Rngs | None
|
RNG state. If None, initializes to nnx.Rngs(0). |
None
|
dtype
|
DTypeLike
|
Computation dtype. Defaults to jnp.float32. |
float32
|
param_dtype
|
DTypeLike
|
Parameter dtype. Defaults to jnp.float32. |
float32
|
sharding
|
ShardingSpec | None
|
Sharding specification. Defaults to AIMv2Sharding(). |
None
|
Source code in src/jimm/models/aimv2/aimv2_model.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
from_config(config, *, rngs=None, dtype=jnp.float32, param_dtype=jnp.float32, sharding=None, use_gradient_checkpointing=False, attention_fn=None)
classmethod
Create AIMv2Model from a HuggingFace-compatible config dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Configuration dictionary in HuggingFace AIMv2 format. |
required |
rngs
|
Rngs | None
|
RNG state. If None, initializes to nnx.Rngs(0). |
None
|
dtype
|
DTypeLike
|
Computation dtype. Defaults to jnp.float32. |
float32
|
param_dtype
|
DTypeLike
|
Parameter dtype. Defaults to jnp.float32. |
float32
|
sharding
|
ShardingSpec | None
|
Sharding specification. Defaults to AIMv2Sharding(). |
None
|
use_gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
attention_fn
|
Callable[..., Any] | None
|
Custom attention function. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
AIMv2Model |
AIMv2Model
|
Model with randomly initialized weights matching the given config. |
Source code in src/jimm/models/aimv2/aimv2_model.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
from_pretrained(model_name_or_path, use_pytorch=False, rngs=None, dtype=jnp.float32, param_dtype=jnp.float32, sharding=None, use_gradient_checkpointing=False, attention_fn=None)
classmethod
Load a pretrained AIMv2 model from a local path or HuggingFace Hub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name_or_path
|
str
|
Local directory or HuggingFace model ID (e.g. "apple/aimv2-large-patch14-224"). |
required |
use_pytorch
|
bool
|
Whether to load from PyTorch weights. Defaults to False. |
False
|
rngs
|
Rngs | None
|
RNG state. If None, initializes to nnx.Rngs(0). |
None
|
dtype
|
DTypeLike
|
Computation dtype. Defaults to jnp.float32. |
float32
|
param_dtype
|
DTypeLike
|
Parameter dtype. Defaults to jnp.float32. |
float32
|
sharding
|
ShardingSpec | None
|
Sharding specification. Defaults to AIMv2Sharding(). |
None
|
use_gradient_checkpointing
|
bool
|
Whether to use gradient checkpointing. Defaults to False. |
False
|
attention_fn
|
Callable[..., Any] | None
|
Custom attention function. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
AIMv2Model |
AIMv2Model
|
Model with pretrained weights loaded. |
Source code in src/jimm/models/aimv2/aimv2_model.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
save_pretrained(save_directory)
Save model weights and config in HuggingFace format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_directory
|
str
|
Directory path where the model will be saved. |
required |
Source code in src/jimm/models/aimv2/aimv2_model.py
107 108 109 110 111 112 113 114 115 | |