MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / ggml_new_tensor_impl

Function ggml_new_tensor_impl

subprojects/llama.cpp/ggml/src/ggml.c:1665–1739  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1663}
1664
1665static struct ggml_tensor * ggml_new_tensor_impl(
1666 struct ggml_context * ctx,
1667 enum ggml_type type,
1668 int n_dims,
1669 const int64_t * ne,
1670 struct ggml_tensor * view_src,
1671 size_t view_offs) {
1672
1673 GGML_ASSERT(type >= 0 && type < GGML_TYPE_COUNT);
1674 GGML_ASSERT(n_dims >= 1 && n_dims <= GGML_MAX_DIMS);
1675
1676 // find the base tensor and absolute offset
1677 if (view_src != NULL && view_src->view_src != NULL) {
1678 view_offs += view_src->view_offs;
1679 view_src = view_src->view_src;
1680 }
1681
1682 size_t data_size = ggml_row_size(type, ne[0]);
1683 for (int i = 1; i < n_dims; i++) {
1684 data_size *= ne[i];
1685 }
1686
1687 GGML_ASSERT(view_src == NULL || data_size == 0 || data_size + view_offs <= ggml_nbytes(view_src));
1688
1689 void * data = view_src != NULL ? view_src->data : NULL;
1690 if (data != NULL) {
1691 data = (char *) data + view_offs;
1692 }
1693
1694 size_t obj_alloc_size = 0;
1695
1696 if (view_src == NULL && !ctx->no_alloc) {
1697 // allocate tensor data in the context's memory pool
1698 obj_alloc_size = data_size;
1699 }
1700
1701 struct ggml_object * const obj_new = ggml_new_object(ctx, GGML_OBJECT_TYPE_TENSOR, GGML_TENSOR_SIZE + obj_alloc_size);
1702 GGML_ASSERT(obj_new);
1703
1704 struct ggml_tensor * const result = (struct ggml_tensor *)((char *)ctx->mem_buffer + obj_new->offs);
1705
1706 *result = (struct ggml_tensor) {
1707 /*.type =*/ type,
1708 /*.buffer =*/ NULL,
1709 /*.ne =*/ { 1, 1, 1, 1 },
1710 /*.nb =*/ { 0, 0, 0, 0 },
1711 /*.op =*/ GGML_OP_NONE,
1712 /*.op_params =*/ { 0 },
1713 /*.flags =*/ 0,
1714 /*.src =*/ { NULL },
1715 /*.view_src =*/ view_src,
1716 /*.view_offs =*/ view_offs,
1717 /*.data =*/ obj_alloc_size > 0 ? (void *)(result + 1) : data,
1718 /*.name =*/ { 0 },
1719 /*.extra =*/ NULL,
1720 /*.padding =*/ { 0 },
1721 };
1722

Callers 9

ggml_new_tensorFunction · 0.85
ggml_view_tensorFunction · 0.85
ggml_glu_implFunction · 0.85
ggml_reshapeFunction · 0.85
ggml_reshape_1dFunction · 0.85
ggml_reshape_2dFunction · 0.85
ggml_reshape_3dFunction · 0.85
ggml_reshape_4dFunction · 0.85
ggml_view_implFunction · 0.85

Calls 5

ggml_row_sizeFunction · 0.85
ggml_nbytesFunction · 0.85
ggml_new_objectFunction · 0.85
ggml_type_sizeFunction · 0.85
ggml_blck_sizeFunction · 0.85

Tested by

no test coverage detected