Loading...
Searching...
No Matches
tensor_utilities.h
Go to the documentation of this file.
28[[nodiscard]] inline Index get_aligned_size(Index size) { return align_up(size, ALIGN_ELEMENTS); }
29[[nodiscard]] inline Index get_aligned_bytes(Index n_bytes) { return align_up(n_bytes, ALIGN_BYTES); }
30[[nodiscard]] inline Index get_aligned_bytes(Index count, Type dtype) { return get_aligned_bytes(count * type_bytes(dtype)); }
73 Index& back() { if (rank == 0) throw runtime_error("Shape::back() on empty"); return dims[rank - 1]; }
74 [[nodiscard]] const Index& back() const { if (rank == 0) throw runtime_error("Shape::back() on empty"); return dims[rank - 1]; }
79 [[nodiscard]] Index dim_or_zero(size_t i) const noexcept { return i < rank ? dims[i] : Index(0); }
158[[nodiscard]] inline Index get_aligned_bytes(const vector<vector<TensorSpec>>& specs, Type dtype)
249 explicit Buffer(Device new_device_type = Device::CPU) noexcept : device_type(new_device_type) {}
270 if (device_type == Device::CUDA) { void* device_pointer = nullptr; CHECK_CUDA(cudaMalloc(&device_pointer, byte_count)); return device_pointer; }
444 descriptor_handle = shared_ptr<cudnnTensorStruct>(raw_desc, [](cudnnTensorDescriptor_t descriptor) {
512 static cudnnOpTensorDescriptor_t get_operator_sum_descriptor() { return instance().operator_sum_descriptor; }
Process-wide singleton that owns the thread pool and the cuBLAS/cuDNN handles.
Definition tensor_utilities.h:491
static cudaStream_t get_compute_stream()
Default CUDA stream used by the compute backend.
Definition tensor_utilities.h:510
static cudnnHandle_t get_cudnn_handle()
Shared cuDNN handle.
Definition tensor_utilities.h:508
static cublasHandle_t get_cublas_handle()
Shared cuBLAS handle for legacy GEMM calls.
Definition tensor_utilities.h:504
static cublasLtHandle_t get_cublas_lt_handle()
Shared cuBLASLt handle for batched/tuned GEMMs.
Definition tensor_utilities.h:506
static cudnnOpTensorDescriptor_t get_operator_sum_descriptor()
cuDNN op-tensor descriptor configured for elementwise sum.
Definition tensor_utilities.h:512
ThreadPoolDevice * get_thread_pool_device()
Returns the Eigen ThreadPoolDevice used for CPU tensor evaluations.
void set_threads_number(int num_threads)
Reconfigures the underlying thread pool to use num_threads workers.
Definition adaptive_moment_estimation.h:14
size_t hash_combine(const Vs &... values)
Boost-style hash combine that mixes any number of hashable values into a single size_t.
Definition tensor_utilities.h:482
constexpr cudaDataType_t CUDA_REDUCTION_DTYPE
Definition tensor_utilities.h:37
Index type_bytes(Type type) noexcept
Returns the byte size of one element of the given OpenNN Type.
Definition configuration.h:111
constexpr cublasComputeType_t CUBLAS_COMPUTE_DTYPE
Definition tensor_utilities.h:38
Device
Execution device selection for OpenNN runtime (auto-detected, CPU or CUDA GPU).
Definition configuration.h:17
Shape string_to_shape(const string &, const string &=" ")
Parses a separator-joined string of dimensions into a Shape.
ThreadPoolDevice & get_device()
Convenience accessor for the global Eigen ThreadPoolDevice.
Definition tensor_utilities.h:529
cudnnDataType_t to_cudnn(Type type) noexcept
Returns the cuDNN data type matching the given OpenNN Type (Auto resolves to FP32).
Definition configuration.h:83
Type
Numeric precision used for training or inference tensors.
Definition configuration.h:20
void visit_type(Type t, F &&f)
Dispatches f with the TypeInfo of the runtime Type t (must be in Supported).
Definition configuration.h:59
string shape_to_string(const Shape &, const string &=" ")
Serializes a shape as a separator-joined string of dimensions.
cudaDataType_t to_cuda(Type type) noexcept
Returns the CUDA data type matching the given OpenNN Type (Auto resolves to FP32).
Definition configuration.h:97
TensorView & view_at_slot_or(vector< TensorView > &views, const vector< size_t > &slots, size_t i, TensorView &fallback)
Definition tensor_utilities.h:456
TensorMap< Tensor< float, Rank, Layout|AlignedMax >, AlignedMax > TensorMapR
Definition pch.h:201
Buffer & operator=(Buffer &&other) noexcept
Definition tensor_utilities.h:254
bool empty() const
Returns true if no storage is allocated.
Definition tensor_utilities.h:179
Index size_in_floats() const
Capacity expressed in float elements.
Definition tensor_utilities.h:177
void swap(Buffer &other) noexcept
Swaps storage with another buffer.
Definition tensor_utilities.h:259
T * ensure(Index n_elements)
Ensures the buffer holds at least n_elements of T and returns a typed pointer.
Definition tensor_utilities.h:202
void grow_to(Index new_bytes)
Grows the buffer to at least new_bytes; no-op if already large enough.
Definition tensor_utilities.h:194
void setZero()
Zeros all bytes in the buffer (cudaMemset on device, memset on host).
Definition tensor_utilities.h:209
Buffer(Device new_device_type=Device::CPU) noexcept
Constructs an empty buffer targeting the given device type.
Definition tensor_utilities.h:249
Buffer & operator=(const Buffer &)=delete
Buffer(const Buffer &)=delete
void resize_bytes(Index new_bytes, Device new_device_type)
Resizes the buffer to new_bytes on new_device_type, freeing prior storage.
Definition tensor_utilities.h:182
T * as()
Reinterprets the buffer as a typed pointer (no bounds checking).
Definition tensor_utilities.h:172
const T * as() const
Reinterprets the buffer as a typed const pointer.
Definition tensor_utilities.h:174
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42
Shape() noexcept=default
friend ostream & operator<<(ostream &os, const Shape &shape)
Definition tensor_utilities.h:92
Index dim_or_zero(size_t i) const noexcept
Returns dims[i] when i is in range, otherwise 0.
Definition tensor_utilities.h:79
void push_back(Index value) noexcept
Appends a dimension to the shape (silently no-op if already at MaxRank).
Definition tensor_utilities.h:90
Shape(initializer_list< Index > list)
Builds a shape from a brace-enclosed list of dimensions.
Definition tensor_utilities.h:60
const Index & operator[](size_t i) const noexcept
Definition tensor_utilities.h:70
bool operator==(const Shape &other) const noexcept
Definition tensor_utilities.h:100
void clear() noexcept
Resets the shape to rank 0 without freeing storage.
Definition tensor_utilities.h:88
Shape & append(const Shape &other)
Appends another shape's dimensions to this one, stopping at MaxRank.
Definition tensor_utilities.h:106
Index size() const noexcept
Returns the number of elements (product of all dimensions).
Definition tensor_utilities.h:82
Lightweight description of a tensor's shape and data type (no storage attached).
Definition tensor_utilities.h:117
Non-owning view over a tensor: pointer, shape, and data type with rich reshape helpers.
Definition tensor_utilities.h:293
MatrixMap as_matrix() const
Maps the view to an Eigen matrix: rows = first dim, cols = product of the rest.
Definition tensor_utilities.h:349
bool empty() const noexcept
Returns true if the shape is empty.
Definition tensor_utilities.h:315
void setZero()
Zeros every element of the view.
Definition tensor_utilities.h:411
Index size() const noexcept
Total element count.
Definition tensor_utilities.h:309
Index byte_size() const noexcept
Total byte count (size() * sizeof(dtype)).
Definition tensor_utilities.h:312
MatrixMap as_flat_matrix() const
Maps the view to an Eigen matrix flattened across all leading dimensions.
Definition tensor_utilities.h:365
void fill(float value)
Sets every element of the view to the given value, dispatching CPU/GPU as needed.
Definition tensor_utilities.h:534
TensorMapR< Rank > as_tensor(Index batch_index) const
Maps a single batch slice of the view to an Eigen Tensor of rank Rank.
Definition tensor_utilities.h:399
Index get_rank() const noexcept
Number of dimensions in the view.
Definition tensor_utilities.h:306
VectorMap as_vector() const
Maps the view to a flat Eigen vector.
Definition tensor_utilities.h:382
cudaDataType_t cuda_dtype() const noexcept
Returns the CUDA data type tag corresponding to this view's dtype.
Definition tensor_utilities.h:332
float * as_float() const noexcept
Reinterprets the view's data as a float pointer.
Definition tensor_utilities.h:326
TensorView reshape(const Shape &new_shape) const
Returns a new view over the same memory with a different shape.
Definition tensor_utilities.h:345
T * as() const noexcept
Reinterprets the view's data as a pointer to T (no type checking).
Definition tensor_utilities.h:319
TensorView(void *new_data=nullptr, const Shape &new_shape={}, Type new_dtype=Type::FP32) noexcept
Constructs a view from an external buffer, shape, and dtype.
Definition tensor_utilities.h:301
TensorMapR< Rank > as_tensor() const
Maps the view to an Eigen Tensor of the given rank.
Definition tensor_utilities.h:389
void dispatch(F &&fn) const
Dispatches a callable on the concrete element type (FP32 or BF16).
Definition tensor_utilities.h:336
MatrixMap as_matrix(Index batch_index) const
Maps a single batch slice of the view to an Eigen matrix.
Definition tensor_utilities.h:356
MatrixMap as_flat_matrix(Index batch_index) const
Flat-matrix view of a single batch slice.
Definition tensor_utilities.h:373