Skip to content

Commit c143a25

Browse files
committed
deepxIR:front、excuter对接中
1 parent a7196dd commit c143a25

4 files changed

Lines changed: 50 additions & 24 deletions

File tree

front/py/deepx/tensor/elementwise.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,38 @@
33
from .deepxir import DeepxIR
44
OpNode.register("add")
55

6-
@tensor_method
7-
def add(self, other):
8-
resultnode = self.graph.add_tensor("", self)
9-
opnode = self.graph.add_op("add")
10-
opnode.add_input(self.node)
11-
opnode.add_input(other.node)
12-
resultnode.add_input(opnode)
13-
if self.graph.eager:
14-
ir=DeepxIR("add", self._dtype, [self.node.name, other.node.name], [resultnode.name])
6+
def add(a:Tensor,b:Tensor,out:Tensor):
7+
opnode = a.graph.add_op("add")
8+
opnode.add_input(a.node)
9+
opnode.add_input(b.node)
10+
out.node.add_input(opnode)
11+
if a.graph.eager:
12+
ir=DeepxIR("add", a.dtype, [a.node.name, b.node.name], [out.node.name])
1513
print(ir)
16-
return resultnode
14+
15+
@tensor_method
16+
def add_(self, other):
17+
result = Tensor(dtype=self.dtype,shape=self.shape)
18+
result._node = self.graph.add_tensor("", self)
19+
add(self,other,result)
20+
return result
1721

1822
OpNode.register("mul")
1923

24+
def mul(a:Tensor,b:Tensor,out:Tensor):
25+
opnode = a.graph.add_op("mul")
26+
opnode.add_input(a.node)
27+
opnode.add_input(b.node)
28+
out.node.add_input(opnode)
29+
if a.graph.eager:
30+
ir=DeepxIR("mul", a.dtype, [a.node.name, b.node.name], [out.node.name])
31+
print(ir)
32+
2033
@tensor_method
21-
def mul(self, other):
22-
result = self.graph.add_tensor("", self.dtype, self.shape, self.requires_grad)
23-
op = self.graph.add_op("mul")
24-
op.add_input(self.node)
25-
op.add_input(other.node)
26-
result.add_input(op)
34+
def mul_(self, other):
35+
result = Tensor(dtype=self.dtype,shape=self.shape)
36+
result._node = self.graph.add_tensor("", self)
37+
mul(self,other,result)
2738
return result
2839

2940

front/py/deepx/tensor/tensor.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ def __init__(self, data=None, shape=None, device=None, dtype=None, graph=None):
2727
self._dtype = infer_dtype(data)
2828
else:
2929
self._dtype = dtype
30-
self._data = data.astype(DTYPE_MAP[dtype], casting='safe')
31-
self._node.dtype = dtype
3230
# shape
3331
if shape is not None:
3432
if isinstance(shape, (tuple, list)) and all(isinstance(i, int) for i in shape):
@@ -51,10 +49,7 @@ def __init__(self, data=None, shape=None, device=None, dtype=None, graph=None):
5149
self._device = device
5250
else:
5351
self._device = Device.CPU # 默认设备
54-
55-
self._dtype = dtype
56-
57-
self.data = data
52+
5853
# shape
5954
@property
6055
def shape(self):
@@ -78,6 +73,7 @@ def numel(self):
7873

7974

8075
#dtype device
76+
@property
8177
def dtype(self):
8278
return self._dtype
8379

front/py/examples/2_ir/2_add.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
from deepx.tensor import Tensor
1+
from deepx.tensor import Tensor,mul,add
22
print()
33

44
t1 = Tensor([1,2,3])
55
t2 = Tensor([4,5,6])
6-
t3 = t1.add(t2)
6+
t3 = t1.add_(t2)
7+
8+
f1=Tensor([0.5,0.1,6])
9+
f2=t3.add_(f1)
10+
11+
f3=Tensor([1,2,3])
12+
f4=f2.mul_(f3)
13+
14+
f5=Tensor([1,2,3])
15+
f6=Tensor([1,2,3])
16+
mul(f5,f3,out=f6)
17+
18+
f7=Tensor([1,2,3])
19+
mul(f5,f6,f7)
720

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from deepx.tensor import Tensor,mul,add
2+
print()
3+
4+
t1 = Tensor(shape=[2,3,4],dtype="float32")
5+
t2 = Tensor(shape=[2,3,4],dtype="float32")
6+
t3 = t1.add_(t2)

0 commit comments

Comments
 (0)