-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
37 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
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
81 changes: 81 additions & 0 deletions
81
...rch-model-zoo/src/test/java/ai/djl/pytorch/zoo/nlp/textgeneration/TextGenerationTest.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,81 @@ | ||
/* | ||
* 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.pytorch.zoo.nlp.textgeneration; | ||
|
||
import ai.djl.ModelException; | ||
import ai.djl.inference.Predictor; | ||
import ai.djl.modality.nlp.generate.CausalLMOutput; | ||
import ai.djl.ndarray.NDArray; | ||
import ai.djl.ndarray.NDList; | ||
import ai.djl.ndarray.NDManager; | ||
import ai.djl.ndarray.types.Shape; | ||
import ai.djl.nn.Block; | ||
import ai.djl.nn.LambdaBlock; | ||
import ai.djl.repository.zoo.Criteria; | ||
import ai.djl.repository.zoo.ZooModel; | ||
import ai.djl.translate.TranslateException; | ||
|
||
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 TextGenerationTest { | ||
|
||
@Test | ||
public void testGpt2() throws TranslateException, ModelException, IOException { | ||
Block block = | ||
new LambdaBlock( | ||
a -> { | ||
NDList list = new NDList(25); | ||
NDManager manager = a.getManager(); | ||
NDArray arr = manager.full(new Shape(1, 4, 50257), 257); | ||
list.add(arr); | ||
|
||
for (int i = 0; i < 12 * 2; ++i) { | ||
NDArray array = manager.zeros(new Shape(1, 12, 1, 64)); | ||
list.add(array); | ||
} | ||
return list; | ||
}, | ||
"model"); | ||
|
||
Path modelDir = Paths.get("build/text_generation"); | ||
Files.createDirectories(modelDir); | ||
|
||
Criteria<NDList, CausalLMOutput> criteria = | ||
Criteria.builder() | ||
.setTypes(NDList.class, CausalLMOutput.class) | ||
.optModelPath(modelDir) | ||
.optBlock(block) | ||
.optOption("hasParameter", "false") | ||
.optTranslatorFactory(new PtGptTranslatorFactory()) | ||
.build(); | ||
|
||
try (ZooModel<NDList, CausalLMOutput> model = criteria.loadModel(); | ||
Predictor<NDList, CausalLMOutput> predictor = model.newPredictor(); | ||
NDManager manager = NDManager.newBaseManager()) { | ||
long[][] inputIds = {{29744, 28478, 5834, 318}}; | ||
NDArray input = manager.create(inputIds); | ||
CausalLMOutput res = predictor.predict(new NDList(input)); | ||
NDArray logits = res.getLogits(); | ||
Assert.assertEquals(logits.argMax().getLong(), 257); | ||
NDList list = res.getPastKeyValuesList(); | ||
Assert.assertEquals(list.size(), 24); | ||
Assert.assertEquals(res.getHiddenState().getShape().get(0), 1); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...h/pytorch-model-zoo/src/test/java/ai/djl/pytorch/zoo/nlp/textgeneration/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,16 @@ | ||
/* | ||
* 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 unit test classes for text generation. | ||
*/ | ||
package ai.djl.pytorch.zoo.nlp.textgeneration; |
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