funcs.py 7.0 KB
Newer Older
1 2 3 4 5
"""
Overview:
    Common functions, based on ``torch`` module.
"""

6 7
import builtins

8
import torch
HansBug's avatar
HansBug 已提交
9
from treevalue import TreeValue
10
from treevalue import func_treelize as original_func_treelize
HansBug's avatar
HansBug 已提交
11
from treevalue.utils import post_process
12

HansBug's avatar
HansBug 已提交
13
from .tensor import Tensor, tireduce
14
from ..common import TreeObject, ireduce
HansBug's avatar
HansBug 已提交
15
from ..utils import replaceable_partial, doc_from, args_mapping
16

17 18 19 20 21 22 23 24 25
__all__ = [
    'zeros', 'zeros_like',
    'randn', 'randn_like',
    'randint', 'randint_like',
    'ones', 'ones_like',
    'full', 'full_like',
    'empty', 'empty_like',
    'all', 'any',
    'eq', 'equal',
HansBug's avatar
HansBug 已提交
26
    'tensor',
27 28
]

HansBug's avatar
HansBug 已提交
29
func_treelize = post_process(post_process(args_mapping(
30
    lambda i, x: TreeValue(x) if isinstance(x, (dict, TreeValue)) else x)))(
HansBug's avatar
HansBug 已提交
31 32
    replaceable_partial(original_func_treelize, return_type=Tensor)
)
33 34


35
@doc_from(torch.zeros)
36
@func_treelize()
37
def zeros(*args, **kwargs):
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    """
    In ``treetensor``, you can use ``zeros`` to create a tree of tensors with all zeros.

    Example::

        >>> import torch
        >>> import treetensor.torch as ttorch
        >>> ttorch.zeros(2, 3)  # the same as torch.zeros(2, 3)
        torch.tensor([[0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0]])

        >>> ttorch.zeros({
        >>>     'a': (2, 3),
        >>>     'b': (4, ),
        >>> })
        ttorch.tensor({
            'a': torch.tensor([[0.0, 0.0, 0.0],
                               [0.0, 0.0, 0.0]]),
            'b': torch.tensor([0.0, 0.0, 0.0, 0.0]),
        })
    """
59
    return torch.zeros(*args, **kwargs)
60 61


62
@doc_from(torch.zeros_like)
63 64
@func_treelize()
def zeros_like(input_, *args, **kwargs):
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    """
    In ``treetensor``, you can use ``zeros_like`` to create a tree of tensors with all zeros like another tree.

    Example::

        >>> import torch
        >>> import treetensor.torch as ttorch
        >>> ttorch.zeros_like(torch.randn(2, 3))  # the same as torch.zeros_like(torch.randn(2, 3))
        torch.tensor([[0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0]])

        >>> ttorch.zeros_like({
        >>>     'a': torch.randn(2, 3),
        >>>     'b': torch.randn(4, ),
        >>> })
        ttorch.tensor({
            'a': torch.tensor([[0.0, 0.0, 0.0],
                               [0.0, 0.0, 0.0]]),
            'b': torch.tensor([0.0, 0.0, 0.0, 0.0]),
        })
    """
86 87 88
    return torch.zeros_like(input_, *args, **kwargs)


89
@doc_from(torch.randn)
90
@func_treelize()
91 92
def randn(*args, **kwargs):
    return torch.randn(*args, **kwargs)
93 94


95
@doc_from(torch.randn_like)
96 97 98 99 100
@func_treelize()
def randn_like(input_, *args, **kwargs):
    return torch.randn_like(input_, *args, **kwargs)


101
@doc_from(torch.randint)
102
@func_treelize()
103 104
def randint(*args, **kwargs):
    return torch.randint(*args, **kwargs)
105 106


107
@doc_from(torch.randint_like)
108 109 110 111 112
@func_treelize()
def randint_like(input_, *args, **kwargs):
    return torch.randint_like(input_, *args, **kwargs)


113
@doc_from(torch.ones)
114
@func_treelize()
115 116
def ones(*args, **kwargs):
    return torch.ones(*args, **kwargs)
117 118


119
@doc_from(torch.ones_like)
120 121 122 123 124
@func_treelize()
def ones_like(input_, *args, **kwargs):
    return torch.ones_like(input_, *args, **kwargs)


125
@doc_from(torch.full)
126
@func_treelize()
127 128
def full(*args, **kwargs):
    return torch.full(*args, **kwargs)
129 130


131
@doc_from(torch.full_like)
132 133 134 135 136
@func_treelize()
def full_like(input_, *args, **kwargs):
    return torch.full_like(input_, *args, **kwargs)


137
@doc_from(torch.empty)
138
@func_treelize()
139 140
def empty(*args, **kwargs):
    return torch.empty(*args, **kwargs)
141 142


143
@doc_from(torch.empty_like)
144 145 146 147 148
@func_treelize()
def empty_like(input_, *args, **kwargs):
    return torch.empty_like(input_, *args, **kwargs)


149
@doc_from(torch.all)
150
@tireduce(torch.all)
151 152
@func_treelize(return_type=TreeObject)
def all(input_, *args, **kwargs):
153 154 155 156 157
    """
    In ``treetensor``, you can get the ``all`` result of a whole tree with this function.

    Example::

HansBug's avatar
HansBug 已提交
158 159
        >>> import torch
        >>> import treetensor.torch as ttorch
160
        >>> ttorch.all(torch.tensor([True, True]))  # the same as torch.all
161 162
        torch.tensor(True)

163
        >>> ttorch.all(ttorch.tensor({
HansBug's avatar
HansBug 已提交
164 165
        >>>     'a': [True, True],
        >>>     'b': [True, True],
166 167 168
        >>> }))
        torch.tensor(True)

169
        >>> ttorch.all(ttorch.tensor({
HansBug's avatar
HansBug 已提交
170 171
        >>>     'a': [True, True],
        >>>     'b': [True, False],
172 173 174
        >>> }))
        torch.tensor(False)

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    .. note::

        In this ``all`` function, the return value should be a tensor with single boolean value.

        If what you need is a tree of boolean tensors, you should do like this

            >>> ttorch.tensor({
            >>>     'a': [True, True],
            >>>     'b': [True, False],
            >>> }).map(torch.all)
            ttorch.tensor({
                'a': torch.tensor(True),
                'b': torch.tensor(False),
            })

190
    """
191 192 193
    return torch.all(input_, *args, **kwargs)


194
@doc_from(torch.any)
195 196 197
@tireduce(torch.any)
@func_treelize(return_type=TreeObject)
def any(input_, *args, **kwargs):
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
    """
    In ``treetensor``, you can get the ``any`` result of a whole tree with this function.

    Example::

        >>> import torch
        >>> import treetensor.torch as ttorch
        >>> ttorch.any(torch.tensor([False, False]))  # the same as torch.any
        torch.tensor(False)

        >>> ttorch.any(ttorch.tensor({
        >>>     'a': [True, False],
        >>>     'b': [False, False],
        >>> }))
        torch.tensor(True)

        >>> ttorch.any(ttorch.tensor({
        >>>     'a': [False, False],
        >>>     'b': [False, False],
        >>> }))
        torch.tensor(False)

    .. note::

        In this ``any`` function, the return value should be a tensor with single boolean value.

        If what you need is a tree of boolean tensors, you should do like this

            >>> ttorch.tensor({
            >>>     'a': [True, False],
            >>>     'b': [False, False],
            >>> }).map(torch.any)
            ttorch.tensor({
                'a': torch.tensor(True),
                'b': torch.tensor(False),
            })

    """
236 237 238
    return torch.any(input_, *args, **kwargs)


239
@doc_from(torch.eq)
240 241 242 243 244
@func_treelize()
def eq(input_, other, *args, **kwargs):
    return torch.eq(input_, other, *args, **kwargs)


245
@doc_from(torch.equal)
246
@ireduce(builtins.all)
247 248 249
@func_treelize()
def equal(input_, other, *args, **kwargs):
    return torch.equal(input_, other, *args, **kwargs)
HansBug's avatar
HansBug 已提交
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


@doc_from(torch.tensor)
@func_treelize()
def tensor(*args, **kwargs):
    """
    In ``treetensor``, you can create a tree tensor with simple data structure.

    Examples::

        >>> import torch
        >>> import treetensor.torch as ttorch
        >>> ttorch.tensor(True)  # the same as torch.tensor(True)
        torch.tensor(True)

        >>> ttorch.tensor([1, 2, 3])  # the same as torch.tensor([1, 2, 3])
        torch.tensor([1, 2, 3])

        >>> ttorch.tensor({'a': 1, 'b': [1, 2, 3], 'c': [[True, False], [False, True]]})
        ttorch.Tensor({
            'a': torch.tensor(1),
            'b': torch.tensor([1, 2, 3]),
            'c': torch.tensor([[True, False], [False, True]]),
        })

    """
    return torch.tensor(*args, **kwargs)