ndarray.rs 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
//! This module implements the [`NDArray`] type for working with *TVM tensors* or
//! coverting from a Rust's ndarray to TVM `NDArray`.
//!
//! One can create an empty NDArray given the shape, context and dtype using [`empty`].
//! To create an NDArray from a mutable buffer in cpu use [`copy_from_buffer`].
//! To copy an NDArray to different context use [`copy_to_ctx`].
//!
//! Given a [`Rust's dynamic ndarray`], one can convert it to TVM NDArray as follows:
//!
//! # Example
//!
//! ```
//! let a = Array::from_shape_vec((2, 2), vec![1f32, 2., 3., 4.])
//!     .unwrap()
//!     .into_dyn(); // Rust's ndarray
//! let nd = NDArray::from_rust_ndarray(&a, TVMContext::cpu(0), TVMType::from("float32")).unwrap();
//! assert_eq!(nd.shape(), Some(&mut [2, 2]));
//! let rnd: ArrayD<f32> = ArrayD::try_from(&nd).unwrap();
//! assert!(rnd.all_close(&a, 1e-8f32));
//! ```
//!
//! [`Rust's dynamic ndarray`]:https://docs.rs/ndarray/0.12.1/ndarray/
//! [`copy_from_buffer`]:struct.NDArray.html#method.copy_from_buffer
//! [`copy_to_ctx`]:struct.NDArray.html#method.copy_to_ctx

45
use std::{convert::TryFrom, mem, os::raw::c_int, ptr, slice, str::FromStr};
46

47
use failure::Error;
48
use num_traits::Num;
49 50
use rust_ndarray::{Array, ArrayD};
use tvm_common::{ffi, TVMType};
51

52
use crate::{errors, TVMByteArray, TVMContext};
53 54 55 56 57 58

/// See the [`module-level documentation`](../ndarray/index.html) for more details.
///
/// Wrapper around TVM array handle.
#[derive(Debug)]
pub struct NDArray {
59
    pub(crate) handle: ffi::TVMArrayHandle,
60 61 62 63
    is_view: bool,
}

impl NDArray {
64
    pub(crate) fn new(handle: ffi::TVMArrayHandle) -> Self {
65 66
        NDArray {
            handle: handle,
67
            is_view: true,
68 69 70 71
        }
    }

    /// Returns the underlying array handle.
72
    pub fn handle(&self) -> ffi::TVMArrayHandle {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        self.handle
    }

    pub fn is_view(&self) -> bool {
        self.is_view
    }

    /// Returns the shape of the NDArray.
    pub fn shape(&self) -> Option<&mut [usize]> {
        let arr = unsafe { *(self.handle) };
        if arr.shape.is_null() || arr.data.is_null() {
            return None;
        };
        let slc = unsafe { slice::from_raw_parts_mut(arr.shape as *mut usize, arr.ndim as usize) };
        Some(slc)
    }

    /// Returns the total number of entries of the NDArray.
    pub fn size(&self) -> Option<usize> {
        self.shape()
            .map(|v| v.into_iter().fold(1, |acc, &mut e| acc * e))
    }

    /// Returns the context which the NDArray was defined.
    pub fn ctx(&self) -> TVMContext {
        unsafe { (*self.handle).ctx.into() }
    }

    /// Returns the type of the entries of the NDArray.
    pub fn dtype(&self) -> TVMType {
        unsafe { (*self.handle).dtype.into() }
    }

    /// Returns the number of dimensions of the NDArray.
    pub fn ndim(&self) -> usize {
        unsafe { (*self.handle).ndim as usize }
    }

    /// Returns the strides of the underlying NDArray.
    pub fn strides(&self) -> Option<&[usize]> {
        unsafe {
            let sz = self.ndim() * mem::size_of::<usize>();
            let slc = slice::from_raw_parts((*self.handle).strides as *const usize, sz);
            Some(slc)
        }
    }

    /// Shows whether the underlying ndarray is contiguous in memory or not.
121
    pub fn is_contiguous(&self) -> Result<bool, Error> {
122 123 124
        Ok(match self.strides() {
            None => true,
            Some(strides) => {
125 126 127
                // errors::MissingShapeError in case shape is not determined
                self.shape()
                    .ok_or(errors::MissingShapeError)?
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                    .iter()
                    .zip(strides)
                    .rfold(
                        (true, 1),
                        |(is_contig, expected_stride), (shape, stride)| {
                            (
                                is_contig && *stride == expected_stride,
                                expected_stride * (*shape as usize),
                            )
                        },
                    )
                    .0
            }
        })
    }

    pub fn byte_offset(&self) -> isize {
        unsafe { (*self.handle).byte_offset as isize }
    }

    /// Flattens the NDArray to a `Vec` of the same type in cpu.
    ///
    /// ## Example
    ///
    /// ```
    /// let shape = &mut [4];
    /// let mut data = vec![1i32, 2, 3, 4];
    /// let ctx = TVMContext::cpu(0);
    /// let mut ndarray = empty(shape, ctx, TVMType::from("int32"));
    /// ndarray.copy_from_buffer(&mut data);
    /// assert_eq!(ndarray.shape(), Some(shape));
    /// assert_eq!(ndarray.to_vec::<i32>().unwrap(), data);
    /// ```
161 162 163 164 165 166 167
    pub fn to_vec<T>(&self) -> Result<Vec<T>, Error> {
        ensure!(self.shape().is_some(), errors::EmptyArrayError);
        let earr = NDArray::empty(
            self.shape().ok_or(errors::MissingShapeError)?,
            TVMContext::cpu(0),
            self.dtype(),
        );
168 169
        let target = self.copy_to_ndarray(earr)?;
        let arr = unsafe { *(target.handle) };
170
        let sz = self.size().ok_or(errors::MissingShapeError)?;
171 172 173 174 175 176 177 178 179 180
        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)
    }

    /// Converts the NDArray to [`TVMByteArray`].
181
    pub fn to_bytearray(&self) -> Result<TVMByteArray, Error> {
182
        let v = self.to_vec::<u8>()?;
183
        Ok(TVMByteArray::from(v))
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    }

    /// Creates an NDArray from a mutable buffer of types i32, u32 or f32 in cpu.
    ///
    /// ## Example
    ///
    /// ```
    /// let shape = &mut [2];
    /// let mut data = vec![1f32, 2];
    /// let ctx = TVMContext::gpu(0);
    /// let mut ndarray = empty(shape, ctx, TVMType::from("int32"));
    /// ndarray.copy_from_buffer(&mut data);
    /// ```
    ///
    /// *Note*: if something goes wrong during the copy, it will panic
    /// from TVM side. See `TVMArrayCopyFromBytes` in `include/tvm/runtime/c_runtime_api.h`.
    pub fn copy_from_buffer<T: Num32>(&mut self, data: &mut [T]) {
201
        check_call!(ffi::TVMArrayCopyFromBytes(
202 203 204 205 206 207 208
            self.handle,
            data.as_ptr() as *mut _,
            data.len() * mem::size_of::<T>()
        ));
    }

    /// Copies the NDArray to another target NDArray.
209
    pub fn copy_to_ndarray(&self, target: NDArray) -> Result<NDArray, Error> {
210 211 212
        if self.dtype() != target.dtype() {
            bail!(
                "{}",
213 214 215 216
                errors::TypeMismatchError {
                    expected: format!("{}", self.dtype().to_string()),
                    actual: format!("{}", target.dtype().to_string()),
                }
217 218
            );
        }
219
        check_call!(ffi::TVMArrayCopyFromTo(
220 221
            self.handle,
            target.handle,
222
            ptr::null_mut() as ffi::TVMStreamHandle
223 224 225 226 227
        ));
        Ok(target)
    }

    /// Copies the NDArray to a target context.
228 229 230 231 232 233
    pub fn copy_to_ctx(&self, target: &TVMContext) -> Result<NDArray, Error> {
        let tmp = NDArray::empty(
            self.shape().ok_or(errors::MissingShapeError)?,
            target.clone(),
            self.dtype(),
        );
234 235 236 237 238 239 240 241 242
        let copy = self.copy_to_ndarray(tmp)?;
        Ok(copy)
    }

    /// Converts a Rust's ndarray to TVM NDArray.
    pub fn from_rust_ndarray<T: Num32 + Copy>(
        rnd: &ArrayD<T>,
        ctx: TVMContext,
        dtype: TVMType,
243
    ) -> Result<Self, Error> {
244 245 246
        let mut shape = rnd.shape().to_vec();
        let mut nd = NDArray::empty(&mut shape, ctx, dtype);
        let mut buf = Array::from_iter(rnd.into_iter().map(|&v| v as T));
247 248 249 250
        nd.copy_from_buffer(
            buf.as_slice_mut()
                .expect("Array from iter must be contiguous."),
        );
251 252 253 254 255
        Ok(nd)
    }

    /// Allocates and creates an empty NDArray given the shape, context and dtype.
    pub fn empty(shape: &[usize], ctx: TVMContext, dtype: TVMType) -> NDArray {
256 257
        let mut handle = ptr::null_mut() as ffi::TVMArrayHandle;
        check_call!(ffi::TVMArrayAlloc(
258 259
            shape.as_ptr() as *const i64,
            shape.len() as c_int,
260 261 262
            dtype.code as c_int,
            dtype.bits as c_int,
            dtype.lanes as c_int,
263 264 265 266
            ctx.device_type.0 as c_int,
            ctx.device_id as c_int,
            &mut handle as *mut _,
        ));
267 268 269 270
        NDArray {
            handle,
            is_view: false,
        }
271 272 273 274 275 276 277
    }
}

macro_rules! impl_from_ndarray_rustndarray {
    ($type:ty, $type_name:tt) => {
        impl<'a> TryFrom<&'a NDArray> for ArrayD<$type> {
            type Error = Error;
278 279 280 281 282 283 284
            fn try_from(nd: &NDArray) -> Result<ArrayD<$type>, Self::Error> {
                ensure!(nd.shape().is_some(), errors::MissingShapeError);
                assert_eq!(nd.dtype(), TVMType::from_str($type_name)?, "Type mismatch");
                Ok(Array::from_shape_vec(
                    &*nd.shape().ok_or(errors::MissingShapeError)?,
                    nd.to_vec::<$type>()?,
                )?)
285 286 287 288 289
            }
        }

        impl<'a> TryFrom<&'a mut NDArray> for ArrayD<$type> {
            type Error = Error;
290 291 292 293 294 295 296
            fn try_from(nd: &mut NDArray) -> Result<ArrayD<$type>, Self::Error> {
                ensure!(nd.shape().is_some(), errors::MissingShapeError);
                assert_eq!(nd.dtype(), TVMType::from_str($type_name)?, "Type mismatch");
                Ok(Array::from_shape_vec(
                    &*nd.shape().ok_or(errors::MissingShapeError)?,
                    nd.to_vec::<$type>()?,
                )?)
297 298 299 300 301 302 303 304 305 306 307 308
            }
        }
    };
}

impl_from_ndarray_rustndarray!(i32, "int");
impl_from_ndarray_rustndarray!(u32, "uint");
impl_from_ndarray_rustndarray!(f32, "float");

impl Drop for NDArray {
    fn drop(&mut self) {
        if !self.is_view {
309
            check_call!(ffi::TVMArrayFree(self.handle));
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        }
    }
}

mod sealed {
    /// Private trait to prevent other traits from being implemeneted in downstream crates.
    pub trait Sealed {}
}

/// A trait for the supported 32-bits numerical types in frontend.
pub trait Num32: Num + sealed::Sealed {
    const BITS: u8 = 32;
}

macro_rules! impl_num32 {
    ($($type:ty),+) => {
        $(
            impl sealed::Sealed for $type {}
            impl Num32 for $type {}
        )+
    };
}

impl_num32!(i32, u32, f32);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basics() {
        let shape = &mut [1, 2, 3];
        let ctx = TVMContext::cpu(0);
343
        let ndarray = NDArray::empty(shape, ctx, TVMType::from_str("int32").unwrap());
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        assert_eq!(ndarray.shape().unwrap(), shape);
        assert_eq!(
            ndarray.size().unwrap(),
            shape.to_vec().into_iter().product()
        );
        assert_eq!(ndarray.ndim(), 3);
        assert!(ndarray.strides().is_none());
        assert_eq!(ndarray.byte_offset(), 0);
    }

    #[test]
    fn copy() {
        let shape = &mut [4];
        let mut data = vec![1i32, 2, 3, 4];
        let ctx = TVMContext::cpu(0);
359
        let mut ndarray = NDArray::empty(shape, ctx, TVMType::from_str("int32").unwrap());
360 361 362 363 364 365 366 367
        assert!(ndarray.to_vec::<i32>().is_ok());
        ndarray.copy_from_buffer(&mut data);
        assert_eq!(ndarray.shape().unwrap(), shape);
        assert_eq!(ndarray.to_vec::<i32>().unwrap(), data);
        assert_eq!(ndarray.ndim(), 1);
        assert!(ndarray.is_contiguous().is_ok());
        assert_eq!(ndarray.byte_offset(), 0);
        let mut shape = vec![4];
368 369 370 371 372
        let e = NDArray::empty(
            &mut shape,
            TVMContext::cpu(0),
            TVMType::from_str("int32").unwrap(),
        );
373 374 375 376 377 378 379 380 381 382 383
        let nd = ndarray.copy_to_ndarray(e);
        assert!(nd.is_ok());
        assert_eq!(nd.unwrap().to_vec::<i32>().unwrap(), data);
    }

    #[test]
    #[should_panic(expected = "called `Result::unwrap()` on an `Err`")]
    fn copy_wrong_dtype() {
        let mut shape = vec![4];
        let mut data = vec![1f32, 2., 3., 4.];
        let ctx = TVMContext::cpu(0);
384 385 386 387 388
        let mut nd_float = NDArray::empty(
            &mut shape,
            ctx.clone(),
            TVMType::from_str("float32").unwrap(),
        );
389
        nd_float.copy_from_buffer(&mut data);
390
        let empty_int = NDArray::empty(&mut shape, ctx, TVMType::from_str("int32").unwrap());
391 392 393 394 395 396 397 398
        nd_float.copy_to_ndarray(empty_int).unwrap();
    }

    #[test]
    fn rust_ndarray() {
        let a = Array::from_shape_vec((2, 2), vec![1f32, 2., 3., 4.])
            .unwrap()
            .into_dyn();
399 400 401 402 403 404
        let nd = NDArray::from_rust_ndarray(
            &a,
            TVMContext::cpu(0),
            TVMType::from_str("float32").unwrap(),
        )
        .unwrap();
405 406 407 408 409
        assert_eq!(nd.shape().unwrap(), &mut [2, 2]);
        let rnd: ArrayD<f32> = ArrayD::try_from(&nd).unwrap();
        assert!(rnd.all_close(&a, 1e-8f32));
    }
}