MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / tensor_to_float

Function tensor_to_float

tests/test-backend-ops.cpp:231–275  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

229}
230
231static std::vector<float> tensor_to_float(const ggml_tensor * t) {
232 std::vector<float> tv;
233 tv.reserve(ggml_nelements(t));
234
235 std::vector<uint8_t> buf(ggml_nbytes(t));
236 ggml_backend_tensor_get(t, buf.data(), 0, ggml_nbytes(t));
237
238 const auto * tt = ggml_get_type_traits(t->type);
239 size_t bs = ggml_blck_size(t->type);
240 std::vector<float> vq(ggml_blck_size(t->type));
241 bool quantized = ggml_is_quantized(t->type);
242
243 // access elements by index to avoid gaps in views
244 for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
245 for (int64_t i2 = 0; i2 < t->ne[2]; i2++) {
246 for (int64_t i1 = 0; i1 < t->ne[1]; i1++) {
247 for (int64_t i0 = 0; i0 < t->ne[0]; i0 += bs) {
248 size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0];
249 if (t->type == GGML_TYPE_F16) {
250 tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i]));
251 } else if (t->type == GGML_TYPE_BF16) {
252 tv.push_back(ggml_bf16_to_fp32(*(ggml_bf16_t*)&buf[i]));
253 } else if (t->type == GGML_TYPE_F32) {
254 tv.push_back(*(float *) &buf[i]);
255 } else if (t->type == GGML_TYPE_I64) {
256 tv.push_back((float)*(int64_t *) &buf[i]);
257 } else if (t->type == GGML_TYPE_I32) {
258 tv.push_back((float)*(int32_t *) &buf[i]);
259 } else if (t->type == GGML_TYPE_I16) {
260 tv.push_back((float)*(int16_t *) &buf[i]);
261 } else if (t->type == GGML_TYPE_I8) {
262 tv.push_back((float)*(int8_t *) &buf[i]);
263 } else if (quantized) {
264 tt->to_float(&buf[i], vq.data(), bs);
265 tv.insert(tv.end(), vq.begin(), vq.end());
266 } else {
267 GGML_ABORT("fatal error");
268 }
269 }
270 }
271 }
272 }
273
274 return tv;
275}
276
277// normalized mean squared error = mse(a, b) / mse(a, 0)
278static double nmse(const float * a, const float * b, size_t n) {

Callers 2

evalMethod · 0.85
eval_gradMethod · 0.85

Calls 13

ggml_nelementsFunction · 0.85
ggml_nbytesFunction · 0.85
ggml_backend_tensor_getFunction · 0.85
ggml_get_type_traitsFunction · 0.85
ggml_blck_sizeFunction · 0.85
ggml_is_quantizedFunction · 0.85
ggml_fp16_to_fp32Function · 0.85
ggml_bf16_to_fp32Function · 0.85
endMethod · 0.80
dataMethod · 0.45
push_backMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected