A blue-crested bird with half-lidded eyes and a bright orange bill. # BlyxTorch **Implementation language: [Blyx](https://www.blyx-lang.space/).** Train a convolution on a signal, fit a causal transformer to a sequence, or update sparse embeddings by row ID. BlyxTorch's CPU f64 layer implementations, gradients and training examples are written in Blyx. The original dense-network generator is also available for standalone classifiers and regressors. **Version 0.3.0** adds native CPU layers. Run `blyxtorch layer-demo` to train a shared filter and a transformer block. The sequence example trains on 64 synthetic sensor streams and measures its prediction error on 16 held-out streams. [Download the releases](https://github.com/Rahulchaube1/blyxtorch/releases) · [Layer guide](LAYERS.md) · [Sequence example](sequence_example.blyx) · [Browser example for the portable dense API](https://www.blyx-lang.space/play?example=package-blyxtorch) ## Use BlyxTorch in your editor Install **BlyxTorch** (publisher **RahulChaube**) or download the VSIX from the release. The extension bundles the three native trainers, two editable Blyx projects and a walkthrough. To run the first project, create `sensor.blyx`, type `bt-sensor`, accept the completion and choose **Blyx: Run File**. [Editor extension and downloads](https://www.blyx-lang.space/packages/blyxtorch#editor) | Project | What you can run | | --- | --- | | [Desk sensor calibration](projects/sensor-calibration.blyx) | Train a gain and offset using BlyxTorch automatic differentiation and predict two held-out readings. [Run in the browser](https://www.blyx-lang.space/play?example=blyxtorch-sensor) | | [Print-queue classifier](projects/queue-classifier.blyx) | Train a dense network on 16 synthetic print jobs, evaluate six held-out jobs and predict wait categories for four new jobs. [Run in the browser](https://www.blyx-lang.space/play?example=blyxtorch-queue) | The [project workspace](https://github.com/Rahulchaube1/blyxtorch/releases/download/v0.2.0/blyxtorch-projects-0.2.0.zip) includes the CSV files, trained weights and native editor tasks. [Project instructions](projects/README.md) ## Install with a package manager On Apple Silicon macOS or x86-64 Linux, install the tested Homebrew formula. ```sh brew install rahulchaube1/blyx/blyxtorch ``` On Windows x64, add the project bucket and install the command from PowerShell. ```powershell scoop bucket add blyx https://github.com/Rahulchaube1/scoop-blyx scoop install blyx/blyxtorch ``` Run `blyxtorch --help` to see the default network's input schema. With a package-manager install the command is on PATH. The examples below use the executable and fixtures supplied in the release archive. [Homebrew formula](https://github.com/Rahulchaube1/homebrew-blyx/blob/main/Formula/blyxtorch.rb) · [Scoop manifest](https://github.com/Rahulchaube1/scoop-blyx/blob/main/bucket/blyxtorch.json) ## Train your first network Extract the release archive for your machine. These commands train a two-input network on the four XOR examples, save the weights and load them in a separate process for prediction. ```sh ./blyxtorch train fixtures/xor.csv 250 0.03 4 adam 7 > xor.bt ./blyxtorch predict xor.bt fixtures/xor-features.csv ./blyxtorch score xor.bt fixtures/xor.csv ``` In Windows PowerShell, use ASCII output for the checkpoint so it also works with Windows PowerShell 5.1. ```powershell .\blyxtorch.exe train fixtures/xor.csv 250 0.03 4 adam 7 | Out-File -Encoding ascii xor.bt .\blyxtorch.exe predict xor.bt fixtures/xor-features.csv .\blyxtorch.exe score xor.bt fixtures/xor.csv ``` | Inputs | Target | Probability after training | | --- | ---: | ---: | | 0, 0 | 0 | 0.00365 | | 0, 1 | 1 | 0.99391 | | 1, 0 | 1 | 0.99308 | | 1, 1 | 0 | 0.00846 | The mean binary cross-entropy on these four training rows is approximately 0.00630. XOR demonstrates nonlinear learning and checkpoint round-tripping. Use separate held-out data to evaluate a model for a real application. ## Choose an architecture Supply the widths from input to output, one objective and one hidden activation. The example builds a network for three classes with two input features and two hidden layers. Install Blyx 0.2.1, Rust 1.85 or newer, and a host linker on the build machine. ```sh ./blyxtorch emit 2,8,4,3 softmax relu > classifier.blyx blyxc build classifier.blyx -o classifier ./classifier --help ``` In PowerShell, pipe `.\blyxtorch.exe emit 2,8,4,3 softmax relu` to `Out-File -Encoding ascii classifier.blyx`. Compile to `classifier.exe`. The generated file contains the full model and trainer. Once compiled, the executable runs separately from the generator, templates, Python and Rust. Keep the two supplied Blyx template files beside the generator when using `emit`. | Objective | Training columns after `x0,x1,...` | Prediction | | --- | --- | --- | | `mse` | One real-valued `y0,y1,...` per output | Raw output values. Loss averages squared error over outputs. | | `binary` | One `y0` from 0 through 1 | Probability from a single output logit. Training uses stable binary cross-entropy with logits. | | `softmax` | One integer `class` from 0 through output width minus one | A probability per class. Training uses log-sum-exp categorical cross-entropy. | Hidden activations are `tanh`, `relu` and `sigmoid`. A model with just input and output widths is an affine model. The `emit` command specializes up to eight affine layers, widths from 1 through 64 and up to 1,024 trainable parameters. The native layer API accepts runtime shapes. Its tests train a 41,728-parameter dense transformer block and exercise sparse embedding row IDs above 2⁵³. [Native layers and limits](LAYERS.md) ## Commands and data | Command | Result | | --- | --- | | `init [seed]` | Write deterministic initial weights to a checkpoint on standard output. | | `train DATA [epochs] [rate] [batch] [adam or sgd] [seed]` | Train from initialization. Defaults are 100 epochs, learning rate 0.03, batch size 32, Adam and seed 7. | | `train-score TRAIN.csv LABELLED.csv [epochs] [rate] [batch] [adam or sgd] [seed]` | Fit on the training rows and report mean loss on a separate file in one command. | | `train-predict TRAIN.csv FEATURES.csv [epochs] [rate] [batch] [adam or sgd] [seed]` | Fit and return a CSV of predictions for new feature rows. | | `predict CHECKPOINT FEATURES.csv` | Emit a CSV of predictions in input order. | | `score CHECKPOINT LABELLED.csv` | Return a JSON row count and mean loss. | | `gradient CHECKPOINT LABELLED.csv` | Return the loss and every parameter derivative separately for each observation. | Use UTF-8 unquoted numerical CSV. Feature columns are `x0,x1,...` in order, followed by the target columns for training, scoring and gradients. Prediction takes feature columns alone. Ask the compiled model for its exact header with `--help`. The trainer loads the file into memory and reuses its row order across epochs. A final short mini-batch divides by its actual size. SGD applies the mean mini-batch gradient. Adam uses first and second moment decay of 0.9 and 0.999, bias correction per update, and epsilon of 1e-8. Initial weights use a seeded symmetric fan-in/fan-out scale and zero biases. A checkpoint contains a model identity line and the weights in round-trippable decimal form. The loader checks the architecture, objective, activation, parameter count and finiteness. Checkpoints contain inference weights only. ## Use automatic differentiation from Blyx Import `blyxtorch.blyx` for portable forward-mode differentiation. A dual value contains a value and a tangent. Seed a parameter's tangent with 1 and a constant's with 0. Reuse variables freely in the expression. ```blyx use blyxtorch; fn main() { let w = bt_dual(1.0, 1.0); let x = bt_dual(2.0, 0.0); let target = bt_dual(5.0, 0.0); let loss = bt_square(bt_sub(bt_mul(w, x), target)); println!(at(loss, 0)); println!(at(loss, 1)); } ``` This prints 9 and −12. The library supplies addition, subtraction, multiplication, division, square, ReLU, tanh, sigmoid, softplus and binary cross-entropy with logits. Domain or shape errors return an empty vector. ReLU's chosen derivative at zero is zero. [Complete example](example.blyx) A generated model module uses reverse-mode differentiation for all trainable parameters in one backward pass. From a neighboring file, import it with `use classifier;` and call the functions below. | Function | Return | | --- | --- | | `bt_inputs(), bt_outputs(), bt_targets(), bt_parameters()` | Static dimensions | | `bt_init(seed)` | Initial parameter vector | | `bt_predict(weights, features)` | Prediction vector | | `bt_sample_loss(weights, features, target)` | One-element loss vector | | `bt_loss_grad(weights, features, target)` | Loss followed by all weight and bias derivatives | Within each layer, parameters are the output-by-input weight matrix in row-major order followed by the bias vector. Layers appear in forward order. Predictions and gradients return an empty vector for invalid input. The CLI exits with a diagnostic for invalid data. ## Verification and performance The independent oracle compares predictions, per-row losses, parameter derivatives and entire multi-epoch SGD and Adam updates against CPU PyTorch in float64. Five networks cover the three objectives, all hidden activations, two hidden layers and a partial final mini-batch. The verifier also checks selected derivatives by central differences. [Verification source](https://github.com/Rahulchaube1/blyxtorch-verification/blob/367b3d68ab6247a291c28149516dc7dfc295e322/pytorch_reference.py) · [Recorded results](verification/parity.json) · [CI runs](https://github.com/Rahulchaube1/blyxtorch/actions) On one Windows x64 CPU, scoring 4,096 CSV rows took 59.3 ms for a new BlyxTorch process, 3.52 s for a new Python process loading PyTorch, and 6.8 ms with PyTorch already loaded. These are medians of seven runs in rotating order. All three read the checkpoint and CSV, score the same small model and produce the same mean loss. The warm PyTorch path uses vectorized batches. GPU, larger models and training throughput need separate measurements. [Raw timings and environment](verification/score-benchmark.json) · [Benchmark harness](https://github.com/Rahulchaube1/blyxtorch-verification/blob/367b3d68ab6247a291c28149516dc7dfc295e322/bench_score.py) ## Build and contribute ```sh cargo install --locked --git https://github.com/Rahulchaube1/blyxxxx --rev 120d6b6ec6df09ff018d7c18752fb3297b5e629d blyxc blyxc build main.blyx -o blyxtorch blyxc native-run tests.blyx blyxc native-run example.blyx blyxc native-run sequence_example.blyx ``` On Windows compile with `-o blyxtorch.exe`. The shipped default module can be reproduced from the repository root with `blyxc native-run generate.blyx -- 2,4,1 binary tanh`. The [independent verification repository](https://github.com/Rahulchaube1/blyxtorch-verification) checks the installed binary, compares gradients and optimizer updates with CPU PyTorch, and records the scoring benchmark. Please include the model widths, input, expected output and command when reporting an issue. [Issues](https://github.com/Rahulchaube1/blyxtorch/issues) · [Blyx community](https://www.blyx-lang.space/community) BlyxTorch is an independent project by Rahul Chaube and contributors. It is distributed under your choice of the [MIT](LICENSE-MIT) or [Apache 2.0](LICENSE-APACHE) license. ## Connect with Rahul Chaube [Portfolio](https://www.rahulchaube1.com/) · [GitHub](https://github.com/Rahulchaube1) · [LinkedIn](https://www.linkedin.com/in/rahulchaube1) · [Hugging Face](https://huggingface.co/oncody) · [X](https://x.com/RahulChaube_) · [EverestQ](https://www.everestqai.com/) · [Contact](https://www.rahulchaube1.com/contact) [Blyx website](https://www.blyx-lang.space/) · [BlyxTorch](https://www.blyx-lang.space/packages/blyxtorch) · [Handbook](https://doc.blyx-lang.space/) · [Community](https://www.blyx-lang.space/community) Support Rahul’s open-source work through [GitHub Sponsors](https://github.com/sponsors/Rahulchaube1) or [Buy Me a Coffee](https://buymeacoffee.com/rahulchaube).