|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +import tvm |
| 21 | +import tvm.testing |
| 22 | +from tvm import relax |
| 23 | +from tvm.script import relax as R |
| 24 | + |
| 25 | + |
| 26 | +def test_op_size(): |
| 27 | + @tvm.script.ir_module |
| 28 | + class Module: |
| 29 | + @R.function |
| 30 | + def main(x: R.Tensor((2, 3), "float32")) -> R.Tensor((), "int64"): |
| 31 | + return R.size(x) |
| 32 | + |
| 33 | + x_np = np.random.rand(2, 3).astype("float32") |
| 34 | + x = tvm.runtime.tensor(x_np) |
| 35 | + |
| 36 | + target = tvm.target.Target("llvm") |
| 37 | + ex = relax.build(Module, target) |
| 38 | + vm = relax.VirtualMachine(ex, tvm.cpu()) |
| 39 | + |
| 40 | + res = vm["main"](x) |
| 41 | + assert res.numpy() == 6 |
| 42 | + |
| 43 | + |
| 44 | +def test_op_size_dynamic(): |
| 45 | + @tvm.script.ir_module |
| 46 | + class Module: |
| 47 | + @R.function |
| 48 | + def main(x: R.Tensor(("m", "n"), "float32")) -> R.Tensor((), "int64"): |
| 49 | + return R.size(x) |
| 50 | + |
| 51 | + x_np = np.random.rand(4, 5).astype("float32") |
| 52 | + x = tvm.runtime.tensor(x_np) |
| 53 | + |
| 54 | + target = tvm.target.Target("llvm") |
| 55 | + ex = relax.build(Module, target) |
| 56 | + vm = relax.VirtualMachine(ex, tvm.cpu()) |
| 57 | + |
| 58 | + res = vm["main"](x) |
| 59 | + assert res.numpy() == 20 |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + tvm.testing.main() |
0 commit comments