Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
flybirding10011
DI-treetensor
提交
ec7f9fd0
D
DI-treetensor
项目概览
flybirding10011
/
DI-treetensor
与 Fork 源项目一致
Fork自
OpenDILab开源决策智能平台 / DI-treetensor
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DI-treetensor
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ec7f9fd0
编写于
9月 30, 2021
作者:
HansBug
😆
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dev(hansbug): add ttorch.as_tensor, update constructor of ttorch.Tensor
上级
37a4f1f4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
69 addition
and
40 deletion
+69
-40
test/torch/funcs/test_construct.py
test/torch/funcs/test_construct.py
+13
-0
test/torch/funcs/test_reduction.py
test/torch/funcs/test_reduction.py
+24
-24
treetensor/torch/funcs/construct.py
treetensor/torch/funcs/construct.py
+31
-5
treetensor/torch/tensor.py
treetensor/torch/tensor.py
+1
-11
未找到文件。
test/torch/funcs/test_construct.py
浏览文件 @
ec7f9fd0
...
...
@@ -34,6 +34,19 @@ class TestTorchFuncsConstruct:
}
})).
all
()
@
choose_mark
()
def
test_tensor
(
self
):
assert
ttorch
.
as_tensor
(
True
)
==
torch
.
tensor
(
True
)
assert
(
ttorch
.
as_tensor
([
1
,
2
,
3
],
dtype
=
torch
.
float32
)
==
torch
.
tensor
([
1.0
,
2.0
,
3.0
])).
all
()
assert
(
ttorch
.
as_tensor
({
'a'
:
torch
.
tensor
([
1
,
2
,
3
]),
'b'
:
{
'x'
:
[[
4
,
5
],
[
6
,
7
]]}
},
dtype
=
torch
.
float32
)
==
ttorch
.
tensor
({
'a'
:
[
1.0
,
2.0
,
3.0
],
'b'
:
{
'x'
:
[[
4.0
,
5.0
],
[
6.0
,
7.0
]]},
})).
all
()
@
choose_mark
()
def
test_clone
(
self
):
t1
=
ttorch
.
clone
(
torch
.
tensor
([
1.0
,
2.0
,
1.5
]))
...
...
test/torch/funcs/test_reduction.py
浏览文件 @
ec7f9fd0
...
...
@@ -49,16 +49,16 @@ class TestTorchFuncsReduction:
assert
not
r6
r7
=
ttorch
.
all
(
ttorch
.
tensor
({
'a'
:
torch
.
tensor
([
True
,
True
,
True
])
,
'b'
:
torch
.
tensor
([
True
,
True
,
False
])
,
'a'
:
[
True
,
True
,
True
]
,
'b'
:
[
True
,
True
,
False
]
,
}),
reduce
=
False
)
assert
(
r7
==
ttorch
.
tensor
({
'a'
:
True
,
'b'
:
False
})).
all
()
r8
=
ttorch
.
all
(
ttorch
.
tensor
({
'a'
:
torch
.
tensor
([
True
,
True
,
True
])
,
'b'
:
torch
.
tensor
([
True
,
True
,
False
])
,
'a'
:
[
True
,
True
,
True
]
,
'b'
:
[
True
,
True
,
False
]
,
}),
dim
=
0
)
assert
(
r8
==
ttorch
.
tensor
({
'a'
:
True
,
'b'
:
False
...
...
@@ -66,8 +66,8 @@ class TestTorchFuncsReduction:
with
pytest
.
warns
(
UserWarning
):
r9
=
ttorch
.
all
(
ttorch
.
tensor
({
'a'
:
torch
.
tensor
([
True
,
True
,
True
])
,
'b'
:
torch
.
tensor
([
True
,
True
,
False
])
,
'a'
:
[
True
,
True
,
True
]
,
'b'
:
[
True
,
True
,
False
]
,
}),
dim
=
0
,
reduce
=
True
)
assert
(
r9
==
ttorch
.
tensor
({
'a'
:
True
,
'b'
:
False
...
...
@@ -90,41 +90,41 @@ class TestTorchFuncsReduction:
assert
r3
==
torch
.
tensor
(
False
)
assert
not
r3
r4
=
ttorch
.
any
({
'a'
:
torch
.
tensor
([
True
,
True
,
True
])
,
'b'
:
torch
.
tensor
([
True
,
True
,
True
])
,
})
r4
=
ttorch
.
any
(
ttorch
.
tensor
(
{
'a'
:
[
True
,
True
,
True
]
,
'b'
:
[
True
,
True
,
True
]
,
})
)
assert
torch
.
is_tensor
(
r4
)
assert
r4
==
torch
.
tensor
(
True
)
assert
r4
r5
=
ttorch
.
any
({
'a'
:
torch
.
tensor
([
True
,
True
,
True
])
,
'b'
:
torch
.
tensor
([
True
,
True
,
False
])
,
})
r5
=
ttorch
.
any
(
ttorch
.
tensor
(
{
'a'
:
[
True
,
True
,
True
]
,
'b'
:
[
True
,
True
,
False
]
,
})
)
assert
torch
.
is_tensor
(
r5
)
assert
r5
==
torch
.
tensor
(
True
)
assert
r5
r6
=
ttorch
.
any
({
'a'
:
torch
.
tensor
([
False
,
False
,
False
])
,
'b'
:
torch
.
tensor
([
False
,
False
,
False
])
,
})
r6
=
ttorch
.
any
(
ttorch
.
tensor
(
{
'a'
:
[
False
,
False
,
False
]
,
'b'
:
[
False
,
False
,
False
]
,
})
)
assert
torch
.
is_tensor
(
r6
)
assert
r6
==
torch
.
tensor
(
False
)
assert
not
r6
r7
=
ttorch
.
any
(
ttorch
.
tensor
({
'a'
:
torch
.
tensor
([
True
,
True
,
False
])
,
'b'
:
torch
.
tensor
([
False
,
False
,
False
])
,
'a'
:
[
True
,
True
,
False
]
,
'b'
:
[
False
,
False
,
False
]
,
}),
reduce
=
False
)
assert
(
r7
==
ttorch
.
tensor
({
'a'
:
True
,
'b'
:
False
})).
all
()
r8
=
ttorch
.
any
(
ttorch
.
tensor
({
'a'
:
torch
.
tensor
([
True
,
True
,
False
])
,
'b'
:
torch
.
tensor
([
False
,
False
,
False
])
,
'a'
:
[
True
,
True
,
False
]
,
'b'
:
[
False
,
False
,
False
]
,
}),
dim
=
0
)
assert
(
r8
==
ttorch
.
tensor
({
'a'
:
True
,
'b'
:
False
...
...
@@ -132,8 +132,8 @@ class TestTorchFuncsReduction:
with
pytest
.
warns
(
UserWarning
):
r9
=
ttorch
.
any
(
ttorch
.
tensor
({
'a'
:
torch
.
tensor
([
True
,
True
,
False
])
,
'b'
:
torch
.
tensor
([
False
,
False
,
False
])
,
'a'
:
[
True
,
True
,
False
]
,
'b'
:
[
False
,
False
,
False
]
,
}),
dim
=
0
,
reduce
=
True
)
assert
(
r9
==
ttorch
.
tensor
({
'a'
:
True
,
'b'
:
False
...
...
treetensor/torch/funcs/construct.py
浏览文件 @
ec7f9fd0
...
...
@@ -3,7 +3,7 @@ import torch
from
.base
import
doc_from_base
,
func_treelize
__all__
=
[
'tensor'
,
'clone'
,
'tensor'
,
'
as_tensor'
,
'
clone'
,
'zeros'
,
'zeros_like'
,
'randn'
,
'randn_like'
,
'randint'
,
'randint_like'
,
...
...
@@ -36,10 +36,36 @@ def tensor(data, *args, **kwargs):
└── c --> tensor([[ True, False],
[False, True]])
"""
if
torch
.
is_tensor
(
data
):
return
data
else
:
return
torch
.
tensor
(
data
,
*
args
,
**
kwargs
)
return
torch
.
tensor
(
data
,
*
args
,
**
kwargs
)
@
doc_from_base
()
@
func_treelize
()
def
as_tensor
(
data
,
*
args
,
**
kwargs
):
"""
Convert the data into a :class:`treetensor.torch.Tensor` or :class:`torch.Tensor`.
Examples::
>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.as_tensor(True)
tensor(True)
>>> ttorch.as_tensor([1, 2, 3], dtype=torch.float32)
tensor([1., 2., 3.])
>>> ttorch.as_tensor({
... 'a': torch.tensor([1, 2, 3]),
... 'b': {'x': [[4, 5], [6, 7]]}
... }, dtype=torch.float32)
<Tensor 0x7fc2b80c25c0>
├── a --> tensor([1., 2., 3.])
└── b --> <Tensor 0x7fc2b80c24e0>
└── x --> tensor([[4., 5.],
[6., 7.]])
"""
return
torch
.
as_tensor
(
data
,
*
args
,
**
kwargs
)
# noinspection PyShadowingBuiltins
...
...
treetensor/torch/tensor.py
浏览文件 @
ec7f9fd0
...
...
@@ -18,17 +18,7 @@ doc_from_base = replaceable_partial(original_doc_from_base, base=pytorch.Tensor)
_TorchProxy
,
_InstanceTorchProxy
=
get_tree_proxy
(
pytorch
.
Tensor
)
def
_to_tensor
(
*
args
,
**
kwargs
):
if
(
len
(
args
)
==
1
and
not
kwargs
)
or
\
(
not
args
and
set
(
kwargs
.
keys
())
==
{
'data'
}):
data
=
args
[
0
]
if
len
(
args
)
==
1
else
kwargs
[
'data'
]
if
isinstance
(
data
,
pytorch
.
Tensor
):
return
data
return
pytorch
.
tensor
(
*
args
,
**
kwargs
)
class
_BaseTensorMeta
(
clsmeta
(
_to_tensor
,
allow_dict
=
True
)):
class
_BaseTensorMeta
(
clsmeta
(
pytorch
.
as_tensor
,
allow_dict
=
True
)):
pass
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录