Skip to content

Commit

Permalink
Add more debug log for model loading
Browse files Browse the repository at this point in the history
Change-Id: If8564627244f236ead6167eeff429318f77dc4b5
  • Loading branch information
frankfliu committed Jun 17, 2020
1 parent b4d31d9 commit 0af494a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
8 changes: 8 additions & 0 deletions api/src/main/java/ai/djl/repository/SimpleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A {@code SimpleRepository} is a {@link Repository} containing only a single artifact without
Expand All @@ -34,6 +36,8 @@
*/
public class SimpleRepository extends AbstractRepository {

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

private String name;
private Path path;

Expand Down Expand Up @@ -73,8 +77,10 @@ public URI getBaseUri() {
public Metadata locate(MRL mrl) throws IOException {
Path file = path.resolve("metadata.json");
if (Files.isRegularFile(file)) {
logger.debug("Using metadata.json file: {}", file.toAbsolutePath());
return metadataWithFile(file);
}
logger.debug("No metadata.json file found in: {}", path.toAbsolutePath());
return metadataWithoutFile();
}

Expand All @@ -96,6 +102,7 @@ private Metadata metadataWithoutFile() {
metadata.setArtifactId(file.getName());
}
if (!Files.exists(path)) {
logger.debug("Specified path doesn't exists: {}", path.toAbsolutePath());
return metadata;
}

Expand Down Expand Up @@ -159,6 +166,7 @@ protected URI resolvePath(Item item, String path) {
@Override
public List<MRL> getResources() {
if (!Files.exists(path)) {
logger.debug("Specified path doesn't exists: {}", path.toAbsolutePath());
return Collections.emptyList();
}

Expand Down
7 changes: 7 additions & 0 deletions api/src/main/java/ai/djl/repository/zoo/DefaultModelZoo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** A {@link ModelZoo} that contains models in specified locations. */
public class DefaultModelZoo implements ModelZoo {

public static final String GROUP_ID = "ai.djl.localmodelzoo";

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

private List<ModelLoader<?, ?>> modelLoaders;

/**
Expand All @@ -38,10 +42,13 @@ public DefaultModelZoo(String locations) {
for (String url : urls) {
if (!url.isEmpty()) {
Repository repo = Repository.newInstance(url, url);
logger.debug("Scanning models in repo: {}, {}", repo.getClass(), url);
List<MRL> mrls = repo.getResources();
for (MRL mrl : mrls) {
modelLoaders.add(new DefaultModelLoader(repo, mrl));
}
} else {
logger.warn("Model location is empty.");
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions api/src/main/java/ai/djl/repository/zoo/ModelZoo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** An interface represents a collection of models. */
public interface ModelZoo {
Expand Down Expand Up @@ -93,6 +95,7 @@ public interface ModelZoo {
*/
static <I, O> ZooModel<I, O> loadModel(Criteria<I, O> criteria)
throws IOException, ModelNotFoundException, MalformedModelException {
Logger logger = LoggerFactory.getLogger(ModelZoo.class);
String artifactId = criteria.getArtifactId();
ModelZoo modelZoo = criteria.getModelZoo();
String groupId = criteria.getGroupId();
Expand All @@ -101,6 +104,7 @@ static <I, O> ZooModel<I, O> loadModel(Criteria<I, O> criteria)

List<ModelZoo> list = new ArrayList<>();
if (modelZoo != null) {
logger.debug("Searching model in specified model zoo: {}", modelZoo.getGroupId());
if (groupId != null && !modelZoo.getGroupId().equals(groupId)) {
throw new ModelNotFoundException("groupId conflict with ModelZoo criteria.");
}
Expand All @@ -113,16 +117,20 @@ static <I, O> ZooModel<I, O> loadModel(Criteria<I, O> criteria)
} else {
ServiceLoader<ZooProvider> providers = ServiceLoader.load(ZooProvider.class);
for (ZooProvider provider : providers) {
logger.debug("Searching model in zoo provider: {}", provider.getName());
ModelZoo zoo = provider.getModelZoo();
if (zoo == null) {
logger.debug("No model zoo found in zoo provider: {}", provider.getName());
continue;
}
if (groupId != null && !zoo.getGroupId().equals(groupId)) {
// filter out ModelZoo by groupId
logger.debug("Ignored ModelZoo by groupId: {}", zoo.getGroupId());
continue;
}
Set<String> supportedEngine = zoo.getSupportedEngines();
if (engine != null && !supportedEngine.contains(engine)) {
logger.debug("Ignored ModelZoo by specified engine: {}", zoo.getGroupId());
continue;
}
list.add(zoo);
Expand All @@ -131,11 +139,13 @@ static <I, O> ZooModel<I, O> loadModel(Criteria<I, O> criteria)

for (ModelZoo zoo : list) {
for (ModelLoader<?, ?> loader : zoo.getModelLoaders()) {
if (artifactId != null && !artifactId.equals(loader.getArtifactId())) {
Application app = loader.getApplication();
String loaderArtifactId = loader.getArtifactId();
logger.debug("Checking ModelLoader: {} {}:{}", app, groupId, loaderArtifactId);
if (artifactId != null && !artifactId.equals(loaderArtifactId)) {
// filter out by model loader artifactId
continue;
}
Application app = loader.getApplication();
if (application != null
&& app != Application.UNDEFINED
&& !app.equals(application)) {
Expand All @@ -146,7 +156,11 @@ static <I, O> ZooModel<I, O> loadModel(Criteria<I, O> criteria)
try {
return loader.loadModel(criteria);
} catch (ModelNotFoundException e) {
// ignore
logger.debug(
"input/output type found for ModelLoader: {} {}:{}",
app,
groupId,
loaderArtifactId);
}
}
}
Expand Down

0 comments on commit 0af494a

Please sign in to comment.