-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP * WIP * setup check cuda versions * WIP * WIP --------- Co-authored-by: nicolas.brosse <[email protected]>
- Loading branch information
Showing
8 changed files
with
131 additions
and
42 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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,114 @@ | ||
#!/usr/bin/env python3 -u | ||
# Copyright (c) DP Techonology, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import logging | ||
import os | ||
import pickle | ||
import sys | ||
|
||
import torch | ||
from unicore import checkpoint_utils, distributed_utils, options, tasks, utils | ||
from unicore.logging import progress_bar | ||
|
||
logging.basicConfig( | ||
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
level=os.environ.get("LOGLEVEL", "INFO").upper(), | ||
stream=sys.stdout, | ||
) | ||
logger = logging.getLogger("unicore_cli.inference") | ||
|
||
|
||
def main(args): | ||
assert ( | ||
args.batch_size is not None | ||
), "Must specify batch size either with --batch-size" | ||
|
||
use_fp16 = args.fp16 | ||
use_cuda = torch.cuda.is_available() and not args.cpu | ||
|
||
if use_cuda: | ||
torch.cuda.set_device(args.device_id) | ||
|
||
if args.distributed_world_size > 1: | ||
data_parallel_world_size = distributed_utils.get_data_parallel_world_size() | ||
data_parallel_rank = distributed_utils.get_data_parallel_rank() | ||
else: | ||
data_parallel_world_size = 1 | ||
data_parallel_rank = 0 | ||
|
||
# Load model | ||
logger.info("loading model(s) from {}".format(args.path)) | ||
state = checkpoint_utils.load_checkpoint_to_cpu(args.path) | ||
task = tasks.setup_task(args) | ||
model = task.build_model(args) | ||
model.load_state_dict(state["model"], strict=False) | ||
|
||
# Move models to GPU | ||
if use_cuda: | ||
model.cuda() | ||
# fp16 only supported on CUDA for fused kernels | ||
if use_fp16: | ||
model.half() | ||
|
||
# Print args | ||
logger.info(args) | ||
|
||
# Build loss | ||
loss = task.build_loss(args) | ||
loss.eval() | ||
|
||
for subset in args.valid_subset.split(","): | ||
try: | ||
task.load_dataset(subset, combine=False, epoch=1) | ||
dataset = task.dataset(subset) | ||
except KeyError: | ||
raise Exception("Cannot find dataset: " + subset) | ||
|
||
if not os.path.exists(args.results_path): | ||
os.makedirs(args.results_path) | ||
save_path = os.path.join(args.results_path, f"{subset}.out.pkl") | ||
# Initialize data iterator | ||
itr = task.get_batch_iterator( | ||
dataset=dataset, | ||
batch_size=args.batch_size, | ||
ignore_invalid_inputs=True, | ||
required_batch_size_multiple=args.required_batch_size_multiple, | ||
seed=args.seed, | ||
num_shards=data_parallel_world_size, | ||
shard_id=data_parallel_rank, | ||
num_workers=args.num_workers, | ||
data_buffer_size=args.data_buffer_size, | ||
).next_epoch_itr(shuffle=False) | ||
progress = progress_bar.progress_bar( | ||
itr, | ||
log_format=args.log_format, | ||
log_interval=args.log_interval, | ||
prefix=f"valid on '{subset}' subset", | ||
default_log_format=("tqdm" if not args.no_progress_bar else "simple"), | ||
) | ||
log_outputs = [] | ||
for i, sample in enumerate(progress): | ||
sample = utils.move_to_cuda(sample) if use_cuda else sample | ||
if len(sample) == 0: | ||
continue | ||
_, _, log_output = task.valid_step(sample, model, loss, test=True) | ||
progress.log({}, step=i) | ||
log_outputs.append(log_output) | ||
pickle.dump(log_outputs, open(save_path, "wb")) | ||
logger.info("Done inference! ") | ||
return None | ||
|
||
|
||
def cli_main(): | ||
parser = options.get_validation_parser() | ||
options.add_model_args(parser) | ||
args = options.parse_args_and_arch(parser) | ||
distributed_utils.call_main(args, main) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli_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