Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sentencepiece] Fixes #1491, fix NPE bug #1492

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ private void loadModel(Path modelPath, String prefix) throws IOException {
"Model path doesn't exist: " + modelPath.toAbsolutePath());
}
Path modelDir = modelPath.toAbsolutePath();
if (prefix == null || prefix.isEmpty()) {
prefix = modelDir.toFile().getName();
}
Path modelFile = findModelFile(modelDir, prefix);
if (modelFile == null) {
// TODO: support proto and IOStream model
modelFile = findModelFile(modelDir, modelDir.toFile().getName());
if (modelFile == null) {
throw new FileNotFoundException("No .model found in : " + modelPath);
}
throw new FileNotFoundException("No .model found in : " + modelPath);
}

String modelFilePath = modelFile.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public class SpTokenizerTest {

@BeforeTest
public void downloadModel() throws IOException {
Path modelFile = Paths.get("build/test/models/sententpiece_test_model.model");
Path modelFile = Paths.get("build/test/sp_model/sp_model.model");
if (Files.notExists(modelFile)) {
DownloadUtils.download(
"https://resources.djl.ai/test-models/sententpiece_test_model.model",
"build/test/models/sententpiece_test_model.model");
"build/test/sp_model/sp_model.model");
}
}

@Test
public void testLoadFromBytes() throws IOException {
Path modelPath = Paths.get("build/test/models/sententpiece_test_model.model");
Path modelPath = Paths.get("build/test/sp_model/sp_model.model");
byte[] bytes = Files.readAllBytes(modelPath);
try (SpTokenizer tokenizer = new SpTokenizer(bytes)) {
String original = "Hello World";
Expand All @@ -55,7 +55,7 @@ public void testLoadFromBytes() throws IOException {
public void testTokenize() throws IOException {
TestRequirements.notWindows();

Path modelPath = Paths.get("build/test/models/sententpiece_test_model.model");
Path modelPath = Paths.get("build/test/sp_model");
try (SpTokenizer tokenizer = new SpTokenizer(modelPath)) {
String original = "Hello World";
List<String> tokens = tokenizer.tokenize(original);
Expand All @@ -71,7 +71,7 @@ public void testTokenize() throws IOException {
public void testUtf16Tokenize() throws IOException {
TestRequirements.notWindows();

Path modelPath = Paths.get("build/test/models/sententpiece_test_model.model");
Path modelPath = Paths.get("build/test/sp_model/sp_model.model");
try (SpTokenizer tokenizer = new SpTokenizer(modelPath)) {
String original = "\uD83D\uDC4B\uD83D\uDC4B";
List<String> tokens = tokenizer.tokenize(original);
Expand All @@ -84,8 +84,8 @@ public void testUtf16Tokenize() throws IOException {
public void testEncodeDecode() throws IOException {
TestRequirements.notWindows();

Path modelPath = Paths.get("build/test/models");
String prefix = "sententpiece_test_model";
Path modelPath = Paths.get("build/test/sp_model");
String prefix = "sp_model";
try (SpTokenizer tokenizer = new SpTokenizer(modelPath, prefix)) {
String original = "Hello World";
SpProcessor processor = tokenizer.getProcessor();
Expand All @@ -96,4 +96,24 @@ public void testEncodeDecode() throws IOException {
Assert.assertEquals(recovered, original);
}
}

@Test
public void testModelNotFound() throws IOException {
TestRequirements.notWindows();

Assert.assertThrows(
() -> {
new SpTokenizer(Paths.get("build/test/non-exists"));
});

Assert.assertThrows(
() -> {
new SpTokenizer(Paths.get("build/test/sp_model"), "non-exists.model");
});

Assert.assertThrows(
() -> {
new SpTokenizer(Paths.get("build/test/sp_model"), "non-exists");
});
}
}