This release is a pre-release and may not be stable for production use.
KlongPy 2: one array language from GPU to FPGA to microcontroller
KlongPy is an implementation of Klong, an array language. You write +/a for the sum of an array instead of a loop. It is for array and time-series work, for people coming from NumPy or kdb+/q, for gradient-based work, and for small devices.
KlongPy 2 runs Klong on one interpreter, written in Rust. Two commands start it:
| Command | What it is |
|---|---|
kg |
A standalone native program with column tables, embedded DuckDB and optional Metal compute on macOS. |
kgpy |
The same engine hosted in Python, with NumPy and PyTorch arrays, pandas and Arrow interchange, Python callables, web services and FPGA card bindings. |
The same interpreter is in firmware for microcontroller boards, and the tools lint, check, compile c, fpga source and ast run from either command. What Runs Where says which part does what.
Full documentation: https://www.klongpy.org/. New here? Read Start Here.
Where it runs
| Target | Status | More |
|---|---|---|
CPU (kg, and kgpy with NumPy) |
stable; passes the conformance suite | Start Here |
| GPU (Python backend and autograd) | The opt-in GPU acceptance tests have run on Apple MPS. The standalone Metal backend also passes the native MPS cases. | GPU acceptance tests · PyTorch Backend and Autograd |
| Compile to C (static profile) | works for a subset of the language; compiled C has run with desktop compilers and, for three examples, on two Raspberry Pi Picos | Compiling Klong to C |
| Microcontrollers (the interpreter in firmware) | experimental; two RP2040 Picos passed the RC1 paired IPC self-test. Earlier firmware ran console tests on an ESP32-P4 | Native Runtime status |
| FPGA kernels | experimental; run on one Alveo U250 | Klong functions as FPGA hardware |
For 2.0.0rc1, the conformance suite passed 2,220 cases under standalone kg and 2,199 applicable cases under Python-hosted kgpy. The serial console simulator passed 1,716 selected cases. Physical boards have separate test results. The C and FPGA compilers support the static subset of Klong.
Install
For standalone kg, run this from the repository root with Rust installed and Cargo's binary directory on your PATH.
cargo install --locked --path native/klong-cli
kg -e '+/!100' # 4950
Type kg for the REPL or kg trades.kg to run a script. Tools and coding agents can read kg --capabilities as JSON. Firmware builders can select a board and its features separately. Native build guide
For the Python distribution, choose the packages you use.
pip install klongpy # the kgpy and kg commands (NumPy arrays)
pip install "klongpy[torch]" # the torch backend: GPU arrays and gradients
pip install "klongpy[db]" # pandas, Python DuckDB, legacy key-value store
pip install "klongpy[web]" # the .web HTTP server (aiohttp)
pip install "klongpy[ws]" # WebSockets
pip install "klongpy[all]" # all of the above
KlongPy 2 needs Python 3.11 to 3.14. The REPL needs no extra: kgpy or kg starts it. kgpy --backend torch selects the torch backend.
A first program
An expression is read from right to left. !5 is the list 0 to 4, and +/ sums a list. avg(v)::{...} defines a function with a named parameter. Type kgpy and then:
?> +/1+!5
15
?> avg(v)::{(+/v)%#v}
:monad
?> avg(1+!5)
3.0
A gradient is an operator: f:>x is the gradient of f at x. It is autograd on the torch backend and numeric (finite differences) on NumPy, which is what this run uses. Gradient descent on x^2 from 5, 100 steps of size 0.1:
?> f::{x^2}
:monad
?> f:>3
6.000000000838668
?> s::5.0
5.0
?> {s::s-(0.1*f:>s)}'!100;s
1.0185179880729389e-09
The other spellings of the gradient and the Unicode operators are in PyTorch Backend and Autograd.
What is new in 2
Version 2 brings one Rust engine to both commands, built-in tables and DuckDB, kdb+-protocol IPC, and conformance tests across targets. It adds modules, destructuring, named parameters, strict modes, diagnostic tools, compilation to C and experimental FPGA kernels. What's New in v2 is the tour, and Migrating to v2 lists what changes for existing code.
Staying on 0.7
Version 2 changes how some existing code behaves, and there is no pure-Python version of it. To stay on the pure-Python 0.7 line:
pip install "klongpy<2"
The documentation of each release line stays online: pick the version in the header of klongpy.org. The README shown on a release's PyPI page, and at its tag in this repository, is the README of that release.
Security model: trusted networks
KlongPy's network features assume every peer is trusted. Use them on a trusted network only.
- IPC (
.srv,.cli): a connected peer can run arbitrary Klong, and through it arbitrary Python (.py,.pyf) and shell commands (.sys). It can also call any function you have defined and assign any variable. - IPC wire format: the kdb+ IPC protocol, and nothing from the network is unpickled. Message size is limited (
.systemkeyipc_max_message, 256 MiB by default). The pickle protocol of 0.7 was removed in 2.0; a 0.7 peer cannot connect. - No encryption, optional login: IPC credentials (
"host:port:user:password") and all data are sent in clear text and there is no TLS. A server accepts every client unless the program defines.srv.auth..webhas no login or access control. - Bind address:
.srv(8888)and.web(8888;...)listen on all interfaces (0.0.0.0). Anyone who can reach the port has the access above..webroutes run the Klong functions you register with the request parameters. - KVS and tables: keys are used as file names under the store root without checking that they stay inside it, and values are pickled on disk. Treat the root directory as trusted storage and do not load stores you did not write.
.db: DuckDB runs the string you pass as SQL, as given.
To limit exposure:
- Bind to localhost:
.srv("127.0.0.1:8888"),.web("127.0.0.1:8080";get;post).kgpy -s ARGevaluates.srv(ARG), so to bind to localhost pass a quoted string:kgpy -s '"127.0.0.1:8888"'. - Firewall the ports, and never expose a KlongPy port to an untrusted network or the internet.
- Between hosts, use an SSH tunnel or VPN.
See IPC Capabilities for the protocol and the credentials hook.
Lineage
- APL (1966): Ken Iverson's notation.
- J: an ASCII-friendly successor of APL.
- K/Q/kdb+: time-series and trading systems.
- Klong: Nils M Holm's array language, described in the Klong Book. Thanks to him for creating it.
- NumPy and PyTorch: the array backends.
Related projects: Klupyter (KlongPy in Jupyter), VS Code syntax highlighting and Advent of Code solutions.
Contributing
This project does not accept direct issues. Start with a GitHub Discussion; maintainers promote validated discussions to issues. See CONTRIBUTING.md, and docs/contributing.md to build and test.
Licence
MIT. See LICENSE.
Metadata
Release files for klongpy 2.0.0rc1
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 |
|---|---|---|---|---|
| klongpy-2.0.0rc1.tar.gz | 2.3 MB | Without provenance | Details for klongpy-2.0.0rc1.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_x86_64.whl | CPython 3.11 | abi3 | Linux glibc 2.28+ x86-64 | Details for klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_x86_64.whl |
| klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_aarch64.whl | CPython 3.11 | abi3 | Linux glibc 2.28+ ARM64 | Details for klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_aarch64.whl |
| klongpy-2.0.0rc1-cp311-abi3-macosx_11_0_arm64.whl | CPython 3.11 | abi3 | macOS 11.0+ ARM64 | Details for klongpy-2.0.0rc1-cp311-abi3-macosx_11_0_arm64.whl |
Total release size: 115.1 MB
Release files / klongpy-2.0.0rc1.tar.gz
| Download URL | klongpy-2.0.0rc1.tar.gz |
|---|---|
| Size | 2.3 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a8fed9a33fd7199a95b75a827efd2f226201ea8993bbc5b0b236547f9d258c54
|
|
BLAKE2b-256 checksum How to use checksums |
1ca2b287e996ee27318a7a37598eab0d79fc25fefdd9137b6bb34d71bdba10cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.11
|
Release files / klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_x86_64.whl
| Download URL | klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 42.1 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
dcdd2bda3743ac7593ac367119bcbf5f23451742bbf6917a50bade3c9b135969
|
|
BLAKE2b-256 checksum How to use checksums |
768c44bdd8e24a112c1eb5032b9c1521e55c660cc5c8e25c1e5bbe5c11728151
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.11
|
Release files / klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_aarch64.whl
| Download URL | klongpy-2.0.0rc1-cp311-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 38.0 MB |
| Tags | CPython 3.11 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
9c47174166e61314c118f9bc1b903212bf0a42d8f3861e956c0af069f55b559f
|
|
BLAKE2b-256 checksum How to use checksums |
8cfd6297fa3cd08ba6c2b9d364b0363d82b323422a45be05f38481f33a4777a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.11
|
Release files / klongpy-2.0.0rc1-cp311-abi3-macosx_11_0_arm64.whl
| Download URL | klongpy-2.0.0rc1-cp311-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 32.8 MB |
| Tags | CPython 3.11 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
9f6c5d0706ffaa77f4ab446a95893532ae4b124bc87088b5ea9e21993a8ec4c7
|
|
BLAKE2b-256 checksum How to use checksums |
430399769ba2225e1ff8179d7dbf609ce650fa1f16a05af89aa4aeb730c5317e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.11
|
Security for klongpy 2.0.0rc1
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.