MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / tensor_to_float

Function tensor_to_float

smallthinker/tests/test-backend-ops.cpp:130–174  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128}
129
130static std::vector<float> tensor_to_float(const ggml_tensor * t) {
131 std::vector<float> tv;
132 tv.reserve(ggml_nelements(t));
133
134 std::vector<uint8_t> buf(ggml_nbytes(t));
135 ggml_backend_tensor_get(t, buf.data(), 0, ggml_nbytes(t));
136
137 const auto * tt = ggml_get_type_traits(t->type);
138 size_t bs = ggml_blck_size(t->type);
139 std::vector<float> vq(ggml_blck_size(t->type));
140 bool quantized = ggml_is_quantized(t->type);
141
142 // access elements by index to avoid gaps in views
143 for (int64_t i3 = 0; i3 < t->ne[3]; i3++) {
144 for (int64_t i2 = 0; i2 < t->ne[2]; i2++) {
145 for (int64_t i1 = 0; i1 < t->ne[1]; i1++) {
146 for (int64_t i0 = 0; i0 < t->ne[0]; i0 += bs) {
147 size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0];
148 if (t->type == GGML_TYPE_F16) {
149 tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i]));
150 } else if (t->type == GGML_TYPE_BF16) {
151 tv.push_back(ggml_bf16_to_fp32(*(ggml_bf16_t*)&buf[i]));
152 } else if (t->type == GGML_TYPE_F32) {
153 tv.push_back(*(float *) &buf[i]);
154 } else if (t->type == GGML_TYPE_I64) {
155 tv.push_back((float)*(int64_t *) &buf[i]);
156 } else if (t->type == GGML_TYPE_I32) {
157 tv.push_back((float)*(int32_t *) &buf[i]);
158 } else if (t->type == GGML_TYPE_I16) {
159 tv.push_back((float)*(int16_t *) &buf[i]);
160 } else if (t->type == GGML_TYPE_I8) {
161 tv.push_back((float)*(int8_t *) &buf[i]);
162 } else if (quantized) {
163 tt->to_float(&buf[i], vq.data(), bs);
164 tv.insert(tv.end(), vq.begin(), vq.end());
165 } else {
166 GGML_ABORT("fatal error");
167 }
168 }
169 }
170 }
171 }
172
173 return tv;
174}
175
176// normalized mean squared error = mse(a, b) / mse(a, 0)
177static double nmse(const float * a, const float * b, size_t n) {

Callers 2

evalMethod · 0.85
eval_gradMethod · 0.85

Calls 14

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

Tested by

no test coverage detected