haoyang9804 opened a new pull request #10502:
URL: https://github.com/apache/tvm/pull/10502


   When `relay.build` a IRModule in which a function whose returned value 
includes another function exists, TVM will crash with **segmentation fault**. 
Initially I assume this is the behavior disllowed by TVM, but in 
`test_analysis_basic_block_normal_form.py`, the following function shows the 
tolerance of it.
   ```python
   @pytest.mark.xfail(raises=tvm.error.TVMError)
   def test_func():
       x = relay.var("x", shape=(1,), dtype="float32")  # , a)
       y = relay.var("y", shape=(1,), dtype="float32")  # , a)
       z = relay.var("z", shape=(1,), dtype="float32")  # , a)
       x2 = relay.add(x, x)
       func_a = relay.Function([y], relay.add(x2, y))  # , a, [a])
       func_b = relay.Function([z], relay.add(x2, z))  # , a, [a])
       body = relay.Tuple([func_a, func_b])
       body = relay.Function([x], body)
       """
       fn (%x: Tensor[(1), float32]) {
         %1 = fn (%y: Tensor[(1), float32]) {
           %0 = add(%x, %x);
           add(%0, %y)
         };
         %2 = fn (%z: Tensor[(1), float32]) {
           add(%0, %z)
         };
         (%1, %2)
       }
       """
       check_basic_block_normal_form(body)
   ```
   So I write the following script with simpler structure to fuzz TVM:
   ```python
   import tvm
   from tvm import relay
   mod = tvm.IRModule()
   x = relay.var("x", shape=(1,), dtype="float32")
   x2 = relay.add(x, x)
   f = relay.Function([x], x2)
   # body = relay.Tuple([f,])
   body = relay.Function([], f)
   mod['main'] = body
   mod = relay.transform.InferType()(mod)
   print(mod.astext(show_meta_data=False))
   graph, lib, params = relay.build(mod, target='llvm')
   ```
   The relay IR is easy, only including a function whose returned value is 
another function
   ```
   #[version = "0.0.5"]
   def @main() -> fn (Tensor[(1), float32]) -> Tensor[(1), float32] {
     fn (%x: Tensor[(1), float32]) -> Tensor[(1), float32] {
       add(%x, %x) /* ty=Tensor[(1), float32] */
     }
   }
   ```
   As expected, segmentation fault showed up.
   By bug localization, I find this problem is caused during memory allocation 
in graph_executor_codegen. More concretely, function 
`CalculateRelayExprSizeBytes` forgets the situation where the type returned 
variable could be of FuncType and directly turns the typenode into 
TensorTypeNode. Then `auto shape = tensor_type->shape;` triggers the bug.
   


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


Reply via email to