Skip to main content

GPT-2 transformer for symbolic music generation

Project description

MIDI-GPT

A GPT-2 transformer for symbolic music generation — tokenizes MIDI, generates new bars or in-fills missing ones, and decodes back to MIDI.

Paper: AAAI 2025 — MIDI-GPT: A Controllable Generative Model for Computer-Assisted Multitrack Music Composition

This release ships the inference stack as a clean, self-contained Python package:

  • C++ tokenizer / encoder / decoder (built via pybind11 + scikit-build-core)
  • Pure-PyTorch GPT-2 with F.scaled_dot_product_attention and KV cache
  • Self-contained packed checkpoint format (weights + arch config + encoder config in one file)
  • CPU, MPS (Apple Silicon), and CUDA devices

The real-time OSC server and Max MSP integration ship in a separate release.


Install

Requires Python ≥ 3.10, CMake ≥ 3.21, a C++20 compiler, and PyTorch ≥ 2.0.

git clone https://github.com/DaoTwenty/MIDI-GPT.git
cd MIDI-GPT
python -m venv .venv
source .venv/bin/activate
pip install -e ".[inference]"

macOS dependencies:

brew install cmake

Extras:

extra adds
inference torch>=2.0
train transformers, datasets, accelerate, pyarrow
dev pytest, ruff, mypy
all everything

Download a model

Pretrained weights are distributed as packed .pt files (arch config + encoder config + state dict in one archive).

Model Encoder Size Download
Yellow EL_VELOCITY_DURATION_POLYPHONY_YELLOW_ENCODER ~80 MB yellow.pt

Drop it in models/:

mkdir -p models
curl -L https://github.com/DaoTwenty/MIDI-GPT/releases/latest/download/yellow.pt \
     -o models/yellow.pt

Quick start

from midigpt.inference.model import GPT2LMHeadModel
from midigpt.inference.engine import InferenceEngine
from midigpt.inference.config import GenerationRequest, TrackPrompt, SamplingConfig
from midigpt.tokenizer.tokenizer import Tokenizer
from midigpt.attributes import AttributeAnalyzer
from midigpt import _core
import json

# Load the packed checkpoint — auto-detects device (cuda > mps > cpu)
model = GPT2LMHeadModel.from_pretrained("models/yellow.pt", device="auto")

# The encoder config travels inside the checkpoint
cfg       = _core.EncoderConfig.from_json(json.dumps(model.encoder_config))
analyzer  = AttributeAnalyzer.from_config(cfg)
tokenizer = Tokenizer(cfg, analyzer)
engine    = InferenceEngine(model=model, tokenizer=tokenizer, analyzer=analyzer)

# Build a request: tracks to keep as prompt, bars to fill, sampling controls
request = GenerationRequest(
    midi_path="path/to/input.mid",
    tracks=[TrackPrompt(index=0)],
    bars_to_generate=[3, 4],
    sampling=SamplingConfig(temperature=1.0, max_attempts=1),
)

result = engine.generate(request)
result.save("output.mid")

Architecture

┌──────────────────────────────────────────────────────────────┐
│  midigpt (Python package)                                    │
│                                                              │
│   tokenizer/    →  encode MIDI ↔ token IDs   (C++ ext)       │
│   attributes/   →  density / polyphony / duration controls   │
│   inference/                                                 │
│     model.py    →  GPT2LMHeadModel (SDPA + KV cache)         │
│     engine.py   →  high-level generate()                     │
│     session.py  →  multi-step planning + sampling            │
│   augmentation/ →  transpose / velocity / track swap …       │
│   training/     →  optional HF-trainer integration           │
└──────────────────────────────────────────────────────────────┘

Packed checkpoint format (format_version: 1)

{
    "format_version": 1,
    "config":         {"vocab_size": ..., "n_positions": ..., "n_embd": ..., "n_layer": ..., "n_head": ...},
    "encoder_config": {...},   # full encoder JSON
    "state_dict":     {...},   # HF GPT-2 layout
}

GPT2LMHeadModel.from_pretrained(path) auto-detects this format. Everything needed to tokenize and run is inside the file — no sidecar JSON.


Tests

pytest tests/python/                       # Python unit tests
ctest --test-dir build_cpp                 # C++ unit tests (after a build)

License

See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

midigpt-0.2.1.tar.gz (272.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

midigpt-0.2.1-cp312-cp312-win_amd64.whl (903.5 kB view details)

Uploaded CPython 3.12Windows x86-64

midigpt-0.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (664.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

midigpt-0.2.1-cp312-cp312-macosx_11_0_arm64.whl (489.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

midigpt-0.2.1-cp312-cp312-macosx_10_15_x86_64.whl (552.6 kB view details)

Uploaded CPython 3.12macOS 10.15+ x86-64

midigpt-0.2.1-cp311-cp311-win_amd64.whl (615.9 kB view details)

Uploaded CPython 3.11Windows x86-64

midigpt-0.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (662.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

midigpt-0.2.1-cp311-cp311-macosx_11_0_arm64.whl (489.9 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

midigpt-0.2.1-cp311-cp311-macosx_10_15_x86_64.whl (551.1 kB view details)

Uploaded CPython 3.11macOS 10.15+ x86-64

midigpt-0.2.1-cp310-cp310-win_amd64.whl (329.6 kB view details)

Uploaded CPython 3.10Windows x86-64

midigpt-0.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (661.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

midigpt-0.2.1-cp310-cp310-macosx_11_0_arm64.whl (488.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

midigpt-0.2.1-cp310-cp310-macosx_10_15_x86_64.whl (549.7 kB view details)

Uploaded CPython 3.10macOS 10.15+ x86-64

File details

Details for the file midigpt-0.2.1.tar.gz.

File metadata

  • Download URL: midigpt-0.2.1.tar.gz
  • Upload date:
  • Size: 272.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for midigpt-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7ee12172f3a386a0b9a05f4fed7fd81e0e388dd73dbd52dba1211cb5691b426c
MD5 9fea65ce808e018ab3e149df4229084a
BLAKE2b-256 8f5e8ea4f234b9ee1b6be793e5f28e13918f91a90196598e5d1659f412cb290f

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: midigpt-0.2.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 903.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for midigpt-0.2.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 dd39292d49e2e6199c387c317f58b1211c60780e28861689b14f0547f906ece4
MD5 a6d0308012e4f7bb5c69a46a528ead32
BLAKE2b-256 8a3505762f5c17ccb941298e5b6fab9e1abbb97bf9898320cb6cd270b89be800

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 964110bf831e388ae38beb3e22edb3e21aac405744fa122287e1b76cc07fd76b
MD5 2bf786084ffeb66fc9f95d0525360f89
BLAKE2b-256 76ea6ecdf2a9b44ca510e913b984077e8cb788b7a90cad58b91de1516119e212

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 402982d33a5b5185d99b62564f961e1e994ee0622a6b214462d6585f48cd6d75
MD5 70e13bb5a873cdafef58c613f656d2c3
BLAKE2b-256 71e82d52a57691a547da3f19e179bb8315c7ed590200ebe5f5a4ac9675e95565

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp312-cp312-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp312-cp312-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 9f5d5863d81b26cb54a6950db916ce55b6ec9694d08f8b130aab0c94cd8256d2
MD5 420e523037c8a9fca96b206e1901830a
BLAKE2b-256 240dc88317be88702bedcc6fd014b767cfc6dda37bc15637af8e5318ce125b42

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: midigpt-0.2.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 615.9 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for midigpt-0.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 fb1933403b7193575fc81602ff5aba175842f851b59992440e90cbaa7b5ce054
MD5 0f2b16c62da4409ec9b1826ea5f712b2
BLAKE2b-256 07a4419a7895a95bdb9a90f49c779e15f5d53272d3422e87dbcfc943219d111f

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2d8dbe1d4edbea61563562e60c03cafa21377c1439a6526191a149efc69a56b8
MD5 872ed803eed259e1e5425112126fa219
BLAKE2b-256 b1c2f131f9edabce784b9e29c773afb4f6fac2ffaed63d52af96adc033a37bc6

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 42cdcc1400ecff59bd8b71e169423cc9fa3717b49baab44f13857d9c832b3d2e
MD5 95f1a1651c73a95e5aa6297b041801c4
BLAKE2b-256 422977de67099fb50bc68272cb27a04ae50c13808aa6fb7e425df8484f90a36a

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp311-cp311-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 4d51ab0be656dd06b30fa7d47726a9ccf63f8a12764252a6702bdafe9dcc55ba
MD5 d60befd0a90c62864a5db0ce2f8638e0
BLAKE2b-256 1c2d08dc92d8c62853532e36cd47001193dd1c86ba5c588ea09ee9d09b3130ef

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: midigpt-0.2.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 329.6 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for midigpt-0.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6c1da7fa2a227e466f2d3cd47a72bab241831a2493fc2366ab2d5791340c4304
MD5 e32f96820d045b998b2b79c3e397e491
BLAKE2b-256 9e29528c7207e0e2455b5347f6db07900bd794e90dc388a5820e64c2ab6f11f7

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dedb0698709b020362f2a8c817349b62fd119589bcc48ba694a0ebe183b35a67
MD5 2f7aea3fb2f0bc29723df5fe6136cce7
BLAKE2b-256 708088599613a234bb19f91d50cb37268f6c18ad19517b986b1469cb442eed4b

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 19fa68f3b0e49e0de398988004573bacb05ffdfea25d455f4db2f2e3f8d64d51
MD5 31405d49157e8ad1a24252f0874a5485
BLAKE2b-256 522cea2d1f0d61f649f8aec643c1e27d03e6c52ad03347762b2dc6271ceab271

See more details on using hashes here.

File details

Details for the file midigpt-0.2.1-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for midigpt-0.2.1-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 a34943dcb8e707b4979ca54319aae28644005c3e38f2147f95405d623d530d01
MD5 d5adb90373dc56cabf5da9fe6941b52b
BLAKE2b-256 875f423eeecdfa7d2bb02bbe9ea991d9f2332bf54add21e763ec7c22b7f72716

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page