@@ -697,29 +697,27 @@ void kmeans_fit(
697697
698698 rmm::device_uvector<char > batch_workspace (device_buffer_samples, stream);
699699
700- auto large_workspace_mr = raft::resource::get_large_workspace_resource_ref (handle);
701- auto data_batches =
702- cuvs::spatial::knn::detail::utils::make_batch_load_iterator<DataT>(
703- handle,
704- X.data_handle (),
705- n_samples,
706- n_features,
707- device_buffer_samples,
708- stream,
709- large_workspace_mr);
700+ auto batch_memory = raft::resource::get_workspace_resource_ref (handle);
701+ if constexpr (!data_on_device) {
702+ size_t batch_staging_bytes =
703+ static_cast <size_t >(device_buffer_samples) * static_cast <size_t >(n_features) * sizeof (DataT);
704+ if (weight_ptr != nullptr ) {
705+ batch_staging_bytes += static_cast <size_t >(device_buffer_samples) * sizeof (DataT);
706+ }
707+ if (batch_staging_bytes > raft::resource::get_workspace_free_bytes (handle)) {
708+ batch_memory = raft::resource::get_large_workspace_resource_ref (handle);
709+ }
710+ }
711+
712+ auto data_batches = cuvs::spatial::knn::detail::utils::make_batch_load_iterator<DataT>(
713+ handle, X.data_handle (), n_samples, n_features, device_buffer_samples, stream, batch_memory);
710714 // Host-path weight batches: only materialized when weights are provided and
711715 // the data resides on host
712716 std::optional<cuvs::spatial::knn::detail::utils::batch_load_iterator_dyn<DataT>> weight_batches;
713717 if constexpr (!data_on_device) {
714718 if (weight_ptr != nullptr ) {
715719 weight_batches = cuvs::spatial::knn::detail::utils::make_batch_load_iterator<DataT>(
716- handle,
717- weight_ptr,
718- n_samples,
719- IndexT{1 },
720- device_buffer_samples,
721- stream,
722- large_workspace_mr);
720+ handle, weight_ptr, n_samples, IndexT{1 }, device_buffer_samples, stream, batch_memory);
723721 } else {
724722 raft::matrix::fill (handle, batch_weights_buf.view (), DataT{1 });
725723 }
0 commit comments