Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions cpp/include/nvtext/edit_distance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,5 @@ std::unique_ptr<cudf::column> 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<cudf::column> 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
169 changes: 0 additions & 169 deletions cpp/src/text/edit_distance.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -322,120 +278,6 @@ std::unique_ptr<cudf::column> 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<cudf::string_view>(row);
cudf::string_view d_str2 =
d_strings.is_null(col) ? cudf::string_view{} : d_strings.element<cudf::string_view>(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<cudf::string_view>(row);
cudf::string_view const d_str2 =
d_strings.is_null(col) ? cudf::string_view{} : d_strings.element<cudf::string_view>(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<cudf::column> 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<cudf::size_type>()};
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<size_t>(input.size()) <
static_cast<std::size_t>(std::numeric_limits<cudf::size_type>().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<std::ptrdiff_t> 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<cudf::size_type>{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<cudf::size_type> 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<cudf::size_type>();
thrust::for_each_n(
rmm::exec_policy_nosync(stream, cudf::get_current_device_resource_ref()),
cuda::counting_iterator<cudf::size_type>{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<cudf::size_type>(0, true, stream),
cudf::numeric_scalar<cudf::size_type>(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
Expand All @@ -452,15 +294,4 @@ std::unique_ptr<cudf::column> edit_distance(cudf::strings_column_view const& inp
return detail::edit_distance(input, targets, stream, mr);
}

/**
* @copydoc nvtext::edit_distance_matrix
*/
std::unique_ptr<cudf::column> 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
1 change: 0 additions & 1 deletion docs/cudf/source/cudf/api_docs/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ String handling
count
detokenize
edit_distance
edit_distance_matrix
endswith
extract
filter_alphanum
Expand Down
39 changes: 0 additions & 39 deletions python/cudf/cudf/core/accessors/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://www.cuelogic.com/blog/the-levenshtein-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:
Expand Down
20 changes: 0 additions & 20 deletions python/cudf/cudf/core/column/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import itertools
import re
import warnings
from functools import lru_cache
from typing import TYPE_CHECKING, Any, Self, cast

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 0 additions & 6 deletions python/pylibcudf/pylibcudf/libcudf/nvtext/edit_distance.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions python/pylibcudf/pylibcudf/nvtext/edit_distance.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,3 @@ cpdef Column edit_distance(
object stream = *,
DeviceMemoryResource mr=*,
)

cpdef Column edit_distance_matrix(
Column input,
object stream = *,
DeviceMemoryResource mr=*,
)
5 changes: 0 additions & 5 deletions python/pylibcudf/pylibcudf/nvtext/edit_distance.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Loading
Loading