Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

__init__.py 1.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the LICENSE file in
  5. # the root directory of this source tree. An additional grant of patent rights
  6. # can be found in the PATENTS file in the same directory.
  7. import importlib
  8. import os
  9. from .fairseq_criterion import FairseqCriterion
  10. CRITERION_REGISTRY = {}
  11. CRITERION_CLASS_NAMES = set()
  12. def build_criterion(args, task):
  13. return CRITERION_REGISTRY[args.criterion](args, task)
  14. def register_criterion(name):
  15. """Decorator to register a new criterion."""
  16. def register_criterion_cls(cls):
  17. if name in CRITERION_REGISTRY:
  18. raise ValueError('Cannot register duplicate criterion ({})'.format(name))
  19. if not issubclass(cls, FairseqCriterion):
  20. raise ValueError('Criterion ({}: {}) must extend FairseqCriterion'.format(name, cls.__name__))
  21. if cls.__name__ in CRITERION_CLASS_NAMES:
  22. # We use the criterion class name as a unique identifier in
  23. # checkpoints, so all criterions must have unique class names.
  24. raise ValueError('Cannot register criterion with duplicate class name ({})'.format(cls.__name__))
  25. CRITERION_REGISTRY[name] = cls
  26. CRITERION_CLASS_NAMES.add(cls.__name__)
  27. return cls
  28. return register_criterion_cls
  29. # automatically import any Python files in the criterions/ directory
  30. for file in os.listdir(os.path.dirname(__file__)):
  31. if file.endswith('.py') and not file.startswith('_'):
  32. module = file[:file.find('.py')]
  33. importlib.import_module('fairseq.criterions.' + module)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...