mirror of
				https://github.com/ml-explore/mlx.git
				synced 2025-10-31 16:21:27 +08:00 
			
		
		
		
	Remove unnecessary string copies (#891)
1. Use string_view instead of string when there is no need for copy. 2. Otherwise move string when possible.
This commit is contained in:
		| @@ -230,8 +230,8 @@ std::unordered_map<std::string, array> load_arrays(gguf_ctx* ctx) { | ||||
|   return array_map; | ||||
| } | ||||
|  | ||||
| GGUFLoad load_gguf(const std::string& file, StreamOrDevice s) { | ||||
|   gguf_ctx* ctx = gguf_open(file.c_str()); | ||||
| GGUFLoad load_gguf(std::string_view file, StreamOrDevice s) { | ||||
|   gguf_ctx* ctx = gguf_open(file.data()); | ||||
|   if (!ctx) { | ||||
|     throw std::runtime_error("[load_gguf] gguf_init failed"); | ||||
|   } | ||||
|   | ||||
| @@ -105,7 +105,7 @@ void gguf_load_quantized( | ||||
|     weights_per_byte = 1; | ||||
|   } | ||||
|  | ||||
|   std::string name = std::string(tensor.name, tensor.namelen); | ||||
|   std::string name(tensor.name, tensor.namelen); | ||||
|   std::vector<int> shape = get_shape(tensor); | ||||
|   const uint64_t weights_per_block = 32; | ||||
|   if (shape[shape.size() - 1] % weights_per_block != 0) { | ||||
| @@ -136,9 +136,9 @@ void gguf_load_quantized( | ||||
|     extract_q8_0_data(tensor, weights, scales, biases); | ||||
|   } | ||||
|  | ||||
|   a.insert({name, weights}); | ||||
|   a.emplace(std::move(name), std::move(weights)); | ||||
|  | ||||
|   auto check_insert = [](auto inserted) { | ||||
|   auto check_insert = [](const auto& inserted) { | ||||
|     if (!inserted.second) { | ||||
|       std::ostringstream msg; | ||||
|       msg << "[load_gguf] Duplicate parameter name " << inserted.first->second | ||||
| @@ -147,11 +147,11 @@ void gguf_load_quantized( | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   const std::string weight_suffix = ".weight"; | ||||
|   constexpr std::string_view weight_suffix = ".weight"; | ||||
|   const std::string name_prefix = | ||||
|       name.substr(0, name.length() - weight_suffix.length()); | ||||
|   check_insert(a.insert({name_prefix + ".scales", scales})); | ||||
|   check_insert(a.insert({name_prefix + ".biases", biases})); | ||||
|   check_insert(a.emplace(name_prefix + ".scales", std::move(scales))); | ||||
|   check_insert(a.emplace(name_prefix + ".biases", std::move(biases))); | ||||
| } | ||||
|  | ||||
| } // namespace mlx::core | ||||
|   | ||||
| @@ -114,16 +114,13 @@ void save(std::shared_ptr<io::Writer> out_stream, array a) { | ||||
| } | ||||
|  | ||||
| /** Save array to file in .npy format */ | ||||
| void save(const std::string& file_, array a) { | ||||
|   // Open and check file | ||||
|   std::string file = file_; | ||||
|  | ||||
| void save(std::string file, array a) { | ||||
|   // Add .npy to file name if it is not there | ||||
|   if (file.length() < 4 || file.substr(file.length() - 4, 4) != ".npy") | ||||
|     file += ".npy"; | ||||
|  | ||||
|   // Serialize array | ||||
|   save(std::make_shared<io::FileWriter>(file), a); | ||||
|   save(std::make_shared<io::FileWriter>(std::move(file)), a); | ||||
| } | ||||
|  | ||||
| /** Load array from reader in .npy format */ | ||||
| @@ -227,8 +224,8 @@ array load(std::shared_ptr<io::Reader> in_stream, StreamOrDevice s) { | ||||
| } | ||||
|  | ||||
| /** Load array from file in .npy format */ | ||||
| array load(const std::string& file, StreamOrDevice s) { | ||||
|   return load(std::make_shared<io::FileReader>(file), s); | ||||
| array load(std::string file, StreamOrDevice s) { | ||||
|   return load(std::make_shared<io::FileReader>(std::move(file)), s); | ||||
| } | ||||
|  | ||||
| } // namespace mlx::core | ||||
|   | ||||
| @@ -60,7 +60,7 @@ std::string dtype_to_safetensor_str(Dtype t) { | ||||
|   } | ||||
| } | ||||
|  | ||||
| Dtype dtype_from_safetensor_str(std::string str) { | ||||
| Dtype dtype_from_safetensor_str(std::string_view str) { | ||||
|   if (str == ST_F32) { | ||||
|     return float32; | ||||
|   } else if (str == ST_F16) { | ||||
| @@ -88,7 +88,8 @@ Dtype dtype_from_safetensor_str(std::string str) { | ||||
|   } else if (str == ST_C64) { | ||||
|     return complex64; | ||||
|   } else { | ||||
|     throw std::runtime_error("[safetensor] unsupported dtype " + str); | ||||
|     throw std::runtime_error( | ||||
|         "[safetensor] unsupported dtype " + std::string(str)); | ||||
|   } | ||||
| } | ||||
|  | ||||
| @@ -129,9 +130,9 @@ SafetensorsLoad load_safetensors( | ||||
|       } | ||||
|       continue; | ||||
|     } | ||||
|     std::string dtype = item.value().at("dtype"); | ||||
|     std::vector<int> shape = item.value().at("shape"); | ||||
|     std::vector<size_t> data_offsets = item.value().at("data_offsets"); | ||||
|     const std::string& dtype = item.value().at("dtype"); | ||||
|     const std::vector<int>& shape = item.value().at("shape"); | ||||
|     const std::vector<size_t>& data_offsets = item.value().at("data_offsets"); | ||||
|     Dtype type = dtype_from_safetensor_str(dtype); | ||||
|     auto loaded_array = array( | ||||
|         shape, | ||||
| @@ -207,19 +208,17 @@ void save_safetensors( | ||||
| } | ||||
|  | ||||
| void save_safetensors( | ||||
|     const std::string& file_, | ||||
|     std::string file, | ||||
|     std::unordered_map<std::string, array> a, | ||||
|     std::unordered_map<std::string, std::string> metadata /* = {} */) { | ||||
|   // Open and check file | ||||
|   std::string file = file_; | ||||
|  | ||||
|   // Add .safetensors to file name if it is not there | ||||
|   if (file.length() < 12 || | ||||
|       file.substr(file.length() - 12, 12) != ".safetensors") | ||||
|     file += ".safetensors"; | ||||
|  | ||||
|   // Serialize array | ||||
|   save_safetensors(std::make_shared<io::FileWriter>(file), a, metadata); | ||||
|   save_safetensors( | ||||
|       std::make_shared<io::FileWriter>(std::move(file)), a, metadata); | ||||
| } | ||||
|  | ||||
| } // namespace mlx::core | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Cheng
					Cheng