Skip to content

Commit 9ca617a

Browse files
Merge pull request #42 from tsisw/feature/txe-sqr
Added sqr
2 parents 61fb7ab + 93bfe55 commit 9ca617a

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

examples/simple/simple-backend-tsi.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ float test_input_1[GGML_TSAVORITE_KERNEL_TYPE_COUNT][NUM_ELEMENTS] = {
3131
{1.1, 4.4, 10, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
3232
// SQRT Kernel
3333
{1, 4, 9.6, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024},
34+
//SQR Kernel
35+
{1, 2.5, 3, 4, 5.6, 6, 7, 8, 9.2, 10, 11, 12, 13, 14, 15.4, 16, 17, 18, 19, 20, 21.2, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
3436
//NEG Kernel
3537
{1.1, -4.4, 10, -5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, -23, 24, 25, -26, 27, -28, 29, -30, 31, -32.6},
3638
//ABS Kernel
3739
{1.1, -4.4, 10, -5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, -23, 24, 25, -26, 27, -28, 29, -30, 31, -32.6},
3840
//SIN Kernel
3941
{1.1, 4.4, 10, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 20, 20, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32.6}
42+
4043
};
4144
float test_input_2[GGML_TSAVORITE_KERNEL_TYPE_COUNT][NUM_ELEMENTS] = {
4245
//ADD KERNEL
@@ -50,6 +53,8 @@ float test_input_2[GGML_TSAVORITE_KERNEL_TYPE_COUNT][NUM_ELEMENTS] = {
5053
//Below ROW value not used for Unary OPS-SQRT, NEG, ABS, SIN
5154
//SQRT KERNEL input not used
5255
{1.1, 2.2, 5, 10, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
56+
//SQR KERNEL input not used
57+
{1.1, 2.2, 5, 10, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
5358
//NEG KERNEL input not used
5459
{1.1, 2.2, 5, 10, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
5560
//ABS KERNEL input not used
@@ -69,12 +74,15 @@ float test_result[GGML_TSAVORITE_KERNEL_TYPE_COUNT][NUM_ELEMENTS] = {
6974
{1.0, 2.0, 2, 0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
7075
//SQRT Kernel
7176
{1, 2, 3.098387, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
77+
//SQR Kernel
78+
{1, 6.25, 9, 16, 31.36, 36, 49, 64, 84.64, 100, 121, 144, 169, 196, 237.16, 256, 289, 324, 361, 400, 449.44, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024},
7279
//NEG Kernel
7380
{-1.1, 4.4, -10, 5, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22, 23, -24, -25, 26, -27, 28, -29, 30, -31, 32.6},
7481
//ABS Kernel
7582
{1.1, 4.4, 10, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32.6},
7683
//SIN Kernel
7784
{0.891207, -0.951602, -0.544021, -0.958924, -0.958924, -0.279416, 0.656987, 0.989358, 0.412118, -0.544021, -0.999990, -0.536573, 0.420167, 0.990607, 0.650288, -0.287903, -0.961398, -0.750987, 0.149877, 0.912945, 0.912945, 0.912945, -0.846220, -0.905578, -0.132352, 0.762559, 0.956376, 0.270906, -0.663634, -0.988032, -0.404039, 0.926149}
85+
7886
};
7987

8088
float test_input_scale_1[GGML_TSAVORITE_KERNEL_TYPE_COUNT][NUM_ELEMENTS_SCALE] = {
@@ -103,6 +111,12 @@ float test_input_scale_1[GGML_TSAVORITE_KERNEL_TYPE_COUNT][NUM_ELEMENTS_SCALE] =
103111
4, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
104112
4, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
105113
//SQRT KERNEL
114+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
115+
9, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
116+
16, 25, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
117+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
118+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
119+
//SQR KERNEL
106120
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
107121
9, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
108122
16, 25, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -155,6 +169,12 @@ float test_input_scale_2[GGML_TSAVORITE_KERNEL_TYPE_COUNT][NUM_ELEMENTS_SCALE] =
155169
2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
156170
//Below ROW value not used for Unary OPS-SQRT, NEG, ABS, SIN
157171
//SQRT KERNEL input not used
172+
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
173+
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174+
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
175+
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
176+
1, 2, 1, 1, 1, 1, 1, 1, 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232+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
233+
// SQR KERNEL
208234
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
209235
3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
210236
4, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -398,6 +424,9 @@ static struct ggml_cgraph * build_graph(const simple_model& model, enum ggml_tsa
398424
case GGML_TSAVORITE_KERNEL_TYPE_SQRT:
399425
result = ggml_sqrt(ctx0, model.a);
400426
break;
427+
case GGML_TSAVORITE_KERNEL_TYPE_SQR:
428+
result = ggml_sqr(ctx0, model.a);
429+
break;
401430
case GGML_TSAVORITE_KERNEL_TYPE_NEG:
402431
result = ggml_neg(ctx0, model.a);
403432
break;
@@ -451,6 +480,8 @@ enum ggml_tsavorite_kernel_type convert_testcase_to_ops_type (const char *testCa
451480
return GGML_TSAVORITE_KERNEL_TYPE_DIV;
452481
else if (!strcmp(testCase,"sqrt"))
453482
return GGML_TSAVORITE_KERNEL_TYPE_SQRT;
483+
else if (!strcmp(testCase,"sqr"))
484+
return GGML_TSAVORITE_KERNEL_TYPE_SQR;
454485
else if (!strcmp(testCase,"neg"))
455486
return GGML_TSAVORITE_KERNEL_TYPE_NEG;
456487
else if (!strcmp(testCase,"abs"))
@@ -462,6 +493,37 @@ enum ggml_tsavorite_kernel_type convert_testcase_to_ops_type (const char *testCa
462493
return GGML_TSAVORITE_KERNEL_TYPE_ADD;
463494
}
464495

496+
const char* convert_ops_type_to_testcase(enum ggml_tsavorite_kernel_type ops_type) {
497+
498+
switch (ops_type) {
499+
case GGML_TSAVORITE_KERNEL_TYPE_ADD:
500+
return "add";
501+
case GGML_TSAVORITE_KERNEL_TYPE_SUB:
502+
return "sub";
503+
case GGML_TSAVORITE_KERNEL_TYPE_MULT:
504+
return "mult";
505+
case GGML_TSAVORITE_KERNEL_TYPE_DIV:
506+
return "div";
507+
case GGML_TSAVORITE_KERNEL_TYPE_SQRT:
508+
return "sqrt";
509+
case GGML_TSAVORITE_KERNEL_TYPE_SQR:
510+
return "sqr";
511+
case GGML_TSAVORITE_KERNEL_TYPE_NEG:
512+
return "neg";
513+
case GGML_TSAVORITE_KERNEL_TYPE_ABS:
514+
return "abs";
515+
case GGML_TSAVORITE_KERNEL_TYPE_SIN:
516+
return "sin";
517+
case GGML_TSAVORITE_KERNEL_TYPE_SIGMOID:
518+
return "sigmoid";
519+
case GGML_TSAVORITE_KERNEL_TYPE_SILU:
520+
return "silu";
521+
default:
522+
return "unknown";
523+
}
524+
}
525+
526+
465527
int main(int argc, char *argv[]) {
466528
ggml_time_init();
467529
bool test_case_flag = true;
@@ -484,6 +546,7 @@ int main(int argc, char *argv[]) {
484546
ops_type = convert_testcase_to_ops_type("add");
485547
}
486548
if (ops_type == GGML_TSAVORITE_KERNEL_TYPE_SQRT ||
549+
ops_type == GGML_TSAVORITE_KERNEL_TYPE_SQR ||
487550
ops_type == GGML_TSAVORITE_KERNEL_TYPE_NEG ||
488551
ops_type == GGML_TSAVORITE_KERNEL_TYPE_ABS ||
489552
ops_type == GGML_TSAVORITE_KERNEL_TYPE_SIN)
@@ -551,7 +614,7 @@ int main(int argc, char *argv[]) {
551614

552615
// expected result:
553616

554-
fprintf(stderr, "\n operation type: %d, num of elements %d \n", ops_type, (int) result->ne[0]);
617+
fprintf(stderr, "\n operation type: %s, num of elements %d \n", convert_ops_type_to_testcase(ops_type), (int) result->ne[0]);
555618

556619
fprintf(stderr, "\n compute is also done \n");
557620
for (int i = 0; i < result->ne[0] /* cols */; i++) {

ggml-tsi-kernel

ggml/include/ggml-tsavorite.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ enum ggml_tsavorite_kernel_type {
123123
GGML_TSAVORITE_KERNEL_TYPE_MULT,
124124
GGML_TSAVORITE_KERNEL_TYPE_DIV,
125125
GGML_TSAVORITE_KERNEL_TYPE_SQRT,
126+
GGML_TSAVORITE_KERNEL_TYPE_SQR,
126127
GGML_TSAVORITE_KERNEL_TYPE_NEG,
127128
GGML_TSAVORITE_KERNEL_TYPE_ABS,
128129
GGML_TSAVORITE_KERNEL_TYPE_SIN,
@@ -156,6 +157,7 @@ extern void _mlir_ciface_txe_sub_host(void *a, void *b, void *res);
156157
extern void _mlir_ciface_txe_mult_host(void *a, void *b, void *res);
157158
extern void _mlir_ciface_txe_div_host(void *a, void *b, void *res);
158159
extern void _mlir_ciface_txe_sqrt_host(void *a, void *res);
160+
extern void _mlir_ciface_txe_sqr_host(void *a, void *res);
159161
extern void _mlir_ciface_txe_neg_host(void *a, void *res);
160162
extern void _mlir_ciface_txe_abs_host(void *a, void *res);
161163
extern void _mlir_ciface_txe_sin_host(void *a, void *res);

ggml/src/ggml-tsavorite/ggml-tsavorite.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ static txe_compute_pipeline_state_s tsi_kernel_setup(enum ggml_tsavorite_kernel_
416416
kernel_pipeline->kernel_name = "TXE_SQRT";
417417
flag = true;
418418
break;
419+
case GGML_TSAVORITE_KERNEL_TYPE_SQR:
420+
kernel_pipeline->_mlir_fptr_1_input = &_mlir_ciface_txe_sqr_host;
421+
kernel_pipeline->kernel_name = "TXE_SQR";
422+
flag = true;
423+
break;
419424
case GGML_TSAVORITE_KERNEL_TYPE_NEG:
420425
kernel_pipeline->_mlir_fptr_1_input = &_mlir_ciface_txe_neg_host;
421426
kernel_pipeline->kernel_name = "TXE_NEG";
@@ -589,6 +594,7 @@ static struct ggml_backend_tsavorite_context *ggml_tsavorite_init(ggml_backend_d
589594
GGML_TSAVORITE_KERNEL(GGML_TSAVORITE_KERNEL_TYPE_MULT, true);
590595
GGML_TSAVORITE_KERNEL(GGML_TSAVORITE_KERNEL_TYPE_DIV, true);
591596
GGML_TSAVORITE_KERNEL(GGML_TSAVORITE_KERNEL_TYPE_SQRT, true);
597+
GGML_TSAVORITE_KERNEL(GGML_TSAVORITE_KERNEL_TYPE_SQR, true);
592598
GGML_TSAVORITE_KERNEL(GGML_TSAVORITE_KERNEL_TYPE_NEG, true);
593599
GGML_TSAVORITE_KERNEL(GGML_TSAVORITE_KERNEL_TYPE_ABS, true);
594600
GGML_TSAVORITE_KERNEL(GGML_TSAVORITE_KERNEL_TYPE_SIN, true);
@@ -693,6 +699,7 @@ static bool ggml_tsavorite_supports_op(const struct ggml_backend_tsavorite_devic
693699
case GGML_OP_MUL:
694700
case GGML_OP_DIV:
695701
case GGML_OP_SQRT:
702+
case GGML_OP_SQR:
696703
case GGML_OP_SIN:
697704
break;
698705
case GGML_OP_UNARY:
@@ -843,6 +850,10 @@ static enum ggml_status ggml_tsavorite_graph_compute(ggml_backend_t backend,
843850
kernel_type = GGML_TSAVORITE_KERNEL_TYPE_SQRT;
844851
num_of_input_tensors = TSAVORITE_UNARY_INPUT_TENSORS;
845852
break;
853+
case GGML_OP_SQR:
854+
kernel_type = GGML_TSAVORITE_KERNEL_TYPE_SQR;
855+
num_of_input_tensors = TSAVORITE_UNARY_INPUT_TENSORS;
856+
break;
846857
case GGML_OP_SIN:
847858
kernel_type = GGML_TSAVORITE_KERNEL_TYPE_SIN;
848859
num_of_input_tensors = TSAVORITE_UNARY_INPUT_TENSORS;
@@ -1822,6 +1833,7 @@ static bool ggml_backend_tsavorite_device_offload_op(ggml_backend_dev_t dev,
18221833
case GGML_OP_DIV:
18231834
case GGML_OP_MUL:
18241835
case GGML_OP_SQRT:
1836+
case GGML_OP_SQR:
18251837
case GGML_OP_SIN:
18261838
break;
18271839
case GGML_OP_UNARY:

tsi-pkg-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fi
6767
cat > ./${TSI_GGML_BUNDLE_INSTALL_DIR}/ggml.sh << EOL
6868
#!/bin/bash
6969
export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:\$(pwd)
70-
tsi_kernels=("add" "sub" "mult" "div" "abs" "inv" "neg" "sin" "sqrt" "sigmoid" "silu")
70+
tsi_kernels=("add" "sub" "mult" "div" "abs" "inv" "neg" "sin" "sqrt" "sqr" "sigmoid" "silu")
7171
7272
for kernel in "\${tsi_kernels[@]}"; do
7373
mkdir -p ${TSI_BLOB_INSTALL_DIR}/txe_\$kernel

0 commit comments

Comments
 (0)