Project Structure

viva_tensor is a Gleam package with a stable root facade, internal implementation modules, and optional native acceleration. Keep this split clear when adding features: package users should depend on the public Gleam contract, not on the current native or planner internals.

Package Layout

PathPurpose
src/viva_tensor.gleamStable public facade. User examples should prefer import viva_tensor as t.
src/viva_tensor/axis.gleam, layout.gleam, named.gleamPublic companion modules for durable tensor concepts.
src/viva_tensor/tensor.gleamInternal tensor implementation used by the facade. It owns pure operations, native dispatch, shape behavior, and fallback paths.
src/viva_tensor/core/Internal storage, shape, dtype, errors, layout math, and FFI wrappers.
src/viva_tensor/backend/Backend protocol and capability descriptions used by planner-style selection code.
src/viva_tensor/native/Gleam-facing native helpers for BLAS, CUDA, sparse kernels, and TFLOPS/backend diagnostics.
src/viva_tensor/quant.gleamInternal quantization entrypoint that re-exports the supported quantization modules.
src/viva_tensor/quant/Quantization implementations: compression, NF4, AWQ, Hadamard preprocessing, tensor-core layout helpers, and TurboQuant reference code.
src/viva_tensor/nn/, optim/, observability/, experimental/Internal domain modules until their contracts are stable enough for public API.
src/*_ffi.erl, src/*_nif.erl, src/*_zig.erlErlang bridge modules required by the BEAM target and NIF loading path.
zig_src/Native C, CUDA, and Zig implementation for the optional NIF.
priv/Runtime native artifacts loaded by Erlang when present.
test/Unit, behavior, public API contract, and NIF/no-NIF compatibility tests.
dev/Development-only Gleam examples and benchmark entrypoints. These modules are runnable with gleam run -m ... but are not supported package API.
bench/External benchmark scripts, grouped by runtime or tool: python/, r/, erlang/, cuda/, scripts/, and windows/. Generated data/ and reports/ stay ignored.
docs/Maintainer-authored guides and long-form documentation.

Public API Boundary

The package boundary is defined by gleam.toml: the root module and a small set of companion modules are public, while backend, core, native, quant, tensor, nn, optim, observability, and experimental are internal.

Promote a module out of internal_modules only when it has:

Benchmarks, demos, and research probes belong in dev/ or bench/ until they become supported runtime features.

Pure Gleam And NIF Fallback

Native acceleration is optional. Public tensor operations must continue to work when the NIF is missing, fails to load, or returns an error.

The usual flow is:

  1. The public facade delegates to internal tensor code.
  2. Internal code validates shape/broadcasting behavior in Gleam.
  3. If inputs are native tensors and a matching NIF operation exists, the native path is attempted through core/ffi.gleam and the Erlang bridge modules.
  4. If the native path is unavailable or fails, the operation falls back to the pure Gleam implementation.

This contract is important for Hex users, CI portability, and development on machines without CUDA, MKL, or a compiled priv/viva_tensor_zig.* artifact. Functions that are truly native-only must say so explicitly in their docs and tests.

The detailed FFI ownership and split contract lives in FFI Architecture. Keep core/ffi.gleam as the forwarding facade until any core/ffi/* split modules are validated in Gleam and migrated one disjoint resource family at a time.

Backend Planner

Backend selection is split between small internal layers:

Keep planner code descriptive rather than magical: record why a backend was chosen, preserve CPU fallback, and do not let benchmark-only assumptions leak into the stable facade.

Quantization Layout

Quantization code is intentionally layered:

Prefer a readable reference implementation first. Move hot loops to NIF/CUDA only after the Gleam contract, invalid-input behavior, and no-NIF fallback are covered.

Native Backend Locations

The native build is centered in zig_src/build.zig.

Gleam modules should call native code only through the existing FFI wrappers and bridge modules. Do not make public APIs depend on a specific native library being installed unless the API is explicitly documented as native-only.

Search Document