You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/09/30 00:16:29 UTC

[GitHub] [incubator-tvm] jroesch commented on a change in pull request #6563: [Rust] Improve NDArray, GraphRt, and Relay bindings

jroesch commented on a change in pull request #6563:
URL: https://github.com/apache/incubator-tvm/pull/6563#discussion_r497170872



##########
File path: rust/tvm-rt/src/ndarray.rs
##########
@@ -184,28 +246,19 @@ impl NDArray {
     /// let ctx = Context::cpu(0);
     /// let mut ndarray = NDArray::empty(&mut shape, ctx, DataType::from_str("int32").unwrap());
     /// ndarray.copy_from_buffer(&mut data);
-    /// assert_eq!(ndarray.shape(), Some(&mut shape[..]));
+    /// assert_eq!(ndarray.shape(), shape);
     /// assert_eq!(ndarray.to_vec::<i32>().unwrap(), data);
     /// ```
     pub fn to_vec<T>(&self) -> Result<Vec<T>, NDArrayError> {
-        if !self.shape().is_some() {
-            return Err(NDArrayError::EmptyArray);
-        }
-        let earr = NDArray::empty(
-            self.shape().ok_or(NDArrayError::MissingShape)?,
-            Context::cpu(0),
-            self.dtype(),
-        );
-        let target = self.copy_to_ndarray(earr)?;
-        let arr = target.as_dltensor();
-        let sz = self.size().ok_or(NDArrayError::MissingShape)?;
-        let mut v: Vec<T> = Vec::with_capacity(sz * mem::size_of::<T>());
-        unsafe {
-            v.as_mut_ptr()
-                .copy_from_nonoverlapping(arr.data as *const T, sz);
-            v.set_len(sz);
-        }
-        Ok(v)
+        let n = self.size() / size_of::<T>();

Review comment:
       We probably need to constrain T anyways, @mwillsey and I were trying to move the arrays on to the new bindings but it seems like there are so many unsafe gotchas from previous bindings we are trying to work through. In general I think we should probably change NDArray to be like NDArray<T> where T: DataType or something like that. We could probably just slap a `: Sized` on it for now. 




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org