def collate_fn(batch):
images = [sample['image'] for sample in batch]
images = [moondream.vision_encoder.preprocess(image) for image in images]
code
labels_acc = []
tokens_acc = []
for sample in batch:
toks = [tokenizer.bos_token_id]
labs = [-100] * (IMG_TOKENS + 1)
for qa in sample['qa']:
q_t = tokenizer(
f"\n\nQuestion: {qa['question']}\n\nAnswer:",
add_special_tokens=False
).input_ids
toks.extend(q_t)
labs.extend([-100] * len(q_t))
a_t = tokenizer(
f" {qa['answer']}{ANSWER_EOS}",
add_special_tokens=False
).input_ids
toks.extend(a_t)
labs.extend(a_t)
tokens_acc.append(toks)
labels_acc.append(labs)
max_len = -1
for labels in labels_acc:
max_len = max(max_len, len(labels))
attn_mask_acc = []
for i in range(len(batch)):
len_i = len(labels_acc[i])
pad_i = max_len - len_i
labels_acc[i].extend([-100] * pad_i)
tokens_acc[i].extend([tokenizer.eos_token_id] * pad_i)
attn_mask_acc.append([1] * len_i + [0] * pad_i)
return (
images,
torch.stack([torch.tensor(t, dtype=torch.long) for t in tokens_acc]),
torch.stack([torch.tensor(l, dtype=torch.long) for l in labels_acc]),
torch.stack([torch.tensor(a, dtype=torch.bool) for a in attn_mask_acc]),
)
what is does? what acc means
Assistant
The collate_fn function is likely part of a data preprocessing step used in preparing batches of data for a machine learning model, often in the context of training or inference using a PyTorch DataLoader. Let's break down what each part does:
Image Preprocessing:
The function extracts images from each sample in the input batch and preprocesses them using some method within moondream.vision_encoder. The processed images are then stored in a list.
User
Assistant
Token and Label Processing:
tokens_acc and labels_acc are lists that accumulate token ids and labels respectively for each sample in the batch.
For each question-answer pair in a sample, it constructs token ids for the question and answer, adds them to a running list (toks), and constructs corresponding label ids (labs). Special tokens like bos_token_id (beginning of sequence) and ANSWER_EOS (end of answer) are used to format the input.
The labels appear to be aligned with the generated tokens for a language model where only certain parts (e.g., the answer) are actually used for loss calculation, while other parts are masked out with -100.
Padding:
All sequences are padded to the length of the longest sequence in the batch to ensure uniform input sizes, a requirement for batch processing in neural networks.
attn_mask_acc is created to keep track of which tokens are actual data and which are padding (using 1s and 0s respectively).
Return:
The function returns processed images and stacks of tensors containing tokens, labels, and attention masks. These tensors are then ready to be used for model input.
Regarding the term "acc" in labels_acc, tokens_acc, and attn_mask_acc, it typically stands for "accumulate" or "accumulator." These lists accumulate data items over each sample in the batch, effectively building up the batch's data details before final conversion into tensors.
ANSWER_EOS = "<|endoftext|>"
Number of tokens used to represent each image.
IMG_TOKENS = 729
def collate_fn(batch):
images = [sample['image'] for sample in batch]
images = [moondream.vision_encoder.preprocess(image) for image in images]
code
labels_acc = []
tokens_acc = []
for sample in batch:
toks = [tokenizer.bos_token_id]
labs = [-100] * (IMG_TOKENS + 1)
for qa in sample['qa']:
q_t = tokenizer(
f"\n\nQuestion: {qa['question']}\n\nAnswer:",
add_special_tokens=False
).input_ids
toks.extend(q_t)
labs.extend([-100] * len(q_t))
a_t = tokenizer(
f" {qa['answer']}{ANSWER_EOS}",
add_special_tokens=False
).input_ids
toks.extend(a_t)
labs.extend(a_t)
tokens_acc.append(toks)
labels_acc.append(labs)
max_len = -1
for labels in labels_acc:
max_len = max(max_len, len(labels))
attn_mask_acc = []
for i in range(len(batch)):
len_i = len(labels_acc[i])
pad_i = max_len - len_i
labels_acc[i].extend([-100] * pad_i)
tokens_acc[i].extend([tokenizer.eos_token_id] * pad_i)
attn_mask_acc.append([1] * len_i + [0] * pad_i)
return (
images,
torch.stack([torch.tensor(t, dtype=torch.long) for t in tokens_acc]),
torch.stack([torch.tensor(l, dtype=torch.long) for l in labels_acc]),
torch.stack([torch.tensor(a, dtype=torch.bool) for a in attn_mask_acc]),
)
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from flash_attn import flash_attn_with_kvcache
from mamba_ssm.models.mixer_seq_simple import _init_weights
from mamba_ssm.modules.mamba2 import Mamba2
from mamba_ssm.modules.mha import _update_kv_cache
from mamba_ssm.utils.generation import GenerationMixin as MambaGenerationMixin
from transformers.modeling_outputs import CausalLMOutput
from transformers.modeling_utils import PreTrainedModel
from .configuration_rene import ReneConfig
class ReneMLP(nn.Module):
"""One-hidden-layer network with GELU activation.
code
Args:
d_input: Block input dimension.
d_output: Block output dimension.
expand: Block expansion factor.
bias: Use biases in linear layers.
"""
def __init__(self, d_input, d_output=None, expand=3, bias=True, device=None, dtype=None):
super().__init__()
factory_kwargs = {"device": device, "dtype": dtype}
self.d_input = d_input
self.d_output = d_input if d_output is None else d_output
self.d_inner = int(round(expand * d_input))
self.in_proj = nn.Linear(self.d_input, self.d_inner, bias=bias, **factory_kwargs)
self.activation = nn.GELU()
self.out_proj = nn.Linear(self.d_inner, self.d_input, bias=bias, **factory_kwargs)
def forward(self, x, inference_params=None):
"""Forward pass through the MLP module."""
y = self.in_proj(x)
y = self.activation(y)
y = self.out_proj(y)
return y
def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
"""Allocate inference cache for ReneMLP. (There is nothing to cache for this module)."""
return None
class ReneMHA(nn.Module):
"""Multi-head self-attention. Adapted from mamba_ssm MHA class."""
code
class Block(nn.Module):
"""Simple residual block with normalization that wraps an inner "mixer" module."""
code
def __init__(self, dim, mixer_cls, norm_cls=nn.LayerNorm, residual_in_fp32=False):
"""
dim: The dimension of the input data.
mixer_cls: The class of the mixer module.
norm_cls: The class of the normalization module.
residual_in_fp32: Whether to keep residuals in fp32.
"""
super().__init__()
self.residual_in_fp32 = residual_in_fp32
self.norm = norm_cls(dim)
self.mixer = mixer_cls(dim)
def forward(self, x, inference_params=None, **mixer_kwargs):
"""Forward pass through the block."""
y = self.norm(x.to(dtype=self.norm.weight.dtype))
y = self.mixer(y, inference_params=inference_params, **mixer_kwargs)
residual = x
if self.residual_in_fp32:
residual = residual.to(torch.float32)
y = y + residual
y = y.to(dtype=x.dtype)
return y
def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
"""Allocate inference cache for the mixer module."""
return self.mixer.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)
def _create_block(
d_model,
norm_cls,
ssm_cfg=None,
attn_layer_idx=None,
attn_cfg=None,
mlp_layer_idx=None,
mlp_cfg=None,
residual_in_fp32=False,
layer_idx=None,
device=None,
dtype=None,
):
factory_kwargs = {"device": device, "dtype": dtype}
if ssm_cfg is None:
ssm_cfg = {}
if attn_layer_idx is None:
attn_layer_idx = []
if attn_cfg is None:
attn_cfg = {}
if mlp_layer_idx is None:
mlp_layer_idx = []
if mlp_cfg is None:
mlp_cfg = {}
if layer_idx in attn_layer_idx:
mixer_cls = partial(ReneMHA, layer_idx=layer_idx, **attn_cfg, **factory_kwargs)
elif layer_idx in mlp_layer_idx:
mixer_cls = partial(ReneMLP, **mlp_cfg, **factory_kwargs)
else:
mixer_cls = partial(Mamba2, layer_idx=layer_idx, **ssm_cfg, **factory_kwargs)
return Block(d_model, mixer_cls, norm_cls=norm_cls, residual_in_fp32=residual_in_fp32)
class MixerModel(nn.Module):
"""Adapted from mamba_ssm.models.mixer_seq_simple.MixerModel."""
code
def __init__(
self,
d_model: int,
n_layer: int,
vocab_size: int,
ssm_cfg=None,
attn_layer_idx=None,
attn_cfg=None,
mlp_layer_idx=None,
mlp_cfg=None,
norm_epsilon: float = 1e-5,
rms_norm: bool = False,
initializer_cfg=None,
residual_in_fp32=False,
device=None,
dtype=None,
) -> None:
super().__init__()
factory_kwargs = {"device": device, "dtype": dtype}
self.residual_in_fp32 = residual_in_fp32
if rms_norm:
from mamba_ssm.ops.triton.layer_norm import RMSNorm as norm_cls_base
else:
norm_cls_base = nn.LayerNorm
norm_cls = partial(norm_cls_base, eps=norm_epsilon, **factory_kwargs)
self.embedding = nn.Embedding(vocab_size, d_model, **factory_kwargs)
self.layers = nn.ModuleList(
[
_create_block(
d_model,
norm_cls=norm_cls,
ssm_cfg=ssm_cfg,
attn_layer_idx=attn_layer_idx,
attn_cfg=attn_cfg,
mlp_layer_idx=mlp_layer_idx,
mlp_cfg=mlp_cfg,
residual_in_fp32=residual_in_fp32,
layer_idx=i,
**factory_kwargs,
)
for i in range(n_layer)
]
)
self.norm_f = norm_cls(d_model)
self.apply(
partial(
_init_weights,
n_layer=n_layer,
**(initializer_cfg if initializer_cfg is not None else {}),
n_residuals_per_layer=1,
)
)
def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
"""Allocate inference cache for all layers."""
return {
i: layer.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)
for i, layer in enumerate(self.layers)
}
def forward(self, input_ids, inference_params=None, **mixer_kwargs):
"""Forward pass through the model."""
hidden_states = self.embedding(input_ids)
for layer in self.layers:
hidden_states = layer(hidden_states, inference_params=inference_params, **mixer_kwargs)
hidden_states = self.norm_f(hidden_states.to(dtype=self.norm_f.weight.dtype))
return hidden_states
class ReneLMHeadModel(PreTrainedModel, MambaGenerationMixin):
"""
Rene language model architecture.
Based on mamba_ssm.models.mixer_seq_simple.MambaLMHeadModel, with several adaptations.
"""
code
config_class = ReneConfig
base_model_prefix = "backbone"
_no_split_modules = ["Block", "Mamba2"]
supports_gradient_checkpointing = True
_is_stateful = True
_tied_weights_keys = ["lm_head.weight"]
def __init__(
self,
config: ReneConfig,
initializer_cfg=None,
device=None,
dtype=None,
) -> None:
super().__init__(config)
d_model = config.d_model
n_layer = config.n_layer
vocab_size = config.vocab_size
ssm_cfg = config.ssm_cfg
attn_layer_idx = config.attn_layer_idx
attn_cfg = config.attn_cfg
mlp_layer_idx = config.mlp_layer_idx
mlp_cfg = config.mlp_cfg
rms_norm = config.rms_norm
residual_in_fp32 = config.residual_in_fp32
pad_vocab_size_multiple = config.pad_vocab_size_multiple
factory_kwargs = {"device": device, "dtype": dtype}
if set(attn_layer_idx).intersection(mlp_layer_idx):
raise ValueError(f"Conflicting {attn_layer_idx=} and {mlp_layer_idx=}")
if vocab_size % pad_vocab_size_multiple != 0:
vocab_size += pad_vocab_size_multiple - (vocab_size % pad_vocab_size_multiple)
self.backbone = MixerModel(
d_model=d_model,
n_layer=n_layer,
vocab_size=vocab_size,
ssm_cfg=ssm_cfg,
attn_layer_idx=attn_layer_idx,
attn_cfg=attn_cfg,
mlp_layer_idx=mlp_layer_idx,
mlp_cfg=mlp_cfg,
rms_norm=rms_norm,
initializer_cfg=initializer_cfg,
residual_in_fp32=residual_in_fp32,
**factory_kwargs,
)
self.lm_head = nn.Linear(d_model, vocab_size, bias=False, **factory_kwargs)
# Initialize weights
self.apply(
partial(
_init_weights,
n_layer=n_layer,
**(initializer_cfg if initializer_cfg is not None else {}),
)
)
self.tie_weights()
def tie_weights(self):
"""Tie embeddings and softmax layer weights if specified by config."""
if self.config.tie_word_embeddings:
self.lm_head.weight = self.backbone.embedding.weight
def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
"""Allocate inference cache."""
return self.backbone.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)
def forward(
self, input_ids, position_ids=None, inference_params=None, num_last_tokens=0, **mixer_kwargs
):
"""
"position_ids" is just to be compatible with Transformer generation. We don't use it.
num_last_tokens: if > 0, only return the logits for the last n tokens.
"""
hidden_states = self.backbone(input_ids, inference_params=inference_params, **mixer_kwargs)
if num_last_tokens > 0:
hidden_states = hidden_states[:, -num_last_tokens:]
lm_logits = self.lm_head(hidden_states)
return CausalLMOutput(logits=lm_logits)
def generate(self, *args, **kwargs):
"""
Calls the custom `generate` method from `mamba_ssm.utils.generation.GenerationMixin`.
Refer to that method for argument names and defaults.
"""
return MambaGenerationMixin.generate(self, *args, **kwargs)
To adapt the provided collate_fn and compute_loss functions for the new "Rene" model (assumed to be a replacement for the original moondream framework), we need to ensure that the interaction between the data and the model matches the new architecture. Here's how you can modify the code accordingly:
Replace moondream.vision_encoder with appropriate image feature extraction (if any):
It's assumed that the "Rene" model doesn't directly deal with images. So, you'll need to decide how images are integrated into the text processing pipeline or clarified if this is part of a larger system where image embedding doesn't change.
Replace the text model interface with "ReneLMHeadModel", and ensure input_ids are used:
The ReneLMHeadModel has a specific forward signature primarily focused on processing token sequences.
Rewrite compute_loss to fit ReneLMHeadModel:
You'll need to adapt your computational graph setup to integrate with "Rene." Based on ReneLMHeadModel, it seems input tokens are processed through its backbone and then a language modeling head.
Here's how you might approach these changes:
python
def collate_fn(batch):
images = [sample['image'] for sample in batch]
# If the model doesn't directly process images, this part needs clarification
# images = [some_image_processing_function(image) for image in images]
labels_acc = []
tokens_acc = []
for sample in batch:
toks = [tokenizer.bos_token_id]
labs = [-100] * (IMG_TOKENS + 1) # Needs proper handling of image tokens
for qa in sample['qa']:
q_t = tokenizer(
f"\n\nQuestion: {qa['question']}\n\nAnswer:",
add_special_tokens=False
).input_ids
toks.extend(q_t)
labs.extend([-100] * len(q_t))
a_t = tokenizer(
f" {qa['answer']}{ANSWER_EOS}",
add_special_tokens=False
).input_ids
toks.extend(a_t)
labs.extend(a_t)
tokens_acc.append(toks)
labels_acc.append(labs)
max_len = max(len(labels) for labels in labels_acc)
attn_mask_acc = []
for i in range(len(batch)):
len_i = len(labels_acc[i])
pad_i = max_len - len_i
labels_acc[i].extend([-100] * pad_i)
tokens_acc[i].extend([tokenizer.eos_token_id] * pad_i)
attn_mask_acc.append([1] * len_i + [0] * pad_i)
return (
images,
torch.stack([torch.tensor(t, dtype=torch.long) for t in tokens_acc]),
torch.stack([torch.tensor(l, dtype=torch.long) for l in labels_acc]),
torch.stack([torch.tensor(a, dtype=torch.bool) for a in attn_mask_acc]),
)
def compute_loss(batch, model: ReneLMHeadModel, device=torch.device("cpu")):
# Unpack the batch
_, tokens, labels, attn_mask = batch # Assuming model doesn't take images directly
tokens = tokens.to(device)
labels = labels.to(device)
attn_mask = attn_mask.to(device)
# Forward pass through "Rene" model
outputs = model(input_ids=tokens, attention_mask=attn_mask, labels=labels)
return outputs.loss
Key Adaptation Points:
Image Integration:
Clarify the role of images if they're part of a pre-processing step or a side input to another module not seen here.
Token Processing:
Use the "Rene" model's input methods, ensuring compatibility with the architecture.
Inputs Alignment:
Ensure that the attention mask and labels align correctly with the token ids when invoking the Rene model.
Device Handling:
Ensure that all tensors are moved to the necessary device (CPU or GPU) before model invocation.
This setup assumes that image tokens (IMG_TOKENS) and preprocessing are correctly applied elsewhere or integrated differently into the workflow. If "Rene" can manage visual inputs, additional integration steps are required.
Dataset releases
Not yet included in a published release.
def __init__(
self,
embed_dim,
num_heads,
num_heads_kv=None,
head_dim=None, # If None, use embed_dim // num_heads
qkv_proj_bias=True,
out_proj_bias=True,
softmax_scale=None,
causal=True,
sliding_window_length=None, # If None, infinite context
layer_idx=None,
device=None,
dtype=None,
) -> None:
"""
num_heads_kv: can be used to toggle MQA / GQA. If None, use num_heads.
return_residual: whether to return the input x along with the output. This is for
performance reason: for post-norm architecture, returning the input allows us
to fuse the backward of nn.Linear with the residual connection.
"""
super().__init__()
factory_kwargs = {"device": device, "dtype": dtype}
self.embed_dim = embed_dim
self.layer_idx = layer_idx
self.softmax_scale = softmax_scale
self.causal = causal
assert self.causal, "Rene does not yet support non-causal modeling"
self.num_heads = num_heads
self.num_heads_kv = num_heads_kv if num_heads_kv is not None else num_heads
assert (
self.num_heads % self.num_heads_kv == 0
), "num_heads must be divisible by num_heads_kv"
if head_dim is None:
assert self.embed_dim % num_heads == 0, "embed_dim must be divisible by num_heads"
self.head_dim = head_dim if head_dim is not None else self.embed_dim // num_heads
qkv_dim = self.head_dim * (self.num_heads + 2 * self.num_heads_kv)
out_dim = self.head_dim * self.num_heads
self.sliding_window_length = sliding_window_length
if self.sliding_window_length is None:
self.window_size = (-1, -1)
else:
self.window_size = (self.sliding_window_length - 1, 0) # for flash_attn
self.in_proj = nn.Linear(embed_dim, qkv_dim, bias=qkv_proj_bias, **factory_kwargs)
self.out_proj = nn.Linear(out_dim, embed_dim, bias=out_proj_bias, **factory_kwargs)
def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None):
"""Allocate inference cache for the multi-head self-attention module."""
dtype = self.out_proj.weight.dtype if dtype is None else dtype
device = self.out_proj.weight.device
kv_cache = torch.empty(
batch_size,
max_seqlen,
2,
self.num_heads_kv,
self.head_dim,
dtype=dtype,
device=device,
)
return kv_cache, None
def _pytorch_attn(self, q, kv):
k, v = kv.unbind(dim=-3)
k = torch.repeat_interleave(k, dim=2, repeats=self.num_heads // self.num_heads_kv)
v = torch.repeat_interleave(v, dim=2, repeats=self.num_heads // self.num_heads_kv)
q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2)
L, S = q.size(-2), k.size(-2)
if S > self.sliding_window_length:
attn_mask = (
torch.ones(L, S, dtype=torch.bool)
.tril(diagonal=0)
.triu(-self.window_size[0])
.to(device=q.device)
)
# Since we pass in an attn_mask explicitly, we need to pass is_causal=False to
# `scaled_dot_product_attention` (even though the attn_mask itself is in fact causal).
is_causal_arg = False
else:
# The previous branch would also handle this case correctly, but it is more efficient
# to omit the attn_mask when we don't need it.
attn_mask = None
is_causal_arg = True
return F.scaled_dot_product_attention(
q, k, v, attn_mask=attn_mask, is_causal=is_causal_arg, scale=self.softmax_scale
).transpose(1, 2)
def _update_kv_cache(self, kv, inference_params):
"""kv: (batch_size, seqlen, 2, nheads, head_dim) or (batch_size, 1, 2, nheads, head_dim)."""
assert self.layer_idx is not None, "Generation requires layer_idx in the constructor"
return _update_kv_cache(kv, inference_params, self.layer_idx)
def _update_kvcache_attention(self, q, kv, inference_params):
"""Write kv to inference_params, then compute attention."""
if inference_params.seqlen_offset == 0 or flash_attn_with_kvcache is None:
# TODO: this only uses seqlen_offset and not lengths_per_sample.
kv = self._update_kv_cache(kv, inference_params)
return self._pytorch_attn(q, kv)
else:
batch = q.shape[0]
kv_cache, _ = inference_params.key_value_memory_dict[self.layer_idx]
kv_cache = kv_cache[:batch]
cache_seqlens = (
inference_params.lengths_per_sample[:batch]
if inference_params.lengths_per_sample is not None
else inference_params.seqlen_offset
)
return flash_attn_with_kvcache(
q,
kv_cache[:, :, 0],
kv_cache[:, :, 1],
kv[:, :, 0],
kv[:, :, 1],
cache_seqlens=cache_seqlens,
softmax_scale=self.softmax_scale,
causal=self.causal,
window_size=self.window_size,
)
def forward(self, x, inference_params=None):
"""Forward pass through the multi-head self-attention module."""
if (
inference_params is not None
and self.layer_idx not in inference_params.key_value_memory_dict
):
inference_params.key_value_memory_dict[self.layer_idx] = self.allocate_inference_cache(
x.shape[0], inference_params.max_seqlen, dtype=x.dtype
)
qkv = self.in_proj(x)
q, kv = qkv.split(
[self.num_heads * self.head_dim, self.num_heads_kv * 2 * self.head_dim], dim=-1
)
q = rearrange(q, "... (h d) -> ... h d", d=self.head_dim)
kv = rearrange(kv, "... (two hkv d) -> ... two hkv d", two=2, d=self.head_dim)
if inference_params is None:
context = self._pytorch_attn(q, kv)
else:
context = self._update_kvcache_attention(q, kv, inference_params)
context = rearrange(context, "... h d -> ... (h d)")
out = self.out_proj(context)
return out