Skip to content

Commit 2a814d5

Browse files
Fix cudf breaking change for build_column which is used to create cudf series from buffer (NVIDIA#535)
Fix cudf breaking change from rapidsai/cudf#20233 for build_column which is used to create cudf series from buffer Authors: - Ramakrishnap (https://github.com/rgsl888prabhu) Approvers: - Trevor McKay (https://github.com/tmckayus) URL: NVIDIA#535
1 parent 10f116b commit 2a814d5

6 files changed

Lines changed: 93 additions & 97 deletions

File tree

python/cuopt/cuopt/distance_engine/waypoint_matrix_wrapper.pyx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import cudf
3535
from cudf.core.buffer import as_buffer
3636
from cudf.core.column_accessor import ColumnAccessor
3737

38+
from cuopt.utilities import col_from_buf
39+
3840

3941
cdef class WaypointMatrix:
4042

@@ -107,20 +109,15 @@ cdef class WaypointMatrix:
107109
full_sequence_offset = as_buffer(full_sequence_offset)
108110
full_path = as_buffer(full_path)
109111

110-
route_df['sequence_offset'] = (
111-
cudf.core.column.build_column(
112-
full_sequence_offset,
113-
dtype=np.dtype(np.int32)
114-
)
112+
route_df['sequence_offset'] = col_from_buf(
113+
full_sequence_offset, np.int32
115114
)
116115
locations = route_df["location"].replace(
117116
to_replace=list(range(len(target_locations))),
118117
value=target_locations.tolist()
119118
)
120119
route_df['location'] = locations
121-
waypoint_seq = cudf.core.column.build_column(
122-
full_path, dtype=np.dtype(np.int32)
123-
)
120+
waypoint_seq = col_from_buf(full_path, np.int32)
124121

125122
def create_way_point_types(routes, waypoint_seq):
126123

python/cuopt/cuopt/linear_programming/solver/solver_wrapper.pyx

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ from cuopt.linear_programming.solver_settings.solver_settings import (
6969
PDLPSolverMode,
7070
SolverSettings,
7171
)
72-
from cuopt.utilities import InputValidationError
72+
from cuopt.utilities import InputValidationError, col_from_buf
7373

7474

7575
cdef extern from "cuopt/linear_programming/utilities/internals.hpp" namespace "cuopt::internals": # noqa
@@ -328,11 +328,9 @@ cdef create_solution(unique_ptr[solver_ret_t] sol_ret_ptr,
328328
num_nodes = sol_ret.mip_ret.nodes_
329329
num_simplex_iterations = sol_ret.mip_ret.simplex_iterations_
330330

331+
solution_buf = as_buffer(solution)
331332
solution = cudf.Series._from_column(
332-
cudf.core.column.build_column(
333-
as_buffer(solution),
334-
dtype=np.dtype(np.float64)
335-
)
333+
col_from_buf(solution_buf, np.float64)
336334
).to_numpy()
337335

338336
return Solution(
@@ -361,23 +359,18 @@ cdef create_solution(unique_ptr[solver_ret_t] sol_ret_ptr,
361359
dual_solution = DeviceBuffer.c_from_unique_ptr(move(sol_ret.lp_ret.dual_solution_)) # noqa
362360
reduced_cost = DeviceBuffer.c_from_unique_ptr(move(sol_ret.lp_ret.reduced_cost_)) # noqa
363361

362+
primal_solution_buf = as_buffer(primal_solution)
363+
dual_solution_buf = as_buffer(dual_solution)
364+
reduced_cost_buf = as_buffer(reduced_cost)
365+
364366
primal_solution = cudf.Series._from_column(
365-
cudf.core.column.build_column(
366-
as_buffer(primal_solution),
367-
dtype=np.dtype(np.float64)
368-
)
367+
col_from_buf(primal_solution_buf, np.float64)
369368
).to_numpy()
370369
dual_solution = cudf.Series._from_column(
371-
cudf.core.column.build_column(
372-
as_buffer(dual_solution),
373-
dtype=np.dtype(np.float64)
374-
)
370+
col_from_buf(dual_solution_buf, np.float64)
375371
).to_numpy()
376372
reduced_cost = cudf.Series._from_column(
377-
cudf.core.column.build_column(
378-
as_buffer(reduced_cost),
379-
dtype=np.dtype(np.float64)
380-
)
373+
col_from_buf(reduced_cost_buf, np.float64)
381374
).to_numpy()
382375

383376
termination_status = sol_ret.lp_ret.termination_status_
@@ -430,58 +423,51 @@ cdef create_solution(unique_ptr[solver_ret_t] sol_ret_ptr,
430423
sum_solution_weight = sol_ret.lp_ret.sum_solution_weight_
431424
iterations_since_last_restart = sol_ret.lp_ret.iterations_since_last_restart_ # noqa
432425

426+
current_primal_solution_buf = as_buffer(current_primal_solution)
427+
current_dual_solution_buf = as_buffer(current_dual_solution)
428+
initial_primal_average_buf = as_buffer(initial_primal_average)
429+
initial_dual_average_buf = as_buffer(initial_dual_average)
430+
current_ATY_buf = as_buffer(current_ATY)
431+
sum_primal_solutions_buf = as_buffer(sum_primal_solutions)
432+
sum_dual_solutions_buf = as_buffer(sum_dual_solutions)
433+
last_restart_duality_gap_primal_solution_buf = as_buffer(
434+
last_restart_duality_gap_primal_solution
435+
)
436+
last_restart_duality_gap_dual_solution_buf = as_buffer(
437+
last_restart_duality_gap_dual_solution
438+
)
439+
433440
current_primal_solution = cudf.Series._from_column(
434-
cudf.core.column.build_column(
435-
as_buffer(current_primal_solution),
436-
dtype=np.dtype(np.float64)
437-
)
441+
col_from_buf(current_primal_solution_buf, np.float64)
438442
).to_numpy()
439443
current_dual_solution = cudf.Series._from_column(
440-
cudf.core.column.build_column(
441-
as_buffer(current_dual_solution),
442-
dtype=np.dtype(np.float64)
443-
)
444+
col_from_buf(current_dual_solution_buf, np.float64)
444445
).to_numpy()
445446
initial_primal_average = cudf.Series._from_column(
446-
cudf.core.column.build_column(
447-
as_buffer(initial_primal_average),
448-
dtype=np.dtype(np.float64)
449-
)
447+
col_from_buf(initial_primal_average_buf, np.float64)
450448
).to_numpy()
451449
initial_dual_average = cudf.Series._from_column(
452-
cudf.core.column.build_column(
453-
as_buffer(initial_dual_average),
454-
dtype=np.dtype(np.float64)
455-
)
450+
col_from_buf(initial_dual_average_buf, np.float64)
456451
).to_numpy()
457452
current_ATY = cudf.Series._from_column(
458-
cudf.core.column.build_column(
459-
as_buffer(current_ATY),
460-
dtype=np.dtype(np.float64)
461-
)
453+
col_from_buf(current_ATY_buf, np.float64)
462454
).to_numpy()
463455
sum_primal_solutions = cudf.Series._from_column(
464-
cudf.core.column.build_column(
465-
as_buffer(sum_primal_solutions),
466-
dtype=np.dtype(np.float64)
467-
)
456+
col_from_buf(sum_primal_solutions_buf, np.float64)
468457
).to_numpy()
469458
sum_dual_solutions = cudf.Series._from_column(
470-
cudf.core.column.build_column(
471-
as_buffer(sum_dual_solutions),
472-
dtype=np.dtype(np.float64)
473-
)
459+
col_from_buf(sum_dual_solutions_buf, np.float64)
474460
).to_numpy()
475461
last_restart_duality_gap_primal_solution = cudf.Series._from_column( # noqa
476-
cudf.core.column.build_column(
477-
as_buffer(last_restart_duality_gap_primal_solution),
478-
dtype=np.dtype(np.float64)
462+
col_from_buf(
463+
last_restart_duality_gap_primal_solution_buf,
464+
np.float64
479465
)
480466
).to_numpy()
481467
last_restart_duality_gap_dual_solution = cudf.Series._from_column(
482-
cudf.core.column.build_column(
483-
as_buffer(last_restart_duality_gap_dual_solution),
484-
dtype=np.dtype(np.float64)
468+
col_from_buf(
469+
last_restart_duality_gap_dual_solution_buf,
470+
np.float64
485471
)
486472
).to_numpy()
487473

python/cuopt/cuopt/routing/utils_wrapper.pyx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ from cudf.core.buffer import as_buffer
3737

3838
from libcpp.utility cimport move
3939

40+
from cuopt.utilities import col_from_buf
41+
4042

4143
class DatasetDistribution(IntEnum):
4244
CLUSTERED = dataset_distribution_t.CLUSTERED
@@ -113,8 +115,8 @@ def generate_dataset(locations=100, asymmetric=True, min_demand=cudf.Series(),
113115
y_pos = DeviceBuffer.c_from_unique_ptr(move(g_ret.d_y_pos_))
114116
x_pos = as_buffer(x_pos)
115117
y_pos = as_buffer(y_pos)
116-
coordinates['x'] = cudf.core.column.build_column(x_pos, dtype=np.float32)
117-
coordinates['y'] = cudf.core.column.build_column(y_pos, dtype=np.float32)
118+
coordinates['x'] = col_from_buf(x_pos, np.float32)
119+
coordinates['y'] = col_from_buf(y_pos, np.float32)
118120

119121
matrices_buf = as_buffer(
120122
DeviceBuffer.c_from_unique_ptr(move(g_ret.d_matrices_))
@@ -144,17 +146,13 @@ def generate_dataset(locations=100, asymmetric=True, min_demand=cudf.Series(),
144146
vehicle_latest = as_buffer(vehicle_latest)
145147
vehicle_drop_return_trips = as_buffer(vehicle_drop_return_trips)
146148
vehicle_skip_first_trips = as_buffer(vehicle_skip_first_trips)
147-
vehicles["earliest_time"] = cudf.core.column.build_column(
148-
vehicle_earliest, dtype=np.int32
149-
)
150-
vehicles["latest_time"] = cudf.core.column.build_column(
151-
vehicle_latest, dtype=np.int32
152-
)
153-
vehicles["drop_return_trips"] = cudf.core.column.build_column(
154-
vehicle_drop_return_trips, dtype=np.bool_
149+
vehicles["earliest_time"] = col_from_buf(vehicle_earliest, np.int32)
150+
vehicles["latest_time"] = col_from_buf(vehicle_latest, np.int32)
151+
vehicles["drop_return_trips"] = col_from_buf(
152+
vehicle_drop_return_trips, np.bool_
155153
)
156-
vehicles["skip_first_trips"] = cudf.core.column.build_column(
157-
vehicle_skip_first_trips, dtype=np.bool_
154+
vehicles["skip_first_trips"] = col_from_buf(
155+
vehicle_skip_first_trips, np.bool_
158156
)
159157

160158
fleet_size = vehicles["earliest_time"].shape[0]
@@ -192,12 +190,8 @@ def generate_dataset(locations=100, asymmetric=True, min_demand=cudf.Series(),
192190
)
193191
latest_time = as_buffer(latest_time)
194192

195-
orders["earliest_time"] = cudf.core.column.build_column(
196-
earliest_time, dtype=np.int32
197-
)
198-
orders["latest_time"] = cudf.core.column.build_column(
199-
latest_time, dtype=np.int32
200-
)
193+
orders["earliest_time"] = col_from_buf(earliest_time, np.int32)
194+
orders["latest_time"] = col_from_buf(latest_time, np.int32)
201195

202196
demands_buf = as_buffer(
203197
DeviceBuffer.c_from_unique_ptr(move(g_ret.d_demands_))

python/cuopt/cuopt/routing/vehicle_routing_wrapper.pyx

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ from numba import cuda
5959
import cudf
6060
from cudf.core.buffer import as_buffer
6161

62+
from cuopt.utilities import col_from_buf
63+
6264

6365
class ErrorStatus(IntEnum):
6466
Success = error_type_t.Success
@@ -809,29 +811,17 @@ def Solve(DataModel data_model, SolverSettings solver_settings):
809811
accepted = as_buffer(accepted)
810812

811813
route_df = cudf.DataFrame()
812-
route_df['route'] = cudf.core.column.build_column(
813-
route, dtype=np.dtype(np.int32)
814-
)
815-
route_df['arrival_stamp'] = cudf.core.column.build_column(
816-
arrival_stamp, dtype=np.dtype(np.float64)
817-
)
818-
route_df['truck_id'] = cudf.core.column.build_column(
819-
truck_id, dtype=np.dtype(np.int32)
820-
)
821-
route_df['location'] = cudf.core.column.build_column(
822-
route_locations, dtype=np.dtype(np.int32)
823-
)
824-
route_df['type'] = cudf.core.column.build_column(
825-
node_types, dtype=np.dtype(np.int32)
826-
)
814+
route_df['route'] = col_from_buf(route, np.int32)
815+
route_df['arrival_stamp'] = col_from_buf(arrival_stamp, np.float64)
816+
route_df['truck_id'] = col_from_buf(truck_id, np.int32)
817+
route_df['location'] = col_from_buf(route_locations, np.int32)
818+
route_df['type'] = col_from_buf(node_types, np.int32)
827819

828820
unserviced_nodes = cudf.Series._from_column(
829-
cudf.core.column.build_column(
830-
unserviced_nodes, dtype=np.dtype(np.int32)
831-
)
821+
col_from_buf(unserviced_nodes, np.int32)
832822
)
833823
accepted = cudf.Series._from_column(
834-
cudf.core.column.build_column(accepted, dtype=np.dtype(np.int32))
824+
col_from_buf(accepted, np.int32)
835825
)
836826

837827
def get_type_from_int(type_in_int):

python/cuopt/cuopt/utilities/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
catch_cuopt_exception,
2121
)
2222
from cuopt.utilities.type_casting import type_cast
23-
from cuopt.utilities.utils import check_solution
23+
from cuopt.utilities.utils import check_solution, col_from_buf

python/cuopt/cuopt/utilities/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,42 @@
1515

1616
import numpy as np
1717

18+
import cudf
19+
1820
from cuopt.linear_programming.solver.solver_parameters import (
1921
CUOPT_ABSOLUTE_PRIMAL_TOLERANCE,
2022
CUOPT_MIP_INTEGRALITY_TOLERANCE,
2123
CUOPT_RELATIVE_PRIMAL_TOLERANCE,
2224
)
2325

2426

27+
def col_from_buf(buf, dtype):
28+
"""Helper function to create a cudf column from a buffer.
29+
30+
Parameters
31+
----------
32+
buf : cudf.core.buffer.Buffer
33+
The buffer containing the data
34+
dtype : numpy.dtype or type
35+
The data type for the column
36+
37+
Returns
38+
-------
39+
cudf.core.column.Column
40+
A cudf column built from the buffer
41+
"""
42+
dt = np.dtype(dtype)
43+
return cudf.core.column.build_column(
44+
buf,
45+
dtype=dt,
46+
size=buf.size // dt.itemsize,
47+
mask=None,
48+
offset=0,
49+
null_count=0,
50+
children=(),
51+
)
52+
53+
2554
def validate_variable_bounds(data, settings, solution):
2655
integrality_tolerance = settings.get_parameter(
2756
CUOPT_MIP_INTEGRALITY_TOLERANCE

0 commit comments

Comments
 (0)