krickert commented on code in PR #1462: URL: https://github.com/apache/opennlp/pull/1462#discussion_r4243908743
########## opennlp-core/opennlp-ml/opennlp-dl/src/test/java/opennlp/dl/GpuRequestTest.java: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 opennlp.dl; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Map; + +import ai.onnxruntime.OrtException; +import ai.onnxruntime.OrtSession; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import opennlp.dl.doccat.DocumentCategorizerDL; +import opennlp.dl.doccat.scoring.AverageClassificationScoringStrategy; +import opennlp.dl.namefinder.NameFinderDL; +import opennlp.tools.sentdetect.NewlineSentenceDetector; + +import static opennlp.dl.vectors.TinyVectorModels.TINY_VECTORS; +import static opennlp.dl.vectors.TinyVectorModels.copy; +import static opennlp.dl.vectors.TinyVectorModels.gpu; +import static opennlp.dl.vectors.TinyVectorModels.model; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Tests how a GPU request in {@link InferenceOptions} reaches ONNX Runtime: a request for a + * device that is not usable is rejected with an {@link OrtException}, never served by the CPU, + * and a CPU request does not ask for any device. + */ +class GpuRequestTest { + + private static final int UNUSABLE_DEVICE_ID = 99; + + private static final String PLAIN_VOCAB = "/opennlp/dl/vocab-plain.txt"; + + /** The device id is not read while {@code gpu} is off, so no provider is requested. */ + @ParameterizedTest + @ValueSource(ints = {-1, 0, UNUSABLE_DEVICE_ID}) + void testSessionOptionsWithoutGpuIgnoreTheDeviceId(final int deviceId) { + final InferenceOptions options = new InferenceOptions(); + options.setGpuDeviceId(deviceId); + assertDoesNotThrow(() -> { + try (OrtSession.SessionOptions sessionOptions = AbstractDL.sessionOptions(options)) { + assertNotNull(sessionOptions); + } + }); + } + + /** A name finder on a device id that no machine has fails instead of running on the CPU. */ + @Test + void testNameFinderRejectsAnUnusableDevice(@TempDir final Path dir) throws IOException { + final File model = model(dir, TINY_VECTORS); + final File vocab = copy(dir, PLAIN_VOCAB, "vocab.txt"); + final InferenceOptions options = gpu(UNUSABLE_DEVICE_ID); + final Map<Integer, String> labels = Map.of(0, "O", 1, "B-PER"); + assertThrows(OrtException.class, () -> new NameFinderDL(model, vocab, labels, options, + new NewlineSentenceDetector()).close()); + } + + /** A categorizer on a device id that no machine has fails instead of running on the CPU. */ + @Test + void testDocumentCategorizerRejectsAnUnusableDevice(@TempDir final Path dir) + throws IOException { + final File model = model(dir, TINY_VECTORS); + final File vocab = copy(dir, PLAIN_VOCAB, "vocab.txt"); + final InferenceOptions options = gpu(UNUSABLE_DEVICE_ID); + final Map<Integer, String> categories = Map.of(0, "a", 1, "b"); + assertThrows(OrtException.class, () -> new DocumentCategorizerDL(model, vocab, categories, + new AverageClassificationScoringStrategy(), options).close()); + } + + /** Null options and invalid split sizes are rejected before a device is requested. */ + @Test + void testSessionOptionsRejectNullAndInvalidSplitBeforeAnyDevice() { Review Comment: Done, same placement: `GpuRequestTest` runs the five invalid split cases and the `SentenceVectorsDL` copy is removed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
