Core API catalog
Quick reference for Tensor, Module, Parameter, and state operations.
From the implementationinclude/nexus_model/nexus_model.hpp
01Includes and error type#
nexus_model/nexus_model.hpp aggregates layers, core, containers, and serialization. NexusOptim adapter, MatrixFlash bridge, and CUDA headers are separate includes. ModelError derives from std::runtime_error and carries a message; do not assume every invalid input is automatically checked.
02Tensor methods#
The following methods operate on dense float32 storage. Const overloads provide read-only access. Shared Tensor headers do not copy data; use clone for that.
| Method | Contract |
|---|---|
| Tensor() | empty: rank=0, numel=0 |
| Tensor(shape) | allocate uninitialized storage |
| zeros(shape) | allocate + zero |
| full(shape,value) | allocate + fill |
| from_values(shape,values) | value count must match numel |
| borrow(float*,shape) | nonowning external memory |
| rank(), numel(), empty() | shape metadata |
| dims(), shape_vec(), size(axis) | dimensions / copied vector / checked axis |
| same_shape(other) | rank and dimensions equality |
| data(), operator[](i) | raw pointer / unchecked element |
| zero(), fill(value) | in-place writes |
| copy_from(src) | resize + copy bytes |
| clone() | independent storage copy |
| reshape(shape) | shared view; one size_t(-1) allowed |
| view_as(dims,rank) | shared view; concrete shape |
| resize(shape), resize_like(src) | reuse when uniquely owned and capacity sufficient |
| resize_dims(dims,rank) | low-level resize |
| allocation_count() | atomic AlignedStorage allocation counter |
03Module and Parameter#
Registration methods are protected and called from your Module subclass. state_dict snapshots only data and buffers. API pointers are valid only within their owning model lifetime.
| API | Meaning |
|---|---|
| Module::forward(input) | output + cache |
| Module::backward(grad_output) | input gradient; accumulates parameter gradients |
| parameters() | vector<Parameter*> |
| named_parameters(prefix="") | vector<pair<string,Parameter*>> |
| zero_grad() | clear all registered parameter gradients |
| train(), eval(), is_training() | recursive mode / current mode |
| state_dict() | independent clones |
| load_state_dict(state,strict=true) | validate keys and shapes; copy values |
| register_parameter(name,ptr) | register learnable state |
| register_buffer(name,ptr) | register nonlearnable state |
| register_module(name,ptr) | register child module |
| Parameter(values,need_grad=true) | data + zero grad + requires_grad |
| Parameter::accumulate_grad_locked(extra) | locked replica merge; size checked |
| StateDict::items | map<string,Tensor> |
| save_state_dict(state,path) | write NXM1 |
| load_state_dict_file(path) | read NXM1 |
◆Read alongside the source. API behavior reflects the current implementation.