Changelog
Source:NEWS.md
zulm 0.1.0
Initial CRAN release: computation graphs of tensors, built in R and run on the CPU through a bundled ‘ggml’, with automatic differentiation, the AdamW and SGD optimisers, pretrained weights from GGUF and safetensors files, state tensors for decoding, training batches from Arrow streams, and a versioned C interface for other packages.
Bundles the CPU subset of ‘ggml’ v1.2.0, built once per instruction-set level (
baseline,sse42andavx2on x86-64,neonon arm64, Windows included); the best level the CPU supports is loaded with the package, andoptions(zulm.variant =)forces another.zulm_info()reports the bundled ‘ggml’ version, the variant loaded, and the CPU features it was built for.Installing from source with
ZULM_CFLAGSandZULM_CXXFLAGSset builds one variant for the local CPU (not on Windows).Parameter stores:
zl_params(),zl_param()withinitandtype("f32","f16"or"bf16"),zl_param_get(),zl_param_set(),zl_param_names(),zl_param_count()andzl_param_tensor(); initialiserszl_init_normal(),zl_init_uniform(),zl_init_zeros()andzl_init_ones(), drawing from ‘zurand’ under a key derived from the parameter’s name.zl_save()andzl_load()write and read stores as GGUF files, with metadata, at f32, f16 or bf16.zl_load()checks that the file holds every tensor’s data before it reserves memory for them.Every error inherits
zulm_errorand every warningzulm_warning; see?"zulm-conditions"for the classes. Allocations are checked againstgetOption("zulm.max_bytes")(4 GiB by default).When the system runs out of memory, building or running a graph, training, or reading and writing a file is a
zulm_memory_errorrather than a crash, and the session goes on (#55). A trainer whose step ran out of memory cannot be used again and is azulm_stale_error.Graphs:
zl_graph()builds a graph once from an R function,zl_int()declares integer inputs,zl_forward()runs it any number of times on new inputs, andzl_graph_info()describes it.Operations on tensors inside a graph:
+,-,*,/with broadcasting,zl_scale(),zl_sum(),zl_sum_rows(),zl_mean_rows(),zl_cumsum(), the elementwise functions (zl_sqr()tozl_tanh(),zl_gelu()),zl_swiglu(),zl_crossprod(),zl_reshape(),zl_permute(),zl_t(),zl_cont(),zl_view(),zl_concat(),zl_pad(),zl_get_rows(),zl_where(),zl_clamp(),zl_tri(),zl_softmax()with masks,zl_causal_mask(),zl_rope()in both conventions,zl_rms_norm(),zl_layer_norm(), the losseszl_cross_entropy(),zl_mse()andzl_mae(), andzl_cast().Every argument ‘ggml’ would assert on is checked first; an assertion that still happens while a graph is built is
zulm_engine_error.‘ggml’’s log becomes messages of class
zulm_message, at the level ofoptions(zulm.log =)("warn"by default).Training:
zl_adamw()andzl_sgd()describe optimisers;zl_trainer()prepares a graph with alossfor training through ‘ggml-opt’, with gradient accumulation and global-norm clipping (clip_norm);zl_step()trains on one batch,zl_grad()returns a parameter’s gradient, andzl_reset()clears the optimiser’s state.zl_tanh(),zl_concat(),zl_tri()andzl_causal_mask()have gradients. An operation without a gradient between a parameter and the loss is refused withzulm_unsupported_backward, naming it and its call. A graph whose training graphs would hold more than the 2048 nodes ‘ggml-opt’ allows is refused withzulm_invalid_argumentbefore ‘ggml-opt’ sees it (#56).zl_trainer_save()andzl_trainer_load()save a trainer’s parameters and optimiser state (AdamW’s moments and the step counts) to one GGUF file and restore it into a trainer built the same way. Training resumed this way equals training that never stopped, bit for bit. A file whose parameters, optimiser oraccumulatediffer from the trainer’s, and a save in the middle of an accumulation period, are refused.zl_load()reads such a file’s parameters.zl_graph()andzl_trainer()collect garbage when graphs and trainers no longer referenced hold over 128 MB, which R’s collector does not see.The bundled ‘ggml’ carries patches for the gradients of
SUBandDIVwith broadcasting, for gradient clipping in ‘ggml-opt’, and for reading and setting ‘ggml-opt’’s optimiser state.A NaN reaching
zl_softmax()no longer stops R, a transposed column enters arithmetic and other kernels correctly, and graphs reserve memory as they grow rather than for the largest graph.zl_rope()refuses an odd head width, which ‘ggml’’s kernel would copy past.Random numbers come from ‘zurand’:
zl_seed()sets the session key,zl_rng_uniform()andzl_rng_normal()draw in R, andzl_noise_uniform()andzl_noise_normal()draw inside a graph, anew at each training step. Draws do not depend on the thread count or on how a batch is split.Reported losses (
zl_cross_entropy(),zl_mse(),zl_mae(), fromzl_step()andzl_forward()) are fixed-order sums of per-row values, so they are the same at any thread count.Modules:
zl_linear(),zl_embedding(),zl_norm(),zl_attention(),zl_mlp(),zl_block(),zl_adaln()andzl_time_embedding()declare their parameters in a store and return functions of tensors. They, the losses and two attention blocks in the styles of TimesFM and Toto are checked against PyTorch.zl_threads()sets the number of threads for the session, capped at the available cores and at 2 whileR CMD checklimits cores. One persistent pool serves every computation, and is recreated in a forked process. A computation can be interrupted; it then signalszulm_interrupt.safetensors:
zl_safetensors_read(),zl_load_safetensors()(with a name map) andzl_save_safetensors(). PyTorch’s[out, in]weights load asc(in, out)with no transposition.Half-precision weights:
zl_load(keep_types = TRUE)andzl_load_safetensors()keep f16 and bf16 parameters in their type, andzl_param_names(types = TRUE)reports types. The documentation states their cost in precision.State tensors:
zl_state()declares a persistent tensor that graphs read throughzl_param_tensor()and write withzl_state_write();zl_state_get()andzl_state_set(), withoffset, read and write it from R. States are neither trained nor saved. A key-value cache for incremental decoding is built from them.zl_graph_cache()builds a graph per new set of input shapes forzl_forward(), and frees the least recently used undergetOption("zulm.max_bytes").zl_crossprod()accepts a transposed vector as its first argument, aszl_attention()makes for a single position.zl_flash_attention()is ‘ggml’’s fused attention, forward only: it agrees with the plain form within f32 rounding, gives the same bits at any thread count, and attends over long contexts in memory linear in their length.Arrow streams:
zl_arrow_batches()reads record batches from ‘DuckDB’, Parquet or ‘arrow’ through ‘nanoarrow’ into batches of exactlyrowsrows, with missing values as errors or masks and the last batch dropped or padded;zl_step()andzl_forward()take the iterator asdata, andzl_next()returns a batch as R arrays.zl_int()marks integer columns in itsmap.The C table:
inst/include/zulm.hdeclareszulm_api, registered aszulm_api_v1, for packages withLinkingTo: zulm. From C they run graphs built in R, write inputs, read outputs, read and write parameters and state tensors, draw from named streams under the session key, and fill inputs from an Arrow iterator. Failures are return codes with a message, never R errors.Three articles on the website (they are not built with the package): Get started trains a forecaster for a monthly series, “Training a model” trains a small event-sequence model and samples from it, and “Running a pretrained block” runs an attention block exported by PyTorch and decodes through a cache.
-
Fuzzing (
tools/fuzz/) found, and this version fixes:- sizes, offsets and axes of 2^31 or more, or
NA, reached ‘ggml’ as negative numbers; they arezulm_invalid_argument; - an invalid choice, such as
zl_tri(x, "x"), gave an unclassed error; it iszulm_invalid_argument; -
zl_param()allocated the initial values of a parameter larger thangetOption("zulm.max_bytes")before refusing it; - training graphs whose gradients ‘ggml’ cannot carry back through a permutation, cross-entropy labels that depend on a parameter, and a half-precision product whose second operand needs a gradient, reached ‘ggml’ assertions; they are
zulm_unsupported_backward; - gradient clipping stopped R with a ‘ggml’ assertion on a permuted parameter whose first dimension was 1 (patch P7);
- a GGUF file with an invalid metadata type was undefined behaviour in ‘ggml’’s reader (patch P11).
- sizes, offsets and axes of 2^31 or more, or
-
A critical review before release found, and this version fixes:
-
zl_pad(),zl_crossprod(),zl_concat()and the noise functions could make a tensor whose size wrapped past 2^64 bytes and crash inzl_forward(); such tensors arezulm_memory_error(#67); - an input of more than 2^62 bytes under
options(zulm.max_bytes = Inf)stopped R with a ‘ggml’ assertion; it iszulm_shape_error(#68); -
zl_save_safetensors()wrote an unreadable file when a parameter was named__metadata__; the name is refused (#69); - GGUF integer metadata of -2^31 read as
NA; it reads as a number (#70); -
zl_adamw()accepted betas of 1, which turn every parameter intoNaN; betas must be below 1 in f32 (#71); - GGUF error messages held control bytes (#73);
- graphs with many constants, such as many
zl_time_embedding()calls, failed to finish (#74); - safetensors dimensions past R’s integer range gave an unclassed error (#75), and a 0-d tensor could not fill a parameter of one value (#76);
- GGUF tensors with empty or invalid UTF-8 names loaded; they are
zulm_file_error(#77); -
zl_seed(-1)gave an unclassed error (#78); - the GGUF metadata key
general.alignmentreached a ‘ggml’ assertion; it is reserved (#79).
-