aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/integration/ml/ImportedModels.java
blob: b1714b492563bf2044b062a953757f2e61fd2a03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.integration.ml;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.yahoo.path.Path;

import java.io.File;
import java.util.Collection;
import java.util.Optional;

/**
 * All models imported from the models/ directory in the application package
 *
 * @author bratseth
 */
public class ImportedModels {

    /** All imported models, indexed by their names */
    private final ImmutableMap<String, ImportedModel> importedModels;

    private static final ImmutableList<ModelImporter> importers =
            ImmutableList.of(new TensorFlowImporter(), new OnnxImporter(), new XGBoostImporter());

    /** Create a null imported models */
    public ImportedModels() {
        importedModels = ImmutableMap.of();
    }

    public ImportedModels(File modelsDirectory) {
        ImmutableMap.Builder<String, ImportedModel> builder = new ImmutableMap.Builder<>();

        // Find all subdirectories recursively which contains a model we can read
        importRecursively(modelsDirectory, builder);
        importedModels = builder.build();
    }

    private static void importRecursively(File dir, ImmutableMap.Builder<String, ImportedModel> builder) {
        if ( ! dir.isDirectory()) return;
        for (File child : dir.listFiles()) {
            Optional<ModelImporter> importer = findImporterOf(child);
            if (importer.isPresent()) {
                String name = toName(child);
                builder.put(name, importer.get().importModel(name, child));
            }
            else {
                importRecursively(child, builder);
            }
        }
    }

    private static Optional<ModelImporter> findImporterOf(File path) {
        return importers.stream().filter(item -> item.canImport(path.toString())).findFirst();
    }

    /**
     * Returns the model at the given location in the application package (lazily loaded),
     *
     * @param modelPath the full path to this model (file or directory, depending on model type)
     *                  under the application package
     * @throws IllegalArgumentException if the model cannot be loaded
     */
    public ImportedModel get(File modelPath) {
        return importedModels.get(toName(modelPath));
    }

    /** Returns an immutable collection of all the imported models */
    public Collection<ImportedModel> all() {
        return importedModels.values();
    }

    private static String toName(File modelFile) {
        Path modelPath = Path.fromString(modelFile.toString());
        if (modelFile.isFile())
            modelPath = stripFileEnding(modelPath);
        String localPath = concatenateAfterModelsDirectory(modelPath);
        return localPath.replace('.', '_');
    }

    private static Path stripFileEnding(Path path) {
        int dotIndex = path.last().lastIndexOf(".");
        if (dotIndex <= 0) return path;
        return path.withLast(path.last().substring(0, dotIndex));
    }

    private static String concatenateAfterModelsDirectory(Path path) {
        boolean afterModels = false;
        StringBuilder result = new StringBuilder();
        for (String element : path.elements()) {
            if (afterModels) result.append(element).append("_");
            if (element.equals("models")) afterModels = true;
        }
        return result.substring(0, result.length()-1);
    }

}