-
Notifications
You must be signed in to change notification settings - Fork 8
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
Error "tried creating tensor with negative value in shape" #71
Comments
Hi, @fullymiddleaged Could you please attach the model if it's not too big? |
Hi @kibae ! Sure, the model and example inputs can be downloaded via OneDrive here (don't worry it's only 40mb) - these use the same test data/expected result I have been using. To get these arrays I ran a working inference in python, and dumped the inputs and prediction values to text, then manipulated the inputs to be in the format required for posting json to your api server. For reference, a snippet of my python code below shows the method of creating the inputs and obtaining the prediction. This may be helpful in case I've translated the input json for your api incorrectly. You also get to see how my arrays were created. tokenizer = AutoTokenizer.from_pretrained(config.MODEL_FOLDER)
inputs = tokenizer(text, return_tensors="np", truncation=True, padding="max_length", max_length=512)
inputs = {k: v.astype(np.int64) for k, v in inputs.items()}
output = onnxsession.run(output_names=[label_name], input_feed=dict(inputs))[0]
prediction = np.squeeze(tf.keras.activations.softmax(tf.convert_to_tensor(output)).numpy()) Perhaps I'm being silly and not posting the inputs correctly, as Netron shows me the inputs as both needing to be int64[batch_size,sequence_length] - perhaps I've missed something here? I look forward to your reply, many thanks! |
Please also see additional useful info below: I am running the 1.20.1-linux-cpu docker image with the following options: onnxruntime_server:
# After the docker container is up, you can use the REST API (http://localhost:8080).
# API documentation will be available at http://localhost:8080/api-docs.
image: kibaes/onnxruntime-server:1.20.1-linux-cpu
ports:
- "8080:80" # for http backend
volumes:
# for model files
# https://github.com/kibae/onnxruntime-server#run-the-server
- ./models:/app/models
# for log files
- ./logs:/app/logs
environment:
# for swagger(optional)
- ONNX_SERVER_SWAGGER_URL_PATH=/api-docs
- ONNX_SERVER_MODEL_DIR=/app/models
- ONNX_SERVER_PREPARE_MODEL=quantized:v1(cuda=false) |
Hello, @fullymiddleaged :) I’ve written test code based on the ONNX file and the request you kindly provided. The shape of When using the model in Python, it works without confusion because the tokenizer returns onnxruntime-server/test/sample-onnx-generator/sample-model1.py Lines 73 to 85 in c2b2141
This link includes an example of how to specify the shape for each variable during ONNX export. Could you try re-exporting the ONNX model following this approach? In the meantime, I will consider ways to forcibly define the shape during the session creation process. If you have any good ideas, I’d appreciate it if you could share them! |
Hi @kibae - thanks for the reply and for investigating. Sadly, I've made a few attempts at changing my model, but I'm not having much luck loading my model.safetensors file into Torch to then re-export (I'm using colab and I think the versions of a dependency aren't matching, eek). So, I may end up having to re-train first. 😓 But it would indeed be amazing to define the shape during session creation, I'm happy to test this hard-coded for now if you like? As for possible interactive methods, maybe any of the below could work and are easy to document? Option 1 - Via session options environment variable. - ONNX_SERVER_PREPARE_MODEL=quantized:v2(cuda=false,batchsize=1,length=512) Option 2: via POST call to /api/sessions: {
"model": "string",
"version": "string",
"option": {
"cuda": true
"batchsize":1
"length":512
}
} In both cases an assumption could be made that this would be a global option applied to the model's inputs (e.g. if it had 2 inputs, they would both need to have matching sizes) as beyond this it becomes a lot harder to clearly define maybe? Would the array type (e.g. int64/float32) also need to be specified - hopefully not. |
Hi, @fullymiddleaged Thank you for your great suggestion. 👍 Since there could be multiple inputs, I believe option 2 might be a better approach than option 1. I’ll give it more thought after the year-end holidays. I was also considering whether we could treat shapes like Wishing you a Merry Christmas and a Happy New Year! Thank you. |
Hey, thats actually a great idea around the -1 inputs. If the input length could remain dynamic like that it would make it extra useable. :) I've managed to edit my onnx file inputs and can now carry on my tests. Have a good Christmas break! Let's carry on later, I may have some other ideas too which could be awesome to add. 🎄🎄 |
Hi,
I have an onnx model that works correctly via the python api for onnxruntime, but if I send identical input array values (as sent via python version) the api here will error with "tried creating tensor with negative value in shape". My arrays are int64 type, with input_ids and attention_mask array values.
I have read posts suggesting that because the input dimension is listed as -1 there is some manual way of specifying this in C, that doesn't seem to be an issue with the python api for onnxruntime.
I am happy to attach my model if it helps. I have a feeling this could be an easy fix for a C guru. At least I hope so!
Thanks!
The text was updated successfully, but these errors were encountered: