lmtorch

lmtorch logo

lmtorch is a CPU tensor host for LMScript: tensor operations, automatic differentiation, model layers, optimizers, and safetensors and CSV input and output through one released native executable (version 0.5.1). This page is the quick reference; details are in the linked pages.

Install and run

npm install -g @lmctl-ai/lmctl
lmctl init
lmctl autorun https://lmctl.com/tools/lmtorch/lmtorch_solo.lms

A block that declares cli="lmtorch" and install="github:lmctl-ai/lmtorch@latest" downloads the release binary when lmtorch is not on PATH; pin a tag such as @v0.5.1 for reproducible runs.

Host protocol

CommandEffect
lmtorch --versionPrints lmtorch and the version number, exit 0.
lmtorch --lmcode-schemaPrints a readable summary of the host API grouped by type.
lmtorch --lmcode-schema --jsonPrints the exact embedded host schema bytes (schema 2, name lmtorch) that lmscript compiles against.
lmtorch --lmcode-signaturesPrints one canonical signature per line (context, function and method entries).
lmtorch --lmcode FILE.dslbRuns compiled bytecode under the v1 invocation manifest and prints one terminal return frame. Several files may follow one --lmcode.
--first FILE.dslb, --last FILE.dslbAdditional bytecode files ordered before or after the --lmcode files in the same invocation.

Exit codes: 0 success, 2 command-line or manifest error, 3 load or run error, 4 application or output error. Retired flag spellings are rejected.

Context objects and types

NameKindRole
torchcontext object of type TorchTensor constructors, seeding, thread count, grad mode, loading of tensors, bundles, models and CSV, handle marks.
outcontext object of type OutBuilds the Json result of the block with set and verifies expectations with check.
Tensortype, 88 methodsCPU tensor: shape, indexing, arithmetic, reductions, activations, autograd, save and CSV output. Operators + - * and unary minus map to add, sub, mul and neg; division is the div method.
TensorListtype, 3 methodsOrdered list of tensors.
Bundletype, 7 methodsNamed tensors, stored as one safetensors file.
Modeltype, 24 methodsSequential model built by chaining layer methods.
Optimizertype, 4 methodsOptimizer created from a model.
DTypeenumF32, F64, I64, BOOL.

Most used calls

TaskCalls
Create tensorstorch.tensor(json text), torch.zeros, torch.ones, torch.full, torch.rand, torch.randn, torch.randint (shapes are 1 to 4 int arguments)
Reproducibilitytorch.manual_seed(int), torch.set_num_threads(int)
Read valuesTensor.item, Tensor.shape, Tensor.sum, Tensor.mean
GradientsTensor.requires_grad(true), Tensor.backward, Tensor.grad, Tensor.zero_grad, torch.grad_enabled(bool)
FilesTensor.save, torch.load, torch.load_model, Model.save; paths stay inside the working directory
Resultout.set(key, value) chained, out.check(condition, message)
Memory in long loopstorch.mark and torch.release

Minimal example

A complete lmscript file, taken from examples/tensor-sum-dsl.lms in the repository. run_lmcode compiles the block, runs the host and returns an object with ok and result.

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}");

Standard library

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.

More