| 200 | return bytes; |
| 201 | } |
| 202 | |
| 203 | void set_tensor_bytes(ggml_tensor * tensor, const void * data, size_t bytes, std::string_view name) { |
| 204 | if (tensor == nullptr) { |
| 205 | throw std::runtime_error("cannot upload to a null backend tensor: " + std::string(name)); |
| 206 | } |
| 207 | if (bytes != ggml_nbytes(tensor)) { |
| 208 | throw std::runtime_error("backend tensor byte size mismatch for " + std::string(name)); |
| 209 | } |
| 210 | ggml_backend_tensor_set(tensor, data, 0, bytes); |
| 211 | } |
| 212 | |
| 213 | std::vector<float> decode_tensor_data_f32(std::string_view name, const TensorData & tensor) { |
| 214 | if (tensor.type == GGML_TYPE_F32) { |
| 215 | if (tensor.bytes.size() != static_cast<size_t>(tensor.shape.num_elements()) * sizeof(float)) { |
| 216 | throw std::runtime_error("invalid F32 tensor byte size: " + std::string(name)); |
| 217 | } |
| 218 | std::vector<float> values(static_cast<size_t>(tensor.shape.num_elements())); |
| 219 | std::memcpy(values.data(), tensor.bytes.data(), tensor.bytes.size()); |
| 220 | return values; |
| 221 | } |
| 222 | if (tensor.type == GGML_TYPE_F16) { |
| 223 | if (tensor.bytes.size() != static_cast<size_t>(tensor.shape.num_elements()) * sizeof(ggml_fp16_t)) { |
| 224 | throw std::runtime_error("invalid F16 tensor byte size: " + std::string(name)); |
| 225 | } |
| 226 | std::vector<float> values(static_cast<size_t>(tensor.shape.num_elements())); |
| 227 | ggml_fp16_to_fp32_row( |
no test coverage detected