Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add n_gpu_layers to llama.cpp #4679

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions langchain/embeddings/llamacpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class LlamaCppEmbeddings(BaseModel, Embeddings):
"""Number of tokens to process in parallel.
Should be a number between 1 and n_ctx."""

n_gpu_layers: Optional[int] = None
"""Number of layers to store in VRAM."""

class Config:
"""Configuration for this pydantic object."""

Expand All @@ -71,6 +74,7 @@ def validate_environment(cls, values: Dict) -> Dict:
use_mlock = values["use_mlock"]
n_threads = values["n_threads"]
n_batch = values["n_batch"]
n_gpu_layers = values["n_gpu_layers"]

try:
from llama_cpp import Llama
Expand All @@ -86,6 +90,7 @@ def validate_environment(cls, values: Dict) -> Dict:
use_mlock=use_mlock,
n_threads=n_threads,
n_batch=n_batch,
n_gpu_layers=n_gpu_layers,
embedding=True,
)
except ImportError:
Expand Down
5 changes: 5 additions & 0 deletions langchain/llms/llamacpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class LlamaCpp(LLM):
streaming: bool = True
"""Whether to stream the results, token by token."""

n_gpu_layers: Optional[int] = None
"""Number of layers to store in VRAM."""

@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that llama-cpp-python library is installed."""
Expand All @@ -117,6 +120,7 @@ def validate_environment(cls, values: Dict) -> Dict:
n_batch = values["n_batch"]
use_mmap = values["use_mmap"]
last_n_tokens_size = values["last_n_tokens_size"]
n_gpu_layers = values["n_gpu_layers"]

try:
from llama_cpp import Llama
Expand All @@ -136,6 +140,7 @@ def validate_environment(cls, values: Dict) -> Dict:
n_batch=n_batch,
use_mmap=use_mmap,
last_n_tokens_size=last_n_tokens_size,
n_gpu_layers=n_gpu_layers,
)
except ImportError:
raise ModuleNotFoundError(
Expand Down