31 [[nodiscard]]
inline std::string
shape_text(
const std::vector<uint64_t>& shape)
33 std::string text =
"[";
34 for (
size_t i = 0; i < shape.size(); ++i) {
35 text += (i > 0 ?
", " :
"") + std::to_string(shape[i]);
40 [[nodiscard]]
inline size_t element_count(
const std::vector<uint64_t>& shape)
43 for (
const uint64_t extent : shape) {
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);
64 if (tensor->spec().shape != need.shape) {
65 return std::format(
"tensor '{}' has shape {} but {} is required",
86 [[nodiscard]] uint64_t
address(uint64_t buffer,
size_t index)
const noexcept
88 return buffer +
offsets[index] *
sizeof(float);
98[[nodiscard]]
inline std::expected<PackedWeights, std::string>
pack_weights(
101 if (std::string reason =
check_tensors(weights, required); !reason.empty()) {
102 return std::unexpected(std::move(reason));
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;
111 values = tensor->to_vector<
float>();
112 }
else if (
const auto view = tensor->access()) {
115 return std::unexpected(std::format(
"tensor '{}' cannot become float32: {}", need.name, converted.error()));
117 values = converted->to_vector<
float>();
120 return std::unexpected(std::format(
"tensor '{}' could not be read as float32", need.name));
123 packed.
values.insert(packed.
values.end(), values->begin(), values->end());
137 if (packed.
values.empty()) {
143 packed.
values.size() *
sizeof(
float));
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.
Named tensors that belong together, such as a model's weights or I/O.
enum MayaFlux::Portal::Graphics::GpuBufferBinding::ElementType FLOAT32
std::vector< float > values
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.
std::vector< uint64_t > shape
One tensor a network needs.