Skip to content

Commit 2741a99

Browse files
committed
Apply ggerganov's fixes for test-backend-ops
1 parent bc278c8 commit 2741a99

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

ggml-metal.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ static bool ggml_metal_supports_op(const struct ggml_metal_context * ctx, const
803803
case GGML_OP_DIAG_MASK_INF:
804804
case GGML_OP_GET_ROWS:
805805
{
806-
return op->ne[3] == 1;
806+
return op->src[0]->type != GGML_TYPE_BF16 && op->ne[3] == 1;
807807
}
808808
default:
809809
return false;

ggml.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -19765,7 +19765,10 @@ struct ggml_cplan ggml_graph_plan(const struct ggml_cgraph * cgraph, int n_threa
1976519765
case GGML_OP_CPY:
1976619766
case GGML_OP_DUP:
1976719767
{
19768-
if (ggml_is_quantized(node->type)) {
19768+
if (ggml_is_quantized(node->type) ||
19769+
// F16 -> BF16 and BF16 -> F16 copies go through intermediate F32
19770+
(node->src[0]->type == GGML_TYPE_F16 && node->src[1] && node->src[1]->type == GGML_TYPE_BF16) ||
19771+
(node->src[0]->type == GGML_TYPE_BF16 && node->src[1] && node->src[1]->type == GGML_TYPE_F16)) {
1976919772
cur = ggml_type_size(GGML_TYPE_F32) * node->ne[0] * n_tasks;
1977019773
}
1977119774
} break;

gguf-py/gguf/constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,6 @@ def get_type(val: Any) -> GGUFValueType:
863863
GGML_QUANT_SIZES = {
864864
GGMLQuantizationType.F32: (1, 4),
865865
GGMLQuantizationType.F16: (1, 2),
866-
GGMLQuantizationType.BF16: (1, 2),
867866
GGMLQuantizationType.Q4_0: (32, 2 + 16),
868867
GGMLQuantizationType.Q4_1: (32, 2 + 2 + 16),
869868
GGMLQuantizationType.Q5_0: (32, 2 + 4 + 16),
@@ -890,6 +889,7 @@ def get_type(val: Any) -> GGUFValueType:
890889
GGMLQuantizationType.I64: (1, 8),
891890
GGMLQuantizationType.F64: (1, 8),
892891
GGMLQuantizationType.IQ1_M: (256, QK_K // 8 + QK_K // 16 + QK_K // 32),
892+
GGMLQuantizationType.BF16: (1, 2),
893893
}
894894

895895

tests/test-backend-ops.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float m
5050

5151
if (tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_I32) {
5252
ggml_backend_tensor_set(tensor, data.data(), 0, size * sizeof(float));
53-
} else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F16) {
53+
} else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) {
5454
GGML_ASSERT(size % ggml_blck_size(tensor->type) == 0);
5555
std::vector<uint8_t> dataq(ggml_row_size(tensor->type, size));
5656
std::vector<float> imatrix(tensor->ne[0], 1.0f); // dummy importance matrix
@@ -92,6 +92,8 @@ static std::vector<float> tensor_to_float(const ggml_tensor * t) {
9292
size_t i = i3*t->nb[3] + i2*t->nb[2] + i1*t->nb[1] + i0/bs*t->nb[0];
9393
if (t->type == GGML_TYPE_F16) {
9494
tv.push_back(ggml_fp16_to_fp32(*(ggml_fp16_t*)&buf[i]));
95+
} else if (t->type == GGML_TYPE_BF16) {
96+
tv.push_back(ggml_bf16_to_fp32(*(ggml_bf16_t*)&buf[i]));
9597
} else if (t->type == GGML_TYPE_F32) {
9698
tv.push_back(*(float *) &buf[i]);
9799
} else if (t->type == GGML_TYPE_I32) {
@@ -1898,7 +1900,7 @@ static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op
18981900
std::default_random_engine rng(0);
18991901

19001902
const ggml_type all_types[] = {
1901-
GGML_TYPE_F32, GGML_TYPE_F16,
1903+
GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
19021904
GGML_TYPE_Q4_0, GGML_TYPE_Q4_1,
19031905
GGML_TYPE_Q5_0, GGML_TYPE_Q5_1,
19041906
GGML_TYPE_Q8_0,

0 commit comments

Comments
 (0)