gberton commited on
Commit
ed1e4dc
·
1 Parent(s): 23cfc93

Use relative imports for sibling modules (fixes local loading, save_pretrained, pickling)

Browse files
Files changed (1) hide show
  1. modeling_tips.py +14 -34
modeling_tips.py CHANGED
@@ -1,34 +1,23 @@
1
  """TIPSv2 model for HuggingFace — wraps vision and text encoders."""
2
 
3
- import importlib
4
- import os
5
  from dataclasses import dataclass
6
- from pathlib import Path
7
  from typing import List, Optional, Union
8
 
9
- import numpy as np
10
  import torch
11
- from huggingface_hub import hf_hub_download
12
  from transformers import PreTrainedModel
 
13
 
14
  from .configuration_tips import TIPSv2Config
 
 
15
 
16
- _this_dir = Path(__file__).parent
17
- _sibling_cache = {}
18
-
19
-
20
- def _load_sibling(name, repo_id=None):
21
- """Import a sibling .py from the same dir, downloading from HF if needed."""
22
- if name in _sibling_cache:
23
- return _sibling_cache[name]
24
- path = _this_dir / f"{name}.py"
25
- if not path.exists() and repo_id:
26
- path = Path(hf_hub_download(repo_id, f"{name}.py"))
27
- spec = importlib.util.spec_from_file_location(name, str(path))
28
- mod = importlib.util.module_from_spec(spec)
29
- spec.loader.exec_module(mod)
30
- _sibling_cache[name] = mod
31
- return mod
32
 
33
 
34
  @dataclass
@@ -75,12 +64,7 @@ class TIPSv2Model(PreTrainedModel):
75
  def __init__(self, config: TIPSv2Config):
76
  super().__init__(config)
77
 
78
- repo_id = getattr(config, "_name_or_path", None)
79
- ie = _load_sibling("image_encoder", repo_id)
80
- te = _load_sibling("text_encoder", repo_id)
81
-
82
- build_fn = getattr(ie, config.vision_fn)
83
- self.vision_encoder = build_fn(
84
  img_size=config.img_size,
85
  patch_size=config.patch_size,
86
  ffn_layer=config.ffn_layer,
@@ -90,7 +74,7 @@ class TIPSv2Model(PreTrainedModel):
90
  interpolate_offset=0.0,
91
  )
92
 
93
- self.text_encoder = te.TextEncoder(
94
  config={
95
  "hidden_size": config.text_hidden_size,
96
  "mlp_dim": config.text_mlp_dim,
@@ -101,14 +85,10 @@ class TIPSv2Model(PreTrainedModel):
101
  )
102
 
103
  self._tokenizer = None
104
- self._te_mod = te
105
 
106
  def _load_tokenizer(self):
107
- """Lazy-load the SentencePiece tokenizer."""
108
- tok_path = _this_dir / "tokenizer.model"
109
- if not tok_path.exists():
110
- tok_path = hf_hub_download(self.name_or_path, "tokenizer.model")
111
- return self._te_mod.Tokenizer(str(tok_path))
112
 
113
  @torch.no_grad()
114
  def encode_image(self, pixel_values: torch.Tensor) -> TIPSv2ImageOutput:
 
1
  """TIPSv2 model for HuggingFace — wraps vision and text encoders."""
2
 
 
 
3
  from dataclasses import dataclass
 
4
  from typing import List, Optional, Union
5
 
 
6
  import torch
 
7
  from transformers import PreTrainedModel
8
+ from transformers.utils import cached_file
9
 
10
  from .configuration_tips import TIPSv2Config
11
+ from .image_encoder import vit_base, vit_giant2, vit_large, vit_small, vit_so400m
12
+ from .text_encoder import TextEncoder, Tokenizer
13
 
14
+ _VISION_FACTORIES = {
15
+ "vit_small": vit_small,
16
+ "vit_base": vit_base,
17
+ "vit_large": vit_large,
18
+ "vit_so400m": vit_so400m,
19
+ "vit_giant2": vit_giant2,
20
+ }
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  @dataclass
 
64
  def __init__(self, config: TIPSv2Config):
65
  super().__init__(config)
66
 
67
+ self.vision_encoder = _VISION_FACTORIES[config.vision_fn](
 
 
 
 
 
68
  img_size=config.img_size,
69
  patch_size=config.patch_size,
70
  ffn_layer=config.ffn_layer,
 
74
  interpolate_offset=0.0,
75
  )
76
 
77
+ self.text_encoder = TextEncoder(
78
  config={
79
  "hidden_size": config.text_hidden_size,
80
  "mlp_dim": config.text_mlp_dim,
 
85
  )
86
 
87
  self._tokenizer = None
 
88
 
89
  def _load_tokenizer(self):
90
+ """Load the SentencePiece tokenizer shipped with the checkpoint."""
91
+ return Tokenizer(cached_file(self.name_or_path, "tokenizer.model"))
 
 
 
92
 
93
  @torch.no_grad()
94
  def encode_image(self, pixel_values: torch.Tensor) -> TIPSv2ImageOutput: