| 2005 | } |
| 2006 | |
| 2007 | static struct ggml_tensor * graph_copy_dup_tensor(struct ggml_hash_set hash_set, struct ggml_tensor ** node_copies, |
| 2008 | struct ggml_context * ctx_allocated, struct ggml_context * ctx_unallocated, struct ggml_tensor * src) { |
| 2009 | |
| 2010 | GGML_ASSERT(src != NULL); |
| 2011 | GGML_ASSERT(src->data && "graph must be allocated"); |
| 2012 | |
| 2013 | size_t id = ggml_hash_insert(&hash_set, src); |
| 2014 | if (id == GGML_HASHSET_ALREADY_EXISTS) { |
| 2015 | return node_copies[ggml_hash_find(&hash_set, src)]; |
| 2016 | } |
| 2017 | |
| 2018 | struct ggml_tensor * dst = ggml_dup_tensor_layout(src->data && !src->view_src ? ctx_allocated : ctx_unallocated, src); |
| 2019 | if (src->view_src != NULL) { |
| 2020 | dst->view_src = graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, src->view_src); |
| 2021 | dst->view_offs = src->view_offs; |
| 2022 | } |
| 2023 | dst->op = src->op; |
| 2024 | dst->flags = src->flags; |
| 2025 | memcpy(dst->op_params, src->op_params, sizeof(dst->op_params)); |
| 2026 | ggml_set_name(dst, src->name); |
| 2027 | |
| 2028 | // copy src |
| 2029 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
| 2030 | struct ggml_tensor * s = src->src[i]; |
| 2031 | if (s == NULL) { |
| 2032 | continue; |
| 2033 | } |
| 2034 | dst->src[i] = graph_copy_dup_tensor(hash_set, node_copies, ctx_allocated, ctx_unallocated, s); |
| 2035 | } |
| 2036 | |
| 2037 | node_copies[id] = dst; |
| 2038 | return dst; |
| 2039 | } |
| 2040 | |
| 2041 | static void graph_copy_init_tensor(struct ggml_hash_set * hash_set, struct ggml_tensor ** node_copies, bool * node_init, struct ggml_tensor * src) { |
| 2042 | size_t id = ggml_hash_find(hash_set, src); |
no test coverage detected