Instructions to use Angshul/SpliNet with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Angshul/SpliNet with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("fill-mask", model="Angshul/SpliNet", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForMaskedLM model = AutoModelForMaskedLM.from_pretrained("Angshul/SpliNet", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload repaired SpliNet 2B-token pretrained model
Browse files- README.md +50 -0
- checksums.sha256 +13 -0
- config.json +36 -0
- configuration_splinet.py +24 -0
- model.safetensors +3 -0
- modeling_splinet.py +366 -0
- requirements.txt +4 -0
- special_tokens_map.json +9 -0
- spiece.model +3 -0
- spiece.vocab +0 -0
- tokenization_splinet.py +153 -0
- tokenizer_config.json +18 -0
- tokenizer_metadata.json +21 -0
- training_metadata.json +28 -0
README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
pipeline_tag: fill-mask
|
| 5 |
+
library_name: transformers
|
| 6 |
+
datasets:
|
| 7 |
+
- allenai/c4
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# SpliNet
|
| 11 |
+
|
| 12 |
+
**SpliNet: A Zero-Parameter B-Spline Transformer with Linear Complexity**
|
| 13 |
+
|
| 14 |
+
SpliNet replaces learned self-attention token mixing with a fixed order-2 single-sided cardinal B-spline operator.
|
| 15 |
+
|
| 16 |
+
This model was pretrained from scratch on exactly **2,000,000,000 C4 tokens** using the dedicated SpliNet tokenizer.
|
| 17 |
+
|
| 18 |
+
## Architecture
|
| 19 |
+
|
| 20 |
+
- Layers: 12
|
| 21 |
+
- Hidden size: 768
|
| 22 |
+
- Heads: 12
|
| 23 |
+
- FFN width: 3072
|
| 24 |
+
- Sequence length: 512
|
| 25 |
+
- Vocabulary: 32000
|
| 26 |
+
- Spline order: 2
|
| 27 |
+
- Spline radius: 16
|
| 28 |
+
- Trainable mixer parameters: 0
|
| 29 |
+
- Total parameters: 82,894,592
|
| 30 |
+
- Trainable parameters: 82,894,592
|
| 31 |
+
|
| 32 |
+
## Pretraining
|
| 33 |
+
|
| 34 |
+
- Training tokens: 2,000,000,000
|
| 35 |
+
- Validation tokens: 5,120,000
|
| 36 |
+
- Objective: masked language modeling
|
| 37 |
+
- Masked positions: 77/512
|
| 38 |
+
- Optimizer: AdamW
|
| 39 |
+
- Precision: BF16
|
| 40 |
+
- Hardware: NVIDIA A100-SXM4-80GB
|
| 41 |
+
|
| 42 |
+
## Final validation
|
| 43 |
+
|
| 44 |
+
- MLM loss: 4.614502
|
| 45 |
+
- MLM perplexity: 100.937575
|
| 46 |
+
|
| 47 |
+
Load with `trust_remote_code=True`.
|
| 48 |
+
|
| 49 |
+
OpenReview: https://openreview.net/forum?id=nWHnuiEF3C
|
| 50 |
+
GitHub: https://github.com/AngshulMajumdar/SpliNet
|
checksums.sha256
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
2d50d51e32c691a1034ed87770298f054aaf7fcf6e3046274a3b528f3d0135d0 README.md
|
| 2 |
+
e22e250a1383a774e2842c1f9183895ea7678b6974114489aec8752b06f4dcb8 config.json
|
| 3 |
+
91c0167660e96c39d55a31053c58f87abbcbe58cf22525dfb9b90ce6d28bdd3e configuration_splinet.py
|
| 4 |
+
a891f4a978c2551fc8851346601fa3d560701ba69471272c4c5a39f4d70fbdd6 model.safetensors
|
| 5 |
+
68b78991d599f1c6c3b52d3882bb07e65c81243500064b9317e0f289f57576ba modeling_splinet.py
|
| 6 |
+
321813b734e7c50df348e53600d5b90ba9d204eba535545279babac85babe2ab requirements.txt
|
| 7 |
+
ace55eb0f41e170e8f3c891dd4ab707a2d871299ab7e4f4116c6a9d3128f31cf special_tokens_map.json
|
| 8 |
+
119ec6b2af9cbbc56f297bd606b69f79f6e3130a34ee56122ee813a58d15bb9d spiece.model
|
| 9 |
+
ec799958399f5fcd35f12ac451e6b7517e2eda01f50a7b9d2089c1425e9a0d05 spiece.vocab
|
| 10 |
+
9e0b42c275c87f712d095f002bbfa5a435ed89250785ef9b4d4ee9a7ab18a53e tokenization_splinet.py
|
| 11 |
+
8171bc64afff9fe2f89ac1e011d0b829d4b5413b398ef7b8b9f408ad5cd30325 tokenizer_config.json
|
| 12 |
+
d80f0c668bf183e6f327c167bb2723f0e565c946c05d6b5587afec7ca10575fb tokenizer_metadata.json
|
| 13 |
+
8f64420de4f4a6cd9ec7c8920f8be32463c8ba32d85dc9ab86865c3aba01e804 training_metadata.json
|
config.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"SpliNetForMaskedLM"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_splinet.SpliNetConfig",
|
| 7 |
+
"AutoModel": "modeling_splinet.SpliNetModel",
|
| 8 |
+
"AutoModelForMaskedLM": "modeling_splinet.SpliNetForMaskedLM"
|
| 9 |
+
},
|
| 10 |
+
"bos_token_id": 1,
|
| 11 |
+
"dtype": "float32",
|
| 12 |
+
"eos_token_id": 2,
|
| 13 |
+
"hidden_act": "gelu_new",
|
| 14 |
+
"hidden_dropout_prob": 0.1,
|
| 15 |
+
"hidden_size": 768,
|
| 16 |
+
"initializer_range": 0.02,
|
| 17 |
+
"intermediate_size": 3072,
|
| 18 |
+
"layer_norm_eps": 1e-12,
|
| 19 |
+
"max_position_embeddings": 512,
|
| 20 |
+
"model_type": "splinet",
|
| 21 |
+
"num_hidden_layers": 12,
|
| 22 |
+
"pad_token_id": 3,
|
| 23 |
+
"splinet_num_heads": 12,
|
| 24 |
+
"splinet_order": 2,
|
| 25 |
+
"splinet_radius": 16,
|
| 26 |
+
"splinet_sidedness": "single",
|
| 27 |
+
"tie_word_embeddings": true,
|
| 28 |
+
"tokenizer_class": "SpliNetTokenizer",
|
| 29 |
+
"tokenizer_sha256": "119ec6b2af9cbbc56f297bd606b69f79f6e3130a34ee56122ee813a58d15bb9d",
|
| 30 |
+
"tpu_short_seq_length": 512,
|
| 31 |
+
"training_tokens": 2000000000,
|
| 32 |
+
"transformers_version": "5.16.1",
|
| 33 |
+
"type_vocab_size": 4,
|
| 34 |
+
"use_tpu_fourier_optimizations": false,
|
| 35 |
+
"vocab_size": 32000
|
| 36 |
+
}
|
configuration_splinet.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import FNetConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class SpliNetConfig(FNetConfig):
|
| 5 |
+
model_type = "splinet"
|
| 6 |
+
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
splinet_num_heads=12,
|
| 10 |
+
splinet_radius=16,
|
| 11 |
+
**kwargs,
|
| 12 |
+
):
|
| 13 |
+
super().__init__(**kwargs)
|
| 14 |
+
|
| 15 |
+
self.splinet_num_heads = int(
|
| 16 |
+
splinet_num_heads
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
self.splinet_radius = int(
|
| 20 |
+
splinet_radius
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
self.splinet_order = 2
|
| 24 |
+
self.splinet_sidedness = "single"
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a891f4a978c2551fc8851346601fa3d560701ba69471272c4c5a39f4d70fbdd6
|
| 3 |
+
size 331590896
|
modeling_splinet.py
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.nn.functional as F
|
| 6 |
+
|
| 7 |
+
from transformers.modeling_outputs import (
|
| 8 |
+
BaseModelOutput,
|
| 9 |
+
MaskedLMOutput,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
from transformers.models.fnet.modeling_fnet import (
|
| 13 |
+
FNetEmbeddings,
|
| 14 |
+
FNetIntermediate,
|
| 15 |
+
FNetOnlyMLMHead,
|
| 16 |
+
FNetOutput,
|
| 17 |
+
FNetPreTrainedModel,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
from .configuration_splinet import SpliNetConfig
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class SplineMixer(nn.Module):
|
| 24 |
+
def __init__(self, config):
|
| 25 |
+
super().__init__()
|
| 26 |
+
|
| 27 |
+
self.radius = int(
|
| 28 |
+
config.splinet_radius
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
z = -3.0 + 2.0 * math.sqrt(2.0)
|
| 32 |
+
|
| 33 |
+
# Fixed analytic spline coefficients.
|
| 34 |
+
#
|
| 35 |
+
# IMPORTANT:
|
| 36 |
+
# Do NOT register these as a non-persistent tensor buffer.
|
| 37 |
+
# Hugging Face low-memory/meta-device loading can materialize
|
| 38 |
+
# such a buffer without its analytically initialized values.
|
| 39 |
+
#
|
| 40 |
+
# Store the 33 coefficients as ordinary Python floats instead.
|
| 41 |
+
# They are recreated on the actual input device and dtype in
|
| 42 |
+
# forward(). They are not learned model state.
|
| 43 |
+
self.kernel_values = tuple(
|
| 44 |
+
float(
|
| 45 |
+
math.sqrt(2.0)
|
| 46 |
+
* (z ** abs(k))
|
| 47 |
+
)
|
| 48 |
+
for k in range(
|
| 49 |
+
-self.radius,
|
| 50 |
+
self.radius + 1,
|
| 51 |
+
)
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def forward(self, x):
|
| 55 |
+
if x.shape[1] != 512:
|
| 56 |
+
raise ValueError(
|
| 57 |
+
"This released SpliNet checkpoint was pretrained "
|
| 58 |
+
"and validated on fixed 512-token blocks. "
|
| 59 |
+
f"Received sequence length {x.shape[1]}. "
|
| 60 |
+
"Tokenize/pack the input to exactly 512 tokens."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
d = x.shape[-1]
|
| 64 |
+
|
| 65 |
+
y = (
|
| 66 |
+
x.transpose(1, 2)
|
| 67 |
+
.contiguous()
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
y = F.pad(
|
| 71 |
+
y,
|
| 72 |
+
(
|
| 73 |
+
self.radius,
|
| 74 |
+
self.radius,
|
| 75 |
+
),
|
| 76 |
+
mode="reflect",
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
kernel = (
|
| 80 |
+
torch.tensor(
|
| 81 |
+
self.kernel_values,
|
| 82 |
+
device=y.device,
|
| 83 |
+
dtype=y.dtype,
|
| 84 |
+
)
|
| 85 |
+
.view(1, 1, -1)
|
| 86 |
+
.expand(d, 1, -1)
|
| 87 |
+
.contiguous()
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
y = F.conv1d(
|
| 91 |
+
y,
|
| 92 |
+
kernel,
|
| 93 |
+
groups=d,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return (
|
| 97 |
+
y.transpose(1, 2)
|
| 98 |
+
.contiguous()
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class SpliNetMixingBlock(nn.Module):
|
| 103 |
+
def __init__(self, config):
|
| 104 |
+
super().__init__()
|
| 105 |
+
|
| 106 |
+
self.mixer = SplineMixer(
|
| 107 |
+
config
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
self.LayerNorm = nn.LayerNorm(
|
| 111 |
+
config.hidden_size,
|
| 112 |
+
eps=config.layer_norm_eps,
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
def forward(self, x):
|
| 116 |
+
return self.LayerNorm(
|
| 117 |
+
x + self.mixer(x)
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
class SpliNetLayer(nn.Module):
|
| 122 |
+
def __init__(self, config):
|
| 123 |
+
super().__init__()
|
| 124 |
+
|
| 125 |
+
self.mixing = (
|
| 126 |
+
SpliNetMixingBlock(
|
| 127 |
+
config
|
| 128 |
+
)
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
self.intermediate = (
|
| 132 |
+
FNetIntermediate(
|
| 133 |
+
config
|
| 134 |
+
)
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
self.output = (
|
| 138 |
+
FNetOutput(
|
| 139 |
+
config
|
| 140 |
+
)
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
def forward(self, x):
|
| 144 |
+
x = self.mixing(x)
|
| 145 |
+
|
| 146 |
+
return self.output(
|
| 147 |
+
self.intermediate(x),
|
| 148 |
+
x,
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
class SpliNetEncoder(nn.Module):
|
| 153 |
+
def __init__(self, config):
|
| 154 |
+
super().__init__()
|
| 155 |
+
|
| 156 |
+
self.layer = nn.ModuleList(
|
| 157 |
+
[
|
| 158 |
+
SpliNetLayer(config)
|
| 159 |
+
for _ in range(
|
| 160 |
+
config.num_hidden_layers
|
| 161 |
+
)
|
| 162 |
+
]
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
def forward(
|
| 166 |
+
self,
|
| 167 |
+
x,
|
| 168 |
+
output_hidden_states=False,
|
| 169 |
+
):
|
| 170 |
+
hidden_states = (
|
| 171 |
+
()
|
| 172 |
+
if output_hidden_states
|
| 173 |
+
else None
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
for layer in self.layer:
|
| 177 |
+
if output_hidden_states:
|
| 178 |
+
hidden_states += (x,)
|
| 179 |
+
|
| 180 |
+
x = layer(x)
|
| 181 |
+
|
| 182 |
+
if output_hidden_states:
|
| 183 |
+
hidden_states += (x,)
|
| 184 |
+
|
| 185 |
+
return BaseModelOutput(
|
| 186 |
+
last_hidden_state=x,
|
| 187 |
+
hidden_states=hidden_states,
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class SpliNetModel(FNetPreTrainedModel):
|
| 192 |
+
config_class = SpliNetConfig
|
| 193 |
+
base_model_prefix = "splinet"
|
| 194 |
+
|
| 195 |
+
def __init__(self, config):
|
| 196 |
+
super().__init__(config)
|
| 197 |
+
|
| 198 |
+
self.embeddings = (
|
| 199 |
+
FNetEmbeddings(config)
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
self.encoder = (
|
| 203 |
+
SpliNetEncoder(config)
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
self.post_init()
|
| 207 |
+
|
| 208 |
+
def get_input_embeddings(self):
|
| 209 |
+
return (
|
| 210 |
+
self.embeddings
|
| 211 |
+
.word_embeddings
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
def set_input_embeddings(
|
| 215 |
+
self,
|
| 216 |
+
value,
|
| 217 |
+
):
|
| 218 |
+
self.embeddings.word_embeddings = value
|
| 219 |
+
|
| 220 |
+
def forward(
|
| 221 |
+
self,
|
| 222 |
+
input_ids=None,
|
| 223 |
+
token_type_ids=None,
|
| 224 |
+
position_ids=None,
|
| 225 |
+
inputs_embeds=None,
|
| 226 |
+
output_hidden_states=False,
|
| 227 |
+
**kwargs,
|
| 228 |
+
):
|
| 229 |
+
if input_ids is not None:
|
| 230 |
+
shape = input_ids.shape
|
| 231 |
+
device = input_ids.device
|
| 232 |
+
|
| 233 |
+
elif inputs_embeds is not None:
|
| 234 |
+
shape = inputs_embeds.shape[:-1]
|
| 235 |
+
device = inputs_embeds.device
|
| 236 |
+
|
| 237 |
+
else:
|
| 238 |
+
raise ValueError(
|
| 239 |
+
"input_ids or inputs_embeds required."
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
if shape[1] != 512:
|
| 243 |
+
raise ValueError(
|
| 244 |
+
"This SpliNet checkpoint requires exactly "
|
| 245 |
+
f"512 tokens; received {shape[1]}."
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
if token_type_ids is None:
|
| 249 |
+
token_type_ids = torch.zeros(
|
| 250 |
+
shape,
|
| 251 |
+
dtype=torch.long,
|
| 252 |
+
device=device,
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
x = self.embeddings(
|
| 256 |
+
input_ids=input_ids,
|
| 257 |
+
token_type_ids=token_type_ids,
|
| 258 |
+
position_ids=position_ids,
|
| 259 |
+
inputs_embeds=inputs_embeds,
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
return self.encoder(
|
| 263 |
+
x,
|
| 264 |
+
output_hidden_states=output_hidden_states,
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
class SpliNetForMaskedLM(FNetPreTrainedModel):
|
| 269 |
+
config_class = SpliNetConfig
|
| 270 |
+
base_model_prefix = "splinet"
|
| 271 |
+
|
| 272 |
+
_tied_weights_keys = {
|
| 273 |
+
"cls.predictions.decoder.bias":
|
| 274 |
+
"cls.predictions.bias",
|
| 275 |
+
|
| 276 |
+
"cls.predictions.decoder.weight":
|
| 277 |
+
"splinet.embeddings.word_embeddings.weight",
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
def __init__(self, config):
|
| 281 |
+
super().__init__(config)
|
| 282 |
+
|
| 283 |
+
self.splinet = (
|
| 284 |
+
SpliNetModel(config)
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
self.cls = (
|
| 288 |
+
FNetOnlyMLMHead(config)
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
self.post_init()
|
| 292 |
+
|
| 293 |
+
if config.tie_word_embeddings:
|
| 294 |
+
(
|
| 295 |
+
self.cls
|
| 296 |
+
.predictions
|
| 297 |
+
.decoder.weight
|
| 298 |
+
) = (
|
| 299 |
+
self.splinet
|
| 300 |
+
.embeddings
|
| 301 |
+
.word_embeddings
|
| 302 |
+
.weight
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
def get_input_embeddings(self):
|
| 306 |
+
return (
|
| 307 |
+
self.splinet
|
| 308 |
+
.embeddings
|
| 309 |
+
.word_embeddings
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
def set_input_embeddings(
|
| 313 |
+
self,
|
| 314 |
+
value,
|
| 315 |
+
):
|
| 316 |
+
(
|
| 317 |
+
self.splinet
|
| 318 |
+
.embeddings
|
| 319 |
+
.word_embeddings
|
| 320 |
+
) = value
|
| 321 |
+
|
| 322 |
+
def get_output_embeddings(self):
|
| 323 |
+
return (
|
| 324 |
+
self.cls
|
| 325 |
+
.predictions
|
| 326 |
+
.decoder
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
def set_output_embeddings(
|
| 330 |
+
self,
|
| 331 |
+
value,
|
| 332 |
+
):
|
| 333 |
+
self.cls.predictions.decoder = value
|
| 334 |
+
|
| 335 |
+
def forward(
|
| 336 |
+
self,
|
| 337 |
+
input_ids=None,
|
| 338 |
+
labels=None,
|
| 339 |
+
**kwargs,
|
| 340 |
+
):
|
| 341 |
+
out = self.splinet(
|
| 342 |
+
input_ids=input_ids,
|
| 343 |
+
**kwargs,
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
logits = self.cls(
|
| 347 |
+
out.last_hidden_state
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
loss = None
|
| 351 |
+
|
| 352 |
+
if labels is not None:
|
| 353 |
+
loss = F.cross_entropy(
|
| 354 |
+
logits.reshape(
|
| 355 |
+
-1,
|
| 356 |
+
self.config.vocab_size,
|
| 357 |
+
),
|
| 358 |
+
labels.reshape(-1),
|
| 359 |
+
ignore_index=-100,
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
return MaskedLMOutput(
|
| 363 |
+
loss=loss,
|
| 364 |
+
logits=logits,
|
| 365 |
+
hidden_states=out.hidden_states,
|
| 366 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers>=4.45.0
|
| 3 |
+
sentencepiece>=0.2.0
|
| 4 |
+
safetensors>=0.4.5
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"unk_token": "<unk>",
|
| 3 |
+
"bos_token": "<s>",
|
| 4 |
+
"eos_token": "</s>",
|
| 5 |
+
"pad_token": "<pad>",
|
| 6 |
+
"cls_token": "<cls>",
|
| 7 |
+
"sep_token": "<sep>",
|
| 8 |
+
"mask_token": "<mask>"
|
| 9 |
+
}
|
spiece.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:119ec6b2af9cbbc56f297bd606b69f79f6e3130a34ee56122ee813a58d15bb9d
|
| 3 |
+
size 806898
|
spiece.vocab
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenization_splinet.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import unicodedata
|
| 4 |
+
import sentencepiece as spm
|
| 5 |
+
|
| 6 |
+
from transformers import PreTrainedTokenizer
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class SpliNetTokenizer(PreTrainedTokenizer):
|
| 10 |
+
|
| 11 |
+
vocab_files_names = {
|
| 12 |
+
"vocab_file": "spiece.model"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
model_input_names = [
|
| 16 |
+
"input_ids",
|
| 17 |
+
"token_type_ids",
|
| 18 |
+
"attention_mask",
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
def __init__(
|
| 22 |
+
self,
|
| 23 |
+
vocab_file,
|
| 24 |
+
do_lower_case=True,
|
| 25 |
+
**kwargs,
|
| 26 |
+
):
|
| 27 |
+
self.vocab_file = vocab_file
|
| 28 |
+
self.do_lower_case = bool(do_lower_case)
|
| 29 |
+
|
| 30 |
+
self.sp_model = spm.SentencePieceProcessor(
|
| 31 |
+
model_file=vocab_file
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# tokenizer_config.json may already provide these.
|
| 35 |
+
# setdefault prevents passing any keyword twice.
|
| 36 |
+
kwargs.setdefault("unk_token", "<unk>")
|
| 37 |
+
kwargs.setdefault("bos_token", "<s>")
|
| 38 |
+
kwargs.setdefault("eos_token", "</s>")
|
| 39 |
+
kwargs.setdefault("pad_token", "<pad>")
|
| 40 |
+
kwargs.setdefault("cls_token", "<cls>")
|
| 41 |
+
kwargs.setdefault("sep_token", "<sep>")
|
| 42 |
+
kwargs.setdefault("mask_token", "<mask>")
|
| 43 |
+
|
| 44 |
+
super().__init__(**kwargs)
|
| 45 |
+
|
| 46 |
+
@property
|
| 47 |
+
def vocab_size(self):
|
| 48 |
+
return int(
|
| 49 |
+
self.sp_model.get_piece_size()
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
def get_vocab(self):
|
| 53 |
+
return {
|
| 54 |
+
self.sp_model.id_to_piece(i): i
|
| 55 |
+
for i in range(self.vocab_size)
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
def _normalize(self, text):
|
| 59 |
+
text = text or ""
|
| 60 |
+
|
| 61 |
+
if self.do_lower_case:
|
| 62 |
+
text = unicodedata.normalize(
|
| 63 |
+
"NFKC",
|
| 64 |
+
text,
|
| 65 |
+
).lower()
|
| 66 |
+
|
| 67 |
+
return " ".join(text.split())
|
| 68 |
+
|
| 69 |
+
def _tokenize(self, text):
|
| 70 |
+
return self.sp_model.encode(
|
| 71 |
+
self._normalize(text),
|
| 72 |
+
out_type=str,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def _convert_token_to_id(self, token):
|
| 76 |
+
return int(
|
| 77 |
+
self.sp_model.piece_to_id(token)
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
def _convert_id_to_token(self, index):
|
| 81 |
+
return self.sp_model.id_to_piece(
|
| 82 |
+
int(index)
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
def convert_tokens_to_string(self, tokens):
|
| 86 |
+
return self.sp_model.decode(tokens)
|
| 87 |
+
|
| 88 |
+
def build_inputs_with_special_tokens(
|
| 89 |
+
self,
|
| 90 |
+
token_ids_0,
|
| 91 |
+
token_ids_1=None,
|
| 92 |
+
):
|
| 93 |
+
if token_ids_1 is None:
|
| 94 |
+
return (
|
| 95 |
+
[self.cls_token_id]
|
| 96 |
+
+ list(token_ids_0)
|
| 97 |
+
+ [self.sep_token_id]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
return (
|
| 101 |
+
[self.cls_token_id]
|
| 102 |
+
+ list(token_ids_0)
|
| 103 |
+
+ [self.sep_token_id]
|
| 104 |
+
+ list(token_ids_1)
|
| 105 |
+
+ [self.sep_token_id]
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
def create_token_type_ids_from_sequences(
|
| 109 |
+
self,
|
| 110 |
+
token_ids_0,
|
| 111 |
+
token_ids_1=None,
|
| 112 |
+
):
|
| 113 |
+
if token_ids_1 is None:
|
| 114 |
+
return [0] * (
|
| 115 |
+
len(token_ids_0) + 2
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
return (
|
| 119 |
+
[0] * (len(token_ids_0) + 2)
|
| 120 |
+
+ [1] * (len(token_ids_1) + 1)
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
def save_vocabulary(
|
| 124 |
+
self,
|
| 125 |
+
save_directory,
|
| 126 |
+
filename_prefix=None,
|
| 127 |
+
):
|
| 128 |
+
os.makedirs(
|
| 129 |
+
save_directory,
|
| 130 |
+
exist_ok=True,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
prefix = (
|
| 134 |
+
filename_prefix + "-"
|
| 135 |
+
if filename_prefix
|
| 136 |
+
else ""
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
destination = os.path.join(
|
| 140 |
+
save_directory,
|
| 141 |
+
prefix + "spiece.model",
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if (
|
| 145 |
+
os.path.abspath(self.vocab_file)
|
| 146 |
+
!= os.path.abspath(destination)
|
| 147 |
+
):
|
| 148 |
+
shutil.copy2(
|
| 149 |
+
self.vocab_file,
|
| 150 |
+
destination,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
return (destination,)
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"tokenizer_class": "SpliNetTokenizer",
|
| 3 |
+
"auto_map": {
|
| 4 |
+
"AutoTokenizer": [
|
| 5 |
+
"tokenization_splinet.SpliNetTokenizer",
|
| 6 |
+
null
|
| 7 |
+
]
|
| 8 |
+
},
|
| 9 |
+
"model_max_length": 512,
|
| 10 |
+
"do_lower_case": true,
|
| 11 |
+
"unk_token": "<unk>",
|
| 12 |
+
"bos_token": "<s>",
|
| 13 |
+
"eos_token": "</s>",
|
| 14 |
+
"pad_token": "<pad>",
|
| 15 |
+
"cls_token": "<cls>",
|
| 16 |
+
"sep_token": "<sep>",
|
| 17 |
+
"mask_token": "<mask>"
|
| 18 |
+
}
|
tokenizer_metadata.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"source": "allenai/c4",
|
| 3 |
+
"config": "en",
|
| 4 |
+
"split": "train",
|
| 5 |
+
"model_type": "unigram",
|
| 6 |
+
"vocab_size": 32000,
|
| 7 |
+
"lowercase": true,
|
| 8 |
+
"normalization": "NFKC + SentencePiece nmt_nfkc",
|
| 9 |
+
"training_text_bytes": 536874807,
|
| 10 |
+
"training_documents": 247312,
|
| 11 |
+
"special_token_ids": {
|
| 12 |
+
"<unk>": 0,
|
| 13 |
+
"<s>": 1,
|
| 14 |
+
"</s>": 2,
|
| 15 |
+
"<pad>": 3,
|
| 16 |
+
"<cls>": 4,
|
| 17 |
+
"<sep>": 5,
|
| 18 |
+
"<mask>": 6
|
| 19 |
+
},
|
| 20 |
+
"sha256_spiece_model": "119ec6b2af9cbbc56f297bd606b69f79f6e3130a34ee56122ee813a58d15bb9d"
|
| 21 |
+
}
|
training_metadata.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model": "SpliNet",
|
| 3 |
+
"dataset": "allenai/c4 en",
|
| 4 |
+
"training_tokens": 2000000000,
|
| 5 |
+
"validation_tokens": 5120000,
|
| 6 |
+
"tokenizer_sha256": "119ec6b2af9cbbc56f297bd606b69f79f6e3130a34ee56122ee813a58d15bb9d",
|
| 7 |
+
"vocab_size": 32000,
|
| 8 |
+
"sequence_length": 512,
|
| 9 |
+
"layers": 12,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"heads": 12,
|
| 12 |
+
"ffn_size": 3072,
|
| 13 |
+
"spline_order": 2,
|
| 14 |
+
"spline_radius": 16,
|
| 15 |
+
"spline_sidedness": "single",
|
| 16 |
+
"physical_batch": 160,
|
| 17 |
+
"effective_batch": 1024,
|
| 18 |
+
"optimizer": "AdamW",
|
| 19 |
+
"base_learning_rate": 0.0001,
|
| 20 |
+
"weight_decay": 0.01,
|
| 21 |
+
"precision": "bfloat16",
|
| 22 |
+
"gpu": "NVIDIA A100-SXM4-80GB",
|
| 23 |
+
"final_validation_loss": 4.6145022583007815,
|
| 24 |
+
"final_validation_perplexity": 100.93757518673716,
|
| 25 |
+
"best_validation_loss": 4.6145022583007815,
|
| 26 |
+
"openreview": "https://openreview.net/forum?id=nWHnuiEF3C",
|
| 27 |
+
"github": "https://github.com/AngshulMajumdar/SpliNet"
|
| 28 |
+
}
|