Repository: ggerganov/llama.cpp
File: tests/test-backend-ops.cpp (C++)

Code before:
        return out;
    }

    void initialize_tensors(ggml_context * ctx) override {
        std::random_device rd;
        std::default_random_engine rng(rd());
        for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
            if (t->type == GGML_TYPE_I64 || t->type == GGML_TYPE_I32) {
                if (ggml_is_view_op(t->op)) {
                    continue;
                }

                for (int i2 = 0; i2 < t->ne[2]; i2++) {
                    for (int i1 = 0; i1 < t->ne[1]; i1++) {
                        // generate a shuffled subset of row indices
                        std::vector<int64_t> data(ne[1]);
                        for (int i = 0; i < ne[1]; i++) {
                            data[i] = i;
                        }
                        std::shuffle(data.begin(), data.end(), rng);
                        data.resize(t->ne[0]);

                        const size_t offs = i1*t->nb[1] + i2*t->nb[2];
                        if (t->type == GGML_TYPE_I32) {
                            // TODO: Make a template or something
                            std::vector<int32_t> data_i32(ne[1]);
                            for (int i = 0; i < t->ne[0]; i++) {
                                data_i32[i] = static_cast<int32_t>(data[i]);
                            }
                            ggml_backend_tensor_set(t, data_i32.data(), offs, t->ne[0]*sizeof(int32_t));
                        } else {
                            ggml_backend_tensor_set(t, data.data(), offs, t->ne[0]*sizeof(int64_t));
                        }
                    }
                }
            } else {
                init_tensor_uniform(t);
            }
        }
    }
};

// GGML_OP_ARGMAX
struct test_argmax : public test_case {
    const ggml_type type;
    const std::array<int64_t, 4> ne;

    std::string vars() override {
        return VARS_TO_STR2(type, ne);
    }


Review comment (suggestion):
```suggestion
                            std::vector<int32_t> data_i32(t->ne[0]);
```

Code after:
        return out;
    }

    void initialize_tensors(ggml_context * ctx) override {
        std::random_device rd;
        std::default_random_engine rng(rd());
        for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
            if (t->type == GGML_TYPE_I64 || t->type == GGML_TYPE_I32) {
                if (ggml_is_view_op(t->op)) {
                    continue;
                }

                for (int i2 = 0; i2 < t->ne[2]; i2++) {
                    for (int i1 = 0; i1 < t->ne[1]; i1++) {
                        // generate a shuffled subset of row indices
                        std::vector<int64_t> data(ne[1]);
                        for (int i = 0; i < ne[1]; i++) {
                            data[i] = i;
                        }
                        std::shuffle(data.begin(), data.end(), rng);
                        data.resize(t->ne[0]);

                        const size_t offs = i1*t->nb[1] + i2*t->nb[2];
                        if (t->type == GGML_TYPE_I32) {
                            // TODO: Make a template or something
                            std::vector<int32_t> data_i32(t->ne[0]);
                            for (int i = 0; i < t->ne[0]; i++) {
                                data_i32[i] = static_cast<int32_t>(data[i]);
                            }
                            ggml_backend_tensor_set(t, data_i32.data(), offs, t->ne[0]*sizeof(int32_t));
                        } else {
                            ggml_backend_tensor_set(t, data.data(), offs, t->ne[0]*sizeof(int64_t));
                        }
                    }
                }
            } else {
                init_tensor_uniform(t);
            }
        }
    }
};

// GGML_OP_ARGMAX
struct test_argmax : public test_case {
    const ggml_type type;
    const std::array<int64_t, 4> ne;

    std::string vars() override {
        return VARS_TO_STR2(type, ne);
    }