Repository navigation
feat: add reusable transformer, checkpoint, and SafeTensors support - #5
Merged
Merged
Conversation
antlobach
force-pushed
the
feat/tabicl-v1-parity
branch
2 times, most recently
from
October 14, 2026 21:21
35f7c17 to
badc6ab
Compare
antlobach
force-pushed
the
feat/tabicl-v1-parity
branch
from
October 14, 2026 21:21
badc6ab to
388034a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Generic framework functionality extracted while porting TabICLv1 to Clojure. The TabICL model itself lives in antlobach/tabicl-clorch, which consumes these APIs.
Reusable transformer modules (
clorch.nn)multihead-attention— batch-first multi-head attention with packed QKV projection and PyTorch-equivalent parameter layout. Forward accepts a tensor for self-attention or a map with:query/:key/:value,:attention-mask,:key-padding-mask,:causal?,:rope,:cached-key/:cached-value, and:return-kv?.transformer-encoder-layer— pre-norm (:norm-first? true) or post-norm block with:relu/:gelu/custom activation, matchingtorch.nn.TransformerEncoderLayersemantics.transformer-encoder— layer stack mirroringtorch.nn.TransformerEncoder, optionally returning a per-layer key/value cache for incremental decoding.Model lifecycle fixes required by GPU execution
defmodelnow uses keyword field keys when recursively applyingtrainandto; the previous generated methods looked fields up by their values and therefore did nothing.defmodeldevice/dtype conversion now delegates every field throughnn/to, including directParameterfields.Parameternow detaches the converted tensor and restoresrequires-grad, preserving leaf status so optimizers receive gradients after CPU/CUDA or dtype conversion.Functional and tensor support
clorch.nn.functional/combine-attention-masks— composes SDPA masks with module-convention key-padding masks through boolean and additive-float paths.torch/precompute-rope-freqsandtorch/apply-ropesupport:interleaved?for adjacent-pair rotation, used by Llama-family and TabICL models; the default half-split layout is unchanged.torch/precompute-rope-freqsaccepts:device, allowing RoPE tables to be created for CUDA inputs without a device mismatch.SafeTensors I/O (
clorch.safetensors)load-file,save-file!,metadata, andload-state-dict!read/write SafeTensors archives without Python or pickle execution.defmodelmodels.Checkpoint and initialization primitives
nn.init/trunc-normal!— inverse-CDF truncated-normal initialization.nn/load-flat-state-dict!/nn/flat-parameter-names— dotted-path state-dict loading for native modules, records, parameters, tensors, and nested collections.float[]tensor construction for binary checkpoint readers.torch/copy-: it usedset_(storage rebinding) rather than value copying.Verification
scripts/release-check.sh --mode gpu)multihead-attentionforward/backwardtransformer-encoder-layerforward/backward[2 4 2 8]; finite values0.0[2 3 3]; 94 parameters; all finiterun_transformer_comparison.sh)2.98e-8, cross-attention5.96e-8, transformer layer1.34e-6, interleaved RoPE0.0(tolerance2e-5)run_transformer_benchmark.sh, dim 512 / 8 heads / batch 4 / seq 256 / 1 thread)32.0 msvs PyTorch35.7 msmedianGPU environment: Ubuntu 24.04, OpenJDK
25.0.4.1, NVIDIA RTX 2000 Ada 16 GB, driver580.159.04, CUDA 13.1 user-space libraries, LibTorch2.10.0-1.5.13.Consumer verification is in antlobach/tabicl-clorch#1. It replaces the port's private attention, transformer, RoPE, and SafeTensors implementations with these APIs.
Files
src/clorch/{nn.clj,nn/functional.clj,nn/init.clj,torch.clj,safetensors.clj}test/clorch/{nn_transformer_test.clj,safetensors_test.clj,nn_init_test.clj,nn_model_test.clj,torch_test.clj}tests_comparison/{compare_transformer.clj,transformer_reference.py},run_transformer_comparison.shbenchmarks/transformer_{clorch.clj,pytorch.py},run_transformer_benchmark.shREADME.md,PORTING_STATUS.md,docs/{nn,functional,init,tensors,safetensors,pytorch-parity}.md,docs/mkdocs.yml