-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse safetensors, model, tokenizer, generation configs
- Loading branch information
1 parent
b277796
commit 94cd02e
Showing
12 changed files
with
210 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "inference/safetensors.h" | ||
|
||
#include <fcntl.h> | ||
|
||
#include <cstdint> | ||
#include <cstdio> | ||
#include <memory> | ||
|
||
#include "json/json.h" | ||
#include "json/parser.h" | ||
#include "utils/logging.h" | ||
|
||
namespace gabby { | ||
namespace inference { | ||
|
||
/* static */ | ||
Safetensors Safetensors::LoadFile(const std::filesystem::path& path) { | ||
// format: https://github.com/huggingface/safetensors | ||
size_t file_size = std::filesystem::file_size(path); | ||
OwnedFd fd = Open(path.c_str(), O_RDONLY); | ||
OwnedMmap data = Mmap(file_size, std::move(fd)); | ||
|
||
// get header size | ||
uint64_t header_size = 0; | ||
for (int i = 0; i < 8; i++) { | ||
header_size |= data.get()[i] << (8 * i); | ||
} | ||
LOG(DEBUG) << "header size: " << header_size; | ||
|
||
// get header | ||
std::string s(data.get() + 8, data.get() + 8 + header_size); | ||
json::ValuePtr header = json::Parse(s); | ||
LOG(DEBUG) << "header: " << *header; | ||
|
||
return Safetensors(std::move(data), header, 8 + header_size); | ||
} | ||
|
||
} // namespace inference | ||
} // namespace gabby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef GABBY_INFERENCE_SAFETENSORS_H_ | ||
#define GABBY_INFERENCE_SAFETENSORS_H_ | ||
|
||
#include <filesystem> | ||
#include <stdexcept> | ||
|
||
#include "json/json.h" | ||
#include "utils/pointers.h" | ||
|
||
namespace gabby { | ||
namespace inference { | ||
|
||
class Safetensors { | ||
public: | ||
static Safetensors LoadFile(const std::filesystem::path& path); | ||
const json::ValuePtr header() const { return header_; } | ||
|
||
private: | ||
Safetensors(OwnedMmap mem, json::ValuePtr header, size_t data_offset) | ||
: mem_(std::move(mem)), header_(header), data_offset_(data_offset) {} | ||
|
||
OwnedMmap mem_; | ||
json::ValuePtr header_; | ||
size_t data_offset_ = 0; | ||
}; | ||
|
||
} // namespace inference | ||
} // namespace gabby | ||
|
||
#endif // GABBY_INFERENCE_SAFETENSORS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "utils/pointers.h" | ||
|
||
#include <fcntl.h> | ||
#include <sys/mman.h> | ||
|
||
#include <cerrno> | ||
|
||
#include "utils/logging.h" | ||
|
||
namespace gabby { | ||
|
||
OwnedStream Fopen(const char *name, const char *mode) { | ||
std::unique_ptr<FILE, decltype(&fclose)> f(fopen(name, mode), fclose); | ||
if (f.get() == nullptr) throw SystemError(errno); | ||
return std::move(f); | ||
} | ||
|
||
OwnedFd Own(int fd) { | ||
return OwnedFd(new int(fd), [](int *fdp) { | ||
if (fdp && *fdp >= 0) { | ||
close(*fdp); | ||
delete fdp; | ||
} | ||
}); | ||
} | ||
|
||
OwnedFd Open(const char *name, int flags) { | ||
int fd = open(name, flags); | ||
if (fd < 0) throw SystemError(errno); | ||
return Own(fd); | ||
} | ||
|
||
void MmapDeleter::operator()(uint8_t *p) { munmap(p, size); } | ||
|
||
OwnedMmap Mmap(size_t size, OwnedFd fd) { | ||
void *data = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, *fd.release(), 0); | ||
return OwnedMmap(static_cast<uint8_t *>(data), MmapDeleter{.size = size}); | ||
} | ||
|
||
} // namespace gabby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef GABBY_UTILS_POINTERS_H_ | ||
#define GABBY_UTILS_POINTERS_H_ | ||
|
||
#include <cstdint> | ||
#include <cstdio> | ||
#include <memory> | ||
#include <string_view> | ||
|
||
namespace gabby { | ||
|
||
using OwnedStream = std::unique_ptr<FILE, decltype(&fclose)>; | ||
|
||
OwnedStream Fopen(const char *name, const char *mode); | ||
|
||
using OwnedFd = std::unique_ptr<int, void (*)(int *)>; | ||
|
||
OwnedFd Own(int fd); | ||
|
||
OwnedFd Open(const char *name, int flags); | ||
|
||
struct MmapDeleter { | ||
size_t size; | ||
void operator()(uint8_t *p); | ||
}; | ||
|
||
using OwnedMmap = std::unique_ptr<uint8_t, MmapDeleter>; | ||
|
||
OwnedMmap Mmap(size_t size, OwnedFd fd); | ||
|
||
} // namespace gabby | ||
|
||
#endif // GABBY_UTILS_POINTERS_H_ |