MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / create_tensor

Method create_tensor

src/llama-model-loader.cpp:1046–1286  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1044}
1045
1046struct ggml_tensor * llama_model_loader::create_tensor(
1047 const llama_hparams & hparams, const buft_list_t * buft_list_cpu, const buft_list_t * buft_list_input, const buft_list_t * buft_list_output,
1048 const buft_list_t * buft_list_layer, const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) {
1049 auto ctx_for_buft = [&](ggml_backend_buffer_type_t buft) -> ggml_context * {
1050 auto it = ctx_map.find(buft);
1051 if (it == ctx_map.end()) {
1052 // one ggml context per buffer type
1053 int max_n_tensors = n_tensors;
1054 max_n_tensors += 1; // duplicated output tensor
1055 max_n_tensors += hparams.n_layer*2; // duplicated rope freq tensors
1056 if (files.empty()) {
1057 max_n_tensors += hparams.n_layer*256; // this should be well above what any model actually uses
1058 }
1059 const size_t ctx_size = ggml_tensor_overhead()*max_n_tensors;
1060
1061 ggml_init_params params = {
1062 /*.mem_size =*/ ctx_size,
1063 /*.mem_buffer =*/ NULL,
1064 /*.no_alloc =*/ true,
1065 };
1066
1067 ggml_context * ctx = ggml_init(params);
1068 if (!ctx) {
1069 throw std::runtime_error(format("failed to create ggml context"));
1070 }
1071
1072 ctx_map.emplace(buft, ctx);
1073
1074 return ctx;
1075 }
1076 return it->second.get();
1077 };
1078
1079 auto buft_for_tensor = [&](ggml_tensor * t_meta) -> ggml_backend_buffer_type_t {
1080 if (!t_meta) {
1081 if (flags & TENSOR_NOT_REQUIRED) {
1082 return nullptr;
1083 }
1084 throw std::runtime_error(format("missing tensor '%s'", tn.str().c_str()));
1085 }
1086
1087 // some models use the token embedding tensor as the output, but since these are used in different layers and with different ops
1088 // the tensor is duplicated
1089 // to handle this, we check if the tensor is duplicated, and if so, we assume that it is being loaded as the output tensor
1090 llm_tensor tn_tensor = tn.tensor;
1091 if (tn.tensor == LLM_TENSOR_TOKEN_EMBD && (flags & TENSOR_DUPLICATED)) {
1092 tn_tensor = LLM_TENSOR_OUTPUT;
1093 }
1094
1095 llm_tensor_info info;
1096 try {
1097 info = llm_tensor_info_for(tn_tensor);
1098 } catch (const std::out_of_range & e) {
1099 throw std::runtime_error(format("missing tensor info mapping for %s", tn.str().c_str()));
1100 }
1101
1102 // skip unused tensors
1103 if (info.op == GGML_OP_NONE || (flags & TENSOR_SKIP)) {

Callers 2

load_tensorsMethod · 0.80

Calls 15

ggml_tensor_overheadFunction · 0.85
ggml_initFunction · 0.85
formatFunction · 0.85
ggml_nbytesFunction · 0.85
select_weight_buftFunction · 0.85
ggml_type_nameFunction · 0.85
ggml_backend_buft_nameFunction · 0.85
ggml_backend_dev_by_typeFunction · 0.85

Tested by

no test coverage detected