-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add torch model and GPU support. (#195)
* Setup TorchVL * fix build, setup cpu, gpu, cloud variants * remove torch files * update .gitignore * update tests * fix torchVL class and add streaming tests * format * remove space in torch max tokens * format * Add black install * fix cloud test * isort imports * fix test build ci * Remove build py, update readme * update readme * Add back torch deps * update readme * update readme * update readme
- Loading branch information
1 parent
ad09284
commit 40a90c0
Showing
11 changed files
with
271 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ data | |
/pyproject.toml | ||
poetry.lock | ||
dist | ||
clients/python/moondream/torch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from typing import Literal, Optional, Union | ||
|
||
import torch | ||
from PIL import Image | ||
|
||
from .torch.moondream import MoondreamConfig, MoondreamModel | ||
from .torch.weights import load_weights_into_model | ||
from .types import ( | ||
VLM, | ||
Base64EncodedImage, | ||
CaptionOutput, | ||
DetectOutput, | ||
EncodedImage, | ||
PointOutput, | ||
QueryOutput, | ||
SamplingSettings, | ||
) | ||
from .version import __version__ | ||
|
||
|
||
class TorchVL(VLM): | ||
def __init__( | ||
self, | ||
*, | ||
model: str, | ||
): | ||
config = MoondreamConfig() | ||
self.model = MoondreamModel(config) | ||
load_weights_into_model(model, self.model) | ||
self.model.eval() | ||
# Move model to the appropriate device | ||
if torch.cuda.is_available(): | ||
self.device = "cuda" | ||
elif torch.backends.mps.is_available(): | ||
self.device = "mps" | ||
else: | ||
self.device = "cpu" | ||
self.model.to(self.device) | ||
|
||
def encode_image( | ||
self, image: Union[Image.Image, EncodedImage] | ||
) -> Base64EncodedImage: | ||
if isinstance(image, EncodedImage): | ||
assert type(image) == Base64EncodedImage | ||
return image | ||
|
||
if not self.model: | ||
raise ValueError("No local model loaded") | ||
|
||
return self.model.encode_image(image) | ||
|
||
def caption( | ||
self, | ||
image: Union[Image.Image, EncodedImage], | ||
length: Literal["normal", "short"] = "normal", | ||
stream: bool = False, | ||
settings: Optional[SamplingSettings] = None, | ||
) -> CaptionOutput: | ||
if not self.model: | ||
raise ValueError("No local model loaded") | ||
|
||
encoded_image = ( | ||
self.model.encode_image(image) if isinstance(image, Image.Image) else image | ||
) | ||
return self.model.caption( | ||
encoded_image, length=length, stream=stream, settings=settings | ||
) | ||
|
||
def query( | ||
self, | ||
image: Union[Image.Image, EncodedImage], | ||
question: str, | ||
stream: bool = False, | ||
settings: Optional[SamplingSettings] = None, | ||
) -> QueryOutput: | ||
if not self.model: | ||
raise ValueError("No local model loaded") | ||
|
||
encoded_image = ( | ||
self.model.encode_image(image) if isinstance(image, Image.Image) else image | ||
) | ||
return self.model.query( | ||
encoded_image, question, stream=stream, settings=settings | ||
) | ||
|
||
def detect( | ||
self, | ||
image: Union[Image.Image, EncodedImage], | ||
object: str, | ||
) -> DetectOutput: | ||
if not self.model: | ||
raise ValueError("No local model loaded") | ||
|
||
encoded_image = ( | ||
self.model.encode_image(image) if isinstance(image, Image.Image) else image | ||
) | ||
return self.model.detect(encoded_image, object) | ||
|
||
def point( | ||
self, | ||
image: Union[Image.Image, EncodedImage], | ||
object: str, | ||
) -> PointOutput: | ||
if not self.model: | ||
raise ValueError("No local model loaded") | ||
|
||
encoded_image = ( | ||
self.model.encode_image(image) if isinstance(image, Image.Image) else image | ||
) | ||
return self.model.point(encoded_image, object) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,47 @@ | ||
[build-system] | ||
requires = [ "poetry-core",] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry] | ||
name = "moondream" | ||
version = "0.0.5" | ||
version = "0.0.2" | ||
description = "Python client library for moondream" | ||
authors = ["vik <vik@moondream.ai>"] | ||
authors = [ "M87 Labs <contact@moondream.ai>",] | ||
readme = "README.md" | ||
[[tool.poetry.packages]] | ||
include = "moondream" | ||
from = "." | ||
|
||
[tool.pyright] | ||
venvPath = "." | ||
venv = ".venv" | ||
reportMissingParameterType = false | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.10" | ||
pillow = "^10.4.0" | ||
onnxruntime = "^1.19.2" | ||
numpy = "^2.1.2" | ||
onnx = "^1.17.0" | ||
tokenizers = "^0.20.1" | ||
onnxruntime = { version = ">=1.19.2", optional = true } | ||
tokenizers = { version = ">=0.20.1", optional = true } | ||
torch = { version = ">=2.5.0", optional = true } | ||
safetensors = { version = ">=0.4.2", optional = true } | ||
einops = { version = ">=0.7.0", optional = true } | ||
pyvips-binary = { version = ">=8.16.0", optional = true } | ||
pyvips = { version = ">=2.2.1", optional = true } | ||
|
||
[tool.poetry.extras] | ||
cpu = [ | ||
"onnxruntime", | ||
"tokenizers" | ||
] | ||
gpu = [ | ||
"torch", | ||
"safetensors", | ||
"einops", | ||
"pyvips-binary", | ||
"pyvips", | ||
"tokenizers" | ||
] | ||
|
||
[tool.poetry.scripts] | ||
moondream = "moondream.cli:main" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pytest = "^8.3.4" | ||
pytest-asyncio = "^0.25.1" | ||
requests = "^2.32.3" | ||
black = "^24.10.0" | ||
|
||
[tool.pyright] | ||
venvPath = "." | ||
venv = ".venv" | ||
reportMissingParameterType = false | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.