class
SushiBLAS::IO
Input/Output operations for Tensors.
- Declared in
include/SushiBLAS/io.hpp
This class provides methods to save, load, and display tensors. It supports both native .sushi formats and NumPy .npy exports for interoperability.
Public member functions
void save(const Tensor &t, const std::string &path)Save tensor in the native .sushi format.
This includes a header with metadata (shape, dtype) followed by raw data.
Parameters
tThe tensor to save; stored and contiguous.
pathThe filesystem path to save the file to.
Exceptions
std::invalid_argumentwhen t is strided or has no storage.
std::runtime_errorwhen a write fails; the partial file is removed.
void load(Tensor &t, const std::string &path)Load a .sushi file into a tensor.
Runs the recorded graph first, then verifies that the file metadata matches the tensor.
Parameters
tThe target tensor to load data into.
pathThe filesystem path of the .sushi file.
void read(const Tensor &t, void *dst, size_t bytes)Runs the recorded graph, then copies the tensor's elements into host memory.
Parameters
tA stored, contiguous tensor; any residency.
bytesMust equal num_elements times the dtype's element size.
Exceptions
std::invalid_argumentwhen t breaks those rules or bytes differs.
void write(Tensor &t, const void *src, size_t bytes)Runs the recorded graph, then copies host memory into the tensor's elements.
Parameters
tA stored, contiguous tensor; any residency.
bytesMust equal num_elements times the dtype's element size.
Exceptions
std::invalid_argumentwhen t breaks those rules or bytes differs.
template <typename T>
std::vector< T > to_vector(const Tensor &t)Returns the tensor's elements as a host vector, after running the graph.
Template parameters
TThe host type matching t.dtype (see Detail::HostDataType).
Exceptions
std::invalid_argumentwhen t's dtype is not T's, or read() rejects t.
template <typename T>
void from_vector(Tensor &t, const std::vector< T > &values)Copies a host vector into the tensor's elements, after running the graph.
Template parameters
TThe host type matching t.dtype (see Detail::HostDataType).
Exceptions
std::invalid_argumentwhen t's dtype is not T's, the sizes differ, or write() rejects t.
void save_npy(const Tensor &t, const std::string &path)Save tensor in NumPy .npy format.
This allows the tensor to be loaded directly in Python using np.load().
Parameters
tThe tensor to export; stored, contiguous, and not COLUMN_MAJOR at rank 3 or more.
pathThe filesystem path to save the .npy file.
Exceptions
std::invalid_argumentwhen t breaks one of those rules.
std::runtime_errorwhen a write fails; the partial file is removed.
void load_npy(Tensor &t, const std::string &path)Runs the recorded graph, then loads a .npy file of version 1.0, 2.0 or 3.0.
The header's descr, fortran_order and shape must match the tensor exactly.
Parameters
tA stored, contiguous tensor, not COLUMN_MAJOR at rank 3 or more.
Exceptions
std::invalid_argumentwhen t breaks one of those rules.
std::runtime_erroron another version, a malformed header or a mismatch.
See also
include/SushiBLAS/README.md
void save_bin(const Tensor &t, const std::string &path)Raw binary save (no header).
Parameters
tThe tensor to save; stored and contiguous.
pathThe filesystem path for the binary file.
Exceptions
std::runtime_errorwhen a write fails; the partial file is removed.
void load_bin(Tensor &t, const std::string &path)Raw binary load (no header), after running the recorded graph.
Parameters
tThe target tensor to load data into.
pathThe filesystem path of the binary file.
void print(const Tensor &t, int precision=4, int edge_items=3)Print tensor content to the console in a readable format.
Parameters
tThe tensor to print.
precisionNumber of decimal places for numeric display.
edge_itemsNumber of items to show at the edges of each dimension.
std::string to_string(const Tensor &t, int precision=4, int edge_items=3)Returns the tensor's content as a string in the format print() uses.
Parameters
tAny stored view; its elements are read through its strides.
precisionNumber of decimal places for numeric display.
edge_itemsNumber of items shown at the edges of each dimension.

