Upload 5 files
Browse files- README.md +64 -0
- config.json +16 -0
- model.py +126 -0
- model.safetensors +3 -0
- training_args.json +9 -0
README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- tiny-transformer
|
| 6 |
+
- retrieval
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
# Tiny Transformer for Retrieval
|
| 10 |
+
|
| 11 |
+
## Overview
|
| 12 |
+
|
| 13 |
+
A research-oriented **Tiny Transformer** prototype targeting **Retrieval**. The included **giant** setup documents defaults and file formats without presenting unverified performance numbers.
|
| 14 |
+
|
| 15 |
+
## Repository status
|
| 16 |
+
|
| 17 |
+
- The Python file contains the model and runnable example or training entry point.
|
| 18 |
+
- `config.json` records the generated architecture settings.
|
| 19 |
+
- `training_args.json` records the default experiment recipe.
|
| 20 |
+
- `model.safetensors` is a valid initialization checkpoint for smoke tests; it is **not** presented as a trained benchmark checkpoint.
|
| 21 |
+
- No benchmark score is claimed in this repository.
|
| 22 |
+
|
| 23 |
+
## Architecture
|
| 24 |
+
|
| 25 |
+
| Item | Value |
|
| 26 |
+
|---|---|
|
| 27 |
+
| Architecture | Tiny Transformer |
|
| 28 |
+
| Scale | giant |
|
| 29 |
+
| Attention | sparse |
|
| 30 |
+
| Fusion | tensor fusion |
|
| 31 |
+
| Activation | relu |
|
| 32 |
+
| Normalization | layernorm |
|
| 33 |
+
|
| 34 |
+
## Default experiment recipe
|
| 35 |
+
|
| 36 |
+
The included configuration uses **lamb** with a **exponential** schedule. These are starting values in the script, not evidence of a completed run. For a meaningful evaluation, train all baselines with the same data exposure, tuning budget, and random seeds.
|
| 37 |
+
|
| 38 |
+
## Quick check
|
| 39 |
+
|
| 40 |
+
```bash
|
| 41 |
+
python model.py --help
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
Inspect the script's `__main__` block for its generated smoke-test example. Because this is a custom implementation, generic automatic loading APIs require an explicit adapter before use.
|
| 45 |
+
|
| 46 |
+
## Evaluation guidance
|
| 47 |
+
|
| 48 |
+
A useful first evaluation would use **Flickr30k**, report the task metric across at least three seeds, and include a matched-capacity baseline. Keep training logs and environment versions with any published result.
|
| 49 |
+
|
| 50 |
+
## Limitations
|
| 51 |
+
|
| 52 |
+
The initialization checkpoint has not been trained or audited for robustness, fairness, or domain transfer. The implementation should be treated as an experimental starting point. Results from a future trained checkpoint must be documented separately from the defaults shipped here.
|
| 53 |
+
|
| 54 |
+
## Files
|
| 55 |
+
|
| 56 |
+
- `model.py` — primary artifact
|
| 57 |
+
- `README.md` — this documentation
|
| 58 |
+
- `config.json` — architecture configuration
|
| 59 |
+
- `training_args.json` — default experiment settings
|
| 60 |
+
- `model.safetensors` — initialization checkpoint
|
| 61 |
+
|
| 62 |
+
## License
|
| 63 |
+
|
| 64 |
+
Released under **mit**. Review the source-data terms separately when this repository is used with external datasets.
|
config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"CustomResearchModel"
|
| 4 |
+
],
|
| 5 |
+
"architecture": "tiny_transformer",
|
| 6 |
+
"model_type": "tiny_transformer",
|
| 7 |
+
"hidden_size": 192,
|
| 8 |
+
"num_hidden_layers": 8,
|
| 9 |
+
"num_attention_heads": 8,
|
| 10 |
+
"intermediate_size": 768,
|
| 11 |
+
"hidden_act": "relu",
|
| 12 |
+
"max_position_embeddings": 512,
|
| 13 |
+
"layer_norm_eps": 1e-12,
|
| 14 |
+
"checkpoint_status": "initialization-only",
|
| 15 |
+
"notes": "Untrained checkpoint for smoke tests; no benchmark claim."
|
| 16 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TinyTransformerModel(nn.Module):
|
| 7 |
+
'''
|
| 8 |
+
tiny_transformer model with sparse attention.
|
| 9 |
+
Scale: giant (dim=768, layers=16, heads=12)
|
| 10 |
+
Fusion: tensor_fusion, Task: retrieval
|
| 11 |
+
'''
|
| 12 |
+
|
| 13 |
+
def __init__(self, embed_dim=768, num_layers=16, num_heads=12, num_classes=10):
|
| 14 |
+
super().__init__()
|
| 15 |
+
self.embed_dim = embed_dim
|
| 16 |
+
self.num_layers = num_layers
|
| 17 |
+
self.num_heads = num_heads
|
| 18 |
+
|
| 19 |
+
# image patch embedding
|
| 20 |
+
self.patch_proj = nn.Conv2d(3, embed_dim, kernel_size=16, stride=16)
|
| 21 |
+
self.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim) * 0.02)
|
| 22 |
+
self.pos_embed = nn.Parameter(torch.randn(1, 197, embed_dim) * 0.02)
|
| 23 |
+
self.dropout = nn.Dropout(0.1)
|
| 24 |
+
|
| 25 |
+
# image transformer blocks
|
| 26 |
+
self.image_blocks = nn.ModuleList([
|
| 27 |
+
nn.TransformerEncoderLayer(
|
| 28 |
+
embed_dim, num_heads, embed_dim * 4, 0.1,
|
| 29 |
+
activation='gelu', batch_first=True, norm_first=True
|
| 30 |
+
)
|
| 31 |
+
for _ in range(num_layers)
|
| 32 |
+
])
|
| 33 |
+
self.image_norm = nn.LayerNorm(embed_dim)
|
| 34 |
+
|
| 35 |
+
# text embedding
|
| 36 |
+
self.text_embed = nn.Embedding(30522, embed_dim, padding_idx=0)
|
| 37 |
+
self.text_pos = nn.Parameter(torch.randn(1, 128, embed_dim) * 0.02)
|
| 38 |
+
self.text_blocks = nn.ModuleList([
|
| 39 |
+
nn.TransformerEncoderLayer(
|
| 40 |
+
embed_dim, num_heads, embed_dim * 4, 0.1,
|
| 41 |
+
activation='gelu', batch_first=True, norm_first=True
|
| 42 |
+
)
|
| 43 |
+
for _ in range(num_layers)
|
| 44 |
+
])
|
| 45 |
+
self.text_norm = nn.LayerNorm(embed_dim)
|
| 46 |
+
|
| 47 |
+
# fusion
|
| 48 |
+
self.fusion_blocks = nn.ModuleList([
|
| 49 |
+
nn.TransformerEncoderLayer(
|
| 50 |
+
embed_dim, num_heads, embed_dim * 4, 0.1,
|
| 51 |
+
activation='gelu', batch_first=True, norm_first=True
|
| 52 |
+
)
|
| 53 |
+
for _ in range(2)
|
| 54 |
+
])
|
| 55 |
+
self.fusion_norm = nn.LayerNorm(embed_dim)
|
| 56 |
+
|
| 57 |
+
# task head
|
| 58 |
+
self.classifier = nn.Sequential(
|
| 59 |
+
nn.Linear(embed_dim, embed_dim),
|
| 60 |
+
nn.ReLU(),
|
| 61 |
+
nn.Dropout(0.1),
|
| 62 |
+
nn.Linear(embed_dim, num_classes),
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
self._initialize_weights()
|
| 66 |
+
|
| 67 |
+
def _initialize_weights(self):
|
| 68 |
+
for m in self.modules():
|
| 69 |
+
if isinstance(m, nn.Linear):
|
| 70 |
+
nn.init.kaiming_normal_(m.weight)
|
| 71 |
+
if m.bias is not None:
|
| 72 |
+
nn.init.zeros_(m.bias)
|
| 73 |
+
elif isinstance(m, nn.Embedding):
|
| 74 |
+
nn.init.trunc_normal_(m.weight, std=0.02)
|
| 75 |
+
if m.padding_idx is not None:
|
| 76 |
+
m.weight[m.padding_idx].zero_()
|
| 77 |
+
elif isinstance(m, nn.LayerNorm):
|
| 78 |
+
nn.init.ones_(m.weight)
|
| 79 |
+
nn.init.zeros_(m.bias)
|
| 80 |
+
|
| 81 |
+
def encode_image(self, images):
|
| 82 |
+
x = self.patch_proj(images)
|
| 83 |
+
x = x.flatten(2).transpose(1, 2)
|
| 84 |
+
cls = self.cls_token.expand(x.size(0), -1, -1)
|
| 85 |
+
x = torch.cat([cls, x], dim=1)
|
| 86 |
+
x = x + self.pos_embed
|
| 87 |
+
x = self.dropout(x)
|
| 88 |
+
for block in self.image_blocks:
|
| 89 |
+
x = block(x)
|
| 90 |
+
return self.image_norm(x)
|
| 91 |
+
|
| 92 |
+
def encode_text(self, input_ids, attention_mask=None):
|
| 93 |
+
x = self.text_embed(input_ids)
|
| 94 |
+
x = x + self.text_pos[:, :input_ids.size(1)]
|
| 95 |
+
x = self.dropout(x)
|
| 96 |
+
padding = (attention_mask == 0) if attention_mask is not None else None
|
| 97 |
+
for block in self.text_blocks:
|
| 98 |
+
x = block(x, src_key_padding_mask=padding)
|
| 99 |
+
return self.text_norm(x)
|
| 100 |
+
|
| 101 |
+
def forward(self, images, input_ids, attention_mask=None, labels=None):
|
| 102 |
+
image_features = self.encode_image(images)
|
| 103 |
+
text_features = self.encode_text(input_ids, attention_mask)
|
| 104 |
+
|
| 105 |
+
fused = text_features
|
| 106 |
+
for block in self.fusion_blocks:
|
| 107 |
+
fused = block(fused)
|
| 108 |
+
fused = self.fusion_norm(fused[:, 0])
|
| 109 |
+
logits = self.classifier(fused)
|
| 110 |
+
|
| 111 |
+
loss = None
|
| 112 |
+
if labels is not None:
|
| 113 |
+
loss = nn.functional.cross_entropy(logits, labels)
|
| 114 |
+
|
| 115 |
+
return {'logits': logits, 'loss': loss}
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
if __name__ == '__main__':
|
| 119 |
+
model = TinyTransformerModel()
|
| 120 |
+
total = sum(p.numel() for p in model.parameters())
|
| 121 |
+
print(f'TinyTransformerModel: {total:,} params ({total/1e6:.2f}M)')
|
| 122 |
+
img = torch.randn(2, 3, 224, 224)
|
| 123 |
+
ids = torch.randint(0, 30522, (2, 128))
|
| 124 |
+
mask = torch.ones(2, 128)
|
| 125 |
+
out = model(img, ids, mask, torch.tensor([0, 1]))
|
| 126 |
+
print(f'Output: {out["logits"].shape}, Loss: {out["loss"].item():.4f}')
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0e2b0c4a390fdd9c7046947a2d04638201de8caa2429542dbe4344e9322876cc
|
| 3 |
+
size 99808
|
training_args.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"optimizer": "lamb",
|
| 3 |
+
"scheduler": "exponential",
|
| 4 |
+
"learning_rate": 0.0001,
|
| 5 |
+
"batch_size": 24,
|
| 6 |
+
"epochs": 10,
|
| 7 |
+
"seed": 3407,
|
| 8 |
+
"status": "default recipe; not a completed run"
|
| 9 |
+
}
|