33from .shape import Shape
44from .devicetype import Device
55from deepx .autograd import Graph ,DataNode
6-
6+ from .deepxir import DeepxIR
7+ from .dtype import infer_dtype ,DTYPE_MAP
78class Tensor :
89 def __init__ (self , data = None , shape = None , device = None , dtype = None , graph = None ):
10+ # 计算图相关
11+ if graph is None :
12+ self ._graph = Graph .get_default ()
13+ else :
14+ self ._graph = graph
15+ self ._node = self ._graph .add_tensor ("" ,data = self )
16+
917 # data
1018 if data is not None :
1119 import numpy as np
1220 if not isinstance (data , np .ndarray ):
1321 data = np .array (data )
14- self .data = data
22+ self .data = data
1523 self ._shape = Shape (data .shape )
1624
25+ # dtype
26+ if dtype is None :
27+ self ._dtype = infer_dtype (data )
28+ else :
29+ self ._dtype = dtype
30+ self ._data = data .astype (DTYPE_MAP [dtype ], casting = 'safe' )
31+ self ._node .dtype = dtype
1732 # shape
1833 if shape is not None :
1934 if isinstance (shape , (tuple , list )) and all (isinstance (i , int ) for i in shape ):
@@ -22,7 +37,13 @@ def __init__(self, data=None, shape=None, device=None, dtype=None, graph=None):
2237 self ._shape = shape
2338 else :
2439 raise ValueError ("Invalid shape" )
25-
40+ shapeNode = self ._graph .add_vector ("" ,data = self ._shape .shape )
41+ self ._node .add_input (shapeNode )
42+ if self ._graph .eager :
43+ ir1 = DeepxIR ("argset" , 'int32' , self ._shape .shape , [shapeNode .name ])
44+ print (ir1 )
45+ ir2 = DeepxIR ("newtensor" , self ._dtype , [shapeNode .name ], [self ._node .name ])
46+ print (ir2 )
2647 # device
2748 if isinstance (device , str ):
2849 self ._device = Device .from_string (device )
@@ -32,17 +53,7 @@ def __init__(self, data=None, shape=None, device=None, dtype=None, graph=None):
3253 self ._device = Device .CPU # 默认设备
3354
3455 self ._dtype = dtype
35-
36- # 计算图相关
37- if graph is None :
38- self ._graph = Graph .get_default ()
39- else :
40- self ._graph = graph
41- self ._node = self ._graph .add_data (name = "" ,data = self .data )
42-
43-
44- self ._requires_grad = False
45-
56+
4657 self .data = data
4758 # shape
4859 @property
0 commit comments