Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: caikit/caikit-nlp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.5.1
Choose a base ref
...
head repository: caikit/caikit-nlp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.5.2
Choose a head ref
  • 6 commits
  • 6 files changed
  • 3 contributors

Commits on Aug 5, 2024

  1. 👷 Reclaim space on image build

    Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
    evaline-ju committed Aug 5, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    caarlos0 Carlos Alexandro Becker
    Copy the full SHA
    5510f10 View commit details
  2. Merge pull request #375 from evaline-ju/reclaim-space

    👷 Reclaim space on image build
    gkumbhat authored Aug 5, 2024

    Partially verified

    This commit is signed with the committer’s verified signature.
    bashbunni’s contribution has been verified via GPG key.
    We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
    Copy the full SHA
    b28c2b1 View commit details

Commits on Aug 10, 2024

  1. Update sentence-transformers and allow setting trust_remote_code

    sentence-transformers APIs changed. This update allows 3.x (so far).
    The update is required to support some models.
    Most of the update is adding new params to be in sync with the
    super SentenceTransformer class. Most of these params are not used.
    
    Support is added to allow passing the trust_remote_code paramater.
    This can be done in the runtime config or with EMBEDDING_TRUST_REMOTE_CODE=true
    (or =1) environment variable.
    
    Signed-off-by: Mark Sturdevant <mark.sturdevant@ibm.com>
    markstur committed Aug 10, 2024
    Copy the full SHA
    41ac8ec View commit details

Commits on Aug 12, 2024

  1. tox.ini passenv PYTORCH_ENABLE_MPS_FALLBACK to run tests on Mac M3

    Setting PYTORCH_ENABLE_MPS_FALLBACK=1 allows the tests to run on Mac MPS (M3).
    
    Signed-off-by: Mark Sturdevant <mark.sturdevant@ibm.com>
    markstur committed Aug 12, 2024
    Copy the full SHA
    3343d88 View commit details
  2. Bump transformers and stop at <= 4.44.0

    * Use >= 4.38.0 because that is the sentence-transformers min anyway.
    * Add <= 4.44.0 because 4.44.0 breaks our tests with errors like this:
      > `RuntimeError: The expanded size of the tensor (22) must match the existing size (30) at non-singleton dimension 2.  Target sizes: [4, 22, 22].  Tensor sizes: [4, 1, 30]`
    
    Signed-off-by: Mark Sturdevant <mark.sturdevant@ibm.com>
    markstur committed Aug 12, 2024
    Copy the full SHA
    bcc1233 View commit details
  3. Merge pull request #379 from markstur/sentence-transformers-3

    Update sentence-transformers and allow setting trust_remote_code
    evaline-ju authored Aug 12, 2024
    Copy the full SHA
    989cd45 View commit details
Showing with 50 additions and 4 deletions.
  1. +3 −0 .github/workflows/build-image.yml
  2. +2 −0 caikit_nlp/config/config.yml
  3. +40 −2 caikit_nlp/modules/text_embedding/embedding.py
  4. +2 −2 pyproject.toml
  5. +2 −0 runtime_config.yaml
  6. +1 −0 tox.ini
3 changes: 3 additions & 0 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
@@ -17,6 +17,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Reclaim space
run: |
sudo rm -rf /opt/hostedtoolcache
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build image
2 changes: 2 additions & 0 deletions caikit_nlp/config/config.yml
Original file line number Diff line number Diff line change
@@ -38,6 +38,8 @@ training_data_limit:

# Config used only in EmbeddingModule. Set here or use env vars like EMBEDDING_RETRIES=32
embedding:
# Allow models with remote code.
trust_remote_code: false
# Number of times to retry on error. Most deployments should use 0 retries.
retries: 0
# Batch size for encode() if <= 0 or invalid, the sentence-transformers default is used
42 changes: 40 additions & 2 deletions caikit_nlp/modules/text_embedding/embedding.py
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
Dict,
Iterable,
List,
Literal,
NamedTuple,
Optional,
TypeVar,
@@ -82,6 +83,8 @@
sentence_transformers = importlib.import_module("sentence_transformers")
# Third Party
from sentence_transformers import SentenceTransformer
from sentence_transformers.model_card import SentenceTransformerModelCardData
from sentence_transformers.similarity_functions import SimilarityFunction
from sentence_transformers.util import batch_to_device, cos_sim, dot_score
from sentence_transformers.util import (
normalize_embeddings as normalize, # avoid parameter shadowing
@@ -107,6 +110,7 @@ def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
val=embedding_cfg.get("implicit_truncation_errors", True)
)
DEVICE = embedding_cfg.get("device", "")
TRUST_REMOTE_CODE = embedding_cfg.get("trust_remote_code")

RT = TypeVar("RT") # return type

@@ -183,7 +187,9 @@ def load(cls, model_path: str, *args, **kwargs) -> "EmbeddingModule":
ipex = cls._get_ipex(IPEX)
device = cls._select_device(ipex, DEVICE)
model = SentenceTransformerWithTruncate(
model_name_or_path=artifacts_path, device=device
model_name_or_path=artifacts_path,
device=device,
trust_remote_code=TRUST_REMOTE_CODE,
)
model.eval() # required for IPEX at least
if device is not None:
@@ -719,7 +725,12 @@ def bootstrap(cls, model_name_or_path: str) -> "EmbeddingModule":
model_name_or_path: str
Model name (Hugging Face hub) or path to model to load.
"""
return cls(model=SentenceTransformer(model_name_or_path=model_name_or_path))
return cls(
model=SentenceTransformer(
model_name_or_path=model_name_or_path,
trust_remote_code=TRUST_REMOTE_CODE,
)
)

def save(self, model_path: str, *args, **kwargs):
"""Save model using config in model_path
@@ -875,21 +886,39 @@ def __init__(
model_name_or_path: Optional[str] = None,
modules: Optional[Iterable[nn.Module]] = None,
device: Optional[str] = None,
prompts: Optional[Dict[str, str]] = None,
default_prompt_name: Optional[str] = None,
similarity_fn_name: Optional[Union[str, SimilarityFunction]] = None,
cache_folder: Optional[str] = None,
trust_remote_code: bool = False,
revision: Optional[str] = None,
local_files_only: bool = False,
token: Optional[Union[bool, str]] = None,
use_auth_token: Optional[Union[bool, str]] = None,
truncate_dim: Optional[int] = None,
model_kwargs: Optional[Dict[str, Any]] = None,
tokenizer_kwargs: Optional[Dict[str, Any]] = None,
config_kwargs: Optional[Dict[str, Any]] = None,
model_card_data: Optional[SentenceTransformerModelCardData] = None,
):
super().__init__(
model_name_or_path,
modules,
device,
prompts,
default_prompt_name,
similarity_fn_name,
cache_folder,
trust_remote_code,
revision,
local_files_only,
token,
use_auth_token,
truncate_dim,
model_kwargs,
tokenizer_kwargs,
config_kwargs,
model_card_data,
)
self.tokenizers = {}

@@ -1014,9 +1043,12 @@ def _get_tokenized(self, texts):
def encode(
self,
sentences: Union[str, List[str]],
prompt_name: Optional[str] = None,
prompt: Optional[str] = None,
batch_size: int = 32,
show_progress_bar: bool = None,
output_value: str = "sentence_embedding",
precision: Literal["float32", "int8", "uint8", "binary", "ubinary"] = "float32",
convert_to_numpy: bool = True,
convert_to_tensor: bool = False,
device: str = None,
@@ -1029,9 +1061,12 @@ def encode(
Computes sentence embeddings
:param sentences: the sentences to embed
:param prompt_name: Ignored here. Added for compatibility with super API.
:param prompt: Ignored here. Added for compatibility with super API.
:param batch_size: the batch size used for the computation
:param show_progress_bar: Ignored here. Added for compatibility with super API.
:param output_value: Ignored here. Added for compatibility with super API.
:param precision: Ignored here. Added for compatibility with super API.
:param convert_to_numpy: If true, the output is a list of numpy vectors. Else, it is a list
of pytorch tensors.
:param convert_to_tensor: If true, you get one large tensor as return. Overwrites any
@@ -1057,8 +1092,11 @@ def encode(

# These args are for API compatability, but are currently ignored in our version of encode()
_ = (
prompt_name,
prompt,
show_progress_bar,
output_value,
precision,
normalize_embeddings,
)

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -27,11 +27,11 @@ dependencies = [
"pandas>=1.5.0",
"scikit-learn>=1.1",
"scipy>=1.8.1",
"sentence-transformers>=2.3.1,<2.4.0",
"sentence-transformers>=3.0.0,<3.1.0",
"tokenizers>=0.13.3",
"torch>=2.3.1,<2.4.0",
"tqdm>=4.65.0",
"transformers>=4.32.0",
"transformers>=4.38.0,<4.44.0",
"peft==0.6.0",
]

2 changes: 2 additions & 0 deletions runtime_config.yaml
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@ model_management:

# Config used only in EmbeddingModule. Set here or use env vars like EMBEDDING_RETRIES=32
embedding:
# Allow models with remote code.
trust_remote_code: false
# Number of times to retry on error. Most deployments should use 0 retries.
retries: 0
# Batch size for encode() if <= 0 or invalid, the sentence-transformers default is used
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ passenv =
LOG_FORMATTER
LOG_THREAD_ID
LOG_CHANNEL_WIDTH
PYTORCH_ENABLE_MPS_FALLBACK
commands = pytest --durations=42 --cov=caikit_nlp --cov-report=term --cov-report=html {posargs:tests}

; Unclear: We probably want to test wheel packaging