diff --git a/cpp/include/nvtext/edit_distance.hpp b/cpp/include/nvtext/edit_distance.hpp index b80c38e806c2..cad6fd47b0cb 100644 --- a/cpp/include/nvtext/edit_distance.hpp +++ b/cpp/include/nvtext/edit_distance.hpp @@ -54,46 +54,5 @@ std::unique_ptr edit_distance( rmm::cuda_stream_view stream = cudf::get_default_stream(), rmm::device_async_resource_ref mr = cudf::get_current_device_resource_ref()); -/** - * @brief Compute the edit distance between all the strings in the input column. - * - * @deprecated Deprecated since release 26.04 - * - * This uses the Levenshtein algorithm to calculate the edit distance between - * two strings as documented here: https://www.cuelogic.com/blog/the-levenshtein-algorithm - * - * The output is essentially a `input.size() x input.size()` square matrix of integers. - * All values at diagonal `row == col` are 0 since the edit distance between two identical - * strings is zero. All values above the diagonal are reflected below since the edit distance - * calculation is also commutative. - * - * @code{.pseudo} - * Example: - * s = ["hello", "hallo", "hella"] - * d = edit_distance_matrix(s) - * d is now [[0, 1, 1], - * [1, 0, 2] - * [1, 2, 0]] - * @endcode - * - * Null entries for `input` are ignored and the edit distance - * is computed as though the null entry is an empty string. - * - * The output is a lists column of size `input.size()` and where each list item - * is `input.size()` elements. - * - * @throw std::invalid_argument if `input.size() == 1` - * @throw std::overflow_error if `input.size() * input.size()` greater than max size_type - * - * @param input Strings column of input strings - * @param stream CUDA stream used for device memory operations and kernel launches - * @param mr Device memory resource used to allocate the returned column's device memory - * @return New lists column of edit distance values - */ -[[deprecated]] std::unique_ptr edit_distance_matrix( - cudf::strings_column_view const& input, - rmm::cuda_stream_view stream = cudf::get_default_stream(), - rmm::device_async_resource_ref mr = cudf::get_current_device_resource_ref()); - /** @} */ // end of group } // namespace CUDF_EXPORT nvtext diff --git a/cpp/src/text/edit_distance.cu b/cpp/src/text/edit_distance.cu index 7cf3ace54dab..0598355f646b 100644 --- a/cpp/src/text/edit_distance.cu +++ b/cpp/src/text/edit_distance.cu @@ -37,50 +37,6 @@ namespace nvtext { namespace detail { namespace { -/** - * @brief Compute the Levenshtein distance for each string pair - * - * Documentation here: https://www.cuelogic.com/blog/the-levenshtein-algorithm - * And here: https://en.wikipedia.org/wiki/Levenshtein_distance - * - * @param d_str First string - * @param d_tgt Second string - * @param buffer Working buffer for intermediate calculations - * @return The edit distance value - */ -__device__ cudf::size_type compute_distance(cudf::string_view const& d_str, - cudf::string_view const& d_tgt, - cudf::size_type* buffer) -{ - auto const str_length = d_str.length(); - auto const tgt_length = d_tgt.length(); - if (str_length == 0) return tgt_length; - if (tgt_length == 0) return str_length; - - auto begin = str_length < tgt_length ? d_str.begin() : d_tgt.begin(); - auto itr = str_length < tgt_length ? d_tgt.begin() : d_str.begin(); - auto const n = cuda::std::min(str_length, tgt_length); - auto const m = cuda::std::max(str_length, tgt_length); - // setup compute buffer pointers - auto v0 = buffer; - auto v1 = v0 + n + 1; - // initialize v0 - thrust::sequence(thrust::seq, v0, v1); - - for (int i = 0; i < m; ++i, ++itr) { - auto itr_tgt = begin; - v1[0] = i + 1; - for (int j = 0; j < n; ++j, ++itr_tgt) { - auto sub_cost = v0[j] + (*itr != *itr_tgt); - auto del_cost = v0[j + 1] + 1; - auto ins_cost = v1[j] + 1; - v1[j + 1] = cuda::std::min(cuda::std::min(sub_cost, del_cost), ins_cost); - } - cuda::std::swap(v0, v1); - } - return v0[n]; -} - constexpr cudf::size_type row_pad_size = 2; // each row has potentially 2 extra values struct calculate_compute_buffer_fn { @@ -322,120 +278,6 @@ std::unique_ptr edit_distance(cudf::strings_column_view const& inp return results; } -namespace { -struct edit_distance_matrix_levenshtein_algorithm { - cudf::column_device_view d_strings; // computing these against itself - cudf::size_type* d_buffer; // compute buffer for each string - std::ptrdiff_t const* d_offsets; // locate sub-buffer for each string - cudf::size_type* d_results; // edit distance values - - __device__ void operator()(cudf::size_type idx) const - { - auto const strings_count = d_strings.size(); - auto const row = idx / strings_count; - auto const col = idx % strings_count; - if (row > col) return; // bottom half is computed with the top half of matrix - cudf::string_view d_str1 = - d_strings.is_null(row) ? cudf::string_view{} : d_strings.element(row); - cudf::string_view d_str2 = - d_strings.is_null(col) ? cudf::string_view{} : d_strings.element(col); - auto work_buffer = d_buffer + d_offsets[idx - ((row + 1L) * (row + 2L)) / 2L]; - auto const distance = (row == col) ? 0 : compute_distance(d_str1, d_str2, work_buffer); - d_results[idx] = distance; // top half of matrix - d_results[col * strings_count + row] = distance; // bottom half of matrix - } -}; - -struct calculate_matrix_compute_buffer_fn { - cudf::column_device_view d_strings; - std::ptrdiff_t* d_sizes; - - __device__ void operator()(cudf::size_type idx) const - { - auto const row = idx / d_strings.size(); - auto const col = idx % d_strings.size(); - if (row >= col) { return; } // compute only the top half - cudf::string_view const d_str1 = - d_strings.is_null(row) ? cudf::string_view{} : d_strings.element(row); - cudf::string_view const d_str2 = - d_strings.is_null(col) ? cudf::string_view{} : d_strings.element(col); - if (d_str1.empty() || d_str2.empty()) { return; } - // the temp size needed is 2 integers per character of the shorter string - d_sizes[idx - ((row + 1L) * (row + 2L)) / 2L] = - (cuda::std::min(d_str1.length(), d_str2.length()) + 1L) * 2L; - } -}; - -} // namespace - -/** - * @copydoc nvtext::edit_distance_matrix - */ -std::unique_ptr edit_distance_matrix(cudf::strings_column_view const& input, - rmm::cuda_stream_view stream, - rmm::device_async_resource_ref mr) -{ - auto const output_type = cudf::data_type{cudf::type_to_id()}; - if (input.is_empty()) { return cudf::make_empty_column(output_type); } - CUDF_EXPECTS( - input.size() > 1, "the input strings must include at least 2 strings", std::invalid_argument); - CUDF_EXPECTS(input.size() * static_cast(input.size()) < - static_cast(std::numeric_limits().max()), - "too many strings to create the output column", - std::overflow_error); - - // create device column of the input strings column - auto d_strings = cudf::column_device_view::create(input.parent(), stream); - - // Calculate the size of the compute-buffer. - // We only need memory for half the size of the output matrix since the edit distance calculation - // is commutative -- `distance(strings[i],strings[j]) == distance(strings[j],strings[i])` - auto const n_upper = (input.size() * (input.size() - 1L)) / 2L; - auto const output_size = input.size() * input.size(); - rmm::device_uvector offsets(n_upper + 1, stream); - thrust::uninitialized_fill( - rmm::exec_policy_nosync(stream, cudf::get_current_device_resource_ref()), - offsets.begin(), - offsets.end(), - 0); - thrust::for_each_n(rmm::exec_policy_nosync(stream, cudf::get_current_device_resource_ref()), - cuda::counting_iterator{0}, - output_size, - calculate_matrix_compute_buffer_fn{*d_strings, offsets.data()}); - - // get the total size for the compute buffer - // and convert sizes to offsets in-place - auto const compute_size = - cudf::detail::sizes_to_offsets(offsets.begin(), offsets.end(), offsets.begin(), 0, stream); - - // create the compute buffer - rmm::device_uvector compute_buffer(compute_size, stream); - auto d_buffer = compute_buffer.data(); - - // compute the edit distance into the output column - auto results = cudf::make_fixed_width_column( - output_type, output_size, rmm::device_buffer{0, stream, mr}, 0, stream, mr); - auto d_results = results->mutable_view().data(); - thrust::for_each_n( - rmm::exec_policy_nosync(stream, cudf::get_current_device_resource_ref()), - cuda::counting_iterator{0}, - output_size, - edit_distance_matrix_levenshtein_algorithm{*d_strings, d_buffer, offsets.data(), d_results}); - - // build a lists column of the results - auto offsets_column = - cudf::detail::sequence(input.size() + 1, - cudf::numeric_scalar(0, true, stream), - cudf::numeric_scalar(input.size(), true, stream), - stream, - mr); - return cudf::make_lists_column(input.size(), - std::move(offsets_column), - std::move(results), - 0, // no nulls - rmm::device_buffer{0, stream, mr}); -} - } // namespace detail // external APIs @@ -452,15 +294,4 @@ std::unique_ptr edit_distance(cudf::strings_column_view const& inp return detail::edit_distance(input, targets, stream, mr); } -/** - * @copydoc nvtext::edit_distance_matrix - */ -std::unique_ptr edit_distance_matrix(cudf::strings_column_view const& input, - rmm::cuda_stream_view stream, - rmm::device_async_resource_ref mr) -{ - CUDF_FUNC_RANGE(); - return detail::edit_distance_matrix(input, stream, mr); -} - } // namespace nvtext diff --git a/docs/cudf/source/cudf/api_docs/series.rst b/docs/cudf/source/cudf/api_docs/series.rst index e6ecff3a9562..6b809eb233fa 100644 --- a/docs/cudf/source/cudf/api_docs/series.rst +++ b/docs/cudf/source/cudf/api_docs/series.rst @@ -354,7 +354,6 @@ String handling count detokenize edit_distance - edit_distance_matrix endswith extract filter_alphanum diff --git a/python/cudf/cudf/core/accessors/string.py b/python/cudf/cudf/core/accessors/string.py index 01ca1b37abf7..c25fc28bd888 100644 --- a/python/cudf/cudf/core/accessors/string.py +++ b/python/cudf/cudf/core/accessors/string.py @@ -5371,45 +5371,6 @@ def edit_distance(self, targets) -> Series | Index: self._column.edit_distance(targets_column) # type: ignore[arg-type] ) - def edit_distance_matrix(self) -> Series | Index: - """Computes the edit distance between strings in the series. - - The series to compute the matrix should have more than 2 strings and - should not contain nulls. - - Edit distance is measured based on the `Levenshtein edit distance - algorithm `_. - - Returns - ------- - Series of ListDtype(int64) - Assume ``N`` is the length of this series. The return series - contains ``N`` lists of size ``N``, where the ``j`` th number in - the ``i`` th row of the series tells the edit distance between the - ``i`` th string and the ``j`` th string of this series. The matrix - is symmetric. Diagonal elements are 0. - - Examples - -------- - >>> import cudf - >>> s = cudf.Series(['abc', 'bc', 'cba']) - >>> s.str.edit_distance_matrix() - 0 [0, 1, 2] - 1 [1, 0, 2] - 2 [2, 2, 0] - dtype: list - """ - if self._column.size < 2: - raise ValueError( - "Require size >= 2 to compute edit distance matrix." - ) - if self._column.has_nulls(): - raise ValueError( - "Cannot compute edit distance between null strings. " - "Consider removing them using `dropna` or fill with `fillna`." - ) - return self._return_or_inplace(self._column.edit_distance_matrix()) - def minhash( self, seed: int, a: ColumnLike, b: ColumnLike, width: int ) -> Series | Index: diff --git a/python/cudf/cudf/core/column/string.py b/python/cudf/cudf/core/column/string.py index fd768b888bfd..29f5d13bc489 100644 --- a/python/cudf/cudf/core/column/string.py +++ b/python/cudf/cudf/core/column/string.py @@ -5,7 +5,6 @@ import itertools import re -import warnings from functools import lru_cache from typing import TYPE_CHECKING, Any, Self, cast @@ -751,25 +750,6 @@ def edit_distance(self, targets: Self) -> NumericalColumn: ), ) - def edit_distance_matrix(self) -> ListColumn: - warnings.warn( - "edit_distance_matrix is deprecated. Use edit_distance instead.", - FutureWarning, - ) - with self.access(mode="read", scope="internal"): - result = plc.nvtext.edit_distance.edit_distance_matrix( - self.plc_column - ) - return cast( - cudf.core.column.lists.ListColumn, - ColumnBase.create( - result, - cudf.ListDtype( - get_dtype_of_same_kind(self.dtype, np.dtype(np.int32)) - ), - ), - ) - def byte_pair_encoding( self, merge_pairs: plc.nvtext.byte_pair_encode.BPEMergePairs, diff --git a/python/pylibcudf/pylibcudf/libcudf/nvtext/edit_distance.pxd b/python/pylibcudf/pylibcudf/libcudf/nvtext/edit_distance.pxd index f3c10c11abf8..f18fa8321429 100644 --- a/python/pylibcudf/pylibcudf/libcudf/nvtext/edit_distance.pxd +++ b/python/pylibcudf/pylibcudf/libcudf/nvtext/edit_distance.pxd @@ -18,9 +18,3 @@ cdef extern from "nvtext/edit_distance.hpp" namespace "nvtext" nogil: cudaStream_t stream, device_async_resource_ref mr ) except +libcudf_exception_handler - - cdef unique_ptr[column] edit_distance_matrix( - const column_view & strings, - cudaStream_t stream, - device_async_resource_ref mr - ) except +libcudf_exception_handler diff --git a/python/pylibcudf/pylibcudf/nvtext/edit_distance.pxd b/python/pylibcudf/pylibcudf/nvtext/edit_distance.pxd index c0297ebd8876..7293510e9d99 100644 --- a/python/pylibcudf/pylibcudf/nvtext/edit_distance.pxd +++ b/python/pylibcudf/pylibcudf/nvtext/edit_distance.pxd @@ -11,9 +11,3 @@ cpdef Column edit_distance( object stream = *, DeviceMemoryResource mr=*, ) - -cpdef Column edit_distance_matrix( - Column input, - object stream = *, - DeviceMemoryResource mr=*, -) diff --git a/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyi b/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyi index 5a6bde4cb66a..90d5991aeaa7 100644 --- a/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyi +++ b/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyi @@ -12,8 +12,3 @@ def edit_distance( stream: CudaStreamLike | None = None, mr: DeviceMemoryResource | None = None, ) -> Column: ... -def edit_distance_matrix( - input: Column, - stream: CudaStreamLike | None = None, - mr: DeviceMemoryResource | None = None, -) -> Column: ... diff --git a/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyx b/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyx index 4b9d3f6bcc36..06ae6a8366dc 100644 --- a/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyx +++ b/python/pylibcudf/pylibcudf/nvtext/edit_distance.pyx @@ -1,15 +1,12 @@ # SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION. # SPDX-License-Identifier: Apache-2.0 -import warnings - from libcpp.memory cimport unique_ptr from libcpp.utility cimport move from pylibcudf.libcudf.column.column cimport column from pylibcudf.libcudf.column.column_view cimport column_view from pylibcudf.libcudf.nvtext.edit_distance cimport ( edit_distance as cpp_edit_distance, - edit_distance_matrix as cpp_edit_distance_matrix, ) from rmm.pylibrmm.memory_resource cimport DeviceMemoryResource @@ -19,7 +16,7 @@ from ..column cimport Column from ..utils cimport _get_stream, _get_memory_resource from cuda.bindings.cyruntime cimport cudaStream_t -__all__ = ["edit_distance", "edit_distance_matrix"] +__all__ = ["edit_distance"] cpdef Column edit_distance( Column input, @@ -57,44 +54,3 @@ cpdef Column edit_distance( c_result = cpp_edit_distance(c_strings, c_targets, _cs, mr.get_mr()) return Column.from_libcudf(move(c_result), _stream, mr) - - -cpdef Column edit_distance_matrix( - Column input, - object stream=None, - DeviceMemoryResource mr=None, -): - """ - Returns the edit distance between all strings in the input strings column - - .. deprecated:: release 26.04 - edit_distance_matrix is deprecated. - - For details, see :cpp:func:`edit_distance_matrix` - - Parameters - ---------- - input : Column - Input strings - stream : Stream | None - CUDA stream on which to perform the operation. - - Returns - ------- - Column - New column of edit distance values - """ - warnings.warn( - "edit_distance_matrix is deprecated.", - FutureWarning, - ) - cdef column_view c_strings = input.view() - cdef unique_ptr[column] c_result - cdef Stream _stream = _get_stream(stream) - cdef cudaStream_t _cs = _stream.view().value() - mr = _get_memory_resource(mr) - - with nogil: - c_result = cpp_edit_distance_matrix(c_strings, _cs, mr.get_mr()) - - return Column.from_libcudf(move(c_result), _stream, mr)