本文整理匯總了Python中fairseq.tasks.setup_task方法的典型用法代碼示例。如果您正苦於以下問題:Python tasks.setup_task方法的具體用法?Python tasks.setup_task怎麽用?Python tasks.setup_task使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fairseq.tasks
的用法示例。
在下文中一共展示了tasks.setup_task方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: load_models_and_criterions
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def load_models_and_criterions(filenames, arg_overrides=None, task=None):
models = []
criterions = []
for filename in filenames:
if not os.path.exists(filename):
raise IOError("Model file not found: {}".format(filename))
state = checkpoint_utils.load_checkpoint_to_cpu(filename, arg_overrides)
args = state["args"]
if task is None:
task = tasks.setup_task(args)
# build model for ensemble
model = task.build_model(args)
model.load_state_dict(state["model"], strict=True)
models.append(model)
criterion = task.build_criterion(args)
if "criterion" in state:
criterion.load_state_dict(state["criterion"], strict=True)
criterions.append(criterion)
return models, criterions, args
示例2: load_model
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def load_model(self, args):
args.user_dir = os.path.join(os.path.dirname(__file__), '..', '..')
utils.import_user_module(args)
filename = args.model_path
if not os.path.exists(filename):
raise IOError("Model file not found: {}".format(filename))
state = checkpoint_utils.load_checkpoint_to_cpu(filename, json.loads(args.model_overrides))
saved_args = state["args"]
saved_args.data = args.data_bin
task = tasks.setup_task(saved_args)
# build model for ensemble
self.model = task.build_model(saved_args)
self.model.load_state_dict(state["model"], strict=True)
# Set dictionary
self.load_dictionary(task)
示例3: load_model_ensemble_and_task
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def load_model_ensemble_and_task(filenames, arg_overrides=None, task=None, strict=True, suffix=''):
from fairseq import tasks
ensemble = []
for filename in filenames:
filename = filename.replace(".pt", suffix + ".pt")
if not PathManager.exists(filename):
raise IOError("Model file not found: {}".format(filename))
state = load_checkpoint_to_cpu(filename, arg_overrides)
args = state["args"]
if task is None:
task = tasks.setup_task(args)
# build model for ensemble
model = task.build_model(args)
model.load_state_dict(state["model"], strict=strict, args=args)
ensemble.append(model)
return ensemble, args, task
示例4: __init__
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def __init__(self, path, data, use_cpu=True):
# Create the language modeling task.
self.args = FluencyArgs(path, data)
self.task = tasks.setup_task(self.args)
self.use_cuda = torch.cuda.is_available and not use_cpu
# Load language model ensemble.
models, model_args = utils.load_ensemble_for_inference(self.args.path.split(':'), self.task)
self.models = models
self.model_args = model_args
# Optimize ensemble for generation.
for model in self.models:
model.make_generation_fast_()
if self.use_cuda and self.model_args.fp16:
model.half()
# Create the sequence scorer.
self.scorer = SequenceScorer(self.models, self.task.target_dictionary)
if self.use_cuda:
self.scorer.cuda()
示例5: setup_asr
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def setup_asr(args, logger):
check_args(args)
import_user_module(args)
if args.max_tokens is None and args.max_sentences is None:
args.max_tokens = 30000
logger.info(args)
use_cuda = torch.cuda.is_available() and not args.cpu
# Load dataset splits
task = tasks.setup_task(args)
# Set dictionary
tgt_dict = task.target_dictionary
if args.ctc or args.rnnt:
tgt_dict.add_symbol("<ctc_blank>")
if args.ctc:
logger.info("| decoding a ctc model")
if args.rnnt:
logger.info("| decoding a rnnt model")
# Load ensemble
logger.info("| loading model(s) from {}".format(args.path))
models, _model_args = load_ensemble_for_inference(
args.path.split(":"),
task,
model_arg_overrides=eval(args.model_overrides), # noqa
)
optimize_models(args, use_cuda, models)
# Initialize generator
generator = task.build_generator(args)
sp = spm.SentencePieceProcessor()
sp.Load(os.path.join(args.data, "spm.model"))
return task, generator, models, sp, tgt_dict
示例6: create_task_and_model
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def create_task_and_model(args):
task = tasks.setup_task(args)
model = task.build_model(args)
return task, model
示例7: __init__
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def __init__(self, parsed_args):
self.args = parsed_args
import_user_module(parsed_args)
assert parsed_args.path is not None, '--path required for evaluation'
print(parsed_args)
self.use_cuda = torch.cuda.is_available() and not parsed_args.cpu
self.task = tasks.setup_task(parsed_args)
# Load ensemble
print('| loading model(s) from {}'.format(parsed_args.path))
self.models, args = utils.load_ensemble_for_inference(
parsed_args.path.split(':'), self.task, model_arg_overrides=eval(parsed_args.model_overrides),
)
for model in self.models:
model.make_generation_fast_()
if self.use_cuda:
model.cuda()
for arg in vars(parsed_args).keys():
if arg not in {'self_target', 'future_target', 'past_target', 'tokens_per_sample',
'output_size_dictionary'}:
setattr(args, arg, getattr(parsed_args, arg))
self.task = tasks.setup_task(args)
self.gen_timer = StopwatchMeter()
self.scorer = SequenceScorer(self.task.target_dictionary)
示例8: main
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def main(args):
assert args.path is not None, '--path required for evaluation!'
if args.tokens_per_sample is None:
args.tokens_per_sample = 1024
print(args)
use_cuda = torch.cuda.is_available() and not args.cpu
# Load dataset splits
task = tasks.setup_task(args)
task.load_dataset(args.gen_subset)
print('| {} {} {} examples'.format(args.data, args.gen_subset, len(task.dataset(args.gen_subset))))
# Load ensemble
print('| loading model(s) from {}'.format(args.path))
models, _ = utils.load_ensemble_for_inference(args.path.split(':'), task)
# Optimize ensemble for generation and set the source and dest dicts on the model (required by scorer)
for model in models:
model.make_generation_fast_()
itr = data.EpochBatchIterator(
dataset=task.dataset(args.gen_subset),
max_sentences=args.max_sentences or 4,
max_positions=model.max_positions(),
num_shards=args.num_shards,
shard_id=args.shard_id,
).next_epoch_itr(shuffle=False)
gen_timer = StopwatchMeter()
scorer = SequenceScorer(models, task.target_dictionary)
if use_cuda:
scorer.cuda()
score_sum = 0.
count = 0
with progress_bar.build_progress_bar(args, itr) as t:
results = scorer.score_batched_itr(t, cuda=use_cuda, timer=gen_timer)
wps_meter = TimeMeter()
for _, src_tokens, __, hypos in results:
for hypo in hypos:
pos_scores = hypo['positional_scores']
inf_scores = pos_scores.eq(float('inf')) | pos_scores.eq(float('-inf'))
if inf_scores.any():
print('| Skipping tokens with inf scores:',
task.target_dictionary.string(hypo['tokens'][inf_scores.nonzero()]))
pos_scores = pos_scores[(~inf_scores).nonzero()]
score_sum += pos_scores.sum()
count += pos_scores.numel()
wps_meter.update(src_tokens.size(0))
t.log({'wps': round(wps_meter.avg)})
avg_nll_loss = -score_sum / count
print('| Evaluated {} tokens in {:.1f}s ({:.2f} tokens/s)'.format(gen_timer.n, gen_timer.sum, 1. / gen_timer.avg))
print('| Loss: {:.4f}, Perplexity: {:.2f}'.format(avg_nll_loss, np.exp(avg_nll_loss)))
示例9: load_diverse_ensemble_for_inference
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def load_diverse_ensemble_for_inference(
filenames: List[str], task: Optional[tasks.FairseqTask] = None
):
"""Load an ensemble of diverse models for inference.
This method is similar to fairseq.utils.load_ensemble_for_inference
but allows to load diverse models with non-uniform args.
Args:
filenames: List of file names to checkpoints
task: Optional[FairseqTask]. If this isn't provided, we setup the task
using the first checkpoint's model args loaded from the saved state.
Return:
models, args: Tuple of lists. models contains the loaded models, args
the corresponding configurations.
task: Either the input task or the task created within this function
using args
"""
# load model architectures and weights
checkpoints_data = []
for filename in filenames:
if not PathManager.exists(filename):
raise IOError("Model file not found: {}".format(filename))
with PathManager.open(filename, "rb") as f:
checkpoints_data.append(
torch.load(
f,
map_location=lambda s, l: torch.serialization.default_restore_location(
s, "cpu"
),
)
)
# build ensemble
ensemble = []
if task is None:
if hasattr(checkpoints_data[0]["args"], "mode"):
checkpoints_data[0]["args"].mode = "eval"
task = tasks.setup_task(checkpoints_data[0]["args"])
for checkpoint_data in checkpoints_data:
model = task.build_model(checkpoint_data["args"])
model.load_state_dict(checkpoint_data["model"])
ensemble.append(model)
args_list = [s["args"] for s in checkpoints_data]
return ensemble, args_list, task
示例10: __init__
# 需要導入模塊: from fairseq import tasks [as 別名]
# 或者: from fairseq.tasks import setup_task [as 別名]
def __init__(self, model_path, user_dir, lang_pair, n_cpu_threads=-1):
"""Initializes a fairseq predictor.
Args:
model_path (string): Path to the fairseq model (*.pt). Like
--path in fairseq-interactive.
lang_pair (string): Language pair string (e.g. 'en-fr').
user_dir (string): Path to fairseq user directory.
n_cpu_threads (int): Number of CPU threads. If negative,
use GPU.
"""
super(FairseqPredictor, self).__init__()
_initialize_fairseq(user_dir)
self.use_cuda = torch.cuda.is_available() and n_cpu_threads < 0
parser = options.get_generation_parser()
input_args = ["--path", model_path, os.path.dirname(model_path)]
if lang_pair:
src, trg = lang_pair.split("-")
input_args.extend(["--source-lang", src, "--target-lang", trg])
args = options.parse_args_and_arch(parser, input_args)
# Setup task, e.g., translation
task = tasks.setup_task(args)
self.src_vocab_size = len(task.source_dictionary)
self.trg_vocab_size = len(task.target_dictionary)
self.pad_id = task.source_dictionary.pad()
# Load ensemble
logging.info('Loading fairseq model(s) from {}'.format(model_path))
self.models, _ = checkpoint_utils.load_model_ensemble(
model_path.split(':'),
task=task,
)
# Optimize ensemble for generation
for model in self.models:
model.make_generation_fast_(
beamable_mm_beam_size=1,
need_attn=False,
)
if self.use_cuda:
model.cuda()
self.model = EnsembleModel(self.models)
self.model.eval()