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.7 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
46
  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_optimizer import FairseqOptimizer
  10. OPTIMIZER_REGISTRY = {}
  11. OPTIMIZER_CLASS_NAMES = set()
  12. def build_optimizer(args, params):
  13. params = filter(lambda p: p.requires_grad, params)
  14. return OPTIMIZER_REGISTRY[args.optimizer](args, params)
  15. def register_optimizer(name):
  16. """Decorator to register a new optimizer."""
  17. def register_optimizer_cls(cls):
  18. if name in OPTIMIZER_REGISTRY:
  19. raise ValueError('Cannot register duplicate optimizer ({})'.format(name))
  20. if not issubclass(cls, FairseqOptimizer):
  21. raise ValueError('Optimizer ({}: {}) must extend FairseqOptimizer'.format(name, cls.__name__))
  22. if cls.__name__ in OPTIMIZER_CLASS_NAMES:
  23. # We use the optimizer class name as a unique identifier in
  24. # checkpoints, so all optimizer must have unique class names.
  25. raise ValueError('Cannot register optimizer with duplicate class name ({})'.format(cls.__name__))
  26. OPTIMIZER_REGISTRY[name] = cls
  27. OPTIMIZER_CLASS_NAMES.add(cls.__name__)
  28. return cls
  29. return register_optimizer_cls
  30. # automatically import any Python files in the optim/ directory
  31. for file in os.listdir(os.path.dirname(__file__)):
  32. if file.endswith('.py') and not file.startswith('_'):
  33. module = file[:file.find('.py')]
  34. importlib.import_module('fairseq.optim.' + module)
Tip!

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

Comments

Loading...