Skip to content

Commit a7312ae

Browse files
ggml : add a set of functions for checking contiguity of inner tensor dimensions (ggml-org#25650)
Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
1 parent 657e011 commit a7312ae

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

ggml/include/ggml.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ extern "C" {
780780
GGML_API bool ggml_is_contiguous_1(const struct ggml_tensor * tensor); // contiguous for dims >= 1
781781
GGML_API bool ggml_is_contiguous_2(const struct ggml_tensor * tensor); // contiguous for dims >= 2
782782

783+
GGML_API bool ggml_is_contiguous_to_1(const struct ggml_tensor * tensor); // contiguous for dims < 1
784+
GGML_API bool ggml_is_contiguous_to_2(const struct ggml_tensor * tensor); // contiguous for dims < 2
785+
GGML_API bool ggml_is_contiguous_to_3(const struct ggml_tensor * tensor); // contiguous for dims < 3
786+
783787
// returns whether the tensor elements are allocated as one contiguous block of memory (no gaps, but permutation ok)
784788
GGML_API bool ggml_is_contiguously_allocated(const struct ggml_tensor * tensor);
785789

ggml/src/ggml.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,14 +1464,14 @@ bool ggml_is_transposed(const struct ggml_tensor * tensor) {
14641464
return tensor->nb[0] > tensor->nb[1];
14651465
}
14661466

1467-
static bool ggml_is_contiguous_n(const struct ggml_tensor * tensor, int n) {
1467+
static bool ggml_is_contiguous_m_n(const struct ggml_tensor * tensor, int m, int n) {
14681468
size_t next_nb = ggml_type_size(tensor->type);
14691469
if (tensor->ne[0] != ggml_blck_size(tensor->type) && tensor->nb[0] != next_nb) {
14701470
return false;
14711471
}
14721472
next_nb *= tensor->ne[0]/ggml_blck_size(tensor->type);
1473-
for (int i = 1; i < GGML_MAX_DIMS; i++) {
1474-
if (i > n) {
1473+
for (int i = 1; i < n; i++) {
1474+
if (i > m) {
14751475
if (tensor->ne[i] != 1 && tensor->nb[i] != next_nb) {
14761476
return false;
14771477
}
@@ -1489,15 +1489,27 @@ bool ggml_is_contiguous(const struct ggml_tensor * tensor) {
14891489
}
14901490

14911491
bool ggml_is_contiguous_0(const struct ggml_tensor * tensor) {
1492-
return ggml_is_contiguous_n(tensor, 0);
1492+
return ggml_is_contiguous_m_n(tensor, 0, GGML_MAX_DIMS);
14931493
}
14941494

14951495
bool ggml_is_contiguous_1(const struct ggml_tensor * tensor) {
1496-
return ggml_is_contiguous_n(tensor, 1);
1496+
return ggml_is_contiguous_m_n(tensor, 1, GGML_MAX_DIMS);
14971497
}
14981498

14991499
bool ggml_is_contiguous_2(const struct ggml_tensor * tensor) {
1500-
return ggml_is_contiguous_n(tensor, 2);
1500+
return ggml_is_contiguous_m_n(tensor, 2, GGML_MAX_DIMS);
1501+
}
1502+
1503+
bool ggml_is_contiguous_to_1(const struct ggml_tensor * tensor) {
1504+
return ggml_is_contiguous_m_n(tensor, 0, 1);
1505+
}
1506+
1507+
bool ggml_is_contiguous_to_2(const struct ggml_tensor * tensor) {
1508+
return ggml_is_contiguous_m_n(tensor, 0, 2);
1509+
}
1510+
1511+
bool ggml_is_contiguous_to_3(const struct ggml_tensor * tensor) {
1512+
return ggml_is_contiguous_m_n(tensor, 0, 3);
15011513
}
15021514

15031515
bool ggml_is_contiguously_allocated(const struct ggml_tensor * tensor) {

0 commit comments

Comments
 (0)