calogero-jerik-scozzaro commited on
Commit
ea74213
·
verified ·
1 Parent(s): 0c5f7ff

Update modeling_matformer.py

Browse files
Files changed (1) hide show
  1. modeling_matformer.py +28 -137
modeling_matformer.py CHANGED
@@ -1,156 +1,47 @@
1
  # modeling_matformer.py
2
-
3
- import importlib
4
  import os
5
- import site
6
- import subprocess
7
  import sys
8
- import sysconfig
9
-
10
-
11
- MATFORMER_VERSION = "alberturin-v1"
12
- MATFORMER_REPOSITORY = "https://github.com/mrinaldi97/matformer.git"
13
- MATFORMER_INSTALL_URL = (
14
- f"git+{MATFORMER_REPOSITORY}@{MATFORMER_VERSION}"
15
- )
16
-
17
-
18
- def _add_matformer_root():
19
- """
20
- Optional development override.
21
-
22
- If MATFORMER_ROOT is defined, prepend it to sys.path so that a local
23
- checkout of Matformer can be used instead of the installed package.
24
- """
25
- matformer_root = os.getenv("MATFORMER_ROOT")
26
-
27
- if not matformer_root:
28
- return
29
-
30
- matformer_root = os.path.abspath(
31
- os.path.expanduser(matformer_root)
32
- )
33
 
 
 
 
34
  if matformer_root not in sys.path:
35
  sys.path.insert(0, matformer_root)
36
 
37
-
38
- def _clear_matformer_import_cache():
39
- """
40
- Remove partially imported Matformer modules from sys.modules.
41
-
42
- This is necessary because the first import can fail before Matformer
43
- is installed, leaving stale module entries in the running interpreter.
44
- """
45
- for module_name in list(sys.modules):
46
- if (
47
- module_name == "matformer"
48
- or module_name.startswith("matformer.")
49
- ):
50
- del sys.modules[module_name]
51
-
52
- importlib.invalidate_caches()
53
-
54
-
55
- def _refresh_site_packages():
56
- """
57
- Refresh site-packages after pip installs a package while the current
58
- Python interpreter is already running.
59
- """
60
- paths = sysconfig.get_paths()
61
-
62
- for key in ("purelib", "platlib"):
63
- path = paths.get(key)
64
-
65
- if path and os.path.isdir(path):
66
- site.addsitedir(path)
67
-
68
- importlib.invalidate_caches()
69
-
70
-
71
- def _install_matformer():
72
- """
73
- Install the exact Matformer release used by AlBERTurin.
74
- """
75
- print(
76
- f"Matformer is not available. "
77
- f"Installing Matformer {MATFORMER_VERSION}..."
78
- )
79
-
80
- subprocess.check_call(
81
- [
82
- sys.executable,
83
- "-m",
84
- "pip",
85
- "install",
86
- MATFORMER_INSTALL_URL,
87
- ]
88
- )
89
-
90
- _refresh_site_packages()
91
- _clear_matformer_import_cache()
92
-
93
-
94
- def _load_matformer():
95
- """
96
- Import and register the Hugging Face integration from Matformer.
97
- """
98
  from matformer.huggingface_integration import (
99
- MatformerConfig,
100
  MatformerForCausalLM,
101
  MatformerForMaskedLM,
102
  MatformerForSequenceClassification,
103
  MatformerModel,
104
- register_matformer,
105
- )
106
-
107
- register_matformer()
108
-
109
- return (
110
  MatformerConfig,
111
- MatformerModel,
112
- MatformerForMaskedLM,
113
- MatformerForCausalLM,
114
- MatformerForSequenceClassification,
115
  )
116
-
117
-
118
- # -------------------------------------------------------------------------
119
- # Initialization
120
- # -------------------------------------------------------------------------
121
-
122
- _add_matformer_root()
123
-
124
- try:
125
- (
126
- MatformerConfig,
127
- MatformerModel,
128
- MatformerForMaskedLM,
129
- MatformerForCausalLM,
130
- MatformerForSequenceClassification,
131
- ) = _load_matformer()
132
-
133
- except ModuleNotFoundError as first_error:
134
-
135
  try:
136
- _clear_matformer_import_cache()
137
- _install_matformer()
138
-
139
- (
140
- MatformerConfig,
141
- MatformerModel,
142
- MatformerForMaskedLM,
143
  MatformerForCausalLM,
 
144
  MatformerForSequenceClassification,
145
- ) = _load_matformer()
146
-
 
 
 
 
147
  except Exception as install_error:
148
  raise ImportError(
149
- "\n"
150
- "Failed to automatically install or import the Matformer "
151
- "version required by AlBERTurin.\n\n"
152
- "You can install it manually with:\n\n"
153
- f" pip install {MATFORMER_INSTALL_URL}\n\n"
154
- "Alternatively, set MATFORMER_ROOT to a local Matformer "
155
- "checkout.\n"
156
- ) from install_error
 
1
  # modeling_matformer.py
 
 
2
  import os
 
 
3
  import sys
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ matformer_root = os.getenv("MATFORMER_ROOT")
6
+ if matformer_root:
7
+ matformer_root = os.path.abspath(os.path.expanduser(matformer_root))
8
  if matformer_root not in sys.path:
9
  sys.path.insert(0, matformer_root)
10
 
11
+ try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  from matformer.huggingface_integration import (
 
13
  MatformerForCausalLM,
14
  MatformerForMaskedLM,
15
  MatformerForSequenceClassification,
16
  MatformerModel,
 
 
 
 
 
 
17
  MatformerConfig,
18
+ register_matformer
 
 
 
19
  )
20
+ register_matformer()
21
+ except ImportError as e:
22
+ import subprocess
23
+ import tempfile
24
+
25
+ print("Installing Matformer from GitHub...")
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  try:
27
+ subprocess.check_call([
28
+ sys.executable, "-m", "pip", "install",
29
+ "git+https://github.com/mrinaldi97/matformer.git@alberturin-v1"
30
+ ])
31
+
32
+ from matformer.huggingface_integration import (
 
33
  MatformerForCausalLM,
34
+ MatformerForMaskedLM,
35
  MatformerForSequenceClassification,
36
+ MatformerModel,
37
+ MatformerConfig,
38
+ register_matformer
39
+ )
40
+ register_matformer()
41
+
42
  except Exception as install_error:
43
  raise ImportError(
44
+ "Failed to install Matformer. Install manually:\n"
45
+ " pip install git+https://github.com/mrinaldi97/matformer.git\n"
46
+ "Or set MATFORMER_ROOT environment variable"
47
+ ) from install_error