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

原始是byte[]的数据,没法传。会报triton.client.InferenceException: [request id: <id_unknown>] inference input data-type is 'INT8', model expects 'BYTES' for 'xxxxx' #784

Open
freeleeq opened this issue Sep 24, 2024 · 3 comments

Comments

@freeleeq
Copy link

freeleeq commented Sep 24, 2024

API:
{
name: "IMAGE" # image
data_type: TYPE_STRING
dims: [ 1 ]
}

CODE:
byte[] imgBytes = bos.toByteArray();
InferInput imgInput = new InferInput("IMAGE", new long[] {1, imgBytes.length}, DataType.INT8);
imgInput.setData(imgBytes, true);

@freeleeq freeleeq changed the title 没法原始便是byte[]的数据。会报triton.client.InferenceException: [request id: <id_unknown>] inference input data-type is 'INT8', model expects 'BYTES' for 'xxxxx' 原始是byte[]的数据,没法传。会报triton.client.InferenceException: [request id: <id_unknown>] inference input data-type is 'INT8', model expects 'BYTES' for 'xxxxx' Sep 24, 2024
@freeleeq
Copy link
Author

if use byte[] -> String,It will get worse

@jixiwen
Copy link

jixiwen commented Jan 9, 2025

try try this, it work on my project,and tensor result STRING, you need reverse operation
算了,英文不好,我说中文,triton的STRING类型需要你传Tensor的BYTES类型进去

重点来了:你把String直接转换成byte[],他是不认识的,因为他需要你把这个byte数组的长度拼到数组的最前面,用int32,也就是4个字节的int值,而且要小端序存储,下面是我写的处理方法,拿过去直接用就好了

创建参数的逻辑

byte[] bytes = string2InputByte(inputJson);
InferInput otherInput = new InferInput(Const.INPUT, new long[]{1}, DataType.BYTES);
otherInput.setData(bytes, true);
inputs.add(otherInput);

转换方法

    private byte[] string2InputByte(String str) {
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        return getParamByte(bytes);
    }
    public static byte[] convert2ParamByte(byte[] data) {
        // 小端序存一个int
        ByteBuffer byteBuffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.putInt(data.length);
        // 获取int 的小端序字节数组
        byte[] lenBytes = byteBuffer.array();
        // 拼接并返回结果
        byte[] result = new byte[lenBytes.length + data.length];
        System.arraycopy(lenBytes, 0, result, 0, lenBytes.length);
        System.arraycopy(data, 0, result, lenBytes.length, data.length);
        return result;
    }

获取String类型的结果

如果你要从结果中获取STRING类型的结果,那就反向操作,获取byte[]之后,把前四个字节去掉,下面是操作方法

InferResult result = client.infer("generalOcr", inputs, Lists.newArrayList(resultOutput));
byte[] binaryData = result.getBinaryData();
ByteBuffer wrap = ByteBuffer.wrap(binaryData);
wrap.order(ByteOrder.LITTLE_ENDIAN);
int length = wrap.getInt();
String algResult = new String(binaryData, binaryData.length - length, length);

@freeleeq
Copy link
Author

freeleeq commented Jan 9, 2025 via email

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

2 participants