MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ pack_weights()

std::expected< PackedWeights, std::string > MayaFlux::Yantra::TensorKernels::pack_weights ( const Kakshya::TensorSet & weights,
std::span< const TensorRequirement > required )
inlinenodiscard

Convert the required tensors to float32 and lay them out in the order of required.

Parameters
weightsTensors to read. Other element types than float32 are converted.
requiredWhat the network needs.
Returns
The packed values, or the reason: what check_tensors says, or the tensor that cannot become float32.

Definition at line 98 of file Yantra/Executors/TensorKernels/Weights.hpp.

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}
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.
size_t element_count(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.
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

References check_tensors(), MayaFlux::Kakshya::convert_tensor(), MayaFlux::Yantra::TensorKernels::detail::element_count(), MayaFlux::Kakshya::TensorSet::find(), MayaFlux::Kakshya::TensorDType::of(), MayaFlux::Yantra::TensorKernels::PackedWeights::offsets, and MayaFlux::Yantra::TensorKernels::PackedWeights::values.

Referenced by MayaFlux::Yantra::SileroVadModel::SileroVadModel().

Here is the call graph for this function:
Here is the caller graph for this function: