@@ -46,6 +46,35 @@ private static Tensor _constant_if_small<T>(T value, Shape shape, TF_DataType dt
4646 }
4747 }
4848
49+ public static Tensor _autopacking_conversion_function ( object [ ] v , TF_DataType dtype = TF_DataType . DtInvalid , string name = null , bool as_ref = false )
50+ {
51+ var inferred_dtype = _get_dtype_from_nested_lists ( v ) ;
52+ if ( dtype == TF_DataType . DtInvalid )
53+ dtype = inferred_dtype ;
54+
55+ return _autopacking_helper ( v , dtype , name == null ? "packed" : name ) ;
56+ }
57+
58+ private static TF_DataType _get_dtype_from_nested_lists ( object [ ] list_or_tuple )
59+ {
60+ TF_DataType dtype = TF_DataType . DtInvalid ;
61+
62+ foreach ( var obj in list_or_tuple )
63+ {
64+ switch ( obj )
65+ {
66+ case Tensor t :
67+ dtype = t . dtype . as_base_dtype ( ) ;
68+ break ;
69+ }
70+
71+ if ( dtype != TF_DataType . DtInvalid )
72+ break ;
73+ }
74+
75+ return dtype ;
76+ }
77+
4978 public static Tensor _autopacking_helper ( object [ ] list_or_tuple , TF_DataType dtype , string name )
5079 {
5180 var must_pack = false ;
@@ -242,32 +271,21 @@ private static Tensor shape_internal(Tensor input, string name = null, bool opti
242271
243272 private static Tensor size_internal ( Tensor input , string name = null , bool optimize = true , TF_DataType out_type = TF_DataType . TF_INT32 )
244273 {
245- return with ( ops . name_scope ( name , "Size" , new Tensor [ ] { input } ) , scope =>
274+ return with ( ops . name_scope ( name , "Size" , new { input } ) , scope =>
246275 {
247276 name = scope ;
248277
249- if ( ! tf . context . executing_eagerly ( ) )
278+ var input_tensor = ops . convert_to_tensor ( input ) ;
279+ var input_shape = tensor_util . to_shape ( input_tensor . shape ) ;
280+ if ( optimize )
250281 {
251- var input_tensor = ops . convert_to_tensor ( input ) ;
252- var input_shape = tensor_util . to_shape ( input_tensor . shape ) ;
253- if ( optimize )
282+ if ( input_shape . is_fully_defined ( ) )
254283 {
255- if ( input_shape . is_fully_defined ( ) )
256- {
257- var nd = np . array ( input_tensor . shape , out_type . as_numpy_datatype ( ) ) ;
258- return constant_op . constant ( nd , name : name ) ;
259- }
284+ return constant_op . constant ( input_shape . Size , dtype : out_type , name : name ) ;
260285 }
261-
262- return gen_array_ops . size ( input , name : name , out_type : out_type ) ;
263- }
264- else
265- {
266- // result = gen_array_ops.shape();
267- throw new NotImplementedException ( "array_ops.size_internal" ) ;
268286 }
269287
270- return null ;
288+ return gen_array_ops . size ( input , name : name , out_type : out_type ) ;
271289 } ) ;
272290 }
273291
0 commit comments