-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] Moves NeuronUtils to api package (#2496)
- Loading branch information
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. 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. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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. | ||
*/ | ||
package ai.djl.util; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Iterator; | ||
import java.util.stream.Stream; | ||
|
||
/** A utility class to detect number of nueron cores. */ | ||
public final class NeuronUtils { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(NeuronUtils.class); | ||
|
||
private NeuronUtils() {} | ||
|
||
/** | ||
* Returns whether Neuron runtime library is in the system. | ||
* | ||
* @return {@code true} if Neuron runtime library is in the system | ||
*/ | ||
public static boolean hasNeuron() { | ||
return getNeuronCores() > 0; | ||
} | ||
|
||
/** | ||
* Returns the number of NeuronCores available in the system. | ||
* | ||
* @return the number of NeuronCores available in the system | ||
*/ | ||
public static int getNeuronCores() { | ||
return getNeuronCores("/sys/devices/virtual/neuron_device/"); | ||
} | ||
|
||
@SuppressWarnings("PMD.ForLoopCanBeForeach") | ||
static int getNeuronCores(String location) { | ||
Path path = Paths.get(location); | ||
if (!Files.exists(path)) { | ||
return 0; | ||
} | ||
int count = 0; | ||
try (Stream<Path> dev = Files.list(path)) { | ||
for (Iterator<Path> it = dev.iterator(); it.hasNext(); ) { | ||
Path dir = it.next(); | ||
if (dir.toFile().getName().startsWith("neuron")) { | ||
Stream<Path> cores = Files.list(dir); | ||
count += Math.toIntExact(cores.filter(NeuronUtils::matches).count()); | ||
cores.close(); | ||
} | ||
} | ||
} catch (IOException e) { | ||
logger.warn("Failed to list neuron cores", e); | ||
} | ||
return count; | ||
} | ||
|
||
private static boolean matches(Path p) { | ||
return p.getFileName().toString().startsWith("neuron_core"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. 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. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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. | ||
*/ | ||
package ai.djl.util; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class NeuronUtilsTest { | ||
|
||
@Test | ||
public void testNeuronUtils() throws IOException { | ||
Path dir = Paths.get("build/neuron_device/"); | ||
try { | ||
for (int i = 0; i < 4; ++i) { | ||
Path nd = dir.resolve("neuron" + i); | ||
for (int j = 0; j < 2; ++j) { | ||
Path nc = nd.resolve("neuron_core" + j); | ||
Files.createDirectories(nc); | ||
} | ||
} | ||
NeuronUtils.hasNeuron(); | ||
Assert.assertEquals(NeuronUtils.getNeuronCores(dir.toString()), 8); | ||
} finally { | ||
Utils.deleteQuietly(dir); | ||
} | ||
} | ||
} |