oraluben commented on issue #325:
URL: https://github.com/apache/tvm-ffi/issues/325#issuecomment-3634956826

   Thanks!
   
   Yes, such helper utilities would indeed be very useful. For tensors passed 
from an external framework, the goal can be achieved by modifying the shape and 
stride fields of the DLTensor, and I have verified this is feasible in a local 
POC (this doesn't look safe in C++, would be great if you could help with that 
as well):
   
   ```
   static tvm::ffi::Tensor unsafe_view(const tvm::ffi::Tensor& t, const 
tvm::ffi::ShapeView& shape, const tvm::ffi::ShapeView& strides) {
       tvm::ffi::TensorObj* n = 
static_cast<tvm::ffi::TensorObj*>(TVMFFITensorGetDLTensorPtr(const_cast<void*>(static_cast<const
 void*>(&t))));
   
       n->ndim = shape.size();
       n->shape = const_cast<int64_t*>(shape.data());
       n->strides = const_cast<int64_t*>(strides.data());
       return t;
   }
   ```
   
   However, for cases where allocation truly needs to happen within tvm-ffi, 
specifically in scenarios requiring control over memory layout, there is 
sometimes a need to propagate these stride details back to runtimes like 
PyTorch. In such cases, the layout information in the DLTensor would be lost. I 
believe providing optional parameters in `FromEnvAlloc` would be a good 
implementation approach.
   
   To have a more concrete example, if we want to return a tensor allocated in 
tvm-ffi, we can use `FromEnvAlloc` to replace `at::empty`, but there's no way 
to replace an `at::empty_strided` (and `at::empty_like` for complex layout).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to