|
| 1 | +# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | +import os |
| 17 | +import time |
| 18 | + |
| 19 | +from nemo_curator.datasets import DocumentDataset |
| 20 | +from nemo_curator.log import create_logger |
| 21 | +from nemo_curator.modules.config import SemDedupConfig |
| 22 | +from nemo_curator.modules.semantic_dedup import SemDedup |
| 23 | +from nemo_curator.utils.distributed_utils import get_client, read_data |
| 24 | +from nemo_curator.utils.file_utils import ( |
| 25 | + expand_outdir_and_mkdir, |
| 26 | + get_all_files_paths_under, |
| 27 | +) |
| 28 | +from nemo_curator.utils.script_utils import ArgumentHelper |
| 29 | + |
| 30 | + |
| 31 | +def silence_hf_warnings(): |
| 32 | + from transformers.utils import logging |
| 33 | + |
| 34 | + logging.set_verbosity_error() |
| 35 | + |
| 36 | + |
| 37 | +def main(args): |
| 38 | + semdedup_config = SemDedupConfig.from_yaml(args.config_file) |
| 39 | + client = get_client(**ArgumentHelper.parse_client_args(args)) |
| 40 | + |
| 41 | + silence_hf_warnings() |
| 42 | + client.run(silence_hf_warnings) |
| 43 | + |
| 44 | + expand_outdir_and_mkdir(semdedup_config.cache_dir) |
| 45 | + logger = create_logger( |
| 46 | + rank=0, |
| 47 | + name="logger-end-to_end-semdup", |
| 48 | + log_file=os.path.join(semdedup_config.cache_dir, "compute_embeddings.log"), |
| 49 | + log_level=logging.INFO, |
| 50 | + stdout=True, |
| 51 | + ) |
| 52 | + st = time.time() |
| 53 | + input_files = get_all_files_paths_under( |
| 54 | + root=args.input_data_dir, |
| 55 | + ) |
| 56 | + if semdedup_config.num_files > 0: |
| 57 | + input_files = input_files[: semdedup_config.num_files] |
| 58 | + logger.info(f"Processing {len(input_files)} files") |
| 59 | + ddf = read_data( |
| 60 | + input_files=input_files, |
| 61 | + file_type=args.input_file_type, |
| 62 | + add_filename=False, |
| 63 | + backend="cudf", |
| 64 | + ) |
| 65 | + dataset = DocumentDataset(ddf) |
| 66 | + semdup = SemDedup(semdedup_config, logger=logger) |
| 67 | + dedup_ids = semdup(dataset) |
| 68 | + print(dedup_ids.df.head()) |
| 69 | + logger.info(f"Time taken: {time.time() - st}") |
| 70 | + client.cancel(client.futures, force=True) |
| 71 | + client.close() |
| 72 | + |
| 73 | + |
| 74 | +def attach_args(): |
| 75 | + parser = ArgumentHelper.parse_semdedup_args(add_input_args=True) |
| 76 | + return parser |
| 77 | + |
| 78 | + |
| 79 | +def console_script(): |
| 80 | + main(attach_args().parse_args()) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main(attach_args().parse_args()) |
0 commit comments