funcs.py 6.9 KB
Newer Older
1 2
import builtins

3
import torch
HansBug's avatar
HansBug 已提交
4
from treevalue import TreeValue
5
from treevalue import func_treelize as original_func_treelize
HansBug's avatar
HansBug 已提交
6
from treevalue.utils import post_process
7

HansBug's avatar
HansBug 已提交
8
from .tensor import Tensor, tireduce
9
from ..common import TreeObject, ireduce
HansBug's avatar
HansBug 已提交
10
from ..utils import replaceable_partial, doc_from, args_mapping
11

12 13 14 15 16 17 18 19 20
__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 已提交
21
    'tensor',
22 23
]

HansBug's avatar
HansBug 已提交
24 25 26 27
func_treelize = post_process(post_process(args_mapping(
    lambda i, x: Tensor(x) if isinstance(x, (dict, TreeValue)) else x)))(
    replaceable_partial(original_func_treelize, return_type=Tensor)
)
28 29


30
@doc_from(torch.zeros)
31
@func_treelize()
32
def zeros(*args, **kwargs):
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    """
    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]),
        })
    """
54
    return torch.zeros(*args, **kwargs)
55 56


57
@doc_from(torch.zeros_like)
58 59
@func_treelize()
def zeros_like(input_, *args, **kwargs):
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    """
    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]),
        })
    """
81 82 83
    return torch.zeros_like(input_, *args, **kwargs)


84
@doc_from(torch.randn)
85
@func_treelize()
86 87
def randn(*args, **kwargs):
    return torch.randn(*args, **kwargs)
88 89


90
@doc_from(torch.randn_like)
91 92 93 94 95
@func_treelize()
def randn_like(input_, *args, **kwargs):
    return torch.randn_like(input_, *args, **kwargs)


96
@doc_from(torch.randint)
97
@func_treelize()
98 99
def randint(*args, **kwargs):
    return torch.randint(*args, **kwargs)
100 101


102
@doc_from(torch.randint_like)
103 104 105 106 107
@func_treelize()
def randint_like(input_, *args, **kwargs):
    return torch.randint_like(input_, *args, **kwargs)


108
@doc_from(torch.ones)
109
@func_treelize()
110 111
def ones(*args, **kwargs):
    return torch.ones(*args, **kwargs)
112 113


114
@doc_from(torch.ones_like)
115 116 117 118 119
@func_treelize()
def ones_like(input_, *args, **kwargs):
    return torch.ones_like(input_, *args, **kwargs)


120
@doc_from(torch.full)
121
@func_treelize()
122 123
def full(*args, **kwargs):
    return torch.full(*args, **kwargs)
124 125


126
@doc_from(torch.full_like)
127 128 129 130 131
@func_treelize()
def full_like(input_, *args, **kwargs):
    return torch.full_like(input_, *args, **kwargs)


132
@doc_from(torch.empty)
133
@func_treelize()
134 135
def empty(*args, **kwargs):
    return torch.empty(*args, **kwargs)
136 137


138
@doc_from(torch.empty_like)
139 140 141 142 143
@func_treelize()
def empty_like(input_, *args, **kwargs):
    return torch.empty_like(input_, *args, **kwargs)


144
@doc_from(torch.all)
145
@tireduce(torch.all)
146 147
@func_treelize(return_type=TreeObject)
def all(input_, *args, **kwargs):
148 149 150 151 152
    """
    In ``treetensor``, you can get the ``all`` result of a whole tree with this function.

    Example::

HansBug's avatar
HansBug 已提交
153 154
        >>> import torch
        >>> import treetensor.torch as ttorch
155
        >>> ttorch.all(torch.tensor([True, True]))  # the same as torch.all
156 157
        torch.tensor(True)

158
        >>> ttorch.all(ttorch.tensor({
HansBug's avatar
HansBug 已提交
159 160
        >>>     'a': [True, True],
        >>>     'b': [True, True],
161 162 163
        >>> }))
        torch.tensor(True)

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

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    .. 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),
            })

185
    """
186 187 188
    return torch.all(input_, *args, **kwargs)


189
@doc_from(torch.any)
190 191 192
@tireduce(torch.any)
@func_treelize(return_type=TreeObject)
def any(input_, *args, **kwargs):
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
    """
    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),
            })

    """
231 232 233
    return torch.any(input_, *args, **kwargs)


234
@doc_from(torch.eq)
235 236 237 238 239
@func_treelize()
def eq(input_, other, *args, **kwargs):
    return torch.eq(input_, other, *args, **kwargs)


240
@doc_from(torch.equal)
241
@ireduce(builtins.all)
242 243 244
@func_treelize()
def equal(input_, other, *args, **kwargs):
    return torch.equal(input_, other, *args, **kwargs)
HansBug's avatar
HansBug 已提交
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


@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)