Пример #1
0
import importlib
import os

from fairseq_stchde import registry
from fairseq_stchde.criterions.fairseq_criterion import (  # noqa
    FairseqCriterion,
    LegacyFairseqCriterion,
)
from omegaconf import DictConfig


(
    build_criterion_,
    register_criterion,
    CRITERION_REGISTRY,
    CRITERION_DATACLASS_REGISTRY,
) = registry.setup_registry(
    "--criterion", base_class=FairseqCriterion, default="cross_entropy"
)


def build_criterion(cfg: DictConfig, task):
    return build_criterion_(cfg, task)


# automatically import any Python files in the criterions/ directory
for file in os.listdir(os.path.dirname(__file__)):
    if file.endswith(".py") and not file.startswith("_"):
        file_name = file[: file.find(".py")]
        importlib.import_module("fairseq_stchde.criterions." + file_name)
Пример #2
0
# Copyright (c) Facebook, 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 importlib
import os

from fairseq_stchde import registry

build_tokenizer, register_tokenizer, TOKENIZER_REGISTRY, _ = registry.setup_registry(
    "--tokenizer",
    default=None,
)

build_bpe, register_bpe, BPE_REGISTRY, _ = registry.setup_registry(
    "--bpe",
    default=None,
)

# automatically import any Python files in the encoders/ directory
for file in os.listdir(os.path.dirname(__file__)):
    if file.endswith(".py") and not file.startswith("_"):
        module = file[:file.find(".py")]
        importlib.import_module("fairseq_stchde.data.encoders." + module)
Пример #3
0
        self.pred = []

    def add_string(self, ref, pred):
        self.ref.append(ref)
        self.pred.append(pred)

    @abstractmethod
    def score(self) -> float:
        pass

    @abstractmethod
    def result_string(self) -> str:
        pass


_build_scorer, register_scorer, SCORER_REGISTRY, _ = registry.setup_registry(
    "--scoring", default="bleu")


def build_scorer(choice, tgt_dict):
    _choice = choice._name if isinstance(choice, DictConfig) else choice

    if _choice == "bleu":
        from fairseq_stchde.scoring import bleu

        return bleu.Scorer(
            bleu.BleuConfig(pad=tgt_dict.pad(),
                            eos=tgt_dict.eos(),
                            unk=tgt_dict.unk()))
    return _build_scorer(choice)

Пример #4
0
import importlib
import os

from fairseq_stchde import registry
from fairseq_stchde.optim.lr_scheduler.fairseq_lr_scheduler import (  # noqa
    FairseqLRScheduler, LegacyFairseqLRScheduler,
)
from omegaconf import DictConfig

(
    build_lr_scheduler_,
    register_lr_scheduler,
    LR_SCHEDULER_REGISTRY,
    LR_SCHEDULER_DATACLASS_REGISTRY,
) = registry.setup_registry("--lr-scheduler",
                            base_class=FairseqLRScheduler,
                            default="fixed")


def build_lr_scheduler(cfg: DictConfig, optimizer):
    return build_lr_scheduler_(cfg, optimizer)


# automatically import any Python files in the optim/lr_scheduler/ directory
for file in os.listdir(os.path.dirname(__file__)):
    if file.endswith(".py") and not file.startswith("_"):
        file_name = file[:file.find(".py")]
        importlib.import_module("fairseq_stchde.optim.lr_scheduler." +
                                file_name)
Пример #5
0
from omegaconf import DictConfig

__all__ = [
    "FairseqOptimizer",
    "FP16Optimizer",
    "MemoryEfficientFP16Optimizer",
    "shard_",
]

(
    _build_optimizer,
    register_optimizer,
    OPTIMIZER_REGISTRY,
    OPTIMIZER_DATACLASS_REGISTRY,
) = registry.setup_registry("--optimizer",
                            base_class=FairseqOptimizer,
                            required=True)


def build_optimizer(cfg: DictConfig, params, *extra_args, **extra_kwargs):
    if all(isinstance(p, dict) for p in params):
        params = [t for p in params for t in p.values()]
    params = list(filter(lambda p: p.requires_grad, params))
    return _build_optimizer(cfg, params, *extra_args, **extra_kwargs)


# automatically import any Python files in the optim/ directory
for file in os.listdir(os.path.dirname(__file__)):
    if file.endswith(".py") and not file.startswith("_"):
        file_name = file[:file.find(".py")]
        importlib.import_module("fairseq_stchde.optim." + file_name)