Upload 5 files
Browse files- README.md +64 -0
- config.json +16 -0
- finetune.py +53 -0
- model.safetensors +3 -0
- training_args.json +9 -0
README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: bsd-3-clause
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- blip
|
| 6 |
+
- matching
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
# Blip for Matching
|
| 10 |
+
|
| 11 |
+
## Overview
|
| 12 |
+
|
| 13 |
+
This is an experimental **Blip** codebase for **Matching**. It keeps the **giant** setup intentionally manageable so architecture changes can be inspected before a full training run.
|
| 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 | Blip |
|
| 28 |
+
| Scale | giant |
|
| 29 |
+
| Attention | sparse |
|
| 30 |
+
| Fusion | tucker |
|
| 31 |
+
| Activation | mish |
|
| 32 |
+
| Normalization | groupnorm |
|
| 33 |
+
|
| 34 |
+
## Default experiment recipe
|
| 35 |
+
|
| 36 |
+
The included configuration uses **sgd** with a **cosine** 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 finetune.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 **a paired validation set**, 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 |
+
- `finetune.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 **bsd-3-clause**. 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": "blip",
|
| 6 |
+
"model_type": "blip",
|
| 7 |
+
"hidden_size": 384,
|
| 8 |
+
"num_hidden_layers": 6,
|
| 9 |
+
"num_attention_heads": 4,
|
| 10 |
+
"intermediate_size": 768,
|
| 11 |
+
"hidden_act": "mish",
|
| 12 |
+
"max_position_embeddings": 128,
|
| 13 |
+
"layer_norm_eps": 1e-12,
|
| 14 |
+
"checkpoint_status": "initialization-only",
|
| 15 |
+
"notes": "Untrained checkpoint for smoke tests; no benchmark claim."
|
| 16 |
+
}
|
finetune.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch, torch.nn as nn, math
|
| 2 |
+
|
| 3 |
+
class M(nn.Module):
|
| 4 |
+
def __init__(self, d=768, L=16, H=12, nc=10):
|
| 5 |
+
super().__init__()
|
| 6 |
+
self.pe = nn.Conv2d(3, d, 16, 16)
|
| 7 |
+
self.cls = nn.Parameter(torch.randn(1,1,d)*.02)
|
| 8 |
+
self.pos = nn.Parameter(torch.randn(1,197,d)*.02)
|
| 9 |
+
self.blks = nn.ModuleList([nn.TransformerEncoderLayer(d, H, d*4, .1, activation='gelu', batch_first=True, norm_first=True) for _ in range(L)])
|
| 10 |
+
self.ln = nn.LayerNorm(d)
|
| 11 |
+
self.te = nn.Embedding(30522, d, padding_idx=0)
|
| 12 |
+
self.tpos = nn.Parameter(torch.randn(1,128,d)*.02)
|
| 13 |
+
self.tblks = nn.ModuleList([nn.TransformerEncoderLayer(d, H, d*4, .1, activation='gelu', batch_first=True, norm_first=True) for _ in range(L)])
|
| 14 |
+
self.tln = nn.LayerNorm(d)
|
| 15 |
+
self.fuse = nn.ModuleList([nn.TransformerEncoderLayer(d, H, d*4, .1, activation='gelu', batch_first=True, norm_first=True) for _ in range(2)])
|
| 16 |
+
self.fln = nn.LayerNorm(d)
|
| 17 |
+
self.head = nn.Sequential(nn.Linear(d,d), nn.Mish(), nn.Dropout(.1), nn.Linear(d,nc))
|
| 18 |
+
self._init()
|
| 19 |
+
|
| 20 |
+
def _init(self):
|
| 21 |
+
for m in self.modules():
|
| 22 |
+
if isinstance(m, nn.Linear):
|
| 23 |
+
nn.init.trunc_normal_(m.weight, std=0.02)
|
| 24 |
+
if m.bias is not None: nn.init.zeros_(m.bias)
|
| 25 |
+
|
| 26 |
+
def enc_img(self, x):
|
| 27 |
+
x = self.pe(x).flatten(2).transpose(1,2)
|
| 28 |
+
x = torch.cat([self.cls.expand(x.size(0),-1,-1), x], 1)
|
| 29 |
+
x = x + self.pos
|
| 30 |
+
for b in self.blks: x = b(x)
|
| 31 |
+
return self.ln(x)
|
| 32 |
+
|
| 33 |
+
def enc_txt(self, ids, mask=None):
|
| 34 |
+
x = self.te(ids) + self.tpos[:, :ids.size(1)]
|
| 35 |
+
m = (mask == 0) if mask is not None else None
|
| 36 |
+
for b in self.tblks: x = b(x, src_key_padding_mask=m)
|
| 37 |
+
return self.tln(x)
|
| 38 |
+
|
| 39 |
+
def forward(self, img, ids, mask=None, lbl=None):
|
| 40 |
+
fi = self.enc_img(img)
|
| 41 |
+
ft = self.enc_txt(ids, mask)
|
| 42 |
+
x = ft
|
| 43 |
+
for f in self.fuse: x = f(x)
|
| 44 |
+
x = self.fln(x[:, 0])
|
| 45 |
+
logits = self.head(x)
|
| 46 |
+
loss = nn.functional.cross_entropy(logits, lbl) if lbl is not None else None
|
| 47 |
+
return {'logits': logits, 'loss': loss}
|
| 48 |
+
|
| 49 |
+
if __name__ == '__main__':
|
| 50 |
+
m = M()
|
| 51 |
+
print(f'Params: {sum(p.numel() for p in m.parameters()):,}')
|
| 52 |
+
o = m(torch.randn(2,3,224,224), torch.randint(0,30522,(2,128)), torch.ones(2,128), torch.tensor([0,1]))
|
| 53 |
+
print(o['logits'].shape, o['loss'].item())
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:942106f79313f005044e24a9b3447078e1f0f9857fac7f54fd97107770626036
|
| 3 |
+
size 198880
|
training_args.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"optimizer": "sgd",
|
| 3 |
+
"scheduler": "cosine",
|
| 4 |
+
"learning_rate": 0.0002,
|
| 5 |
+
"batch_size": 24,
|
| 6 |
+
"epochs": 10,
|
| 7 |
+
"seed": 123,
|
| 8 |
+
"status": "default recipe; not a completed run"
|
| 9 |
+
}
|