Skip to content
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

[Tripy] Do not implicitly convert external input formats in convert_inputs_to_tensors decorator #185

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
be8e4de
Do not perform implicit conversions of external datatypes in convert_…
slyubomirsky Sep 5, 2024
b112332
Fix doc comment
slyubomirsky Sep 5, 2024
8191d54
Add checking for unions, sequences, and tensor literals in the functi…
slyubomirsky Sep 8, 2024
2642aa0
Convert non-Tripy tensors in Parameter
slyubomirsky Sep 8, 2024
efe49b8
Simplify error handling, fix spurious test cases
slyubomirsky Sep 10, 2024
531845f
Improve type signature for Parameter
slyubomirsky Sep 10, 2024
bba9828
Remove unnecessary imports
slyubomirsky Sep 10, 2024
0893c7a
Document TensorLiteral separately from other categories.
slyubomirsky Sep 10, 2024
ac22dd2
Support ForwardRef and remove WAR for TensorLiterals
slyubomirsky Sep 10, 2024
bc6ff80
Fix broken references
slyubomirsky Sep 10, 2024
1058252
Fix incorrect types in tp.ones in test case
slyubomirsky Sep 10, 2024
16f749f
Use numbers.Number for checking numerical types
slyubomirsky Sep 10, 2024
2de0d88
Keep test_reason_context the same
slyubomirsky Sep 10, 2024
0443d53
Remove redundant argument types line from function registry error
slyubomirsky Sep 10, 2024
fc006cd
Remove redundant tests in test_functional
slyubomirsky Sep 10, 2024
f9242c6
Do further validation on sequence arguments
slyubomirsky Sep 10, 2024
7c32ce9
Also validate seq inputs in the function registry
slyubomirsky Sep 11, 2024
9a389f5
Cannot assume ordering in list
slyubomirsky Sep 11, 2024
9b1df32
Move registry for typing
slyubomirsky Sep 11, 2024
f7a56d8
Change autodoc options for types
slyubomirsky Sep 11, 2024
bfc7c04
Add docstring to new types
slyubomirsky Sep 11, 2024
2b15fe6
Improve error messages for tp.Shape
slyubomirsky Sep 11, 2024
5ccebdb
Change name from TensorLiteral to NumberArray, add shorthand for Unio…
slyubomirsky Sep 11, 2024
ae4a04d
Tweak names for types
slyubomirsky Sep 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion tripy/examples/nanogpt/weight_loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
Expand Down
4 changes: 2 additions & 2 deletions tripy/tests/backend/mlir/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def test_reason_context(self):
with FlatIRTensor.context(["This is the first level of context"]):
with FlatIRTensor.context(["This is the second level of context"]):
# We need to emit an error from one of the internally created `FlatIRTensor`s to see the context
a = tp.ones(1)
b = tp.ones(1)
slyubomirsky marked this conversation as resolved.
Show resolved Hide resolved
a = tp.ones((1,))
b = tp.ones((1,))
trace = Trace([a + b])
flat_ir = trace.to_flat_ir()
producer = flat_ir.outputs[0].producer.inputs[0]
Expand Down
29 changes: 20 additions & 9 deletions tripy/tests/frontend/test_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ def values(request):


@pytest.fixture(
params=[[4, 5], tp.Tensor([4, 5], dtype=tp.int32), np.array([4, 5], dtype=np.int32)],
params=[[4, 5], tp.Tensor([4, 5], dtype=tp.int32)],
ids=[
"python_list",
"tripy_tensor",
"numpy_array",
],
)
def other_values(request):
Expand Down Expand Up @@ -95,7 +94,7 @@ def test_plus_override(self, values, other_values):
appended = [4, 5]
s = tp.Shape(values)

# conversion is implicit except for tp.Tensor
# conversion must be explicit for tp.Tensor
lhs_shape = other_values if not isinstance(other_values, tp.Tensor) else tp.Shape(other_values)
new_shape = s + lhs_shape
assert isinstance(new_shape, tp.Shape)
Expand Down Expand Up @@ -308,7 +307,7 @@ def test_right_addition(self, other_values):
appended = [4, 5]
s = tp.Shape(values)

# conversion is implicit except for tp.Tensor
# conversion must be explicit for tp.Tensor
rhs_shape = other_values if not isinstance(other_values, tp.Tensor) else tp.Shape(other_values)

new_shape = rhs_shape + s
Expand Down Expand Up @@ -418,19 +417,29 @@ def test_invalid_input_rank_tensor(self):
with raises(tp.TripyException, match="Shape tensors must be of rank 1, but input tensor is rank 2"):
_ = tp.Shape(tp.ones((3, 2), dtype=tp.int32))

def test_invalid_mul_sequence(self, values):
s = tp.Shape(values)
with raises(tp.TripyException, match="Attempting to multiply a Tripy Shape by a sequence, which is undefined"):
_ = s * values

def test_invalid_mul_rank(self, values):
s = tp.Shape(values)
t = tp.Tensor(values)
with raises(
tp.TripyException, match="Attempting to multiply a Tripy Shape by a tensor of rank >= 1, which is undefined"
):
_ = s * values
_ = s * t

def test_invalid_plus_type(self, values):
s = tp.Shape(values)
t = tp.Tensor(values, dtype=tp.int32)
with raises(
tp.TripyException,
match="Attempting to add a Tripy Tensor to a Tripy Shape, which is not allowed. Consider calling tp.Shape explicitly",
match=(
"Invalid types for addition with a Tripy Shape."
r"\s*Implicit conversions are done only for sequences of Python ints. "
"Consider calling tp.Shape for an explicit conversion."
),
):
s + t

Expand All @@ -439,7 +448,11 @@ def test_invalid_right_addition_type(self, values):
t = tp.Tensor(values, dtype=tp.int32)
with raises(
tp.TripyException,
match="Attempting to add a Tripy Tensor to a Tripy Shape, which is not allowed. Consider calling tp.Shape explicitly",
match=(
"Invalid types for addition with a Tripy Shape."
r"\s*Implicit conversions are done only for sequences of Python ints. "
"Consider calling tp.Shape for an explicit conversion."
),
):
t + s

Expand Down Expand Up @@ -469,8 +482,6 @@ def test_unary_elementwise_fails_at_run_time(self, values):

def test_shape_equality(self, other_values):
a = tp.Shape([4, 5])
if isinstance(other_values, np.ndarray):
pytest.skip("numpy array cannot be implicitly cast to Shape type")
eq = a == other_values
assert isinstance(eq, bool)
assert eq
Expand Down
27 changes: 27 additions & 0 deletions tripy/tests/frontend/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#

import cupy as cp
import numpy as np

import tripy as tp
from tripy.frontend.utils import convert_inputs_to_tensors
Expand Down Expand Up @@ -291,3 +292,29 @@ def test_sync_arg_type_invalid(self):
match=r"At least one of the arguments: \('a', 'b', 'c'\) must be a \`tripy.Tensor\`.",
):
x, y, z = sync_arg_types(3.0, 3, 4)

def test_seq_arg_invalid(self):
with helper.raises(
tp.TripyException,
match=r"Encountered non-number of type str in sequence: hello",
):
_ = func([1, 2, "hello"])

def test_nested_seq_inconsistent_len(self):
with helper.raises(
tp.TripyException,
match=r"Expected a sequence of length 3 but got length 4: \[7, 8, 9, 10\]",
):
_ = func([[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]])

def test_nested_seq_inconsistent_types(self):
with helper.raises(
tp.TripyException,
match=r"Expected a sequence but got str: hi",
):
_ = func([[1, 2, 3], [4, 5, 6], "hi"])

def test_invalid_argument_type_not_converted(self):
a = np.array([1, 2, 3])
b = func(np.array([1, 2, 3]))
assert (a == b).all()
4 changes: 0 additions & 4 deletions tripy/tests/frontend/trace/ops/test_binary_elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ class TestBinaryElementwise:
"lhs, rhs, left_side_is_non_tensor",
[
(tp.Tensor([1.0]), tp.Tensor([2.0]), False),
(tp.Tensor([1.0]), np.array([2.0], dtype=np.float32), False),
# shape of (0,) is broadcastable with (1,)
(tp.Tensor([], dtype=tp.float32), tp.Tensor([1.0], dtype=tp.float32), False),
(np.array([1.0], dtype=np.float32), tp.Tensor([2.0]), True),
(tp.Tensor([1.0]), 2.0, False),
(1.0, tp.Tensor([2.0]), True),
],
Expand All @@ -86,8 +84,6 @@ def test_op_funcs(self, kind, func, lhs, rhs, left_side_is_non_tensor):
"lhs, rhs, expected_rank",
[
(tp.Tensor([1.0]), tp.Tensor([2.0]), 1),
(tp.Tensor([1.0]), np.array([2.0], dtype=np.float32), 1),
(np.array([1.0], dtype=np.float32), tp.Tensor([2.0]), 1),
(tp.Tensor([1.0]), 2.0, 1),
(1.0, tp.Tensor([2.0]), 1),
(tp.ones((2, 3)), 2.0, 2),
Expand Down
33 changes: 0 additions & 33 deletions tripy/tests/integration/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,36 +130,3 @@ def test_multiple_copy_2(self):
out = tp.copy(a, tp.device("gpu"))
assert out.tolist() == [1, 2]
assert out.device.kind == "gpu"


class TestConversionToTripyType:
@pytest.mark.parametrize(
"reverse_direction",
[False, True],
)
@pytest.mark.parametrize(
"input0",
[cp.ones((2, 3), dtype=cp.float32), cp.ones((3,), dtype=np.float32)],
)
@pytest.mark.parametrize(
"input1",
[
[
4.0,
],
(5.0,),
cp.array([4.0], dtype=cp.float32),
cp.ones((1, 3), dtype=cp.float32),
torch.Tensor([[4.0]]),
],
)
def test_element_wise_prod(self, reverse_direction, input0, input1):
a = tp.Tensor(input0)
if isinstance(input1, torch.Tensor):
input1 = input1.to("cuda")
if reverse_direction:
out = input1 * a
input0, input1 = input1, input0
else:
out = a * input1
assert cp.array_equal(cp.from_dlpack(out), cp.array(input0) * cp.array(input1))
Loading