Skip to content

Commit 2dd67cc

Browse files
authored
Simplify cpu_to_gpu and gpu_to_cpu (#6395)
This is fixing a few warts I noticed when fixing a bug in some related code a while ago. In particular, there's no reason to store the desired order as an attribute on the cls as `{name}_order` (added [here](https://github.com/rapidsai/cuml/pull/4918/files#r1012248790)) - you can just pull it off the descriptor directly when needed. The rest is some pythonic improvements, but logic-wise everything should be the same as it was before. Authors: - Jim Crist-Harif (https://github.com/jcrist) Approvers: - Tim Head (https://github.com/betatim) - Victor Lafargue (https://github.com/viclafargue) URL: #6395
1 parent e26dd05 commit 2dd67cc

2 files changed

Lines changed: 34 additions & 49 deletions

File tree

python/cuml/cuml/common/array_descriptor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2020-2023, NVIDIA CORPORATION.
2+
# Copyright (c) 2020-2025, NVIDIA CORPORATION.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -66,7 +66,6 @@ def __init__(self, order="K"):
6666

6767
def __set_name__(self, owner, name):
6868
self.name = name
69-
setattr(owner, name + "_order", self.order)
7069

7170
def _get_meta(
7271
self, instance, throw_on_missing=False

python/cuml/cuml/internals/base.pyx

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ except ImportError:
3939
import cuml
4040
import cuml.common
4141
from cuml.common.sparse_utils import is_sparse
42+
from cuml.common.array_descriptor import CumlArrayDescriptor
4243
import cuml.internals.logger as logger
4344
import cuml.internals
4445
from 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

Comments
 (0)