-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Arith] ConstIntBound was incorrectly assuming bounds were over int64… #13918
Changes from all commits
68c0ea9
7817c8b
f8578cf
c4084ff
e54d48b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# 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. | ||
import numpy as np | ||
import pytest | ||
import tvm | ||
from tvm import te | ||
import tvm.testing | ||
from tvm.script import tir | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Looks like some redundant imports here. |
||
|
||
|
||
def test_floor_div_op(): | ||
target = "llvm" | ||
dev = tvm.device(target) | ||
N = 100 | ||
divisor = 5 | ||
|
||
@tir.prim_func | ||
def func_64( | ||
A: tir.Buffer((N + 100, 2), "int64"), | ||
B: tir.Buffer((N), "int64"), | ||
C: tir.Buffer((N), "int64"), | ||
): | ||
for i in tir.serial(N): | ||
with tir.block("A"): | ||
v_i = tir.axis.spatial(N, i) | ||
A[v_i, 0] = tir.floordiv(C[v_i] - tir.max_value("int64"), divisor) | ||
A[v_i, 1] = tir.floormod(C[v_i] - tir.max_value("int64"), divisor) | ||
A[v_i + 100, 0] = tir.floordiv(B[v_i], divisor) | ||
A[v_i + 100, 1] = tir.floormod(B[v_i], divisor) | ||
|
||
@tir.prim_func | ||
def func_32( | ||
A: tir.Buffer((N + 100, 2), "int32"), | ||
B: tir.Buffer((N), "int32"), | ||
C: tir.Buffer((N), "int32"), | ||
): | ||
for i in tir.serial(N): | ||
with tir.block("A"): | ||
v_i = tir.axis.spatial(N, i) | ||
A[v_i, 0] = tir.floordiv(C[v_i] - tir.max_value("int32"), divisor) | ||
A[v_i, 1] = tir.floormod(C[v_i] - tir.max_value("int32"), divisor) | ||
A[v_i + 100, 0] = tir.floordiv(B[v_i], divisor) | ||
A[v_i + 100, 1] = tir.floormod(B[v_i], divisor) | ||
|
||
@tir.prim_func | ||
def func_16( | ||
A: tir.Buffer((N + 100, 2), "int16"), | ||
B: tir.Buffer((N), "int16"), | ||
C: tir.Buffer((N), "int16"), | ||
): | ||
for i in tir.serial(N): | ||
with tir.block("A"): | ||
v_i = tir.axis.spatial(N, i) | ||
A[v_i, 0] = tir.floordiv(C[v_i] - tir.max_value("int16"), divisor) | ||
A[v_i, 1] = tir.floormod(C[v_i] - tir.max_value("int16"), divisor) | ||
A[v_i + 100, 0] = tir.floordiv(B[v_i], divisor) | ||
A[v_i + 100, 1] = tir.floormod(B[v_i], divisor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: second set of checks that use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The was primarily to check the behavior of the floormod and floordiv over a random sampling of values across the whole range of integer. I think in the specific case of this test it won't be able to determine the bounds to anything other than min_value(type) max_value(type) so I suppose all it verifies is that the check isn't broken for smaller integer sizes. |
||
|
||
@tir.prim_func | ||
def func_8( | ||
A: tir.Buffer((N + 100, 2), "int8"), B: tir.Buffer((N), "int8"), C: tir.Buffer((N), "int8") | ||
): | ||
for i in tir.serial(N): | ||
with tir.block("A"): | ||
v_i = tir.axis.spatial(N, i) | ||
A[v_i, 0] = tir.floordiv(C[v_i] - tir.max_value("int8"), divisor) | ||
A[v_i, 1] = tir.floormod(C[v_i] - tir.max_value("int8"), divisor) | ||
A[v_i + 100, 0] = tir.floordiv(B[v_i], divisor) | ||
A[v_i + 100, 1] = tir.floormod(B[v_i], divisor) | ||
|
||
for opfunc, type in [ | ||
(func_8, "int8"), | ||
(func_16, "int16"), | ||
(func_32, "int32"), | ||
(func_64, "int64"), | ||
]: | ||
built = tvm.build(opfunc, target=target) | ||
x_data = np.random.randint(te.min_value(type), te.max_value(type), size=(100), dtype=type) | ||
y_data = np.asarray([i for i in range(N)], dtype=type) | ||
|
||
a_dev = tvm.nd.empty([N + 100, 2], type, dev) | ||
b_dev = tvm.nd.array(x_data, dev) | ||
c_dev = tvm.nd.array(y_data, dev) | ||
|
||
built(a_dev, b_dev, c_dev) | ||
|
||
a = a_dev.numpy() | ||
b = b_dev.numpy() | ||
c = c_dev.numpy() | ||
|
||
# python modulo behaves a bit different to tvm floormod for negative numbers | ||
for i in range(N + 100): | ||
if a[i, 1] < 0: | ||
a[i, 1] = divisor + a[i, 1] | ||
|
||
np.testing.assert_array_equal(a[:100, 0], (c - te.max_value(type)) // divisor) | ||
np.testing.assert_array_equal(a[:100, 1], (c - te.max_value(type)) % divisor) | ||
np.testing.assert_array_equal(a[100 : N + 100, 0], b // divisor) | ||
np.testing.assert_array_equal(a[100 : N + 100, 1], b % divisor) | ||
|
||
|
||
if __name__ == "__main__": | ||
tvm.testing.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reusing the existing APIs to be consistent 😅