kizzasi-tokenizer 0.1.0

Signal quantization and tokenization for Kizzasi AGSP - VQ-VAE, μ-law, continuous embeddings
Documentation
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Contributing to kizzasi-tokenizer

Thank you for your interest in contributing to kizzasi-tokenizer! This document provides guidelines and instructions for contributing.

## Development Setup

### Prerequisites

- Rust 1.70+ (stable)
- Git
- (Optional) Docker for local CI testing

### Initial Setup

1. Clone the repository:
```bash
git clone https://github.com/cool-japan/kizzasi.git
cd kizzasi/crates/kizzasi-tokenizer
```

2. Install development tools:
```bash
make install-tools
```

This installs:
- `cargo-tarpaulin` - Code coverage
- `cargo-criterion` - Benchmarking
- `critcmp` - Benchmark comparison
- `cargo-audit` - Security auditing
- `cargo-udeps` - Unused dependency detection
- `cargo-minimal-versions` - Minimum version testing

3. Run tests to verify setup:
```bash
make test
```

## Development Workflow

### Before Making Changes

1. Create a feature branch:
```bash
git checkout -b feature/my-feature
```

2. Ensure all tests pass:
```bash
make test
```

### Making Changes

1. Write code following Rust conventions:
   - Use `cargo fmt` to format code
   - Run `cargo clippy` to catch common mistakes
   - Add documentation for public APIs
   - Write tests for new functionality

2. Run pre-commit checks:
```bash
make pre-commit
```

This runs:
- Code formatting check
- Clippy lints
- All tests

### Testing

We have comprehensive testing requirements:

#### Unit Tests
```bash
make test-unit
```

#### Integration Tests
```bash
make test
```

#### Property-Based Tests
```bash
make test-proptest
```

#### Benchmarks
```bash
make bench
```

### Code Coverage

Generate a coverage report:
```bash
make coverage
```

Open `coverage/index.html` in your browser to view the report.

**Coverage Requirements:**
- New code should have >80% coverage
- Critical paths should have >95% coverage
- Property-based tests are encouraged for mathematical properties

### Performance Testing

Check for performance regressions:
```bash
make check-perf
```

This compares your branch's performance against `master`. Regressions >10% will fail the check.

### Documentation

Build and view documentation:
```bash
make docs
```

**Documentation Requirements:**
- All public APIs must have rustdoc comments
- Include examples in doc comments
- Use `//!` for module-level documentation
- Add usage examples to `examples/` directory

## Pull Request Process

### Before Submitting

1. Ensure all checks pass:
```bash
make ci-local
```

2. Update documentation if needed
3. Add examples for new features
4. Update CHANGELOG.md (if applicable)

### Submitting a PR

1. Push your branch:
```bash
git push origin feature/my-feature
```

2. Create a pull request on GitHub

3. Fill out the PR template with:
   - Description of changes
   - Related issues
   - Testing performed
   - Breaking changes (if any)

### PR Review Process

Your PR will be automatically checked for:
- ✅ All tests passing on Ubuntu, macOS, and Windows
- ✅ Clippy lints passing
- ✅ Code formatting
- ✅ Documentation building without warnings
- ✅ No performance regressions >150%
- ✅ Code coverage maintained or improved

Manual review will check for:
- Code quality and style
- Test coverage
- Documentation completeness
- API design
- Performance implications

## Code Style Guidelines

### Rust Style

Follow the [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/):

```rust
// Good: Clear, documented, tested
/// Encodes a signal into tokens using linear quantization.
///
/// # Arguments
///
/// * `signal` - Input signal to encode
/// * `bits` - Number of bits for quantization (1-16)
///
/// # Returns
///
/// Encoded tokens as a 1D array
///
/// # Examples
///
/// ```
/// use kizzasi_tokenizer::LinearQuantizer;
///
/// let quantizer = LinearQuantizer::new(8, -1.0, 1.0)?;
/// let tokens = quantizer.encode(&signal)?;
/// # Ok::<(), kizzasi_tokenizer::TokenizerError>(())
/// ```
pub fn encode(&self, signal: &Array1<f32>) -> TokenizerResult<Array1<f32>> {
    // Implementation
}
```

### Naming Conventions

- **Types**: `PascalCase` (e.g., `LinearQuantizer`)
- **Functions**: `snake_case` (e.g., `encode_batch`)
- **Constants**: `SCREAMING_SNAKE_CASE` (e.g., `DEFAULT_BITS`)
- **Module files**: `snake_case` (e.g., `linear_quantizer.rs`)

### Error Handling

Use the `TokenizerError` enum for all errors:

```rust
// Good: Specific error with context
if signal.is_empty() {
    return Err(TokenizerError::invalid_input(
        "encode",
        "Signal cannot be empty"
    ));
}

// Bad: Generic error
if signal.is_empty() {
    return Err(TokenizerError::InvalidConfig("empty".into()));
}
```

### Testing Guidelines

```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_encode_decode_roundtrip() {
        // Setup
        let quantizer = LinearQuantizer::new(8, -1.0, 1.0).unwrap();
        let signal = Array1::from_vec(vec![0.0, 0.5, -0.5, 1.0]);

        // Execute
        let encoded = quantizer.encode(&signal).unwrap();
        let decoded = quantizer.decode(&encoded).unwrap();

        // Verify
        assert_eq!(decoded.len(), signal.len());
        for (orig, dec) in signal.iter().zip(decoded.iter()) {
            assert!((orig - dec).abs() < 0.01); // Within quantization error
        }
    }
}
```

## Benchmarking

### Writing Benchmarks

Add benchmarks to `benches/comprehensive_benchmarks.rs`:

```rust
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn bench_my_feature(c: &mut Criterion) {
    let mut group = c.benchmark_group("my_feature");

    for size in [64, 256, 1024, 4096] {
        group.bench_function(format!("size_{}", size), |b| {
            let data = vec![0.0f32; size];
            b.iter(|| {
                // Benchmark code
                black_box(my_feature(&data))
            });
        });
    }

    group.finish();
}

criterion_group!(benches, bench_my_feature);
criterion_main!(benches);
```

### Running Benchmarks

```bash
# Run all benchmarks
make bench

# Save baseline
make bench-baseline

# Compare with baseline
make bench-compare
```

## CI/CD Pipeline

### Automated Checks

Every push and PR triggers:

1. **Test Suite** (`ci.yml`)
   - Multi-platform testing (Ubuntu, macOS, Windows)
   - Multiple Rust versions (stable, beta, nightly)
   - Clippy and rustfmt checks
   - Documentation build
   - Miri undefined behavior detection
   - Security audit
   - Minimal versions check
   - Unused dependencies check

2. **Benchmarks** (`benchmark.yml`)
   - Performance benchmarking
   - Regression detection
   - Memory profiling
   - Throughput analysis

3. **Coverage** (`coverage.yml`)
   - Code coverage with Tarpaulin
   - Coverage diff for PRs
   - Line-by-line coverage with grcov

### Local CI Testing

Test workflows locally with [act](https://github.com/nektos/act):

```bash
# Install act
brew install act  # macOS
# or curl -s https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash

# Run CI tests
act -j test

# Run benchmarks
act -j benchmark

# Run coverage
act -j coverage
```

Or use the Makefile:

```bash
make ci-local
```

## Release Process

Releases are automated through GitHub Actions:

### Version Bump

1. Update version in `Cargo.toml`:
```toml
[package]
version = "0.2.0"  # Bump version
```

2. Update CHANGELOG.md with changes

3. Commit and tag:
```bash
git commit -am "chore: bump version to 0.2.0"
git tag kizzasi-tokenizer-v0.2.0
git push --tags
```

4. GitHub Actions will:
   - Run all tests
   - Build for all platforms
   - Create GitHub release
   - Publish to crates.io
   - Deploy documentation

### Pre-Release Checklist

Use the Makefile for a comprehensive check:

```bash
make release-checklist
```

This runs:
- Clean build
- All CI checks
- Performance regression check
- Coverage report
- Publish dry-run

## Getting Help

- **Questions**: Open a [Discussion]https://github.com/cool-japan/kizzasi/discussions
- **Bugs**: Open an [Issue]https://github.com/cool-japan/kizzasi/issues
- **Security**: Email security@cool-japan.org

## Code of Conduct

Be respectful, inclusive, and collaborative. We follow the [Rust Code of Conduct](https://www.rust-lang.org/policies/code-of-conduct).

## License

By contributing, you agree that your contributions will be licensed under the same license as the project (check LICENSE file).

## Additional Resources

- [Rust API Guidelines]https://rust-lang.github.io/api-guidelines/
- [Rust Book]https://doc.rust-lang.org/book/
- [Criterion.rs Guide]https://bheisler.github.io/criterion.rs/book/
- [Property-based testing with proptest]https://proptest-rs.github.io/proptest/

Thank you for contributing! 🦀