| 1711 | // |
| 1712 | |
| 1713 | static common_control_vector_data common_control_vector_load_one(const common_control_vector_load_info & load_info) { |
| 1714 | common_control_vector_data result = { -1, {} }; |
| 1715 | |
| 1716 | ggml_context * ctx = nullptr; |
| 1717 | struct gguf_init_params meta_gguf_params = { |
| 1718 | /* .no_alloc = */ false, |
| 1719 | /* .ctx = */ &ctx, |
| 1720 | }; |
| 1721 | struct gguf_context * ctx_gguf = gguf_init_from_file(load_info.fname.c_str(), meta_gguf_params); |
| 1722 | if (!ctx_gguf) { |
| 1723 | LOG_ERR("%s: failed to load control vector file from %s\n", __func__, load_info.fname.c_str()); |
| 1724 | return result; |
| 1725 | } |
| 1726 | |
| 1727 | int32_t n_tensors = gguf_get_n_tensors(ctx_gguf); |
| 1728 | if (n_tensors == 0) { |
| 1729 | LOG_WRN("%s: no direction tensors found in %s\n", __func__, load_info.fname.c_str()); |
| 1730 | } |
| 1731 | |
| 1732 | for (int i = 0; i < n_tensors; i++) { |
| 1733 | std::string name = gguf_get_tensor_name(ctx_gguf, i); |
| 1734 | |
| 1735 | int layer_idx = -1; |
| 1736 | |
| 1737 | // split on '.' |
| 1738 | size_t dotpos = name.find('.'); |
| 1739 | if (dotpos != std::string::npos && name.substr(0, dotpos) == "direction") { |
| 1740 | try { |
| 1741 | layer_idx = std::stoi(name.substr(dotpos + 1)); |
| 1742 | } catch (...) { |
| 1743 | layer_idx = -1; |
| 1744 | } |
| 1745 | } |
| 1746 | if (layer_idx < 0) { |
| 1747 | LOG_ERR("%s: invalid/unparsable direction tensor layer index in %s\n", __func__, load_info.fname.c_str()); |
| 1748 | result.n_embd = -1; |
| 1749 | break; |
| 1750 | } else if (layer_idx == 0) { |
| 1751 | LOG_ERR("%s: invalid (zero) direction tensor layer index in %s\n", __func__, load_info.fname.c_str()); |
| 1752 | result.n_embd = -1; |
| 1753 | break; |
| 1754 | } |
| 1755 | |
| 1756 | struct ggml_tensor * tensor = ggml_get_tensor(ctx, name.c_str()); |
| 1757 | if (tensor->type != GGML_TYPE_F32) { |
| 1758 | LOG_ERR("%s: invalid (non-F32) direction tensor type in %s\n", __func__, load_info.fname.c_str()); |
| 1759 | result.n_embd = -1; |
| 1760 | break; |
| 1761 | } |
| 1762 | if (ggml_n_dims(tensor) != 1) { |
| 1763 | LOG_ERR("%s: invalid (non-1D) direction tensor shape in %s\n", __func__, load_info.fname.c_str()); |
| 1764 | result.n_embd = -1; |
| 1765 | break; |
| 1766 | } |
| 1767 | |
| 1768 | if (result.n_embd == -1) { |
| 1769 | result.n_embd = ggml_nelements(tensor); |
| 1770 | } else if (ggml_nelements(tensor) != result.n_embd) { |
no test coverage detected