Skip to content

Commit d854d98

Browse files
authored
cuda : use std::string instead of VLA for archName (ggml-org#30463)
* cuda : use std::string instead of VLA for archName Modernize the code (old C style) and fix the following warnings: ``` ggml/src/ggml-cuda/ggml-cuda.cu:180:19: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension] 180 | char archName[archLen + 1]; | ^~~~~~~~~~~ ggml/src/ggml-cuda/ggml-cuda.cu:180:19: note: read of non-const variable 'archLen' is not allowed in a constant expression ggml/src/ggml-cuda/ggml-cuda.cu:179:9: note: declared here 179 | int archLen = strlen(devName); | ^ ``` Signed-off-by: Adrien Gallouët <angt@huggingface.co> * Add a comment about archName Signed-off-by: Adrien Gallouët <adrien@gallouet.fr> --------- Signed-off-by: Adrien Gallouët <angt@huggingface.co> Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
1 parent 48c9f08 commit d854d98

1 file changed

Lines changed: 18 additions & 34 deletions

File tree

‎ggml/src/ggml-cuda/ggml-cuda.cu‎

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -214,52 +214,36 @@ static cudaError_t ggml_cuda_device_malloc(void ** ptr, size_t size, int device)
214214
}
215215

216216
#if defined(GGML_USE_HIP)
217-
static int ggml_cuda_parse_id(char devName[]) {
217+
static int ggml_cuda_parse_id(const char * devName) {
218218
// A list of possible Target IDs can be found under the rocclr/clr repo in device.cpp
219219
// these values are not stable so this is susceptible to breakage
220220
// https://github.com/ROCm/clr/blob/amd-staging/rocclr/device/device.cpp
221-
int archMajor = 0x0;
222-
int archMinor = 0x0;
223-
int archNum = GGML_CUDA_CC_OFFSET_AMD;
224-
int archLen = strlen(devName);
225-
char archName[archLen + 1];
221+
// Extract the base architecture model ("gfx908:sramecc+:xnack-" -> "908")
222+
std::string archName = devName;
223+
archName.erase(0, 3);
224+
archName = archName.substr(0, archName.find(':'));
226225

227-
// strip leading 'gfx' while copying into our buffer
228-
if (archLen > 3) {
229-
strcpy(archName, &devName[3]);
230-
archLen -= 3;
231-
}
226+
const auto hex = [](const std::string & s) { return (int) strtoul(s.c_str(), nullptr, 16); };
232227

233-
// trim trailing :xnack- or :sramecc- statuses
234-
archLen = strcspn(archName, ":");
235-
archName[archLen] = '\0';
228+
int cc = 0;
236229

237230
// tease out the version information
238-
if (archLen > 8) {
231+
if (archName.size() > 8) {
239232
// versions labeled generic use '-' as delimiter
240-
// strip the trailing "-generic" then iterate through what remains
241-
if ((strstr(archName, "-generic"))) {
242-
archName[archLen - 8] = '\0';
243-
char * pch;
244-
if ((pch = strtok(archName, "-"))) {
245-
archMajor = (int)strtoul(pch, 0, 16);
246-
if ((pch = strtok(NULL, "-"))) {
247-
archMinor = 0x10 * (int)strtoul(pch, 0, 16);
248-
}
233+
if (archName.compare(archName.size() - 8, 8, "-generic") == 0) {
234+
archName.resize(archName.size() - 8);
235+
const size_t sep = archName.find('-');
236+
cc = 0x100 * hex(archName.substr(0, sep));
237+
if (sep != std::string::npos) {
238+
cc += 0x10 * hex(archName.substr(sep + 1));
249239
}
250240
}
251-
} else if (archLen >= 3) {
252-
// last two digits should be the minor * 0x10 + stepping
253-
archMinor = (int)strtoul(&archName[archLen - 2], 0, 16);
254-
archName[archLen - 2] = '\0';
255-
256-
// only the major version remains
257-
archMajor = (int)strtoul(archName, 0, 16);
241+
} else if (archName.size() >= 3) {
242+
// ordinary IDs are the version in hex
243+
cc = hex(archName);
258244
}
259-
archNum += archMajor * 0x100;
260-
archNum += archMinor;
261245

262-
return archNum;
246+
return GGML_CUDA_CC_OFFSET_AMD + cc;
263247
}
264248
#endif // defined(GGML_USE_HIP)
265249

0 commit comments

Comments
 (0)