Aurelien-Lac commited on
Commit
a6bce77
·
1 Parent(s): 652f920

Load the repo's own LFM3 classes on transformers 5.23 (#8)

Browse files

- Load the repo's own LFM3 classes on transformers 5.23 (dcd5c0cc4c1327b4d5554fcdb5889e9da19d4a95)

Files changed (3) hide show
  1. native_lfm3.py +14 -14
  2. native_lfm3_vl.py +15 -17
  3. runner.py +9 -4
native_lfm3.py CHANGED
@@ -1,24 +1,24 @@
1
  """LFM3's transformers classes, one module a model type: `native_lfm3` (text), `native_lfm3_vl` (pictures),
2
  `native_lfm3_omni` (pictures and audio), each importing the one before it, so a Hub repository serves only its
3
- model's files. transformers' own classes once a release ships them; until then copies of transformers' LFM3 code
4
- beside these (each names its commit), registered with the Auto classes, so a checkpoint's config, tokenizer and
5
- processor load and the composite models build their parts.
 
 
6
 
7
- Shipping LFM3, transformers makes the fallbacks dead: delete them and the copies (the files headed "Vendored from"),
8
- nothing else."""
9
 
10
  from __future__ import annotations
11
 
12
- try:
13
- from transformers import Lfm3Config, Lfm3ForCausalLM, Lfm3Model
14
- except ImportError:
15
- from transformers import AutoConfig, AutoModel
16
 
17
- from .configuration_lfm3 import Lfm3Config
18
- from .modeling_lfm3 import Lfm3ForCausalLM, Lfm3Model
19
 
20
- # exist_ok: a second copy of these files in one process (two snapshots) registers again
21
- AutoConfig.register(Lfm3Config.model_type, Lfm3Config, exist_ok=True)
22
- AutoModel.register(Lfm3Config, Lfm3Model, exist_ok=True) # the composite models build their text model by config
 
23
 
24
  __all__ = ["Lfm3Config", "Lfm3ForCausalLM", "Lfm3Model"]
 
1
  """LFM3's transformers classes, one module a model type: `native_lfm3` (text), `native_lfm3_vl` (pictures),
2
  `native_lfm3_omni` (pictures and audio), each importing the one before it, so a Hub repository serves only its
3
+ model's files. The classes are the copies of transformers' LFM3 code beside these (each names its commit), on every
4
+ transformers: the released weights, the recorded losses and `lfm3.lfm3_conv` were made on them. transformers ships
5
+ LFM3 from 5.23 (`lfm3`, `lfm3_vl`) as other code: a torch-only convolution, another picture processor and
6
+ placeholder. So the copies are registered with the Auto classes over transformers' own: a checkpoint's config and
7
+ processor load as these, and the composite models build their parts from these.
8
 
9
+ Moving to transformers' classes is a migration of its own, checked for parity on the released weights; until then
10
+ the copies (the files headed "Vendored from") stay."""
11
 
12
  from __future__ import annotations
13
 
14
+ from transformers import AutoConfig, AutoModel
 
 
 
15
 
16
+ from .configuration_lfm3 import Lfm3Config
17
+ from .modeling_lfm3 import Lfm3ForCausalLM, Lfm3Model
18
 
19
+ # exist_ok: over transformers' class of the model type, and a second copy of these files in one process (two
20
+ # snapshots) registers again. A model mapping is read by config class, so the copies' configs give the copies' models.
21
+ AutoConfig.register(Lfm3Config.model_type, Lfm3Config, exist_ok=True)
22
+ AutoModel.register(Lfm3Config, Lfm3Model, exist_ok=True) # the composite models build their text model by config
23
 
24
  __all__ = ["Lfm3Config", "Lfm3ForCausalLM", "Lfm3Model"]
native_lfm3_vl.py CHANGED
@@ -2,24 +2,22 @@
2
 
3
  from __future__ import annotations
4
 
5
- from .native_lfm3 import Lfm3Model # noqa: F401 (registered: the VL builds its text model by config)
6
-
7
- try:
8
- from transformers import Lfm3VlConfig, Lfm3VlForConditionalGeneration, Lfm3VlProcessor
9
- except ImportError:
10
- from transformers import AutoConfig, AutoImageProcessor, AutoModel, AutoProcessor, AutoVideoProcessor
11
 
12
- from .configuration_lfm3_vl import Lfm3VlConfig, Lfm3VlVisionConfig
13
- from .image_processing_lfm3_vl import Lfm3VlImageProcessor
14
- from .modeling_lfm3_vl import Lfm3VlForConditionalGeneration, Lfm3VlVisionModel
15
- from .processing_lfm3_vl import Lfm3VlProcessor
16
- from .video_processing_lfm3_vl import Lfm3VlVideoProcessor
 
17
 
18
- for config in (Lfm3VlConfig, Lfm3VlVisionConfig):
19
- AutoConfig.register(config.model_type, config, exist_ok=True)
20
- AutoModel.register(Lfm3VlVisionConfig, Lfm3VlVisionModel, exist_ok=True) # the tower, built by config
21
- AutoImageProcessor.register(Lfm3VlConfig, fast_image_processor_class=Lfm3VlImageProcessor, exist_ok=True)
22
- AutoVideoProcessor.register(Lfm3VlConfig, Lfm3VlVideoProcessor, exist_ok=True)
23
- AutoProcessor.register(Lfm3VlConfig, Lfm3VlProcessor, exist_ok=True)
 
 
24
 
25
  __all__ = ["Lfm3VlConfig", "Lfm3VlForConditionalGeneration", "Lfm3VlProcessor"]
 
2
 
3
  from __future__ import annotations
4
 
5
+ from transformers import AutoConfig, AutoImageProcessor, AutoModel, AutoProcessor, AutoVideoProcessor
 
 
 
 
 
6
 
7
+ from .configuration_lfm3_vl import Lfm3VlConfig, Lfm3VlVisionConfig
8
+ from .image_processing_lfm3_vl import Lfm3VlImageProcessor
9
+ from .modeling_lfm3_vl import Lfm3VlForConditionalGeneration, Lfm3VlVisionModel
10
+ from .native_lfm3 import Lfm3Model # noqa: F401 (registered: the VL builds its text model by config)
11
+ from .processing_lfm3_vl import Lfm3VlProcessor
12
+ from .video_processing_lfm3_vl import Lfm3VlVideoProcessor
13
 
14
+ for config in (Lfm3VlConfig, Lfm3VlVisionConfig):
15
+ AutoConfig.register(config.model_type, config, exist_ok=True)
16
+ # the tower, built by config. Never the VL itself: a class registered for its config would load in place of the
17
+ # Hub class a repository's `auto_map` names
18
+ AutoModel.register(Lfm3VlVisionConfig, Lfm3VlVisionModel, exist_ok=True)
19
+ AutoImageProcessor.register(Lfm3VlConfig, fast_image_processor_class=Lfm3VlImageProcessor, exist_ok=True)
20
+ AutoVideoProcessor.register(Lfm3VlConfig, Lfm3VlVideoProcessor, exist_ok=True)
21
+ AutoProcessor.register(Lfm3VlConfig, Lfm3VlProcessor, exist_ok=True) # read by `runner.auto_processor`
22
 
23
  __all__ = ["Lfm3VlConfig", "Lfm3VlForConditionalGeneration", "Lfm3VlProcessor"]
runner.py CHANGED
@@ -70,10 +70,15 @@ def remote_ok(model_id) -> bool:
70
 
71
 
72
  def auto_processor(path: str, revision: str | None = None):
73
- """The media processor a checkpoint holds."""
74
- from transformers import AutoProcessor
75
-
76
- return AutoProcessor.from_pretrained(path, revision=revision, trust_remote_code=remote_ok(path))
 
 
 
 
 
77
 
78
 
79
  def _under(config: dict | None, compiled):
 
70
 
71
 
72
  def auto_processor(path: str, revision: str | None = None):
73
+ """The media processor a checkpoint holds. A model type whose classes are registered from outside transformers
74
+ (copies served beside this file) takes the processor registered with them: by name, `AutoProcessor` finds
75
+ transformers' class of that name first."""
76
+ from transformers import CONFIG_MAPPING, PROCESSOR_MAPPING, AutoProcessor, PretrainedConfig
77
+
78
+ kind = PretrainedConfig.get_config_dict(path, revision=revision)[0].get("model_type")
79
+ own = PROCESSOR_MAPPING.get(CONFIG_MAPPING[kind], None) if kind in CONFIG_MAPPING else None
80
+ cls = AutoProcessor if own is None or own.__module__.startswith("transformers.") else own
81
+ return cls.from_pretrained(path, revision=revision, trust_remote_code=remote_ok(path))
82
 
83
 
84
  def _under(config: dict | None, compiled):