@@ -18,7 +18,7 @@ use std::ops;
18
18
use std:: rc:: Rc ;
19
19
use super :: DataType ;
20
20
use super :: Graph ;
21
- use super :: Node ;
21
+ use super :: Operation ;
22
22
use super :: Port ;
23
23
use super :: Status ;
24
24
use super :: Tensor ;
@@ -37,7 +37,7 @@ pub enum OpLevel {
37
37
38
38
////////////////////////
39
39
40
- /// A node in an expression tree, which is a thin wrapper around an ExprImpl.
40
+ /// A operation in an expression tree, which is a thin wrapper around an ExprImpl.
41
41
///
42
42
/// This is separate from ExprImpl because we want expressions to be wrapped in an Rc,
43
43
/// and we can't directly implement std::ops::Add, etc., for Rc<E: ExprImpl<T>>.
@@ -68,7 +68,7 @@ pub trait ExprImpl<T: TensorType>: Display + Debug {
68
68
/// Returns the precedence level for this operator.
69
69
fn op_level ( & self ) -> OpLevel ;
70
70
fn children ( & self ) -> Vec < Box < AnyExpr > > ; // TODO: return an iterator
71
- fn create_node ( & self , graph : & mut Graph , children : & [ Rc < Node > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > ;
71
+ fn create_operation ( & self , graph : & mut Graph , children : & [ Rc < Operation > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > ;
72
72
}
73
73
74
74
impl < T : TensorType > ExprImpl < T > for T {
@@ -80,8 +80,8 @@ impl<T: TensorType> ExprImpl<T> for T {
80
80
vec ! [ ]
81
81
}
82
82
83
- fn create_node ( & self , graph : & mut Graph , _children : & [ Rc < Node > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > {
84
- let mut nd = try!( graph. new_node ( "Const" , & id_gen ( ) ) ) ;
83
+ fn create_operation ( & self , graph : & mut Graph , _children : & [ Rc < Operation > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > {
84
+ let mut nd = try!( graph. new_operation ( "Const" , & id_gen ( ) ) ) ;
85
85
try!( nd. set_attr_type ( "dtype" , DataType :: Float ) ) ;
86
86
let mut value = Tensor :: new ( & [ 1 ] ) ;
87
87
value[ 0 ] = * self ;
@@ -157,10 +157,10 @@ macro_rules! impl_bin_op {
157
157
vec![ Box :: new( self . left. clone( ) ) , Box :: new( self . right. clone( ) ) ]
158
158
}
159
159
160
- fn create_node ( & self , graph: & mut Graph , children: & [ Rc <Node >] , id_gen: & mut FnMut ( ) -> String ) -> Result <Node , Status > {
161
- let mut nd = try!( graph. new_node ( $tf_op, & id_gen( ) ) ) ;
162
- nd. add_input( Port { node : & children[ 0 ] , index: 0 } ) ;
163
- nd. add_input( Port { node : & children[ 1 ] , index: 0 } ) ;
160
+ fn create_operation ( & self , graph: & mut Graph , children: & [ Rc <Operation >] , id_gen: & mut FnMut ( ) -> String ) -> Result <Operation , Status > {
161
+ let mut nd = try!( graph. new_operation ( $tf_op, & id_gen( ) ) ) ;
162
+ nd. add_input( Port { operation : & children[ 0 ] , index: 0 } ) ;
163
+ nd. add_input( Port { operation : & children[ 1 ] , index: 0 } ) ;
164
164
nd. finish( )
165
165
}
166
166
}
@@ -213,9 +213,9 @@ impl<T: TensorType> ExprImpl<T> for Neg<T> {
213
213
vec ! [ Box :: new( self . expr. clone( ) ) ]
214
214
}
215
215
216
- fn create_node ( & self , graph : & mut Graph , children : & [ Rc < Node > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > {
217
- let mut nd = try!( graph. new_node ( "Neg" , & id_gen ( ) ) ) ;
218
- nd. add_input ( Port { node : & children[ 0 ] , index : 0 } ) ;
216
+ fn create_operation ( & self , graph : & mut Graph , children : & [ Rc < Operation > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > {
217
+ let mut nd = try!( graph. new_operation ( "Neg" , & id_gen ( ) ) ) ;
218
+ nd. add_input ( Port { operation : & children[ 0 ] , index : 0 } ) ;
219
219
nd. finish ( )
220
220
}
221
221
}
@@ -261,8 +261,8 @@ impl<T: TensorType> ExprImpl<T> for Variable<T> {
261
261
vec ! [ ]
262
262
}
263
263
264
- fn create_node ( & self , graph : & mut Graph , _children : & [ Rc < Node > ] , _id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > {
265
- let mut nd = try!( graph. new_node ( "Variable" , & self . name ) ) ;
264
+ fn create_operation ( & self , graph : & mut Graph , _children : & [ Rc < Operation > ] , _id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > {
265
+ let mut nd = try!( graph. new_operation ( "Variable" , & self . name ) ) ;
266
266
nd. set_attr_type ( "dtype" , DataType :: Float ) . unwrap ( ) ;
267
267
nd. set_attr_shape ( "shape" , & vec ! [ ] ) . unwrap ( ) ;
268
268
nd. finish ( )
@@ -310,8 +310,8 @@ impl<T: TensorType> ExprImpl<T> for Placeholder<T> {
310
310
vec ! [ ]
311
311
}
312
312
313
- fn create_node ( & self , graph : & mut Graph , _children : & [ Rc < Node > ] , _id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > {
314
- let mut nd = try!( graph. new_node ( "Placeholder" , & self . name ) ) ;
313
+ fn create_operation ( & self , graph : & mut Graph , _children : & [ Rc < Operation > ] , _id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > {
314
+ let mut nd = try!( graph. new_operation ( "Placeholder" , & self . name ) ) ;
315
315
nd. set_attr_type ( "dtype" , DataType :: Float ) . unwrap ( ) ;
316
316
nd. set_attr_shape ( "shape" , & vec ! [ ] ) . unwrap ( ) ;
317
317
nd. finish ( )
@@ -358,10 +358,10 @@ impl<T: TensorType> ExprImpl<T> for Assign<T> {
358
358
vec ! [ Box :: new( self . variable. clone( ) ) , Box :: new( self . value. clone( ) ) ]
359
359
}
360
360
361
- fn create_node ( & self , graph : & mut Graph , children : & [ Rc < Node > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > {
362
- let mut nd = try!( graph. new_node ( "Assign" , & id_gen ( ) ) ) ;
363
- nd. add_input ( Port { node : & children[ 0 ] , index : 0 } ) ;
364
- nd. add_input ( Port { node : & children[ 1 ] , index : 0 } ) ;
361
+ fn create_operation ( & self , graph : & mut Graph , children : & [ Rc < Operation > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > {
362
+ let mut nd = try!( graph. new_operation ( "Assign" , & id_gen ( ) ) ) ;
363
+ nd. add_input ( Port { operation : & children[ 0 ] , index : 0 } ) ;
364
+ nd. add_input ( Port { operation : & children[ 1 ] , index : 0 } ) ;
365
365
nd. finish ( )
366
366
}
367
367
}
@@ -372,7 +372,7 @@ impl<T: TensorType> ExprImpl<T> for Assign<T> {
372
372
pub trait AnyExpr {
373
373
fn key ( & self ) -> * const ( ) ;
374
374
fn children ( & self ) -> Vec < Box < AnyExpr > > ; // TODO: return an iterator
375
- fn create_node ( & self , graph : & mut Graph , children : & [ Rc < Node > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > ;
375
+ fn create_operation ( & self , graph : & mut Graph , children : & [ Rc < Operation > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > ;
376
376
fn clone_box ( & self ) -> Box < AnyExpr > ;
377
377
}
378
378
@@ -385,8 +385,8 @@ impl<T: TensorType> AnyExpr for Expr<T> {
385
385
self . expr . children ( )
386
386
}
387
387
388
- fn create_node ( & self , graph : & mut Graph , children : & [ Rc < Node > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Node , Status > {
389
- self . expr . create_node ( graph, children, id_gen)
388
+ fn create_operation ( & self , graph : & mut Graph , children : & [ Rc < Operation > ] , id_gen : & mut FnMut ( ) -> String ) -> Result < Operation , Status > {
389
+ self . expr . create_operation ( graph, children, id_gen)
390
390
}
391
391
392
392
fn clone_box ( & self ) -> Box < AnyExpr > {
@@ -414,45 +414,45 @@ impl Hash for Key {
414
414
415
415
pub struct Compiler < ' l > {
416
416
graph : & ' l mut Graph ,
417
- nodes : HashMap < Key , Rc < Node > > ,
417
+ operations : HashMap < Key , Rc < Operation > > ,
418
418
next_id : i32 ,
419
419
}
420
420
421
421
impl < ' l > Compiler < ' l > {
422
422
pub fn new ( graph : & ' l mut Graph ) -> Self {
423
423
Compiler {
424
424
graph : graph,
425
- nodes : HashMap :: new ( ) ,
425
+ operations : HashMap :: new ( ) ,
426
426
next_id : 0 ,
427
427
}
428
428
}
429
429
430
- pub fn compile < T : TensorType > ( & mut self , expr : Expr < T > ) -> Result < Rc < Node > , Status > {
430
+ pub fn compile < T : TensorType > ( & mut self , expr : Expr < T > ) -> Result < Rc < Operation > , Status > {
431
431
self . compile_any ( Box :: new ( expr) )
432
432
}
433
433
434
- pub fn compile_any ( & mut self , expr : Box < AnyExpr > ) -> Result < Rc < Node > , Status > {
435
- let mut child_nodes = vec ! [ ] ;
434
+ pub fn compile_any ( & mut self , expr : Box < AnyExpr > ) -> Result < Rc < Operation > , Status > {
435
+ let mut child_operations = vec ! [ ] ;
436
436
for child in expr. children ( ) {
437
437
let key = Key ( child. clone_box ( ) ) ;
438
438
// The result is mapped separately from the match statement below to avoid
439
439
// reference lifetime isues.
440
- let value = self . nodes . get ( & key) . map ( |v| v. clone ( ) ) ;
441
- child_nodes . push ( match value {
440
+ let value = self . operations . get ( & key) . map ( |v| v. clone ( ) ) ;
441
+ child_operations . push ( match value {
442
442
Some ( v) => v,
443
443
None => try!( self . compile_any ( child) ) ,
444
444
} ) ;
445
445
}
446
446
let mut next_id = self . next_id ;
447
- let result = expr. create_node ( self . graph , & child_nodes , & mut || {
448
- let id = format ! ( "node_ {}" , next_id) ;
447
+ let result = expr. create_operation ( self . graph , & child_operations , & mut || {
448
+ let id = format ! ( "operation_ {}" , next_id) ;
449
449
next_id += 1 ;
450
450
id
451
451
} ) ;
452
452
self . next_id = next_id;
453
- let node = Rc :: new ( try!( result) ) ;
454
- self . nodes . insert ( Key ( expr) , node . clone ( ) ) ;
455
- Ok ( node )
453
+ let operation = Rc :: new ( try!( result) ) ;
454
+ self . operations . insert ( Key ( expr) , operation . clone ( ) ) ;
455
+ Ok ( operation )
456
456
}
457
457
}
458
458
0 commit comments