Skip to main content

src/DataObjects/Tensors/storage/TensorStorageBase.hpp

src/DataObjects/Tensors/storage/TensorStorageBase.hpp

CRTP base class and cache structure for tensor storage backends. More…

Classes

Name
struct TensorStorageCache
Cache structure for fast-path access to tensor storage.
class TensorStorageBase
CRTP base class for tensor storage implementations.

Types

Name
enum class TensorStorageType { Armadillo, Dense, LibTorch, View, Lazy, MemoryMapped}
Storage type enumeration for tensor storage backends.

Detailed Description

CRTP base class and cache structure for tensor storage backends.

See: tensor_data_refactor_proposal.md §6.1 for design rationale.

Defines the compile-time polymorphic interface that all tensor storage backends (Armadillo, Dense, LibTorch, View, Lazy) implement. The CRTP pattern eliminates virtual dispatch overhead while TensorStorageWrapper provides type-erased runtime polymorphism on top.

Also defines TensorStorageCache for fast-path contiguous data access and TensorStorageType for backend identification.

Part of the TensorData refactor (step 2).

Types Documentation

enum TensorStorageType

Enumerator Value Description
Armadillo arma::fvec / arma::fmat / arma::fcube (≤3D, always available)
Dense Flat std::vector + shape (>3D fallback).
LibTorch at::Tensor (optional, behind #ifdef)
View Zero-copy slice of another storage.
Lazy Lazily computed columns (transforms v2 pipelines).
MemoryMapped Block-cached memory-mapped interleaved binary (Phase 2).

Storage type enumeration for tensor storage backends.

Each concrete tensor storage implementation reports its type via getStorageType(). This is used for logging, debugging, and by the type-erasure wrapper’s tryGetAs() method.

Source code

#ifndef TENSOR_STORAGE_BASE_HPP
#define TENSOR_STORAGE_BASE_HPP


#include <cstddef>
#include <span>
#include <vector>

enum class TensorStorageType {
    Armadillo,  
    Dense,      
    LibTorch,   
    View,       
    Lazy,       
    MemoryMapped
};

struct TensorStorageCache {
    float const * data_ptr = nullptr;
    std::size_t total_elements = 0;  
    std::vector<std::size_t> shape;  
    std::vector<std::size_t> strides;
    bool is_valid = false;           

    [[nodiscard]] constexpr bool isValid() const noexcept { return is_valid; }
};

template<typename Derived>
class TensorStorageBase {
public:
    // ========== Element Access ==========

    [[nodiscard]] float getValueAt(std::span<std::size_t const> indices) const {
        return self().getValueAtImpl(indices);
    }

    // ========== Bulk Access ==========

    [[nodiscard]] std::span<float const> flatData() const {
        return self().flatDataImpl();
    }

    // ========== Slicing ==========

    [[nodiscard]] std::vector<float> sliceAlongAxis(
            std::size_t axis, std::size_t index) const {
        return self().sliceAlongAxisImpl(axis, index);
    }

    [[nodiscard]] std::vector<float> getColumn(std::size_t col) const {
        return self().getColumnImpl(col);
    }

    // ========== Metadata ==========

    [[nodiscard]] std::vector<std::size_t> shape() const {
        return self().shapeImpl();
    }

    [[nodiscard]] std::size_t totalElements() const {
        return self().totalElementsImpl();
    }

    [[nodiscard]] bool isContiguous() const {
        return self().isContiguousImpl();
    }

    // ========== Storage Type ==========

    [[nodiscard]] TensorStorageType getStorageType() const {
        return self().getStorageTypeImpl();
    }

    // ========== Cache Optimization ==========

    [[nodiscard]] TensorStorageCache tryGetCache() const {
        return self().tryGetCacheImpl();
    }

protected:
    ~TensorStorageBase() = default;

private:
    [[nodiscard]] Derived const & self() const {
        return static_cast<Derived const &>(*this);
    }
    [[nodiscard]] Derived & self() {
        return static_cast<Derived &>(*this);
    }
};

#endif// TENSOR_STORAGE_BASE_HPP

Updated on 2026-11-13 at 20:25:43 +0000