|
| 1 | +# |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# 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, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +import cupy as cp |
| 18 | +import pytest |
| 19 | +from tests import helper |
| 20 | +from tests.backend.api.conftest import * |
| 21 | + |
| 22 | +import tripy as tp |
| 23 | + |
| 24 | + |
| 25 | +class TestCompile: |
| 26 | + # TODO (#246): Verify that it's actually compiling somehow here and below. |
| 27 | + # Need to return something programatically queriable from compile to do this. |
| 28 | + def test_function(self): |
| 29 | + compiled_gelu = tp.compile(tp.relu, args=[tp.InputInfo((2, 2), dtype=tp.float32)]) |
| 30 | + |
| 31 | + inp = tp.ones((2, 2), dtype=tp.float32) |
| 32 | + out = compiled_gelu(inp) |
| 33 | + |
| 34 | + # TODO (#225): Replace with tp.all |
| 35 | + assert cp.array_equal(cp.from_dlpack(out), cp.from_dlpack(tp.relu(inp))) |
| 36 | + |
| 37 | + def test_module(self): |
| 38 | + layernorm = tp.LayerNorm(2) |
| 39 | + compiled_layernorm = tp.compile(layernorm, args=[tp.InputInfo((2, 2), dtype=tp.float32)]) |
| 40 | + |
| 41 | + inp = tp.ones((2, 2), dtype=tp.float32) |
| 42 | + out = compiled_layernorm(inp) |
| 43 | + |
| 44 | + assert cp.array_equal(cp.from_dlpack(out), cp.from_dlpack(layernorm(inp))) |
| 45 | + |
| 46 | + def test_compile_arg_order_irrelevant(self): |
| 47 | + # The order of arguments we specify to `compile` should not affect the order |
| 48 | + # of the arguments in the compiled function, which should just follow the order |
| 49 | + # of the original function. |
| 50 | + compiled_sub = tp.compile( |
| 51 | + sub, kwargs=dict(b=tp.InputInfo((2, 2), dtype=tp.float32), a=tp.InputInfo((2, 2), dtype=tp.float32)) |
| 52 | + ) |
| 53 | + |
| 54 | + a = tp.ones((2, 2), dtype=tp.float32) * 2 |
| 55 | + b = tp.ones((2, 2), dtype=tp.float32) |
| 56 | + |
| 57 | + # Compiled function should still take arguments in (a, b) order. |
| 58 | + out = compiled_sub(a, b) |
| 59 | + assert cp.array_equal(cp.from_dlpack(out), cp.ones((2, 2), dtype=cp.float32)) |
| 60 | + |
| 61 | + @pytest.mark.parametrize("b", [2, tp.ones((2, 2), dtype=tp.float32) * 2]) |
| 62 | + def test_constants_baked(self, b): |
| 63 | + # Any non-InputInfo argument to compile is baked into the compiled function. |
| 64 | + compiled_add = tp.compile(add, args=[tp.InputInfo((2, 2), dtype=tp.float32), b]) |
| 65 | + |
| 66 | + a = tp.zeros((2, 2), dtype=tp.float32) |
| 67 | + |
| 68 | + out = compiled_add(a) |
| 69 | + |
| 70 | + assert cp.array_equal(cp.from_dlpack(out), cp.ones((2, 2), dtype=cp.float32) * 2) |
| 71 | + |
| 72 | + @pytest.mark.parametrize("func", [variadic_positional, variadic_keyword]) |
| 73 | + def test_variadic_arguments_rejected(self, func): |
| 74 | + with helper.raises(tp.TripyException, "Variadic positional/keyword arguments are not currently supported."): |
| 75 | + tp.compile(func) |
| 76 | + |
| 77 | + @pytest.mark.parametrize("func", [returns_non_tensor, returns_nothing]) |
| 78 | + def test_invalid_return_rejected(self, func): |
| 79 | + with helper.raises(tp.TripyException, "Function must return 1 or more Tensors"): |
| 80 | + tp.compile(func, args=[tp.InputInfo((2, 2), dtype=tp.float32)]) |
| 81 | + |
| 82 | + def test_multiple_return_values(self): |
| 83 | + compiled_func = tp.compile( |
| 84 | + returns_multiple_tensors, |
| 85 | + args=[tp.InputInfo((2, 2), dtype=tp.float32), tp.InputInfo((2, 2), dtype=tp.float32)], |
| 86 | + ) |
| 87 | + |
| 88 | + a = tp.ones((2, 2), dtype=tp.float32) * 2 |
| 89 | + b = tp.ones((2, 2), dtype=tp.float32) |
| 90 | + |
| 91 | + plus, minus = compiled_func(a, b) |
| 92 | + |
| 93 | + assert cp.array_equal(cp.from_dlpack(plus), cp.ones((2, 2), dtype=cp.float32) * 3) |
| 94 | + assert cp.array_equal(cp.from_dlpack(minus), cp.ones((2, 2), dtype=cp.float32)) |
| 95 | + |
| 96 | + def test_incorrect_dtype_rejected(self): |
| 97 | + a = tp.ones((2, 2), dtype=tp.int32) |
| 98 | + |
| 99 | + with helper.raises(tp.TripyException, "Unexpected tensor data type.", has_stack_info_for=[a]): |
| 100 | + compiled_add = tp.compile( |
| 101 | + add, args=[tp.InputInfo((2, 2), dtype=tp.float32), tp.InputInfo((2, 2), dtype=tp.float32)] |
| 102 | + ) |
| 103 | + compiled_add(a, a) |
| 104 | + |
| 105 | + def test_incorrect_shape_rejected(self): |
| 106 | + a = tp.ones((1, 2), dtype=tp.float32) |
| 107 | + |
| 108 | + with helper.raises(tp.TripyException, "Unexpected tensor shape.", has_stack_info_for=[a]): |
| 109 | + compiled_add = tp.compile( |
| 110 | + add, args=[tp.InputInfo((2, 2), dtype=tp.float32), tp.InputInfo((2, 2), dtype=tp.float32)] |
| 111 | + ) |
| 112 | + compiled_add(a, a) |
| 113 | + |
| 114 | + @pytest.mark.skip("TODO (#155): Re-enable once we no longer implicitly copy inputs to device") |
| 115 | + def test_incorrect_device_rejected(self): |
| 116 | + compiled_add = tp.compile( |
| 117 | + add, args=[tp.InputInfo((2, 2), dtype=tp.float32), tp.InputInfo((2, 2), dtype=tp.float32)] |
| 118 | + ) |
| 119 | + a = tp.copy(tp.ones((2, 2), dtype=tp.float32), device=tp.device("cpu")) |
| 120 | + |
| 121 | + with helper.raises(tp.TripyException): |
| 122 | + compiled_add(a, a) |
| 123 | + |
| 124 | + # TODO (#244): Add multi-profile test |
| 125 | + def test_dynamic_shapes(self): |
| 126 | + compiled_add = tp.compile( |
| 127 | + add, args=[tp.InputInfo(((1, 2, 3), 1), dtype=tp.float32), tp.InputInfo(((1, 2, 3), 1), dtype=tp.float32)] |
| 128 | + ) |
| 129 | + |
| 130 | + out = compiled_add(tp.ones((2, 1), dtype=tp.float32), tp.ones((2, 1), dtype=tp.float32)) |
| 131 | + assert cp.array_equal(cp.from_dlpack(out), cp.ones((2, 1), dtype=cp.float32) * 2) |
| 132 | + |
| 133 | + out = compiled_add(tp.ones((3, 1), dtype=tp.float32), tp.ones((3, 1), dtype=tp.float32)) |
| 134 | + assert cp.array_equal(cp.from_dlpack(out), cp.ones((3, 1), dtype=cp.float32) * 2) |
| 135 | + |
| 136 | + |
| 137 | +# TODO (#256): Remove these tests and replace with exhaustive integration testing |
| 138 | +class TestCompiledOps: |
| 139 | + def test_cast(self): |
| 140 | + compiled_cast = tp.compile(tp.cast, args=[tp.InputInfo((2, 2), dtype=tp.float32)], kwargs=dict(dtype=tp.int32)) |
| 141 | + |
| 142 | + a = tp.ones((2, 2), dtype=tp.float32) |
| 143 | + out = compiled_cast(a) |
| 144 | + |
| 145 | + assert cp.array_equal(cp.from_dlpack(out), cp.ones((2, 2), dtype=cp.int32)) |
| 146 | + |
| 147 | + def test_linear(self): |
| 148 | + linear = tp.Linear(2, 3) |
| 149 | + |
| 150 | + compiled_linear = tp.compile(linear, args=[tp.InputInfo((2, 2), dtype=tp.float32)]) |
| 151 | + |
| 152 | + a = tp.ones((2, 2), dtype=tp.float32) |
| 153 | + |
| 154 | + out = compiled_linear(a) |
| 155 | + |
| 156 | + assert cp.array_equal(cp.from_dlpack(out), cp.from_dlpack(linear(a))) |
0 commit comments