Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Pandas Series to cuDF Series #76

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions crossfit/op/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import cudf
import cupy as cp
import pandas as pd
import torch
from cudf.core.subword_tokenizer import SubwordTokenizer, _cast_to_appropriate_type
from cudf.utils.hash_vocab_utils import hash_vocab
Expand Down Expand Up @@ -63,6 +64,8 @@ def tokenize_strings(self, sentences, max_length=None):

if isinstance(sentences, cudf.Series):
sentences = sentences.to_arrow().to_pylist()
elif isinstance(sentences, pd.Series):
sentences = sentences.to_list()

with torch.no_grad():
tokenized_data = tokenizer.batch_encode_plus(
Expand Down
11 changes: 11 additions & 0 deletions tests/op/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
cp = pytest.importorskip("cupy")
cudf = pytest.importorskip("cudf")
dask_cudf = pytest.importorskip("dask_cudf")
dd = pytest.importorskip("dask.dataframe")
pd = pytest.importorskip("pandas")
transformers = pytest.importorskip("transformers")
torch = pytest.importorskip("torch")

Expand Down Expand Up @@ -144,3 +146,12 @@ def test_clip_tokens_no_clipping_needed():
assert result["attention_mask"].shape == (2, 3)
assert torch.equal(result["input_ids"].to("cpu"), torch.tensor([[1, 2, 3], [4, 5, 6]]))
assert torch.equal(result["attention_mask"].to("cpu"), torch.tensor([[1, 1, 1], [1, 1, 1]]))


def test_tokenize_strings_cpu(model_name="microsoft/deberta-v3-base"):
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved
model = cf.HFModel(model_name)
tokenizer = op.Tokenizer(model, cols=["text"], tokenizer_type="spm")
input_strings = ["hello world", "this is a sentence"]
ddf = dd.from_pandas(pd.DataFrame({"text": input_strings}), npartitions=1)
results = tokenizer(ddf)
results = results.compute()
Loading