tenslib 0.0.1__tar.gz

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.
@@ -0,0 +1,55 @@
1
+ # The check for each push and each pull request: `make check` (formatting, the ASCII rule, clippy, then the package
2
+ # built and its tests) on x86-64 and AArch64 Linux and on macOS, and on Windows the wheel built and its tests run
3
+ # (Windows has no make).
4
+ #
5
+ # The compiler is the one rust-toolchain.toml names, installed by rustup, which the runners have. The tests that
6
+ # compare with Hugging Face's tokenizers library run here (it is installed with the tests).
7
+ name: check
8
+ on:
9
+ push:
10
+ pull_request:
11
+ permissions:
12
+ contents: read
13
+ concurrency:
14
+ group: check-${{ github.ref }}
15
+ cancel-in-progress: true
16
+ jobs:
17
+ check:
18
+ strategy:
19
+ fail-fast: false
20
+ matrix:
21
+ os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest]
22
+ runs-on: ${{ matrix.os }}
23
+ timeout-minutes: 60
24
+ steps:
25
+ - uses: actions/checkout@v4
26
+ - uses: actions/setup-python@v5
27
+ with:
28
+ python-version: "3.12"
29
+ - name: the toolchain of rust-toolchain.toml
30
+ run: |
31
+ rustup toolchain install
32
+ rustc --version
33
+ - name: make check
34
+ run: make check
35
+ windows:
36
+ runs-on: windows-latest
37
+ timeout-minutes: 60
38
+ defaults:
39
+ run:
40
+ shell: bash
41
+ steps:
42
+ - uses: actions/checkout@v4
43
+ - uses: actions/setup-python@v5
44
+ with:
45
+ python-version: "3.12"
46
+ - name: the toolchain of rust-toolchain.toml
47
+ run: |
48
+ rustup toolchain install
49
+ rustc --version
50
+ - name: the wheel, and its tests
51
+ run: |
52
+ python -m pip install --quiet maturin pytest tokenizers
53
+ maturin build --release --out dist
54
+ python -m pip install --no-index --find-links dist tenslib
55
+ python -m pytest -q -rs
@@ -0,0 +1,126 @@
1
+ # Each version tag pushed here (v0.1.0) is released: the wheels and the source archive built and tested, a GitHub
2
+ # Release made of them (titled "tenslib-python <version>", its text that version's section of CHANGELOG.md), and the
3
+ # same files published on PyPI as the package `tenslib`.
4
+ #
5
+ # - Wheels are abi3 (one serves CPython 3.10 and later) for x86-64 and AArch64 Linux (manylinux), macOS on Apple
6
+ # silicon and on Intel, and x86-64 Windows. Each is installed and its tests run on its own kind of machine, but for
7
+ # the Intel macOS wheel, which is built on Apple silicon and not run.
8
+ # - PyPI takes the files by trusted publishing: no token is kept here. PyPI must name this repository, this workflow
9
+ # (release.yml) and the environment `pypi` as the project's trusted publisher (PyPI: Account, Publishing,
10
+ # "Add a new pending publisher"). Nothing reaches PyPI unless every wheel's tests passed and the Release was made.
11
+ # - A tag whose push made no run can be released by hand: Actions, "release", "Run workflow", with the tag.
12
+ name: release
13
+ on:
14
+ push:
15
+ tags: ["v*"]
16
+ workflow_dispatch:
17
+ inputs:
18
+ tag:
19
+ description: "The tag to release (v0.1.0)"
20
+ required: true
21
+ permissions:
22
+ contents: read
23
+ jobs:
24
+ wheels:
25
+ strategy:
26
+ fail-fast: false
27
+ matrix:
28
+ include:
29
+ - { os: ubuntu-latest, target: x86_64, manylinux: auto, test: true }
30
+ - { os: ubuntu-24.04-arm, target: aarch64, manylinux: auto, test: true }
31
+ - { os: macos-latest, target: aarch64, test: true }
32
+ - { os: macos-latest, target: x86_64, test: false }
33
+ - { os: windows-latest, target: x64, test: true }
34
+ runs-on: ${{ matrix.os }}
35
+ timeout-minutes: 60
36
+ defaults:
37
+ run:
38
+ shell: bash
39
+ steps:
40
+ - uses: actions/checkout@v4
41
+ with:
42
+ ref: ${{ inputs.tag || github.ref }}
43
+ - name: the toolchain rust-toolchain.toml names
44
+ id: toolchain
45
+ run: echo "channel=$(sed -n 's/^channel = "\(.*\)"$/\1/p' rust-toolchain.toml)" >> "$GITHUB_OUTPUT"
46
+ - uses: PyO3/maturin-action@v1
47
+ with:
48
+ target: ${{ matrix.target }}
49
+ manylinux: ${{ matrix.manylinux }}
50
+ rust-toolchain: ${{ steps.toolchain.outputs.channel }}
51
+ args: --release --out dist
52
+ - uses: actions/setup-python@v5
53
+ if: matrix.test
54
+ with:
55
+ python-version: "3.12"
56
+ - name: the wheel installed, and the tests
57
+ if: matrix.test
58
+ run: |
59
+ python -m pip install --quiet pytest tokenizers
60
+ python -m pip install --no-index --find-links dist tenslib
61
+ python -m pytest -q -rs
62
+ - uses: actions/upload-artifact@v4
63
+ with:
64
+ name: wheel-${{ matrix.os }}-${{ matrix.target }}
65
+ path: dist
66
+ sdist:
67
+ runs-on: ubuntu-latest
68
+ timeout-minutes: 20
69
+ steps:
70
+ - uses: actions/checkout@v4
71
+ with:
72
+ ref: ${{ inputs.tag || github.ref }}
73
+ - uses: PyO3/maturin-action@v1
74
+ with:
75
+ command: sdist
76
+ args: --out dist
77
+ - uses: actions/upload-artifact@v4
78
+ with:
79
+ name: sdist
80
+ path: dist
81
+ release:
82
+ needs: [wheels, sdist]
83
+ runs-on: ubuntu-latest
84
+ timeout-minutes: 10
85
+ permissions:
86
+ contents: write
87
+ steps:
88
+ - uses: actions/checkout@v4
89
+ with:
90
+ ref: ${{ inputs.tag || github.ref }}
91
+ - uses: actions/download-artifact@v4
92
+ with:
93
+ path: dist
94
+ merge-multiple: true
95
+ - name: the Release, from CHANGELOG.md, with the files
96
+ env:
97
+ GH_TOKEN: ${{ github.token }}
98
+ TAG: ${{ inputs.tag || github.ref_name }}
99
+ run: |
100
+ version=${TAG#v}
101
+ awk -v v="## $version" '$0 == v {on = 1; next} /^## / {on = 0} on' CHANGELOG.md | sed -e '/./,$!d' > "$RUNNER_TEMP/notes.md"
102
+ if [ ! -s "$RUNNER_TEMP/notes.md" ]; then
103
+ echo "CHANGELOG.md has no section \"## $version\"" >&2
104
+ exit 1
105
+ fi
106
+ ls dist
107
+ if gh release view "$TAG" > /dev/null 2>&1; then
108
+ gh release upload "$TAG" dist/* --clobber
109
+ else
110
+ gh release create "$TAG" --verify-tag --title "tenslib-python $version" --notes-file "$RUNNER_TEMP/notes.md" dist/*
111
+ fi
112
+ pypi:
113
+ needs: [release]
114
+ runs-on: ubuntu-latest
115
+ timeout-minutes: 10
116
+ environment: pypi
117
+ permissions:
118
+ id-token: write
119
+ steps:
120
+ - uses: actions/download-artifact@v4
121
+ with:
122
+ path: dist
123
+ merge-multiple: true
124
+ - uses: pypa/gh-action-pypi-publish@release/v1
125
+ with:
126
+ packages-dir: dist
@@ -0,0 +1,8 @@
1
+ /target/
2
+ __pycache__/
3
+ *.so
4
+ *.pyd
5
+ /dist/
6
+ /.venv/
7
+ *.egg-info/
8
+ /.cargo/
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ ## 0.0.1
4
+
5
+ The first public release: an early one, with the tokenizers.
6
+
7
+ - `tenslib.tokenizer.Tokenizer`: a language model's tokenizer from a GGUF file or a Hugging Face directory (tenstok
8
+ 0.3): `open`, `encode`, `decode`, `decode_bytes`, `piece`, `token_id`, `token_text`, the special tokens and the
9
+ tokens that end a reply, `warnings`.
10
+ - `StreamDecoder`: the text of a reply as its tokens arrive.
11
+ - `TokenizerError`; a file that cannot be read is an `OSError`.
@@ -0,0 +1,134 @@
1
+ # Commenting standard
2
+
3
+ How tenslib-python's code is commented: the Rust of the compiled module (`src/`) and the Python of the package
4
+ (`python/tenslib/`, `tests/`, `tools/`).
5
+
6
+ The people who maintain this code may never have read PyO3's guide, llama.cpp's tokenizers or Hugging Face's
7
+ tokenizers library.
8
+ Someone who knows Rust should be able to understand the code from its comments: what each piece is for, why it is
9
+ shaped the way it is, and which outside code or documents it follows. The compiler checks some of these rules (see "Lints"
10
+ below). The rest is up to review.
11
+
12
+ ## 1. Every file starts with a module header
13
+
14
+ Use a `//!` block at the top of each file:
15
+
16
+ ```rust
17
+ //! # The GPT-2 byte-level table
18
+ //!
19
+ //! Byte-level BPE tokenisers work on bytes, not characters, so that any text can be tokenised without an
20
+ //! "unknown" token. But their vocabularies are stored as text. To make every byte printable, GPT-2 maps each of
21
+ //! the 256 byte values to one Unicode character:
22
+ //!
23
+ //! ```text
24
+ //! bytes 0x21..=0x7E, 0xA1..=0xAC, 0xAE..=0xFF -> the character with the same number
25
+ //! the other 68 bytes (controls, space, ...) -> U+0100, U+0101, ... in byte order
26
+ //! ```
27
+ //!
28
+ //! ## Not done here
29
+ //!
30
+ //! - Merging the bytes into tokens: `gguf` and `hf` do that.
31
+ ```
32
+
33
+ The header says:
34
+
35
+ 1. What the module is for, in plain English.
36
+ 2. Where it sits in the flow. Use a small ASCII diagram for the larger modules.
37
+ 3. What it deliberately does **not** do, so readers don't go looking for it here.
38
+
39
+ ## 2. Every public item has a doc comment
40
+
41
+ Use `///` doc comments that cover:
42
+
43
+ - **Purpose**: one sentence first; `cargo doc` uses it as the summary.
44
+ - **Units and invariants**: bytes, characters or tokens; whether an offset is into the text as given or as
45
+ normalised; whether text is UTF-8 or may end inside a character; what has already been checked.
46
+ - **`# Errors`** for every function that returns `Result`: which errors and when.
47
+ - **`# Panics`** if the function can panic, and why that can only happen through a programming error.
48
+ - **`# Examples`** where a short example makes the use clearer than prose.
49
+
50
+ Doc comments describe the tokenizers for the library's users. Where the code follows another project's
51
+ source, that goes in a plain comment (rule 3).
52
+
53
+ Private items get comments when their purpose is not obvious from their name and the module header.
54
+
55
+ ## 3. Upstream citations name the source and its commit
56
+
57
+ When code follows upstream code, name it, pinned to a commit, in a plain `//` comment beside the code, so a
58
+ maintainer can open the same lines:
59
+
60
+ ```rust
61
+ /// A token's kind, as a GGUF file's `tokenizer.ggml.token_type` array gives it.
62
+ // UPSTREAM llama.cpp f6d944f6d5d3 include/llama.h enum llama_token_type.
63
+ pub enum GgufTokenType { Normal, Unknown, Control, ... }
64
+ ```
65
+
66
+ What a warning is about is not cited: it is a `Rule` in `src/rules.rs`, with a name and a sentence, and the
67
+ warning carries its name (`rule: Some(rules::GGUF_PRE.id)`).
68
+
69
+ The tables in `src/generated/` are not written by hand; their headers say what they are made from.
70
+
71
+ State the behaviour itself in the doc comment as well: the reader should not need the upstream source to know what
72
+ the code does. Where something is derived rather than stated upstream, say so. NOTICE.md credits what is followed.
73
+
74
+ ## 4. Our own decisions are marked
75
+
76
+ When we choose between reasonable options, and no upstream source makes the choice, write it down:
77
+
78
+ ```rust
79
+ // DECISION: a vocabulary without a token for some byte is loaded, with a warning, and such a byte becomes the
80
+ // unknown token. The engine refuses the file; transformers loads it, and files like this are on the Hub.
81
+ ```
82
+
83
+ Give the reason and the alternative we rejected. A later maintainer can then revisit the decision knowing what was
84
+ weighed.
85
+
86
+ ## Python
87
+
88
+ The same rules hold for the Python files, with Python's means: a docstring at the top of each module saying what it
89
+ is for, a docstring for each public class and function (the compiled module's are in its Rust doc comments, which
90
+ Python shows as the docstrings, and its types in `python/tenslib/_native.pyi`), and `#` comments for the why. What a
91
+ call does is the Rust library's, so the Python side states it once, in the words the Rust side uses.
92
+
93
+ ## 5. No `unsafe`
94
+
95
+ The crate forbids `unsafe` code (`unsafe_code = "forbid"` in `Cargo.toml`). A change that seems to need it is a
96
+ reason to talk first.
97
+
98
+ ## 6. Tone
99
+
100
+ - Write in full sentences, for a reader who is new to the subject.
101
+ - Explain *why*. Don't repeat *what* the next line of code already says.
102
+ - Define a term the first time a file uses it (for example, "a *merge* is a pair of tokens that becomes one
103
+ token").
104
+ - Keep comments true. When you change the code, change the comment in the same commit.
105
+
106
+ ## 7. ASCII only
107
+
108
+ Comments, documentation and Markdown files hold ASCII characters only.
109
+
110
+ - A diagram is drawn with `-`, `|`, `+`, `>` and `v`; an arrow is `->`.
111
+ - A character the code handles is written as Rust writes it in a literal, `\u{2581}`, or by its code point, U+2581.
112
+
113
+ Why: a character that looks like an ASCII one and is not (a minus sign, a curly quote, a non-breaking space) cannot be
114
+ told from it by eye, cannot be typed or searched for by the next maintainer, and makes code hosts warn that the file
115
+ "contains ambiguous Unicode characters".
116
+
117
+ Not covered: string literals and test data, which hold what the program reads and writes, and text a licence requires
118
+ word for word.
119
+
120
+ `make check` enforces it (`ascii`): a non-ASCII byte after a comment marker on a line of source, or anywhere in a
121
+ Markdown file, fails.
122
+
123
+ ## 8. Lints
124
+
125
+ `Cargo.toml` sets these, and `make check` enforces them:
126
+
127
+ | lint | level | why |
128
+ |---|---|---|
129
+ | `missing_docs` | deny | every public item is documented |
130
+ | `unsafe_code` | forbid | rule 5 |
131
+ | `clippy::missing_errors_doc` | warn | rule 2, `# Errors` |
132
+ | `clippy::missing_panics_doc` | warn | rule 2, `# Panics` |
133
+
134
+ `make check` turns warnings into errors, so in practice all of them must be clean before a commit.