voxyne 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- voxyne/__init__.py +65 -0
- voxyne/config.py +34 -0
- voxyne/encoder.py +68 -0
- voxyne/generate.py +95 -0
- voxyne/markers.py +55 -0
- voxyne/model.py +144 -0
- voxyne-0.1.0.dist-info/METADATA +109 -0
- voxyne-0.1.0.dist-info/RECORD +10 -0
- voxyne-0.1.0.dist-info/WHEEL +4 -0
- voxyne-0.1.0.dist-info/licenses/LICENSE +201 -0
voxyne/__init__.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Voxyne: a tiny byte-level conversational language model with the RahuKetu
|
|
2
|
+
sigma_K control channel. Loads on CPU, runs offline."""
|
|
3
|
+
|
|
4
|
+
from .config import VoxyneConfig, smoke_config
|
|
5
|
+
from .encoder import RahuKetuEncoder
|
|
6
|
+
from .generate import build_prompt, generate, generate_cached
|
|
7
|
+
from .model import VoxyneLM
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def build(config: VoxyneConfig):
|
|
13
|
+
"""Construct (model, encoder) with matching aux_dim from a VoxyneConfig."""
|
|
14
|
+
encoder = RahuKetuEncoder(
|
|
15
|
+
alphabet_size=config.alphabet_size,
|
|
16
|
+
polar_depth=config.polar_depth,
|
|
17
|
+
shred_width=config.shred_width,
|
|
18
|
+
use_sigma_k=config.use_sigma_k,
|
|
19
|
+
use_sigma_r=config.use_sigma_r,
|
|
20
|
+
use_features=config.use_features,
|
|
21
|
+
)
|
|
22
|
+
model = VoxyneLM(
|
|
23
|
+
alphabet_size=config.alphabet_size,
|
|
24
|
+
d_model=config.d_model,
|
|
25
|
+
n_heads=config.n_heads,
|
|
26
|
+
n_layers=config.n_layers,
|
|
27
|
+
max_len=config.model_max_len,
|
|
28
|
+
aux_dim=encoder.aux_dim,
|
|
29
|
+
dropout=config.dropout,
|
|
30
|
+
use_aux=config.use_aux,
|
|
31
|
+
byte_embed_bottleneck=config.byte_embed_bottleneck,
|
|
32
|
+
)
|
|
33
|
+
return model, encoder
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def load_weights(model, path, *, map_location="cpu", strict=False):
|
|
37
|
+
"""Load released VoxyneLM weights into `model` from a local checkpoint.
|
|
38
|
+
|
|
39
|
+
Download the weights from the Hugging Face model repo first (see the README /
|
|
40
|
+
model card). Accepts a raw state_dict or a {"model": state_dict, ...}
|
|
41
|
+
checkpoint. strict=False tolerates auxiliary-head differences across releases.
|
|
42
|
+
"""
|
|
43
|
+
import torch
|
|
44
|
+
|
|
45
|
+
state = torch.load(path, map_location=map_location)
|
|
46
|
+
if isinstance(state, dict):
|
|
47
|
+
for key in ("model_state", "model", "state_dict"):
|
|
48
|
+
if key in state:
|
|
49
|
+
state = state[key]
|
|
50
|
+
break
|
|
51
|
+
model.load_state_dict(state, strict=strict)
|
|
52
|
+
return model
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
__all__ = [
|
|
56
|
+
"VoxyneConfig",
|
|
57
|
+
"smoke_config",
|
|
58
|
+
"RahuKetuEncoder",
|
|
59
|
+
"VoxyneLM",
|
|
60
|
+
"build",
|
|
61
|
+
"load_weights",
|
|
62
|
+
"generate",
|
|
63
|
+
"generate_cached",
|
|
64
|
+
"build_prompt",
|
|
65
|
+
]
|
voxyne/config.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""VoxyneLM configuration for architecture and encoder settings."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class VoxyneConfig:
|
|
10
|
+
# architecture (full base model ~128.8M params)
|
|
11
|
+
alphabet_size: int = 256
|
|
12
|
+
d_model: int = 768
|
|
13
|
+
n_heads: int = 12
|
|
14
|
+
n_layers: int = 18
|
|
15
|
+
model_max_len: int = 1024
|
|
16
|
+
dropout: float = 0.1
|
|
17
|
+
|
|
18
|
+
# aux / RahuKetu channels. The released path is sigma_K-only.
|
|
19
|
+
use_aux: bool = True
|
|
20
|
+
use_sigma_k: bool = True
|
|
21
|
+
use_sigma_r: bool = False
|
|
22
|
+
use_features: bool = False # True needs a rahuketu build with a features module
|
|
23
|
+
polar_depth: int = 8 # only used when use_features=True
|
|
24
|
+
shred_width: int = 8
|
|
25
|
+
byte_embed_bottleneck: int = 0
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def inference_max_len(self) -> int:
|
|
29
|
+
return self.model_max_len
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def smoke_config() -> "VoxyneConfig":
|
|
33
|
+
"""Tiny CPU-friendly config for smoke tests."""
|
|
34
|
+
return VoxyneConfig(d_model=128, n_heads=4, n_layers=2, model_max_len=64, dropout=0.0)
|
voxyne/encoder.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""RahuKetuEncoder for byte digits and auxiliary channels.
|
|
2
|
+
|
|
3
|
+
The aux tensor carries sigma_K, the role and direction channel. It provides
|
|
4
|
+
information that the bytes themselves do not contain, such as role when markers
|
|
5
|
+
are stripped. This is the released path when use_features=False. Optional
|
|
6
|
+
recursive-polar byte features require a rahuketu build with a `features` module
|
|
7
|
+
and are reserved for ablations.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import torch
|
|
13
|
+
from torch import nn
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class RahuKetuEncoder(nn.Module):
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
alphabet_size: int = 256,
|
|
20
|
+
polar_depth: int = 8,
|
|
21
|
+
shred_width: int = 8,
|
|
22
|
+
use_sigma_k: bool = True,
|
|
23
|
+
use_sigma_r: bool = False,
|
|
24
|
+
use_features: bool = False,
|
|
25
|
+
):
|
|
26
|
+
super().__init__()
|
|
27
|
+
self.alphabet_size = alphabet_size
|
|
28
|
+
self.use_sigma_k = use_sigma_k
|
|
29
|
+
self.use_sigma_r = use_sigma_r
|
|
30
|
+
self.use_features = use_features
|
|
31
|
+
|
|
32
|
+
if use_features:
|
|
33
|
+
import numpy as np
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
from rahuketu import features
|
|
37
|
+
except ImportError as e:
|
|
38
|
+
raise ImportError(
|
|
39
|
+
"use_features=True needs a rahuketu build that provides recursive-polar "
|
|
40
|
+
"byte features through rahuketu.features. This is not available in v0.1"
|
|
41
|
+
) from e
|
|
42
|
+
table = features.all_byte_feature_table(polar_depth, shred_width)
|
|
43
|
+
self.feature_dim = int(table.shape[1])
|
|
44
|
+
self.register_buffer(
|
|
45
|
+
"feature_table", torch.from_numpy(table.astype(np.float32)), persistent=False
|
|
46
|
+
)
|
|
47
|
+
else:
|
|
48
|
+
self.feature_dim = 0
|
|
49
|
+
|
|
50
|
+
self.aux_dim = self.feature_dim + int(use_sigma_r) + int(use_sigma_k)
|
|
51
|
+
if self.aux_dim == 0:
|
|
52
|
+
raise ValueError("aux has no channels: enable sigma_k, sigma_r, or features")
|
|
53
|
+
|
|
54
|
+
def forward(self, digits, sigma_K=None, sigma_R=None):
|
|
55
|
+
if int(digits.min()) < 1 or int(digits.max()) > self.alphabet_size:
|
|
56
|
+
raise ValueError("digits must be in [1, alphabet_size]")
|
|
57
|
+
parts = []
|
|
58
|
+
if self.use_features:
|
|
59
|
+
parts.append(self.feature_table[digits - 1])
|
|
60
|
+
if self.use_sigma_r:
|
|
61
|
+
if sigma_R is None:
|
|
62
|
+
sigma_R = torch.ones_like(digits, dtype=torch.float32)
|
|
63
|
+
parts.append(sigma_R.unsqueeze(-1).float())
|
|
64
|
+
if self.use_sigma_k:
|
|
65
|
+
if sigma_K is None:
|
|
66
|
+
sigma_K = torch.ones_like(digits, dtype=torch.float32)
|
|
67
|
+
parts.append(sigma_K.unsqueeze(-1).float())
|
|
68
|
+
return digits, torch.cat(parts, dim=-1)
|
voxyne/generate.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Byte-level generation for VoxyneLM (markers + sigma_K format).
|
|
2
|
+
|
|
3
|
+
A prompt is wrapped as a user turn. The model generates the assistant turn in
|
|
4
|
+
the format it trained on:
|
|
5
|
+
<|bos|><|user|>PROMPT<|endturn|><|assistant|> ... generated ...
|
|
6
|
+
sigma_K is -1 over the user prompt bytes, +1 over markers and generated bytes.
|
|
7
|
+
Greedy by default. Top-k and temperature are optional. generate_cached uses a KV
|
|
8
|
+
cache and matches greedy generate().
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
import torch
|
|
15
|
+
import torch.nn.functional as F
|
|
16
|
+
|
|
17
|
+
from .markers import ASSISTANT, BOS, ENDTURN, EOS, USER, sigma_k_for_dialogue_bytes
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def build_prompt(user_text: str):
|
|
21
|
+
b = BOS + USER + user_text.encode("utf-8") + ENDTURN + ASSISTANT
|
|
22
|
+
sig = np.frombuffer(sigma_k_for_dialogue_bytes(b), dtype=np.int8).astype(np.float32)
|
|
23
|
+
return b, sig
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _sample(nl, temperature, top_k):
|
|
27
|
+
if temperature and temperature > 0:
|
|
28
|
+
probs = F.softmax(nl / temperature, dim=-1)
|
|
29
|
+
if top_k:
|
|
30
|
+
v, i = torch.topk(probs, min(top_k, probs.numel()))
|
|
31
|
+
probs = torch.zeros_like(probs).scatter_(0, i, v)
|
|
32
|
+
probs = probs / probs.sum()
|
|
33
|
+
return int(torch.multinomial(probs, 1))
|
|
34
|
+
return int(nl.argmax())
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@torch.no_grad()
|
|
38
|
+
def generate(model, encoder, user_text, *, max_new=160, temperature=0.0, top_k=40,
|
|
39
|
+
device="cpu", max_len=1024) -> str:
|
|
40
|
+
model.eval()
|
|
41
|
+
encoder.eval()
|
|
42
|
+
prefix, sig = build_prompt(user_text)
|
|
43
|
+
digits = [x + 1 for x in prefix]
|
|
44
|
+
sigma = list(sig)
|
|
45
|
+
gen = bytearray()
|
|
46
|
+
for _ in range(max_new):
|
|
47
|
+
if len(digits) >= max_len:
|
|
48
|
+
break
|
|
49
|
+
x = torch.tensor([digits], dtype=torch.long, device=device)
|
|
50
|
+
s = torch.tensor([sigma], dtype=torch.float32, device=device)
|
|
51
|
+
_, aux = encoder(x, sigma_K=s)
|
|
52
|
+
logits, _, _ = model(x, aux)
|
|
53
|
+
nb = _sample(logits[0, -1], temperature, top_k)
|
|
54
|
+
digits.append(nb + 1)
|
|
55
|
+
sigma.append(1.0)
|
|
56
|
+
gen.append(nb)
|
|
57
|
+
if gen.endswith(ENDTURN) or gen.endswith(EOS):
|
|
58
|
+
break
|
|
59
|
+
for m in (ENDTURN, EOS):
|
|
60
|
+
if gen.endswith(m):
|
|
61
|
+
gen = gen[: -len(m)]
|
|
62
|
+
return gen.decode("utf-8", errors="replace")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@torch.no_grad()
|
|
66
|
+
def generate_cached(model, encoder, user_text, *, max_new=160, temperature=0.0, top_k=40,
|
|
67
|
+
device="cpu", max_len=1024) -> str:
|
|
68
|
+
"""KV-cached generation: prefill once, then decode one byte at a time feeding
|
|
69
|
+
only the new byte + cached K/V. Greedy output matches generate()."""
|
|
70
|
+
model.eval()
|
|
71
|
+
encoder.eval()
|
|
72
|
+
prefix, sig = build_prompt(user_text)
|
|
73
|
+
digits = [x + 1 for x in prefix]
|
|
74
|
+
sigma = list(sig)
|
|
75
|
+
x = torch.tensor([digits], dtype=torch.long, device=device)
|
|
76
|
+
s = torch.tensor([sigma], dtype=torch.float32, device=device)
|
|
77
|
+
_, aux = encoder(x, sigma_K=s)
|
|
78
|
+
logits, _, _, past = model(x, aux, use_cache=True)
|
|
79
|
+
nl = logits[0, -1]
|
|
80
|
+
gen, seqlen = bytearray(), len(digits)
|
|
81
|
+
for _ in range(max_new):
|
|
82
|
+
nb = _sample(nl, temperature, top_k)
|
|
83
|
+
gen.append(nb)
|
|
84
|
+
if gen.endswith(ENDTURN) or gen.endswith(EOS) or seqlen >= max_len:
|
|
85
|
+
break
|
|
86
|
+
seqlen += 1
|
|
87
|
+
xx = torch.tensor([[nb + 1]], dtype=torch.long, device=device)
|
|
88
|
+
ss = torch.tensor([[1.0]], dtype=torch.float32, device=device)
|
|
89
|
+
_, aux1 = encoder(xx, sigma_K=ss)
|
|
90
|
+
logits, _, _, past = model(xx, aux1, past_key_values=past, use_cache=True)
|
|
91
|
+
nl = logits[0, -1]
|
|
92
|
+
for m in (ENDTURN, EOS):
|
|
93
|
+
if gen.endswith(m):
|
|
94
|
+
gen = gen[: -len(m)]
|
|
95
|
+
return gen.decode("utf-8", errors="replace")
|
voxyne/markers.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Byte-level conversational markers for Voxyne.
|
|
2
|
+
|
|
3
|
+
Marker format: STX + ascii-tag + ETX (0x01 + tag + 0x02). Control bytes are rare
|
|
4
|
+
in real UTF-8 text, so these give the model a clear, collision-free signal.
|
|
5
|
+
This module holds the conversational subset (the data-building wrappers live in
|
|
6
|
+
the training package).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
STX = 0x01
|
|
14
|
+
ETX = 0x02
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _tag(name: str) -> bytes:
|
|
18
|
+
if any(c <= 0x02 for c in name.encode("ascii")):
|
|
19
|
+
raise ValueError("tag name cannot contain control bytes")
|
|
20
|
+
return bytes([STX]) + name.encode("ascii") + bytes([ETX])
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
BOS = _tag("bos")
|
|
24
|
+
EOS = _tag("eos")
|
|
25
|
+
USER = _tag("user")
|
|
26
|
+
ASSISTANT = _tag("assistant")
|
|
27
|
+
SYSTEM = _tag("system")
|
|
28
|
+
ENDTURN = _tag("endturn")
|
|
29
|
+
PAD = _tag("pad")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def sigma_k_for_dialogue_bytes(data: bytes) -> bytes:
|
|
33
|
+
"""Role-bit stream over a wrapped dialogue: user-turn bytes -> -1, everything
|
|
34
|
+
else (assistant, system, markers) -> +1. Serialized as int8 bytes (0xff=-1,
|
|
35
|
+
0x01=+1), directly memmap-able as np.int8."""
|
|
36
|
+
n = len(data)
|
|
37
|
+
sig = np.ones(n, dtype=np.int8)
|
|
38
|
+
in_user = False
|
|
39
|
+
i = 0
|
|
40
|
+
while i < n:
|
|
41
|
+
if data[i:i + len(USER)] == USER:
|
|
42
|
+
in_user = True
|
|
43
|
+
i += len(USER)
|
|
44
|
+
elif data[i:i + len(ASSISTANT)] == ASSISTANT:
|
|
45
|
+
in_user = False
|
|
46
|
+
i += len(ASSISTANT)
|
|
47
|
+
elif data[i:i + len(SYSTEM)] == SYSTEM:
|
|
48
|
+
in_user = False
|
|
49
|
+
i += len(SYSTEM)
|
|
50
|
+
elif data[i:i + len(ENDTURN)] == ENDTURN:
|
|
51
|
+
i += len(ENDTURN)
|
|
52
|
+
else:
|
|
53
|
+
sig[i] = -1 if in_user else +1
|
|
54
|
+
i += 1
|
|
55
|
+
return sig.tobytes()
|
voxyne/model.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Byte-level RahuKetu transformer for Voxyne.
|
|
2
|
+
|
|
3
|
+
A decoder-only Transformer backbone over a byte-level zero-free representation.
|
|
4
|
+
It uses standard building blocks: causal self-attention, learned positions,
|
|
5
|
+
pre-norm, and GELU MLP. It adds the RahuKetu sigma_K control channel and an
|
|
6
|
+
out_sigma head. It does not use a BPE or GPT tokenizer stack.
|
|
7
|
+
|
|
8
|
+
Shape: d768 / 18L / 12h / FFN 4x, about 128.8M parameters.
|
|
9
|
+
Runtime path: SDPA / FlashAttention and KV cache.
|
|
10
|
+
|
|
11
|
+
Per position:
|
|
12
|
+
digit in [1, 256] byte identity. byte+1, with 0 as pad
|
|
13
|
+
aux in R^aux_dim sigma_K (role) direction channel
|
|
14
|
+
Input: x = ByteEmbed[digit] (+ AuxProj(aux)) + PosEmbed[pos]
|
|
15
|
+
Heads: out_digit -> 256 (next byte), out_sigma -> 2 (next sigma_K), recon (optional)
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import torch
|
|
21
|
+
import torch.nn.functional as F
|
|
22
|
+
from torch import nn
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class CausalSelfAttention(nn.Module):
|
|
26
|
+
def __init__(self, d_model: int, n_heads: int, dropout: float):
|
|
27
|
+
super().__init__()
|
|
28
|
+
assert d_model % n_heads == 0
|
|
29
|
+
self.n_heads = n_heads
|
|
30
|
+
self.head_dim = d_model // n_heads
|
|
31
|
+
self.qkv = nn.Linear(d_model, 3 * d_model)
|
|
32
|
+
self.proj = nn.Linear(d_model, d_model)
|
|
33
|
+
self.dropout = dropout
|
|
34
|
+
|
|
35
|
+
def forward(self, x, past_kv=None, use_cache=False):
|
|
36
|
+
B, T, C = x.shape
|
|
37
|
+
qkv = self.qkv(x).view(B, T, 3, self.n_heads, self.head_dim)
|
|
38
|
+
q, k, v = qkv.permute(2, 0, 3, 1, 4).unbind(0)
|
|
39
|
+
if past_kv is not None:
|
|
40
|
+
pk, pv = past_kv
|
|
41
|
+
k = torch.cat([pk, k], dim=2)
|
|
42
|
+
v = torch.cat([pv, v], dim=2)
|
|
43
|
+
new_kv = (k, v) if use_cache else None
|
|
44
|
+
causal = past_kv is None
|
|
45
|
+
y = F.scaled_dot_product_attention(
|
|
46
|
+
q, k, v, is_causal=causal,
|
|
47
|
+
dropout_p=self.dropout if self.training else 0.0,
|
|
48
|
+
)
|
|
49
|
+
y = y.transpose(1, 2).contiguous().view(B, T, C)
|
|
50
|
+
return self.proj(y), new_kv
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Block(nn.Module):
|
|
54
|
+
def __init__(self, d_model: int, n_heads: int, dropout: float):
|
|
55
|
+
super().__init__()
|
|
56
|
+
self.ln1 = nn.LayerNorm(d_model)
|
|
57
|
+
self.attn = CausalSelfAttention(d_model, n_heads, dropout)
|
|
58
|
+
self.ln2 = nn.LayerNorm(d_model)
|
|
59
|
+
self.mlp = nn.Sequential(
|
|
60
|
+
nn.Linear(d_model, 4 * d_model),
|
|
61
|
+
nn.GELU(),
|
|
62
|
+
nn.Linear(4 * d_model, d_model),
|
|
63
|
+
)
|
|
64
|
+
self.drop = nn.Dropout(dropout)
|
|
65
|
+
|
|
66
|
+
def forward(self, x, past_kv=None, use_cache=False):
|
|
67
|
+
a, new_kv = self.attn(self.ln1(x), past_kv, use_cache)
|
|
68
|
+
x = x + self.drop(a)
|
|
69
|
+
x = x + self.drop(self.mlp(self.ln2(x)))
|
|
70
|
+
return x, new_kv
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class VoxyneLM(nn.Module):
|
|
74
|
+
def __init__(
|
|
75
|
+
self,
|
|
76
|
+
alphabet_size: int = 256,
|
|
77
|
+
d_model: int = 768,
|
|
78
|
+
n_heads: int = 12,
|
|
79
|
+
n_layers: int = 18,
|
|
80
|
+
max_len: int = 1024,
|
|
81
|
+
aux_dim: int = 1,
|
|
82
|
+
dropout: float = 0.1,
|
|
83
|
+
use_aux: bool = True,
|
|
84
|
+
byte_embed_bottleneck: int = 0,
|
|
85
|
+
recon: bool = False,
|
|
86
|
+
):
|
|
87
|
+
super().__init__()
|
|
88
|
+
self.alphabet_size = alphabet_size
|
|
89
|
+
self.d_model = d_model
|
|
90
|
+
self.max_len = max_len
|
|
91
|
+
self.aux_dim = aux_dim
|
|
92
|
+
self.use_aux = use_aux
|
|
93
|
+
|
|
94
|
+
if byte_embed_bottleneck and byte_embed_bottleneck > 0:
|
|
95
|
+
self.byte_embed = nn.Embedding(alphabet_size + 1, byte_embed_bottleneck, padding_idx=0)
|
|
96
|
+
self.byte_up = nn.Linear(byte_embed_bottleneck, d_model, bias=False)
|
|
97
|
+
else:
|
|
98
|
+
self.byte_embed = nn.Embedding(alphabet_size + 1, d_model, padding_idx=0)
|
|
99
|
+
self.byte_up = None
|
|
100
|
+
self.aux_proj = nn.Linear(aux_dim, d_model, bias=False) if use_aux else None
|
|
101
|
+
self.pos_embed = nn.Embedding(max_len, d_model)
|
|
102
|
+
self.drop = nn.Dropout(dropout)
|
|
103
|
+
|
|
104
|
+
self.blocks = nn.ModuleList([Block(d_model, n_heads, dropout) for _ in range(n_layers)])
|
|
105
|
+
self.norm = nn.LayerNorm(d_model)
|
|
106
|
+
|
|
107
|
+
self.out_digit = nn.Linear(d_model, alphabet_size)
|
|
108
|
+
self.out_sigma = nn.Linear(d_model, 2)
|
|
109
|
+
self.recon_head = nn.Linear(aux_dim, alphabet_size) if (recon and use_aux) else None
|
|
110
|
+
|
|
111
|
+
def param_count(self) -> int:
|
|
112
|
+
return sum(p.numel() for p in self.parameters())
|
|
113
|
+
|
|
114
|
+
def forward(self, digits, aux=None, past_key_values=None, use_cache=False):
|
|
115
|
+
B, T = digits.shape
|
|
116
|
+
past_len = past_key_values[0][0].shape[2] if past_key_values else 0
|
|
117
|
+
if past_len + T > self.max_len:
|
|
118
|
+
raise ValueError(f"sequence length {past_len + T} exceeds max_len {self.max_len}")
|
|
119
|
+
|
|
120
|
+
x = self.byte_embed(digits)
|
|
121
|
+
if self.byte_up is not None:
|
|
122
|
+
x = self.byte_up(x)
|
|
123
|
+
recon_logits = None
|
|
124
|
+
if self.use_aux:
|
|
125
|
+
if aux is None:
|
|
126
|
+
raise ValueError("use_aux=True but aux not provided")
|
|
127
|
+
x = x + self.aux_proj(aux)
|
|
128
|
+
if self.recon_head is not None:
|
|
129
|
+
recon_logits = self.recon_head(aux)
|
|
130
|
+
pos = torch.arange(past_len, past_len + T, device=digits.device).unsqueeze(0).expand(B, T)
|
|
131
|
+
x = self.drop(x + self.pos_embed(pos))
|
|
132
|
+
|
|
133
|
+
new_past = [] if use_cache else None
|
|
134
|
+
for i, blk in enumerate(self.blocks):
|
|
135
|
+
pkv = past_key_values[i] if past_key_values is not None else None
|
|
136
|
+
x, kv = blk(x, pkv, use_cache)
|
|
137
|
+
if use_cache:
|
|
138
|
+
new_past.append(kv)
|
|
139
|
+
x = self.norm(x)
|
|
140
|
+
|
|
141
|
+
out = (self.out_digit(x), self.out_sigma(x), recon_logits)
|
|
142
|
+
if use_cache:
|
|
143
|
+
return out + (new_past,)
|
|
144
|
+
return out
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: voxyne
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A tiny byte-level conversational language model with the RahuKetu sigma_K channel.
|
|
5
|
+
Project-URL: Homepage, https://github.com/DecipherPunk/voxyne
|
|
6
|
+
Author-email: Ramakrishnan <human@setc.dev>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: byte-level,conversational,language-model,rahuketu,voxyne
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Requires-Dist: numpy>=1.24
|
|
15
|
+
Requires-Dist: torch>=2.1
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
18
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# voxyne
|
|
22
|
+
|
|
23
|
+
A tiny byte-level conversational language model with the RahuKetu `sigma_K`
|
|
24
|
+
control channel. Loads on CPU and runs offline.
|
|
25
|
+
|
|
26
|
+
> The **code** is Apache-2.0. The released **research weights** are hosted on
|
|
27
|
+
> Hugging Face under a separate non-commercial model license. See the model card.
|
|
28
|
+
|
|
29
|
+
## Project
|
|
30
|
+
|
|
31
|
+
Voxyne is a small byte-level conversational model package.
|
|
32
|
+
|
|
33
|
+
- **rahuketu** - the zero-free encoding framework behind the `sigma_K` control channel.
|
|
34
|
+
- **voxyne** - this repository contains the model code, marker format, `sigma_K`
|
|
35
|
+
encoder, generation helpers, and tests. It does not include a training loop,
|
|
36
|
+
retrieval system, mutable memory, or autonomous learning runtime.
|
|
37
|
+
- Released research weights and quantized artifacts are published separately on
|
|
38
|
+
Hugging Face with their own model card and license.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
pip install voxyne
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
Smoke test with random weights:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import torch
|
|
52
|
+
from voxyne import smoke_config, build
|
|
53
|
+
|
|
54
|
+
model, enc = build(smoke_config())
|
|
55
|
+
x = torch.tensor([[1, 2, 3, 4]])
|
|
56
|
+
_, aux = enc(x, sigma_K=torch.tensor([[1.0, 1.0, -1.0, 1.0]]))
|
|
57
|
+
print(model(x, aux)[0].shape) # -> next-byte logits
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
To generate real responses, load a trained checkpoint. The package does not
|
|
61
|
+
bundle weights. Download `voxyne-v0.1.pt` from the Hugging Face model repo,
|
|
62
|
+
review the license there, and pass the local path to `load_weights`:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from voxyne import VoxyneConfig, build, load_weights, generate
|
|
66
|
+
|
|
67
|
+
model, enc = build(VoxyneConfig()) # 128.8M base with sigma_K aux
|
|
68
|
+
load_weights(model, "/path/to/voxyne-v0.1.pt")
|
|
69
|
+
print(generate(model, enc, "hello", device="cpu"))
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Components
|
|
73
|
+
|
|
74
|
+
- `VoxyneConfig` - architecture + encoder settings.
|
|
75
|
+
- `RahuKetuEncoder` - byte digits -> `(digits, aux)` with the `sigma_K` channel.
|
|
76
|
+
- `VoxyneLM` - transformer with `out_digit`, `out_sigma`, and KV cache.
|
|
77
|
+
- `generate` / `generate_cached` - greedy or top-k decoding. Cached generation uses KV cache.
|
|
78
|
+
- `build(config)` - construct a matching `(model, encoder)` pair.
|
|
79
|
+
|
|
80
|
+
## Weights
|
|
81
|
+
|
|
82
|
+
The package ships the **architecture only**. No model weights are bundled in this
|
|
83
|
+
repository.
|
|
84
|
+
|
|
85
|
+
Released research weights and ONNX artifacts:
|
|
86
|
+
|
|
87
|
+
- Model repo: `https://huggingface.co/decipherpunk/voxyne`
|
|
88
|
+
- PyTorch checkpoint: `voxyne-v0.1.pt`
|
|
89
|
+
- ONNX artifacts: `voxyne-int4.onnx`, `voxyne-int8.onnx`, `voxyne-fp32.onnx`
|
|
90
|
+
- Weight license: CC BY-NC 4.0 / non-commercial, per the model card
|
|
91
|
+
|
|
92
|
+
The Apache-2.0 license in this repository covers code only. Weight files remain
|
|
93
|
+
under the model-card license.
|
|
94
|
+
|
|
95
|
+
## Tests
|
|
96
|
+
|
|
97
|
+
`pytest` covers the package API, tensor shapes, loading, and KV-cache path.
|
|
98
|
+
Model quality depends on the released weights.
|
|
99
|
+
|
|
100
|
+
## Provenance / AI assistance
|
|
101
|
+
|
|
102
|
+
Built by Ramakrishnan (ORCID 0009-0006-0905-7275). The idea, architecture, and
|
|
103
|
+
research direction are by Ramakrishnan. AI tools helped with tests, automation,
|
|
104
|
+
and QA.
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
Apache-2.0 (code). See `LICENSE` and `NOTICE`. Weights are licensed separately on
|
|
109
|
+
the model card.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
voxyne/__init__.py,sha256=BeQ_f1c0lLlQ-1XSNDXVI3xm0OBjzPnIpv0R6EYUKok,2017
|
|
2
|
+
voxyne/config.py,sha256=mEDHbfe6Wt0mNP5c-dDxbjz_iiVag9N6jRoQHzHAMo4,1011
|
|
3
|
+
voxyne/encoder.py,sha256=ld7RUD4p1aBlGK9KZHoq4Yyr1GFMrR9jRBBjQ9XhRcQ,2597
|
|
4
|
+
voxyne/generate.py,sha256=kxVfmcgpsvTC1meJoQKa779SWwpPecu2Ji2nunNDrjk,3569
|
|
5
|
+
voxyne/markers.py,sha256=hpPd-JNWz1p7cVF3B3Lq29TFT50xQQb_GN8XSuX6ovc,1627
|
|
6
|
+
voxyne/model.py,sha256=ej7dqO-JJgYnAvNJnKYQWjtlj2aTE796wT01t8-yfZ4,5453
|
|
7
|
+
voxyne-0.1.0.dist-info/METADATA,sha256=ZMixU_8tLCTCmR9jxaJVh-93EH9lAfJHwWK8lFbMbZY,3709
|
|
8
|
+
voxyne-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
9
|
+
voxyne-0.1.0.dist-info/licenses/LICENSE,sha256=rsp2NW_5agm5DzKAqHa36QiJDI4FP3abnXBJA7ne9tA,11342
|
|
10
|
+
voxyne-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Ramakrishnan
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|