MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Yantra/Executors/TensorKernels/Weights.hpp
Go to the documentation of this file.
1#pragma once
2
6
7/**
8 * @file Weights.hpp
9 * @brief Checking a set of tensors against what a network needs, and packing it into one float32 buffer.
10 *
11 * A network built from the tensor kernels lists the tensors it needs by name
12 * and shape, in the order its layers use them. check_tensors says whether a
13 * set holds them, pack_weights converts them to float32 and lays them out one
14 * after another, and upload_packed_weights puts the result on a context, where
15 * PackedWeights::address gives each tensor's device address for a stage.
16 */
17
19
20/**
21 * @struct TensorRequirement
22 * @brief One tensor a network needs.
23 */
25 std::string name;
26 std::vector<uint64_t> shape;
27};
28
29namespace detail {
30
31 [[nodiscard]] inline std::string shape_text(const std::vector<uint64_t>& shape)
32 {
33 std::string text = "[";
34 for (size_t i = 0; i < shape.size(); ++i) {
35 text += (i > 0 ? ", " : "") + std::to_string(shape[i]);
36 }
37 return text + "]";
38 }
39
40 [[nodiscard]] inline size_t element_count(const std::vector<uint64_t>& shape)
41 {
42 size_t count = 1;
43 for (const uint64_t extent : shape) {
44 count *= extent;
45 }
46 return count;
47 }
48
49}
50
51/**
52 * @brief Whether a set holds every required tensor with the required shape.
53 * @param weights Candidate tensors.
54 * @param required What the network needs.
55 * @return Empty when it does, otherwise the first missing tensor or shape mismatch, in the order of required.
56 */
57[[nodiscard]] inline std::string check_tensors(const Kakshya::TensorSet& weights, std::span<const TensorRequirement> required)
58{
59 for (const auto& need : required) {
60 const auto* tensor = weights.find(need.name);
61 if (tensor == nullptr) {
62 return std::format("missing tensor '{}'", need.name);
63 }
64 if (tensor->spec().shape != need.shape) {
65 return std::format("tensor '{}' has shape {} but {} is required",
66 need.name, detail::shape_text(tensor->spec().shape), detail::shape_text(need.shape));
67 }
68 }
69 return {};
70}
71
72/**
73 * @struct PackedWeights
74 * @brief The required tensors as float32 in one run, in the order they were required.
75 */
77 std::vector<float> values;
78 /** Element offset of each required tensor in values. */
79 std::vector<size_t> offsets;
80
81 /**
82 * @brief Device address of one tensor, given the address of the uploaded buffer.
83 * @param buffer Device address of the buffer upload_packed_weights filled.
84 * @param index Position of the tensor in the requirements.
85 */
86 [[nodiscard]] uint64_t address(uint64_t buffer, size_t index) const noexcept
87 {
88 return buffer + offsets[index] * sizeof(float);
89 }
90};
91
92/**
93 * @brief Convert the required tensors to float32 and lay them out in the order of required.
94 * @param weights Tensors to read. Other element types than float32 are converted.
95 * @param required What the network needs.
96 * @return The packed values, or the reason: what check_tensors says, or the tensor that cannot become float32.
97 */
98[[nodiscard]] inline std::expected<PackedWeights, std::string> pack_weights(
99 const Kakshya::TensorSet& weights, std::span<const TensorRequirement> required)
100{
101 if (std::string reason = check_tensors(weights, required); !reason.empty()) {
102 return std::unexpected(std::move(reason));
103 }
104
105 PackedWeights packed;
106 packed.offsets.reserve(required.size());
107 for (const auto& need : required) {
108 const auto* tensor = weights.find(need.name);
109 std::optional<std::vector<float>> values;
110 if (tensor->spec().dtype == Kakshya::TensorDType::of<float>()) {
111 values = tensor->to_vector<float>();
112 } else if (const auto view = tensor->access()) {
114 if (!converted) {
115 return std::unexpected(std::format("tensor '{}' cannot become float32: {}", need.name, converted.error()));
116 }
117 values = converted->to_vector<float>();
118 }
119 if (!values || values->size() != detail::element_count(need.shape)) {
120 return std::unexpected(std::format("tensor '{}' could not be read as float32", need.name));
121 }
122 packed.offsets.push_back(packed.values.size());
123 packed.values.insert(packed.values.end(), values->begin(), values->end());
124 }
125 return packed;
126}
127
128/**
129 * @brief Allocate a shared buffer on a context and fill it with packed weights.
130 * @param context The context whose shared buffer holds the weights.
131 * @param set Descriptor set of the buffer.
132 * @param binding Binding of the buffer.
133 * @param packed The weights. The buffer is device local, so it is read by the kernels and not from the host.
134 */
135inline void upload_packed_weights(GpuDispatchCore& context, uint32_t set, size_t binding, const PackedWeights& packed)
136{
137 if (packed.values.empty()) {
138 return;
139 }
140 context.ensure_shared_buffer(set, binding, packed.values.size(), GpuBufferBinding::ElementType::FLOAT32,
142 context.upload_shared_raw(set, binding, reinterpret_cast<const uint8_t*>(packed.values.data()),
143 packed.values.size() * sizeof(float));
144}
145
146} // namespace MayaFlux::Yantra::TensorKernels
void ensure_shared_buffer(uint32_t set, size_t binding_index, size_t element_count, GpuBufferBinding::ElementType element_type, Portal::Graphics::BufferUsageHint usage_hint=Portal::Graphics::BufferUsageHint::COMPUTE_STORAGE)
Ensure a shared storage buffer of at least element_count elements exists at (set, binding_index).
void upload_shared_raw(uint32_t set, size_t binding_index, const uint8_t *data, size_t byte_size)
Non-template base that owns all type-independent GPU dispatch logic.
std::expected< TensorData, std::string > convert_tensor(const TensorAccess &source, TensorDType target, const std::optional< TensorQuantization > &quantization)
Convert the elements of a tensor to another element type.
@ COMPUTE
Storage buffer for compute shaders (device-local).
size_t element_count(const std::vector< uint64_t > &shape)
std::string shape_text(const std::vector< uint64_t > &shape)
std::string check_tensors(const Kakshya::TensorSet &weights, std::span< const TensorRequirement > required)
Whether a set holds every required tensor with the required shape.
std::expected< PackedWeights, std::string > pack_weights(const Kakshya::TensorSet &weights, std::span< const TensorRequirement > required)
Convert the required tensors to float32 and lay them out in the order of required.
void upload_packed_weights(GpuDispatchCore &context, uint32_t set, size_t binding, const PackedWeights &packed)
Allocate a shared buffer on a context and fill it with packed weights.
static constexpr TensorDType of() noexcept
Type for a C++ scalar or complex element.
const TensorData * find(std::string_view name) const
Tensor with this exact name, or nullptr.
Definition Tensor.cpp:176
Named tensors that belong together, such as a model's weights or I/O.
Definition Tensor.hpp:167
enum MayaFlux::Portal::Graphics::GpuBufferBinding::ElementType FLOAT32
uint64_t address(uint64_t buffer, size_t index) const noexcept
Device address of one tensor, given the address of the uploaded buffer.
std::vector< size_t > offsets
Element offset of each required tensor in values.
The required tensors as float32 in one run, in the order they were required.