-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[Documentation] Mixing CXX API and C API dtypes #17498
Comments
The C++ API is a convenience wrapper over the C API rather than a separate API. As such it's tightly bound to the C API types. You can create an allocator with either Simply pass the ORT C++ type - it will automatically convert to a pointer to the wrapped C API type. In this case an Ort::Allocator instance will convert to OrtAllocator* automatically. e.g. Ort::AllocatorWithDefaultOptions allocator;
Ort::Session session = Ort::Session(*ort_env, model.c_str(), Ort::SessionOptions{});
std::vector<int64_t> shape{1, 2, 3, 4};
auto ort_value = Ort::Value::CreateTensor<float>(allocator, shape.data(), shape.size()); |
This is exactly what I tried with the result being:
The compile chain is VS build tools 2022. |
Please, check if your |
Oh yeah sorry I noticed this shortly after posting as well and tried without |
@scottmckay just a friendly ping ;) I believe it is just some compiler warning magic, but it would be great to understand this better. |
@JTischbein I believe you also hit problems with allocators right ? |
@gedoensmax sorry - didn't notice this as my github id is skottmckay not the 'scottmckay' you tagged. It seems like your issue is due to you declaring the allocator as being const: I assume there's some helper code or something involved as I don't see a Again, I'm not sure what a e.g. // DoSomething doesn't need to care if the T is in a unique_ptr or not, so we take a const T&
void DoSomething(const T& value) { ... }
unique_ptr<T> instance = std::make_unique<T>(...); // this owns the T instance and deletes it when it goes out of scope.
DoSomething(*instance); |
@skottmckay sorry for mistyping you gh id, for some reason it always takes forever to auto complete. const auto mem_info = Ort::MemoryInfo("Cuda",
OrtAllocatorType::OrtDeviceAllocator,
0,
OrtMemType::OrtMemTypeDefault);
auto allocator = Ort::Allocator(session_, &mem_info);
std::vector<int64_t> shape{1, 1, 64, 64};
auto ort_value = Ort::Value::CreateTensor(&allocator, shape.data(), shape.size(),
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); When adding appropriate, and if I understand even non braking const auto mem_info = Ort::MemoryInfo("Cuda",
OrtAllocatorType::OrtDeviceAllocator,
0,
OrtMemType::OrtMemTypeDefault);
auto allocator = Ort::Allocator(session_, reinterpret_cast<const OrtMemoryInfo*>(&mem_info));
std::vector<int64_t> shape{1, 1, 64, 64};
auto ort_value = Ort::Value::CreateTensor(reinterpret_cast<OrtAllocator*>(&allocator), shape.data(), shape.size(),
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); Nonetheless this looks quite unintuitive for a C++ API, while the docs show the correct types this is mangles C and C++ API IMO. Or I am still doing something wrong. |
The C++ API is a lightweight convenience wrapper over the C API (vs. a 'real' parallel standalone C++ API). Due to this, a reference to a C++ API class implicitly converts to a pointer to the C API type, and the arguments to the C++ API methods are the C API types. So in your original code the only issue is you're passing a pointer to the C++ class instead of letting the implicit conversion to the C API type happen. The fix is to remove the '&' from auto allocator = Ort::Allocator(session_, mem_info);
// ...
auto ort_value = Ort::Value::CreateTensor(allocator, shape.data(), shape.size(),
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT);
With that change it compiles for me. |
Ok thank you a lot for explaining, that works. I did not know that concept behind the C++ API and somehow got around it so far. |
Describe the documentation issue
What would be the nicest way to allocate additional tensors given a session ?
I tried allocating tensors given a session but it feels unnatural as all the
CreateTensor
APIs need C API structs. Not even entirely sure if it works that way.Below links show the mixed C and CXX structs usage in the CXX API. It would be great to get an idea on how to best do this.
A way to resolve this would be to call
Alloc
on the allocator and callCreateTensor
with memory info and a raw buffer.Page / URL
No response
The text was updated successfully, but these errors were encountered: