Search in sources :

Example 6 with TranslateException

use of ai.djl.translate.TranslateException in project djl by deepjavalibrary.

the class FixedBucketSamplerTest method testFixedBucketSampler.

@Test
public void testFixedBucketSampler() throws IOException, TranslateException {
    FixedBucketSampler fixedBucketSampler = new FixedBucketSampler(10, 10, false);
    TatoebaEnglishFrenchDataset dataset = TatoebaEnglishFrenchDataset.builder().setSampling(fixedBucketSampler).optDataBatchifier(PaddingStackBatchifier.builder().optIncludeValidLengths(true).addPad(0, 0, (m) -> m.zeros(new Shape(1)), 10).build()).optLabelBatchifier(PaddingStackBatchifier.builder().optIncludeValidLengths(true).addPad(0, 0, (m) -> m.ones(new Shape(1)), 10).build()).optLimit(200).build();
    dataset.prepare();
    Iterator<List<Long>> iterator = fixedBucketSampler.sample(dataset);
    long count = 0;
    Set<Long> indicesSet = new HashSet<>();
    while (iterator.hasNext()) {
        List<Long> indices = iterator.next();
        indicesSet.addAll(indices);
        count += indices.size();
    }
    Assert.assertEquals(count, dataset.size());
    Assert.assertEquals(indicesSet.size(), dataset.size());
    fixedBucketSampler = new FixedBucketSampler(10, 5, true);
    iterator = fixedBucketSampler.sample(dataset);
    count = 0;
    indicesSet.clear();
    while (iterator.hasNext()) {
        List<Long> indices = iterator.next();
        indicesSet.addAll(indices);
        count = count + indices.size();
    }
    Assert.assertEquals(count, dataset.size());
    Assert.assertEquals(indicesSet.size(), dataset.size());
}
Also used : HashSet(java.util.HashSet) List(java.util.List) TranslateException(ai.djl.translate.TranslateException) Iterator(java.util.Iterator) Assert(org.testng.Assert) TatoebaEnglishFrenchDataset(ai.djl.basicdataset.nlp.TatoebaEnglishFrenchDataset) FixedBucketSampler(ai.djl.basicdataset.utils.FixedBucketSampler) Shape(ai.djl.ndarray.types.Shape) Set(java.util.Set) IOException(java.io.IOException) Test(org.testng.annotations.Test) PaddingStackBatchifier(ai.djl.translate.PaddingStackBatchifier) Shape(ai.djl.ndarray.types.Shape) TatoebaEnglishFrenchDataset(ai.djl.basicdataset.nlp.TatoebaEnglishFrenchDataset) List(java.util.List) FixedBucketSampler(ai.djl.basicdataset.utils.FixedBucketSampler) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 7 with TranslateException

use of ai.djl.translate.TranslateException in project djl by deepjavalibrary.

the class TrtTest method testSerializedEngine.

@Test
public void testSerializedEngine() throws ModelException, IOException, TranslateException {
    Engine engine;
    try {
        engine = Engine.getEngine("TensorRT");
    } catch (Exception ignore) {
        throw new SkipException("Your os configuration doesn't support TensorRT.");
    }
    Device device = engine.defaultDevice();
    if (!device.isGpu()) {
        throw new SkipException("TensorRT only support GPU.");
    }
    String sm = CudaUtils.getComputeCapability(device.getDeviceId());
    Criteria<float[], float[]> criteria = Criteria.builder().setTypes(float[].class, float[].class).optModelPath(Paths.get("src/test/resources/identity_" + sm + ".trt")).optTranslator(new MyTranslator()).optEngine("TensorRT").build();
    try (ZooModel<float[], float[]> model = criteria.loadModel();
        Predictor<float[], float[]> predictor = model.newPredictor()) {
        float[] data = new float[] { 1, 2, 3, 4 };
        float[] ret = predictor.predict(data);
        Assert.assertEquals(ret, data);
    }
}
Also used : Device(ai.djl.Device) SkipException(org.testng.SkipException) Engine(ai.djl.engine.Engine) SkipException(org.testng.SkipException) ModelException(ai.djl.ModelException) TranslateException(ai.djl.translate.TranslateException) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 8 with TranslateException

use of ai.djl.translate.TranslateException in project djl-serving by deepjavalibrary.

the class WorkerThread method run.

/**
 * {@inheritDoc}
 */
@Override
public void run() {
    Thread thread = Thread.currentThread();
    thread.setName(workerName);
    currentThread.set(thread);
    this.state = WorkerState.WORKER_STARTED;
    List<Input> req = null;
    String errorMessage = "Worker shutting down";
    try {
        while (isRunning() && !aggregator.isFinished()) {
            req = aggregator.getRequest();
            if (req != null && !req.isEmpty()) {
                try {
                    List<Output> reply = predictor.batchPredict(req);
                    aggregator.sendResponse(reply);
                } catch (TranslateException e) {
                    logger.warn("Failed to predict", e);
                    aggregator.sendError(e);
                }
            }
            req = null;
        }
    } catch (InterruptedException e) {
        logger.debug("Shutting down the thread .. Scaling down.");
    } catch (Throwable t) {
        logger.error("Server error", t);
        errorMessage = t.getMessage();
    } finally {
        logger.debug("Shutting down worker thread .. {}", currentThread.get().getName());
        currentThread.set(null);
        shutdown(WorkerState.WORKER_STOPPED);
        if (req != null) {
            Exception e = new WlmException(errorMessage);
            aggregator.sendError(e);
        }
    }
}
Also used : Input(ai.djl.modality.Input) TranslateException(ai.djl.translate.TranslateException) WlmException(ai.djl.serving.wlm.util.WlmException) Output(ai.djl.modality.Output) WlmException(ai.djl.serving.wlm.util.WlmException) TranslateException(ai.djl.translate.TranslateException)

Example 9 with TranslateException

use of ai.djl.translate.TranslateException in project djl by deepjavalibrary.

the class BaseModelLoader method loadModel.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public <I, O> ZooModel<I, O> loadModel(Criteria<I, O> criteria) throws IOException, ModelNotFoundException, MalformedModelException {
    Artifact artifact = mrl.match(criteria.getFilters());
    if (artifact == null) {
        throw new ModelNotFoundException("No matching filter found");
    }
    Progress progress = criteria.getProgress();
    Map<String, Object> arguments = artifact.getArguments(criteria.getArguments());
    Map<String, String> options = artifact.getOptions(criteria.getOptions());
    try {
        TranslatorFactory factory = getTranslatorFactory(criteria, arguments);
        Class<I> input = criteria.getInputClass();
        Class<O> output = criteria.getOutputClass();
        if (factory == null || !factory.isSupported(input, output)) {
            factory = defaultFactory;
            if (!factory.isSupported(input, output)) {
                throw new ModelNotFoundException(getFactoryLookupErrorMessage(factory));
            }
        }
        mrl.prepare(artifact, progress);
        if (progress != null) {
            progress.reset("Loading", 2);
            progress.update(1);
        }
        Path modelPath = mrl.getRepository().getResourceDirectory(artifact);
        Path modelDir = Files.isRegularFile(modelPath) ? modelPath.getParent() : modelPath;
        if (modelDir == null) {
            throw new AssertionError("Directory should not be null.");
        }
        loadServingProperties(modelDir, arguments, options);
        Application application = criteria.getApplication();
        if (application != Application.UNDEFINED) {
            arguments.put("application", application.getPath());
        }
        String engine = criteria.getEngine();
        if (engine == null) {
            // get engine from serving.properties
            engine = (String) arguments.get("engine");
        }
        // Otherwise if none of them is specified or model zoo is null, go to default engine.
        if (engine == null) {
            ModelZoo modelZoo = ModelZoo.getModelZoo(mrl.getGroupId());
            if (modelZoo != null) {
                String defaultEngine = Engine.getDefaultEngineName();
                for (String supportedEngine : modelZoo.getSupportedEngines()) {
                    if (supportedEngine.equals(defaultEngine)) {
                        engine = supportedEngine;
                        break;
                    } else if (Engine.hasEngine(supportedEngine)) {
                        engine = supportedEngine;
                    }
                }
                if (engine == null) {
                    throw new ModelNotFoundException("No supported engine available for model zoo: " + modelZoo.getGroupId());
                }
            }
        }
        if (engine != null && !Engine.hasEngine(engine)) {
            throw new ModelNotFoundException(engine + " is not supported");
        }
        String modelName = criteria.getModelName();
        if (modelName == null) {
            modelName = artifact.getName();
        }
        Model model = createModel(modelDir, modelName, criteria.getDevice(), criteria.getBlock(), arguments, engine);
        model.load(modelPath, null, options);
        Translator<I, O> translator = (Translator<I, O>) factory.newInstance(input, output, model, arguments);
        return new ZooModel<>(model, translator);
    } catch (TranslateException e) {
        throw new ModelNotFoundException("No matching translator found", e);
    } finally {
        if (progress != null) {
            progress.end();
        }
    }
}
Also used : Path(java.nio.file.Path) Progress(ai.djl.util.Progress) DefaultTranslatorFactory(ai.djl.translate.DefaultTranslatorFactory) TranslatorFactory(ai.djl.translate.TranslatorFactory) TranslateException(ai.djl.translate.TranslateException) Artifact(ai.djl.repository.Artifact) Translator(ai.djl.translate.Translator) Model(ai.djl.Model) Application(ai.djl.Application)

Example 10 with TranslateException

use of ai.djl.translate.TranslateException in project djl by deepjavalibrary.

the class Predictor method batchPredict.

/**
 * Predicts a batch for inference.
 *
 * @param inputs a list of inputs
 * @return a list of output objects defined by the user
 * @throws TranslateException if an error occurs during prediction
 */
@SuppressWarnings({ "PMD.AvoidRethrowingException", "PMD.IdenticalCatchBranches" })
public List<O> batchPredict(List<I> inputs) throws TranslateException {
    long begin = System.nanoTime();
    try (PredictorContext context = new PredictorContext()) {
        if (!prepared) {
            translator.prepare(context);
            prepared = true;
        }
        Batchifier batchifier = translator.getBatchifier();
        if (batchifier == null) {
            List<O> ret = new ArrayList<>(inputs.size());
            for (I input : inputs) {
                timestamp = System.nanoTime();
                begin = timestamp;
                NDList ndList = translator.processInput(context, input);
                preprocessEnd(ndList);
                NDList result = predictInternal(context, ndList);
                predictEnd(result);
                ret.add(translator.processOutput(context, result));
                postProcessEnd(begin);
            }
            return ret;
        }
        timestamp = System.nanoTime();
        NDList inputBatch = processInputs(context, inputs);
        preprocessEnd(inputBatch);
        NDList result = predictInternal(context, inputBatch);
        predictEnd(result);
        List<O> ret = processOutputs(context, result);
        postProcessEnd(begin);
        return ret;
    } catch (TranslateException e) {
        throw e;
    } catch (Exception e) {
        throw new TranslateException(e);
    }
}
Also used : Batchifier(ai.djl.translate.Batchifier) TranslateException(ai.djl.translate.TranslateException) ArrayList(java.util.ArrayList) NDList(ai.djl.ndarray.NDList) TranslateException(ai.djl.translate.TranslateException)

Aggregations

TranslateException (ai.djl.translate.TranslateException)20 IOException (java.io.IOException)9 ModelException (ai.djl.ModelException)7 Engine (ai.djl.engine.Engine)6 Classifications (ai.djl.modality.Classifications)6 Shape (ai.djl.ndarray.types.Shape)4 Application (ai.djl.Application)3 Model (ai.djl.Model)3 Metrics (ai.djl.metric.Metrics)3 Image (ai.djl.modality.cv.Image)3 NDList (ai.djl.ndarray.NDList)3 ProgressBar (ai.djl.training.util.ProgressBar)3 PaddingStackBatchifier (ai.djl.translate.PaddingStackBatchifier)3 List (java.util.List)3 Locale (java.util.Locale)3 Device (ai.djl.Device)2 MalformedModelException (ai.djl.MalformedModelException)2 Arguments (ai.djl.examples.training.util.Arguments)2 Predictor (ai.djl.inference.Predictor)2 ToTensor (ai.djl.modality.cv.transform.ToTensor)2