lmtorch reference
Command-line flags, exit codes, context objects, host types and methods, and standard-library signatures of lmtorch 0.5.1, generated from the repository schema (schema/lmtorch.host.json), the host source and the vendored lmcode std schema. The host schema declares 214 method signatures on 7 types. Parameter and return types are int, float, string, bool, void and the host or standard types named here.
Command line
| Command | Effect |
|---|---|
| lmtorch --version | Prints lmtorch and the version number, exit 0. |
| lmtorch --lmcode-schema | Prints a readable summary of the host API grouped by type. |
| lmtorch --lmcode-schema --json | Prints the exact embedded host schema bytes (schema 2, name lmtorch) that lmscript compiles against. |
| lmtorch --lmcode-signatures | Prints one canonical signature per line (context, function and method entries). |
| lmtorch --lmcode FILE.dslb | Runs compiled bytecode under the v1 invocation manifest and prints one terminal return frame. Several files may follow one --lmcode. |
| --first FILE.dslb, --last FILE.dslb | Additional bytecode files ordered before or after the --lmcode files in the same invocation. |
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. |
Used from lmscript
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}");
Context and host types
| Name | Kind | Role |
|---|---|---|
| torch | context object of type Torch | Tensor constructors, seeding, thread count, grad mode, loading of tensors, bundles, models and CSV, handle marks. |
| out | context object of type Out | Builds the Json result of the block with set and verifies expectations with check. |
| Tensor | type, 88 methods | CPU 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. |
| TensorList | type, 3 methods | Ordered list of tensors. |
| Bundle | type, 7 methods | Named tensors, stored as one safetensors file. |
| Model | type, 24 methods | Sequential model built by chaining layer methods. |
| Optimizer | type, 4 methods | Optimizer created from a model. |
| DType | enum | F32, F64, I64, BOOL. |
Enum DType
| Value | Meaning |
|---|---|
| F32 | 32-bit float |
| F64 | 64-bit float |
| I64 | 64-bit integer |
| BOOL | boolean, one byte in safetensors |
Tensor operators
| Operator key | Tensor method |
|---|---|
| + | add |
| - | sub |
| * | mul |
| neg | neg |
| r+ | add |
| r- | rsub |
| r* | mul |
Keys beginning with r apply when the tensor is the right operand. There is no operator for division; call div.
Torch methods
| Method | Signatures |
|---|---|
| version | version() -> string |
| manual_seed | manual_seed(int) -> void |
| set_num_threads | set_num_threads(int) -> void |
| grad_enabled | grad_enabled(bool) -> void |
| is_grad_enabled | is_grad_enabled() -> bool |
| zeros | zeros(int) -> Tensor; zeros(int, int) -> Tensor; zeros(int, int, int) -> Tensor; zeros(int, int, int, int) -> Tensor |
| ones | ones(int) -> Tensor; ones(int, int) -> Tensor; ones(int, int, int) -> Tensor; ones(int, int, int, int) -> Tensor |
| full | full(int, float) -> Tensor; full(int, int, float) -> Tensor; full(int, int, int, float) -> Tensor; full(int, int, int, int, float) -> Tensor |
| rand | rand(int) -> Tensor; rand(int, int) -> Tensor; rand(int, int, int) -> Tensor; rand(int, int, int, int) -> Tensor |
| randn | randn(int) -> Tensor; randn(int, int) -> Tensor; randn(int, int, int) -> Tensor; randn(int, int, int, int) -> Tensor |
| randint | randint(int, int, int) -> Tensor; randint(int, int, int, int) -> Tensor |
| randperm | randperm(int) -> Tensor |
| arange | arange(int) -> Tensor; arange(float, float, float) -> Tensor |
| linspace | linspace(float, float, int) -> Tensor |
| eye | eye(int) -> Tensor |
| scalar | scalar(float) -> Tensor |
| tensor | tensor(string) -> Tensor; tensor(string, DType) -> Tensor |
| where | where(Tensor, Tensor, Tensor) -> Tensor |
| list | list() -> TensorList |
| cat | cat(TensorList, int) -> Tensor |
| stack | stack(TensorList, int) -> Tensor |
| load | load(string) -> Tensor; load(string, string) -> Tensor |
| save | save(string, Tensor) -> void |
| bundle | bundle() -> Bundle |
| open | open(string) -> Bundle |
| read_csv | read_csv(string, bool) -> Tensor |
| sequential | sequential() -> Model |
| load_model | load_model(string) -> Model |
| sgd | sgd(Model, float) -> Optimizer; sgd(Model, float, float) -> Optimizer; sgd(TensorList, float) -> Optimizer |
| adam | adam(Model, float) -> Optimizer; adam(TensorList, float) -> Optimizer |
| adamw | adamw(Model, float, float) -> Optimizer |
| mark | mark() -> int |
| release | release(int) -> int |
| live_tensors | live_tensors() -> int |
Tensor methods
| Method | Signatures |
|---|---|
| numel | numel() -> int |
| dim | dim() -> int |
| size | size(int) -> int |
| shape | shape() -> string |
| dtype | dtype() -> DType |
| item | item() -> float |
| item_int | item_int() -> int |
| value | value(int) -> float |
| value_int | value_int(int) -> int |
| set | set(int, float) -> self |
| to_text | to_text() -> string |
| to_json | to_json() -> string |
| to | to(DType) -> Tensor |
| add | add(Tensor) -> Tensor; add(float) -> Tensor |
| sub | sub(Tensor) -> Tensor; sub(float) -> Tensor |
| rsub | rsub(float) -> Tensor |
| mul | mul(Tensor) -> Tensor; mul(float) -> Tensor |
| div | div(Tensor) -> Tensor; div(float) -> Tensor |
| pow | pow(float) -> Tensor |
| neg | neg() -> Tensor |
| matmul | matmul(Tensor) -> Tensor |
| maximum | maximum(Tensor) -> Tensor |
| minimum | minimum(Tensor) -> Tensor |
| abs | abs() -> Tensor |
| exp | exp() -> Tensor |
| log | log() -> Tensor |
| sqrt | sqrt() -> Tensor |
| square | square() -> Tensor |
| sin | sin() -> Tensor |
| cos | cos() -> Tensor |
| sign | sign() -> Tensor |
| floor | floor() -> Tensor |
| round | round() -> Tensor |
| clamp | clamp(float, float) -> Tensor |
| relu | relu() -> Tensor |
| sigmoid | sigmoid() -> Tensor |
| tanh | tanh() -> Tensor |
| gelu | gelu() -> Tensor |
| leaky_relu | leaky_relu(float) -> Tensor |
| softmax | softmax(int) -> Tensor |
| log_softmax | log_softmax(int) -> Tensor |
| gt | gt(Tensor) -> Tensor; gt(float) -> Tensor |
| lt | lt(Tensor) -> Tensor; lt(float) -> Tensor |
| ge | ge(float) -> Tensor |
| le | le(float) -> Tensor |
| eq | eq(Tensor) -> Tensor; eq(float) -> Tensor |
| sum | sum() -> Tensor; sum(int) -> Tensor; sum(int, bool) -> Tensor |
| mean | mean() -> Tensor; mean(int) -> Tensor; mean(int, bool) -> Tensor |
| std | std() -> Tensor; std(int, bool) -> Tensor |
| var | var() -> Tensor |
| norm | norm() -> Tensor |
| max | max() -> Tensor |
| min | min() -> Tensor |
| amax | amax(int) -> Tensor |
| amin | amin(int) -> Tensor |
| argmax | argmax() -> Tensor; argmax(int) -> Tensor |
| argmin | argmin(int) -> Tensor |
| reshape | reshape(int) -> Tensor; reshape(int, int) -> Tensor; reshape(int, int, int) -> Tensor; reshape(int, int, int, int) -> Tensor |
| flatten | flatten() -> Tensor |
| t | t() -> Tensor |
| transpose | transpose(int, int) -> Tensor |
| unsqueeze | unsqueeze(int) -> Tensor |
| squeeze | squeeze() -> Tensor; squeeze(int) -> Tensor |
| slice | slice(int, int, int) -> Tensor |
| select | select(int, int) -> Tensor |
| index_select | index_select(int, Tensor) -> Tensor |
| linear | linear(Tensor) -> Tensor; linear(Tensor, Tensor) -> Tensor |
| mse_loss | mse_loss(Tensor) -> Tensor |
| l1_loss | l1_loss(Tensor) -> Tensor |
| smooth_l1_loss | smooth_l1_loss(Tensor) -> Tensor |
| cross_entropy | cross_entropy(Tensor) -> Tensor |
| nll_loss | nll_loss(Tensor) -> Tensor |
| bce_with_logits | bce_with_logits(Tensor) -> Tensor |
| allclose | allclose(Tensor, float, float) -> bool |
| equal | equal(Tensor) -> bool |
| is_finite | is_finite() -> bool |
| any | any() -> bool |
| all | all() -> bool |
| requires_grad | requires_grad() -> bool; requires_grad(bool) -> self |
| backward | backward() -> void |
| grad | grad() -> Tensor |
| has_grad | has_grad() -> bool |
| zero_grad | zero_grad() -> self |
| detach | detach() -> Tensor |
| clone | clone() -> Tensor |
| keep | keep() -> self |
| save | save(string) -> self |
| write_csv | write_csv(string) -> self |
TensorList methods
| Method | Signatures |
|---|---|
| add | add(Tensor) -> self |
| len | len() -> int |
| at | at(int) -> Tensor |
Bundle methods
| Method | Signatures |
|---|---|
| put | put(string, Tensor) -> self |
| get | get(string) -> Tensor |
| has | has(string) -> bool |
| keys | keys() -> StrList |
| meta | meta(string) -> string; meta(string, string) -> self |
| has_meta | has_meta(string) -> bool |
| save | save(string) -> void |
Model methods
| Method | Signatures |
|---|---|
| linear | linear(int, int) -> self |
| conv2d | conv2d(int, int, int) -> self; conv2d(int, int, int, int, int) -> self |
| max_pool2d | max_pool2d(int) -> self |
| flatten | flatten() -> self |
| relu | relu() -> self |
| tanh | tanh() -> self |
| sigmoid | sigmoid() -> self |
| gelu | gelu() -> self |
| leaky_relu | leaky_relu(float) -> self |
| softmax | softmax(int) -> self |
| log_softmax | log_softmax(int) -> self |
| dropout | dropout(float) -> self |
| layer_norm | layer_norm(int) -> self |
| forward | forward(Tensor) -> Tensor |
| parameters | parameters() -> TensorList |
| param | param(string) -> Tensor |
| num_parameters | num_parameters() -> int |
| layers | layers() -> int |
| summary | summary() -> string |
| train | train() -> self |
| eval | eval() -> self |
| zero_grad | zero_grad() -> self |
| save | save(string) -> void |
| load | load(string) -> self |
Optimizer methods
| Method | Signatures |
|---|---|
| zero_grad | zero_grad() -> void |
| step | step() -> void |
| lr | lr() -> float |
| set_lr | set_lr(float) -> self |
Out methods
| Method | Signatures |
|---|---|
| set | set(string, float) -> self; set(string, int) -> self; set(string, string) -> self; set(string, bool) -> self; set(string, Tensor) -> self |
| append | append(string, float) -> self; append(string, int) -> self |
| log | log(string) -> void |
| check | check(bool, string) -> void |
| fail | fail(string) -> void |
| write_text | write_text(string, string) -> void |
Standard library in lmtorch 0.5.1
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. lmtorch implements none of it; the embedded VM registers it. Std file functions are not confined to the working directory.
Functions: file (5 signatures)
| Function | Signatures |
|---|---|
| file_append | file_append(string, string) -> void |
| file_exists | file_exists(string) -> bool |
| file_lines | file_lines(string) -> StrList |
| file_read | file_read(string) -> string |
| file_write | file_write(string, string) -> void |
Functions: float (5 signatures)
| Function | Signatures |
|---|---|
| float_list | float_list() -> FloatList |
| float_map | float_map() -> FloatMap |
| float_pq | float_pq() -> FloatPQ |
| float_queue | float_queue() -> FloatQueue |
| float_stack | float_stack() -> FloatStack |
Functions: int (5 signatures)
| Function | Signatures |
|---|---|
| int_list | int_list() -> IntList |
| int_map | int_map() -> IntMap |
| int_pq | int_pq() -> IntPQ |
| int_queue | int_queue() -> IntQueue |
| int_stack | int_stack() -> IntStack |
Functions: lmcode (24 signatures)
| Function | Signatures |
|---|---|
| lmcode_return_null | lmcode_return_null() -> void |
| lmcode_return_put | lmcode_return_put(string, bool) -> void; lmcode_return_put(string, float) -> void; lmcode_return_put(string, FloatList) -> void; lmcode_return_put(string, FloatMap) -> void; lmcode_return_put(string, int) -> void; lmcode_return_put(string, IntList) -> void; lmcode_return_put(string, IntMap) -> void; lmcode_return_put(string, Json) -> void; lmcode_return_put(string, StrList) -> void; lmcode_return_put(string, StrMap) -> void; lmcode_return_put(string, string) -> void |
| lmcode_return_put_null | lmcode_return_put_null(string) -> void |
| lmcode_return_set | lmcode_return_set(bool) -> void; lmcode_return_set(float) -> void; lmcode_return_set(FloatList) -> void; lmcode_return_set(FloatMap) -> void; lmcode_return_set(int) -> void; lmcode_return_set(IntList) -> void; lmcode_return_set(IntMap) -> void; lmcode_return_set(Json) -> void; lmcode_return_set(StrList) -> void; lmcode_return_set(StrMap) -> void; lmcode_return_set(string) -> void |
Functions: math (16 signatures)
| Function | Signatures |
|---|---|
| math_abs | math_abs(int) -> int; math_abs(float) -> float |
| math_ceil | math_ceil(int) -> int; math_ceil(float) -> float |
| math_floor | math_floor(int) -> int; math_floor(float) -> float |
| math_max | math_max(int, int) -> int; math_max(float, float) -> float |
| math_min | math_min(int, int) -> int; math_min(float, float) -> float |
| math_pow | math_pow(int, int) -> int; math_pow(float, float) -> float |
| math_round | math_round(int) -> int; math_round(float) -> float |
| math_sqrt | math_sqrt(int) -> int; math_sqrt(float) -> float |
Functions: str (26 signatures)
| Function | Signatures |
|---|---|
| str_contains | str_contains(string, string) -> bool |
| str_ends_with | str_ends_with(string, string) -> bool |
| str_find | str_find(string, string) -> int |
| str_is_float | str_is_float(string) -> bool |
| str_is_int | str_is_int(string) -> bool |
| str_len | str_len(string) -> int |
| str_list | str_list() -> StrList |
| str_lower | str_lower(string) -> string |
| str_map | str_map() -> StrMap |
| str_match | str_match(string, string) -> bool |
| str_pq | str_pq() -> StrPQ |
| str_queue | str_queue() -> StrQueue |
| str_repeat | str_repeat(string, int) -> string |
| str_replace | str_replace(string, string, string) -> string |
| str_split | str_split(string, string) -> StrList |
| str_stack | str_stack() -> StrStack |
| str_starts_with | str_starts_with(string, string) -> bool |
| str_substr | str_substr(string, int, int) -> string |
| str_to_float | str_to_float(string) -> float |
| str_to_int | str_to_int(string) -> int |
| str_trim | str_trim(string) -> string |
| str_upper | str_upper(string) -> string |
| str_base64_encode | str_base64_encode(string) -> string |
| str_base64_decode | str_base64_decode(string) -> string |
| str_hex_encode | str_hex_encode(string) -> string |
| str_hex_decode | str_hex_decode(string) -> string |
Functions: other (2 signatures)
| Function | Signatures |
|---|---|
| json_parse | json_parse(string) -> Json |
| template_render | template_render(string, Json) -> string |
FloatList (7 signatures)
| Method | Signatures |
|---|---|
| add | add(float) -> FloatList |
| at | at(int) -> float |
| clear | clear() -> FloatList |
| contains | contains(float) -> bool |
| index_of | index_of(float) -> int |
| len | len() -> int |
| set | set(int, float) -> FloatList |
FloatMap (10 signatures)
| Method | Signatures |
|---|---|
| len | len() -> int |
| has | has(string) -> bool |
| get | get(string) -> float |
| get_or | get_or(string, float) -> float |
| set | set(string, float) -> FloatMap |
| key_at | key_at(int) -> string |
| remove | remove(string) -> bool |
| clear | clear() -> FloatMap |
| keys | keys() -> StrList |
| values | values() -> FloatList |
FloatPQ (6 signatures)
| Method | Signatures |
|---|---|
| push | push(float, float) -> FloatPQ |
| pop | pop() -> float |
| peek | peek() -> float |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> FloatPQ |
FloatQueue (7 signatures)
| Method | Signatures |
|---|---|
| push | push(float) -> FloatQueue |
| pop | pop() -> float |
| peek | peek() -> float |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> FloatQueue |
| at | at(int) -> float |
FloatStack (7 signatures)
| Method | Signatures |
|---|---|
| push | push(float) -> FloatStack |
| pop | pop() -> float |
| peek | peek() -> float |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> FloatStack |
| at | at(int) -> float |
IntList (7 signatures)
| Method | Signatures |
|---|---|
| add | add(int) -> IntList |
| at | at(int) -> int |
| clear | clear() -> IntList |
| contains | contains(int) -> bool |
| index_of | index_of(int) -> int |
| len | len() -> int |
| set | set(int, int) -> IntList |
IntMap (10 signatures)
| Method | Signatures |
|---|---|
| len | len() -> int |
| has | has(string) -> bool |
| get | get(string) -> int |
| get_or | get_or(string, int) -> int |
| set | set(string, int) -> IntMap |
| key_at | key_at(int) -> string |
| remove | remove(string) -> bool |
| clear | clear() -> IntMap |
| keys | keys() -> StrList |
| values | values() -> IntList |
IntPQ (6 signatures)
| Method | Signatures |
|---|---|
| push | push(int, float) -> IntPQ |
| pop | pop() -> int |
| peek | peek() -> int |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> IntPQ |
IntQueue (7 signatures)
| Method | Signatures |
|---|---|
| push | push(int) -> IntQueue |
| pop | pop() -> int |
| peek | peek() -> int |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> IntQueue |
| at | at(int) -> int |
IntStack (7 signatures)
| Method | Signatures |
|---|---|
| push | push(int) -> IntStack |
| pop | pop() -> int |
| peek | peek() -> int |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> IntStack |
| at | at(int) -> int |
Json (11 signatures)
| Method | Signatures |
|---|---|
| kind | kind() -> string |
| len | len() -> int |
| at | at(int) -> Json |
| get | get(string) -> Json |
| has | has(string) -> bool |
| keys | keys() -> StrList |
| as_string | as_string() -> string |
| as_int | as_int() -> int |
| as_float | as_float() -> float |
| as_bool | as_bool() -> bool |
| to_json | to_json() -> string |
StrList (8 signatures)
| Method | Signatures |
|---|---|
| add | add(string) -> StrList |
| at | at(int) -> string |
| clear | clear() -> StrList |
| contains | contains(string) -> bool |
| index_of | index_of(string) -> int |
| join | join(string) -> string |
| len | len() -> int |
| set | set(int, string) -> StrList |
StrMap (10 signatures)
| Method | Signatures |
|---|---|
| clear | clear() -> StrMap |
| get | get(string) -> string |
| get_or | get_or(string, string) -> string |
| has | has(string) -> bool |
| key_at | key_at(int) -> string |
| len | len() -> int |
| remove | remove(string) -> bool |
| set | set(string, string) -> StrMap |
| keys | keys() -> StrList |
| values | values() -> StrList |
StrPQ (6 signatures)
| Method | Signatures |
|---|---|
| push | push(string, float) -> StrPQ |
| pop | pop() -> string |
| peek | peek() -> string |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> StrPQ |
StrQueue (7 signatures)
| Method | Signatures |
|---|---|
| push | push(string) -> StrQueue |
| pop | pop() -> string |
| peek | peek() -> string |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> StrQueue |
| at | at(int) -> string |
StrStack (7 signatures)
| Method | Signatures |
|---|---|
| push | push(string) -> StrStack |
| pop | pop() -> string |
| peek | peek() -> string |
| len | len() -> int |
| empty | empty() -> bool |
| clear | clear() -> StrStack |
| at | at(int) -> string |
REST and CLI interfaces
The current lmcode_std_v3 catalog (226 signatures) adds the former REST module. None of the names below exist in the std vendored by lmtorch 0.5.1; they become available when lmtorch syncs its lmcode-cpp pin and releases.
| Kind | Names (not in lmtorch 0.5.1) |
|---|---|
| Standard interfaces | RestGet, RestPost, RestPut, RestDelete, RestPatch, RestHead, RestOptions, RestHandler, CliCommand, CliOption |
| Functions | rest_config(string, string) -> void; rest_route(string, string, string) -> void; rest_serve() -> void; rest_stop() -> void; rest_cli(StrList) -> string |
| Exchange type | RestExchange with 15 methods |