nexaai 1.0.9__cp310-cp310-macosx_14_0_universal2.whl → 1.0.11rc1__cp310-cp310-macosx_14_0_universal2.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nexaai might be problematic. Click here for more details.
- nexaai/_stub.cpython-310-darwin.so +0 -0
- nexaai/_version.py +1 -1
- nexaai/binds/common_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/embedder_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/libnexa_bridge.dylib +0 -0
- nexaai/binds/llm_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/nexa_llama_cpp/libggml-base.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libggml-cpu.so +0 -0
- nexaai/binds/nexa_llama_cpp/libggml-metal.so +0 -0
- nexaai/binds/nexa_llama_cpp/libggml.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libllama.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libmtmd.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libnexa_plugin.dylib +0 -0
- nexaai/binds/nexa_mlx/libnexa_plugin.dylib +0 -0
- nexaai/embedder_impl/mlx_embedder_impl.py +5 -6
- nexaai/mlx_backend/embedding/generate.py +16 -219
- nexaai/mlx_backend/embedding/interface.py +41 -346
- nexaai/mlx_backend/embedding/main.py +35 -126
- nexaai/utils/model_manager.py +87 -103
- nexaai/utils/progress_tracker.py +8 -12
- {nexaai-1.0.9.dist-info → nexaai-1.0.11rc1.dist-info}/METADATA +1 -2
- {nexaai-1.0.9.dist-info → nexaai-1.0.11rc1.dist-info}/RECORD +24 -27
- nexaai/utils/manifest_utils.py +0 -280
- nexaai/utils/model_types.py +0 -47
- nexaai/utils/quantization_utils.py +0 -239
- {nexaai-1.0.9.dist-info → nexaai-1.0.11rc1.dist-info}/WHEEL +0 -0
- {nexaai-1.0.9.dist-info → nexaai-1.0.11rc1.dist-info}/top_level.txt +0 -0
nexaai/utils/manifest_utils.py
DELETED
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Manifest and metadata utilities for handling nexa.manifest files and model metadata.
|
|
3
|
-
|
|
4
|
-
This module provides utilities to:
|
|
5
|
-
- Load and save nexa.manifest files
|
|
6
|
-
- Create GGUF and MLX manifests
|
|
7
|
-
- Process manifest metadata (handle null fields, fetch avatars, etc.)
|
|
8
|
-
- Manage backward compatibility with old download_metadata.json files
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
import os
|
|
12
|
-
import json
|
|
13
|
-
from datetime import datetime
|
|
14
|
-
from typing import Dict, Any, List, Optional
|
|
15
|
-
|
|
16
|
-
from .quantization_utils import (
|
|
17
|
-
extract_quantization_from_filename,
|
|
18
|
-
detect_quantization_for_mlx
|
|
19
|
-
)
|
|
20
|
-
from .model_types import (
|
|
21
|
-
PIPELINE_TO_MODEL_TYPE,
|
|
22
|
-
MODEL_TYPE_TO_PIPELINE
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def process_manifest_metadata(manifest: Dict[str, Any], repo_id: str) -> Dict[str, Any]:
|
|
27
|
-
"""Process manifest metadata to handle null/missing fields."""
|
|
28
|
-
# Handle pipeline_tag
|
|
29
|
-
pipeline_tag = manifest.get('pipeline_tag')
|
|
30
|
-
if not pipeline_tag:
|
|
31
|
-
# Reverse map from ModelType if available
|
|
32
|
-
model_type = manifest.get('ModelType')
|
|
33
|
-
pipeline_tag = MODEL_TYPE_TO_PIPELINE.get(model_type) if model_type else None
|
|
34
|
-
|
|
35
|
-
# Handle download_time - keep as null if missing
|
|
36
|
-
download_time = manifest.get('download_time')
|
|
37
|
-
|
|
38
|
-
# Handle avatar_url - fetch on-the-fly if missing/null
|
|
39
|
-
avatar_url = manifest.get('avatar_url')
|
|
40
|
-
if not avatar_url:
|
|
41
|
-
try:
|
|
42
|
-
from .avatar_fetcher import get_avatar_url_for_repo
|
|
43
|
-
avatar_url = get_avatar_url_for_repo(repo_id)
|
|
44
|
-
except Exception:
|
|
45
|
-
# If fetching fails, leave as None
|
|
46
|
-
avatar_url = None
|
|
47
|
-
|
|
48
|
-
# Return processed metadata
|
|
49
|
-
processed_manifest = manifest.copy()
|
|
50
|
-
processed_manifest.update({
|
|
51
|
-
'pipeline_tag': pipeline_tag,
|
|
52
|
-
'download_time': download_time,
|
|
53
|
-
'avatar_url': avatar_url
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
return processed_manifest
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def load_nexa_manifest(directory_path: str) -> Dict[str, Any]:
|
|
60
|
-
"""Load manifest from nexa.manifest if it exists."""
|
|
61
|
-
manifest_path = os.path.join(directory_path, 'nexa.manifest')
|
|
62
|
-
if os.path.exists(manifest_path):
|
|
63
|
-
try:
|
|
64
|
-
with open(manifest_path, 'r', encoding='utf-8') as f:
|
|
65
|
-
return json.load(f)
|
|
66
|
-
except (json.JSONDecodeError, IOError):
|
|
67
|
-
pass
|
|
68
|
-
return {}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
def load_download_metadata(directory_path: str, repo_id: Optional[str] = None) -> Dict[str, Any]:
|
|
72
|
-
"""Load download metadata from nexa.manifest if it exists, fallback to old format."""
|
|
73
|
-
# First try to load from new manifest format
|
|
74
|
-
manifest = load_nexa_manifest(directory_path)
|
|
75
|
-
if manifest and repo_id:
|
|
76
|
-
# Process the manifest to handle null/missing fields
|
|
77
|
-
return process_manifest_metadata(manifest, repo_id)
|
|
78
|
-
elif manifest:
|
|
79
|
-
# Return manifest as-is if no repo_id provided (for backward compatibility)
|
|
80
|
-
return manifest
|
|
81
|
-
|
|
82
|
-
# Fallback to old format for backward compatibility
|
|
83
|
-
old_metadata_path = os.path.join(directory_path, 'download_metadata.json')
|
|
84
|
-
if os.path.exists(old_metadata_path):
|
|
85
|
-
try:
|
|
86
|
-
with open(old_metadata_path, 'r', encoding='utf-8') as f:
|
|
87
|
-
return json.load(f)
|
|
88
|
-
except (json.JSONDecodeError, IOError):
|
|
89
|
-
pass
|
|
90
|
-
return {}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
def save_download_metadata(directory_path: str, metadata: Dict[str, Any]) -> None:
|
|
94
|
-
"""Save download metadata to nexa.manifest in the new format."""
|
|
95
|
-
manifest_path = os.path.join(directory_path, 'nexa.manifest')
|
|
96
|
-
try:
|
|
97
|
-
with open(manifest_path, 'w', encoding='utf-8') as f:
|
|
98
|
-
json.dump(metadata, f, indent=2)
|
|
99
|
-
except IOError:
|
|
100
|
-
# If we can't save metadata, don't fail the download
|
|
101
|
-
pass
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def create_gguf_manifest(repo_id: str, files: List[str], directory_path: str, old_metadata: Dict[str, Any]) -> Dict[str, Any]:
|
|
105
|
-
"""Create GGUF format manifest."""
|
|
106
|
-
|
|
107
|
-
# Load existing manifest to merge GGUF files if it exists
|
|
108
|
-
existing_manifest = load_nexa_manifest(directory_path)
|
|
109
|
-
|
|
110
|
-
model_files = {}
|
|
111
|
-
if existing_manifest and "ModelFile" in existing_manifest:
|
|
112
|
-
model_files = existing_manifest["ModelFile"].copy()
|
|
113
|
-
|
|
114
|
-
# Process GGUF files
|
|
115
|
-
for file_name in files:
|
|
116
|
-
if file_name.endswith('.gguf'):
|
|
117
|
-
# Use the new enum-based quantization extraction
|
|
118
|
-
quantization_type = extract_quantization_from_filename(file_name)
|
|
119
|
-
quant_level = quantization_type.value if quantization_type else "UNKNOWN"
|
|
120
|
-
|
|
121
|
-
file_path = os.path.join(directory_path, file_name)
|
|
122
|
-
file_size = 0
|
|
123
|
-
if os.path.exists(file_path):
|
|
124
|
-
try:
|
|
125
|
-
file_size = os.path.getsize(file_path)
|
|
126
|
-
except (OSError, IOError):
|
|
127
|
-
pass
|
|
128
|
-
|
|
129
|
-
model_files[quant_level] = {
|
|
130
|
-
"Name": file_name,
|
|
131
|
-
"Downloaded": True,
|
|
132
|
-
"Size": file_size
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
manifest = {
|
|
136
|
-
"Name": repo_id,
|
|
137
|
-
"ModelType": PIPELINE_TO_MODEL_TYPE.get(old_metadata.get('pipeline_tag'), "other"),
|
|
138
|
-
"PluginId": "llama_cpp",
|
|
139
|
-
"ModelFile": model_files,
|
|
140
|
-
"MMProjFile": {
|
|
141
|
-
"Name": "",
|
|
142
|
-
"Downloaded": False,
|
|
143
|
-
"Size": 0
|
|
144
|
-
},
|
|
145
|
-
"TokenizerFile": {
|
|
146
|
-
"Name": "",
|
|
147
|
-
"Downloaded": False,
|
|
148
|
-
"Size": 0
|
|
149
|
-
},
|
|
150
|
-
"ExtraFiles": None,
|
|
151
|
-
# Preserve old metadata fields
|
|
152
|
-
"pipeline_tag": old_metadata.get('pipeline_tag'),
|
|
153
|
-
"download_time": old_metadata.get('download_time'),
|
|
154
|
-
"avatar_url": old_metadata.get('avatar_url')
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return manifest
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
def create_mlx_manifest(repo_id: str, files: List[str], directory_path: str, old_metadata: Dict[str, Any]) -> Dict[str, Any]:
|
|
161
|
-
"""Create MLX format manifest."""
|
|
162
|
-
|
|
163
|
-
model_files = {}
|
|
164
|
-
extra_files = []
|
|
165
|
-
|
|
166
|
-
# Try different methods to extract quantization for MLX models
|
|
167
|
-
quantization_type = detect_quantization_for_mlx(repo_id, directory_path)
|
|
168
|
-
|
|
169
|
-
# Use the detected quantization or default to "DEFAULT"
|
|
170
|
-
quant_level = quantization_type.value if quantization_type else "DEFAULT"
|
|
171
|
-
|
|
172
|
-
for file_name in files:
|
|
173
|
-
file_path = os.path.join(directory_path, file_name)
|
|
174
|
-
file_size = 0
|
|
175
|
-
if os.path.exists(file_path):
|
|
176
|
-
try:
|
|
177
|
-
file_size = os.path.getsize(file_path)
|
|
178
|
-
except (OSError, IOError):
|
|
179
|
-
pass
|
|
180
|
-
|
|
181
|
-
# Check if this is a main model file (safetensors but not index files)
|
|
182
|
-
if (file_name.endswith('.safetensors') and not file_name.endswith('.index.json')):
|
|
183
|
-
model_files[quant_level] = {
|
|
184
|
-
"Name": file_name,
|
|
185
|
-
"Downloaded": True,
|
|
186
|
-
"Size": file_size
|
|
187
|
-
}
|
|
188
|
-
else:
|
|
189
|
-
# Add to extra files
|
|
190
|
-
extra_files.append({
|
|
191
|
-
"Name": file_name,
|
|
192
|
-
"Downloaded": True,
|
|
193
|
-
"Size": file_size
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
manifest = {
|
|
197
|
-
"Name": repo_id,
|
|
198
|
-
"ModelType": PIPELINE_TO_MODEL_TYPE.get(old_metadata.get('pipeline_tag'), "other"),
|
|
199
|
-
"PluginId": "mlx",
|
|
200
|
-
"ModelFile": model_files,
|
|
201
|
-
"MMProjFile": {
|
|
202
|
-
"Name": "",
|
|
203
|
-
"Downloaded": False,
|
|
204
|
-
"Size": 0
|
|
205
|
-
},
|
|
206
|
-
"TokenizerFile": {
|
|
207
|
-
"Name": "",
|
|
208
|
-
"Downloaded": False,
|
|
209
|
-
"Size": 0
|
|
210
|
-
},
|
|
211
|
-
"ExtraFiles": extra_files if extra_files else None,
|
|
212
|
-
# Preserve old metadata fields
|
|
213
|
-
"pipeline_tag": old_metadata.get('pipeline_tag'),
|
|
214
|
-
"download_time": old_metadata.get('download_time'),
|
|
215
|
-
"avatar_url": old_metadata.get('avatar_url')
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return manifest
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
def detect_model_type(files: List[str]) -> str:
|
|
222
|
-
"""Detect if this is a GGUF or MLX model based on file extensions."""
|
|
223
|
-
has_gguf = any(f.endswith('.gguf') for f in files)
|
|
224
|
-
has_safetensors = any(f.endswith('.safetensors') or 'safetensors' in f for f in files)
|
|
225
|
-
|
|
226
|
-
if has_gguf:
|
|
227
|
-
return "gguf"
|
|
228
|
-
elif has_safetensors:
|
|
229
|
-
return "mlx"
|
|
230
|
-
else:
|
|
231
|
-
# Default to mlx for other types
|
|
232
|
-
return "mlx"
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
def create_manifest_from_files(repo_id: str, files: List[str], directory_path: str, old_metadata: Dict[str, Any]) -> Dict[str, Any]:
|
|
236
|
-
"""
|
|
237
|
-
Create appropriate manifest format based on detected model type.
|
|
238
|
-
|
|
239
|
-
Args:
|
|
240
|
-
repo_id: Repository ID
|
|
241
|
-
files: List of files in the model directory
|
|
242
|
-
directory_path: Path to the model directory
|
|
243
|
-
old_metadata: Existing metadata (pipeline_tag, download_time, avatar_url)
|
|
244
|
-
|
|
245
|
-
Returns:
|
|
246
|
-
Dict containing the appropriate manifest format
|
|
247
|
-
"""
|
|
248
|
-
model_type = detect_model_type(files)
|
|
249
|
-
|
|
250
|
-
if model_type == "gguf":
|
|
251
|
-
return create_gguf_manifest(repo_id, files, directory_path, old_metadata)
|
|
252
|
-
else: # mlx or other
|
|
253
|
-
return create_mlx_manifest(repo_id, files, directory_path, old_metadata)
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
def save_manifest_with_files_metadata(repo_id: str, local_dir: str, old_metadata: Dict[str, Any]) -> None:
|
|
257
|
-
"""
|
|
258
|
-
Create and save manifest based on files found in the directory.
|
|
259
|
-
|
|
260
|
-
Args:
|
|
261
|
-
repo_id: Repository ID
|
|
262
|
-
local_dir: Local directory containing the model files
|
|
263
|
-
old_metadata: Existing metadata to preserve
|
|
264
|
-
"""
|
|
265
|
-
# Get list of files in the directory
|
|
266
|
-
files = []
|
|
267
|
-
try:
|
|
268
|
-
for root, dirs, filenames in os.walk(local_dir):
|
|
269
|
-
for filename in filenames:
|
|
270
|
-
# Store relative path from the directory
|
|
271
|
-
rel_path = os.path.relpath(os.path.join(root, filename), local_dir)
|
|
272
|
-
files.append(rel_path)
|
|
273
|
-
except (OSError, IOError):
|
|
274
|
-
pass
|
|
275
|
-
|
|
276
|
-
# Create appropriate manifest
|
|
277
|
-
manifest = create_manifest_from_files(repo_id, files, local_dir, old_metadata)
|
|
278
|
-
|
|
279
|
-
# Save manifest
|
|
280
|
-
save_download_metadata(local_dir, manifest)
|
nexaai/utils/model_types.py
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Model type mappings for HuggingFace pipeline tags to our internal model types.
|
|
3
|
-
|
|
4
|
-
This module provides centralized model type mapping functionality to avoid
|
|
5
|
-
circular imports between other utility modules.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from enum import Enum
|
|
9
|
-
from typing import Dict
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class ModelTypeMapping(Enum):
|
|
13
|
-
"""Enum for mapping HuggingFace pipeline_tag to our ModelType."""
|
|
14
|
-
TEXT_GENERATION = ("text-generation", "llm")
|
|
15
|
-
IMAGE_TEXT_TO_TEXT = ("image-text-to-text", "vlm")
|
|
16
|
-
|
|
17
|
-
def __init__(self, pipeline_tag: str, model_type: str):
|
|
18
|
-
self.pipeline_tag = pipeline_tag
|
|
19
|
-
self.model_type = model_type
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
# Create mapping dictionaries from the enum
|
|
23
|
-
PIPELINE_TO_MODEL_TYPE: Dict[str, str] = {
|
|
24
|
-
mapping.pipeline_tag: mapping.model_type
|
|
25
|
-
for mapping in ModelTypeMapping
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
MODEL_TYPE_TO_PIPELINE: Dict[str, str] = {
|
|
29
|
-
mapping.model_type: mapping.pipeline_tag
|
|
30
|
-
for mapping in ModelTypeMapping
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def map_pipeline_tag_to_model_type(pipeline_tag: str) -> str:
|
|
35
|
-
"""Map HuggingFace pipeline_tag to our ModelType."""
|
|
36
|
-
if not pipeline_tag:
|
|
37
|
-
return "other"
|
|
38
|
-
|
|
39
|
-
return PIPELINE_TO_MODEL_TYPE.get(pipeline_tag, "other")
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
def map_model_type_to_pipeline_tag(model_type: str) -> str:
|
|
43
|
-
"""Reverse map ModelType back to HuggingFace pipeline_tag."""
|
|
44
|
-
if not model_type:
|
|
45
|
-
return None
|
|
46
|
-
|
|
47
|
-
return MODEL_TYPE_TO_PIPELINE.get(model_type)
|
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Quantization utilities for extracting quantization types from model files and configurations.
|
|
3
|
-
|
|
4
|
-
This module provides utilities to extract quantization information from:
|
|
5
|
-
- GGUF model filenames
|
|
6
|
-
- MLX model repository IDs
|
|
7
|
-
- MLX model config.json files
|
|
8
|
-
"""
|
|
9
|
-
|
|
10
|
-
import os
|
|
11
|
-
import json
|
|
12
|
-
import re
|
|
13
|
-
import logging
|
|
14
|
-
from enum import Enum
|
|
15
|
-
from typing import Optional
|
|
16
|
-
|
|
17
|
-
# Set up logger
|
|
18
|
-
logger = logging.getLogger(__name__)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class QuantizationType(str, Enum):
|
|
22
|
-
"""Enum for GGUF and MLX model quantization types."""
|
|
23
|
-
# GGUF quantization types
|
|
24
|
-
BF16 = "BF16"
|
|
25
|
-
F16 = "F16"
|
|
26
|
-
Q2_K = "Q2_K"
|
|
27
|
-
Q2_K_L = "Q2_K_L"
|
|
28
|
-
Q3_K_M = "Q3_K_M"
|
|
29
|
-
Q3_K_S = "Q3_K_S"
|
|
30
|
-
Q4_0 = "Q4_0"
|
|
31
|
-
Q4_1 = "Q4_1"
|
|
32
|
-
Q4_K_M = "Q4_K_M"
|
|
33
|
-
Q4_K_S = "Q4_K_S"
|
|
34
|
-
Q5_K_M = "Q5_K_M"
|
|
35
|
-
Q5_K_S = "Q5_K_S"
|
|
36
|
-
Q6_K = "Q6_K"
|
|
37
|
-
Q8_0 = "Q8_0"
|
|
38
|
-
MXFP4 = "MXFP4"
|
|
39
|
-
MXFP8 = "MXFP8"
|
|
40
|
-
|
|
41
|
-
# MLX bit-based quantization types
|
|
42
|
-
BIT_1 = "1BIT"
|
|
43
|
-
BIT_2 = "2BIT"
|
|
44
|
-
BIT_3 = "3BIT"
|
|
45
|
-
BIT_4 = "4BIT"
|
|
46
|
-
BIT_5 = "5BIT"
|
|
47
|
-
BIT_6 = "6BIT"
|
|
48
|
-
BIT_7 = "7BIT"
|
|
49
|
-
BIT_8 = "8BIT"
|
|
50
|
-
BIT_16 = "16BIT"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def extract_quantization_from_filename(filename: str) -> Optional[QuantizationType]:
|
|
54
|
-
"""
|
|
55
|
-
Extract quantization type from filename.
|
|
56
|
-
|
|
57
|
-
Args:
|
|
58
|
-
filename: The filename to extract quantization from
|
|
59
|
-
|
|
60
|
-
Returns:
|
|
61
|
-
QuantizationType enum value or None if not found
|
|
62
|
-
"""
|
|
63
|
-
# Define mapping from lowercase patterns to enum values
|
|
64
|
-
# Include "." to ensure precise matching (e.g., "q4_0." not "q4_0_xl")
|
|
65
|
-
pattern_to_enum = {
|
|
66
|
-
'bf16.': QuantizationType.BF16,
|
|
67
|
-
'f16.': QuantizationType.F16, # Add F16 support
|
|
68
|
-
'q2_k_l.': QuantizationType.Q2_K_L, # Check Q2_K_L before Q2_K to avoid partial match
|
|
69
|
-
'q2_k.': QuantizationType.Q2_K,
|
|
70
|
-
'q3_k_m.': QuantizationType.Q3_K_M,
|
|
71
|
-
'q3_ks.': QuantizationType.Q3_K_S,
|
|
72
|
-
'q4_k_m.': QuantizationType.Q4_K_M,
|
|
73
|
-
'q4_k_s.': QuantizationType.Q4_K_S,
|
|
74
|
-
'q4_0.': QuantizationType.Q4_0,
|
|
75
|
-
'q4_1.': QuantizationType.Q4_1,
|
|
76
|
-
'q5_k_m.': QuantizationType.Q5_K_M,
|
|
77
|
-
'q5_k_s.': QuantizationType.Q5_K_S,
|
|
78
|
-
'q6_k.': QuantizationType.Q6_K,
|
|
79
|
-
'q8_0.': QuantizationType.Q8_0,
|
|
80
|
-
'mxfp4.': QuantizationType.MXFP4,
|
|
81
|
-
'mxfp8.': QuantizationType.MXFP8,
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
filename_lower = filename.lower()
|
|
85
|
-
|
|
86
|
-
# Check longer patterns first to avoid partial matches
|
|
87
|
-
# Sort by length descending to check q2_k_l before q2_k, q4_k_m before q4_0, etc.
|
|
88
|
-
for pattern in sorted(pattern_to_enum.keys(), key=len, reverse=True):
|
|
89
|
-
if pattern in filename_lower:
|
|
90
|
-
return pattern_to_enum[pattern]
|
|
91
|
-
|
|
92
|
-
return None
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def extract_quantization_from_repo_id(repo_id: str) -> Optional[QuantizationType]:
|
|
96
|
-
"""
|
|
97
|
-
Extract quantization type from repo_id for MLX models by looking for bit patterns.
|
|
98
|
-
|
|
99
|
-
Args:
|
|
100
|
-
repo_id: The repository ID to extract quantization from
|
|
101
|
-
|
|
102
|
-
Returns:
|
|
103
|
-
QuantizationType enum value or None if not found
|
|
104
|
-
"""
|
|
105
|
-
# Define mapping from bit numbers to enum values
|
|
106
|
-
bit_to_enum = {
|
|
107
|
-
1: QuantizationType.BIT_1,
|
|
108
|
-
2: QuantizationType.BIT_2,
|
|
109
|
-
3: QuantizationType.BIT_3,
|
|
110
|
-
4: QuantizationType.BIT_4,
|
|
111
|
-
5: QuantizationType.BIT_5,
|
|
112
|
-
6: QuantizationType.BIT_6,
|
|
113
|
-
7: QuantizationType.BIT_7,
|
|
114
|
-
8: QuantizationType.BIT_8,
|
|
115
|
-
16: QuantizationType.BIT_16,
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
# First check for patterns like "4bit", "8bit" etc. (case insensitive)
|
|
119
|
-
pattern = r'(\d+)bit'
|
|
120
|
-
matches = re.findall(pattern, repo_id.lower())
|
|
121
|
-
|
|
122
|
-
for match in matches:
|
|
123
|
-
try:
|
|
124
|
-
bit_number = int(match)
|
|
125
|
-
if bit_number in bit_to_enum:
|
|
126
|
-
logger.debug(f"Found {bit_number}bit quantization in repo_id: {repo_id}")
|
|
127
|
-
return bit_to_enum[bit_number]
|
|
128
|
-
except ValueError:
|
|
129
|
-
continue
|
|
130
|
-
|
|
131
|
-
# Also check for patterns like "-q8", "_Q4" etc.
|
|
132
|
-
q_pattern = r'[-_]q(\d+)'
|
|
133
|
-
q_matches = re.findall(q_pattern, repo_id.lower())
|
|
134
|
-
|
|
135
|
-
for match in q_matches:
|
|
136
|
-
try:
|
|
137
|
-
bit_number = int(match)
|
|
138
|
-
if bit_number in bit_to_enum:
|
|
139
|
-
logger.debug(f"Found Q{bit_number} quantization in repo_id: {repo_id}")
|
|
140
|
-
return bit_to_enum[bit_number]
|
|
141
|
-
except ValueError:
|
|
142
|
-
continue
|
|
143
|
-
|
|
144
|
-
return None
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def extract_quantization_from_mlx_config(mlx_folder_path: str) -> Optional[QuantizationType]:
|
|
148
|
-
"""
|
|
149
|
-
Extract quantization type from MLX model's config.json file.
|
|
150
|
-
|
|
151
|
-
Args:
|
|
152
|
-
mlx_folder_path: Path to the MLX model folder
|
|
153
|
-
|
|
154
|
-
Returns:
|
|
155
|
-
QuantizationType enum value or None if not found
|
|
156
|
-
"""
|
|
157
|
-
config_path = os.path.join(mlx_folder_path, "config.json")
|
|
158
|
-
|
|
159
|
-
if not os.path.exists(config_path):
|
|
160
|
-
logger.debug(f"Config file not found: {config_path}")
|
|
161
|
-
return None
|
|
162
|
-
|
|
163
|
-
try:
|
|
164
|
-
with open(config_path, 'r', encoding='utf-8') as f:
|
|
165
|
-
config = json.load(f)
|
|
166
|
-
|
|
167
|
-
# Look for quantization.bits field
|
|
168
|
-
quantization_config = config.get("quantization", {})
|
|
169
|
-
if isinstance(quantization_config, dict):
|
|
170
|
-
bits = quantization_config.get("bits")
|
|
171
|
-
if isinstance(bits, int):
|
|
172
|
-
# Define mapping from bit numbers to enum values
|
|
173
|
-
bit_to_enum = {
|
|
174
|
-
1: QuantizationType.BIT_1,
|
|
175
|
-
2: QuantizationType.BIT_2,
|
|
176
|
-
3: QuantizationType.BIT_3,
|
|
177
|
-
4: QuantizationType.BIT_4,
|
|
178
|
-
5: QuantizationType.BIT_5,
|
|
179
|
-
6: QuantizationType.BIT_6,
|
|
180
|
-
7: QuantizationType.BIT_7,
|
|
181
|
-
8: QuantizationType.BIT_8,
|
|
182
|
-
16: QuantizationType.BIT_16,
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if bits in bit_to_enum:
|
|
186
|
-
logger.debug(f"Found {bits}bit quantization in config.json: {config_path}")
|
|
187
|
-
return bit_to_enum[bits]
|
|
188
|
-
else:
|
|
189
|
-
logger.debug(f"Unsupported quantization bits value: {bits}")
|
|
190
|
-
|
|
191
|
-
except (json.JSONDecodeError, IOError) as e:
|
|
192
|
-
logger.warning(f"Error reading config.json from {config_path}: {e}")
|
|
193
|
-
except Exception as e:
|
|
194
|
-
logger.warning(f"Unexpected error reading config.json from {config_path}: {e}")
|
|
195
|
-
|
|
196
|
-
return None
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
def extract_gguf_quantization(filename: str) -> str:
|
|
200
|
-
"""
|
|
201
|
-
Extract quantization level from GGUF filename using the enum-based approach.
|
|
202
|
-
|
|
203
|
-
This function provides backward compatibility by returning a string representation
|
|
204
|
-
of the quantization type.
|
|
205
|
-
|
|
206
|
-
Args:
|
|
207
|
-
filename: The GGUF filename
|
|
208
|
-
|
|
209
|
-
Returns:
|
|
210
|
-
String representation of the quantization type or "UNKNOWN" if not found
|
|
211
|
-
"""
|
|
212
|
-
quantization_type = extract_quantization_from_filename(filename)
|
|
213
|
-
if quantization_type:
|
|
214
|
-
return quantization_type.value
|
|
215
|
-
return "UNKNOWN"
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
def detect_quantization_for_mlx(repo_id: str, directory_path: str) -> Optional[QuantizationType]:
|
|
219
|
-
"""
|
|
220
|
-
Detect quantization for MLX models using multiple methods in priority order.
|
|
221
|
-
|
|
222
|
-
Args:
|
|
223
|
-
repo_id: The repository ID
|
|
224
|
-
directory_path: Path to the model directory
|
|
225
|
-
|
|
226
|
-
Returns:
|
|
227
|
-
QuantizationType enum value or None if not found
|
|
228
|
-
"""
|
|
229
|
-
# Method 1: Extract from repo_id
|
|
230
|
-
quantization_type = extract_quantization_from_repo_id(repo_id)
|
|
231
|
-
if quantization_type:
|
|
232
|
-
return quantization_type
|
|
233
|
-
|
|
234
|
-
# Method 2: Extract from config.json if available
|
|
235
|
-
quantization_type = extract_quantization_from_mlx_config(directory_path)
|
|
236
|
-
if quantization_type:
|
|
237
|
-
return quantization_type
|
|
238
|
-
|
|
239
|
-
return None
|
|
File without changes
|
|
File without changes
|