Skip to content

Commit

Permalink
[api] Moves NeuronUtils to api package (#2496)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Mar 31, 2023
1 parent 6aedd50 commit 5ab2685
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
75 changes: 75 additions & 0 deletions api/src/main/java/ai/djl/util/NeuronUtils.java
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");
}
}
42 changes: 42 additions & 0 deletions api/src/test/java/ai/djl/util/NeuronUtilsTest.java
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);
}
}
}

0 comments on commit 5ab2685

Please sign in to comment.