Skip to content

Commit 0235750

Browse files
authored
transformer目录,按照pytorch风格整合进nn.module (#79)
* py: 1.transformer目录,按照pytorch风格整合进nn.module
1 parent 84b626e commit 0235750

34 files changed

Lines changed: 190 additions & 157 deletions

front/py/deepx/nn/functional/changeshape.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
from typing import Union
22
from deepx import Tensor
3-
from .leaffunc_changeshape import reshape,indexselect, concat,broadcastTo
3+
from .leaffunc_changeshape import reshape,indexselect, concat,broadcastTo,permute
44
from .leaffunc_init import newtensor,arange
5+
6+
7+
def transpose(t:Tensor,dim0:int,dim1:int,out:Union[Tensor,str]='')->Tensor:
8+
dimorder = list(range(t.ndim))
9+
dimorder[dim0],dimorder[dim1]=dimorder[dim1],dimorder[dim0]
10+
return permute(t,tuple(dimorder),out)
11+
12+
513
def squeeze(t:Tensor,dim:int)->Tensor:
614
assert isinstance(dim,int)
715
assert isinstance(t,Tensor)

front/py/deepx/nn/functional/leaffunc_changeshape.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def reshape(t:Tensor,shape:tuple[int,...],out:Union[Tensor,str]='')->Tensor:
99
for i in shape:
1010
assert isinstance(i,int) and i>0
1111

12-
outtensor=out
1312
if isinstance(out,str) or out is None:
1413
outshape=shape
1514
outtensor=newtensor(outshape,dtype=t.dtype,name=out)
@@ -40,11 +39,6 @@ def permute(t:Tensor,
4039
rtf_transpose(t,dimorder,outtensor,defaultauthor['transpose'])
4140
return outtensor
4241

43-
def transpose(t:Tensor,out:Union[Tensor,str]='')->Tensor:
44-
dimorder = list(range(t.ndim))
45-
dimorder[-1],dimorder[-2]=dimorder[-2],dimorder[-1]
46-
return permute(t,tuple(dimorder),out)
47-
4842

4943

5044
def concat(tensors:Union[list[Tensor],tuple[Tensor,...]],dim:int,out:Union[Tensor,str]='')->Tensor:

front/py/deepx/nn/functional/rtf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def A_op_C(op:str,a:Tensor,out:Tensor,author='miaobyte'):
3737
ir=DeepxIR(op, args, returns,author)
3838
send(ir)
3939

40-
def A_b1_b2_op_C(op:str,a:Tensor,b1:tuple[int],b2:bool,out:Tensor,author='miaobyte'):
40+
def A_b1_b2_op_C(op:str,a:Tensor,b1:tuple[int,...],b2:bool,out:Tensor,author='miaobyte'):
4141
args=[Param.tensor(a),Param.vector(b1,'int32'),Param.varbool(b2)]
4242
returns=[Param.tensor(out)]
4343
ir=DeepxIR(op, args, returns,author)

front/py/deepx/nn/functional/rtf_changeshape.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
from deepx.nn.deepxir import DeepxIR,Param
33
from deepx.scheduler import send
44

5-
def rtf_reshape(t:Tensor,shape:tuple[int],out:Tensor,author='miaobyte'):
5+
def rtf_reshape(t:Tensor,shape:tuple[int,...],out:Tensor,author='miaobyte'):
66
args=[Param.tensor(t),Param.vector(shape,'int32')]
77
returns=[Param.tensor(out)]
88
ir=DeepxIR("reshape", args, returns,author)
99
send(ir)
1010

1111

12-
def rtf_transpose(t:Tensor,dimorder:tuple[int],out:Tensor,author='miaobyte'):
12+
def rtf_transpose(t:Tensor,dimorder:tuple[int,...],out:Tensor,author='miaobyte'):
1313
args=[Param.tensor(t),Param.vector(dimorder,'int32')]
1414
returns=[Param.tensor(out)]
1515
ir=DeepxIR("transpose", args, returns,author)
@@ -22,7 +22,7 @@ def rtf_concat(tensors:tuple[Tensor],dim:int,out:Tensor,author='miaobyte'):
2222
send(ir)
2323

2424

25-
def rtf_broadcastTo(t:Tensor,new_shape:tuple[int],out:Tensor,author='miaobyte'):
25+
def rtf_broadcastTo(t:Tensor,new_shape:tuple[int,...],out:Tensor,author='miaobyte'):
2626
args=[Param.tensor(t),Param.vector(new_shape,'int32')]
2727
returns=[Param.tensor(out)]
2828
ir=DeepxIR("broadcastTo", args, returns,author)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
from deepx.tensor import Tensor
22
from .rtf import A_b1_b2_op_C
33

4-
def rtf_sum(a:Tensor,dim:tuple[int],keepdim:bool,out: Tensor, author:str='miaobyte')->Tensor:
4+
def rtf_sum(a:Tensor,dim:tuple[int,...],keepdim:bool,out: Tensor, author:str='miaobyte')->Tensor:
55
A_b1_b2_op_C("sum",a,dim,keepdim,out,author)
66

77

8-
def rtf_prod(a:Tensor,dim:tuple[int],keepdim:bool,out:Tensor, author:str='miaobyte')->Tensor:
8+
def rtf_prod(a:Tensor,dim:tuple[int,...],keepdim:bool,out:Tensor, author:str='miaobyte')->Tensor:
99
A_b1_b2_op_C("prod",a,dim,keepdim,out,author)
1010

1111

12-
def rtf_reducemax(a:Tensor,dim:tuple[int],keepdim:bool,out:Tensor, author:str='miaobyte')->Tensor:
12+
def rtf_reducemax(a:Tensor,dim:tuple[int,...],keepdim:bool,out:Tensor, author:str='miaobyte')->Tensor:
1313
A_b1_b2_op_C("reducemax",a,dim,keepdim,out,author)
1414

1515

16-
def rtf_reducemin(a:Tensor,dim:tuple[int],keepdim:bool,out:Tensor, author:str='miaobyte')->Tensor:
16+
def rtf_reducemin(a:Tensor,dim:tuple[int,...],keepdim:bool,out:Tensor, author:str='miaobyte')->Tensor:
1717
A_b1_b2_op_C("reducemin",a,dim,keepdim,out,author)
1818

front/py/deepx/nn/modules/linear.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def reset_parameters(self) -> None:
4141

4242
def forward(self, input: Tensor) -> Tensor:
4343
#`y = xA^T + b`
44-
y=input @ self.weight.T
44+
y=input @ self.weight.mT
4545
oldshape=y.shape
4646
if self.bias is not None:
4747
y.reshape_(tuple(y.shape[1:]))

front/py/deepx/transformer/models/llama/mlp.py renamed to front/py/deepx/nn/modules/mlp/mlp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"silu":swish_fn,
66
}
77

8-
class LlamaMLP(Module):
8+
class MLP(Module):
99
def __init__(self, config:dict):
1010
super().__init__()
1111
# 输入层大小
File renamed without changes.

front/py/deepx/nn/modules/normalization.py

Whitespace-only changes.

0 commit comments

Comments
 (0)