|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +"""Test script for boolean tensor support""" |
| 20 | +import tempfile |
| 21 | + |
| 22 | +import torch |
| 23 | + |
| 24 | +import tvm |
| 25 | +import tvm.testing |
| 26 | +from tvm.contrib.torch import as_torch, optimize_torch |
| 27 | +from tvm.script import tir as T |
| 28 | + |
| 29 | + |
| 30 | +def negate(x): |
| 31 | + return x.logical_not() |
| 32 | + |
| 33 | + |
| 34 | +def sum_up_tensor(x): |
| 35 | + return x.size(dim=0) - torch.sum(x.int()) |
| 36 | + |
| 37 | + |
| 38 | +def tensor_boolean_operation(x): |
| 39 | + arr1 = (x + 0.3).floor().bool() |
| 40 | + arr2 = (~((x + 0.7).int().bool())).bool() |
| 41 | + ret = ((arr1 & arr2).byte() + 0.5).half() |
| 42 | + return ~(ret.bool()) |
| 43 | + |
| 44 | + |
| 45 | +def test_bool_tensor_negate(): |
| 46 | + input = torch.ones(1, dtype=torch.bool) |
| 47 | + optimized_negate = optimize_torch( |
| 48 | + negate, |
| 49 | + input, |
| 50 | + ) |
| 51 | + with tempfile.NamedTemporaryFile(suffix=".pt") as tmp: |
| 52 | + torch.save(optimized_negate, tmp.name) |
| 53 | + loaded_mod = torch.load(tmp.name) |
| 54 | + output = loaded_mod(negate(input)) |
| 55 | + tvm.testing.assert_allclose(input.numpy(), output.numpy(), atol=1e-5, rtol=1e-5) |
| 56 | + |
| 57 | + |
| 58 | +def test_sum_up_tensor(): |
| 59 | + x = torch.randint(0, 2, (16,)) |
| 60 | + y = x.bool() |
| 61 | + optimized_func = optimize_torch( |
| 62 | + sum_up_tensor, |
| 63 | + (y,), |
| 64 | + ) |
| 65 | + ret1 = (x[x == 0]).size(dim=0) |
| 66 | + ret2 = optimized_func(y).numpy() |
| 67 | + tvm.testing.assert_allclose(ret1, ret2, atol=1e-5, rtol=1e-5) |
| 68 | + |
| 69 | + |
| 70 | +def test_tensor_boolean_operation(): |
| 71 | + input = torch.rand(200) |
| 72 | + model = optimize_torch( |
| 73 | + tensor_boolean_operation, |
| 74 | + input, |
| 75 | + ) |
| 76 | + ret1 = tensor_boolean_operation(input) |
| 77 | + ret2 = model(input) |
| 78 | + tvm.testing.assert_allclose(ret1, ret2, atol=1e-5, rtol=1e-5) |
| 79 | + |
| 80 | + |
| 81 | +@as_torch |
| 82 | +@T.prim_func |
| 83 | +def negate_tvmscript( |
| 84 | + X: T.Buffer[(8, 8), "bool"], |
| 85 | + Y: T.Buffer[(8, 8), "float32"], |
| 86 | + Z: T.Buffer[(8, 8), "bool"], |
| 87 | + U: T.Buffer[(8, 8), "float32"], |
| 88 | +) -> None: |
| 89 | + for i, j in T.grid(8, 8): |
| 90 | + with T.block(): |
| 91 | + if Y[i, j] > 0.0: |
| 92 | + Z[i, j] = X[i, j] |
| 93 | + U[i, j] = Y[i, j] |
| 94 | + else: |
| 95 | + Z[i, j] = not X[i, j] |
| 96 | + U[i, j] = 0.0 - Y[i, j] |
| 97 | + |
| 98 | + |
| 99 | +def negate_vanila(x, y): |
| 100 | + z = torch.zeros(8, 8).bool() |
| 101 | + for i in range(8): |
| 102 | + for j in range(8): |
| 103 | + if y[i, j] > 0: |
| 104 | + z[i, j] = x[i, j] |
| 105 | + else: |
| 106 | + z[i, j] = ~x[i, j] |
| 107 | + return z |
| 108 | + |
| 109 | + |
| 110 | +def test_tvmscript_torch_decorator(): |
| 111 | + q1 = (torch.rand(8, 8) + 0.5).int().bool() |
| 112 | + q2 = torch.rand(8, 8) - 0.5 |
| 113 | + q3 = torch.zeros(8, 8).bool() |
| 114 | + q4 = torch.zeros(8, 8) |
| 115 | + |
| 116 | + std1 = negate_vanila(q1, q2) |
| 117 | + std2 = torch.abs(q2) |
| 118 | + |
| 119 | + negate_tvmscript(q1, q2, q3, q4) |
| 120 | + |
| 121 | + tvm.testing.assert_allclose(std1.numpy(), q3.numpy(), atol=1e-5, rtol=1e-5) |
| 122 | + tvm.testing.assert_allclose(std2.numpy(), q4.numpy(), atol=1e-5, rtol=1e-5) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + test_tvmscript_torch_decorator() |
| 127 | + test_bool_tensor_negate() |
| 128 | + test_sum_up_tensor() |
| 129 | + test_tensor_boolean_operation() |
0 commit comments