12101111 commented on issue #6197:
URL: https://github.com/apache/incubator-tvm/issues/6197#issuecomment-668367782


   This example code is incorrect.
   ```rust
       // get the global TVM graph runtime function
       let runtime_create_fn = 
Function::get("tvm.graph_runtime.create").unwrap();
       let runtime_create_fn_ret = runtime_create_fn.invoke(vec![
           graph.into(),
           (&lib).into(),
           (&ctx.device_type).into(),
           (&ctx.device_id).into(),
       ]);
   ```
   The `into()` here don't convert `Context` to `ffi::DLContext`, so type of 
`ctx.device_id` is still  `usize`.
   To confirm this, run the following code.
   ```rust
       let runtime_create_fn_ret = runtime_create_fn.invoke(vec![
           graph.into(),
           (&lib).into(),
           dbg!((&ctx.device_type).into()),
           dbg!((&ctx.device_id).into()),
       ]);
   ```
   The output is:
   ```
   [src/main.rs:37] (&ctx.device_type).into() = Int(
       1,
   )
   [src/main.rs:38] (&ctx.device_id).into() = UInt(
       0,
   )
   thread 'main' panicked at 'TVMError: Check failed: type_code_ == kDLInt (1 
vs. 0) : expected int but get uint
   ```
   This C++ type check macro expect type `int` but get `uint`.
   
   And type of `ffi::DLContext` is wrong too. 
   rust-bindgen generated this code:
   ```rust
   #[doc = " \\brief The device type in DLContext."]
   pub type DLDeviceType = u32;
   #[doc = " \\brief A Device context for Tensor and operator."]
   #[repr(C)]
   #[derive(Debug, Copy, Clone, PartialEq, Eq)]
   pub struct DLContext {
       #[doc = " \\brief The device type used in the device."]
       pub device_type: DLDeviceType,
       #[doc = " \\brief The device index"]
       pub device_id: ::std::os::raw::c_int,
   }
   ```
   the type of `device_type`, `DLDeviceType` is `u32` in rust, but C++ side 
expect it's a `int`:
   ```rust
       let dlctx: ffi::DLContext = ctx.into();
       let runtime_create_fn_ret = runtime_create_fn.invoke(vec![
           graph.into(),
           (&lib).into(),
           dbg!((&dlctx.device_type).into()),
           dbg!((&dlctx.device_id).into()),
       ]);
   ```
   ```
   [src/main.rs:37] (&dlctx.device_type).into() = UInt(
       1,
   )
   [src/main.rs:38] (&dlctx.device_id).into() = Int(
       0,
   )
   thread 'main' panicked at 'TVMError: Check failed: type_code_ == kDLInt (1 
vs. 0) : expected int but get uint
   ```
   The following code can pass the type check (and run into another error, 
which I should open a new issue for it)
   ```rust
   let runtime_create_fn_ret = runtime_create_fn.invoke(vec![
           graph.into(),
           (&lib).into(),
           dbg!(1i32.into()),
           dbg!(0i32.into()),
       ]);
   ```
   ```
   [src/main.rs:37] 1i32.into() = Int(
       1,
   )
   [src/main.rs:38] 0i32.into() = Int(
       0,
   )
   terminate called after throwing an instance of 'std::bad_alloc'
     what():  std::bad_alloc
   ```
   (This std::bad_alloc is cause by 
`load_param_fn.invoke(vec![barr.into()]).unwrap();`)


----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to