lmtorch API guide

The lmtorch lmcode host runs a compiled LMScript block in one native process. Tensor values and model state stay in memory until the block returns.

Overview

Start with a host block that declares the released executable and the operation to run.

Start a host block

Declare the released host with cli and install. LMScript asks that binary for its embedded lmcode schema and uses the same executable to run the compiled block.

p = _lmcode_ cli="lmtorch" install="github:lmctl-ai/lmtorch@v0.5.1";
let x = torch.tensor("[1.0, 2.0, 3.0]");
out.set("sum", x.sum().item());
end _lmcode_
r = run_lmcode(p);

run_lmcode(p) compiles the block, invokes the host with --lmcode and returns an object with ok and result; result holds the values set through out. The host flags --lmcode-schema --json, --lmcode-signatures and --lmcode FILE.dslb remain the host protocol. The install value may be @latest or a pinned tag such as @v0.5.1.

Tensor creation and operations

Torch creates tensors from JSON arrays and provides zeros, ones, full, rand, randn, arange, linspace, eye, and scalar constructors. Tensor methods cover shape, indexing, arithmetic, matrix multiplication, reductions, reshaping, activations, comparisons, and file output.

let a = torch.tensor("[[1.0, 2.0], [3.0, 4.0]]");
let total = a.sum().item();
let product = a.matmul(a);
out.set("sum", total).set("product", product);

Automatic differentiation

Mark a tensor with requires_grad(true), build a scalar loss, and call backward. Read the derivative with grad. Gradients accumulate until zero_grad is called on the tensor or optimizer.

let x = torch.tensor("[1.0, -2.0, 3.0]").requires_grad(true);
let loss = x.square().sum();
loss.backward();
out.set("gradient", x.grad());

Models and optimizers

Build a sequential model by chaining layers such as linear, conv2d, relu, flatten, and dropout. Call forward to evaluate it. Create an SGD, Adam, or AdamW optimizer from the model, then zero gradients, backpropagate a loss, and take a step.

Files and runtime limits

Use Tensor.save and Torch.load for safetensors, read_csv for numeric CSV input, and write_csv for tabular predictions. Output paths must stay inside the current working directory. The public release is CPU-only and includes its native runtime dependencies.

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.

See the reference for every signature, docs/dsl-api.md for method signatures and the source examples for complete programs.