Skip to content

Commit 7ab4b6e

Browse files
committed
Initial memory optimisations
1 parent c60ab4b commit 7ab4b6e

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/segger/cli/segment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def segment(
154154
group=group_prediction,
155155
)] = registry.get_default("prediction_graph_max_k"),
156156

157-
prediction_expansion_ratio: Annotated[float | None, registry.get_parameter(
157+
prediction_graph_buffer_ratio: Annotated[float | None, registry.get_parameter(
158158
"prediction_graph_buffer_ratio",
159159
validator=validators.Number(gt=0),
160160
group=group_prediction,
@@ -323,7 +323,7 @@ def segment(
323323
transcripts_graph_max_dist=transcripts_max_dist,
324324
prediction_graph_mode=prediction_mode,
325325
prediction_graph_max_k=prediction_max_k,
326-
prediction_graph_buffer_ratio=prediction_expansion_ratio,
326+
prediction_graph_buffer_ratio=prediction_graph_buffer_ratio,
327327
tiling_margin_training=tiling_margin_training,
328328
tiling_margin_prediction=tiling_margin_prediction,
329329
tiling_nodes_per_tile=max_nodes_per_tile,

src/segger/data/tiling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def __init__(
199199
):
200200
# Calculate QuadTree on points and set as tiles
201201
points = points_to_geoseries(positions, backend='cuspatial')
202-
_, quadtree = get_quadtree_index(
202+
_, quadtree, _ = get_quadtree_index(
203203
points,
204204
max_tile_size,
205205
with_bounds=True,

src/segger/geometry/quadtree.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ def get_quadtree_kwargs(
2525
A dictionary of keyword arguments including x_min, x_max, y_min,
2626
y_max, scale, and max_depth.
2727
"""
28-
# Calculate bounds
29-
x_min = float(points.points.x.min())
30-
x_max = float(points.points.x.max())
31-
y_min = float(points.points.y.min())
32-
y_max = float(points.points.y.max())
33-
34-
# Get hyperparams for quadtree
28+
# Calculate bounds | Optimisation: Use interleaved view, without copying data
29+
xy = cp.asarray(points.points.xy).reshape(-1, 2) # zero-copy view
30+
x_min = float(xy[:, 0].min())
31+
x_max = float(xy[:, 0].max())
32+
y_min = float(xy[:, 1].min())
33+
y_max = float(xy[:, 1].max())
34+
35+
# Get hyperparams for quadtree
3536
extent = max(x_max - x_min, y_max - y_min)
3637
max_depth = 1
3738
while extent // (1 << max_depth) > 0:
@@ -140,7 +141,7 @@ def get_quadtree_index(
140141
points: cuspatial.GeoSeries,
141142
max_size: int,
142143
with_bounds: bool = True,
143-
) -> tuple[cudf.Series, cudf.DataFrame]:
144+
) -> tuple[cudf.Series, cudf.DataFrame, dict]:
144145
"""Build a cuSpatial quadtree from 2D point data.
145146
146147
Parameters
@@ -190,7 +191,7 @@ def get_quadtree_index(
190191
y_max=y_max,
191192
)
192193

193-
return indices, quadtree
194+
return indices, quadtree, kwargs
194195

195196

196197
def quadtree_to_geoseries(

src/segger/geometry/query.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import cuspatial
55
import cudf
6+
import cupy as cp
67

78
from .conversion import (
89
polygons_to_geoseries,
@@ -47,14 +48,15 @@ def _points_in_polygons_contains(
4748
mapping each contained point to its containing polygon.
4849
"""
4950
# Setup inputs for spatial join
51+
cp.get_default_memory_pool().free_all_blocks()
52+
cp.get_default_pinned_memory_pool().free_all_blocks()
5053
if max_size is None:
5154
max_size = 10000 if len(points) > 5e7 else 1000 # heuristic
52-
point_indices, quadtree = get_quadtree_index(
55+
point_indices, quadtree, kwargs = get_quadtree_index(
5356
points,
5457
max_size,
5558
with_bounds=False
5659
)
57-
kwargs = get_quadtree_kwargs(points)
5860

5961
# Perform spatial join in batches
6062
batch_idx = np.linspace(0, len(polygons), (batches or 1) + 1, dtype=int)

0 commit comments

Comments
 (0)