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

Use try-with-resources for InferenceServerClient to handle resource management and potential TimeoutException #780

Open
1ComputerMaster opened this issue Sep 7, 2024 · 0 comments

Comments

@1ComputerMaster
Copy link

Currently, the InferenceServerClient object is not being closed properly after usage in the SimpleInferClient example. Since InferenceServerClient implements AutoCloseable, it should be managed using a try-with-resources statement to ensure proper resource cleanup and avoid potential resource leaks.

Additionally, there's a potential risk of a TimeoutException occurring when communicating with the inference server, but the code doesn't currently handle this. By wrapping the client usage inside a try-with-resources block and catching TimeoutException, we can ensure proper error handling and resource management.

Proposed code

try (InferenceServerClient client =
                 new InferenceServerClient("0.0.0.0:8000", 5000, 5000)) {
      
    } catch (TimeoutException e) {
      System.err.println("Inference request timed out: " + e.getMessage());
      e.printStackTrace();
    }

Updated whole code

package triton.client.examples;

import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import triton.client.InferInput;
import triton.client.InferRequestedOutput;
import triton.client.InferResult;
import triton.client.InferenceServerClient;
import triton.client.pojo.DataType;

import java.util.concurrent.TimeoutException;

/**
 * @author xiafei.qiuxf
 * @date 2021/7/28
 */

public class SimpleInferClient {
  public static void main(String[] args) throws Exception
  {
    // Initialize the data
    boolean isBinary0 = false;
    InferInput input0 =
        new InferInput("INPUT0", new long[] {1L, 16}, DataType.INT32);
    List<Integer> lst_0 =
        IntStream.rangeClosed(1, 16).boxed().collect(Collectors.toList());
    int[] input0_data = lst_0.stream().mapToInt(i -> i).toArray();
    input0.setData(input0_data, isBinary0);

    boolean isBinary1 = true;
    InferInput input1 =
        new InferInput("INPUT1", new long[] {1L, 16}, DataType.INT32);
    List<Integer> lst_1 =
        IntStream.rangeClosed(1, 16).boxed().collect(Collectors.toList());
    int[] input1_data = lst_1.stream().mapToInt(i -> i).toArray();
    input1.setData(input1_data, isBinary1);

    List<InferInput> inputs = Lists.newArrayList(input0, input1);
    List<InferRequestedOutput> outputs = Lists.newArrayList(
        new InferRequestedOutput("OUTPUT0", isBinary0),
        new InferRequestedOutput("OUTPUT1", isBinary1));

    try (InferenceServerClient client =
                 new InferenceServerClient("0.0.0.0:8000", 5000, 5000)) {
      InferResult result = client.infer("simple", inputs, outputs);

      int[] op0 = result.getOutputAsInt("OUTPUT0");
      int[] op1 = result.getOutputAsInt("OUTPUT1");

      for (int i = 0; i < op0.length; i++) {
        System.out.println(
                Integer.toString(lst_0.get(i)) + " + "
                        + Integer.toString(lst_1.get(i)) + " = " + Integer.toString(op0[i]));
        System.out.println(
                Integer.toString(lst_0.get(i)) + " - "
                        + Integer.toString(lst_1.get(i)) + " = " + Integer.toString(op1[i]));

        if (op0[i] != (lst_0.get(i) + lst_1.get(i))) {
          System.out.println("OUTPUT0 contains incorrect sum");
          System.exit(1);
        }

        if (op1[i] != (lst_0.get(i) - lst_1.get(i))) {
          System.out.println("OUTPUT1 contains incorrect difference");
          System.exit(1);
        }
      }
    } catch (TimeoutException e) {
      System.err.println("Inference request timed out: " + e.getMessage());
      e.printStackTrace();
    }
  }
}

Benefits:

  1. Ensures that InferenceServerClient is properly closed after use, preventing potential resource leaks.
  2. Adds proper error handling for TimeoutException, improving the robustness of the code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant