-
Notifications
You must be signed in to change notification settings - Fork 116
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
Hard negative mining for Retriever fine-tuning #523
Open
vinay-raman
wants to merge
19
commits into
NVIDIA:main
Choose a base branch
from
vinay-raman:vinayraman/hardnegativemining
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e5a88c9
Signed-off by viraman@nvidia.com
vinay-raman f6a47f9
Merge branch 'main' into vinayraman/nvbug_5025154_fix
vinay-raman 971df10
Signed-off by viraman@nvidia.com
vinay-raman 8a6deb9
Signed-off by viraman@nvidia.com
vinay-raman 75c869e
fixed qa bug 5008113, Signed-off by viraman@nvidia.com
vinay-raman 547c849
bug fixes for generator, Signed-off by viraman@nvidia.com
vinay-raman cf0ec14
fixed precommit, Signed-off by viraman@nvidia.com
vinay-raman d9f7be3
fixed filters, Signed-off by viraman@nvidia.com
vinay-raman d9ee0ee
fixed all issues, Signed-off by viraman@nvidia.com
vinay-raman 0e69b74
fixed model caching in hard negative mining, Signed-off by viraman@nv…
vinay-raman d5d051a
fixed minor issues, Signed-off by viraman@nvidia.com
vinay-raman f512886
fixed merge conflicts, Signed-off by viraman@nvidia.com
vinay-raman 794ef20
fixed minor bug, Signed-off by viraman@nvidia.com
vinay-raman 531b5ac
fixed minor issues, Signed-off by viraman@nvidia.com
vinay-raman 2082f16
removed duplicates in pos_docs, Signed-off by viraman@nvidia.com
vinay-raman bb9ed81
Merge branch 'main' into vinayraman/hardnegativemining
vinay-raman 94800e5
Merge branch 'main' into vinayraman/hardnegativemining
vinay-raman 1b122f0
Merge branch 'main' into vinayraman/hardnegativemining
vinay-raman 6acaa62
fixed some comments, added input file for hard negative mining & test…
vinay-raman 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 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 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
19 changes: 19 additions & 0 deletions
19
tutorials/nemo-retriever-synthetic-data-generation/config/hard-negative-mining-config.yaml
This file contains 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,19 @@ | ||
# HARD-NEGATIVE MINING parameters | ||
|
||
# base_url: "https://integrate.api.nvidia.com/v1" | ||
model_name: "sentence-transformers/all-MiniLM-L6-v2" | ||
model_type: "hf" | ||
query_prefix: "query:" | ||
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. Is there a reason why the defaults in the dataclass don't match the defaults in the yaml? |
||
passage_prefix: "passage:" | ||
api_key: "your api key here" | ||
truncate: "END" | ||
hard_negatives_to_mine: 4 | ||
hard_neg_mining_algorithm: "topk_percpos" | ||
percpos: 0.95 | ||
|
||
|
||
# SEMANTIC CLUSTERING parameters | ||
min_cluster_size: 100 | ||
max_number_clusters: 200 | ||
cluster_output_dir: "/workspace/hnm/clusters" | ||
logger_output_dir: "/workspace/hnm/logs" |
This file contains 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
104 changes: 104 additions & 0 deletions
104
tutorials/nemo-retriever-synthetic-data-generation/mine_hard_negatives.py
This file contains 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,104 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import glob | ||
import importlib | ||
import os | ||
import pdb | ||
import shutil | ||
import time | ||
from pathlib import Path | ||
from typing import Any, List | ||
|
||
from retriever_hardnegative_miner import HardNegativeMiner | ||
from tqdm.dask import TqdmCallback | ||
|
||
from config.config import RetrieverHardNegativeMiningConfig | ||
from nemo_curator.datasets import DocumentDataset | ||
from nemo_curator.utils.distributed_utils import get_client | ||
from nemo_curator.utils.file_utils import get_all_files_paths_under | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--input-dir", | ||
type=str, | ||
default="", | ||
help="Input dir path containing annotated data files in jsonl format", | ||
) | ||
parser.add_argument( | ||
"--hard-negative-mining-config", | ||
type=str, | ||
default="", | ||
help="Configuration yaml file path containing config for hard negative mining", | ||
) | ||
parser.add_argument( | ||
"--output-dir", | ||
type=str, | ||
default="", | ||
help="Output file containing hard negatives", | ||
) | ||
parser.add_argument( | ||
"--api-key", | ||
type=str, | ||
default=None, | ||
help="The API key to use for the synthetic data generation LLM client.", | ||
) | ||
parser.add_argument( | ||
"--api-timeout", | ||
type=int, | ||
default=120, | ||
help="The timeout value for API calls in seconds.", | ||
) | ||
args = parser.parse_args() | ||
|
||
if not os.path.exists(args.input_dir): | ||
raise ValueError("Input directory not found") | ||
|
||
if os.path.exists(args.output_dir): | ||
raise ValueError("Output dir exists already, use a new file name!") | ||
|
||
if args.input_dir: | ||
input_files = get_all_files_paths_under(args.input_dir, keep_extensions="part") | ||
input_dataset = DocumentDataset.read_json(input_files) | ||
else: | ||
raise ValueError("provide input file path") | ||
|
||
if args.hard_negative_mining_config: | ||
cfg = RetrieverHardNegativeMiningConfig.from_yaml( | ||
args.hard_negative_mining_config | ||
) | ||
|
||
else: | ||
raise ValueError("provide config for hard negative mining") | ||
if args.api_key: | ||
cfg.api_key = args.api_key | ||
|
||
mine_hard_negatives = HardNegativeMiner(cfg) | ||
print("Mining hard negatives ...") | ||
st_time = time.time() | ||
mined_dataset = mine_hard_negatives(input_dataset) | ||
|
||
print("Time taken = {:.2f} s".format(time.time() - st_time)) | ||
print("Saving data in jsonl format ...") | ||
mined_dataset.df.to_json( | ||
os.path.join(args.output_dir), lines=True, orient="records" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
dask_client = get_client(cluster_type="gpu") | ||
main() |
100 changes: 100 additions & 0 deletions
100
tutorials/nemo-retriever-synthetic-data-generation/repartition.py
This file contains 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,100 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import glob | ||
import importlib | ||
import os | ||
import pdb | ||
import shutil | ||
import time | ||
from typing import Any, List | ||
|
||
from retriever_hardnegative_miner import HardNegativeMiner | ||
from tqdm.dask import TqdmCallback | ||
|
||
from config.config import RetrieverHardNegativeMiningConfig | ||
from nemo_curator.datasets import DocumentDataset | ||
from nemo_curator.utils.distributed_utils import get_client | ||
from nemo_curator.utils.file_utils import get_all_files_paths_under | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--input-dir", | ||
type=str, | ||
default="", | ||
help="Input dir path containing annotated data files in jsonl format", | ||
) | ||
parser.add_argument( | ||
"--hard-negative-mining-config", | ||
type=str, | ||
default="", | ||
help="Configuration yaml file path containing config for hard negative mining", | ||
) | ||
parser.add_argument( | ||
"--output-dir", | ||
type=str, | ||
default="", | ||
help="Output file containing clustered dataset", | ||
) | ||
parser.add_argument( | ||
"--api-key", | ||
type=str, | ||
default=None, | ||
help="The API key to use for the synthetic data generation LLM client.", | ||
) | ||
parser.add_argument( | ||
"--api-timeout", | ||
type=int, | ||
default=120, | ||
help="The timeout value for API calls in seconds.", | ||
) | ||
args = parser.parse_args() | ||
|
||
if not os.path.exists(args.input_dir): | ||
raise ValueError("Input directory not found") | ||
|
||
if os.path.exists(args.output_dir): | ||
raise ValueError("Output dir exists already, use a new file name!") | ||
|
||
if args.input_dir: | ||
input_files = get_all_files_paths_under(args.input_dir, keep_extensions="jsonl") | ||
input_dataset = DocumentDataset.read_json(input_files) | ||
else: | ||
raise ValueError("provide input file path") | ||
if args.hard_negative_mining_config: | ||
cfg = RetrieverHardNegativeMiningConfig.from_yaml( | ||
args.hard_negative_mining_config | ||
) | ||
else: | ||
raise ValueError("provide config for hard negative mining") | ||
if args.api_key: | ||
cfg.api_key = args.api_key | ||
|
||
st_time = time.time() | ||
miner = HardNegativeMiner(cfg) | ||
clustered_dataset = miner.repartition_semantic_similarity(input_dataset) | ||
clustered_dataset.persist() | ||
|
||
# saving clustered dataset | ||
print("saving clustered dataset") | ||
clustered_dataset.df.to_json(os.path.join(args.output_dir, "clustered_dataset")) | ||
print("Time taken to cluster data = {:.2f} s".format(time.time() - st_time)) | ||
|
||
|
||
if __name__ == "__main__": | ||
dask_client = get_client(cluster_type="gpu") | ||
main() |
This file contains 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
Oops, something went wrong.
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.
Why was this change made?