Skip to content

Commit 5a508b0

Browse files
committed
improve f64 support (for convert mostly)
1 parent 6d84a30 commit 5a508b0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

model.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,12 @@ void convert_tensor(void* src,
835835
} else if (src_type == GGML_TYPE_F32) {
836836
if (dst_type == GGML_TYPE_F16) {
837837
ggml_fp32_to_fp16_row((float*)src, (ggml_fp16_t*)dst, n);
838+
} else if (dst_type == GGML_TYPE_F64) {
839+
double* ddst = (double*)dst;
840+
float* fsrc = (float*)src;
841+
for (int64_t i = 0; i < n; i++) {
842+
ddst[i] = (double)(fsrc[i]);
843+
}
838844
} else {
839845
std::vector<float> imatrix(n_per_row, 1.0f); // dummy importance matrix
840846
const float* im = imatrix.data();
@@ -843,6 +849,41 @@ void convert_tensor(void* src,
843849
} else if (dst_type == GGML_TYPE_F32) {
844850
if (src_type == GGML_TYPE_F16) {
845851
ggml_fp16_to_fp32_row((ggml_fp16_t*)src, (float*)dst, n);
852+
} else if (src_type == GGML_TYPE_F64) {
853+
float* fdst = (float*)dst;
854+
double* dsrc = (double*)src;
855+
for (int64_t i = 0; i < n; i++) {
856+
fdst[i] = (float)(dsrc[i]);
857+
}
858+
} else {
859+
auto qtype = ggml_get_type_traits(src_type);
860+
if (qtype->to_float == NULL) {
861+
throw std::runtime_error(format("type %s unsupported for integer quantization: no dequantization available",
862+
ggml_type_name(src_type)));
863+
}
864+
qtype->to_float(src, (float*)dst, n);
865+
}
866+
} else if (src_type == GGML_TYPE_F64) {
867+
if (dst_type == GGML_TYPE_F16) {
868+
// ggml_fp32_to_fp16_row((float*)src, (ggml_fp16_t*)dst, n);
869+
ggml_fp16_t* fdst = (ggml_fp16_t*)dst;
870+
double* dsrc = (double*)src;
871+
for (int64_t i = 0; i < n; i++) {
872+
fdst[i] = ggml_fp32_to_fp16((float)dsrc[i]);
873+
}
874+
} else {
875+
std::vector<float> imatrix(n_per_row, 1.0f); // dummy importance matrix
876+
const float* im = imatrix.data();
877+
ggml_quantize_chunk(dst_type, (float*)src, dst, 0, nrows, n_per_row, im);
878+
}
879+
} else if (dst_type == GGML_TYPE_F64) {
880+
if (src_type == GGML_TYPE_F16) {
881+
// ggml_fp16_to_fp32_row((ggml_fp16_t*)src, (float*)dst, n);
882+
double* ddst = (double*)dst;
883+
ggml_fp16_t* fsrc = (ggml_fp16_t*)src;
884+
for (int64_t i = 0; i < n; i++) {
885+
ddst[i] = (double)ggml_fp16_to_fp32(fsrc[i]);
886+
}
846887
} else {
847888
auto qtype = ggml_get_type_traits(src_type);
848889
if (qtype->to_float == NULL) {

0 commit comments

Comments
 (0)