@@ -39,6 +39,7 @@ except ImportError:
3939import cuml
4040import cuml.common
4141from cuml.common.sparse_utils import is_sparse
42+ from cuml.common.array_descriptor import CumlArrayDescriptor
4243import cuml.internals.logger as logger
4344import cuml.internals
4445from cuml.internals import api_context_managers
@@ -664,55 +665,40 @@ class UniversalBase(Base):
664665 self ._cpu_model = self ._cpu_model_class(** filtered_kwargs)
665666
666667 def gpu_to_cpu (self ):
667- # transfer attributes from GPU to CPU estimator
668- for attr in self .get_attr_names():
669- if hasattr ( self , attr) :
670- cu_attr = getattr (self , attr )
671- if isinstance (cu_attr, CumlArray) :
672- # transform cumlArray to numpy and set it
673- # as an attribute in the CPU estimator
674- setattr ( self ._cpu_model, attr, cu_attr.to_output( ' numpy ' ))
675- elif isinstance (cu_attr, cp_ndarray):
676- # transform cupy to numpy and set it
677- # as an attribute in the CPU estimator
678- setattr ( self ._cpu_model, attr, cp.asnumpy(cu_attr))
679- else :
680- # transfer all other types of attributes directly
681- setattr (self ._cpu_model, attr, cu_attr )
668+ """ Transfer attributes from GPU estimator to CPU estimator. """
669+ for name in self .get_attr_names():
670+ try :
671+ value = getattr (self , name )
672+ except AttributeError :
673+ # Skip missing attributes
674+ continue
675+
676+ # Coerce all arrays to numpy
677+ if isinstance (value, CumlArray):
678+ value = value.to_output( " numpy " )
679+ elif isinstance (value, cp_ndarray):
680+ value = cp.asnumpy(value)
681+
682+ setattr (self ._cpu_model, name, value )
682683
683684 def cpu_to_gpu (self ):
684- # transfer attributes from CPU to GPU estimator
685- with using_memory_type(
686- (MemoryType.host, MemoryType.device)[
687- is_cuda_available()
688- ]
689- ):
690- for attr in self .get_attr_names():
691- if hasattr (self ._cpu_model, attr):
692- cpu_attr = getattr (self ._cpu_model, attr)
693- # if the cpu attribute is an array
694- if isinstance (cpu_attr, np.ndarray):
695- # get data order wished for by
696- # CumlArrayDescriptor
697- if hasattr (self , attr + ' _order' ):
698- order = getattr (self , attr + ' _order' )
699- else :
700- order = ' K'
701- # transfer array to gpu and set it as a cuml
702- # attribute
703- cuml_array = input_to_cuml_array(
704- cpu_attr,
705- order = order,
706- convert_to_mem_type = (
707- MemoryType.host,
708- MemoryType.device
709- )[is_cuda_available()]
710- )[0 ]
711- setattr (self , attr, cuml_array)
712- else :
713- # transfer all other types of attributes
714- # directly
715- setattr (self , attr, cpu_attr)
685+ """ Transfer attributes from CPU estimator to GPU estimator."""
686+ mem_type = MemoryType.device if is_cuda_available() else MemoryType.host
687+ with using_memory_type(mem_type):
688+ for name in self .get_attr_names():
689+ try :
690+ value = getattr (self ._cpu_model, name)
691+ except AttributeError :
692+ # Skip missing attributes
693+ continue
694+
695+ if isinstance (value, np.ndarray):
696+ # Coerce arrays to CumlArrays with the proper order
697+ descriptor = getattr (type (self ), name, None )
698+ order = descriptor.order if isinstance (descriptor, CumlArrayDescriptor) else " K"
699+ value = input_to_cuml_array(value, order = order, convert_to_mem_type = mem_type)[0 ]
700+
701+ setattr (self , name, value)
716702
717703 def args_to_cpu (self , *args , **kwargs ):
718704 # put all the args on host
0 commit comments