-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[llama.cpp] Adds llama.cpp huggingface model zoo
- Loading branch information
Showing
8 changed files
with
307 additions
and
1 deletion.
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
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
172 changes: 172 additions & 0 deletions
172
engines/llama/src/main/java/ai/djl/llama/zoo/LlamaModelZoo.java
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,172 @@ | ||
/* | ||
* Copyright 2023 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.llama.zoo; | ||
|
||
import ai.djl.Application; | ||
import ai.djl.repository.Repository; | ||
import ai.djl.repository.zoo.ModelLoader; | ||
import ai.djl.repository.zoo.ModelZoo; | ||
import ai.djl.util.ClassLoaderUtils; | ||
import ai.djl.util.JsonUtils; | ||
import ai.djl.util.Utils; | ||
|
||
import com.google.gson.reflect.TypeToken; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
import java.lang.reflect.Type; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** LlamaModelZoo is a repository that contains llama.cpp models. */ | ||
public class LlamaModelZoo extends ModelZoo { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(LlamaModelZoo.class); | ||
|
||
private static final String REPO = "https://mlrepo.djl.ai/"; | ||
private static final Repository REPOSITORY = Repository.newInstance("gguf", REPO); | ||
private static final String GROUP_ID = "ai.djl.huggingface.gguf"; | ||
|
||
private static final long ONE_DAY = Duration.ofDays(1).toMillis(); | ||
|
||
private boolean initialized; | ||
|
||
LlamaModelZoo() {} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public String getGroupId() { | ||
return GROUP_ID; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Set<String> getSupportedEngines() { | ||
return Collections.singleton("Llama"); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Collection<ModelLoader> getModelLoaders() { | ||
init(); | ||
return super.getModelLoaders(); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public ModelLoader getModelLoader(String name) { | ||
init(); | ||
return super.getModelLoader(name); | ||
} | ||
|
||
private void init() { | ||
if (!initialized) { | ||
Application app = Application.NLP.TEXT_GENERATION; | ||
Map<String, ModelDetail> map = listModels(app); | ||
for (Map.Entry<String, ModelDetail> entry : map.entrySet()) { | ||
String artifactId = entry.getKey(); | ||
Map<String, Object> gguf = entry.getValue().getGguf(); | ||
if (gguf != null) { | ||
for (String key : gguf.keySet()) { | ||
addModel(REPOSITORY.model(app, GROUP_ID, artifactId, "0.0.1", key)); | ||
} | ||
} | ||
} | ||
initialized = true; | ||
} | ||
} | ||
|
||
private Map<String, ModelDetail> listModels(Application app) { | ||
try { | ||
String path = "model/" + app.getPath() + "/ai/djl/huggingface/gguf/"; | ||
Path dir = Utils.getCacheDir().resolve("cache/repo/" + path); | ||
if (Files.notExists(dir)) { | ||
Files.createDirectories(dir); | ||
} else if (!Files.isDirectory(dir)) { | ||
logger.warn("Failed initialize cache directory: " + dir); | ||
return Collections.emptyMap(); | ||
} | ||
Type type = new TypeToken<Map<String, ModelDetail>>() {}.getType(); | ||
|
||
Path file = dir.resolve("models.json"); | ||
if (Files.exists(file)) { | ||
long lastModified = Files.getLastModifiedTime(file).toMillis(); | ||
if (Utils.isOfflineMode() || System.currentTimeMillis() - lastModified < ONE_DAY) { | ||
try (Reader reader = Files.newBufferedReader(file)) { | ||
return JsonUtils.GSON.fromJson(reader, type); | ||
} | ||
} | ||
} | ||
|
||
URL url = URI.create(REPO).resolve(path + "models.json.gz").toURL(); | ||
Path tmp = Files.createTempFile(dir, "models", ".tmp"); | ||
try (GZIPInputStream gis = new GZIPInputStream(Utils.openUrl(url))) { | ||
String json = Utils.toString(gis); | ||
try (Writer writer = Files.newBufferedWriter(tmp)) { | ||
writer.write(json); | ||
} | ||
Utils.moveQuietly(tmp, file); | ||
return JsonUtils.GSON.fromJson(json, type); | ||
} catch (IOException e) { | ||
logger.warn("Failed to download Huggingface gguf index: {}", app); | ||
if (Files.exists(file)) { | ||
try (Reader reader = Files.newBufferedReader(file)) { | ||
return JsonUtils.GSON.fromJson(reader, type); | ||
} | ||
} | ||
|
||
String resource = app.getPath() + "/" + GROUP_ID + ".json"; | ||
try (InputStream is = ClassLoaderUtils.getResourceAsStream(resource)) { | ||
String json = Utils.toString(is); | ||
try (Writer writer = Files.newBufferedWriter(tmp)) { | ||
writer.write(json); | ||
} | ||
Utils.moveQuietly(tmp, file); | ||
return JsonUtils.GSON.fromJson(json, type); | ||
} | ||
} finally { | ||
Utils.deleteQuietly(tmp); | ||
} | ||
} catch (IOException e) { | ||
logger.warn("Failed load gguf index file", e); | ||
} | ||
|
||
return Collections.emptyMap(); | ||
} | ||
|
||
private static final class ModelDetail { | ||
|
||
private Map<String, Object> gguf; | ||
|
||
public Map<String, Object> getGguf() { | ||
return gguf; | ||
} | ||
|
||
public void setGguf(Map<String, Object> gguf) { | ||
this.gguf = gguf; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
engines/llama/src/main/java/ai/djl/llama/zoo/LlamaZooProvider.java
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,29 @@ | ||
/* | ||
* Copyright 2023 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.llama.zoo; | ||
|
||
import ai.djl.repository.zoo.ModelZoo; | ||
import ai.djl.repository.zoo.ZooProvider; | ||
|
||
/** | ||
* An Huggingface llama.cpp model zoo provider implements the {@link | ||
* ai.djl.repository.zoo.ZooProvider} interface. | ||
*/ | ||
public class LlamaZooProvider implements ZooProvider { | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public ModelZoo getModelZoo() { | ||
return new LlamaModelZoo(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
engines/llama/src/main/java/ai/djl/llama/zoo/package-info.java
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,14 @@ | ||
/* | ||
* Copyright 2023 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. | ||
*/ | ||
/** Contains the built-in {@link ai.djl.llama.zoo.LlamaModelZoo}. */ | ||
package ai.djl.llama.zoo; |
1 change: 1 addition & 0 deletions
1
engines/llama/src/main/resources/META-INF/services/ai.djl.repository.zoo.ZooProvider
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 @@ | ||
ai.djl.llama.zoo.LlamaZooProvider |
62 changes: 62 additions & 0 deletions
62
engines/llama/src/test/java/ai/djl/llama/zoo/LlamaModelZooTest.java
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,62 @@ | ||
/* | ||
* Copyright 2023 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.llama.zoo; | ||
|
||
import ai.djl.repository.zoo.ModelLoader; | ||
import ai.djl.repository.zoo.ModelZoo; | ||
import ai.djl.util.Utils; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.nio.file.Paths; | ||
import java.util.Collection; | ||
|
||
public class LlamaModelZooTest { | ||
|
||
@Test | ||
public void testLlamaModelZoo() { | ||
System.setProperty("DJL_CACHE_DIR", "build/cache"); | ||
Utils.deleteQuietly(Paths.get("build/cache/cache")); | ||
try { | ||
ModelZoo zoo = ModelZoo.getModelZoo("ai.djl.huggingface.gguf"); | ||
Collection<ModelLoader> models = zoo.getModelLoaders(); | ||
Assert.assertFalse(models.isEmpty()); | ||
Assert.assertEquals(zoo.getSupportedEngines().size(), 1); | ||
ModelLoader loader = zoo.getModelLoader("TinyLlama/TinyLlama-1.1B-Chat-v0.6"); | ||
Assert.assertNotNull(loader); | ||
|
||
ModelZoo llamaModelZoo = new LlamaModelZoo(); | ||
Assert.assertFalse(llamaModelZoo.getModelLoaders().isEmpty()); | ||
} finally { | ||
System.clearProperty("DJL_CACHE_DIR"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOffLine() { | ||
System.setProperty("DJL_CACHE_DIR", "build/cache"); | ||
System.setProperty("ai.djl.offline", "true"); | ||
Utils.deleteQuietly(Paths.get("build/cache/cache")); | ||
try { | ||
// static variables cannot not be initialized properly if directly use LlamaModelZoo() | ||
ModelZoo.getModelZoo("ai.djl.huggingface.gguf"); | ||
|
||
ModelZoo zoo = new LlamaModelZoo(); | ||
Assert.assertFalse(zoo.getModelLoaders().isEmpty()); | ||
} finally { | ||
System.clearProperty("DJL_CACHE_DIR"); | ||
System.clearProperty("ai.djl.offline"); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
engines/llama/src/test/java/ai/djl/llama/zoo/package-info.java
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,14 @@ | ||
/* | ||
* Copyright 2023 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. | ||
*/ | ||
/** Contains test classes for llama model zoo. */ | ||
package ai.djl.llama.zoo; |