Skip to content

Commit

Permalink
fix: windows fseek. (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz authored May 28, 2024
1 parent ad7498f commit 5fee854
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/transformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,8 @@ TransformerSpec Transformer::loadSpecFromFile(const char* path, const unsigned i
printf("💡 nSlices: %d\n", spec.nSlices);
printf("💡 ropeTheta: %.1f\n", spec.ropeTheta);

fseek(fd, 0, SEEK_END);
long fileSize = ftell(fd);
if (fileSize == -1L) {
fclose(fd);
throw std::runtime_error("Error determining model file size");
}
spec.fileSize = (size_t)seekToEnd(fd);
fclose(fd);

spec.fileSize = static_cast<size_t>(fileSize);
return spec;
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ float randomF32(unsigned long long *state) {
return (randomU32(state) >> 8) / 16777216.0f;
}

long seekToEnd(FILE* file) {
#ifdef _WIN32
_fseeki64(file, 0, SEEK_END);
return _ftelli64(file);
#else
fseek(file, 0, SEEK_END);
return ftell(file);
#endif
}

void openMmapFile(MmapFile* file, const char* path, size_t size) {
file->size = size;
#ifdef _WIN32
Expand Down
2 changes: 2 additions & 0 deletions src/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define UTILS_HPP

#include <atomic>
#include <cstdio>
#include "common/pthread.h"

#ifdef _WIN32
Expand All @@ -17,6 +18,7 @@ void freeBuffer(void* buffer);
unsigned long timeMs();
unsigned int randomU32(unsigned long long *state);
float randomF32(unsigned long long *state);
long seekToEnd(FILE* file);

struct MmapFile {
void* data;
Expand Down

0 comments on commit 5fee854

Please sign in to comment.