File size: 7,600 Bytes
00db36f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
57
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifdef _WIN32
// Windows implementation: GDS (GPUDirect Storage) not available, so
// GDSStreamReader is fully stubbed. StreamReader uses a normal std::ifstream
// since that's how engines are loaded from disk on Windows.
#include "tllmStreamReaders.h"
#include "tensorrt_llm/common/assert.h"
#include "tensorrt_llm/common/logger.h"

#include <fstream>

StreamReader::StreamReader(std::filesystem::path fp)
    : mFile(fp, std::ios::binary)
{
    TLLM_CHECK_WITH_INFO(mFile.good(), "Failed to open file for reading: %s", fp.string().c_str());
}

StreamReader::~StreamReader() = default;

int64_t StreamReader::read(void* destination, int64_t nbBytes)

{
    if (!mFile.good())
    {
        return -1;
    }
    mFile.read(static_cast<char*>(destination), nbBytes);
    return static_cast<int64_t>(mFile.gcount());
}

// GDSStreamReader is fully stubbed on Windows (no cuFile/GDS support).
GDSStreamReader::GDSStreamReader(std::filesystem::path const& /*filePath*/) {}

GDSStreamReader::~GDSStreamReader() = default;

void GDSStreamReader::close() {}

bool GDSStreamReader::isOpen() const

{
    return false;
}

bool GDSStreamReader::open(std::string const& /*filepath*/)

{
    return false;
}

int64_t GDSStreamReader::read(void* /*dest*/, int64_t /*bytes*/, cudaStream_t /*stream*/) noexcept

{
    return -1;
}

void GDSStreamReader::reset() {}

bool GDSStreamReader::seek(int64_t /*offset*/, nvinfer1::SeekPosition /*where*/) noexcept

{
    return false;
}

bool GDSStreamReader::initializeDriver()

{
    return false;
}
#else
/*

 * Copyright (c) 2022-2024, NVIDIA CORPORATION.  All rights reserved.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

#include "tllmStreamReaders.h"
#include "tensorrt_llm/common/assert.h"
#include "tensorrt_llm/common/logger.h"

#include <cufile.h>
#ifndef _WIN32
#include <dlfcn.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#endif
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <string>
#ifndef _WIN32
#include <unistd.h>
#endif

// Non-GDS StreamReader

StreamReader::StreamReader(std::filesystem::path fp)
{
    mFile.open(fp.string(), std::ios::binary | std::ios::in);
    TLLM_CHECK_WITH_INFO(mFile.good(), std::string("Error opening engine file: " + fp.string()));
}

StreamReader::~StreamReader()
{
    if (mFile.is_open())
    {
        mFile.close();
    }
}

int64_t StreamReader::read(void* destination, int64_t nbBytes)

{
    if (!mFile.good())
    {
        return -1;
    }

    mFile.read(static_cast<char*>(destination), nbBytes);

    return mFile.gcount();
}

// StreamReader using GDS

GDSStreamReader::GDSStreamReader(std::filesystem::path const& filePath)
{
    auto const start_time = std::chrono::high_resolution_clock::now();
    initializeDriver();
    auto const elapsed_ms
        = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start_time);

    TLLM_LOG_INFO("GDS driver initialization time %lld ms", elapsed_ms);

    open(filePath);
}

bool GDSStreamReader::open(std::string const& filepath)

{
    if (!initializeDriver())
    {
        TLLM_LOG_INFO("Failed to initialize cuFile driver");
        return false;
    }

    int32_t const ret = ::open(filepath.c_str(), O_CREAT | O_RDWR | O_DIRECT, 0664);

    if (ret < 0)
    {
        TLLM_LOG_INFO("Failed to open engine file");
        return false;
    }

    mFd = ret;
    mFileSize = lseek(mFd, 0, SEEK_END);
    lseek(mFd, 0, SEEK_SET);

    CUfileDescr_t fileDescr;
    memset((void*) &fileDescr, 0, sizeof(fileDescr));
    fileDescr.handle.fd = mFd;
    fileDescr.type = CU_FILE_HANDLE_TYPE_OPAQUE_FD;

    CUfileError_t gdsStatus = cuFileHandleRegister(&mFileHandle, &fileDescr);

    if (gdsStatus.err != CU_FILE_SUCCESS)
    {
        TLLM_LOG_INFO("Failed to cuFileHandleRegister");
        ::close(mFd);
        return false;
    }
    return true;
}

void GDSStreamReader::close()

{
    if (mFd >= 0)
    {
        ::close(mFd);
        mFd = -1;
    }
}

GDSStreamReader::~GDSStreamReader()
{
    if (mFileHandle)
    {
        cuFileHandleDeregister(mFileHandle);
        mFileHandle = nullptr;
    }

    if (mDriverInitialized)
    {
        cuFileDriverClose();
    }
}

bool GDSStreamReader::seek(int64_t offset, nvinfer1::SeekPosition where) noexcept

{
    switch (where)
    {
    case nvinfer1::SeekPosition::kSET: mCursor = offset; return true;
    case nvinfer1::SeekPosition::kCUR: mCursor += offset; return true;
    case nvinfer1::SeekPosition::kEND: mCursor = -offset; return true;
    default: return false;
    }
    return true;
}

int64_t GDSStreamReader::read(void* dest, int64_t bytes, cudaStream_t stream) noexcept

{
    cudaPointerAttributes attributes{};
    if (cudaPointerGetAttributes(&attributes, dest) != cudaSuccess)
    {
        TLLM_LOG_INFO("cudaPointerGetAttributes failed");
    }

    off_t destOffset = 0;
    void* destBase = dest;

    if (attributes.type == cudaMemoryTypeDevice)
    {
        CUdeviceptr cuDest = reinterpret_cast<CUdeviceptr>(dest);
        CUdeviceptr cuBufBase = 0;
        size_t cuBufSize = 0;

        cuMemGetAddressRange(&cuBufBase, &cuBufSize, cuDest);
        destOffset += cuDest - cuBufBase;
        destBase = reinterpret_cast<void*>(cuBufBase);
    }
    cuFileRead(this->mFileHandle, destBase, bytes, mCursor, destOffset);

    mCursor += bytes;
    return bytes;
}

void GDSStreamReader::reset()

{
    lseek(mFd, 0, SEEK_SET);
    mCursor = 0;
}

[[nodiscard]] bool GDSStreamReader::isOpen() const

{
    bool open = mFd >= 0;
    return open;
}

bool GDSStreamReader::initializeDriver()

{
    if (mDriverInitialized)
    {
        return true;
    }

    mCuFileLibHandle = dlopen("libcufile.so", RTLD_LAZY | RTLD_GLOBAL);
    if (!mCuFileLibHandle)
    {
        TLLM_LOG_INFO("Failed to dlopen libcufile.so");
        return false;
    }

    // Load the required functions
    *reinterpret_cast<void**>(&cuFileDriverOpen) = dlsym(mCuFileLibHandle, "cuFileDriverOpen");
    *reinterpret_cast<void**>(&cuFileHandleRegister) = dlsym(mCuFileLibHandle, "cuFileHandleRegister");
    *reinterpret_cast<void**>(&cuFileHandleDeregister) = dlsym(mCuFileLibHandle, "cuFileHandleDeregister");
    *reinterpret_cast<void**>(&cuFileDriverClose) = dlsym(mCuFileLibHandle, "cuFileDriverClose");
    *reinterpret_cast<void**>(&cuFileRead) = dlsym(mCuFileLibHandle, "cuFileRead");

    if (!cuFileDriverOpen || !cuFileHandleRegister || !cuFileHandleDeregister || !cuFileDriverClose || !cuFileRead)
    {
        TLLM_LOG_INFO("Failed to dlsym libcufile.so");
        return false;
    }

    CUfileError_t gdsStatus = cuFileDriverOpen();
    if (gdsStatus.err != CU_FILE_SUCCESS)
    {
        TLLM_LOG_INFO("cuFileDriverOpen failed");
        return false;
    }

    mDriverInitialized = true;
    return true;
}

#endif // _WIN32