-
Notifications
You must be signed in to change notification settings - Fork 812
Add support for QQP dataset with unit tests #1713
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ecf8012
Add QQP dataset + unit test
vcm2114 c030373
Adjust output + add different strings for tests
vcm2114 269c1cb
Merge branch 'pytorch:main' into qqp_dataset
vcm2114 d35d2f4
Remove lambda functions + correct docstring
vcm2114 fb7e9de
Merge branch 'qqp_dataset' of github.com:VirgileHlav/text into qqp_da…
vcm2114 1950d57
Fix lint
vcm2114 b53de4a
Add dataset documentation
vcm2114 2f0fc52
Add shuffle and sharding
vcm2114 053ff90
Merge branch 'main' into qqp_dataset
vcm2114 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,11 @@ MRPC | |
|
||
.. autofunction:: MRPC | ||
|
||
QQP | ||
~~~~ | ||
|
||
.. autofunction:: QQP | ||
|
||
SogouNews | ||
~~~~~~~~~ | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
from unittest.mock import patch | ||
|
||
from torchtext.datasets.qqp import QQP | ||
|
||
from ..common.case_utils import TempDirMixin, zip_equal, get_random_unicode | ||
from ..common.torchtext_test_case import TorchtextTestCase | ||
|
||
|
||
def _get_mock_dataset(root_dir): | ||
""" | ||
root_dir: directory to the mocked dataset | ||
""" | ||
base_dir = os.path.join(root_dir, "QQP") | ||
os.makedirs(base_dir, exist_ok=True) | ||
|
||
seed = 1 | ||
file_name = "quora_duplicate_questions.tsv" | ||
txt_file = os.path.join(base_dir, file_name) | ||
mocked_data = [] | ||
print(txt_file) | ||
with open(txt_file, "w", encoding="utf-8") as f: | ||
f.write("id\tqid1\tqid2\tquestion1\tquestion2\tis_duplicate\n") | ||
for i in range(5): | ||
label = seed % 2 | ||
rand_string_1 = get_random_unicode(seed) | ||
rand_string_2 = get_random_unicode(seed + 1) | ||
dataset_line = (label, rand_string_1, rand_string_2) | ||
# append line to correct dataset split | ||
mocked_data.append(dataset_line) | ||
f.write(f"{i}\t{i}\t{i}\t{rand_string_1}\t{rand_string_2}\t{label}\n") | ||
seed += 1 | ||
|
||
return mocked_data | ||
|
||
|
||
class TestQQP(TempDirMixin, TorchtextTestCase): | ||
root_dir = None | ||
samples = [] | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.root_dir = cls.get_base_temp_dir() | ||
print(cls.root_dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove print There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solved with #1734 |
||
cls.samples = _get_mock_dataset(cls.root_dir) | ||
cls.patcher = patch("torchdata.datapipes.iter.util.cacheholder._hash_check", return_value=True) | ||
cls.patcher.start() | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.patcher.stop() | ||
super().tearDownClass() | ||
|
||
def test_qqp(self): | ||
dataset = QQP(root=self.root_dir) | ||
|
||
samples = list(dataset) | ||
expected_samples = self.samples | ||
for sample, expected_sample in zip_equal(samples, expected_samples): | ||
self.assertEqual(sample, expected_sample) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
|
||
from torchtext._internal.module_utils import is_module_available | ||
from torchtext.data.datasets_utils import _create_dataset_directory | ||
|
||
if is_module_available("torchdata"): | ||
from torchdata.datapipes.iter import FileOpener, IterableWrapper | ||
from torchtext._download_hooks import HttpReader | ||
|
||
URL = "http://qim.fs.quoracdn.net/quora_duplicate_questions.tsv" | ||
|
||
MD5 = "b6d5672bd9dc1e66ab2bb020ebeafb8d" | ||
|
||
_PATH = "quora_duplicate_questions.tsv" | ||
|
||
NUM_LINES = {"train": 404290} | ||
|
||
DATASET_NAME = "QQP" | ||
|
||
|
||
@_create_dataset_directory(dataset_name=DATASET_NAME) | ||
def QQP(root: str): | ||
"""QQP dataset | ||
For additional details refer to https://quoradata.quora.com/First-Quora-Dataset-Release-Question-Pairs | ||
|
||
Args: | ||
root: Directory where the datasets are saved. Default: os.path.expanduser('~/.torchtext/cache') | ||
|
||
:returns: DataPipe that yields rows from QQP dataset (label (int), question1 (str), question2 (str)) | ||
:rtype: (int, str, str) | ||
""" | ||
if not is_module_available("torchdata"): | ||
raise ModuleNotFoundError( | ||
"Package `torchdata` not found. Please install following instructions at `https://github.com/pytorch/data`" | ||
) | ||
|
||
def _filepath_fn(_=None): | ||
return os.path.join(root, _PATH) | ||
|
||
def _modify_res(x): | ||
return (int(x[-1]), x[3], x[4]) | ||
|
||
url_dp = IterableWrapper([URL]) | ||
cache_dp = url_dp.on_disk_cache( | ||
filepath_fn=_filepath_fn, | ||
hash_dict={_filepath_fn(): MD5}, | ||
hash_type="md5", | ||
) | ||
cache_dp = HttpReader(cache_dp).end_caching(mode="wb", same_filepath_fn=True) | ||
cache_dp = FileOpener(cache_dp, encoding="utf-8") | ||
# some context stored at top of the file needs to be removed | ||
parsed_data = cache_dp.parse_csv(skip_lines=1, delimiter="\t").map(_modify_res) | ||
return parsed_data.shuffle().set_shuffle(False).sharding_filter() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove the print statement here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved with #1734