MxlTok
MxlTok tokenizes MusicXML scores into LMX (Linearized MusicXML) token sequences for language models, and decodes token sequences back into MusicXML.
This is the official tokenizer of the ISMIR 2026 paper Does Notation Matter? MusicXML Tokenisation for Symbolic Music Generation and Understanding (Poltronieri, Spanio, Chieppa, Serra and Rocamora). The paper introduces this tokenisation framework for MusicXML and compares it with MIDI- and ABC-based representations across 26 autoregressive and masked language models, on music understanding (composer classification, difficulty estimation, accompaniment suggestion) and generation (objective metrics and a human preference study). MusicXML-tokenised models outperform their MIDI and text-based counterparts on both. If you use MxlTok, please cite the paper.
<BOS> num_parts:1 part:instrument:Piano part:start part:instrument:Piano
measure key:fifths:-3 time:num:4 time:den:4 clef:G2 dynamics:p
E5b voice:1 whole
measure E5b voice:1 eighth stem:down beam:begin F5 eighth G5 eighth beam:end ...
part:end <EOS>
- Faithful: notes, rests, chords, grace notes, voices, staves, ties, slurs, beams, tuplets, articulations, ornaments, dynamics, hairpins, pedals, tempo, chord symbols and repeats.
- Readable: every token is a plain word (
C4#,quarter,stem:up). - Ready for training: special tokens, BPE with 🤗
tokenizers, and tokenizers saved as a single JSON file or on the Hugging Face Hub. - Light: one dependency (
tokenizers); MusicXML is parsed with the standard library.
Installation
pip install mxltok # or: uv add mxltok
pip install "mxltok[hub]" # to load and push tokenizers on the Hugging Face Hub
MxlTok requires Python 3.10 or later.
Quick start
from mxltok import MXLTokenizer, write_musicxml
tokenizer = MXLTokenizer()
sequence = tokenizer.encode("score.mxl") # .mxl, .musicxml or .xml
sequence.tokens # ['<BOS>', 'num_parts:1', 'part:instrument:Piano', ...]
sequence.ids # [1, 10, 26, ...]
score = tokenizer.decode(sequence.ids) # ids, tokens or a TokSequence
write_musicxml(score, "decoded.musicxml") # a .mxl path is compressed
decode accepts lists, NumPy arrays and PyTorch tensors. Sequences generated
by a model need not be well formed: malformed notes are skipped with a
warning, or raise MxlTokError with decode(ids, strict=True).
Configuration
from mxltok import MXLTokenizer, TokenizerConfig
tokenizer = MXLTokenizer(
TokenizerConfig(
arrangement="measure_first", # or "part_first" (default)
pitch_mode="absolute", # or "interval"
include_grace_notes=True,
include_rests=True,
prepend_instruments=True, # num_parts:N and instrument header
add_bos_eos=True,
max_parts=0, # 0 keeps every part
excluded_categories={"stem"}, # drop whole token categories
token_order=None, # reorder tokens within each note
)
)
| Option | Default | Effect |
|---|---|---|
arrangement |
"part_first" |
part_first writes each part in turn; measure_first writes each measure with all its parts |
pitch_mode |
"absolute" |
interval writes pitches as semitones from the previous one (interval:-3) |
include_rests, include_grace_notes |
True |
encode rests and grace notes |
prepend_instruments |
True |
start with num_parts:N and one part:instrument:* per part |
add_bos_eos |
True |
wrap the sequence in <BOS> … <EOS> |
max_parts |
0 |
encode only the first parts (0: all) |
excluded_categories |
set() |
remove token categories, such as {"stem", "beam"} (see TokenCategory) |
token_order |
None |
sort the tokens of each note by category priority; {} uses the default order |
special_tokens |
<PAD> <BOS> <EOS> <UNK> <MASK> |
the special tokens, first in the vocabulary |
The token format is specified in docs/format.md.
BPE
Byte-pair encoding merges frequent token successions into new ids, which
shortens sequences. Train it on the token sequences of a corpus; encode then
returns BPE ids and decode accepts them:
from pathlib import Path
files = sorted(Path("scores").rglob("*.mxl"))
tokenizer.train_bpe((tokenizer.encode(f).tokens for f in files), vocab_size=10_000)
tokenizer.vocab_size # 10000: the size of the model's embedding table
tokenizer.encode(files[0]).ids
Saving and loading
tokenizer.save_pretrained("my_tokenizer") # writes my_tokenizer/tokenizer.json
tokenizer = MXLTokenizer.from_pretrained("my_tokenizer")
tokenizer.push_to_hub("user/mxltok-bpe10k") # requires mxltok[hub]
tokenizer = MXLTokenizer.from_pretrained("user/mxltok-bpe10k")
from_pretrained also loads the tokenizers saved by mxlGPT, from a
tokenizer.json or from a preprocessed dataset directory (manifest.json
with a tokenizer/ folder), BPE included. MxlTok produces exactly the tokens
and ids of mxlGPT, so models trained with it keep working.
Scores
read_musicxml parses a file into a Score, a typed model of the MusicXML
that LMX encodes (parts, measures, notes, attributes, directions...), and
write_musicxml writes one. Scores can be edited before encoding, for
instance to transpose them for data augmentation:
from mxltok import read_musicxml, transpose
score = read_musicxml("score.mxl")
sequences = [tokenizer.encode(transpose(score, shift)) for shift in range(-3, 4)]
Development
The project uses uv:
uv sync # create the environment
uv run pytest # run the tests
uv run ruff check # lint
uv run ruff format # format
uv run mypy # type-check
uv build # build the wheel and the source distribution
The tests compare MxlTok with reference outputs of mxlGPT's tokenizer, stored
under tests/fixtures. scripts/mxlgpt_parity.py regenerates them and
compares both tokenizers on any corpus of MusicXML files.
Citation
@inproceedings{poltronieri2026notation,
title = {Does Notation Matter? {MusicXML} Tokenisation for Symbolic Music
Generation and Understanding},
author = {Poltronieri, Andrea and Spanio, Matteo and Chieppa, Simone and
Serra, Xavier and Rocamora, Mart{\'i}n},
booktitle = {Proceedings of the 27th International Society for Music
Information Retrieval Conference (ISMIR)},
year = {2026}
}
License
MIT
Metadata
Release files for mxltok 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mxltok-0.1.0.tar.gz | 598.3 kB | Without provenance | Details for mxltok-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mxltok-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for mxltok-0.1.0-py3-none-any.whl |
Total release size: 645.3 kB
Release files / mxltok-0.1.0.tar.gz
| Download URL | mxltok-0.1.0.tar.gz |
|---|---|
| Size | 598.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3ac877771687b5c1fe0c8a1b952c8cef590cdd6ab2e7c07f6b8951d11fdd1c41
|
|
BLAKE2b-256 checksum How to use checksums |
91c341595de68c0922c879241849daf1c60f81641670d936254f34d31862cae1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / mxltok-0.1.0-py3-none-any.whl
| Download URL | mxltok-0.1.0-py3-none-any.whl |
|---|---|
| Size | 47.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
90e5cfdcb79efe6c85cbf26d03ccd5c08c6f504995295fafb5a08c05c952ff26
|
|
BLAKE2b-256 checksum How to use checksums |
cbdffbe0f8fb886d4027638bfbaa381fe25cedb0ee233b18e6365f49d674b7eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for mxltok 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.