GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — tkintegers/base3_pack.py at master · CodeMasterCody3D/tkintegers

Expand file tree

/

base3_pack.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
67 lines (51 loc) · 2.33 KB
/

base3_pack.py

Copy path

File metadata and controls

67 lines (51 loc) · 2.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Packed balanced base-3 codes used by Tk-Integers.
Five trits occupy one byte. The implementation is copied from the TAARDIS
any-k packer and keeps k in the documented range 1..16.
"""
from __future__ import annotations
import torch
def _pow3(n: int, device):
return (3 ** torch.arange(n, device=device)).to(torch.int64)
def quantize_ktrit(W, k: int, group_size: int = 128):
"""Quantize ``[out, in]`` weights to signed balanced base-3 levels."""
O, I = W.shape
L = (3 ** k - 1) // 2
if not 1 <= k <= 16:
raise ValueError("k must be between 1 and 16")
if I % group_size:
raise ValueError(f"I={I} is not divisible by group_size={group_size}")
x = W.reshape(O, I // group_size, group_size).double()
scale = x.abs().amax(-1, keepdim=True).clamp_min(1e-12)
codes = torch.round(x / scale * L).clamp(-L, L).to(torch.int32).reshape(O, I)
scale_dtype = torch.float16 if k <= 5 else torch.float32
return codes, scale.squeeze(-1).to(scale_dtype)
def pack_ktrit(codes, k: int):
"""Pack signed codes ``[out, in]`` into five-trit-per-byte rows."""
O, I = codes.shape
L = (3 ** k - 1) // 2
if not 1 <= k <= 16:
raise ValueError("k must be between 1 and 16")
pad = (-(I * k)) % 5
nbytes = (I * k + pad) // 5
digits = ((codes.to(torch.int64) + L).reshape(O, I, 1) // _pow3(k, codes.device)) % 3
stream = digits.reshape(O, I * k)
if pad:
stream = torch.nn.functional.pad(stream, (0, pad))
packed = (stream.reshape(O, nbytes, 5) * _pow3(5, codes.device)).sum(-1).to(torch.uint8)
return packed.contiguous(), I, k
def unpack_ktrit(packed, I: int, k: int):
"""Unpack rows into signed int32 codes."""
O, nbytes = packed.shape
L = (3 ** k - 1) // 2
digits = (packed.reshape(O, nbytes, 1).to(torch.int64) // _pow3(5, packed.device)) % 3
stream = digits.reshape(O, nbytes * 5)[:, :I * k]
levels = (stream.reshape(O, I, k) * _pow3(k, packed.device)).sum(-1)
return (levels - L).to(torch.int32)
def pack_base3(codes):
packed, width, _ = pack_ktrit(codes.to(torch.int32), 1)
return packed, width
def unpack_base3(packed, width: int):
return unpack_ktrit(packed, width, 1)
def packed_bpw(weights: int, k: int) -> float:
"""Finite code-stream bits per weight, excluding scales."""
return 8.0 * ((weights * k + 4) // 5) / weights
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.