Serialization.cpp 7.71 KB
Newer Older
Zhekai Zhang's avatar
Zhekai Zhang committed
1
2
3
4
5
6
7
8
#include "Serialization.h"

#include <nlohmann/json.hpp>
#include <mio/mmap.hpp>

using json = nlohmann::json;
using spdlog::fmt_lib::format;

9
class SafeTensors::MMapImpl {
Zhekai Zhang's avatar
Zhekai Zhang committed
10
public:
11
    virtual ~MMapImpl() {}
Muyang Li's avatar
Muyang Li committed
12
    virtual size_t size()      = 0;
13
    virtual const char *data() = 0;
Zhekai Zhang's avatar
Zhekai Zhang committed
14
15
};

16
17
18
19
20
21
22
23
24
25
26
27
28
29
class SafeTensors::MMapImplMio : public SafeTensors::MMapImpl {
public:
    MMapImplMio(const std::string &filename) : impl(filename, 0, mio::map_entire_file) {}
    virtual size_t size() override {
        return impl.size();
    }
    virtual const char *data() override {
        return impl.data();
    }

private:
    mio::mmap_source impl;
};

muyangli's avatar
muyangli committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class SafeTensors::MMapImplRead : public SafeTensors::MMapImpl {
public:
    MMapImplRead(const std::string &filename, bool pin) {
        std::ifstream fin(filename, std::ios::binary);
        fin.seekg(0, std::ios::end);
        size_t size = fin.tellg();
        fin.seekg(0);

        if (pin) {
            buffer = std::make_unique<BufferHost>(size);
        } else {
            buffer = std::make_unique<BufferMalloc>(size);
        }

        fin.read((char *)buffer->getPtr(), size);
    }
    virtual size_t size() override {
        return buffer->getSize();
    }
    virtual const char *data() override {
        return (const char *)buffer->getPtr();
    }

private:
    std::unique_ptr<Buffer> buffer;
};

Muyang Li's avatar
Muyang Li committed
57
#ifdef __linux__
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

class SafeTensors::MMapImplPrivate : public SafeTensors::MMapImpl {
public:
    MMapImplPrivate(const std::string &filename) {
        int fd = open(filename.c_str(), O_RDONLY);
        if (fd < 0) {
            throw std::system_error(errno, std::generic_category(), filename);
        }

        struct stat statbuf;
        fstat(fd, &statbuf);
        filesize = statbuf.st_size;

        ptr = mmap(0, filesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
        if (ptr == MAP_FAILED) {
            close(fd);
            throw std::system_error(errno, std::generic_category(), filename);
        }

        close(fd);
    }
    ~MMapImplPrivate() {
        munmap(ptr, filesize);
    }

    virtual size_t size() override {
        return filesize;
    }
    virtual const char *data() override {
        return (const char *)ptr;
    }

private:
    size_t filesize;
    void *ptr;
};

Muyang Li's avatar
Muyang Li committed
99
#else
100
101
102
103

class SafeTensors::MMapImplPrivate : public SafeTensors::MMapImpl {
public:
    MMapImplPrivate(const std::string &filename) {
sxtyzhangzk's avatar
sxtyzhangzk committed
104
        throw std::runtime_error("MAP_PRIVATE is not implemented on this system");
Zhekai Zhang's avatar
Zhekai Zhang committed
105
    }
106
107
108
109
110
111
112
113
114
115
116
117

    virtual size_t size() override {
        return 0;
    }
    virtual const char *data() override {
        return nullptr;
    }
};

#endif

SafeTensors::SafeTensors(const std::string &filename) {
muyangli's avatar
muyangli committed
118
    this->hostRegistered = false;
Muyang Li's avatar
Muyang Li committed
119
    this->memoryPinned   = false;
120

muyangli's avatar
muyangli committed
121
    auto methodPrivate = [&]() {
122
        this->mapped = std::make_unique<MMapImplPrivate>(filename);
Muyang Li's avatar
Muyang Li committed
123
124
        checkCUDA(
            cudaHostRegister(const_cast<char *>(this->mapped->data()), this->mapped->size(), cudaHostRegisterPortable));
muyangli's avatar
muyangli committed
125
        this->hostRegistered = true;
Muyang Li's avatar
Muyang Li committed
126
        this->memoryPinned   = true;
muyangli's avatar
muyangli committed
127
128
129
    };
    auto methodMio = [&]() {
        this->mapped = std::make_unique<MMapImplMio>(filename);
Muyang Li's avatar
Muyang Li committed
130
131
132
        checkCUDA(cudaHostRegister(const_cast<char *>(this->mapped->data()),
                                   this->mapped->size(),
                                   cudaHostRegisterPortable | cudaHostRegisterReadOnly));
muyangli's avatar
muyangli committed
133
        this->hostRegistered = true;
Muyang Li's avatar
Muyang Li committed
134
        this->memoryPinned   = true;
muyangli's avatar
muyangli committed
135
136
    };
    auto methodRead = [&]() {
Muyang Li's avatar
Muyang Li committed
137
        this->mapped       = std::make_unique<MMapImplRead>(filename, true);
muyangli's avatar
muyangli committed
138
139
        this->memoryPinned = true;
    };
Muyang Li's avatar
Muyang Li committed
140
141
    auto methodReadNopin = [&]() { this->mapped = std::make_unique<MMapImplRead>(filename, false); };

muyangli's avatar
muyangli committed
142
    const std::map<std::string, std::function<void()>> methods = {
Muyang Li's avatar
Muyang Li committed
143
144
145
146
        {"PRIVATE", methodPrivate},
        {"MIO", methodMio},
        {"READ", methodRead},
        {"READNOPIN", methodReadNopin},
muyangli's avatar
muyangli committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    };

    auto tryMethod = [&](std::string name) {
        spdlog::debug("Trying to load safetensors using method {}", name);
        this->mapped.reset();
        try {
            methods.at(name)();
            return true;
        } catch (std::exception &e) {
            spdlog::warn("Failed to load safetensors using method {}: {}", name, e.what());
        }
        return false;
    };

    if (char *env = getenv("NUNCHAKU_LOAD_METHOD")) {
        std::string method = std::string(env);
        tryMethod(method);
    } else {

#ifdef __linux__
        tryMethod("PRIVATE") || tryMethod("MIO") || tryMethod("READ") || tryMethod("READNOPIN");
#else
        tryMethod("MIO") || tryMethod("READ") || tryMethod("READNOPIN");
170
#endif
muyangli's avatar
muyangli committed
171
172
173
174
175
176
177
178
    }

    if (!this->mapped) {
        throw std::runtime_error("Failed to load safetensors");
    }

    if (!this->memoryPinned) {
        spdlog::warn("Memory not pinned");
Zhekai Zhang's avatar
Zhekai Zhang committed
179
    }
180

Zhekai Zhang's avatar
Zhekai Zhang committed
181
182
183
184
    parseHeader();
}

SafeTensors::~SafeTensors() {
muyangli's avatar
muyangli committed
185
186
187
188
189
    if (this->hostRegistered) {
        if (cudaHostUnregister(const_cast<char *>(this->mapped->data())) != cudaSuccess) {
            spdlog::warn("cudaHostUnregister failed: {}", cudaGetErrorString(cudaGetLastError()));
        }
    }
Zhekai Zhang's avatar
Zhekai Zhang committed
190
191
192
193
}

void SafeTensors::parseHeader() {
    static const std::unordered_map<std::string, Tensor::ScalarType> mapDType = {
Muyang Li's avatar
Muyang Li committed
194
195
196
197
198
199
200
201
        {"BF16", Tensor::BF16},
        {"F16", Tensor::FP16},
        {"F32", Tensor::FP32},
        {"I8", Tensor::INT8},
        {"I32", Tensor::INT32},
        {"I64", Tensor::INT64},
        {"F8_E4M3", Tensor::FP8_E4M3},
        {"F8_E5M2", Tensor::FP8_E5M2},
Zhekai Zhang's avatar
Zhekai Zhang committed
202
203
204
205
    };

    auto check = [](bool cond, std::source_location location = std::source_location::current()) {
        if (!cond) {
Muyang Li's avatar
Muyang Li committed
206
207
            throw std::runtime_error(
                format("Safetensors check failed at {}:{}", location.file_name(), location.line()));
Zhekai Zhang's avatar
Zhekai Zhang committed
208
209
210
211
212
213
214
        }
    };

    check(this->mapped->size() > 8);
    uint64_t sizeHeader = *reinterpret_cast<const uint64_t *>(this->mapped->data());

    check(this->mapped->size() - 8 >= sizeHeader);
215
    json header = json::parse(this->mapped->data() + 8, this->mapped->data() + 8 + sizeHeader);
Zhekai Zhang's avatar
Zhekai Zhang committed
216
217
218
219
220
221
222
223
224

    const uint64_t offsetMax = this->mapped->size() - sizeHeader - 8;
    std::set<size_t> offsets;

    for (auto &&[key, info] : header.items()) {
        if (key == "__metadata__") {
            continue;
        }

Muyang Li's avatar
Muyang Li committed
225
226
227
        auto dtype = mapDType.at(info["dtype"].get<std::string>());
        ;
        auto shape        = info["shape"].get<std::vector<int>>();
Zhekai Zhang's avatar
Zhekai Zhang committed
228
229
230
231
232
233
234
235
236
237
238
        auto data_offsets = info["data_offsets"].get<std::vector<uint64_t>>();

        check(data_offsets.size() == 2);
        check(data_offsets[0] <= data_offsets[1]);
        check(data_offsets[0] < offsetMax);
        check(data_offsets[1] <= offsetMax);
        for (auto &&dim : shape) {
            check(dim >= 0);
        }

        TensorInfo tinfo;
Muyang Li's avatar
Muyang Li committed
239
240
        tinfo.type   = dtype;
        tinfo.shape  = TensorShape(shape);
Zhekai Zhang's avatar
Zhekai Zhang committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
        tinfo.length = data_offsets[1] - data_offsets[0];
        tinfo.offset = 8 + sizeHeader + data_offsets[0];

        // TODO: check range overlap
        check(!offsets.contains(tinfo.offset));
        offsets.insert(tinfo.offset);

        check(tinfo.shape.size() * Tensor::scalarSize.at(tinfo.type) <= tinfo.length);

        tensors[key] = tinfo;
    }
}

Tensor SafeTensors::getTensor(const std::string &key) {
    if (!tensors.contains(key)) {
        return Tensor{};
    }
    TensorInfo &info = tensors.at(key);

    std::shared_ptr<BufferMMap> buffer = info.buffer.lock();
    if (!buffer) {
Muyang Li's avatar
Muyang Li committed
262
263
        buffer = std::make_shared<BufferMMap>(
            const_cast<char *>(this->mapped->data() + info.offset), info.length, shared_from_this());
Zhekai Zhang's avatar
Zhekai Zhang committed
264
265
266
267
        info.buffer = buffer;
    }

    Tensor result;
Muyang Li's avatar
Muyang Li committed
268
    result.shape      = info.shape;
Zhekai Zhang's avatar
Zhekai Zhang committed
269
    result.scalarType = info.type;
Muyang Li's avatar
Muyang Li committed
270
    result.buffer     = buffer;
Zhekai Zhang's avatar
Zhekai Zhang committed
271
272
273

    return result;
}