Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] Fixed detect platform for different CUDA version #2527

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions api/src/main/java/ai/djl/util/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import ai.djl.util.cuda.CudaUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -25,6 +28,8 @@
*/
public final class Platform {

private static final Logger logger = LoggerFactory.getLogger(Platform.class);

private String version;
private String apiVersion;
private String osPrefix;
Expand Down Expand Up @@ -285,15 +290,19 @@ public boolean matches(Platform system) {
if (!osPrefix.equals(system.osPrefix) || !osArch.equals(system.osArch)) {
return false;
}
// if system Machine is GPU
if (system.flavor.startsWith("cu")) {
// system flavor doesn't contain mkl, but MXNet has: cu110mkl
return flavor.startsWith("cpu")
|| "mkl".equals(flavor)
|| Integer.parseInt(flavor.substring(2, 5))
<= Integer.parseInt(system.flavor.substring(2, 5));
if (flavor.startsWith("cpu") || "mkl".equals(flavor)) {
// CPU package can run on all system platform
return true;
}

// native package can run on system which major version is greater or equal
if (system.flavor.startsWith("cu")
&& Integer.parseInt(flavor.substring(2, 4))
<= Integer.parseInt(system.flavor.substring(2, 4))) {
return true;
}
return flavor.startsWith("cpu") || "mkl".equals(flavor);
logger.warn("The bundled library: " + this + " doesn't match system: " + system);
return false;
}

/** {@inheritDoc} */
Expand Down
8 changes: 8 additions & 0 deletions api/src/test/java/ai/djl/util/PlatformTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void testPlatform() throws IOException {
Assert.assertThrows(IllegalArgumentException.class, () -> Platform.fromUrl(invalid));

URL url = createPropertyFile("version=1.8.0\nclassifier=cu113-linux-x86_64");
// Use cu113 as target machine
Platform system = Platform.fromUrl(url);
Assert.assertEquals(system.getFlavor(), "cu113");
Assert.assertEquals(system.getClassifier(), "linux-x86_64");
Expand All @@ -48,6 +49,7 @@ public void testPlatform() throws IOException {
Assert.assertEquals(platform.getClassifier(), "linux-x86_64");
Assert.assertEquals(platform.getOsPrefix(), "linux");
Assert.assertEquals(platform.getOsArch(), "x86_64");
// cpu should always match with system
Assert.assertTrue(platform.matches(system));
Assert.assertFalse(system.matches(platform));

Expand All @@ -57,11 +59,17 @@ public void testPlatform() throws IOException {

url = createPropertyFile("version=1.8.0\nclassifier=cu111-linux-x86_64");
platform = Platform.fromUrl(url);
// cu111 can run on cu113 machine
Assert.assertTrue(platform.matches(system));
// cu113 can run on cu111 machine (the same major version)
Assert.assertTrue(system.matches(platform));

url = createPropertyFile("version=1.8.0\nclassifier=cu102-linux-x86_64");
platform = Platform.fromUrl(url);
// cu102 (lower major version) can run on cu113 machine,
Assert.assertTrue(platform.matches(system));
// cu113 can not run on cu102 machine
Assert.assertFalse(system.matches(platform));

// MXNet
url = createPropertyFile("version=1.8.0\nclassifier=cu113mkl-linux-x86_64");
Expand Down