Repository navigation
Expand file tree
/
Copy pathrun.go
More file actions
179 lines (171 loc) · 4.75 KB
/
Copy pathrun.go
File metadata and controls
179 lines (171 loc) · 4.75 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"strconv"
"chr/internal/verify"
)
func run(args []string, stdout, stderr io.Writer) int {
if len(args) == 0 {
fmt.Fprintf(stderr, "usage: chr <compress|decode|verify> [flags]\n")
return 1
}
if args[0] == "-h" || args[0] == "--help" {
fmt.Fprintf(stdout, "chr compress|decode|verify\n")
return 0
}
switch args[0] {
case "compress":
return runCompress(args[1:], stdout, stderr)
case "decode":
return runDecode(args[1:], stdout, stderr)
case "verify":
return runVerify(args[1:], stdout, stderr)
default:
fmt.Fprintf(stderr, "chr: unknown command %s\n", args[0])
return 1
}
}
func newFS(name string, stderr io.Writer) *flag.FlagSet {
fs := flag.NewFlagSet(name, flag.ContinueOnError)
fs.SetOutput(stderr)
return fs
}
func parseFS(fs *flag.FlagSet, args []string) int {
if err := fs.Parse(args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 1
}
return -1
}
func runCompress(args []string, stdout, stderr io.Writer) int {
fs := newFS("compress", stderr)
in := fs.String("in", "", "safetensors file, dir, or index.json")
out := fs.String("out", "", "output .chr")
codec := fs.String("codec", "", "nf4 or vq")
group := fs.Int("group-size", 0, "must match codec canonical size")
seed := fs.Int64("seed", 0, "VQ k-means seed")
iters := fs.Int("iters", 20, "VQ Lloyd iterations")
chunk := fs.Int("chunk", 262144, "VQ assignment chunk")
stripeB := fs.Int64("stripe-bytes", 268435456, "F32 stripe threshold")
stripeR := fs.Int("stripe-rows", 4096, "preferred stripe height")
arch := fs.String("arch", "unknown", "CHR0 arch")
hidden := fs.Int("hidden-size", 0, "header hidden_size")
inter := fs.Int("intermediate-size", 0, "header intermediate_size")
layers := fs.Int("num-layers", 0, "header num_layers")
vocab := fs.Int("vocab-size", 0, "header vocab_size")
quiet := fs.Bool("quiet", false, "no progress")
if c := parseFS(fs, args); c >= 0 {
return c
}
if *in == "" || *out == "" || *codec == "" {
if *codec == "" && *in != "" && *out != "" {
fmt.Fprintf(stderr, "chr: --codec is required\n")
return 1
}
fmt.Fprintf(stderr, "chr: compress requires --in --out --codec\n")
return 1
}
gs := *group
if gs == 0 {
if *codec == "nf4" {
gs = 64
} else if *codec == "vq" {
gs = 8
}
}
if *codec == "nf4" && gs != 64 {
fmt.Fprintf(stderr, "chr: nf4 group-size must be 64\n")
return 1
}
if *codec == "vq" && gs != 8 {
fmt.Fprintf(stderr, "chr: vq group-size must be 8\n")
return 1
}
if *iters < 1 {
fmt.Fprintf(stderr, "chr: iters must be >= 1\n")
return 1
}
if *chunk < 256 {
fmt.Fprintf(stderr, "chr: chunk must be >= 256\n")
return 1
}
err := verify.Compress(verify.CompressOptions{
In: *in, Out: *out, Codec: *codec,
GroupSize: gs, Seed: uint64(*seed), Iters: *iters, Chunk: *chunk,
StripeBytes: *stripeB, StripeRows: *stripeR,
Arch: *arch, HiddenSize: *hidden, Intermediate: *inter, NumLayers: *layers, VocabSize: *vocab,
Quiet: *quiet, Log: stderr,
})
if err != nil {
fmt.Fprintf(stderr, "chr: %v\n", err)
_ = os.Remove(*out)
return 1
}
_ = stdout
return 0
}
func runDecode(args []string, stdout, stderr io.Writer) int {
fs := newFS("decode", stderr)
in := fs.String("in", "", "input .chr")
out := fs.String("out", "", "output .safetensors")
if c := parseFS(fs, args); c >= 0 {
return c
}
if *in == "" || *out == "" {
fmt.Fprintf(stderr, "chr: decode requires --in --out\n")
return 1
}
if err := verify.Decode(*in, *out); err != nil {
fmt.Fprintf(stderr, "chr: %v\n", err)
return 1
}
_ = stdout
return 0
}
func runVerify(args []string, stdout, stderr io.Writer) int {
fs := newFS("verify", stderr)
orig := fs.String("orig", "", "original safetensors")
chrPath := fs.String("chr", "", "compressed .chr")
failRMSE := fs.String("fail-rmse", "", "RMSE threshold")
failMax := fs.String("fail-maxabs", "", "maxabs threshold")
js := fs.Bool("json", false, "JSON report on stdout")
quiet := fs.Bool("quiet", false, "summary only")
if c := parseFS(fs, args); c >= 0 {
return c
}
if *orig == "" || *chrPath == "" {
fmt.Fprintf(stderr, "chr: verify requires --orig --chr\n")
return 1
}
opt := verify.VerifyOptions{
Orig: *orig, CHR: *chrPath, JSON: *js, Quiet: *quiet,
Stdout: stdout, Stderr: stderr,
}
if *failRMSE != "" {
v, err := strconv.ParseFloat(*failRMSE, 64)
if err != nil || v != v || v < 0 {
fmt.Fprintf(stderr, "chr: bad --fail-rmse\n")
return 1
}
opt.FailRMSE = &v
}
if *failMax != "" {
v, err := strconv.ParseFloat(*failMax, 64)
if err != nil || v != v || v < 0 {
fmt.Fprintf(stderr, "chr: bad --fail-maxabs\n")
return 1
}
opt.FailMax = &v
}
r := verify.Verify(opt)
if r.Err != nil && r.ExitCode == 1 {
fmt.Fprintf(stderr, "chr: %v\n", r.Err)
}
return r.ExitCode
}