Skip to content

Commit 36c3f41

Browse files
committed
ggml : fix CPU implementation
1 parent 199f6bd commit 36c3f41

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

ggml.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10337,19 +10337,17 @@ static void ggml_compute_forward_out_prod(
1033710337
static void ggml_compute_forward_scale_f32(
1033810338
const struct ggml_compute_params * params,
1033910339
const struct ggml_tensor * src0,
10340-
const struct ggml_tensor * src1,
1034110340
struct ggml_tensor * dst) {
1034210341
GGML_ASSERT(ggml_is_contiguous(src0));
1034310342
GGML_ASSERT(ggml_is_contiguous(dst));
1034410343
GGML_ASSERT(ggml_are_same_shape(src0, dst));
10345-
GGML_ASSERT(ggml_is_scalar(src1));
1034610344

1034710345
if (params->type == GGML_TASK_INIT || params->type == GGML_TASK_FINALIZE) {
1034810346
return;
1034910347
}
1035010348

1035110349
// scale factor
10352-
const float v = *(float *) src1->data;
10350+
const float v = *(float *) dst->op_params;
1035310351

1035410352
const int ith = params->ith;
1035510353
const int nth = params->nth;
@@ -10380,12 +10378,11 @@ static void ggml_compute_forward_scale_f32(
1038010378
static void ggml_compute_forward_scale(
1038110379
const struct ggml_compute_params * params,
1038210380
const struct ggml_tensor * src0,
10383-
const struct ggml_tensor * src1,
1038410381
struct ggml_tensor * dst) {
1038510382
switch (src0->type) {
1038610383
case GGML_TYPE_F32:
1038710384
{
10388-
ggml_compute_forward_scale_f32(params, src0, src1, dst);
10385+
ggml_compute_forward_scale_f32(params, src0, dst);
1038910386
} break;
1039010387
default:
1039110388
{
@@ -14395,7 +14392,7 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
1439514392
} break;
1439614393
case GGML_OP_SCALE:
1439714394
{
14398-
ggml_compute_forward_scale(params, tensor->src[0], tensor->src[1], tensor);
14395+
ggml_compute_forward_scale(params, tensor->src[0], tensor);
1439914396
} break;
1440014397
case GGML_OP_SET:
1440114398
{

tests/test-backend-ops.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,18 +766,19 @@ struct test_bin_bcast : public test_case {
766766
struct test_scale : public test_case {
767767
const ggml_type type;
768768
const std::array<int64_t, 4> ne;
769+
float scale;
769770

770771
std::string vars() override {
771-
return VARS_TO_STR2(type, ne);
772+
return VARS_TO_STR3(type, ne, scale);
772773
}
773774

774775
test_scale(ggml_type type = GGML_TYPE_F32,
775-
std::array<int64_t, 4> ne = {10, 10, 10, 10})
776-
: type(type), ne(ne) {}
776+
std::array<int64_t, 4> ne = {10, 10, 10, 10},
777+
float scale = 2.0f)
778+
: type(type), ne(ne), scale(scale) {}
777779

778780
ggml_tensor * build_graph(ggml_context * ctx) override {
779781
ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne.data());
780-
ggml_tensor * scale = ggml_new_tensor_1d(ctx, type, 1);
781782
ggml_tensor * out = ggml_scale(ctx, a, scale);
782783
return out;
783784
}

tests/test-grad0.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,13 +887,14 @@ int main(int argc, const char ** argv) {
887887
ne2[0] = 1;
888888

889889
for (int ndims = 1; ndims <= 2; ++ndims) {
890-
x[1] = get_random_tensor_f32(ctx0, 1, ne2, -1.0f, 1.0f);
891890
x[0] = get_random_tensor_f32(ctx0, ndims, ne, -1.0f, 1.0f);
892891

892+
const float s = -1.0f + 2.0f*frand();
893+
893894
ggml_set_param(ctx0, x[0]);
894895
ggml_set_param(ctx0, x[1]);
895896

896-
struct ggml_tensor * f = ggml_sum(ctx0, ggml_scale(ctx0, x[0], x[1]));
897+
struct ggml_tensor * f = ggml_sum(ctx0, ggml_scale(ctx0, x[0], s));
897898

898899
check_gradient("scale", ctx0, x, f, ndims, nargs, 1e-3f, 1e-3f, INFINITY);
899900
}
@@ -1395,7 +1396,7 @@ int main(int argc, const char ** argv) {
13951396
ggml_add1(ctx0,
13961397
ggml_scale(ctx0,
13971398
ggml_soft_max(ctx0, x[0]),
1398-
ggml_new_f32(ctx0, 1.0f - eps)),
1399+
1.0f - eps),
13991400
ggml_new_f32(ctx0, eps))));
14001401

14011402
check_gradient("softmax", ctx0, x, f, ndims, nargs, 1e-3f, 2e-1f, INFINITY);

0 commit comments

Comments
 (0)