]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
llama: handle short reads in direct I/O path (#18504) upstream/0.0.7599
authortriplenom <redacted>
Thu, 1 Jan 2026 02:24:43 +0000 (21:24 -0500)
committerGitHub <redacted>
Thu, 1 Jan 2026 02:24:43 +0000 (10:24 +0800)
src/llama-mmap.cpp

index 23b648a2e3b63b6e6dd526814f305dac4099d000..232005e140245288f73771be19d7e36be4aab194 100644 (file)
@@ -240,9 +240,10 @@ struct llama_file::impl {
                 throw std::runtime_error("unexpectedly reached end of file");
             }
         } else {
-            bool successful = false;
-            while (!successful) {
-                off_t ret = read(fd, ptr, len);
+            size_t bytes_read = 0;
+            while (bytes_read < len) {
+                const size_t to_read = len - bytes_read;
+                ssize_t ret = ::read(fd, reinterpret_cast<char *>(ptr) + bytes_read, to_read);
 
                 if (ret == -1) {
                     if (errno == EINTR) {
@@ -251,10 +252,16 @@ struct llama_file::impl {
                     throw std::runtime_error(format("read error: %s", strerror(errno)));
                 }
                 if (ret == 0) {
+                    // EOF: allow if this read was only pulling alignment padding past file end
+                    off_t pos = lseek(fd, 0, SEEK_CUR);
+                    if (pos != -1 && (size_t) pos == size) {
+                        std::memset(reinterpret_cast<char *>(ptr) + bytes_read, 0, len - bytes_read);
+                        return;
+                    }
                     throw std::runtime_error("unexpectedly reached end of file");
                 }
 
-                successful = true;
+                bytes_read += (size_t) ret;
             }
         }
     }