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

[api] Adds dimension for prediction metric #3013

Merged
merged 1 commit into from
Mar 2, 2024
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
12 changes: 8 additions & 4 deletions api/src/main/java/ai/djl/inference/Predictor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ai.djl.inference.streaming.StreamingBlock;
import ai.djl.inference.streaming.StreamingTranslator;
import ai.djl.inference.streaming.StreamingTranslator.StreamOutput;
import ai.djl.metric.Dimension;
import ai.djl.metric.Metrics;
import ai.djl.metric.Unit;
import ai.djl.ndarray.LazyNDArray;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class Predictor<I, O> implements AutoCloseable {
protected Metrics metrics;
protected Block block;
protected ParameterStore parameterStore;
protected Dimension dimension;

/**
* Creates a new instance of {@code BasePredictor} with the given {@link Model} and {@link
Expand All @@ -116,6 +118,7 @@ public Predictor(Model model, Translator<I, O> translator, Device device, boolea
this.translator = translator;
block = model.getBlock();
parameterStore = new ParameterStore(manager, copy);
dimension = new Dimension("Model", model.getProperty("metric_dimension", "model"));
}

/**
Expand Down Expand Up @@ -315,7 +318,7 @@ private void preprocessEnd(NDList list) {
long tmp = System.nanoTime();
long duration = (tmp - timestamp) / 1000;
timestamp = tmp;
metrics.addMetric("Preprocess", duration, Unit.MICROSECONDS);
metrics.addMetric("Preprocess", duration, Unit.MICROSECONDS, dimension);
}
}

Expand All @@ -325,7 +328,7 @@ private void predictEnd(NDList list) {
long tmp = System.nanoTime();
long duration = (tmp - timestamp) / 1000;
timestamp = tmp;
metrics.addMetric("Inference", duration, Unit.MICROSECONDS);
metrics.addMetric("Inference", duration, Unit.MICROSECONDS, dimension);
}
}

Expand All @@ -334,8 +337,9 @@ private void postProcessEnd(long begin) {
long tmp = System.nanoTime();
long duration = (tmp - timestamp) / 1000;
timestamp = tmp;
metrics.addMetric("Postprocess", duration, Unit.MICROSECONDS);
metrics.addMetric("Total", (tmp - begin) / 1000, Unit.MICROSECONDS);
metrics.addMetric("Postprocess", duration, Unit.MICROSECONDS, dimension);
long prediction = (tmp - begin) / 1000;
metrics.addMetric("Prediction", prediction, Unit.MICROSECONDS, dimension);
}
}

Expand Down
5 changes: 3 additions & 2 deletions api/src/main/java/ai/djl/metric/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ public void addMetric(String name, Number value) {
* @param name the metric name
* @param value the metric value
* @param unit the metric unit
* @param dimensions the metric dimensions
*/
public void addMetric(String name, Number value, Unit unit) {
addMetric(new Metric(name, value, unit));
public void addMetric(String name, Number value, Unit unit, Dimension... dimensions) {
addMetric(new Metric(name, value, unit, dimensions));
}

/**
Expand Down
Loading