nexaai 1.0.21rc16__cp312-cp312-win_arm64.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/__init__.py +95 -0
- nexaai/_stub.cp312-win_arm64.pyd +0 -0
- nexaai/_version.py +4 -0
- nexaai/asr.py +68 -0
- nexaai/asr_impl/__init__.py +0 -0
- nexaai/asr_impl/mlx_asr_impl.py +92 -0
- nexaai/asr_impl/pybind_asr_impl.py +127 -0
- nexaai/base.py +39 -0
- nexaai/binds/__init__.py +6 -0
- nexaai/binds/asr_bind.cp312-win_arm64.pyd +0 -0
- nexaai/binds/common_bind.cp312-win_arm64.pyd +0 -0
- nexaai/binds/cpu_gpu/ggml-base.dll +0 -0
- nexaai/binds/cpu_gpu/ggml-cpu.dll +0 -0
- nexaai/binds/cpu_gpu/ggml-opencl.dll +0 -0
- nexaai/binds/cpu_gpu/ggml.dll +0 -0
- nexaai/binds/cpu_gpu/libomp140.aarch64.dll +0 -0
- nexaai/binds/cpu_gpu/mtmd.dll +0 -0
- nexaai/binds/cpu_gpu/nexa_cpu_gpu.dll +0 -0
- nexaai/binds/cpu_gpu/nexa_plugin.dll +0 -0
- nexaai/binds/embedder_bind.cp312-win_arm64.pyd +0 -0
- nexaai/binds/libcrypto-3-arm64.dll +0 -0
- nexaai/binds/libssl-3-arm64.dll +0 -0
- nexaai/binds/llm_bind.cp312-win_arm64.pyd +0 -0
- nexaai/binds/nexa_bridge.dll +0 -0
- nexaai/binds/npu/FLAC.dll +0 -0
- nexaai/binds/npu/convnext-sdk.dll +0 -0
- nexaai/binds/npu/embed-gemma-sdk.dll +0 -0
- nexaai/binds/npu/fftw3.dll +0 -0
- nexaai/binds/npu/fftw3f.dll +0 -0
- nexaai/binds/npu/ggml-base.dll +0 -0
- nexaai/binds/npu/ggml-cpu.dll +0 -0
- nexaai/binds/npu/ggml-opencl.dll +0 -0
- nexaai/binds/npu/ggml.dll +0 -0
- nexaai/binds/npu/granite-nano-sdk.dll +0 -0
- nexaai/binds/npu/granite4-sdk.dll +0 -0
- nexaai/binds/npu/htp-files/Genie.dll +0 -0
- nexaai/binds/npu/htp-files/PlatformValidatorShared.dll +0 -0
- nexaai/binds/npu/htp-files/QnnChrometraceProfilingReader.dll +0 -0
- nexaai/binds/npu/htp-files/QnnCpu.dll +0 -0
- nexaai/binds/npu/htp-files/QnnCpuNetRunExtensions.dll +0 -0
- nexaai/binds/npu/htp-files/QnnDsp.dll +0 -0
- nexaai/binds/npu/htp-files/QnnDspNetRunExtensions.dll +0 -0
- nexaai/binds/npu/htp-files/QnnDspV66CalculatorStub.dll +0 -0
- nexaai/binds/npu/htp-files/QnnDspV66Stub.dll +0 -0
- nexaai/binds/npu/htp-files/QnnGenAiTransformer.dll +0 -0
- nexaai/binds/npu/htp-files/QnnGenAiTransformerCpuOpPkg.dll +0 -0
- nexaai/binds/npu/htp-files/QnnGenAiTransformerModel.dll +0 -0
- nexaai/binds/npu/htp-files/QnnGpu.dll +0 -0
- nexaai/binds/npu/htp-files/QnnGpuNetRunExtensions.dll +0 -0
- nexaai/binds/npu/htp-files/QnnGpuProfilingReader.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtp.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpNetRunExtensions.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpOptraceProfilingReader.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpPrepare.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpProfilingReader.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpV68CalculatorStub.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpV68Stub.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpV73CalculatorStub.dll +0 -0
- nexaai/binds/npu/htp-files/QnnHtpV73Stub.dll +0 -0
- nexaai/binds/npu/htp-files/QnnIr.dll +0 -0
- nexaai/binds/npu/htp-files/QnnJsonProfilingReader.dll +0 -0
- nexaai/binds/npu/htp-files/QnnModelDlc.dll +0 -0
- nexaai/binds/npu/htp-files/QnnSaver.dll +0 -0
- nexaai/binds/npu/htp-files/QnnSystem.dll +0 -0
- nexaai/binds/npu/htp-files/SNPE.dll +0 -0
- nexaai/binds/npu/htp-files/SnpeDspV66Stub.dll +0 -0
- nexaai/binds/npu/htp-files/SnpeHtpPrepare.dll +0 -0
- nexaai/binds/npu/htp-files/SnpeHtpV68Stub.dll +0 -0
- nexaai/binds/npu/htp-files/SnpeHtpV73Stub.dll +0 -0
- nexaai/binds/npu/htp-files/calculator.dll +0 -0
- nexaai/binds/npu/htp-files/calculator_htp.dll +0 -0
- nexaai/binds/npu/htp-files/libCalculator_skel.so +0 -0
- nexaai/binds/npu/htp-files/libQnnHtpV73.so +0 -0
- nexaai/binds/npu/htp-files/libQnnHtpV73QemuDriver.so +0 -0
- nexaai/binds/npu/htp-files/libQnnHtpV73Skel.so +0 -0
- nexaai/binds/npu/htp-files/libQnnSaver.so +0 -0
- nexaai/binds/npu/htp-files/libQnnSystem.so +0 -0
- nexaai/binds/npu/htp-files/libSnpeHtpV73Skel.so +0 -0
- nexaai/binds/npu/htp-files/libqnnhtpv73.cat +0 -0
- nexaai/binds/npu/htp-files/libsnpehtpv73.cat +0 -0
- nexaai/binds/npu/jina-rerank-sdk.dll +0 -0
- nexaai/binds/npu/libcrypto-3-arm64.dll +0 -0
- nexaai/binds/npu/libmp3lame.DLL +0 -0
- nexaai/binds/npu/libomp140.aarch64.dll +0 -0
- nexaai/binds/npu/libssl-3-arm64.dll +0 -0
- nexaai/binds/npu/liquid-sdk.dll +0 -0
- nexaai/binds/npu/llama3-3b-sdk.dll +0 -0
- nexaai/binds/npu/mpg123.dll +0 -0
- nexaai/binds/npu/nexa-mm-process.dll +0 -0
- nexaai/binds/npu/nexa-sampling.dll +0 -0
- nexaai/binds/npu/nexa_plugin.dll +0 -0
- nexaai/binds/npu/nexaproc.dll +0 -0
- nexaai/binds/npu/ogg.dll +0 -0
- nexaai/binds/npu/omni-neural-sdk.dll +0 -0
- nexaai/binds/npu/openblas.dll +0 -0
- nexaai/binds/npu/opus.dll +0 -0
- nexaai/binds/npu/paddle-ocr-proc-lib.dll +0 -0
- nexaai/binds/npu/paddleocr-sdk.dll +0 -0
- nexaai/binds/npu/parakeet-sdk.dll +0 -0
- nexaai/binds/npu/phi3-5-sdk.dll +0 -0
- nexaai/binds/npu/phi4-sdk.dll +0 -0
- nexaai/binds/npu/pyannote-sdk.dll +0 -0
- nexaai/binds/npu/qwen3-4b-sdk.dll +0 -0
- nexaai/binds/npu/qwen3vl-sdk.dll +0 -0
- nexaai/binds/npu/qwen3vl-vision.dll +0 -0
- nexaai/binds/npu/rtaudio.dll +0 -0
- nexaai/binds/npu/vorbis.dll +0 -0
- nexaai/binds/npu/vorbisenc.dll +0 -0
- nexaai/binds/npu/yolov12-sdk.dll +0 -0
- nexaai/binds/npu/zlib1.dll +0 -0
- nexaai/binds/rerank_bind.cp312-win_arm64.pyd +0 -0
- nexaai/binds/vlm_bind.cp312-win_arm64.pyd +0 -0
- nexaai/common.py +105 -0
- nexaai/cv.py +93 -0
- nexaai/cv_impl/__init__.py +0 -0
- nexaai/cv_impl/mlx_cv_impl.py +89 -0
- nexaai/cv_impl/pybind_cv_impl.py +32 -0
- nexaai/embedder.py +73 -0
- nexaai/embedder_impl/__init__.py +0 -0
- nexaai/embedder_impl/mlx_embedder_impl.py +118 -0
- nexaai/embedder_impl/pybind_embedder_impl.py +96 -0
- nexaai/image_gen.py +141 -0
- nexaai/image_gen_impl/__init__.py +0 -0
- nexaai/image_gen_impl/mlx_image_gen_impl.py +292 -0
- nexaai/image_gen_impl/pybind_image_gen_impl.py +85 -0
- nexaai/llm.py +98 -0
- nexaai/llm_impl/__init__.py +0 -0
- nexaai/llm_impl/mlx_llm_impl.py +271 -0
- nexaai/llm_impl/pybind_llm_impl.py +220 -0
- nexaai/log.py +92 -0
- nexaai/rerank.py +57 -0
- nexaai/rerank_impl/__init__.py +0 -0
- nexaai/rerank_impl/mlx_rerank_impl.py +94 -0
- nexaai/rerank_impl/pybind_rerank_impl.py +136 -0
- nexaai/runtime.py +68 -0
- nexaai/runtime_error.py +24 -0
- nexaai/tts.py +75 -0
- nexaai/tts_impl/__init__.py +0 -0
- nexaai/tts_impl/mlx_tts_impl.py +94 -0
- nexaai/tts_impl/pybind_tts_impl.py +43 -0
- nexaai/utils/decode.py +18 -0
- nexaai/utils/manifest_utils.py +531 -0
- nexaai/utils/model_manager.py +1562 -0
- nexaai/utils/model_types.py +49 -0
- nexaai/utils/progress_tracker.py +385 -0
- nexaai/utils/quantization_utils.py +245 -0
- nexaai/vlm.py +130 -0
- nexaai/vlm_impl/__init__.py +0 -0
- nexaai/vlm_impl/mlx_vlm_impl.py +259 -0
- nexaai/vlm_impl/pybind_vlm_impl.py +256 -0
- nexaai-1.0.21rc16.dist-info/METADATA +31 -0
- nexaai-1.0.21rc16.dist-info/RECORD +154 -0
- nexaai-1.0.21rc16.dist-info/WHEEL +5 -0
- nexaai-1.0.21rc16.dist-info/top_level.txt +1 -0
nexaai/binds/npu/ogg.dll
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
nexaai/common.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import TypedDict, Literal, Optional, List
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PluginID(str, Enum):
|
|
7
|
+
"""Enum for plugin identifiers."""
|
|
8
|
+
MLX = "mlx"
|
|
9
|
+
LLAMA_CPP = "llama_cpp"
|
|
10
|
+
NEXAML = "nexaml"
|
|
11
|
+
NPU = "npu"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ChatMessage(TypedDict):
|
|
15
|
+
role: Literal["user", "assistant", "system"]
|
|
16
|
+
content: str
|
|
17
|
+
|
|
18
|
+
class MultiModalMessageContent(TypedDict):
|
|
19
|
+
type: Literal["text", "image", "audio", "video"]
|
|
20
|
+
text: Optional[str]
|
|
21
|
+
url: Optional[str]
|
|
22
|
+
path: Optional[str]
|
|
23
|
+
|
|
24
|
+
class MultiModalMessage(TypedDict):
|
|
25
|
+
role: Literal["user", "assistant", "system"]
|
|
26
|
+
content: List[MultiModalMessageContent]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class SamplerConfig:
|
|
31
|
+
temperature: float = 0.8
|
|
32
|
+
top_p: float = 0.95
|
|
33
|
+
top_k: int = 40
|
|
34
|
+
repetition_penalty: float = 1.0
|
|
35
|
+
presence_penalty: float = 0.0
|
|
36
|
+
frequency_penalty: float = 0.0
|
|
37
|
+
seed: int = -1
|
|
38
|
+
grammar_path: str = None
|
|
39
|
+
grammar_string: str = None
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class GenerationConfig:
|
|
43
|
+
max_tokens: int = 1024
|
|
44
|
+
stop_words: list[str] = None
|
|
45
|
+
sampler_config: SamplerConfig = None
|
|
46
|
+
image_paths: list[str] = None
|
|
47
|
+
audio_paths: list[str] = None
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class ModelConfig:
|
|
51
|
+
n_ctx: int = 4096
|
|
52
|
+
n_threads: int = None
|
|
53
|
+
n_threads_batch: int = None
|
|
54
|
+
n_batch: int = 512
|
|
55
|
+
n_ubatch: int = 512
|
|
56
|
+
n_seq_max: int = 1
|
|
57
|
+
n_gpu_layers: int = 999
|
|
58
|
+
chat_template_path: str = None
|
|
59
|
+
chat_template_content: str = None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True) # Read-only
|
|
63
|
+
class ProfilingData:
|
|
64
|
+
"""Profiling data structure for LLM/VLM performance metrics."""
|
|
65
|
+
ttft: int = 0 # Time to first token (us)
|
|
66
|
+
prompt_time: int = 0 # Prompt processing time (us)
|
|
67
|
+
decode_time: int = 0 # Token generation time (us)
|
|
68
|
+
prompt_tokens: int = 0 # Number of prompt tokens
|
|
69
|
+
generated_tokens: int = 0 # Number of generated tokens
|
|
70
|
+
audio_duration: int = 0 # Audio duration (us)
|
|
71
|
+
prefill_speed: float = 0.0 # Prefill speed (tokens/sec)
|
|
72
|
+
decoding_speed: float = 0.0 # Decoding speed (tokens/sec)
|
|
73
|
+
real_time_factor: float = 0.0 # Real-Time Factor (RTF)
|
|
74
|
+
stop_reason: str = "" # Stop reason: "eos", "length", "user", "stop_sequence"
|
|
75
|
+
|
|
76
|
+
@classmethod
|
|
77
|
+
def from_dict(cls, data: dict) -> "ProfilingData":
|
|
78
|
+
"""Create ProfilingData from dictionary."""
|
|
79
|
+
return cls(
|
|
80
|
+
ttft=data.get("ttft", 0),
|
|
81
|
+
prompt_time=data.get("prompt_time", 0),
|
|
82
|
+
decode_time=data.get("decode_time", 0),
|
|
83
|
+
prompt_tokens=data.get("prompt_tokens", 0),
|
|
84
|
+
generated_tokens=data.get("generated_tokens", 0),
|
|
85
|
+
audio_duration=data.get("audio_duration", 0),
|
|
86
|
+
prefill_speed=data.get("prefill_speed", 0.0),
|
|
87
|
+
decoding_speed=data.get("decoding_speed", 0.0),
|
|
88
|
+
real_time_factor=data.get("real_time_factor", 0.0),
|
|
89
|
+
stop_reason=data.get("stop_reason", "")
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def to_dict(self) -> dict:
|
|
93
|
+
"""Convert to dictionary."""
|
|
94
|
+
return {
|
|
95
|
+
"ttft": self.ttft,
|
|
96
|
+
"prompt_time": self.prompt_time,
|
|
97
|
+
"decode_time": self.decode_time,
|
|
98
|
+
"prompt_tokens": self.prompt_tokens,
|
|
99
|
+
"generated_tokens": self.generated_tokens,
|
|
100
|
+
"audio_duration": self.audio_duration,
|
|
101
|
+
"prefill_speed": self.prefill_speed,
|
|
102
|
+
"decoding_speed": self.decoding_speed,
|
|
103
|
+
"real_time_factor": self.real_time_factor,
|
|
104
|
+
"stop_reason": self.stop_reason
|
|
105
|
+
}
|
nexaai/cv.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from typing import List, Optional, Union
|
|
2
|
+
from abc import abstractmethod
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from nexaai.base import BaseModel
|
|
6
|
+
from nexaai.common import PluginID
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class BoundingBox:
|
|
11
|
+
"""Generic bounding box structure."""
|
|
12
|
+
x: float # X coordinate (normalized or pixel, depends on model)
|
|
13
|
+
y: float # Y coordinate (normalized or pixel, depends on model)
|
|
14
|
+
width: float # Width
|
|
15
|
+
height: float # Height
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class CVResult:
|
|
20
|
+
"""Generic detection/classification result."""
|
|
21
|
+
image_paths: Optional[List[str]] = None # Output image paths
|
|
22
|
+
image_count: int = 0 # Number of output images
|
|
23
|
+
class_id: int = 0 # Class ID (example: ConvNext)
|
|
24
|
+
confidence: float = 0.0 # Confidence score [0.0-1.0]
|
|
25
|
+
bbox: Optional[BoundingBox] = None # Bounding box (example: YOLO)
|
|
26
|
+
text: Optional[str] = None # Text result (example: OCR)
|
|
27
|
+
embedding: Optional[List[float]] = None # Feature embedding (example: CLIP embedding)
|
|
28
|
+
embedding_dim: int = 0 # Embedding dimension
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class CVResults:
|
|
33
|
+
"""Generic CV inference result."""
|
|
34
|
+
results: List[CVResult] # Array of CV results
|
|
35
|
+
result_count: int # Number of CV results
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class CVCapabilities:
|
|
39
|
+
"""CV capabilities enum."""
|
|
40
|
+
OCR = 0 # OCR
|
|
41
|
+
CLASSIFICATION = 1 # Classification
|
|
42
|
+
SEGMENTATION = 2 # Segmentation
|
|
43
|
+
CUSTOM = 3 # Custom task
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class CVModelConfig:
|
|
48
|
+
"""CV model preprocessing configuration."""
|
|
49
|
+
capabilities: int # CVCapabilities
|
|
50
|
+
|
|
51
|
+
# MLX-OCR
|
|
52
|
+
det_model_path: Optional[str] = None # Detection model path
|
|
53
|
+
rec_model_path: Optional[str] = None # Recognition model path
|
|
54
|
+
|
|
55
|
+
# QNN
|
|
56
|
+
model_path: Optional[str] = None # Model path
|
|
57
|
+
system_library_path: Optional[str] = None # System library path
|
|
58
|
+
backend_library_path: Optional[str] = None # Backend library path
|
|
59
|
+
extension_library_path: Optional[str] = None # Extension library path
|
|
60
|
+
config_file_path: Optional[str] = None # Config file path
|
|
61
|
+
char_dict_path: Optional[str] = None # Character dictionary path
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class CVModel(BaseModel):
|
|
65
|
+
"""Abstract base class for generic computer vision models."""
|
|
66
|
+
|
|
67
|
+
def __init__(self):
|
|
68
|
+
"""Initialize base CV model class."""
|
|
69
|
+
pass
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def _load_from(cls,
|
|
73
|
+
_: str, # TODO: remove this argument, this is a hack to make api design happy
|
|
74
|
+
config: CVModelConfig,
|
|
75
|
+
plugin_id: Union[PluginID, str] = PluginID.LLAMA_CPP,
|
|
76
|
+
device_id: Optional[str] = None,
|
|
77
|
+
**kwargs
|
|
78
|
+
) -> 'CVModel':
|
|
79
|
+
"""Load CV model from configuration, routing to appropriate implementation."""
|
|
80
|
+
# Check plugin_id value for routing - handle both enum and string
|
|
81
|
+
plugin_value = plugin_id.value if isinstance(plugin_id, PluginID) else plugin_id
|
|
82
|
+
|
|
83
|
+
if plugin_value == "mlx":
|
|
84
|
+
from nexaai.cv_impl.mlx_cv_impl import MLXCVImpl
|
|
85
|
+
return MLXCVImpl._load_from(config, plugin_id, device_id)
|
|
86
|
+
else:
|
|
87
|
+
from nexaai.cv_impl.pybind_cv_impl import PyBindCVImpl
|
|
88
|
+
return PyBindCVImpl._load_from(config, plugin_id, device_id)
|
|
89
|
+
|
|
90
|
+
@abstractmethod
|
|
91
|
+
def infer(self, input_image_path: str) -> CVResults:
|
|
92
|
+
"""Perform inference on image."""
|
|
93
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Note: This code is generated by Cursor, not tested yet.
|
|
2
|
+
|
|
3
|
+
from typing import Optional, Union
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from nexaai.common import PluginID
|
|
7
|
+
from nexaai.cv import CVModel, CVModelConfig, CVResults
|
|
8
|
+
from nexaai.mlx_backend.cv.interface import CVModel as MLXCVInterface, create_cv_model
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MLXCVImpl(CVModel):
|
|
12
|
+
def __init__(self):
|
|
13
|
+
"""Initialize MLX CV implementation."""
|
|
14
|
+
super().__init__()
|
|
15
|
+
self._mlx_cv = None
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
def _load_from(cls,
|
|
19
|
+
config: CVModelConfig,
|
|
20
|
+
plugin_id: Union[PluginID, str] = PluginID.MLX,
|
|
21
|
+
device_id: Optional[str] = None
|
|
22
|
+
) -> 'MLXCVImpl':
|
|
23
|
+
"""Load CV model from configuration using MLX backend."""
|
|
24
|
+
try:
|
|
25
|
+
# Get MLX config class
|
|
26
|
+
from nexaai.mlx_backend.ml import CVModelConfig as MLXCVModelConfig
|
|
27
|
+
|
|
28
|
+
# Convert our config to MLX format
|
|
29
|
+
mlx_config = MLXCVModelConfig(
|
|
30
|
+
capabilities=config.capabilities,
|
|
31
|
+
det_model_path=config.det_model_path,
|
|
32
|
+
rec_model_path=config.rec_model_path,
|
|
33
|
+
model_path=config.model_path,
|
|
34
|
+
system_library_path=config.system_library_path,
|
|
35
|
+
backend_library_path=config.backend_library_path,
|
|
36
|
+
extension_library_path=config.extension_library_path,
|
|
37
|
+
config_file_path=config.config_file_path,
|
|
38
|
+
char_dict_path=config.char_dict_path
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
# Create instance and load MLX CV model
|
|
42
|
+
instance = cls()
|
|
43
|
+
instance._mlx_cv = create_cv_model(mlx_config, device_id)
|
|
44
|
+
|
|
45
|
+
return instance
|
|
46
|
+
except Exception as e:
|
|
47
|
+
raise RuntimeError(f"Failed to load MLX CV: {str(e)}")
|
|
48
|
+
|
|
49
|
+
def eject(self):
|
|
50
|
+
"""Destroy the model and free resources."""
|
|
51
|
+
if self._mlx_cv:
|
|
52
|
+
self._mlx_cv.destroy()
|
|
53
|
+
self._mlx_cv = None
|
|
54
|
+
|
|
55
|
+
def infer(self, input_image_path: str) -> CVResults:
|
|
56
|
+
"""Perform inference on image."""
|
|
57
|
+
if not self._mlx_cv:
|
|
58
|
+
raise RuntimeError("MLX CV not loaded")
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
# Use MLX CV inference
|
|
62
|
+
result = self._mlx_cv.infer(input_image_path)
|
|
63
|
+
|
|
64
|
+
# Convert MLX result to our format
|
|
65
|
+
from nexaai.cv import CVResult
|
|
66
|
+
|
|
67
|
+
our_results = []
|
|
68
|
+
for mlx_result in result.results:
|
|
69
|
+
our_result = CVResult(
|
|
70
|
+
image_paths=mlx_result.image_paths,
|
|
71
|
+
image_count=mlx_result.image_count,
|
|
72
|
+
class_id=mlx_result.class_id,
|
|
73
|
+
confidence=mlx_result.confidence,
|
|
74
|
+
bbox=mlx_result.bbox,
|
|
75
|
+
text=mlx_result.text,
|
|
76
|
+
embedding=mlx_result.embedding,
|
|
77
|
+
embedding_dim=mlx_result.embedding_dim
|
|
78
|
+
)
|
|
79
|
+
our_results.append(our_result)
|
|
80
|
+
|
|
81
|
+
return CVResults(
|
|
82
|
+
results=our_results,
|
|
83
|
+
result_count=result.result_count
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
except Exception as e:
|
|
87
|
+
raise RuntimeError(f"Failed to perform CV inference: {str(e)}")
|
|
88
|
+
|
|
89
|
+
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from typing import Optional, Union
|
|
2
|
+
|
|
3
|
+
from nexaai.common import PluginID
|
|
4
|
+
from nexaai.cv import CVModel, CVModelConfig, CVResults
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class PyBindCVImpl(CVModel):
|
|
8
|
+
def __init__(self):
|
|
9
|
+
"""Initialize PyBind CV implementation."""
|
|
10
|
+
super().__init__()
|
|
11
|
+
# TODO: Add PyBind-specific initialization
|
|
12
|
+
|
|
13
|
+
@classmethod
|
|
14
|
+
def _load_from(cls,
|
|
15
|
+
config: CVModelConfig,
|
|
16
|
+
plugin_id: Union[PluginID, str] = PluginID.LLAMA_CPP,
|
|
17
|
+
device_id: Optional[str] = None
|
|
18
|
+
) -> 'PyBindCVImpl':
|
|
19
|
+
"""Load CV model from configuration using PyBind backend."""
|
|
20
|
+
# TODO: Implement PyBind CV loading
|
|
21
|
+
instance = cls()
|
|
22
|
+
return instance
|
|
23
|
+
|
|
24
|
+
def eject(self):
|
|
25
|
+
"""Destroy the model and free resources."""
|
|
26
|
+
# TODO: Implement PyBind CV cleanup
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
def infer(self, input_image_path: str) -> CVResults:
|
|
30
|
+
"""Perform inference on image."""
|
|
31
|
+
# TODO: Implement PyBind CV inference
|
|
32
|
+
raise NotImplementedError("PyBind CV inference not yet implemented")
|
nexaai/embedder.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from abc import abstractmethod
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
from nexaai.base import BaseModel
|
|
7
|
+
from nexaai.common import PluginID
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class EmbeddingConfig:
|
|
12
|
+
batch_size: int = 32
|
|
13
|
+
normalize: bool = True
|
|
14
|
+
normalize_method: str = "l2"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Embedder(BaseModel):
|
|
18
|
+
def __init__(self):
|
|
19
|
+
"""
|
|
20
|
+
Internal initializer
|
|
21
|
+
"""
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
@classmethod
|
|
25
|
+
def _load_from(cls, model_path: str, model_name: str = None, tokenizer_file: str = "tokenizer.json", plugin_id: Union[PluginID, str] = PluginID.LLAMA_CPP, **kwargs):
|
|
26
|
+
"""
|
|
27
|
+
Load an embedder from model files, routing to appropriate implementation.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
model_path: Path to the model file
|
|
31
|
+
model_name: Name of the model
|
|
32
|
+
tokenizer_file: Path to the tokenizer file (default: "tokenizer.json")
|
|
33
|
+
plugin_id: Plugin ID to use for the model (default: PluginID.LLAMA_CPP)
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
Embedder instance
|
|
37
|
+
"""
|
|
38
|
+
# Check plugin_id value for routing - handle both enum and string
|
|
39
|
+
plugin_value = plugin_id.value if isinstance(plugin_id, PluginID) else plugin_id
|
|
40
|
+
|
|
41
|
+
if plugin_value == "mlx":
|
|
42
|
+
from nexaai.embedder_impl.mlx_embedder_impl import MLXEmbedderImpl
|
|
43
|
+
return MLXEmbedderImpl._load_from(model_path, model_name, tokenizer_file, plugin_id)
|
|
44
|
+
else:
|
|
45
|
+
from nexaai.embedder_impl.pybind_embedder_impl import PyBindEmbedderImpl
|
|
46
|
+
return PyBindEmbedderImpl._load_from(model_path, model_name, tokenizer_file, plugin_id)
|
|
47
|
+
|
|
48
|
+
@abstractmethod
|
|
49
|
+
def generate(self, texts: Union[List[str], str] = None, config: EmbeddingConfig = EmbeddingConfig(), input_ids: Union[List[int], List[List[int]]] = None) -> np.ndarray:
|
|
50
|
+
"""
|
|
51
|
+
Generate embeddings for the given texts or input_ids.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
texts: List of strings or single string to embed
|
|
55
|
+
input_ids: Pre-tokenized input as:
|
|
56
|
+
- Single sequence: list of integers [1, 2, 3, 4]
|
|
57
|
+
- Multiple sequences: list of lists [[1, 2, 3], [4, 5, 6]]
|
|
58
|
+
config: Configuration for embedding generation
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
numpy array of embeddings with shape (num_sequences, embedding_dim)
|
|
62
|
+
"""
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
@abstractmethod
|
|
66
|
+
def get_embedding_dim(self) -> int:
|
|
67
|
+
"""
|
|
68
|
+
Get the embedding dimension of the model
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
The embedding dimension in int
|
|
72
|
+
"""
|
|
73
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from nexaai.common import PluginID
|
|
5
|
+
from nexaai.embedder import Embedder, EmbeddingConfig
|
|
6
|
+
from nexaai.mlx_backend.embedding.interface import create_embedder
|
|
7
|
+
from nexaai.mlx_backend.ml import ModelConfig as MLXModelConfig, SamplerConfig as MLXSamplerConfig, GenerationConfig as MLXGenerationConfig, EmbeddingConfig
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MLXEmbedderImpl(Embedder):
|
|
11
|
+
def __init__(self):
|
|
12
|
+
"""Initialize MLX Embedder implementation."""
|
|
13
|
+
super().__init__()
|
|
14
|
+
self._mlx_embedder = None
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def _load_from(cls, model_path: str, model_name: str = None, tokenizer_file: str = "tokenizer.json", plugin_id: Union[PluginID, str] = PluginID.MLX):
|
|
18
|
+
"""
|
|
19
|
+
Load an embedder from model files using MLX backend.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
model_path: Path to the model file
|
|
23
|
+
model_name: Name of the model
|
|
24
|
+
tokenizer_file: Path to the tokenizer file (default: "tokenizer.json")
|
|
25
|
+
plugin_id: Plugin ID to use for the model (default: PluginID.MLX)
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
MLXEmbedderImpl instance
|
|
29
|
+
"""
|
|
30
|
+
try:
|
|
31
|
+
# Create instance
|
|
32
|
+
instance = cls()
|
|
33
|
+
|
|
34
|
+
# Use the factory function to create the appropriate embedder based on model type
|
|
35
|
+
# This will automatically detect if it's JinaV2 or generic model and route correctly
|
|
36
|
+
instance._mlx_embedder = create_embedder(
|
|
37
|
+
model_path=model_path,
|
|
38
|
+
# model_name=model_name, # FIXME: For MLX Embedder, model_name is not used
|
|
39
|
+
tokenizer_path=tokenizer_file
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Load the model
|
|
43
|
+
success = instance._mlx_embedder.load_model(model_path)
|
|
44
|
+
if not success:
|
|
45
|
+
raise RuntimeError("Failed to load MLX embedder model")
|
|
46
|
+
|
|
47
|
+
return instance
|
|
48
|
+
except Exception as e:
|
|
49
|
+
raise RuntimeError(f"Failed to load MLX Embedder: {str(e)}")
|
|
50
|
+
|
|
51
|
+
def eject(self):
|
|
52
|
+
"""
|
|
53
|
+
Clean up resources and destroy the embedder
|
|
54
|
+
"""
|
|
55
|
+
if self._mlx_embedder:
|
|
56
|
+
self._mlx_embedder.destroy()
|
|
57
|
+
self._mlx_embedder = None
|
|
58
|
+
|
|
59
|
+
def generate(self, texts: Union[List[str], str] = None, config: EmbeddingConfig = EmbeddingConfig(), input_ids: Union[List[int], List[List[int]]] = None) -> np.ndarray:
|
|
60
|
+
"""
|
|
61
|
+
Generate embeddings for the given texts or input_ids.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
texts: List of strings or single string to embed
|
|
65
|
+
input_ids: Pre-tokenized input as:
|
|
66
|
+
- Single sequence: list of integers [1, 2, 3, 4]
|
|
67
|
+
- Multiple sequences: list of lists [[1, 2, 3], [4, 5, 6]]
|
|
68
|
+
config: Configuration for embedding generation
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
numpy array of embeddings with shape (num_sequences, embedding_dim)
|
|
72
|
+
"""
|
|
73
|
+
if not self._mlx_embedder:
|
|
74
|
+
raise RuntimeError("MLX Embedder not loaded")
|
|
75
|
+
|
|
76
|
+
if texts is None and input_ids is None:
|
|
77
|
+
raise ValueError("Either texts or input_ids must be provided")
|
|
78
|
+
|
|
79
|
+
# MLX embedder currently only supports text input, not pre-tokenized input_ids
|
|
80
|
+
if input_ids is not None:
|
|
81
|
+
raise NotImplementedError("MLX embedder does not support input_ids, only text input")
|
|
82
|
+
|
|
83
|
+
try:
|
|
84
|
+
# Convert single string to list if needed
|
|
85
|
+
if isinstance(texts, str):
|
|
86
|
+
texts = [texts]
|
|
87
|
+
|
|
88
|
+
# MLX config classes are already imported
|
|
89
|
+
|
|
90
|
+
# Convert our config to MLX config
|
|
91
|
+
mlx_config = EmbeddingConfig()
|
|
92
|
+
mlx_config.batch_size = config.batch_size
|
|
93
|
+
mlx_config.normalize = config.normalize
|
|
94
|
+
mlx_config.normalize_method = config.normalize_method
|
|
95
|
+
|
|
96
|
+
# Generate embeddings using MLX
|
|
97
|
+
embeddings = self._mlx_embedder.embed(texts, mlx_config)
|
|
98
|
+
|
|
99
|
+
# Convert to numpy array
|
|
100
|
+
return np.array(embeddings, dtype=np.float32)
|
|
101
|
+
|
|
102
|
+
except Exception as e:
|
|
103
|
+
raise RuntimeError(f"Failed to generate embeddings: {str(e)}")
|
|
104
|
+
|
|
105
|
+
def get_embedding_dim(self) -> int:
|
|
106
|
+
"""
|
|
107
|
+
Get the embedding dimension of the model
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
The embedding dimension in int
|
|
111
|
+
"""
|
|
112
|
+
if not self._mlx_embedder:
|
|
113
|
+
raise RuntimeError("MLX Embedder not loaded")
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
return self._mlx_embedder.embedding_dim()
|
|
117
|
+
except Exception as e:
|
|
118
|
+
raise RuntimeError(f"Failed to get embedding dimension: {str(e)}")
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from nexaai.common import PluginID
|
|
5
|
+
from nexaai.embedder import Embedder, EmbeddingConfig
|
|
6
|
+
from nexaai.binds import embedder_bind
|
|
7
|
+
from nexaai.runtime import _ensure_runtime
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PyBindEmbedderImpl(Embedder):
|
|
11
|
+
def __init__(self, _handle_ptr):
|
|
12
|
+
"""
|
|
13
|
+
Internal initializer
|
|
14
|
+
"""
|
|
15
|
+
super().__init__()
|
|
16
|
+
self._handle = _handle_ptr
|
|
17
|
+
|
|
18
|
+
@classmethod
|
|
19
|
+
def _load_from(cls, model_path: str, model_name: str = None, tokenizer_file: str = "tokenizer.json", plugin_id: Union[PluginID, str] = PluginID.LLAMA_CPP):
|
|
20
|
+
"""
|
|
21
|
+
Load an embedder from model files
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
model_path: Path to the model file
|
|
25
|
+
model_name: Name of the model
|
|
26
|
+
tokenizer_file: Path to the tokenizer file (default: "tokenizer.json")
|
|
27
|
+
plugin_id: Plugin ID to use for the model (default: PluginID.LLAMA_CPP)
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
PyBindEmbedderImpl instance
|
|
31
|
+
"""
|
|
32
|
+
_ensure_runtime()
|
|
33
|
+
# Convert enum to string for C++ binding
|
|
34
|
+
plugin_id_str = plugin_id.value if isinstance(plugin_id, PluginID) else plugin_id
|
|
35
|
+
# New parameter order: model_path, plugin_id, tokenizer_path (optional)
|
|
36
|
+
handle = embedder_bind.ml_embedder_create(model_path, model_name, plugin_id_str, tokenizer_file)
|
|
37
|
+
return cls(handle)
|
|
38
|
+
|
|
39
|
+
def eject(self):
|
|
40
|
+
"""
|
|
41
|
+
Clean up resources and destroy the embedder
|
|
42
|
+
"""
|
|
43
|
+
# Destructor of the handle will unload the model correctly
|
|
44
|
+
del self._handle
|
|
45
|
+
self._handle = None
|
|
46
|
+
|
|
47
|
+
def generate(self, texts: Union[List[str], str] = None, config: EmbeddingConfig = EmbeddingConfig(), input_ids: Union[List[int], List[List[int]]] = None) -> np.ndarray:
|
|
48
|
+
"""
|
|
49
|
+
Generate embeddings for the given texts or input_ids.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
texts: List of strings or single string to embed
|
|
53
|
+
input_ids: Pre-tokenized input as:
|
|
54
|
+
- Single sequence: list of integers [1, 2, 3, 4]
|
|
55
|
+
- Multiple sequences: list of lists [[1, 2, 3], [4, 5, 6]]
|
|
56
|
+
config: Configuration for embedding generation
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
numpy array of embeddings with shape (num_sequences, embedding_dim)
|
|
60
|
+
"""
|
|
61
|
+
if texts is None and input_ids is None:
|
|
62
|
+
raise ValueError("Either texts or input_ids must be provided")
|
|
63
|
+
|
|
64
|
+
# Create bind config
|
|
65
|
+
bind_config = embedder_bind.EmbeddingConfig()
|
|
66
|
+
bind_config.batch_size = config.batch_size
|
|
67
|
+
bind_config.normalize = config.normalize
|
|
68
|
+
bind_config.normalize_method = config.normalize_method
|
|
69
|
+
|
|
70
|
+
# Convert single string to list if needed
|
|
71
|
+
if isinstance(texts, str):
|
|
72
|
+
texts = [texts]
|
|
73
|
+
|
|
74
|
+
# Convert input_ids to 2D format if needed
|
|
75
|
+
processed_input_ids = None
|
|
76
|
+
if input_ids is not None:
|
|
77
|
+
if len(input_ids) > 0 and isinstance(input_ids[0], int):
|
|
78
|
+
# Single sequence: convert [1, 2, 3] to [[1, 2, 3]]
|
|
79
|
+
processed_input_ids = [input_ids]
|
|
80
|
+
else:
|
|
81
|
+
# Multiple sequences: already in correct format [[1, 2], [3, 4]]
|
|
82
|
+
processed_input_ids = input_ids
|
|
83
|
+
|
|
84
|
+
# Pass both parameters, let the ABI handle validation
|
|
85
|
+
embeddings = embedder_bind.ml_embedder_embed(self._handle, bind_config, texts, processed_input_ids)
|
|
86
|
+
|
|
87
|
+
return embeddings
|
|
88
|
+
|
|
89
|
+
def get_embedding_dim(self) -> int:
|
|
90
|
+
"""
|
|
91
|
+
Get the embedding dimension of the model
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
The embedding dimension in int
|
|
95
|
+
"""
|
|
96
|
+
return embedder_bind.ml_embedder_embedding_dim(self._handle)
|