Use lmtorch from LMScript
lmtorch is a CPU tensor and neural network host for LMScript. It runs compiled lmcode blocks with native tensor operations, automatic differentiation, model layers, optimizers, and local tensor files.
Purpose
Use lmtorch when a local LMScript program needs tensor math, gradients, model training, or safetensors and CSV input or output. All parts of one invocation run in one process, so intermediate tensors stay in memory.
When to use it
Use lmtorch for deterministic CPU tensor workflows and small models. Choose another host when the task requires a GPU, a remote training service, or the Python package ecosystem; the executable contains no Python runtime.
Install
Install LMScript with npm install -g @lmctl-ai/lmctl and run lmctl init. A block declared with cli="lmtorch" and install="github:lmctl-ai/lmtorch@latest" downloads the newest release binary when lmtorch is not on PATH; pin a tag such as @v0.5.1 for reproducibility and digest verification.
lmtorch 0.5.1 embeds its host-specific schema (context torch and out, 7 types, enum DType). Standard functions come from the lmcode std catalog registered by the embedded VM and are not repeated in the host schema. lmtorch 0.5.1 vendors the lmcode_std_v3 catalog of the lmcode-cpp commit pinned in sdk.lock: 206 canonical signatures (83 functions and 123 methods on 16 standard types). The current lmcode_std_v3 catalog has 226 signatures, adding the REST and CLI interfaces; lmtorch picks those up when it syncs its SDK pin and releases.
Commands
Write an _lmcode_ block and execute it with run_lmcode, which compiles the block against the schema of the same binary, invokes the host with the v1 invocation manifest and decodes the terminal return frame.
p = _lmcode_ cli="lmtorch" install="github:lmctl-ai/lmtorch@latest";
let values = torch.tensor("[[1.0, 2.0], [3.0, 4.0]]");
let total = values.sum().item();
out.check(total == 10.0, "sum must be 10");
out.set("shape", values.shape()).set("sum", total);
end _lmcode_
run = run_lmcode(p);
if (!run.ok) { println(f"lmtorch {run.phase} error {run.code}: {run.message}"); exit(1); }
result = run.result;
println(f"shape {result.shape}, sum {result.sum}");
The host protocol flags are stable and can be called directly for discovery.
lmtorch --version
lmtorch --lmcode-schema
lmtorch --lmcode-schema --json
lmtorch --lmcode-signatures
lmtorch --lmcode FILE.dslb
Compile-check a script without running it with lmscript --check file.lms.
Inputs and outputs
Literal data enters as JSON text through torch.tensor, usually built in lmscript with json_stringify and substituted into the block with a dollar-brace expression. Shapes are one to four int arguments, as in torch.zeros(2, 3). Tensor operations return tensors; methods such as item and shape return scalar or structured values.
The result of a block is the out object: set fields with out.set and assert expectations with out.check. run_lmcode returns an object whose ok field tells success; on success result holds the out fields, on failure phase, code and message describe the error.
Tensor.save and torch.load use safetensors (F32, F64, I64, BOOL). CSV and text files are also supported by host methods. Every host path is relative to, and must stay inside, the working directory; saves replace an existing file atomically.
Safety rules
The host and LMScript run with the local user's permissions. Review scripts before running them. The install declaration downloads a native executable from the public lmtorch release. Host loads and saves are confined to the working directory, but the std file functions (file_read, file_write, file_append) are not confined and their writes are not atomic, so keep generated artifacts in a dedicated working directory.
In long training loops use torch.mark and torch.release so that unkept tensor handles are freed; using a released handle is reported as an error.
Worked examples
The tensor sum example reduces a two-by-two tensor and checks the result. The autograd example differentiates a sum of squares and verifies that the gradient is 2x.
A gradient through a matrix product, from the repository API document:
prog = _lmcode_ cli="lmtorch" install="github:lmctl-ai/lmtorch@latest";
let x = torch.tensor("[[1.0, 2.0], [3.0, 4.0]]").requires_grad(true);
let loss = (x.matmul(x) + 1.0).square().mean();
loss.backward();
out.set("loss", loss.item()).set("grad", x.grad());
end _lmcode_
r = run_lmcode(prog);
For a training workflow, see classify-moons-dsl.lms in the public repository.
Errors and exit codes
| Exit code | Phase | Meaning |
|---|---|---|
| 0 | none | Success. |
| 2 | cli | Usage error (CLI_USAGE), retired or unknown flag, invalid invocation manifest, or a missing or nonboolean dry value; no host work is done. |
| 3 | load or run | Bytecode failed verification against the embedded schema (for example LOAD_BAD_BYTECODE), or a program failed while running (host errors are reported as RUN_HOST). |
| 4 | app | Application-phase failure, or the result could not be written to standard output. |
A compile error from lmscript usually means LMScript is too old or the host declaration is invalid; upgrade @lmctl-ai/lmctl and check the cli and install values. Invalid tensor shapes, unsupported dtypes, nonfinite or ragged literals and paths outside the working directory are rejected by the host as located RUN_HOST errors. All bytecode files are verified before any of them runs, so a load error leaves no partial work.
How to verify
Run the tensor sum quickrun and confirm its result is 10. For gradients, run the autograd quickrun and confirm the derivative is [2, -4, 6]. Compare lmtorch --lmcode-signatures with the reference when an API name is in doubt. The repository test.sh runs the host API checks and examples.