To use a Vision Transformer as an augmentation model, use AugmentParams with input_shape, hidden_position, and an extract_blocks_fn. The extract_blocks_fn is used to identify the specific modules (like Attention blocks) that will be used for connectivity.
Example configuration for a ViT:
AugmentParams(
model=vit,
input_shape=(3, 256, 256),
hidden_position='input',
extract_blocks_fn=lambda vit: [m for m in vit.modules() if isinstance(m, Attention)]
)
from vit_pytorch.vit import ViT, Attention
from CALM_pytorch import CALM, AugmentParams
# ... setup anchor_llm, augment_llm1, augment_llm2, vit ...
calm = CALM(
anchor_llm=anchor_llm,
augment_llms=(
AugmentParams(model=augment_llm1, mask_kwarg='mask'),
AugmentParams(model=augment_llm2, mask_kwarg='mask'),
AugmentParams(
model=vit,
input_shape=(3, 256, 256),
hidden_position='input',
extract_blocks_fn=lambda vit: [m for m in vit.modules() if isinstance(m, Attention)]
)
),
attn_kwargs=dict(
linear_project_context=True,
pre_rmsnorm=True,
flash=True
)
)
# Prompt can be a tuple for multi-modal inputs
prompt = (
torch.randint(0, 20000, (1, 256)),
torch.randint(0, 20000, (1, 256)),
torch.randn(1, 3, 256, 256)
)
loss = calm(seq, mask=mask, prompt=prompt)