| 1401 | // |
| 1402 | |
| 1403 | static common_control_vector_data common_control_vector_load_one(const common_control_vector_load_info & load_info) { |
| 1404 | common_control_vector_data result = { -1, {} }; |
| 1405 | |
| 1406 | ggml_context * ctx = nullptr; |
| 1407 | struct gguf_init_params meta_gguf_params = { |
| 1408 | /* .no_alloc = */ false, |
| 1409 | /* .ctx = */ &ctx, |
| 1410 | }; |
| 1411 | struct gguf_context * ctx_gguf = gguf_init_from_file(load_info.fname.c_str(), meta_gguf_params); |
| 1412 | if (!ctx_gguf) { |
| 1413 | LOG_ERR("%s: failed to load control vector file from %s\n", __func__, load_info.fname.c_str()); |
| 1414 | return result; |
| 1415 | } |
| 1416 | |
| 1417 | int32_t n_tensors = gguf_get_n_tensors(ctx_gguf); |
| 1418 | if (n_tensors == 0) { |
| 1419 | LOG_WRN("%s: no direction tensors found in %s\n", __func__, load_info.fname.c_str()); |
| 1420 | } |
| 1421 | |
| 1422 | for (int i = 0; i < n_tensors; i++) { |
| 1423 | std::string name = gguf_get_tensor_name(ctx_gguf, i); |
| 1424 | |
| 1425 | int layer_idx = -1; |
| 1426 | |
| 1427 | // split on '.' |
| 1428 | size_t dotpos = name.find('.'); |
| 1429 | if (dotpos != std::string::npos && name.substr(0, dotpos) == "direction") { |
| 1430 | try { |
| 1431 | layer_idx = std::stoi(name.substr(dotpos + 1)); |
| 1432 | } catch (...) { |
| 1433 | layer_idx = -1; |
| 1434 | } |
| 1435 | } |
| 1436 | if (layer_idx < 0) { |
| 1437 | LOG_ERR("%s: invalid/unparsable direction tensor layer index in %s\n", __func__, load_info.fname.c_str()); |
| 1438 | result.n_embd = -1; |
| 1439 | break; |
| 1440 | } else if (layer_idx == 0) { |
| 1441 | LOG_ERR("%s: invalid (zero) direction tensor layer index in %s\n", __func__, load_info.fname.c_str()); |
| 1442 | result.n_embd = -1; |
| 1443 | break; |
| 1444 | } |
| 1445 | |
| 1446 | struct ggml_tensor * tensor = ggml_get_tensor(ctx, name.c_str()); |
| 1447 | if (tensor->type != GGML_TYPE_F32) { |
| 1448 | LOG_ERR("%s: invalid (non-F32) direction tensor type in %s\n", __func__, load_info.fname.c_str()); |
| 1449 | result.n_embd = -1; |
| 1450 | break; |
| 1451 | } |
| 1452 | if (ggml_n_dims(tensor) != 1) { |
| 1453 | LOG_ERR("%s: invalid (non-1D) direction tensor shape in %s\n", __func__, load_info.fname.c_str()); |
| 1454 | result.n_embd = -1; |
| 1455 | break; |
| 1456 | } |
| 1457 | |
| 1458 | if (result.n_embd == -1) { |
| 1459 | result.n_embd = ggml_nelements(tensor); |
| 1460 | } else if (ggml_nelements(tensor) != result.n_embd) { |
no test coverage detected