File: test_binary_ew.py

package info (click to toggle)
open3d 0.19.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,496 kB
  • sloc: cpp: 206,543; python: 27,254; ansic: 8,356; javascript: 1,883; sh: 1,527; makefile: 259; xml: 69
file content (93 lines) | stat: -rw-r--r-- 2,934 bytes parent folder | download | duplicates (2)
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
# ----------------------------------------------------------------------------
# -                        Open3D: www.open3d.org                            -
# ----------------------------------------------------------------------------
# Copyright (c) 2018-2024 www.open3d.org
# SPDX-License-Identifier: MIT
# ----------------------------------------------------------------------------

import open3d as o3d
import open3d.core as o3c
import numpy as np
import pytest
import operator

import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/..")
from open3d_benchmark import list_tensor_sizes, list_non_bool_dtypes, to_numpy_dtype


class BinaryEWOps:

    @staticmethod
    def logical_and(lhs, rhs):
        return lhs.logical_and(rhs)

    @staticmethod
    def logical_or(lhs, rhs):
        return lhs.logical_or(rhs)

    @staticmethod
    def logical_xor(lhs, rhs):
        return lhs.logical_xor(rhs)


def list_binary_ops():
    return [
        operator.add,
        operator.sub,
        operator.mul,
        operator.truediv,
        BinaryEWOps.logical_and,
        BinaryEWOps.logical_or,
        BinaryEWOps.logical_xor,
        operator.gt,
        operator.lt,
        operator.ge,
        operator.le,
        operator.eq,
        operator.ne,
    ]


def to_numpy_binary_op(op):
    conversions = {
        operator.add: operator.add,
        operator.sub: operator.sub,
        operator.mul: operator.mul,
        operator.truediv: operator.truediv,
        BinaryEWOps.logical_and: np.logical_and,
        BinaryEWOps.logical_or: np.logical_or,
        BinaryEWOps.logical_xor: np.logical_xor,
        operator.gt: operator.gt,
        operator.lt: operator.lt,
        operator.ge: operator.ge,
        operator.le: operator.le,
        operator.eq: operator.eq,
        operator.ne: operator.ne,
    }
    return conversions[op]


@pytest.mark.parametrize("size", list_tensor_sizes())
@pytest.mark.parametrize("dtype", list_non_bool_dtypes())
@pytest.mark.parametrize("op", list_binary_ops())
def test_binary_ew_ops(benchmark, size, dtype, op):
    np_a = np.array(np.random.uniform(1, 127, size),
                    dtype=to_numpy_dtype(dtype))
    np_b = np.array(np.random.uniform(1, 127, size),
                    dtype=to_numpy_dtype(dtype))
    a = o3c.Tensor(np_a, dtype=dtype, device=o3c.Device("CPU:0"))
    b = o3c.Tensor(np_b, dtype=dtype, device=o3c.Device("CPU:0"))
    benchmark(op, a, b)


@pytest.mark.parametrize("size", list_tensor_sizes())
@pytest.mark.parametrize("dtype", list_non_bool_dtypes())
@pytest.mark.parametrize("op", list_binary_ops())
def test_binary_ew_ops_numpy(benchmark, size, dtype, op):
    np_a = np.array(np.random.uniform(1, 127, size),
                    dtype=to_numpy_dtype(dtype))
    np_b = np.array(np.random.uniform(1, 127, size),
                    dtype=to_numpy_dtype(dtype))
    benchmark(to_numpy_binary_op(op), np_a, np_b)