question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with equality operator of `expr.Mod`?

See original GitHub issue

I’m looking at unit test of ir builder, looks like the == operator does not work for Mod as expected:

    ib = tvm.ir_builder.create()
    n = tvm.var("n")
    A = ib.pointer("float32", name="A")
    with ib.for_range(0, n, name="i") as i:
        with ib.if_scope((i % 2) == 0):
            A[i] = A[i] + 1
        with ib.else_scope():
            A[0] = A[i] + 2

The generated IR is:

➜  ~ ipython
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import tvm
   ...:

In [2]:     ib = tvm.ir_builder.create()
   ...:     n = tvm.var("n")
   ...:     A = ib.pointer("float32", name="A")
   ...:     with ib.for_range(0, n, name="i") as i:
   ...:         with ib.if_scope((i % 2) == 0):
   ...:             A[i] = A[i] + 1
   ...:         with ib.else_scope():
   ...:             A[0] = A[i] + 2
   ...:
   ...:     body = ib.get()
   ...:

In [3]: body
Out[3]:
for (i, 0, n) {
  if (0) {
    A[i] = (A[i] + 1.000000f)
  } else {
    A[0] = (A[i] + 2.000000f)
  }
}

Note that the condition expression is constant 0 instead of an equality check expression. The __eq__ operator for Mod comes from tvm/_ffi/node.py

In [4]: i = ib.for_range(0, n, name="i")

In [5]: a = i._enter_value % 1

In [6]: a.__eq__.im_func
Out[6]: <function tvm._ffi.node.__eq__>

Which I assume what we want is tvm/expr.py?

I modify the unit test to use self.equal directly, and the code now makes sense to me:

In [7]: %paste
    ib = tvm.ir_builder.create()
    n = tvm.var("n")
    A = ib.pointer("float32", name="A")
    with ib.for_range(0, n, name="i") as i:
        with ib.if_scope((i % 2).equal(0)):
            A[i] = A[i] + 1
        with ib.else_scope():
            A[0] = A[i] + 2

    body = ib.get()
## -- End pasted text --

In [8]: body
Out[8]:
for (i, 0, n) {
  if (((i % 2) == 0)) {
    A[i] = (A[i] + 1.000000f)
  } else {
    A[0] = (A[i] + 2.000000f)
  }
}

Let me know if you think this is a problem. I’d be interested to help figure out why.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
wweiccommented, Oct 12, 2017

That sounds good. I also prefer leaving the syntax sugar == for logical equality operator. Will follow up this later.

0reactions
wweiccommented, Oct 13, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use mod operator in bash? - Stack Overflow
Try the following: for i in {1..600}; do echo wget http://example.com/search/link$(($i % 5)); done. The $(( )) syntax does an arithmetic ...
Read more >
Modulo Operator (%) in C/C++ with Examples - GeeksforGeeks
The modulo division operator produces the remainder of an integer division. Syntax: If x and y are integers, then the expression:.
Read more >
What is modular arithmetic? (article) - Khan Academy
Sometimes, we are only interested in what the remainder is when we divide A A AA by B B BB. For these cases...
Read more >
How to Use Modulus in Bash? - Linux Hint
The modulo operator is widely known to determine the remainder of two values upon division. This mathematical operator can also be used in...
Read more >
Mod Operator - Visual Basic - Microsoft Learn
This can lead to unexpected results from certain operations, such as value comparison and the Mod operator. For more information, see ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found