Skip to content

Commit 7247503

Browse files
committed
moving changes from laptop [no ci]
1 parent fed482f commit 7247503

File tree

7 files changed

+53
-88
lines changed

7 files changed

+53
-88
lines changed

ggml/include/ggml.h

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ extern "C" {
532532
GGML_OP_ARGSORT,
533533
GGML_OP_LEAKY_RELU,
534534
GGML_OP_TRI,
535-
GGML_OP_CONST,
535+
GGML_OP_FILL,
536536

537537
GGML_OP_FLASH_ATTN_EXT,
538538
GGML_OP_FLASH_ATTN_BACK,
@@ -2227,40 +2227,10 @@ extern "C" {
22272227
struct ggml_tensor * a,
22282228
enum ggml_tri_type type);
22292229

2230-
// Create a tensor of dimensions ne0, ne1, ne2, ne3 filled with the constant c
2231-
GGML_API struct ggml_tensor * ggml_const(
2230+
// Fill tensor a with constant c
2231+
GGML_API struct ggml_tensor * ggml_fill(
22322232
struct ggml_context * ctx,
2233-
int64_t ne0,
2234-
int64_t ne1,
2235-
int64_t ne2,
2236-
int64_t ne3,
2237-
float c);
2238-
2239-
// Convenience calls
2240-
GGML_API struct ggml_tensor * ggml_const_1d(
2241-
struct ggml_context * ctx,
2242-
int64_t ne0,
2243-
float c);
2244-
2245-
GGML_API struct ggml_tensor * ggml_const_2d(
2246-
struct ggml_context * ctx,
2247-
int64_t ne0,
2248-
int64_t ne1,
2249-
float c);
2250-
2251-
GGML_API struct ggml_tensor * ggml_const_3d(
2252-
struct ggml_context * ctx,
2253-
int64_t ne0,
2254-
int64_t ne1,
2255-
int64_t ne2,
2256-
float c);
2257-
2258-
GGML_API struct ggml_tensor * ggml_const_4d(
2259-
struct ggml_context * ctx,
2260-
int64_t ne0,
2261-
int64_t ne1,
2262-
int64_t ne2,
2263-
int64_t ne3,
2233+
struct ggml_tensor * a,
22642234
float c);
22652235

22662236
// Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
@@ -2431,16 +2401,26 @@ extern "C" {
24312401
struct ggml_tensor * b,
24322402
struct ggml_tensor * state);
24332403

2434-
/* Solves a specific equation of the form Ax=B, where A is a lower triangular matrix
2404+
/* Solves a specific equation of the form Ax=B, where A is a triangular matrix
2405+
* without zeroes on the diagonal (i.e. invertible).
24352406
* B can have any number of columns, but must have the same number of rows as A
24362407
* If A is [n, n] and B is [n, m], then the result will be [n, m] as well
24372408
* Has O(n^3) complexity (unlike most matrix ops out there), so use on cases
2438-
* where n > 100 sparingly, pre-chunk if necessary
2409+
* where n > 100 sparingly, pre-chunk if necessary.
2410+
*
2411+
* If left = false, solves xA=B instead
2412+
* If lower = false, assumes upper triangular instead
2413+
* If unitriangular = true, assumes diagonal of A to be all ones (will override actual values)
2414+
*
2415+
* TODO: currently only lower, right, non-unitriangular variant is implemented
24392416
*/
24402417
GGML_API struct ggml_tensor * ggml_solve_tri(
24412418
struct ggml_context * ctx,
24422419
struct ggml_tensor * a,
2443-
struct ggml_tensor * b);
2420+
struct ggml_tensor * b,
2421+
bool left,
2422+
bool lower,
2423+
bool unitriangular);
24442424

24452425
// custom operators
24462426

ggml/src/ggml-cpu/ggml-cpu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,9 +1951,9 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
19511951
{
19521952
ggml_compute_forward_tri(params, tensor);
19531953
} break;
1954-
case GGML_OP_CONST:
1954+
case GGML_OP_FILL:
19551955
{
1956-
ggml_compute_forward_const(params, tensor);
1956+
ggml_compute_forward_fill(params, tensor);
19571957
} break;
19581958
case GGML_OP_FLASH_ATTN_EXT:
19591959
{
@@ -2158,7 +2158,7 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
21582158
case GGML_OP_ACC:
21592159
case GGML_OP_CUMSUM:
21602160
case GGML_OP_TRI:
2161-
case GGML_OP_CONST:
2161+
case GGML_OP_FILL:
21622162
{
21632163
n_tasks = n_threads;
21642164
} break;

ggml/src/ggml-cpu/ops.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ static void ggml_compute_forward_const_f32(const ggml_compute_params * params, g
22112211
}
22122212
}
22132213

2214-
void ggml_compute_forward_const(const ggml_compute_params * params, ggml_tensor * dst) {
2214+
void ggml_compute_forward_fill(const ggml_compute_params * params, ggml_tensor * dst) {
22152215
ggml_compute_forward_const_f32(params, dst);
22162216
}
22172217

ggml/src/ggml-cpu/ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void ggml_compute_forward_timestep_embedding(const struct ggml_compute_params *
8787
void ggml_compute_forward_argsort(const struct ggml_compute_params * params, struct ggml_tensor * dst);
8888
void ggml_compute_forward_leaky_relu(const struct ggml_compute_params * params, struct ggml_tensor * dst);
8989
void ggml_compute_forward_tri(const struct ggml_compute_params * params, struct ggml_tensor * dst);
90-
void ggml_compute_forward_const(const struct ggml_compute_params * params, struct ggml_tensor * dst);
90+
void ggml_compute_forward_fill(const struct ggml_compute_params * params, struct ggml_tensor * dst);
9191
void ggml_compute_forward_flash_attn_ext(const struct ggml_compute_params * params, struct ggml_tensor * dst);
9292
void ggml_compute_forward_flash_attn_back(
9393
const struct ggml_compute_params * params,

ggml/src/ggml-cpu/vec.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,8 +1422,6 @@ inline static void ggml_vec_sum_f32(const int n, float * s, const float * x) {
14221422
// r - current row index
14231423
// dst - output array
14241424
// src - input array
1425-
// keep_org_val - if true, keep original value where mask applies; otherwise use constant 'c'
1426-
// c - constant value to use when not keeping original value
14271425
// bipred - the predicate on coordinates, derived from tri_type
14281426
inline static void ggml_vec_tri_f32(const int n, const int r, float * dst, const float * src, bool (*bipred)(int, int)) {
14291427
for (int i = 0; i < n; ++i) {

ggml/src/ggml.c

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5098,42 +5098,20 @@ struct ggml_tensor * ggml_tri(
50985098

50995099
// ggml_const
51005100

5101-
struct ggml_tensor * ggml_const(
5101+
struct ggml_tensor * ggml_fill(
51025102
struct ggml_context * ctx,
5103-
const int64_t ne0,
5104-
const int64_t ne1,
5105-
const int64_t ne2,
5106-
const int64_t ne3,
5103+
struct ggml_tensor * dst,
51075104
const float c) {
51085105

5109-
struct ggml_tensor * result = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, ne0, ne1, ne2, ne3);
5106+
struct ggml_tensor * result = ggml_view_tensor(ctx, dst);
51105107

51115108
ggml_set_op_params_f32(result, 0, c);
51125109

5113-
result->op = GGML_OP_CONST;
5110+
result->op = GGML_OP_FILL;
51145111

51155112
return result;
51165113
}
51175114

5118-
struct ggml_tensor * ggml_const_1d(struct ggml_context * ctx, const int64_t ne0, const float c) {
5119-
return ggml_const(ctx, ne0, 1, 1, 1, c);
5120-
}
5121-
5122-
struct ggml_tensor * ggml_const_2d(struct ggml_context * ctx, const int64_t ne0, const int64_t ne1,
5123-
const float c) {
5124-
return ggml_const(ctx, ne0, ne1, 1, 1, c);
5125-
}
5126-
5127-
struct ggml_tensor * ggml_const_3d(struct ggml_context * ctx, const int64_t ne0, const int64_t ne1,
5128-
const int64_t ne2, const float c) {
5129-
return ggml_const(ctx, ne0, ne1, ne2, 1, c);
5130-
}
5131-
5132-
struct ggml_tensor * ggml_const_4d(struct ggml_context * ctx, const int64_t ne0, const int64_t ne1,
5133-
const int64_t ne2, const int64_t ne3, const float c) {
5134-
return ggml_const(ctx, ne0, ne1, ne2, ne3, c);
5135-
}
5136-
51375115
// ggml_argsort
51385116

51395117
struct ggml_tensor * ggml_argsort(
@@ -5993,7 +5971,10 @@ struct ggml_tensor * ggml_opt_step_sgd(
59935971
struct ggml_tensor * ggml_solve_tri(
59945972
struct ggml_context * ctx,
59955973
struct ggml_tensor * a,
5996-
struct ggml_tensor * b) {
5974+
struct ggml_tensor * b,
5975+
bool lower,
5976+
bool left,
5977+
bool unitriangular) {
59975978

59985979
// A must be square and lower diagonal
59995980
GGML_ASSERT(a->ne[0] == a->ne[1]);
@@ -6007,6 +5988,8 @@ struct ggml_tensor * ggml_solve_tri(
60075988
GGML_ASSERT(ggml_is_contiguous(a));
60085989
GGML_ASSERT(ggml_is_contiguous(b));
60095990

5991+
GGML_ASSERT(lower && left && !unitriangular); // TODO: support other variants
5992+
60105993
struct ggml_tensor * result = ggml_new_tensor_4d(ctx, GGML_TYPE_F32, b->ne[0], b->ne[1], b->ne[2], b->ne[3]);
60115994

60125995
result->op = GGML_OP_SOLVE_TRI;

tests/test-backend-ops.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5971,20 +5971,24 @@ struct test_tri : public test_case {
59715971
}
59725972
};
59735973

5974-
// GGML_OP_CONST
5975-
struct test_const : public test_case {
5974+
// GGML_OP_FILL
5975+
struct test_fill : public test_case {
59765976
const ggml_type type;
59775977
const std::array<int64_t, 4> ne;
59785978
float c;
59795979

59805980
std::string vars() override { return VARS_TO_STR3(type, ne, c); }
59815981

5982-
test_const(float c, ggml_type type = GGML_TYPE_F32,
5982+
test_fill(float c, ggml_type type = GGML_TYPE_F32,
59835983
std::array<int64_t, 4> ne = { 10, 10, 4, 3 })
59845984
: type(type), ne(ne), c(c) {}
59855985

59865986
ggml_tensor * build_graph(ggml_context * ctx) override {
5987-
ggml_tensor * out = ggml_const(ctx, ne[0], ne[1], ne[2], ne[3], c);
5987+
ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]);
5988+
ggml_set_param(a);
5989+
ggml_set_name(a, "a");
5990+
5991+
ggml_tensor * out = ggml_fill(ctx, a, c);
59885992

59895993
ggml_set_name(out, "out");
59905994

@@ -5995,27 +5999,27 @@ struct test_const : public test_case {
59955999
// GGML_OP_SOLVE_TRI
59966000
struct test_solve_tri : public test_case {
59976001
const ggml_type type;
5998-
const std::array<int64_t, 4> ne;
5999-
const std::array<int64_t, 4> ne2;
6002+
const std::array<int64_t, 4> neLHS;
6003+
const std::array<int64_t, 4> neRHS;
60006004

6001-
std::string vars() override { return VARS_TO_STR3(type, ne, ne2); }
6005+
std::string vars() override { return VARS_TO_STR3(type, neLHS, neRHS); }
60026006

60036007
test_solve_tri(ggml_type type = GGML_TYPE_F32,
6004-
std::array<int64_t, 4> ne = { 10, 10, 4, 3 },
6005-
std::array<int64_t, 4> ne2 = { 3, 10, 4, 3 }
6008+
std::array<int64_t, 4> neLHS = { 10, 10, 4, 3 },
6009+
std::array<int64_t, 4> neRHS = { 3, 10, 4, 3 }
60066010
)
6007-
: type(type), ne(ne), ne2(ne2) {}
6011+
: type(type), neLHS(neLHS), neRHS(neRHS) {}
60086012

60096013
ggml_tensor * build_graph(ggml_context * ctx) override {
6010-
ggml_tensor * a = ggml_new_tensor_4d(ctx, type, ne[0], ne[1], ne[2], ne[3]);
6014+
ggml_tensor * a = ggml_new_tensor_4d(ctx, type, neLHS[0], neLHS[1], neLHS[2], neLHS[3]);
60116015
ggml_set_param(a);
60126016
ggml_set_name(a, "a");
60136017

6014-
ggml_tensor * b = ggml_new_tensor_4d(ctx, type, ne2[0], ne2[1], ne2[2], ne2[3]);
6018+
ggml_tensor * b = ggml_new_tensor_4d(ctx, type, neRHS[0], neRHS[1], neRHS[2], neRHS[3]);
60156019
ggml_set_param(b);
60166020
ggml_set_name(b, "b");
60176021

6018-
ggml_tensor * out = ggml_solve_tri(ctx, a, b);
6022+
ggml_tensor * out = ggml_solve_tri(ctx, a, b, true, true, false);
60196023
ggml_set_name(out, "out");
60206024

60216025
return out;
@@ -6024,7 +6028,7 @@ struct test_solve_tri : public test_case {
60246028
void initialize_tensors(ggml_context * ctx) override {
60256029
for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != NULL; t = ggml_get_next_tensor(ctx, t)) {
60266030
if (strcmp(t->name, "a") == 0) {
6027-
init_tensor_causal(t, 0.1, 1.0f);
6031+
init_tensor_tril(t, 0.1, 1.0f);
60286032
} else {
60296033
init_tensor_uniform(t, 0.1, 1.0f);
60306034
}
@@ -7528,9 +7532,9 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_eval() {
75287532
test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_UPPER));
75297533
test_cases.emplace_back(new test_tri(GGML_TRI_TYPE_UPPER_DIAG));
75307534

7531-
test_cases.emplace_back(new test_const(0.0f));
7532-
test_cases.emplace_back(new test_const(2.0f, GGML_TYPE_F32, { 303, 207, 11, 3 }));
7533-
test_cases.emplace_back(new test_const(-152.0f, GGML_TYPE_F32, { 800, 600, 4, 4 }));
7535+
test_cases.emplace_back(new test_fill(0.0f));
7536+
test_cases.emplace_back(new test_fill(2.0f, GGML_TYPE_F32, { 303, 207, 11, 3 }));
7537+
test_cases.emplace_back(new test_fill(-152.0f, GGML_TYPE_F32, { 800, 600, 4, 4 }));
75347538

75357539
test_cases.emplace_back(new test_solve_tri());
75367540
test_cases.emplace_back(new test_solve_tri(GGML_TYPE_F32, { 11, 11, 1, 1 }, { 5, 11, 1, 1 }));

0 commit comments

Comments
 (0)