Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Fixed jitify commit to prevent header file conflicts (#1522)
Browse files Browse the repository at this point in the history
* Fixed jitify commit to prevent header file conflicts

* Set random seed for debug floor_divide

* Avoid oom error

* Just for debug ci

* Fix floor_divide error when input dtype is int

* Fix bugs and add more tests for floor_divide
  • Loading branch information
FisherWY authored Jun 14, 2023
1 parent 186a981 commit 725ddd5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 132 deletions.
4 changes: 2 additions & 2 deletions cinn/hlir/op/contrib/repeat_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function TestGenerateCodeCpu_Repeat (_test_repeat)
ScheduleBlock(test_repeat)
{
i0, i1 = axis.bind(i, j)
test_repeat[i0, i1] = in[(i0 / 2), i1]
test_repeat[i0, i1] = in[select((((i0 > 0) and (2 > 0)) or ((i0 < 0) and (2 < 0))), (i0 / 2), select(((i0 % 2) == 0), (i0 / 2), ((i0 / 2) - 1))), i1]
}
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ void TestGenerateCodeCpu_Repeat(void* _args, int32_t num_args)
int32_t* test_repeat = ((int32_t*)(_test_repeat->memory));
for (int32_t i = 0; i < 8; i += 1) {
for (int32_t j = 0; j < 4; j += 1) {
test_repeat[((4 * i) + j)] = in[(((i / 2) * 4) + j)];
test_repeat[((4 * i) + j)] = in[((4 * (((((i > 0) && (2 > 0)) || ((i < 0) && (2 < 0)))) ? (i / 2) : ((((i & 1) == 0)) ? (i / 2) : ((i / 2) + -1)))) + j)];
};
};
cinn_buffer_free((void*)(0), _in);
Expand Down
12 changes: 11 additions & 1 deletion cinn/lang/builtin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,17 @@ Expr One(const Type& type) { return ir::One(type); }

Expr FloorDivide(Expr a, Expr b) {
CHECK_EQ(a.type(), b.type()) << "FloorDivide's inputs type not equal, where a:" << a.type() << " but b:" << b.type();
return a.type().is_float() ? Floor(a / b) : a / b;
if (a.type().is_float()) {
return Floor(a / b);
} else if (a.type().is_uint()) {
return a / b;
} else {
auto div = a / b;
auto mod = a % b;
auto ret = ir::Select::Make(
ir::EQ::Make(mod, common::make_const(a.type(), 0)), div, div - common::make_const(a.type(), 1));
return ir::Select::Make((a > 0 && b > 0) || (a < 0 && b < 0), div, ret);
}
}

Expr min_value(const Type& type) {
Expand Down
1 change: 0 additions & 1 deletion cinn/runtime/cuda/cuda_intrinsics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ CINN_REGISTER_HELPER(cuda_intrinsics) {
REGISTER_EXTERN_FUNC_2_IN_1_INT32(bitwise_and)
REGISTER_EXTERN_FUNC_2_IN_1_INT32(bitwise_or)
REGISTER_EXTERN_FUNC_2_IN_1_INT32(bitwise_xor)
REGISTER_EXTERN_FUNC_2_IN_1_INT32(floor_divide)
REGISTER_EXTERN_FUNC_2_IN_1_INT32(logical_right_shift)
REGISTER_EXTERN_FUNC_2_IN_1_INT32(mod)

Expand Down
2 changes: 1 addition & 1 deletion cmake/external/jitify.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ExternalProject_Add(
external_jitify
${EXTERNAL_PROJECT_LOG_ARGS}
GIT_REPOSITORY "https://github.com/NVIDIA/jitify.git"
GIT_TAG master
GIT_TAG 57de649139c866eb83acacfe50c92ad7c6278776
PREFIX ${THIRD_PARTY_PATH}/jitify
SOURCE_DIR ${JITIFY_SOURCE_PATH}
CONFIGURE_COMMAND ""
Expand Down
182 changes: 57 additions & 125 deletions python/tests/ops/test_floor_divide_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ def init_case(self):
self.x_np = self.random(
shape=self.case["x_shape"],
dtype=self.case["x_dtype"],
low=-10,
high=10)
low=self.case["x_low"],
high=self.case["x_high"])
self.y_np = self.random(
shape=self.case["y_shape"],
dtype=self.case["y_dtype"],
low=1,
high=10)
low=self.case["y_low"],
high=self.case["y_high"])

def build_paddle_program(self, target):
x = paddle.to_tensor(self.x_np, stop_gradient=True)
Expand All @@ -66,15 +66,15 @@ def build_cinn_program(self, target):
res = self.get_cinn_output(prog, target, [x, y],
[self.x_np, self.y_np], [out])

self.cinn_outputs = [res[0]]
self.cinn_outputs = res

def test_check_results(self):
max_relative_error = self.case[
"max_relative_error"] if "max_relative_error" in self.case else 1e-5
self.check_outputs_and_grads(max_relative_error=max_relative_error)


class TestFloorDivideAll(TestCaseHelper):
class TestFloorDivideShape(TestCaseHelper):
def init_attrs(self):
self.class_name = "TestFloorDivideOpCase"
self.cls = TestFloorDivideOp
Expand Down Expand Up @@ -109,18 +109,26 @@ def init_attrs(self):
"x_dtype": "int32",
"y_dtype": "int32",
},
]
self.attrs = [
{
"x_dtype": "int64",
"y_dtype": "int64",
"x_low": -10,
"x_high": 10,
"y_low": -10,
"y_high": -1,
},
{
"x_low": -10,
"x_high": 10,
"y_low": 1,
"y_high": 10,
},
]
self.attrs = []


class TestFloorDivideAllWithBroadcast(TestCaseHelper):
class TestFloorDivideBroadcast(TestFloorDivideShape):
def init_attrs(self):
self.class_name = "TestFloorDivideOpCase"
self.cls = TestFloorDivideOp
super().init_attrs()
self.inputs = [
{
"x_shape": [1],
Expand All @@ -147,97 +155,26 @@ def init_attrs(self):
"y_shape": [1, 1, 1, 1, 1],
},
]
self.dtypes = [
{
"x_dtype": "int32",
"y_dtype": "int32",
},
{
"x_dtype": "int64",
"y_dtype": "int64",
},
]
self.attrs = []


class TestFloorDivideNegOp(OpTest):
def setUp(self):
print(f"\nRunning {self.__class__.__name__}: {self.case}")
self.init_case()

def init_case(self):
self.x_np = self.random(
shape=self.case["x_shape"],
dtype=self.case["x_dtype"],
low=-10,
high=10)
self.y_np = self.random(
shape=self.case["y_shape"],
dtype=self.case["y_dtype"],
low=-10,
high=-1)

def build_paddle_program(self, target):
x = paddle.to_tensor(self.x_np, stop_gradient=True)
y = paddle.to_tensor(self.y_np, stop_gradient=True)

out = paddle.floor_divide(x, y)

self.paddle_outputs = [out]

def build_cinn_program(self, target):
builder = NetBuilder("pow")
x = builder.create_input(
self.nptype2cinntype(self.case["x_dtype"]), self.case["x_shape"],
"x")
y = builder.create_input(
self.nptype2cinntype(self.case["y_dtype"]), self.case["y_shape"],
"y")
out = builder.floor_divide(x, y)

prog = builder.build()
res = self.get_cinn_output(prog, target, [x, y],
[self.x_np, self.y_np], [out])

self.cinn_outputs = [res[0]]

def test_check_results(self):
max_relative_error = self.case[
"max_relative_error"] if "max_relative_error" in self.case else 1e-5
self.check_outputs_and_grads(max_relative_error=max_relative_error)


class TestFloorDivideNegAll(TestCaseHelper):
class TestFloorDivideDtype(TestFloorDivideShape):
def init_attrs(self):
self.class_name = "TestFloorDivideNegOpCase"
self.cls = TestFloorDivideNegOp
super().init_attrs()
self.inputs = [
{
"x_shape": [1],
"y_shape": [1],
},
{
"x_shape": [1024],
"y_shape": [1024],
},
]
self.dtypes = [
{
"x_shape": [512, 256],
"y_shape": [512, 256],
},
{
"x_shape": [128, 64, 32],
"y_shape": [128, 64, 32],
},
{
"x_shape": [16, 8, 4, 2],
"y_shape": [16, 8, 4, 2],
"x_dtype": "int8",
"y_dtype": "int8",
},
{
"x_shape": [16, 8, 4, 2, 1],
"y_shape": [16, 8, 4, 2, 1],
"x_dtype": "int16",
"y_dtype": "int16",
},
]
self.dtypes = [
{
"x_dtype": "int32",
"y_dtype": "int32",
Expand All @@ -246,55 +183,50 @@ def init_attrs(self):
"x_dtype": "int64",
"y_dtype": "int64",
},
]
self.attrs = []


class TestFloorDivideNegAllWithBroadcast(TestCaseHelper):
def init_attrs(self):
self.class_name = "TestFloorDivideNegOpCase"
self.cls = TestFloorDivideNegOp
self.inputs = [
{
"x_shape": [1],
"y_shape": [1],
},
{
"x_shape": [1024],
"y_shape": [1],
},
{
"x_shape": [512, 256],
"y_shape": [1, 1],
"x_dtype": "float16",
"y_dtype": "float16",
"max_relative_error": 1,
},
{
"x_shape": [128, 64, 32],
"y_shape": [1, 1, 1],
"x_dtype": "float32",
"y_dtype": "float32",
},
{
"x_shape": [16, 8, 4, 2],
"y_shape": [1, 1, 1, 1],
"x_dtype": "float64",
"y_dtype": "float64",
},
]


class TestFloorDivideUINT(TestCaseHelper):
def init_attrs(self):
self.class_name = "TestFloorDivideOpCase"
self.cls = TestFloorDivideOp
self.inputs = [
{
"x_shape": [16, 8, 4, 2, 1],
"y_shape": [1, 1, 1, 1, 1],
"x_shape": [1024],
"y_shape": [1024],
},
]
self.dtypes = [
{
"x_dtype": "int32",
"y_dtype": "int32",
"x_dtype": "uint8",
"y_dtype": "uint8",
},
]
self.attrs = [
{
"x_dtype": "int64",
"y_dtype": "int64",
"x_low": 1,
"x_high": 10,
"y_low": 1,
"y_high": 10,
},
]
self.attrs = []


if __name__ == "__main__":
TestFloorDivideAll().run()
TestFloorDivideNegAll().run()
TestFloorDivideAllWithBroadcast().run()
TestFloorDivideNegAllWithBroadcast().run()
TestFloorDivideShape().run()
TestFloorDivideBroadcast().run()
TestFloorDivideDtype().run()
TestFloorDivideUINT().run()
4 changes: 2 additions & 2 deletions python/tests/ops/test_sign_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def init_attrs(self):
"shape": [80, 1, 5, 7],
},
{
"shape": [80, 3, 1024, 7],
"shape": [80, 3, 32, 7],
},
{
"shape": [10, 5, 1024, 2048],
"shape": [10, 5, 32, 32],
},
{
"shape": [1],
Expand Down

0 comments on commit 725ddd5

Please sign in to comment.