Convert the required tensors to float32 and lay them out in the order of 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;
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 }
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.