@@ -1556,19 +1556,18 @@ def transform(t, device, dtype, blocking):
15561556 if dtype is None :
15571557 dtype = t .dtype
15581558
1559+ if type (dtype ) is str :
1560+ dtype = convert_np_dtype_to_dtype_ (dtype )
1561+
15591562 # 1. gpu place need to determine whether the memory is sufficient for allocation:
15601563 if t .place .is_gpu_place ():
1561- gpu_memory_available = core .gpu_memory_available ()
15621564 # for gpu, minimum memory allocation unit is 256 bytes.
1563- if type (dtype ) is str :
1564- size_dtype = core .size_of_dtype (
1565- convert_np_dtype_to_dtype_ (dtype ))
1566- else :
1567- size_dtype = core .size_of_dtype (dtype )
1565+ size_dtype = core .size_of_dtype (dtype )
15681566 # Note(zhangbo): Paddle GPU minimum memory allocation unit is 256 bytes, waiting_alloc_memory will comput ‘t’ occupied memory space.
15691567 # Coefficient 1.2 is used to avoid OOM that may occur in this critical state when the memory is just enough.
15701568 waiting_alloc_memory = (
1571- (t .numel ().numpy ()[0 ] * size_dtype ) / 256 + 1 ) * 256 * 1.2
1569+ (np .prod (t .shape ) * size_dtype ) / 256 + 1 ) * 256 * 1.2
1570+ gpu_memory_available = core .gpu_memory_available ()
15721571 if gpu_memory_available < waiting_alloc_memory :
15731572 # Copy param / Tensor to cpu
15741573 t_used = t ._copy_to (paddle .CPUPlace (),
@@ -1582,26 +1581,17 @@ def transform(t, device, dtype, blocking):
15821581
15831582 # 2. cast param / Tensor to dtype
15841583 if dtype is not None and dtype != t_used .dtype :
1585- if isinstance (t_used , framework .ParamBase ):
1586- from paddle .fluid .layer_helper import LayerHelper
1587- helper = LayerHelper ("cast" , ** locals ())
1588- t_casted = helper .create_variable_for_type_inference (
1589- dtype = dtype )
1590- framework ._dygraph_tracer ().trace_op (
1591- type = 'cast' ,
1592- inputs = {'X' : t_used },
1593- outputs = {'Out' : t_casted },
1594- attrs = {
1595- 'in_dtype' : t_used .dtype ,
1596- 'out_dtype' : convert_np_dtype_to_dtype_ (dtype )
1597- })
1598- else :
1584+ with paddle .fluid .framework ._dygraph_place_guard (
1585+ place = t_used .place ):
15991586 t_casted = t_used .cast (dtype = dtype )
16001587 else :
16011588 t_casted = t_used
16021589
16031590 # 3. Copy casted cpu param / Tensor to device
1604- new_t = t_casted ._copy_to (device , blocking )
1591+ if device is not None and not t_casted .place ._equals (device ):
1592+ new_t = t_casted ._copy_to (device , blocking )
1593+ else :
1594+ new_t = t_casted
16051595
16061596 # 4. share Tensor to origin param / Tensor
16071597 dst_tensor = t .value ().get_tensor ()
0 commit comments