aboutsummaryrefslogtreecommitdiff
path: root/tests/test_eigen_tensor.py
blob: 3e7ee6b7f29fb6949ea426e6ad5e8fad52c89e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import sys

import pytest

np = pytest.importorskip("numpy")
eigen_tensor = pytest.importorskip("pybind11_tests.eigen_tensor")
submodules = [eigen_tensor.c_style, eigen_tensor.f_style]
try:
    import eigen_tensor_avoid_stl_array as avoid

    submodules += [avoid.c_style, avoid.f_style]
except ImportError as e:
    # Ensure config, build, toolchain, etc. issues are not masked here:
    msg = (
        "import eigen_tensor_avoid_stl_array FAILED, while "
        "import pybind11_tests.eigen_tensor succeeded. "
        "Please ensure that "
        "test_eigen_tensor.cpp & "
        "eigen_tensor_avoid_stl_array.cpp "
        "are built together (or both are not built if Eigen is not available)."
    )
    raise RuntimeError(msg) from e

tensor_ref = np.empty((3, 5, 2), dtype=np.int64)

for i in range(tensor_ref.shape[0]):
    for j in range(tensor_ref.shape[1]):
        for k in range(tensor_ref.shape[2]):
            tensor_ref[i, j, k] = i * (5 * 2) + j * 2 + k

indices = (2, 3, 1)


@pytest.fixture(autouse=True)
def cleanup():
    for module in submodules:
        module.setup()

    yield

    for module in submodules:
        assert module.is_ok()


def test_import_avoid_stl_array():
    pytest.importorskip("eigen_tensor_avoid_stl_array")
    assert len(submodules) == 4


def assert_equal_tensor_ref(mat, writeable=True, modified=None):
    assert mat.flags.writeable == writeable

    copy = np.array(tensor_ref)
    if modified is not None:
        copy[indices] = modified

    np.testing.assert_array_equal(mat, copy)


@pytest.mark.parametrize("m", submodules)
@pytest.mark.parametrize("member_name", ["member", "member_view"])
def test_reference_internal(m, member_name):
    if not hasattr(sys, "getrefcount"):
        pytest.skip("No reference counting")
    foo = m.CustomExample()
    counts = sys.getrefcount(foo)
    mem = getattr(foo, member_name)
    assert_equal_tensor_ref(mem, writeable=False)
    new_counts = sys.getrefcount(foo)
    assert new_counts == counts + 1
    assert_equal_tensor_ref(mem, writeable=False)
    del mem
    assert sys.getrefcount(foo) == counts


assert_equal_funcs = [
    "copy_tensor",
    "copy_fixed_tensor",
    "copy_const_tensor",
    "move_tensor_copy",
    "move_fixed_tensor_copy",
    "take_tensor",
    "take_fixed_tensor",
    "reference_tensor",
    "reference_tensor_v2",
    "reference_fixed_tensor",
    "reference_view_of_tensor",
    "reference_view_of_tensor_v3",
    "reference_view_of_tensor_v5",
    "reference_view_of_fixed_tensor",
]

assert_equal_const_funcs = [
    "reference_view_of_tensor_v2",
    "reference_view_of_tensor_v4",
    "reference_view_of_tensor_v6",
    "reference_const_tensor",
    "reference_const_tensor_v2",
]


@pytest.mark.parametrize("m", submodules)
@pytest.mark.parametrize("func_name", assert_equal_funcs + assert_equal_const_funcs)
def test_convert_tensor_to_py(m, func_name):
    writeable = func_name in assert_equal_funcs
    assert_equal_tensor_ref(getattr(m, func_name)(), writeable=writeable)


@pytest.mark.parametrize("m", submodules)
def test_bad_cpp_to_python_casts(m):
    with pytest.raises(
        RuntimeError, match="Cannot use reference internal when there is no parent"
    ):
        m.reference_tensor_internal()

    with pytest.raises(RuntimeError, match="Cannot move from a constant reference"):
        m.move_const_tensor()

    with pytest.raises(
        RuntimeError, match="Cannot take ownership of a const reference"
    ):
        m.take_const_tensor()

    with pytest.raises(
        RuntimeError,
        match="Invalid return_value_policy for Eigen Map type, must be either reference or reference_internal",
    ):
        m.take_view_tensor()


@pytest.mark.parametrize("m", submodules)
def test_bad_python_to_cpp_casts(m):
    with pytest.raises(
        TypeError, match=r"^round_trip_tensor\(\): incompatible function arguments"
    ):
        m.round_trip_tensor(np.zeros((2, 3)))

    with pytest.raises(TypeError, match=r"^Cannot cast array data from dtype"):
        m.round_trip_tensor(np.zeros(dtype=np.str_, shape=(2, 3, 1)))

    with pytest.raises(
        TypeError,
        match=r"^round_trip_tensor_noconvert\(\): incompatible function arguments",
    ):
        m.round_trip_tensor_noconvert(tensor_ref)

    assert_equal_tensor_ref(
        m.round_trip_tensor_noconvert(tensor_ref.astype(np.float64))
    )

    bad_options = "C" if m.needed_options == "F" else "F"
    # Shape, dtype and the order need to be correct for a TensorMap cast
    with pytest.raises(
        TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments"
    ):
        m.round_trip_view_tensor(
            np.zeros((3, 5, 2), dtype=np.float64, order=bad_options)
        )

    with pytest.raises(
        TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments"
    ):
        m.round_trip_view_tensor(
            np.zeros((3, 5, 2), dtype=np.float32, order=m.needed_options)
        )

    with pytest.raises(
        TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments"
    ):
        m.round_trip_view_tensor(
            np.zeros((3, 5), dtype=np.float64, order=m.needed_options)
        )

    temp = np.zeros((3, 5, 2), dtype=np.float64, order=m.needed_options)
    with pytest.raises(
        TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments"
    ):
        m.round_trip_view_tensor(
            temp[:, ::-1, :],
        )

    temp = np.zeros((3, 5, 2), dtype=np.float64, order=m.needed_options)
    temp.setflags(write=False)
    with pytest.raises(
        TypeError, match=r"^round_trip_view_tensor\(\): incompatible function arguments"
    ):
        m.round_trip_view_tensor(temp)


@pytest.mark.parametrize("m", submodules)
def test_references_actually_refer(m):
    a = m.reference_tensor()
    temp = a[indices]
    a[indices] = 100
    assert_equal_tensor_ref(m.copy_const_tensor(), modified=100)
    a[indices] = temp
    assert_equal_tensor_ref(m.copy_const_tensor())

    a = m.reference_view_of_tensor()
    a[indices] = 100
    assert_equal_tensor_ref(m.copy_const_tensor(), modified=100)
    a[indices] = temp
    assert_equal_tensor_ref(m.copy_const_tensor())


@pytest.mark.parametrize("m", submodules)
def test_round_trip(m):
    assert_equal_tensor_ref(m.round_trip_tensor(tensor_ref))

    with pytest.raises(TypeError, match="^Cannot cast array data from"):
        assert_equal_tensor_ref(m.round_trip_tensor2(tensor_ref))

    assert_equal_tensor_ref(m.round_trip_tensor2(np.array(tensor_ref, dtype=np.int32)))
    assert_equal_tensor_ref(m.round_trip_fixed_tensor(tensor_ref))
    assert_equal_tensor_ref(m.round_trip_aligned_view_tensor(m.reference_tensor()))

    copy = np.array(tensor_ref, dtype=np.float64, order=m.needed_options)
    assert_equal_tensor_ref(m.round_trip_view_tensor(copy))
    assert_equal_tensor_ref(m.round_trip_view_tensor_ref(copy))
    assert_equal_tensor_ref(m.round_trip_view_tensor_ptr(copy))
    copy.setflags(write=False)
    assert_equal_tensor_ref(m.round_trip_const_view_tensor(copy))

    np.testing.assert_array_equal(
        tensor_ref[:, ::-1, :], m.round_trip_tensor(tensor_ref[:, ::-1, :])
    )

    assert m.round_trip_rank_0(np.float64(3.5)) == 3.5
    assert m.round_trip_rank_0(3.5) == 3.5

    with pytest.raises(
        TypeError,
        match=r"^round_trip_rank_0_noconvert\(\): incompatible function arguments",
    ):
        m.round_trip_rank_0_noconvert(np.float64(3.5))

    with pytest.raises(
        TypeError,
        match=r"^round_trip_rank_0_noconvert\(\): incompatible function arguments",
    ):
        m.round_trip_rank_0_noconvert(3.5)

    with pytest.raises(
        TypeError, match=r"^round_trip_rank_0_view\(\): incompatible function arguments"
    ):
        m.round_trip_rank_0_view(np.float64(3.5))

    with pytest.raises(
        TypeError, match=r"^round_trip_rank_0_view\(\): incompatible function arguments"
    ):
        m.round_trip_rank_0_view(3.5)


@pytest.mark.parametrize("m", submodules)
def test_round_trip_references_actually_refer(m):
    # Need to create a copy that matches the type on the C side
    copy = np.array(tensor_ref, dtype=np.float64, order=m.needed_options)
    a = m.round_trip_view_tensor(copy)
    temp = a[indices]
    a[indices] = 100
    assert_equal_tensor_ref(copy, modified=100)
    a[indices] = temp
    assert_equal_tensor_ref(copy)


@pytest.mark.parametrize("m", submodules)
def test_doc_string(m, doc):
    assert (
        doc(m.copy_tensor) == "copy_tensor() -> numpy.ndarray[numpy.float64[?, ?, ?]]"
    )
    assert (
        doc(m.copy_fixed_tensor)
        == "copy_fixed_tensor() -> numpy.ndarray[numpy.float64[3, 5, 2]]"
    )
    assert (
        doc(m.reference_const_tensor)
        == "reference_const_tensor() -> numpy.ndarray[numpy.float64[?, ?, ?]]"
    )

    order_flag = f"flags.{m.needed_options.lower()}_contiguous"
    assert doc(m.round_trip_view_tensor) == (
        f"round_trip_view_tensor(arg0: numpy.ndarray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}])"
        f" -> numpy.ndarray[numpy.float64[?, ?, ?], flags.writeable, {order_flag}]"
    )
    assert doc(m.round_trip_const_view_tensor) == (
        f"round_trip_const_view_tensor(arg0: numpy.ndarray[numpy.float64[?, ?, ?], {order_flag}])"
        " -> numpy.ndarray[numpy.float64[?, ?, ?]]"
    )