Skip to content

Commit e5afc19

Browse files
authored
Remove C++17 features/syntax (#173)
1 parent f59101d commit e5afc19

File tree

10 files changed

+65
-60
lines changed

10 files changed

+65
-60
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
126126
message("Using MSVC as compiler, default target on Windows 10. "
127127
"If the target system is not Windows 10, please update _WIN32_WINNT "
128128
"to corresponding value.")
129-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
130129
target_compile_options(
131130
triton-core-serverstub
132131
PRIVATE

src/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
cmake_minimum_required(VERSION 3.18)
2828

2929
project(libtritonserver LANGUAGES C CXX)
30-
set(CMAKE_CXX_STANDARD 17)
31-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3230

3331
if(NOT CMAKE_BUILD_TYPE)
3432
set(CMAKE_BUILD_TYPE Release)
@@ -233,13 +231,11 @@ add_library(
233231
TritonCore::triton-core ALIAS triton-core
234232
)
235233

236-
target_compile_features(triton-core PRIVATE cxx_std_17)
234+
target_compile_features(triton-core PRIVATE cxx_std_11)
237235
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
238236
message("Using MSVC as compiler, default target on Windows 10. "
239237
"If the target system is not Windows 10, please update _WIN32_WINNT "
240238
"to corresponding value.")
241-
# WAR for target_compile_features not applying correctly to MSVC.
242-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
243239
target_compile_options(
244240
triton-core
245241
PRIVATE

src/cache_entry.cc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,26 @@ namespace triton { namespace core {
3434
size_t
3535
CacheEntry::BufferCount()
3636
{
37-
std::unique_lock lk(buffer_mu_);
37+
std::unique_lock<std::mutex> lk(buffer_mu_);
3838
return buffers_.size();
3939
}
4040

4141
const std::vector<Buffer>&
4242
CacheEntry::Buffers()
4343
{
44-
std::unique_lock lk(buffer_mu_);
44+
std::unique_lock<std::mutex> lk(buffer_mu_);
4545
return buffers_;
4646
}
4747

4848
std::vector<Buffer>&
4949
CacheEntry::MutableBuffers()
5050
{
51-
std::unique_lock lk(buffer_mu_);
51+
std::unique_lock<std::mutex> lk(buffer_mu_);
5252
return buffers_;
5353
}
5454

5555
void
56-
CacheEntry::AddBuffer(boost::span<std::byte> byte_span)
56+
CacheEntry::AddBuffer(boost::span<Byte> byte_span)
5757
{
5858
void* base = static_cast<void*>(byte_span.data());
5959
AddBuffer(base, byte_span.size());
@@ -62,18 +62,19 @@ CacheEntry::AddBuffer(boost::span<std::byte> byte_span)
6262
void
6363
CacheEntry::AddBuffer(void* base, size_t byte_size)
6464
{
65-
std::unique_lock lk(buffer_mu_);
65+
std::unique_lock<std::mutex> lk(buffer_mu_);
6666
buffers_.emplace_back(std::make_pair(base, byte_size));
6767
}
6868

6969
CacheEntry::~CacheEntry()
7070
{
71-
std::unique_lock lk(buffer_mu_);
71+
std::unique_lock<std::mutex> lk(buffer_mu_);
7272
if (!free_buffers_) {
7373
return;
7474
}
7575

76-
for (auto& [base, _] : buffers_) {
76+
for (auto& iter : buffers_) {
77+
auto& base = iter.first;
7778
if (base) {
7879
free(base);
7980
base = nullptr;
@@ -90,7 +91,7 @@ CacheEntry::AddPlaceholderBuffer(size_t byte_size)
9091
// Set the size of each buffer in the CacheEntry object so the cache knows
9192
// how much to allocate for each entry before insertion.
9293
Status
93-
CacheEntry::SetBufferSizes(std::vector<boost::span<std::byte>> buffers)
94+
CacheEntry::SetBufferSizes(std::vector<boost::span<Byte>> buffers)
9495
{
9596
for (const auto buffer : buffers) {
9697
AddPlaceholderBuffer(buffer.size());
@@ -163,7 +164,7 @@ CacheEntry::SerializeResponse(InferenceResponse* response, Buffer& buffer)
163164
// The packed_response buffer will look like:
164165
// [num_outputs, sizeof(output1), output1, ..., sizeof(outputN), outputN]
165166
size_t position = 0;
166-
std::byte* base = static_cast<std::byte*>(buffer.first);
167+
Byte* base = static_cast<Byte*>(buffer.first);
167168
// 1. First the packed buffer will hold the number of outputs as a uint32_t
168169
uint32_t num_outputs = response->Outputs().size();
169170
std::memcpy(base, &num_outputs, sizeof(uint32_t));
@@ -193,8 +194,7 @@ CacheEntry::SerializeResponse(InferenceResponse* response, Buffer& buffer)
193194

194195
Status
195196
CacheEntry::SerializeResponseOutput(
196-
const InferenceResponse::Output& output, std::byte* buffer,
197-
size_t* output_size)
197+
const InferenceResponse::Output& output, Byte* buffer, size_t* output_size)
198198
{
199199
if (!buffer) {
200200
return Status(Status::Code::INVALID_ARG, "buffer arg was nullptr");
@@ -312,7 +312,7 @@ CacheEntry::DeserializeBuffer(InferenceResponse* response, const Buffer& buffer)
312312
}
313313

314314

315-
const std::byte* base = static_cast<std::byte*>(buffer.first);
315+
const Byte* base = static_cast<Byte*>(buffer.first);
316316
if (!base) {
317317
return Status(Status::Code::INTERNAL, "buffer was nullptr");
318318
}
@@ -441,7 +441,7 @@ CacheEntry::GetByteSize(
441441

442442
Status
443443
CacheEntry::DeserializeResponseOutput(
444-
boost::span<const std::byte> packed_bytes, CacheOutput* output)
444+
boost::span<const Byte> packed_bytes, CacheOutput* output)
445445
{
446446
if (!output) {
447447
return Status(Status::Code::INVALID_ARG, "output arg was nullptr");
@@ -453,17 +453,17 @@ CacheEntry::DeserializeResponseOutput(
453453
memcpy(&name_byte_size, packed_bytes.begin() + position, sizeof(uint32_t));
454454
position += sizeof(uint32_t);
455455

456-
std::string name(name_byte_size, 'x');
457-
memcpy(name.data(), packed_bytes.begin() + position, name_byte_size);
456+
auto name_start = packed_bytes.begin() + position;
457+
std::string name(name_start, name_start + name_byte_size);
458458
position += name_byte_size;
459459

460460
// Dtype
461461
uint32_t dtype_byte_size = 0;
462462
memcpy(&dtype_byte_size, packed_bytes.begin() + position, sizeof(uint32_t));
463463
position += sizeof(uint32_t);
464464

465-
std::string dtype(dtype_byte_size, 'x');
466-
memcpy(dtype.data(), packed_bytes.begin() + position, dtype_byte_size);
465+
auto dtype_start = packed_bytes.begin() + position;
466+
std::string dtype(dtype_start, dtype_start + dtype_byte_size);
467467
position += dtype_byte_size;
468468

469469
// Shape

src/cache_entry.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CacheEntry {
6262
const std::vector<Buffer>& Buffers();
6363
std::vector<Buffer>& MutableBuffers();
6464
size_t BufferCount();
65-
void AddBuffer(boost::span<std::byte> buffer);
65+
void AddBuffer(boost::span<Byte> buffer);
6666
void AddBuffer(void* base, size_t byte_size);
6767

6868
/* Insert helpers */
@@ -81,7 +81,7 @@ class CacheEntry {
8181
// and sets entry buffer sizes
8282
Status SetBufferSizes(boost::span<InferenceResponse*> responses);
8383
// Directly sets entry buffer sizes from provided buffers
84-
Status SetBufferSizes(std::vector<boost::span<std::byte>> buffers);
84+
Status SetBufferSizes(std::vector<boost::span<Byte>> buffers);
8585

8686
/* Lookup helpers */
8787
Status DeserializeBuffers(boost::span<InferenceResponse*> responses);
@@ -95,7 +95,7 @@ class CacheEntry {
9595
// Insert helpers
9696
Status SerializeResponse(InferenceResponse* response, Buffer& buffer);
9797
Status SerializeResponseOutput(
98-
const InferenceResponse::Output& output, std::byte* buffer,
98+
const InferenceResponse::Output& output, Byte* buffer,
9999
size_t* output_size);
100100
Status SetBufferSize(InferenceResponse* response);
101101
// Calculates total byte size required to serialize response output and
@@ -107,7 +107,7 @@ class CacheEntry {
107107
// Lookup helpers
108108
Status DeserializeBuffer(InferenceResponse* response, const Buffer& buffer);
109109
Status DeserializeResponseOutput(
110-
boost::span<const std::byte> packed_bytes, CacheOutput* output);
110+
boost::span<const Byte> packed_bytes, CacheOutput* output);
111111

112112
// NOTE: performance gain may be possible by removing this mutex and
113113
// guaranteeing that no two threads will access/modify an entry

src/cache_manager.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ CacheToBytesAllocator::Allocate(TRITONCACHE_CacheEntry* entry)
8686

8787
// NOTE: If the same entry object is re-used for multiple lookups and the
8888
// entry buffers are not freed between uses, this will leak memory.
89-
for (auto& [base, byte_size] : buffers) {
89+
for (auto& iter : buffers) {
90+
auto& base = iter.first;
91+
const auto& byte_size = iter.second;
92+
9093
void* new_base = malloc(byte_size);
9194
std::memcpy(new_base, base, byte_size);
9295
base = new_base;
@@ -98,7 +101,7 @@ CacheToBytesAllocator::Allocate(TRITONCACHE_CacheEntry* entry)
98101
}
99102

100103
BytesToCacheAllocator::BytesToCacheAllocator(
101-
std::vector<boost::span<std::byte>> buffers)
104+
std::vector<boost::span<Byte>> buffers)
102105
{
103106
// Span is a read-only view of the underlying buffer, this should only
104107
// perform a shallow copy of base pointer and size of each span.
@@ -126,7 +129,8 @@ BytesToCacheAllocator::Allocate(TRITONCACHE_CacheEntry* entry)
126129
// Copy from allocator provided buffers into cache-allocated buffers
127130
// that were setup in 'entry' by cache implementation.
128131
for (size_t i = 0; i < buffers_.size(); i++) {
129-
auto [cache_buffer, cache_buffer_size] = cache_buffers[i];
132+
auto cache_buffer = cache_buffers[i].first;
133+
auto cache_buffer_size = cache_buffers[i].second;
130134
if (buffers_[i].size() != cache_buffer_size) {
131135
return Status(
132136
Status::Code::INTERNAL,
@@ -363,7 +367,7 @@ TritonCache::Insert(
363367

364368
Status
365369
TritonCache::Insert(
366-
std::vector<boost::span<std::byte>> buffers, const std::string& key)
370+
std::vector<boost::span<Byte>> buffers, const std::string& key)
367371
{
368372
std::unique_ptr<CacheEntry> entry = std::make_unique<CacheEntry>();
369373
RETURN_IF_ERROR(entry->SetBufferSizes(buffers));

src/cache_manager.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ class CacheToBytesAllocator : TritonCacheAllocator {
7777

7878
class BytesToCacheAllocator : TritonCacheAllocator {
7979
public:
80-
BytesToCacheAllocator(std::vector<boost::span<std::byte>> buffers);
80+
BytesToCacheAllocator(std::vector<boost::span<Byte>> buffers);
8181
Status Allocate(TRITONCACHE_CacheEntry* entry);
8282

8383
private:
84-
std::vector<boost::span<std::byte>> buffers_;
84+
std::vector<boost::span<Byte>> buffers_;
8585
};
8686

8787

@@ -100,8 +100,7 @@ class TritonCache {
100100
Status Insert(InferenceResponse* response, const std::string& key);
101101
Status Insert(
102102
boost::span<InferenceResponse*> responses, const std::string& key);
103-
Status Insert(
104-
std::vector<boost::span<std::byte>> buffers, const std::string& key);
103+
Status Insert(std::vector<boost::span<Byte>> buffers, const std::string& key);
105104
Status Insert(
106105
CacheEntry* entry, const std::string& key,
107106
TRITONCACHE_Allocator* allocator);

src/constants.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
1+
// Copyright 2018-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
//
33
// Redistribution and use in source and binary forms, with or without
44
// modification, are permitted provided that the following conditions
@@ -93,6 +93,9 @@ constexpr int METRIC_REPORTER_ID_CPU = -1;
9393
constexpr int METRIC_REPORTER_ID_RESPONSE_CACHE = -2;
9494
#endif
9595

96+
// Note: This can be replaced with std::byte starting in c++17
97+
using Byte = unsigned char;
98+
9699
#define TIMESPEC_TO_NANOS(TS) \
97100
((TS).tv_sec * triton::core::NANOS_PER_SECOND + (TS).tv_nsec)
98101
#define TIMESPEC_TO_MILLIS(TS) \

src/server.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ InferenceServer::Init()
167167
}
168168

169169
// Initialize each cache with its respective config
170-
for (const auto& [name, config] : cache_config_map_) {
170+
for (const auto& iter : cache_config_map_) {
171+
const auto& name = iter.first;
172+
const auto& config = iter.second;
171173
std::shared_ptr<TritonCache> cache;
172174
status = cache_manager_->CreateCache(name, config, &cache);
173175
if (!status.IsOk()) {

src/test/response_cache_test.cc

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ GenerateRequest(
438438
tc::Status
439439
InsertLookupCompare(
440440
std::shared_ptr<tc::TritonCache> cache,
441-
std::vector<boost::span<std::byte>> expected_buffers,
442-
const std::string& key)
441+
std::vector<boost::span<tc::Byte>> expected_buffers, const std::string& key)
443442
{
444443
if (!cache) {
445444
return tc::Status(tc::Status::Code::INTERNAL, "cache was nullptr");
@@ -448,14 +447,14 @@ InsertLookupCompare(
448447
}
449448

450449
helpers::CheckStatus(cache->Insert(expected_buffers, key));
451-
auto lookup_entry = tc::CacheEntry();
452-
auto status = cache->Lookup(key, &lookup_entry);
450+
auto lookup_entry = std::make_unique<tc::CacheEntry>();
451+
auto status = cache->Lookup(key, lookup_entry.get());
453452
if (!status.IsOk()) {
454453
return tc::Status(
455454
tc::Status::Code::INTERNAL, "Lookup failed: " + status.Message());
456455
}
457456

458-
auto lookup_buffers = lookup_entry.Buffers();
457+
auto lookup_buffers = lookup_entry->Buffers();
459458
if (lookup_buffers.size() != expected_buffers.size()) {
460459
return tc::Status(
461460
tc::Status::Code::INTERNAL,
@@ -464,10 +463,10 @@ InsertLookupCompare(
464463
}
465464

466465
for (size_t b = 0; b < expected_buffers.size(); b++) {
467-
boost::span<std::byte> lookup = {
468-
static_cast<std::byte*>(lookup_buffers[b].first),
466+
boost::span<tc::Byte> lookup = {
467+
static_cast<tc::Byte*>(lookup_buffers[b].first),
469468
lookup_buffers[b].second};
470-
boost::span<std::byte> expected = expected_buffers[b];
469+
boost::span<tc::Byte> expected = expected_buffers[b];
471470
if (!std::equal(
472471
lookup.begin(), lookup.end(), expected.begin(), expected.end())) {
473472
return tc::Status(
@@ -656,9 +655,9 @@ TEST_F(RequestResponseCacheTest, TestCacheSizeSmallerThanEntryBytes)
656655
ASSERT_NE(cache, nullptr);
657656

658657
// Setup byte buffer larger than cache size
659-
std::vector<std::byte> large_data(cache_size + 1);
658+
std::vector<tc::Byte> large_data(cache_size + 1);
660659
// Setup entry
661-
std::vector<boost::span<std::byte>> entry;
660+
std::vector<boost::span<tc::Byte>> entry;
662661
entry.push_back(large_data);
663662

664663
auto status = cache->Insert(entry, "large_bytes");
@@ -724,13 +723,13 @@ TEST_F(RequestResponseCacheTest, TestCacheInsertLookupCompareBytes)
724723
auto cache = helpers::CreateCache(1024);
725724
ASSERT_NE(cache, nullptr);
726725
// Setup byte buffers
727-
std::vector<std::byte> buffer1{1, std::byte{0x01}};
728-
std::vector<std::byte> buffer2{2, std::byte{0x02}};
729-
std::vector<std::byte> buffer3{4, std::byte{0x04}};
730-
std::vector<std::byte> buffer4{8, std::byte{0x08}};
731-
std::vector<std::byte> buffer5{16, std::byte{0xFF}};
726+
std::vector<tc::Byte> buffer1{1, tc::Byte{1}};
727+
std::vector<tc::Byte> buffer2{2, tc::Byte{2}};
728+
std::vector<tc::Byte> buffer3{4, tc::Byte{4}};
729+
std::vector<tc::Byte> buffer4{8, tc::Byte{8}};
730+
std::vector<tc::Byte> buffer5{16, tc::Byte{16}};
732731
// Setup entry
733-
std::vector<boost::span<std::byte>> entry;
732+
std::vector<boost::span<tc::Byte>> entry;
734733
// Add buffers to entry
735734
entry.push_back(buffer1);
736735
entry.push_back(buffer2);
@@ -788,8 +787,8 @@ TEST_F(RequestResponseCacheTest, TestParallelInsert)
788787
size_t cache_misses = 0;
789788
for (size_t idx = 0; idx < thread_count; idx++) {
790789
auto key = std::to_string(idx);
791-
auto entry = tc::CacheEntry();
792-
auto status = cache->Lookup(key, &entry);
790+
auto entry = std::make_unique<tc::CacheEntry>();
791+
auto status = cache->Lookup(key, entry.get());
793792
if (status.IsOk()) {
794793
cache_hits++;
795794
} else {
@@ -828,9 +827,10 @@ TEST_F(RequestResponseCacheTest, TestParallelLookup)
828827
size_t cache_hits = 0;
829828
size_t cache_misses = 0;
830829
for (size_t idx = 0; idx < thread_count; idx++) {
831-
auto entry = tc::CacheEntry();
832830
auto key = std::to_string(idx);
833-
auto status = cache->Lookup(key, &entry);
831+
auto entry = std::make_unique<tc::CacheEntry>();
832+
auto status = cache->Lookup(key, entry.get());
833+
834834
if (status.IsOk()) {
835835
cache_hits++;
836836
} else {

0 commit comments

Comments
 (0)