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 2023/01/17 09:54:08 UTC

[GitHub] [tvm] Kevin-XiongC opened a new issue, #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Kevin-XiongC opened a new issue, #13796:
URL: https://github.com/apache/tvm/issues/13796

   ``` python
   import tvm
   from tvm import relay
   from tvm.contrib import graph_executor
   import numpy as np
   x=relay.var("x",shape=[2,2],dtype="int32")
   t1=relay.cast(x,"bool")
   y=relay.sum(t1)
   f = relay.Function([x],relay.Tuple([t1,y]))
   mod = tvm.IRModule({"main":f})
   target = "llvm"
   with tvm.transform.PassContext(opt_level=3 ):
       lib = relay.build(mod, target=target)
   m = graph_executor.GraphModule(lib['default'](tvm.cpu()))
   x = np.ones((2,2)).astype("int32")
   print(x)
   m.set_input("x",x)
   print(m.get_output(0))
   print(m.get_output(1))
   ```
   The code snippets above should be equivalent to below:
   
   ### Expected behavior
   
   What you were expecting
   
   ### Actual behavior
   
   What actually happened
   
   ### Environment
   
   Any environment details, such as: Operating System, TVM version, etc
   
   ### Steps to reproduce
   
   Preferably a minimal script to cause the issue to occur.
   
   ### Triage
   
   Please refer to the list of label tags [here](https://github.com/apache/tvm/wiki/Issue-Triage-Labels) to find the relevant tags and add them below in a bullet format (example below).
   
   * needs-triage
   


-- 
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: commits-unsubscribe@tvm.apache.org.apache.org

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


[GitHub] [tvm] masahi commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "masahi (via GitHub)" <gi...@apache.org>.
masahi commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1449122523

   Didn't get what you meant, are you saying that we might want to cast bool to int before summation? If the result the result is > 1, we need to convert that to 1.


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] masahi commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "masahi (via GitHub)" <gi...@apache.org>.
masahi commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1447296887

   @padreofthegame that seems good to me! Moreover, I think a "more correct" binary op of sum reduction for boolean is xor. Feel free to send a PR for such change.


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] masahi commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by GitBox <gi...@apache.org>.
masahi commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1386085664

   @Kevin-XiongC Interesting finding, does this come up in a real model?


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Kevin-XiongC commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by GitBox <gi...@apache.org>.
Kevin-XiongC commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1386351976

   @masahi  It was found when I was adapting OPs from a training frontend as an alternative for relay.any().


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Kevin-XiongC commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "Kevin-XiongC (via GitHub)" <gi...@apache.org>.
Kevin-XiongC commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1407934995

   > 
   
   ``` python
   import tvm
   from tvm import relay
   from tvm.contrib import graph_executor
   import numpy as np
   x=relay.var("x",shape=[2,2],dtype="float32")
   t1=relay.cast(x,"bool")
   y=relay.sum(t1)
   f = relay.Function([x],relay.Tuple([t1,y]))
   mod = tvm.IRModule({"main":f})
   target = "llvm"
   with tvm.transform.PassContext(opt_level=3):
       lib = relay.build(mod, target=target)
   m = graph_executor.GraphModule(lib['default'](tvm.cpu()))
   x = np.ones((2,2)).astype("float32")
   m.set_input("x",x)
   m.run()
   print(m.get_output(1))
   print(np.sum(x,dtype=bool))
   
   ```
   
   Apologize for my carelessness. Now it should be the issue of relay.sum() whose behavior is different from np.sum().
   
   ``` bash 
   False
   True
   ```


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] quic-sanirudh commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by GitBox <gi...@apache.org>.
quic-sanirudh commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1385466068

   Okay, when I ran it 30 times in a loop, I got a 2-3 of the outputs as false. There seems to be something inconsistent going on here. I'll look into cast and see what's going on if possible, and sorry for the earlier reply. Looks like there is some unexpected/undefined behavior.


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] padreofthegame commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "padreofthegame (via GitHub)" <gi...@apache.org>.
padreofthegame commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1449113893

   Yeah @masahi, you are absolutely right. In my opinion `xor` is is also considerable as the some kind of sum operator in boolean algebra. 
   
   But one more observation I came up today working on PR is that numpy actually doesn't care about the fact that the input data are of type boolean. It looks like it consider them as zeroes and ones, and it just accumulate corresponding values for True and False. For example, it even  this 
   
   `np.sum(np.array([[True, True], [True, True]]), axis = ())` 
   
   evaluates as
   
   `array([[1, 1],`
   `          [1, 1]])`
   
   So looking at the example reported in this issue (`print(np.sum(x,dtype=bool))`), it may be reasonable to think that in this case numpy do additional cast operation after summation, and maybe better option here, from the aspect of topi.sum or relay.sum, would be to interpret all boolean data as integer values 0 or 1, and keep the current realization of sum function?


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] masahi commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "masahi (via GitHub)" <gi...@apache.org>.
masahi commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1449837170

   I prefer not to do that. If user wants to sum a boolean array for the purpose of counting, cast should be explicitly done at the Relay level.


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] masahi closed issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "masahi (via GitHub)" <gi...@apache.org>.
masahi closed issue #13796: [Bug]  Weird behaviors for relay.sum and relay.cast op
URL: https://github.com/apache/tvm/issues/13796


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] vinx13 commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "vinx13 (via GitHub)" <gi...@apache.org>.
vinx13 commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1405799338

   @Kevin-XiongC it seems you didn't call `m.run()` before getting output and as a result it returned uninitialized tensor


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] padreofthegame commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "padreofthegame (via GitHub)" <gi...@apache.org>.
padreofthegame commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1447286279

   I came up on the similar problem working with `relay.sum` operator. It looks like whenever there is even number of True values in corresponding summing axis result will always be False. Taking this observation into account, it really looks to me that the summing operation works fine, but since the expected output is supposed to be boolean, state of the return value is derived based on the LS bit. That may be the explanation for function returning False for even number of True values. I solved this by just modifying the `relay.sum` operator in relay using cast operation. Similar thing can be done in `topi.sum`, but it requires 2 cast operations to be done, which may be unnecessary. 
   
   But looking deeper into code, I realized that both `relay.sum` and `topi.sum` realizations, depends on the same `sum` function, which means that both problems may be solved by delegating the job inside that function to `tvm::any` function instead of `tvm::sum`, in the case of boolean tensors. Luckily, `tvm::sum` and `tvm::any` has the same calling arguments, so probably the solution for this may be changing the line: 
   
   `return CommReduce(data, axis, tvm::sum, keepdims, atleast1d);`
   
   in the corresponding `sum` function with:
   
   `if(data->dtype.is_bool())`
   `    return CommReduce(data, axis, tvm::any, keepdims, atleast1d);`
   `else`
   `    return CommReduce(data, axis, tvm::sum, keepdims, atleast1d);`
   
   @vinx13 @masahi 


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] Kevin-XiongC commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by GitBox <gi...@apache.org>.
Kevin-XiongC commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1385449830

   
   Yeah, I believe the LLVM version should by no means cause that since these are quite basic computations. That's why I am pretty confused here.
   ![image](https://user-images.githubusercontent.com/22584786/212915258-0e7679b0-a34a-4ace-86f8-6ad41d6c616a.png)
   


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] quic-sanirudh commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by GitBox <gi...@apache.org>.
quic-sanirudh commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1385419141

   This seems to return all True for me consistently with the same commit ID, but I'm also using the latest LLVM and Ubuntu 18.04, but that should not cause changes as far as I can understand.


-- 
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: commits-unsubscribe@tvm.apache.org

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


[GitHub] [tvm] padreofthegame commented on issue #13796: [Bug] Weird behaviors for relay.sum and relay.cast op

Posted by "padreofthegame (via GitHub)" <gi...@apache.org>.
padreofthegame commented on issue #13796:
URL: https://github.com/apache/tvm/issues/13796#issuecomment-1449667605

   Yes and no. Why don't return integer result (as numpy does), which will represent number of True values in corresponding axis. For example:
   
   `data = np.array([[True, True, True], [False, True, True], [False, False, True]])`
   `result = np.sum(data, (0,))`
   
   `print(result)`
   `print(result.dtype)`
   
   ======================
   `array([1, 2, 3])`
   `dtype('int64')`
   
   Difference with the example above is that there we explicitly requested output to be boolean in the call of `np.sum(x,dtype=bool)`, providing the `dtype` parameter. I also suggest maybe to keep it simple and in the case of boolean inputs to cast them to int and provide them to `sum` operator. It should result also with integer output. But if someone wants output to be of other type, it should say that explicitly in the relay or topi definition (by calling `cast` on obtained result).
   
   


-- 
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: commits-unsubscribe@tvm.apache.org

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