input
stringlengths
33
5k
output
stringlengths
32
5k
_base_ = './fovea_r50_fpn_4xb4-1x_coco.py' model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( with_deform=True, norm_cfg=dict(type='GN', num_groups=32, requires_grad=True))) train_pipeline = [ dict(type='LoadImageFromFile', backend_args={{_base_.backend_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomChoiceResize', scales=[(1333, 640), (1333, 800)], keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline)) # learning policy max_epochs = 24 param_scheduler = [ dict( type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[16, 22], gamma=0.1) ] train_cfg = dict(max_epochs=max_epochs)
_base_ = './fovea_r50_fpn_4xb4-1x_coco.py' model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( with_deform=True, norm_cfg=dict(type='GN', num_groups=32, requires_grad=True))) train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomChoiceResize', scales=[(1333, 640), (1333, 800)], keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline)) # learning policy max_epochs = 24 param_scheduler = [ dict( type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[16, 22], gamma=0.1) ] train_cfg = dict(max_epochs=max_epochs)
""" NumPy Array API compatibility library This is a small wrapper around NumPy, CuPy, JAX, sparse and others that are compatible with the Array API standard https://data-apis.org/array-api/latest/. See also NEP 47 https://numpy.org/neps/nep-0047-array-api-standard.html. Unlike array_api_strict, this is not a strict minimal implementation of the Array API, but rather just an extension of the main NumPy namespace with changes needed to be compliant with the Array API. See https://numpy.org/doc/stable/reference/array_api.html for a full list of changes. In particular, unlike array_api_strict, this package does not use a separate Array object, but rather just uses numpy.ndarray directly. Library authors using the Array API may wish to test against array_api_strict to ensure they are not using functionality outside of the standard, but prefer this implementation for the default when working with NumPy arrays. """ __version__ = '1.11.2' from .common import * # noqa: F401, F403
""" NumPy Array API compatibility library This is a small wrapper around NumPy, CuPy, JAX, sparse and others that are compatible with the Array API standard https://data-apis.org/array-api/latest/. See also NEP 47 https://numpy.org/neps/nep-0047-array-api-standard.html. Unlike array_api_strict, this is not a strict minimal implementation of the Array API, but rather just an extension of the main NumPy namespace with changes needed to be compliant with the Array API. See https://numpy.org/doc/stable/reference/array_api.html for a full list of changes. In particular, unlike array_api_strict, this package does not use a separate Array object, but rather just uses numpy.ndarray directly. Library authors using the Array API may wish to test against array_api_strict to ensure they are not using functionality outside of the standard, but prefer this implementation for the default when working with NumPy arrays. """ __version__ = '1.11.1' from .common import * # noqa: F401, F403
_base_ = [ '../common/ms-poly_3x_coco-instance.py', '../_base_/models/mask-rcnn_r50_fpn.py' ] model = dict( backbone=dict( _delete_=True, type='RegNet', arch='regnetx_4.0gf', out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://regnetx_4.0gf')), neck=dict( type='FPN', in_channels=[80, 240, 560, 1360], out_channels=256, num_outs=5)) optim_wrapper = dict( optimizer=dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.00005), clip_grad=dict(max_norm=35, norm_type=2))
_base_ = [ '../common/mstrain-poly_3x_coco_instance.py', '../_base_/models/mask_rcnn_r50_fpn.py' ] model = dict( backbone=dict( _delete_=True, type='RegNet', arch='regnetx_4.0gf', out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://regnetx_4.0gf')), neck=dict( type='FPN', in_channels=[80, 240, 560, 1360], out_channels=256, num_outs=5)) optim_wrapper = dict( optimizer=dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.00005), clip_grad=dict(max_norm=35, norm_type=2))
_base_ = './yolov3_d53_mstrain-608_273e_coco.py' # fp16 settings optim_wrapper = dict(type='AmpOptimWrapper', loss_scale='dynamic')
_base_ = './yolov3_d53_mstrain-608_273e_coco.py' # fp16 settings fp16 = dict(loss_scale='dynamic')
# Copyright (c) OpenMMLab. All rights reserved. third_part_libs = [ 'pip install -r ../requirements/albu.txt', 'pip install instaboostfast', 'pip install git+https://github.com/cocodataset/panopticapi.git', 'pip install timm', 'pip install mmcls>=1.0.0rc0', 'pip install git+https://github.com/lvis-dataset/lvis-api.git', 'pip install -r ../requirements/multimodal.txt', 'pip install -r ../requirements/tracking.txt', 'pip install git+https://github.com/JonathonLuiten/TrackEval.git', ] default_floating_range = 0.5 model_floating_ranges = {'atss/atss_r50_fpn_1x_coco.py': 0.3}
# Copyright (c) OpenMMLab. All rights reserved. third_part_libs = [ 'pip install -r ../requirements/albu.txt', 'pip install instaboostfast', 'pip install git+https://github.com/cocodataset/panopticapi.git', 'pip install timm', 'pip install mmcls>=1.0.0rc0', 'pip install git+https://github.com/lvis-dataset/lvis-api.git', ] default_floating_range = 0.5 model_floating_ranges = {'atss/atss_r50_fpn_1x_coco.py': 0.3}
from llama_index.llms.openai import OpenAI from llama_index.multi_modal_llms.openai import OpenAIMultiModal def test_embedding_class(): names_of_base_classes = [b.__name__ for b in OpenAIMultiModal.__mro__] assert OpenAI.__name__ in names_of_base_classes
from llama_index.core.multi_modal_llms.base import MultiModalLLM from llama_index.multi_modal_llms.openai import OpenAIMultiModal def test_embedding_class(): names_of_base_classes = [b.__name__ for b in OpenAIMultiModal.__mro__] assert MultiModalLLM.__name__ in names_of_base_classes
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import subprocess import numpy as np import pytest from jina import Document, DocumentArray, Flow from jina.executors.metas import get_default_metas from jina_commons.indexers.dump import export_dump_streaming from ...faiss_searcher import FaissSearcher def _get_docs_from_vecs(queries): docs = DocumentArray() for q in queries: doc = Document(embedding=q) docs.append(doc) return docs @pytest.fixture(scope='function', autouse=True) def metas(tmpdir): os.environ['TEST_WORKSPACE'] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ['TEST_WORKSPACE'] metas['name'] = 'faiss_idx' yield metas del os.environ['TEST_WORKSPACE'] def test_save(metas, tmpdir): vec_idx = np.random.randint(0, high=512, size=[512]).astype(str) vec = np.array(np.random.random([512, 10]), dtype=np.float32) query = np.array(np.random.random([10, 10]), dtype=np.float32) query_docs = _get_docs_from_vecs(query) export_dump_streaming( os.path.join(tmpdir, 'dump'), 1, len(vec_idx), zip(vec_idx, vec, [b'' for _ in range(len(vec))]), ) dump_path = os.path.join(tmpdir, 'dump') f = Flow().add( uses=FaissSearcher, timeout_ready=-1, uses_with={ 'index_key': 'Flat', 'dump_path': dump_path, }, uses_meta=metas, ) with f: f.post(on='/save') new_f = Flow().add( uses=FaissSearcher, timeout_ready=-1, uses_with={ 'index_key': 'Flat', }, uses_meta=metas, ) with new_f: result = new_f.post( on='/search', data=query_docs, return_results=True, parameters={'top_k': 4} )[0].docs assert len(result[0].matches) == 4 for d in result: assert ( d.matches[0].scores['cosine'].value <= d.matches[1].scores['cosine'].value )
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import subprocess import numpy as np import pytest from jina import Document, DocumentArray, Flow from jina.executors.metas import get_default_metas from jina_commons.indexers.dump import export_dump_streaming from ...faiss_searcher import FaissSearcher def _get_docs_from_vecs(queries): docs = DocumentArray() for q in queries: doc = Document(embedding=q) docs.append(doc) return docs @pytest.fixture(scope='function', autouse=True) def metas(tmpdir): os.environ['TEST_WORKSPACE'] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ['TEST_WORKSPACE'] metas['name'] = 'faiss_idx' yield metas del os.environ['TEST_WORKSPACE'] def test_save(metas, tmpdir): vec_idx = np.random.randint(0, high=512, size=[512]).astype(str) vec = np.array(np.random.random([512, 10]), dtype=np.float32) query = np.array(np.random.random([10, 10]), dtype=np.float32) query_docs = _get_docs_from_vecs(query) export_dump_streaming( os.path.join(tmpdir, 'dump'), 1, len(vec_idx), zip(vec_idx, vec, [b'' for _ in range(len(vec))]), ) dump_path = os.path.join(tmpdir, 'dump') f = Flow().add( uses=FaissSearcher, timeout_ready=-1, uses_with={ 'index_key': 'Flat', 'dump_path': dump_path, }, ) with f: f.post(on='/save') new_f = Flow().add( uses=FaissSearcher, timeout_ready=-1, uses_with={ 'index_key': 'Flat', }, ) with new_f: result = new_f.post( on='/search', data=query_docs, return_results=True, parameters={'top_k': 4} )[0].docs assert len(result[0].matches) == 4 for d in result: assert ( d.matches[0].scores['cosine'].value <= d.matches[1].scores['cosine'].value )
import gzip from os import PathLike from pathlib import Path from typing import Union import pytest import yaml from vcr import VCR from vcr.persisters.filesystem import CassetteNotFoundError from vcr.request import Request class CustomSerializer: """Custom serializer for VCR cassettes using YAML and gzip. We're using a custom serializer to avoid the default yaml serializer used by VCR, which is not designed to be safe for untrusted input. This step is an extra precaution necessary because the cassette files are in compressed YAML format, which makes it more difficult to inspect their contents during development or debugging. """ @staticmethod def serialize(cassette_dict: dict) -> bytes: """Convert cassette to YAML and compress it.""" cassette_dict["requests"] = [ request._to_dict() for request in cassette_dict["requests"] ] yml = yaml.safe_dump(cassette_dict) return gzip.compress(yml.encode("utf-8")) @staticmethod def deserialize(data: bytes) -> dict: """Decompress data and convert it from YAML.""" text = gzip.decompress(data).decode("utf-8") cassette = yaml.safe_load(text) cassette["requests"] = [ Request._from_dict(request) for request in cassette["requests"] ] return cassette class CustomPersister: """A custom persister for VCR that uses the CustomSerializer.""" @classmethod def load_cassette( cls, cassette_path: Union[str, PathLike[str]], serializer: CustomSerializer ) -> tuple[dict, dict]: """Load a cassette from a file.""" # If cassette path is already Path this is a no-op cassette_path = Path(cassette_path) if not cassette_path.is_file(): msg = f"Cassette file {cassette_path} does not exist." raise CassetteNotFoundError(msg) with cassette_path.open(mode="rb") as f: data = f.read() deser = serializer.deserialize(data) return deser["requests"], deser["responses"] @staticmethod def save_cassette( cassette_path: Union[str, PathLike[str]], cassette_dict: dict, serializer: CustomSerializer, ) -> None: """Save a cassette to a file.""" data = serializer.serialize(cassette_dict) # if cassette path is already Path this is no operation cassette_path = Path(cassette_path) cassette_folder = cassette_path.parent if not cassette_folder.exists(): cassette_folder.mkdir(parents=True) with cassette_path.open("wb") as f: f.write(data) # A list of headers that should be filtered out of the cassettes. # These are typically associated with sensitive information and should # not be stored in cassettes. _BASE_FILTER_HEADERS = [ ("authorization", "PLACEHOLDER"), ("x-api-key", "PLACEHOLDER"), ("api-key", "PLACEHOLDER"), ] @pytest.fixture(scope="session") def _base_vcr_config() -> dict: """Configuration that every cassette will receive. (Anything permitted by vcr.VCR(**kwargs) can be put here.) """ return { "record_mode": "once", "filter_headers": _BASE_FILTER_HEADERS.copy(), "match_on": ["method", "uri", "body"], "allow_playback_repeats": True, "decode_compressed_response": True, "cassette_library_dir": "tests/cassettes", "path_transformer": VCR.ensure_suffix(".yaml"), } @pytest.fixture(scope="session") def vcr_config(_base_vcr_config: dict) -> dict: return _base_vcr_config
import gzip from os import PathLike from pathlib import Path from typing import Union import pytest import yaml from vcr import VCR from vcr.persisters.filesystem import CassetteNotFoundError from vcr.request import Request class CustomSerializer: """Custom serializer for VCR cassettes using YAML and gzip. We're using a custom serializer to avoid the default yaml serializer used by VCR, which is not designed to be safe for untrusted input. This step is an extra precaution necessary because the cassette files are in compressed YAML format, which makes it more difficult to inspect their contents during development or debugging. """ @staticmethod def serialize(cassette_dict: dict) -> bytes: """Convert cassette to YAML and compress it.""" cassette_dict["requests"] = [ request._to_dict() for request in cassette_dict["requests"] ] yml = yaml.safe_dump(cassette_dict) return gzip.compress(yml.encode("utf-8")) @staticmethod def deserialize(data: bytes) -> dict: """Decompress data and convert it from YAML.""" text = gzip.decompress(data).decode("utf-8") cassette = yaml.safe_load(text) cassette["requests"] = [ Request._from_dict(request) for request in cassette["requests"] ] return cassette class CustomPersister: """A custom persister for VCR that uses the CustomSerializer.""" @classmethod def load_cassette( cls, cassette_path: Union[str, PathLike[str]], serializer: CustomSerializer ) -> tuple[dict, dict]: """Load a cassette from a file.""" # If cassette path is already Path this is a no-op cassette_path = Path(cassette_path) if not cassette_path.is_file(): raise CassetteNotFoundError( f"Cassette file {cassette_path} does not exist." ) with cassette_path.open(mode="rb") as f: data = f.read() deser = serializer.deserialize(data) return deser["requests"], deser["responses"] @staticmethod def save_cassette( cassette_path: Union[str, PathLike[str]], cassette_dict: dict, serializer: CustomSerializer, ) -> None: """Save a cassette to a file.""" data = serializer.serialize(cassette_dict) # if cassette path is already Path this is no operation cassette_path = Path(cassette_path) cassette_folder = cassette_path.parent if not cassette_folder.exists(): cassette_folder.mkdir(parents=True) with cassette_path.open("wb") as f: f.write(data) # A list of headers that should be filtered out of the cassettes. # These are typically associated with sensitive information and should # not be stored in cassettes. _BASE_FILTER_HEADERS = [ ("authorization", "PLACEHOLDER"), ("x-api-key", "PLACEHOLDER"), ("api-key", "PLACEHOLDER"), ] @pytest.fixture(scope="session") def _base_vcr_config() -> dict: """Configuration that every cassette will receive. (Anything permitted by vcr.VCR(**kwargs) can be put here.) """ return { "record_mode": "once", "filter_headers": _BASE_FILTER_HEADERS.copy(), "match_on": ["method", "uri", "body"], "allow_playback_repeats": True, "decode_compressed_response": True, "cassette_library_dir": "tests/cassettes", "path_transformer": VCR.ensure_suffix(".yaml"), } @pytest.fixture(scope="session") def vcr_config(_base_vcr_config: dict) -> dict: return _base_vcr_config
from __future__ import annotations import re import pytest from sentence_transformers import SentenceTransformer from sentence_transformers.evaluation import NanoBEIREvaluator from sentence_transformers.util import is_datasets_available if not is_datasets_available(): pytest.skip( reason="Datasets are not installed. Please install `datasets` with `pip install datasets`", allow_module_level=True, ) def test_nanobeir_evaluator(): """Tests that the NanoBERTEvaluator can be loaded and produces expected metrics""" datasets = ["QuoraRetrieval", "MSMARCO"] query_prompts = { "QuoraRetrieval": "Instruct: Given a question, retrieve questions that are semantically equivalent to the given question\\nQuery: ", "MSMARCO": "Instruct: Given a web search query, retrieve relevant passages that answer the query\\nQuery: ", } model = SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") evaluator = NanoBEIREvaluator( dataset_names=datasets, query_prompts=query_prompts, ) results = evaluator(model) assert len(results) > 0 assert all(isinstance(results[metric], float) for metric in results) def test_nanobeir_evaluator_with_invalid_dataset(): """Test that NanoBEIREvaluator raises an error for invalid dataset names.""" invalid_datasets = ["invalidDataset"] with pytest.raises( ValueError, match=re.escape( r"Dataset(s) ['invalidDataset'] not found in the NanoBEIR collection. " r"Valid dataset names are: ['climatefever', 'dbpedia', 'fever', 'fiqa2018', 'hotpotqa', 'msmarco', 'nfcorpus', 'nq', 'quoraretrieval', 'scidocs', 'arguana', 'scifact', 'touche2020']" ), ): NanoBEIREvaluator(dataset_names=invalid_datasets) def test_nanobeir_evaluator_empty_inputs(): """Test that NanoBEIREvaluator behaves correctly with empty datasets.""" with pytest.raises(ValueError, match="dataset_names cannot be empty. Use None to evaluate on all datasets."): NanoBEIREvaluator(dataset_names=[])
from __future__ import annotations import re import pytest from sentence_transformers import SentenceTransformer from sentence_transformers.evaluation import NanoBEIREvaluator def test_nanobeir_evaluator(): """Tests that the NanoBERTEvaluator can be loaded and produces expected metrics""" datasets = ["QuoraRetrieval", "MSMARCO"] query_prompts = { "QuoraRetrieval": "Instruct: Given a question, retrieve questions that are semantically equivalent to the given question\\nQuery: ", "MSMARCO": "Instruct: Given a web search query, retrieve relevant passages that answer the query\\nQuery: ", } model = SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") evaluator = NanoBEIREvaluator( dataset_names=datasets, query_prompts=query_prompts, ) results = evaluator(model) assert len(results) > 0 assert all(isinstance(results[metric], float) for metric in results) def test_nanobeir_evaluator_with_invalid_dataset(): """Test that NanoBEIREvaluator raises an error for invalid dataset names.""" invalid_datasets = ["invalidDataset"] with pytest.raises( ValueError, match=re.escape( r"Dataset(s) ['invalidDataset'] not found in the NanoBEIR collection. " r"Valid dataset names are: ['climatefever', 'dbpedia', 'fever', 'fiqa2018', 'hotpotqa', 'msmarco', 'nfcorpus', 'nq', 'quoraretrieval', 'scidocs', 'arguana', 'scifact', 'touche2020']" ), ): NanoBEIREvaluator(dataset_names=invalid_datasets) def test_nanobeir_evaluator_empty_inputs(): """Test that NanoBEIREvaluator behaves correctly with empty datasets.""" with pytest.raises(ValueError, match="dataset_names cannot be empty. Use None to evaluate on all datasets."): NanoBEIREvaluator(dataset_names=[])
# Copyright (c) OpenMMLab. All rights reserved. import os from typing import Optional import torch def get_max_cuda_memory(device: Optional[torch.device] = None) -> int: """Returns the maximum GPU memory occupied by tensors in megabytes (MB) for a given device. By default, this returns the peak allocated memory since the beginning of this program. Args: device (torch.device, optional): selected device. Returns statistic for the current device, given by :func:`~torch.cuda.current_device`, if ``device`` is None. Defaults to None. Returns: int: The maximum GPU memory occupied by tensors in megabytes for a given device. """ mem = torch.cuda.max_memory_allocated(device=device) mem_mb = torch.tensor([int(mem) // (1024 * 1024)], dtype=torch.int, device=device) torch.cuda.reset_peak_memory_stats() return int(mem_mb.item()) def is_cuda_available() -> bool: """Returns True if cuda devices exist.""" return torch.cuda.is_available() def is_npu_available() -> bool: """Returns True if Ascend PyTorch and npu devices exist.""" try: import torch_npu # noqa: F401 # Enable operator support for dynamic shape and # binary operator support on the NPU. npu_jit_compile = bool(os.getenv('NPUJITCompile', False)) torch.npu.set_compile_mode(jit_compile=npu_jit_compile) except Exception: return False return hasattr(torch, 'npu') and torch.npu.is_available() def is_mlu_available() -> bool: """Returns True if Cambricon PyTorch and mlu devices exist.""" return hasattr(torch, 'is_mlu_available') and torch.is_mlu_available() def is_mps_available() -> bool: """Return True if mps devices exist. It's specialized for mac m1 chips and require torch version 1.12 or higher. """ return hasattr(torch.backends, 'mps') and torch.backends.mps.is_available() def get_device() -> str: """Returns the currently existing device type. Returns: str: cuda | npu | mlu | mps | cpu. """ if is_npu_available(): return 'npu' elif is_cuda_available(): return 'cuda' elif is_mlu_available(): return 'mlu' elif is_mps_available(): return 'mps' else: return 'cpu'
# Copyright (c) OpenMMLab. All rights reserved. from typing import Optional import torch def get_max_cuda_memory(device: Optional[torch.device] = None) -> int: """Returns the maximum GPU memory occupied by tensors in megabytes (MB) for a given device. By default, this returns the peak allocated memory since the beginning of this program. Args: device (torch.device, optional): selected device. Returns statistic for the current device, given by :func:`~torch.cuda.current_device`, if ``device`` is None. Defaults to None. Returns: int: The maximum GPU memory occupied by tensors in megabytes for a given device. """ mem = torch.cuda.max_memory_allocated(device=device) mem_mb = torch.tensor([int(mem) // (1024 * 1024)], dtype=torch.int, device=device) torch.cuda.reset_peak_memory_stats() return int(mem_mb.item()) def is_cuda_available() -> bool: """Returns True if cuda devices exist.""" return torch.cuda.is_available() def is_npu_available() -> bool: """Returns True if Ascend PyTorch and npu devices exist.""" try: import torch_npu # noqa: F401 # Enable operator support for dynamic shape and # binary operator support on the NPU. torch.npu.set_compile_mode(jit_compile=False) except Exception: return False return hasattr(torch, 'npu') and torch.npu.is_available() def is_mlu_available() -> bool: """Returns True if Cambricon PyTorch and mlu devices exist.""" return hasattr(torch, 'is_mlu_available') and torch.is_mlu_available() def is_mps_available() -> bool: """Return True if mps devices exist. It's specialized for mac m1 chips and require torch version 1.12 or higher. """ return hasattr(torch.backends, 'mps') and torch.backends.mps.is_available() def get_device() -> str: """Returns the currently existing device type. Returns: str: cuda | npu | mlu | mps | cpu. """ if is_npu_available(): return 'npu' elif is_cuda_available(): return 'cuda' elif is_mlu_available(): return 'mlu' elif is_mps_available(): return 'mps' else: return 'cpu'
""" Computes embeddings """ from __future__ import annotations import numpy as np import pytest from sentence_transformers import SentenceTransformer @pytest.mark.skip( "This test fails if optimum.intel.openvino is imported, because openvinotoolkit/nncf " "patches torch._C._nn.gelu in a way that breaks pickling." ) @pytest.mark.parametrize("normalize_embeddings", (False, True)) @pytest.mark.parametrize("prompt_name", (None, "retrieval")) def test_encode_multi_process( stsb_bert_tiny_model: SentenceTransformer, normalize_embeddings: bool, prompt_name: str | None ) -> None: model = stsb_bert_tiny_model model.prompts = {"retrieval": "Represent this sentence for searching relevant passages: "} sentences = [f"This is sentence {i}" for i in range(40)] # Start the multi-process pool on e.g. two CPU devices & compute the embeddings using the pool pool = model.start_multi_process_pool(["cpu", "cpu"]) emb = model.encode_multi_process( sentences, pool, chunk_size=10, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name ) model.stop_multi_process_pool(pool) assert emb.shape == (len(sentences), 128) # Make sure the embeddings aren't just all 0 assert emb.sum() != 0.0 # Compare against normal embeddings emb_normal = model.encode(sentences, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name) diff = np.max(np.abs(emb - emb_normal)) assert diff < 0.001 # Ensure that after normalizing, the means are all almost 0, and otherwise not assert np.all(np.abs(emb.mean(1)) < 0.01) == normalize_embeddings
""" Computes embeddings """ from __future__ import annotations import numpy as np import pytest from sentence_transformers import SentenceTransformer @pytest.mark.parametrize("normalize_embeddings", (False, True)) @pytest.mark.parametrize("prompt_name", (None, "retrieval")) def test_encode_multi_process( stsb_bert_tiny_model: SentenceTransformer, normalize_embeddings: bool, prompt_name: str | None ) -> None: model = stsb_bert_tiny_model model.prompts = {"retrieval": "Represent this sentence for searching relevant passages: "} sentences = [f"This is sentence {i}" for i in range(40)] # Start the multi-process pool on e.g. two CPU devices & compute the embeddings using the pool pool = model.start_multi_process_pool(["cpu", "cpu"]) emb = model.encode_multi_process( sentences, pool, chunk_size=10, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name ) model.stop_multi_process_pool(pool) assert emb.shape == (len(sentences), 128) # Make sure the embeddings aren't just all 0 assert emb.sum() != 0.0 # Compare against normal embeddings emb_normal = model.encode(sentences, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name) diff = np.max(np.abs(emb - emb_normal)) assert diff < 0.001 # Ensure that after normalizing, the means are all almost 0, and otherwise not assert np.all(np.abs(emb.mean(1)) < 0.01) == normalize_embeddings
import numpy as np import pytest from docarray import BaseDoc, DocList from docarray.base_doc import AnyDoc from docarray.documents import ImageDoc, TextDoc from docarray.typing import NdArray @pytest.mark.proto def test_simple_proto(): class CustomDoc(BaseDoc): text: str tensor: NdArray da = DocList( [CustomDoc(text='hello', tensor=np.zeros((3, 224, 224))) for _ in range(10)] ) new_da = DocList[CustomDoc].from_protobuf(da.to_protobuf()) for doc1, doc2 in zip(da, new_da): assert doc1.text == doc2.text assert (doc1.tensor == doc2.tensor).all() @pytest.mark.proto def test_nested_proto(): class CustomDocument(BaseDoc): text: TextDoc image: ImageDoc da = DocList[CustomDocument]( [ CustomDocument( text=TextDoc(text='hello'), image=ImageDoc(tensor=np.zeros((3, 224, 224))), ) for _ in range(10) ] ) DocList[CustomDocument].from_protobuf(da.to_protobuf()) @pytest.mark.proto def test_nested_proto_any_doc(): class CustomDocument(BaseDoc): text: TextDoc image: ImageDoc da = DocList[CustomDocument]( [ CustomDocument( text=TextDoc(text='hello'), image=ImageDoc(tensor=np.zeros((3, 224, 224))), ) for _ in range(10) ] ) DocList.from_protobuf(da.to_protobuf()) @pytest.mark.proto def test_any_doc_list_proto(): doc = AnyDoc(hello='world') pt = DocList([doc]).to_protobuf() docs = DocList.from_protobuf(pt) assert docs[0].dict()['hello'] == 'world' @pytest.mark.proto def test_any_nested_doc_list_proto(): from docarray import BaseDoc, DocList class TextDocWithId(BaseDoc): id: str text: str class ResultTestDoc(BaseDoc): matches: DocList[TextDocWithId] index_da = DocList[TextDocWithId]( [TextDocWithId(id=f'{i}', text=f'ID {i}') for i in range(10)] ) out_da = DocList[ResultTestDoc]([ResultTestDoc(matches=index_da[0:2])]) pb = out_da.to_protobuf() docs = DocList.from_protobuf(pb) assert docs[0].matches[0].id == '0' assert len(docs[0].matches) == 2 assert len(docs) == 1
import numpy as np import pytest from docarray import BaseDoc, DocList from docarray.documents import ImageDoc, TextDoc from docarray.typing import NdArray @pytest.mark.proto def test_simple_proto(): class CustomDoc(BaseDoc): text: str tensor: NdArray da = DocList( [CustomDoc(text='hello', tensor=np.zeros((3, 224, 224))) for _ in range(10)] ) new_da = DocList[CustomDoc].from_protobuf(da.to_protobuf()) for doc1, doc2 in zip(da, new_da): assert doc1.text == doc2.text assert (doc1.tensor == doc2.tensor).all() @pytest.mark.proto def test_nested_proto(): class CustomDocument(BaseDoc): text: TextDoc image: ImageDoc da = DocList[CustomDocument]( [ CustomDocument( text=TextDoc(text='hello'), image=ImageDoc(tensor=np.zeros((3, 224, 224))), ) for _ in range(10) ] ) DocList[CustomDocument].from_protobuf(da.to_protobuf()) @pytest.mark.proto def test_nested_proto_any_doc(): class CustomDocument(BaseDoc): text: TextDoc image: ImageDoc da = DocList[CustomDocument]( [ CustomDocument( text=TextDoc(text='hello'), image=ImageDoc(tensor=np.zeros((3, 224, 224))), ) for _ in range(10) ] ) DocList.from_protobuf(da.to_protobuf())
from __future__ import annotations import json import os import torch from safetensors.torch import load_model as load_safetensors_model from safetensors.torch import save_model as save_safetensors_model from torch import Tensor, nn class WeightedLayerPooling(nn.Module): """Token embeddings are weighted mean of their different hidden layer representations""" def __init__( self, word_embedding_dimension, num_hidden_layers: int = 12, layer_start: int = 4, layer_weights=None ): super(WeightedLayerPooling, self).__init__() self.config_keys = ["word_embedding_dimension", "layer_start", "num_hidden_layers"] self.word_embedding_dimension = word_embedding_dimension self.layer_start = layer_start self.num_hidden_layers = num_hidden_layers self.layer_weights = ( layer_weights if layer_weights is not None else nn.Parameter(torch.tensor([1] * (num_hidden_layers + 1 - layer_start), dtype=torch.float)) ) def forward(self, features: dict[str, Tensor]): ft_all_layers = features["all_layer_embeddings"] all_layer_embedding = torch.stack(ft_all_layers) all_layer_embedding = all_layer_embedding[self.layer_start :, :, :, :] # Start from 4th layers output weight_factor = self.layer_weights.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).expand(all_layer_embedding.size()) weighted_average = (weight_factor * all_layer_embedding).sum(dim=0) / self.layer_weights.sum() features.update({"token_embeddings": weighted_average}) return features def get_word_embedding_dimension(self): return self.word_embedding_dimension def get_config_dict(self): return {key: self.__dict__[key] for key in self.config_keys} def save(self, output_path: str, safe_serialization: bool = True): with open(os.path.join(output_path, "config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) if safe_serialization: save_safetensors_model(self, os.path.join(output_path, "model.safetensors")) else: torch.save(self.state_dict(), os.path.join(output_path, "pytorch_model.bin")) @staticmethod def load(input_path): with open(os.path.join(input_path, "config.json")) as fIn: config = json.load(fIn) model = WeightedLayerPooling(**config) if os.path.exists(os.path.join(input_path, "model.safetensors")): load_safetensors_model(model, os.path.join(input_path, "model.safetensors")) else: model.load_state_dict( torch.load(os.path.join(input_path, "pytorch_model.bin"), map_location=torch.device("cpu")) ) return model
import json import os from typing import Dict import torch from safetensors.torch import load_model as load_safetensors_model from safetensors.torch import save_model as save_safetensors_model from torch import Tensor, nn class WeightedLayerPooling(nn.Module): """Token embeddings are weighted mean of their different hidden layer representations""" def __init__( self, word_embedding_dimension, num_hidden_layers: int = 12, layer_start: int = 4, layer_weights=None ): super(WeightedLayerPooling, self).__init__() self.config_keys = ["word_embedding_dimension", "layer_start", "num_hidden_layers"] self.word_embedding_dimension = word_embedding_dimension self.layer_start = layer_start self.num_hidden_layers = num_hidden_layers self.layer_weights = ( layer_weights if layer_weights is not None else nn.Parameter(torch.tensor([1] * (num_hidden_layers + 1 - layer_start), dtype=torch.float)) ) def forward(self, features: Dict[str, Tensor]): ft_all_layers = features["all_layer_embeddings"] all_layer_embedding = torch.stack(ft_all_layers) all_layer_embedding = all_layer_embedding[self.layer_start :, :, :, :] # Start from 4th layers output weight_factor = self.layer_weights.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).expand(all_layer_embedding.size()) weighted_average = (weight_factor * all_layer_embedding).sum(dim=0) / self.layer_weights.sum() features.update({"token_embeddings": weighted_average}) return features def get_word_embedding_dimension(self): return self.word_embedding_dimension def get_config_dict(self): return {key: self.__dict__[key] for key in self.config_keys} def save(self, output_path: str, safe_serialization: bool = True): with open(os.path.join(output_path, "config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) if safe_serialization: save_safetensors_model(self, os.path.join(output_path, "model.safetensors")) else: torch.save(self.state_dict(), os.path.join(output_path, "pytorch_model.bin")) @staticmethod def load(input_path): with open(os.path.join(input_path, "config.json")) as fIn: config = json.load(fIn) model = WeightedLayerPooling(**config) if os.path.exists(os.path.join(input_path, "model.safetensors")): load_safetensors_model(model, os.path.join(input_path, "model.safetensors")) else: model.load_state_dict( torch.load(os.path.join(input_path, "pytorch_model.bin"), map_location=torch.device("cpu")) ) return model
import pytest from llama_index.embeddings.nvidia import NVIDIAEmbedding as Interface @pytest.mark.integration def test_available_models() -> None: models = Interface().available_models assert models assert isinstance(models, list) assert all(isinstance(model.id, str) for model in models)
import pytest from llama_index.embeddings.nvidia import NVIDIAEmbedding as Interface @pytest.mark.integration() def test_available_models() -> None: models = Interface().available_models assert models assert isinstance(models, list) assert all(isinstance(model.id, str) for model in models)
_base_ = './retinanet_r50_fpn_1x_coco.py' # MMEngine support the following two ways, users can choose # according to convenience # optim_wrapper = dict(type='AmpOptimWrapper') _base_.optim_wrapper.type = 'AmpOptimWrapper'
_base_ = './retinanet_r50_fpn_1x_coco.py' # fp16 settings fp16 = dict(loss_scale=512.)
import contextlib from multiprocessing import Pool, RLock from tqdm.auto import tqdm from ..utils import experimental, logging logger = logging.get_logger(__name__) class ParallelBackendConfig: backend_name = None @experimental def parallel_map(function, iterable, num_proc, batched, batch_size, types, disable_tqdm, desc, single_map_nested_func): """ **Experimental.** Apply a function to iterable elements in parallel, where the implementation uses either multiprocessing.Pool or joblib for parallelization. Args: function (`Callable[[Any], Any]`): Function to be applied to `iterable`. iterable (`list`, `tuple` or `np.ndarray`): Iterable elements to apply function to. num_proc (`int`): Number of processes (if no backend specified) or jobs (using joblib). types (`tuple`): Additional types (besides `dict` values) to apply `function` recursively to their elements. disable_tqdm (`bool`): Whether to disable the tqdm progressbar. desc (`str`): Prefix for the tqdm progressbar. single_map_nested_func (`Callable`): Map function that applies `function` to an element from `iterable`. Takes a tuple of function, data_struct, types, rank, disable_tqdm, desc as input, where data_struct is an element of `iterable`, and `rank` is used for progress bar. """ if ParallelBackendConfig.backend_name is None: return _map_with_multiprocessing_pool( function, iterable, num_proc, batched, batch_size, types, disable_tqdm, desc, single_map_nested_func ) return _map_with_joblib( function, iterable, num_proc, batched, batch_size, types, disable_tqdm, desc, single_map_nested_func ) def _map_with_multiprocessing_pool( function, iterable, num_proc, batched, batch_size, types, disable_tqdm, desc, single_map_nested_func ): num_proc = num_proc if num_proc <= len(iterable) else len(iterable) split_kwds = [] # We organize the splits ourselve (contiguous splits) for index in range(num_proc): div = len(iterable) // num_proc mod = len(iterable) % num_proc start = div * index + min(index, mod) end = start + div + (1 if index < mod else 0) split_kwds.append((function, iterable[start:end], batched, batch_size, types, index, disable_tqdm, desc)) if len(iterable) != sum(len(i[1]) for i in split_kwds): raise ValueError( f"Error dividing inputs iterable among processes. " f"Total number of objects {len(iterable)}, " f"length: {sum(len(i[1]) for i in split_kwds)}" ) logger.info( f"Spawning {num_proc} processes for {len(iterable)} objects in slices of {[len(i[1]) for i in split_kwds]}" ) initargs, initializer = None, None if not disable_tqdm: initargs, initializer = (RLock(),), tqdm.set_lock with Pool(num_proc, initargs=initargs, initializer=initializer) as pool: mapped = pool.map(single_map_nested_func, split_kwds) logger.info(f"Finished {num_proc} processes") mapped = [obj for proc_res in mapped for obj in proc_res] logger.info(f"Unpacked {len(mapped)} objects") return mapped def _map_with_joblib( function, iterable, num_proc, batched, batch_size, types, disable_tqdm, desc, single_map_nested_func ): # progress bar is not yet supported for _map_with_joblib, because tqdm couldn't accurately be applied to joblib, # and it requires monkey-patching joblib internal classes which is subject to change import joblib with joblib.parallel_backend(ParallelBackendConfig.backend_name, n_jobs=num_proc): return joblib.Parallel()( joblib.delayed(single_map_nested_func)((function, obj, batched, batch_size, types, None, True, None)) for obj in iterable ) @experimental @contextlib.contextmanager def parallel_backend(backend_name: str): """ **Experimental.** Configures the parallel backend for parallelized dataset loading, which uses the parallelization implemented by joblib. Args: backend_name (str): Name of backend for parallelization implementation, has to be supported by joblib. Example usage: ```py with parallel_backend('spark'): dataset = load_dataset(..., num_proc=2) ``` """ ParallelBackendConfig.backend_name = backend_name if backend_name == "spark": from joblibspark import register_spark register_spark() # TODO: call create_cache_and_write_probe if "download" in steps # TODO: raise NotImplementedError when Dataset.map etc is called try: yield finally: ParallelBackendConfig.backend_name = None
import contextlib from multiprocessing import Pool, RLock from tqdm.auto import tqdm from ..utils import experimental, logging logger = logging.get_logger(__name__) class ParallelBackendConfig: backend_name = None @experimental def parallel_map(function, iterable, num_proc, types, disable_tqdm, desc, single_map_nested_func): """ **Experimental.** Apply a function to iterable elements in parallel, where the implementation uses either multiprocessing.Pool or joblib for parallelization. Args: function (`Callable[[Any], Any]`): Function to be applied to `iterable`. iterable (`list`, `tuple` or `np.ndarray`): Iterable elements to apply function to. num_proc (`int`): Number of processes (if no backend specified) or jobs (using joblib). types (`tuple`): Additional types (besides `dict` values) to apply `function` recursively to their elements. disable_tqdm (`bool`): Whether to disable the tqdm progressbar. desc (`str`): Prefix for the tqdm progressbar. single_map_nested_func (`Callable`): Map function that applies `function` to an element from `iterable`. Takes a tuple of function, data_struct, types, rank, disable_tqdm, desc as input, where data_struct is an element of `iterable`, and `rank` is used for progress bar. """ if ParallelBackendConfig.backend_name is None: return _map_with_multiprocessing_pool( function, iterable, num_proc, types, disable_tqdm, desc, single_map_nested_func ) return _map_with_joblib(function, iterable, num_proc, types, disable_tqdm, desc, single_map_nested_func) def _map_with_multiprocessing_pool(function, iterable, num_proc, types, disable_tqdm, desc, single_map_nested_func): num_proc = num_proc if num_proc <= len(iterable) else len(iterable) split_kwds = [] # We organize the splits ourselve (contiguous splits) for index in range(num_proc): div = len(iterable) // num_proc mod = len(iterable) % num_proc start = div * index + min(index, mod) end = start + div + (1 if index < mod else 0) split_kwds.append((function, iterable[start:end], types, index, disable_tqdm, desc)) if len(iterable) != sum(len(i[1]) for i in split_kwds): raise ValueError( f"Error dividing inputs iterable among processes. " f"Total number of objects {len(iterable)}, " f"length: {sum(len(i[1]) for i in split_kwds)}" ) logger.info( f"Spawning {num_proc} processes for {len(iterable)} objects in slices of {[len(i[1]) for i in split_kwds]}" ) initargs, initializer = None, None if not disable_tqdm: initargs, initializer = (RLock(),), tqdm.set_lock with Pool(num_proc, initargs=initargs, initializer=initializer) as pool: mapped = pool.map(single_map_nested_func, split_kwds) logger.info(f"Finished {num_proc} processes") mapped = [obj for proc_res in mapped for obj in proc_res] logger.info(f"Unpacked {len(mapped)} objects") return mapped def _map_with_joblib(function, iterable, num_proc, types, disable_tqdm, desc, single_map_nested_func): # progress bar is not yet supported for _map_with_joblib, because tqdm couldn't accurately be applied to joblib, # and it requires monkey-patching joblib internal classes which is subject to change import joblib with joblib.parallel_backend(ParallelBackendConfig.backend_name, n_jobs=num_proc): return joblib.Parallel()( joblib.delayed(single_map_nested_func)((function, obj, types, None, True, None)) for obj in iterable ) @experimental @contextlib.contextmanager def parallel_backend(backend_name: str): """ **Experimental.** Configures the parallel backend for parallelized dataset loading, which uses the parallelization implemented by joblib. Args: backend_name (str): Name of backend for parallelization implementation, has to be supported by joblib. Example usage: ```py with parallel_backend('spark'): dataset = load_dataset(..., num_proc=2) ``` """ ParallelBackendConfig.backend_name = backend_name if backend_name == "spark": from joblibspark import register_spark register_spark() # TODO: call create_cache_and_write_probe if "download" in steps # TODO: raise NotImplementedError when Dataset.map etc is called try: yield finally: ParallelBackendConfig.backend_name = None
# Copyright (c) OpenMMLab. All rights reserved. """MMEngine provides 11 root registries to support using modules across projects. More datails can be found at https://mmengine.readthedocs.io/en/latest/tutorials/registry.html. """ from .registry import Registry # manage all kinds of runners like `EpochBasedRunner` and `IterBasedRunner` RUNNERS = Registry('runner') # manage runner constructors that define how to initialize runners RUNNER_CONSTRUCTORS = Registry('runner constructor') # manage all kinds of loops like `EpochBasedTrainLoop` LOOPS = Registry('loop') # manage all kinds of hooks like `CheckpointHook` HOOKS = Registry('hook') # manage data-related modules DATASETS = Registry('dataset') DATA_SAMPLERS = Registry('data sampler') TRANSFORMS = Registry('transform') # mangage all kinds of modules inheriting `nn.Module` MODELS = Registry('model') # mangage all kinds of model wrappers like 'MMDistributedDataParallel' MODEL_WRAPPERS = Registry('model_wrapper') # mangage all kinds of weight initialization modules like `Uniform` WEIGHT_INITIALIZERS = Registry('weight initializer') # mangage all kinds of optimizers like `SGD` and `Adam` OPTIMIZERS = Registry('optimizer') # manage constructors that customize the optimization hyperparameters. OPTIMIZER_CONSTRUCTORS = Registry('optimizer constructor') # mangage all kinds of parameter schedulers like `MultiStepLR` PARAM_SCHEDULERS = Registry('parameter scheduler') # manage all kinds of evaluators for computing metrics EVALUATORS = Registry('evaluator') # manage task-specific modules like anchor generators and box coders TASK_UTILS = Registry('task util') # manage visualizer VISUALIZERS = Registry('visualizer') # manage writer WRITERS = Registry('writer')
# Copyright (c) OpenMMLab. All rights reserved. """MMEngine provides 11 root registries to support using modules across projects. More datails can be found at https://mmengine.readthedocs.io/en/latest/tutorials/registry.html. """ from .registry import Registry # manage all kinds of runners like `EpochBasedRunner` and `IterBasedRunner` RUNNERS = Registry('runner') # manage runner constructors that define how to initialize runners RUNNER_CONSTRUCTORS = Registry('runner constructor') # manage all kinds of loops like `EpochBasedTrainLoop` LOOPS = Registry('loop') # manage all kinds of hooks like `CheckpointHook` HOOKS = Registry('hook') # manage data-related modules DATASETS = Registry('dataset') DATA_SAMPLERS = Registry('data sampler') TRANSFORMS = Registry('transform') # mangage all kinds of modules inheriting `nn.Module` MODELS = Registry('model') # mangage all kinds of model wrappers like 'MMDistributedDataParallel' MODEL_WRAPPERS = Registry('model_wrapper') # mangage all kinds of weight initialization modules like `Uniform` WEIGHT_INITIALIZERS = Registry('weight initializer') # mangage all kinds of optimizers like `SGD` and `Adam` OPTIMIZERS = Registry('optimizer') # manage constructors that customize the optimization hyperparameters. OPTIMIZER_CONSTRUCTORS = Registry('optimizer constructor') # mangage all kinds of parameter schedulers like `MultiStepLR` PARAM_SCHEDULERS = Registry('parameter scheduler') # manage task-specific modules like anchor generators and box coders TASK_UTILS = Registry('task util') # manage all kinds of evaluators for computing metrics EVALUATORS = Registry('evaluator') # manage visualizer VISUALIZERS = Registry('visualizer') # manage writer WRITERS = Registry('writer')
import unittest import torch from torchaudio_unittest.common_utils import PytorchTestCase, skipIfNoCuda from .functional_impl import Functional, FunctionalCUDAOnly @skipIfNoCuda class TestFunctionalFloat32(Functional, PytorchTestCase): dtype = torch.float32 device = torch.device("cuda") @unittest.expectedFailure def test_lfilter_9th_order_filter_stability(self): super().test_lfilter_9th_order_filter_stability() @skipIfNoCuda class TestLFilterFloat64(Functional, PytorchTestCase): dtype = torch.float64 device = torch.device("cuda") @skipIfNoCuda class TestFunctionalCUDAOnlyFloat32(FunctionalCUDAOnly, PytorchTestCase): dtype = torch.float32 device = torch.device("cuda") @skipIfNoCuda class TestFunctionalCUDAOnlyFloat64(FunctionalCUDAOnly, PytorchTestCase): dtype = torch.float64 device = torch.device("cuda")
import unittest import torch from torchaudio_unittest.common_utils import PytorchTestCase, skipIfNoCuda from .functional_impl import Functional @skipIfNoCuda class TestFunctionalFloat32(Functional, PytorchTestCase): dtype = torch.float32 device = torch.device("cuda") @unittest.expectedFailure def test_lfilter_9th_order_filter_stability(self): super().test_lfilter_9th_order_filter_stability() @skipIfNoCuda class TestLFilterFloat64(Functional, PytorchTestCase): dtype = torch.float64 device = torch.device("cuda")
import pytest import torch from mmdet.models.backbones.pvt import (PVTEncoderLayer, PyramidVisionTransformer, PyramidVisionTransformerV2) def test_pvt_block(): # test PVT structure and forward block = PVTEncoderLayer( embed_dims=64, num_heads=4, feedforward_channels=256) assert block.ffn.embed_dims == 64 assert block.attn.num_heads == 4 assert block.ffn.feedforward_channels == 256 x = torch.randn(1, 56 * 56, 64) x_out = block(x, (56, 56)) assert x_out.shape == torch.Size([1, 56 * 56, 64]) def test_pvt(): """Test PVT backbone.""" with pytest.raises(TypeError): # Pretrained arg must be str or None. PyramidVisionTransformer(pretrained=123) # test pretrained image size with pytest.raises(AssertionError): PyramidVisionTransformer(pretrain_img_size=(224, 224, 224)) # Test absolute position embedding temp = torch.randn((1, 3, 224, 224)) model = PyramidVisionTransformer( pretrain_img_size=224, use_abs_pos_embed=True) model.init_weights() model(temp) # Test normal inference temp = torch.randn((1, 3, 32, 32)) model = PyramidVisionTransformer() outs = model(temp) assert outs[0].shape == (1, 64, 8, 8) assert outs[1].shape == (1, 128, 4, 4) assert outs[2].shape == (1, 320, 2, 2) assert outs[3].shape == (1, 512, 1, 1) # Test abnormal inference size temp = torch.randn((1, 3, 33, 33)) model = PyramidVisionTransformer() outs = model(temp) assert outs[0].shape == (1, 64, 8, 8) assert outs[1].shape == (1, 128, 4, 4) assert outs[2].shape == (1, 320, 2, 2) assert outs[3].shape == (1, 512, 1, 1) # Test abnormal inference size temp = torch.randn((1, 3, 112, 137)) model = PyramidVisionTransformer() outs = model(temp) assert outs[0].shape == (1, 64, 28, 34) assert outs[1].shape == (1, 128, 14, 17) assert outs[2].shape == (1, 320, 7, 8) assert outs[3].shape == (1, 512, 3, 4) def test_pvtv2(): """Test PVTv2 backbone.""" with pytest.raises(TypeError): # Pretrained arg must be str or None. PyramidVisionTransformerV2(pretrained=123) # test pretrained image size with pytest.raises(AssertionError): PyramidVisionTransformerV2(pretrain_img_size=(224, 224, 224)) # Test normal inference temp = torch.randn((1, 3, 32, 32)) model = PyramidVisionTransformerV2() outs = model(temp) assert outs[0].shape == (1, 64, 8, 8) assert outs[1].shape == (1, 128, 4, 4) assert outs[2].shape == (1, 320, 2, 2) assert outs[3].shape == (1, 512, 1, 1) # Test abnormal inference size temp = torch.randn((1, 3, 31, 31)) model = PyramidVisionTransformerV2() outs = model(temp) assert outs[0].shape == (1, 64, 8, 8) assert outs[1].shape == (1, 128, 4, 4) assert outs[2].shape == (1, 320, 2, 2) assert outs[3].shape == (1, 512, 1, 1) # Test abnormal inference size temp = torch.randn((1, 3, 112, 137)) model = PyramidVisionTransformerV2() outs = model(temp) assert outs[0].shape == (1, 64, 28, 35) assert outs[1].shape == (1, 128, 14, 18) assert outs[2].shape == (1, 320, 7, 9) assert outs[3].shape == (1, 512, 4, 5)
import pytest import torch from mmdet.models.backbones.pvt import (PVTEncoderLayer, PyramidVisionTransformer, PyramidVisionTransformerV2) def test_pvt_block(): # test PVT structure and forward block = PVTEncoderLayer( embed_dims=64, num_heads=4, feedforward_channels=256) assert block.ffn.embed_dims == 64 assert block.attn.num_heads == 4 assert block.ffn.feedforward_channels == 256 x = torch.randn(1, 56 * 56, 64) x_out = block(x, (56, 56)) assert x_out.shape == torch.Size([1, 56 * 56, 64]) def test_pvt(): """Test PVT backbone.""" with pytest.raises(TypeError): # Pretrained arg must be str or None. PyramidVisionTransformer(pretrained=123) # test pretrained image size with pytest.raises(AssertionError): PyramidVisionTransformer(pretrain_img_size=(224, 224, 224)) # Test absolute position embedding temp = torch.randn((1, 3, 224, 224)) model = PyramidVisionTransformer( pretrain_img_size=224, use_abs_pos_embed=True) model.init_weights() model(temp) # Test normal inference temp = torch.randn((1, 3, 512, 512)) model = PyramidVisionTransformer() outs = model(temp) assert outs[0].shape == (1, 64, 128, 128) assert outs[1].shape == (1, 128, 64, 64) assert outs[2].shape == (1, 320, 32, 32) assert outs[3].shape == (1, 512, 16, 16) # Test abnormal inference size temp = torch.randn((1, 3, 511, 511)) model = PyramidVisionTransformer() outs = model(temp) assert outs[0].shape == (1, 64, 127, 127) assert outs[1].shape == (1, 128, 63, 63) assert outs[2].shape == (1, 320, 31, 31) assert outs[3].shape == (1, 512, 15, 15) # Test abnormal inference size temp = torch.randn((1, 3, 112, 137)) model = PyramidVisionTransformer() outs = model(temp) assert outs[0].shape == (1, 64, 28, 34) assert outs[1].shape == (1, 128, 14, 17) assert outs[2].shape == (1, 320, 7, 8) assert outs[3].shape == (1, 512, 3, 4) def test_pvtv2(): """Test PVTv2 backbone.""" with pytest.raises(TypeError): # Pretrained arg must be str or None. PyramidVisionTransformerV2(pretrained=123) # test pretrained image size with pytest.raises(AssertionError): PyramidVisionTransformerV2(pretrain_img_size=(224, 224, 224)) # Test normal inference temp = torch.randn((1, 3, 512, 512)) model = PyramidVisionTransformerV2() outs = model(temp) assert outs[0].shape == (1, 64, 128, 128) assert outs[1].shape == (1, 128, 64, 64) assert outs[2].shape == (1, 320, 32, 32) assert outs[3].shape == (1, 512, 16, 16) # Test abnormal inference size temp = torch.randn((1, 3, 511, 511)) model = PyramidVisionTransformerV2() outs = model(temp) assert outs[0].shape == (1, 64, 128, 128) assert outs[1].shape == (1, 128, 64, 64) assert outs[2].shape == (1, 320, 32, 32) assert outs[3].shape == (1, 512, 16, 16) # Test abnormal inference size temp = torch.randn((1, 3, 112, 137)) model = PyramidVisionTransformerV2() outs = model(temp) assert outs[0].shape == (1, 64, 28, 35) assert outs[1].shape == (1, 128, 14, 18) assert outs[2].shape == (1, 320, 7, 9) assert outs[3].shape == (1, 512, 4, 5)
# Copyright (c) OpenMMLab. All rights reserved. import torch.nn as nn import torch.utils.checkpoint as cp from mmcv.cnn import ConvModule from mmcv.cnn.bricks import DropPath from mmcv.runner import BaseModule from .se_layer import SELayer class InvertedResidual(BaseModule): """Inverted Residual Block. Args: in_channels (int): The input channels of this Module. out_channels (int): The output channels of this Module. mid_channels (int): The input channels of the depthwise convolution. kernel_size (int): The kernel size of the depthwise convolution. Default: 3. stride (int): The stride of the depthwise convolution. Default: 1. se_cfg (dict): Config dict for se layer. Default: None, which means no se layer. with_expand_conv (bool): Use expand conv or not. If set False, mid_channels must be the same with in_channels. Default: True. conv_cfg (dict): Config dict for convolution layer. Default: None, which means using conv2d. norm_cfg (dict): Config dict for normalization layer. Default: dict(type='BN'). act_cfg (dict): Config dict for activation layer. Default: dict(type='ReLU'). drop_path_rate (float): stochastic depth rate. Defaults to 0. with_cp (bool): Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. Default: False. init_cfg (dict or list[dict], optional): Initialization config dict. Default: None Returns: Tensor: The output tensor. """ def __init__(self, in_channels, out_channels, mid_channels, kernel_size=3, stride=1, se_cfg=None, with_expand_conv=True, conv_cfg=None, norm_cfg=dict(type='BN'), act_cfg=dict(type='ReLU'), drop_path_rate=0., with_cp=False, init_cfg=None): super(InvertedResidual, self).__init__(init_cfg) self.with_res_shortcut = (stride == 1 and in_channels == out_channels) assert stride in [1, 2], f'stride must in [1, 2]. ' \ f'But received {stride}.' self.with_cp = with_cp self.drop_path = DropPath( drop_path_rate) if drop_path_rate > 0 else nn.Identity() self.with_se = se_cfg is not None self.with_expand_conv = with_expand_conv if self.with_se: assert isinstance(se_cfg, dict) if not self.with_expand_conv: assert mid_channels == in_channels if self.with_expand_conv: self.expand_conv = ConvModule( in_channels=in_channels, out_channels=mid_channels, kernel_size=1, stride=1, padding=0, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg) self.depthwise_conv = ConvModule( in_channels=mid_channels, out_channels=mid_channels, kernel_size=kernel_size, stride=stride, padding=kernel_size // 2, groups=mid_channels, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg) if self.with_se: self.se = SELayer(**se_cfg) self.linear_conv = ConvModule( in_channels=mid_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=None) def forward(self, x): def _inner_forward(x): out = x if self.with_expand_conv: out = self.expand_conv(out) out = self.depthwise_conv(out) if self.with_se: out = self.se(out) out = self.linear_conv(out) if self.with_res_shortcut: return x + self.drop_path(out) else: return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) return out
# Copyright (c) OpenMMLab. All rights reserved. import torch.utils.checkpoint as cp from mmcv.cnn import ConvModule from mmcv.runner import BaseModule from .se_layer import SELayer class InvertedResidual(BaseModule): """Inverted Residual Block. Args: in_channels (int): The input channels of this Module. out_channels (int): The output channels of this Module. mid_channels (int): The input channels of the depthwise convolution. kernel_size (int): The kernel size of the depthwise convolution. Default: 3. stride (int): The stride of the depthwise convolution. Default: 1. se_cfg (dict): Config dict for se layer. Default: None, which means no se layer. with_expand_conv (bool): Use expand conv or not. If set False, mid_channels must be the same with in_channels. Default: True. conv_cfg (dict): Config dict for convolution layer. Default: None, which means using conv2d. norm_cfg (dict): Config dict for normalization layer. Default: dict(type='BN'). act_cfg (dict): Config dict for activation layer. Default: dict(type='ReLU'). with_cp (bool): Use checkpoint or not. Using checkpoint will save some memory while slowing down the training speed. Default: False. init_cfg (dict or list[dict], optional): Initialization config dict. Default: None Returns: Tensor: The output tensor. """ def __init__(self, in_channels, out_channels, mid_channels, kernel_size=3, stride=1, se_cfg=None, with_expand_conv=True, conv_cfg=None, norm_cfg=dict(type='BN'), act_cfg=dict(type='ReLU'), with_cp=False, init_cfg=None): super(InvertedResidual, self).__init__(init_cfg) self.with_res_shortcut = (stride == 1 and in_channels == out_channels) assert stride in [1, 2], f'stride must in [1, 2]. ' \ f'But received {stride}.' self.with_cp = with_cp self.with_se = se_cfg is not None self.with_expand_conv = with_expand_conv if self.with_se: assert isinstance(se_cfg, dict) if not self.with_expand_conv: assert mid_channels == in_channels if self.with_expand_conv: self.expand_conv = ConvModule( in_channels=in_channels, out_channels=mid_channels, kernel_size=1, stride=1, padding=0, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg) self.depthwise_conv = ConvModule( in_channels=mid_channels, out_channels=mid_channels, kernel_size=kernel_size, stride=stride, padding=kernel_size // 2, groups=mid_channels, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg) if self.with_se: self.se = SELayer(**se_cfg) self.linear_conv = ConvModule( in_channels=mid_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=None) def forward(self, x): def _inner_forward(x): out = x if self.with_expand_conv: out = self.expand_conv(out) out = self.depthwise_conv(out) if self.with_se: out = self.se(out) out = self.linear_conv(out) if self.with_res_shortcut: return x + out else: return out if self.with_cp and x.requires_grad: out = cp.checkpoint(_inner_forward, x) else: out = _inner_forward(x) return out
from typing import Any, Dict, List, Optional, Sequence, Tuple from llama_index.core.base.llms.types import ChatMessage, MessageRole from llama_index.core.base.llms.generic_utils import get_from_param_or_env DEFAULT_FIREWORKS_API_BASE = "https://api.fireworks.ai/inference/v1" DEFAULT_FIREWORKS_API_VERSION = "" LLAMA_MODELS = { "accounts/fireworks/models/llama-v2-7b-chat": 4096, "accounts/fireworks/models/llama-v2-13b-chat": 4096, "accounts/fireworks/models/llama-v2-70b-chat": 4096, "accounts/fireworks/models/llama-v2-34b-code-instruct": 16384, "accounts/fireworks/models/llamaguard-7b": 4096, "accounts/fireworks/models/llama-v3-8b-instruct": 8192, "accounts/fireworks/models/llama-v3-70b-instruct": 8192, "accounts/fireworks/models/llama-v3p1-8b-instruct": 131072, "accounts/fireworks/models/llama-v3p1-70b-instruct": 131072, "accounts/fireworks/models/llama-v3p1-405b-instruct": 131072, "accounts/fireworks/models/llama-v3p2-1b-instruct": 131072, "accounts/fireworks/models/llama-v3p2-3b-instruct": 131072, "accounts/fireworks/models/llama-v3p2-11b-vision-instruct": 131072, "accounts/fireworks/models/llama-v3p2-90b-vision-instruct": 131072, } MISTRAL_MODELS = { "accounts/fireworks/models/mistral-7b-instruct-4k": 16384, "accounts/fireworks/models/mixtral-8x7b-instruct": 32768, "accounts/fireworks/models/firefunction-v1": 32768, "accounts/fireworks/models/mixtral-8x22b-instruct": 65536, } FUNCTION_CALLING_MODELS = { "accounts/fireworks/models/firefunction-v2": 8192, } DEEPSEEK_MODELS = { "accounts/fireworks/models/deepseek-v3": 131072, "accounts/fireworks/models/deepseek-r1": 163840, } ALL_AVAILABLE_MODELS = { **LLAMA_MODELS, **MISTRAL_MODELS, **FUNCTION_CALLING_MODELS, **DEEPSEEK_MODELS, } DISCONTINUED_MODELS: Dict[str, int] = {} def fireworks_modelname_to_contextsize(modelname: str) -> int: """ Calculate the maximum number of tokens possible to generate for a model. Args: modelname: The modelname we want to know the context size for. Returns: The maximum context size Example: .. code-block:: python max_tokens = fireworks_modelname_to_contextsize(model_name) """ # handling finetuned models # TO BE FILLED if modelname in DISCONTINUED_MODELS: raise ValueError( f"Fireworks hosted model {modelname} has been discontinued. " "Please choose another model." ) context_size = ALL_AVAILABLE_MODELS.get(modelname) if context_size is None: raise ValueError( f"Unknown model: {modelname}. Please provide a valid Fireworks model name." "Known models are: " + ", ".join(ALL_AVAILABLE_MODELS.keys()) ) return context_size def is_function_calling_model(model: str) -> bool: return "function" in model def _message_to_fireworks_prompt(message: ChatMessage) -> Dict[str, Any]: if message.role == MessageRole.USER: prompt = {"role": "user", "content": message.content} elif message.role == MessageRole.ASSISTANT: prompt = {"role": "assistant", "content": message.content} elif message.role == MessageRole.SYSTEM: prompt = {"role": "system", "content": message.content} elif message.role == MessageRole.FUNCTION: raise ValueError(f"Message role {MessageRole.FUNCTION} is not supported.") else: raise ValueError(f"Unknown message role: {message.role}") return prompt def messages_to_fireworks_prompt(messages: Sequence[ChatMessage]) -> List[Dict]: if len(messages) == 0: raise ValueError("Got empty list of messages.") return [_message_to_fireworks_prompt(message) for message in messages] def resolve_fireworks_credentials( api_key: Optional[str] = None, api_base: Optional[str] = None, api_version: Optional[str] = None, ) -> Tuple[Optional[str], str, str]: """ "Resolve OpenAI credentials. The order of precedence is: 1. param 2. env 3. openai module 4. default """ # resolve from param or env api_key = get_from_param_or_env("api_key", api_key, "FIREWORKS_API_KEY", "") api_base = get_from_param_or_env("api_base", api_base, "FIREWORKS_API_BASE", "") api_version = get_from_param_or_env( "api_version", api_version, "FIREWORKS_API_VERSION", "" ) # resolve from openai module or default final_api_key = api_key or "" final_api_base = api_base or DEFAULT_FIREWORKS_API_BASE final_api_version = api_version or DEFAULT_FIREWORKS_API_VERSION return final_api_key, str(final_api_base), final_api_version
from typing import Any, Dict, List, Optional, Sequence, Tuple from llama_index.core.base.llms.types import ChatMessage, MessageRole from llama_index.core.base.llms.generic_utils import get_from_param_or_env DEFAULT_FIREWORKS_API_BASE = "https://api.fireworks.ai/inference/v1" DEFAULT_FIREWORKS_API_VERSION = "" LLAMA_MODELS = { "accounts/fireworks/models/llama-v2-7b-chat": 4096, "accounts/fireworks/models/llama-v2-13b-chat": 4096, "accounts/fireworks/models/llama-v2-70b-chat": 4096, "accounts/fireworks/models/llama-v2-34b-code-instruct": 16384, "accounts/fireworks/models/llamaguard-7b": 4096, "accounts/fireworks/models/llama-v3-8b-instruct": 8192, "accounts/fireworks/models/llama-v3-70b-instruct": 8192, "accounts/fireworks/models/llama-v3p1-8b-instruct": 131072, "accounts/fireworks/models/llama-v3p1-70b-instruct": 131072, "accounts/fireworks/models/llama-v3p1-405b-instruct": 131072, "accounts/fireworks/models/llama-v3p2-1b-instruct": 131072, "accounts/fireworks/models/llama-v3p2-3b-instruct": 131072, "accounts/fireworks/models/llama-v3p2-11b-vision-instruct": 131072, "accounts/fireworks/models/llama-v3p2-90b-vision-instruct": 131072, } MISTRAL_MODELS = { "accounts/fireworks/models/mistral-7b-instruct-4k": 16384, "accounts/fireworks/models/mixtral-8x7b-instruct": 32768, "accounts/fireworks/models/firefunction-v1": 32768, "accounts/fireworks/models/mixtral-8x22b-instruct": 65536, } FUNCTION_CALLING_MODELS = { "accounts/fireworks/models/firefunction-v2": 8192, } DEEPSEEK_MODELS = { "accounts/fireworks/models/deepseek-v3": 131072, "accounts/fireworks/models/deepseek-r1": 163840, } ALL_AVAILABLE_MODELS = { **LLAMA_MODELS, **MISTRAL_MODELS, **FUNCTION_CALLING_MODELS, **DEEPSEEK_MODELS, } DISCONTINUED_MODELS: Dict[str, int] = {} def fireworks_modelname_to_contextsize(modelname: str) -> int: """ Calculate the maximum number of tokens possible to generate for a model. Args: modelname: The modelname we want to know the context size for. Returns: The maximum context size Example: .. code-block:: python max_tokens = fireworks_modelname_to_contextsize(model_name) """ # handling finetuned models # TO BE FILLED if modelname in DISCONTINUED_MODELS: raise ValueError( f"Fireworks hosted model {modelname} has been discontinued. " "Please choose another model." ) context_size = ALL_AVAILABLE_MODELS.get(modelname, None) if context_size is None: raise ValueError( f"Unknown model: {modelname}. Please provide a valid Fireworks model name." "Known models are: " + ", ".join(ALL_AVAILABLE_MODELS.keys()) ) return context_size def is_function_calling_model(model: str) -> bool: return "function" in model def _message_to_fireworks_prompt(message: ChatMessage) -> Dict[str, Any]: if message.role == MessageRole.USER: prompt = {"role": "user", "content": message.content} elif message.role == MessageRole.ASSISTANT: prompt = {"role": "assistant", "content": message.content} elif message.role == MessageRole.SYSTEM: prompt = {"role": "system", "content": message.content} elif message.role == MessageRole.FUNCTION: raise ValueError(f"Message role {MessageRole.FUNCTION} is not supported.") else: raise ValueError(f"Unknown message role: {message.role}") return prompt def messages_to_fireworks_prompt(messages: Sequence[ChatMessage]) -> List[Dict]: if len(messages) == 0: raise ValueError("Got empty list of messages.") return [_message_to_fireworks_prompt(message) for message in messages] def resolve_fireworks_credentials( api_key: Optional[str] = None, api_base: Optional[str] = None, api_version: Optional[str] = None, ) -> Tuple[Optional[str], str, str]: """ "Resolve OpenAI credentials. The order of precedence is: 1. param 2. env 3. openai module 4. default """ # resolve from param or env api_key = get_from_param_or_env("api_key", api_key, "FIREWORKS_API_KEY", "") api_base = get_from_param_or_env("api_base", api_base, "FIREWORKS_API_BASE", "") api_version = get_from_param_or_env( "api_version", api_version, "FIREWORKS_API_VERSION", "" ) # resolve from openai module or default final_api_key = api_key or "" final_api_base = api_base or DEFAULT_FIREWORKS_API_BASE final_api_version = api_version or DEFAULT_FIREWORKS_API_VERSION return final_api_key, str(final_api_base), final_api_version
""" Sphinx Read the Docs theme. From https://github.com/ryan-roemer/sphinx-bootstrap-theme. """ from os import path import sphinx __version__ = "0.5.0" __version_full__ = __version__ def get_html_theme_path(): """Return list of HTML theme paths.""" cur_dir = path.abspath(path.dirname(path.dirname(__file__))) return cur_dir # See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package def setup(app): if sphinx.version_info >= (1, 6, 0): # Register the theme that can be referenced without adding a theme path app.add_html_theme("sphinx_rtd_theme", path.abspath(path.dirname(__file__))) if sphinx.version_info >= (1, 8, 0): # Add Sphinx message catalog for newer versions of Sphinx # See http://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_message_catalog rtd_locale_path = path.join(path.abspath(path.dirname(__file__)), "locale") app.add_message_catalog("sphinx", rtd_locale_path) return {"parallel_read_safe": True, "parallel_write_safe": True}
""" Sphinx Read the Docs theme. From https://github.com/ryan-roemer/sphinx-bootstrap-theme. """ from os import path import sphinx __version__ = "0.5.0" __version_full__ = __version__ def get_html_theme_path(): """Return list of HTML theme paths.""" cur_dir = path.abspath(path.dirname(path.dirname(__file__))) return cur_dir # See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package def setup(app): if sphinx.version_info >= (1, 6, 0): # Register the theme that can be referenced without adding a theme path app.add_html_theme("sphinx_rtd_theme", path.abspath(path.dirname(__file__))) if sphinx.version_info >= (1, 8, 0): # Add Sphinx message catalog for newer versions of Sphinx # See http://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_message_catalog rtd_locale_path = path.join(path.abspath(path.dirname(__file__)), "locale") app.add_message_catalog("sphinx", rtd_locale_path) return {"parallel_read_safe": True, "parallel_write_safe": True}
"""Test chat model integration.""" import json from collections.abc import Generator from contextlib import contextmanager from typing import Any from unittest.mock import patch import pytest from httpx import Client, Request, Response from langchain_core.messages import ChatMessage from langchain_tests.unit_tests import ChatModelUnitTests from langchain_ollama.chat_models import ChatOllama, _parse_arguments_from_tool_call MODEL_NAME = "llama3.1" class TestChatOllama(ChatModelUnitTests): @property def chat_model_class(self) -> type[ChatOllama]: return ChatOllama @property def chat_model_params(self) -> dict: return {"model": "llama3-groq-tool-use"} def test__parse_arguments_from_tool_call() -> None: raw_response = '{"model":"sample-model","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_profile_details","arguments":{"arg_1":"12345678901234567890123456"}}}]},"done":false}' # noqa: E501 raw_tool_calls = json.loads(raw_response)["message"]["tool_calls"] response = _parse_arguments_from_tool_call(raw_tool_calls[0]) assert response is not None assert isinstance(response["arg_1"], str) @contextmanager def _mock_httpx_client_stream( *args: Any, **kwargs: Any ) -> Generator[Response, Any, Any]: yield Response( status_code=200, content='{"message": {"role": "assistant", "content": "The meaning ..."}}', request=Request(method="POST", url="http://whocares:11434"), ) def test_arbitrary_roles_accepted_in_chatmessages( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(Client, "stream", _mock_httpx_client_stream) llm = ChatOllama( base_url="http://whocares:11434", model=MODEL_NAME, verbose=True, format=None, ) messages = [ ChatMessage( role="somerandomrole", content="I'm ok with you adding any role message now!", ), ChatMessage(role="control", content="thinking"), ChatMessage(role="user", content="What is the meaning of life?"), ] llm.invoke(messages) @patch("langchain_ollama.chat_models.validate_model") def test_validate_model_on_init(mock_validate_model: Any) -> None: """Test that the model is validated on initialization when requested.""" # Test that validate_model is called when validate_model_on_init=True ChatOllama(model=MODEL_NAME, validate_model_on_init=True) mock_validate_model.assert_called_once() mock_validate_model.reset_mock() # Test that validate_model is NOT called when validate_model_on_init=False ChatOllama(model=MODEL_NAME, validate_model_on_init=False) mock_validate_model.assert_not_called() # Test that validate_model is NOT called by default ChatOllama(model=MODEL_NAME) mock_validate_model.assert_not_called()
"""Test chat model integration.""" import json from collections.abc import Generator from contextlib import contextmanager from typing import Any import pytest from httpx import Client, Request, Response from langchain_core.messages import ChatMessage from langchain_tests.unit_tests import ChatModelUnitTests from langchain_ollama.chat_models import ChatOllama, _parse_arguments_from_tool_call class TestChatOllama(ChatModelUnitTests): @property def chat_model_class(self) -> type[ChatOllama]: return ChatOllama @property def chat_model_params(self) -> dict: return {"model": "llama3-groq-tool-use"} def test__parse_arguments_from_tool_call() -> None: raw_response = '{"model":"sample-model","message":{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_profile_details","arguments":{"arg_1":"12345678901234567890123456"}}}]},"done":false}' # noqa: E501 raw_tool_calls = json.loads(raw_response)["message"]["tool_calls"] response = _parse_arguments_from_tool_call(raw_tool_calls[0]) assert response is not None assert isinstance(response["arg_1"], str) @contextmanager def _mock_httpx_client_stream( *args: Any, **kwargs: Any ) -> Generator[Response, Any, Any]: yield Response( status_code=200, content='{"message": {"role": "assistant", "content": "The meaning ..."}}', request=Request(method="POST", url="http://whocares:11434"), ) def test_arbitrary_roles_accepted_in_chatmessages( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(Client, "stream", _mock_httpx_client_stream) llm = ChatOllama( base_url="http://whocares:11434", model="granite3.2", verbose=True, format=None, ) messages = [ ChatMessage( role="somerandomrole", content="I'm ok with you adding any role message now!", ), ChatMessage(role="control", content="thinking"), ChatMessage(role="user", content="What is the meaning of life?"), ] llm.invoke(messages)
""" This examples trains a CrossEncoder for the NLI task. A CrossEncoder takes a sentence pair as input and outputs a label. Here, it learns to predict the labels: "contradiction": 0, "entailment": 1, "neutral": 2. It does NOT produce a sentence embedding and does NOT work for individual sentences. Usage: python training_nli.py """ import logging import traceback from datetime import datetime from datasets import load_dataset from sentence_transformers.cross_encoder import CrossEncoder from sentence_transformers.cross_encoder.evaluation import CEClassificationEvaluator from sentence_transformers.cross_encoder.losses.CrossEntropyLoss import CrossEntropyLoss from sentence_transformers.cross_encoder.trainer import CrossEncoderTrainer from sentence_transformers.cross_encoder.training_args import CrossEncoderTrainingArguments # Set the log level to INFO to get more information logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO) train_batch_size = 64 num_epochs = 1 output_dir = "output/training_ce_allnli-" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") # 1. Define our CrossEncoder model. We use distilroberta-base as the base model and set it up to predict 3 labels # You can also use other base models, like bert-base-uncased, microsoft/mpnet-base, etc. model_name = "distilroberta-base" model = CrossEncoder(model_name, num_labels=3) # 2. Load the AllNLI dataset: https://huggingface.co/datasets/sentence-transformers/all-nli # We'll start with 100k training samples, but you can increase this to get a stronger model logging.info("Read AllNLI train dataset") train_dataset = load_dataset("sentence-transformers/all-nli", "pair-class", split="train").select(range(100_000)) eval_dataset = load_dataset("sentence-transformers/all-nli", "pair-class", split="dev").select(range(1000)) test_dataset = load_dataset("sentence-transformers/all-nli", "pair-class", split="test") logging.info(train_dataset) # 3. Define our training loss: loss = CrossEntropyLoss(model) # 4. Before and during training, we use CEClassificationEvaluator to measure the performance on the dev set dev_cls_evaluator = CEClassificationEvaluator( sentence_pairs=list(zip(eval_dataset["premise"], eval_dataset["hypothesis"])), labels=eval_dataset["label"], name="AllNLI-dev", ) dev_cls_evaluator(model) # 5. Define the training arguments short_model_name = model_name if "/" not in model_name else model_name.split("/")[-1] run_name = f"reranker-{short_model_name}-nli" args = CrossEncoderTrainingArguments( # Required parameter: output_dir=output_dir, # Optional training parameters: num_train_epochs=num_epochs, per_device_train_batch_size=train_batch_size, per_device_eval_batch_size=train_batch_size, warmup_ratio=0.1, fp16=False, # Set to False if you get an error that your GPU can't run on FP16 bf16=True, # Set to True if you have a GPU that supports BF16 # Optional tracking/debugging parameters: eval_strategy="steps", eval_steps=500, save_strategy="steps", save_steps=500, save_total_limit=2, logging_steps=100, run_name=run_name, # Will be used in W&B if `wandb` is installed ) # 6. Create the trainer & start training trainer = CrossEncoderTrainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=eval_dataset, loss=loss, evaluator=dev_cls_evaluator, ) trainer.train() # 7. Evaluate the final model on test dataset test_cls_evaluator = CEClassificationEvaluator( list(zip(test_dataset["premise"], test_dataset["hypothesis"])), test_dataset["label"], name="AllNLI-test", ) test_cls_evaluator(model) # 8. Save the final model final_output_dir = f"{output_dir}/final" model.save_pretrained(final_output_dir) # 9. (Optional) save the model to the Hugging Face Hub! # It is recommended to run `huggingface-cli login` to log into your Hugging Face account first model_name = model_name if "/" not in model_name else model_name.split("/")[-1] try: model.push_to_hub(run_name) except Exception: logging.error( f"Error uploading model to the Hugging Face Hub:\n{traceback.format_exc()}To upload it manually, you can run " f"`huggingface-cli login`, followed by loading the model using `model = CrossEncoder({final_output_dir!r})` " f"and saving it using `model.push_to_hub('{run_name}')`." )
""" This examples trains a CrossEncoder for the NLI task. A CrossEncoder takes a sentence pair as input and outputs a label. Here, it learns to predict the labels: "contradiction": 0, "entailment": 1, "neutral": 2. It does NOT produce a sentence embedding and does NOT work for individual sentences. Usage: python training_nli.py """ import logging from datetime import datetime from datasets import load_dataset from sentence_transformers.cross_encoder import CrossEncoder from sentence_transformers.cross_encoder.evaluation import CEClassificationEvaluator from sentence_transformers.cross_encoder.losses.CrossEntropyLoss import CrossEntropyLoss from sentence_transformers.cross_encoder.trainer import CrossEncoderTrainer from sentence_transformers.cross_encoder.training_args import CrossEncoderTrainingArguments # Set the log level to INFO to get more information logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO) train_batch_size = 64 num_epochs = 1 output_dir = "output/training_ce_allnli-" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") # 1. Define our CrossEncoder model. We use distilroberta-base as basis and setup it up to predict 3 labels # You can also use other base models, like bert-base-uncased, microsoft/mpnet-base, etc. model = CrossEncoder("distilroberta-base", num_labels=3) # 2. Load the AllNLI dataset: https://huggingface.co/datasets/sentence-transformers/all-nli # We'll start with 10k training samples, but you can increase this to get a stronger model logging.info("Read AllNLI train dataset") train_dataset = load_dataset("sentence-transformers/all-nli", "pair-class", split="train").select(range(10000)) eval_dataset = load_dataset("sentence-transformers/all-nli", "pair-class", split="dev").select(range(1000)) test_dataset = load_dataset("sentence-transformers/all-nli", "pair-class", split="test") logging.info(train_dataset) # We might want to remap labels from the dataset, you can do that like so: mapping = {0: 1, 1: 2, 2: 0} eval_dataset = eval_dataset.map(lambda x: {"label": mapping[x["label"]]}) test_dataset = test_dataset.map(lambda x: {"label": mapping[x["label"]]}) # 3. Define our training loss: loss = CrossEntropyLoss(model) # During training, we use CEClassificationEvaluator to measure the performance on the dev set dev_cls_evaluator = CEClassificationEvaluator( list(zip(eval_dataset["premise"], eval_dataset["hypothesis"])), eval_dataset["label"], name="AllNLI-dev", ) dev_cls_evaluator(model) # 5. Define the training arguments args = CrossEncoderTrainingArguments( # Required parameter: output_dir=output_dir, # Optional training parameters: num_train_epochs=num_epochs, per_device_train_batch_size=train_batch_size, per_device_eval_batch_size=train_batch_size, warmup_ratio=0.1, fp16=False, # Set to False if you get an error that your GPU can't run on FP16 bf16=True, # Set to True if you have a GPU that supports BF16 # Optional tracking/debugging parameters: eval_strategy="steps", eval_steps=500, save_strategy="steps", save_steps=500, save_total_limit=2, logging_steps=100, run_name="ce-nli-v1", # Will be used in W&B if `wandb` is installed ) # 6. Create the trainer & start training trainer = CrossEncoderTrainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=eval_dataset, loss=loss, evaluator=dev_cls_evaluator, ) trainer.train() # 7. Evaluate the final model on test dataset test_cls_evaluator = CEClassificationEvaluator( list(zip(test_dataset["premise"], test_dataset["hypothesis"])), test_dataset["label"], name="AllNLI-test", ) test_cls_evaluator(model) # 8. Evaluate the final model and save it final_output_dir = f"{output_dir}/final" model.save_pretrained(final_output_dir)
__version__ = '0.13.8' import os from .document import Document from .array import DocumentArray from .dataclasses import dataclass, field if 'DA_NO_RICH_HANDLER' not in os.environ: from rich.traceback import install install()
__version__ = '0.13.7' import os from .document import Document from .array import DocumentArray from .dataclasses import dataclass, field if 'DA_NO_RICH_HANDLER' not in os.environ: from rich.traceback import install install()
import asyncio import copy from typing import Any, List, TYPE_CHECKING from jina.serve.runtimes.servers import BaseServer if TYPE_CHECKING: from jina.logging.logger import JinaLogger class CompositeBaseServer(BaseServer): """Composite Base Server implementation from which u can inherit a specific custom composite one""" servers: List['BaseServer'] logger: 'JinaLogger' def __init__( self, **kwargs, ): """Initialize the gateway :param kwargs: keyword args """ super().__init__(**kwargs) self._kwargs = kwargs @property def _server_kwargs(self): ret = [] # ignore monitoring and tracing args since they are not copyable ignored_attrs = [ 'metrics_registry', 'tracer_provider', 'grpc_tracing_server_interceptors', 'aio_tracing_client_interceptors', 'tracing_client_interceptor', ] for port, protocol in zip(self.ports, self.protocols): # ignore monitoring and tracing args since they are not copyable runtime_args = self._deepcopy_with_ignore_attrs( self.runtime_args, ignored_attrs ) runtime_args.port = port runtime_args.protocol = protocol server_kwargs = {k: v for k, v in self._kwargs.items() if k != 'runtime_args'} server_kwargs['runtime_args'] = dict(vars(runtime_args)) server_kwargs['req_handler'] = self._request_handler ret.append(server_kwargs) return ret @staticmethod def _deepcopy_with_ignore_attrs(obj: Any, ignore_attrs: List[str]) -> Any: """Deep copy an object and ignore some attributes :param obj: the object to copy :param ignore_attrs: the attributes to ignore :return: the copied object """ memo = {} for k in ignore_attrs: if hasattr(obj, k): memo[id(getattr(obj, k))] = None # getattr(obj, k) return copy.deepcopy(obj, memo) async def setup_server(self): """ setup servers inside CompositeServer """ self.logger.debug(f'Setting up Composite server') tasks = [] for server in self.servers: tasks.append(asyncio.create_task(server.setup_server())) await asyncio.gather(*tasks) self.logger.debug(f'Composite server setup successful') async def shutdown(self): """Free other resources allocated with the server, e.g, gateway object, ...""" self.logger.debug(f'Shutting down server') await super().shutdown() shutdown_tasks = [] for server in self.servers: shutdown_tasks.append(asyncio.create_task(server.shutdown())) await asyncio.gather(*shutdown_tasks) self.logger.debug(f'Server shutdown finished') async def run_server(self): """Run servers inside CompositeServer forever""" run_server_tasks = [] for server in self.servers: run_server_tasks.append(asyncio.create_task(server.run_server())) await asyncio.gather(*run_server_tasks) @property def _should_exit(self) -> bool: should_exit_values = [ getattr(server, 'should_exit', True) for server in self.servers ] return all(should_exit_values) class CompositeServer(CompositeBaseServer): """Composite Server implementation""" def __init__( self, **kwargs, ): """Initialize the gateway :param kwargs: keyword args """ super().__init__(**kwargs) from jina.parsers.helper import _get_gateway_class self.servers: List[BaseServer] = [] for server_kwargs in self._server_kwargs: server_cls = _get_gateway_class(server_kwargs['runtime_args']['protocol'], works_as_load_balancer=self.works_as_load_balancer) server = server_cls(**server_kwargs) self.servers.append(server) self.gateways = self.servers # for backwards compatibility
import asyncio import copy from typing import Any, List, TYPE_CHECKING from jina.serve.runtimes.servers import BaseServer if TYPE_CHECKING: from jina.logging.logger import JinaLogger class CompositeBaseServer(BaseServer): """Composite Base Server implementation from which u can inherit a specific custom composite one""" servers: List['BaseServer'] logger: 'JinaLogger' def __init__( self, **kwargs, ): """Initialize the gateway :param kwargs: keyword args """ super().__init__(**kwargs) self._kwargs = kwargs @property def _server_kwargs(self): ret = [] # ignore monitoring and tracing args since they are not copyable ignored_attrs = [ 'metrics_registry', 'tracer_provider', 'grpc_tracing_server_interceptors', 'aio_tracing_client_interceptors', 'tracing_client_interceptor', ] for port, protocol in zip(self.ports, self.protocols): # ignore monitoring and tracing args since they are not copyable runtime_args = self._deepcopy_with_ignore_attrs( self.runtime_args, ignored_attrs ) runtime_args.port = port runtime_args.protocol = protocol server_kwargs = {k: v for k, v in self._kwargs.items() if k != 'runtime_args'} server_kwargs['runtime_args'] = dict(vars(runtime_args)) server_kwargs['req_handler'] = self._request_handler ret.append(server_kwargs) return ret async def setup_server(self): """ setup servers inside CompositeServer """ self.logger.debug(f'Setting up Composite server') tasks = [] for server in self.servers: tasks.append(asyncio.create_task(server.setup_server())) await asyncio.gather(*tasks) self.logger.debug(f'Composite server setup successful') async def shutdown(self): """Free other resources allocated with the server, e.g, gateway object, ...""" self.logger.debug(f'Shutting down server') await super().shutdown() shutdown_tasks = [] for server in self.servers: shutdown_tasks.append(asyncio.create_task(server.shutdown())) await asyncio.gather(*shutdown_tasks) self.logger.debug(f'Server shutdown finished') async def run_server(self): """Run servers inside CompositeServer forever""" run_server_tasks = [] for server in self.servers: run_server_tasks.append(asyncio.create_task(server.run_server())) await asyncio.gather(*run_server_tasks) @property def _should_exit(self) -> bool: should_exit_values = [ getattr(server, 'should_exit', True) for server in self.servers ] return all(should_exit_values) class CompositeServer(CompositeBaseServer): """Composite Server implementation""" def __init__( self, **kwargs, ): """Initialize the gateway :param kwargs: keyword args """ super().__init__(**kwargs) from jina.parsers.helper import _get_gateway_class self.servers: List[BaseServer] = [] for server_kwargs in self._server_kwargs: server_cls = _get_gateway_class(server_kwargs['runtime_args']['protocol'], works_as_load_balancer=self.works_as_load_balancer) server = server_cls(**server_kwargs) self.servers.append(server) self.gateways = self.servers # for backwards compatibility @staticmethod def _deepcopy_with_ignore_attrs(obj: Any, ignore_attrs: List[str]) -> Any: """Deep copy an object and ignore some attributes :param obj: the object to copy :param ignore_attrs: the attributes to ignore :return: the copied object """ memo = {} for k in ignore_attrs: if hasattr(obj, k): memo[id(getattr(obj, k))] = None # getattr(obj, k) return copy.deepcopy(obj, memo)
from pathlib import Path import numpy as np import scipy from jina import Document, DocumentArray, Executor from ...tfidf_text_executor import TFIDFTextEncoder def test_config(): ex = Executor.load_config(str(Path(__file__).parents[2] / 'config.yml')) assert ex.path_vectorizer.endswith('tfidf_vectorizer.pickle') def test_tfidf_text_encoder(): text = 'Han likes eating pizza' encoder = TFIDFTextEncoder() doc = Document(text=text) docarray = DocumentArray([doc]) encoder.encode(docarray, parameters={}) embedding = doc.embedding expected = scipy.sparse.load_npz(Path(__file__).parent / 'expected.npz') np.testing.assert_almost_equal(embedding.todense(), expected.todense(), decimal=4) assert expected.shape[0] == 1 def test_tfidf_text_encoder_batch(): # Input text_batch = ['Han likes eating pizza', 'Han likes pizza', 'Jina rocks'] # Encoder embedding encoder = TFIDFTextEncoder() doc0 = Document(text=text_batch[0]) doc1 = Document(text=text_batch[1]) doc2 = Document(text=text_batch[2]) docarray = DocumentArray([doc0, doc1, doc2]) encoder.encode(docarray, parameters={}) embeddeding_batch = scipy.sparse.vstack(docarray.get_attributes('embedding')) # Compare with ouptut expected_batch = scipy.sparse.load_npz(Path(__file__).parent / 'expected_batch.npz') np.testing.assert_almost_equal( embeddeding_batch.todense(), expected_batch.todense(), decimal=2 ) assert expected_batch.shape[0] == len(text_batch)
import os import numpy as np import scipy from jina import Executor, Document, DocumentArray from ...tfidf_text_executor import TFIDFTextEncoder cur_dir = os.path.dirname(os.path.abspath(__file__)) def test_tfidf(): encoder = Executor.load_config(os.path.join(cur_dir, '../../config.yml')) assert encoder.path_vectorizer.endswith('tfidf_vectorizer.pickle') def test_tfidf_text_encoder(): text = 'Han likes eating pizza' encoder = TFIDFTextEncoder() doc = Document(text=text) docarray = DocumentArray([doc]) encoder.encode(docarray, parameters={}) embedding = doc.embedding expected = scipy.sparse.load_npz(os.path.join(cur_dir, 'expected.npz')) np.testing.assert_almost_equal(embedding.todense(), expected.todense(), decimal=4) assert expected.shape[0] == 1 def test_tfidf_text_encoder_batch(): # Input text_batch = ['Han likes eating pizza', 'Han likes pizza', 'Jina rocks'] # Encoder embedding encoder = TFIDFTextEncoder() doc0 = Document(text=text_batch[0]) doc1 = Document(text=text_batch[1]) doc2 = Document(text=text_batch[2]) docarray = DocumentArray([doc0, doc1, doc2]) encoder.encode(docarray, parameters={}) embeddeding_batch = scipy.sparse.vstack(docarray.get_attributes('embedding')) # Compare with ouptut expected_batch = scipy.sparse.load_npz(os.path.join(cur_dir, 'expected_batch.npz')) np.testing.assert_almost_equal( embeddeding_batch.todense(), expected_batch.todense(), decimal=2 ) assert expected_batch.shape[0] == len(text_batch)
import contextlib import logging import typing import fastapi import fastapi.responses import starlette.middleware.cors import uvicorn from autogpt_libs.feature_flag.client import ( initialize_launchdarkly, shutdown_launchdarkly, ) import backend.data.block import backend.data.db import backend.data.graph import backend.data.user import backend.server.routers.v1 import backend.util.service import backend.util.settings settings = backend.util.settings.Settings() logger = logging.getLogger(__name__) logging.getLogger("autogpt_libs").setLevel(logging.INFO) @contextlib.contextmanager def launch_darkly_context(): if settings.config.app_env != backend.util.settings.AppEnvironment.LOCAL: initialize_launchdarkly() try: yield finally: shutdown_launchdarkly() else: yield @contextlib.asynccontextmanager async def lifespan_context(app: fastapi.FastAPI): await backend.data.db.connect() await backend.data.block.initialize_blocks() await backend.data.user.migrate_and_encrypt_user_integrations() await backend.data.graph.fix_llm_provider_credentials() with launch_darkly_context(): yield await backend.data.db.disconnect() docs_url = ( "/docs" if settings.config.app_env == backend.util.settings.AppEnvironment.LOCAL else None ) app = fastapi.FastAPI( title="AutoGPT Agent Server", description=( "This server is used to execute agents that are created by the " "AutoGPT system." ), summary="AutoGPT Agent Server", version="0.1", lifespan=lifespan_context, docs_url=docs_url, ) def handle_internal_http_error(status_code: int = 500, log_error: bool = True): def handler(request: fastapi.Request, exc: Exception): if log_error: logger.exception(f"{request.method} {request.url.path} failed: {exc}") return fastapi.responses.JSONResponse( content={ "message": f"{request.method} {request.url.path} failed", "detail": str(exc), }, status_code=status_code, ) return handler app.add_exception_handler(ValueError, handle_internal_http_error(400)) app.add_exception_handler(Exception, handle_internal_http_error(500)) app.include_router(backend.server.routers.v1.v1_router, tags=["v1"]) @app.get(path="/health", tags=["health"], dependencies=[]) async def health(): return {"status": "healthy"} class AgentServer(backend.util.service.AppProcess): def run(self): server_app = starlette.middleware.cors.CORSMiddleware( app=app, allow_origins=settings.config.backend_cors_allow_origins, allow_credentials=True, allow_methods=["*"], # Allows all methods allow_headers=["*"], # Allows all headers ) uvicorn.run( server_app, host=backend.util.settings.Config().agent_api_host, port=backend.util.settings.Config().agent_api_port, ) @staticmethod async def test_execute_graph( graph_id: str, node_input: dict[typing.Any, typing.Any], user_id: str ): return backend.server.routers.v1.execute_graph(graph_id, node_input, user_id) @staticmethod async def test_create_graph( create_graph: backend.server.routers.v1.CreateGraph, user_id: str, ): return await backend.server.routers.v1.create_new_graph(create_graph, user_id) @staticmethod async def test_get_graph_run_status(graph_exec_id: str, user_id: str): execution = await backend.data.graph.get_execution( user_id=user_id, execution_id=graph_exec_id ) if not execution: raise ValueError(f"Execution {graph_exec_id} not found") return execution.status @staticmethod async def test_get_graph_run_node_execution_results( graph_id: str, graph_exec_id: str, user_id: str ): return await backend.server.routers.v1.get_graph_run_node_execution_results( graph_id, graph_exec_id, user_id ) @staticmethod async def test_delete_graph(graph_id: str, user_id: str): return await backend.server.routers.v1.delete_graph(graph_id, user_id) def set_test_dependency_overrides(self, overrides: dict): app.dependency_overrides.update(overrides)
import contextlib import logging import typing import fastapi import fastapi.responses import starlette.middleware.cors import uvicorn from autogpt_libs.feature_flag.client import ( initialize_launchdarkly, shutdown_launchdarkly, ) import backend.data.block import backend.data.db import backend.data.graph import backend.data.user import backend.server.routers.v1 import backend.util.service import backend.util.settings settings = backend.util.settings.Settings() logger = logging.getLogger(__name__) logging.getLogger("autogpt_libs").setLevel(logging.INFO) @contextlib.contextmanager def launch_darkly_context(): if settings.config.app_env != backend.util.settings.AppEnvironment.LOCAL: initialize_launchdarkly() try: yield finally: shutdown_launchdarkly() else: yield @contextlib.asynccontextmanager async def lifespan_context(app: fastapi.FastAPI): await backend.data.db.connect() await backend.data.block.initialize_blocks() await backend.data.user.migrate_and_encrypt_user_integrations() await backend.data.graph.fix_llm_provider_credentials() with launch_darkly_context(): yield await backend.data.db.disconnect() docs_url = ( "/docs" if settings.config.app_env == backend.util.settings.AppEnvironment.LOCAL else None ) app = fastapi.FastAPI( title="AutoGPT Agent Server", description=( "This server is used to execute agents that are created by the " "AutoGPT system." ), summary="AutoGPT Agent Server", version="0.1", lifespan=lifespan_context, docs_url=docs_url, ) def handle_internal_http_error(status_code: int = 500, log_error: bool = True): def handler(request: fastapi.Request, exc: Exception): if log_error: logger.exception(f"{request.method} {request.url.path} failed: {exc}") return fastapi.responses.JSONResponse( content={ "message": f"{request.method} {request.url.path} failed", "detail": str(exc), }, status_code=status_code, ) return handler app.add_exception_handler(ValueError, handle_internal_http_error(400)) app.add_exception_handler(Exception, handle_internal_http_error(500)) app.include_router(backend.server.routers.v1.v1_router, tags=["v1"]) @app.get(path="/health", tags=["health"], dependencies=[]) async def health(): return {"status": "healthy"} class AgentServer(backend.util.service.AppProcess): def run(self): server_app = starlette.middleware.cors.CORSMiddleware( app=app, allow_origins=settings.config.backend_cors_allow_origins, allow_credentials=True, allow_methods=["*"], # Allows all methods allow_headers=["*"], # Allows all headers ) uvicorn.run( server_app, host=backend.util.settings.Config().agent_api_host, port=backend.util.settings.Config().agent_api_port, ) @staticmethod async def test_execute_graph( graph_id: str, node_input: dict[typing.Any, typing.Any], user_id: str ): return backend.server.routers.v1.execute_graph(graph_id, node_input, user_id) @staticmethod async def test_create_graph( create_graph: backend.server.routers.v1.CreateGraph, user_id: str, is_template=False, ): return await backend.server.routers.v1.create_new_graph(create_graph, user_id) @staticmethod async def test_get_graph_run_status( graph_id: str, graph_exec_id: str, user_id: str ): return await backend.server.routers.v1.get_graph_run_status( graph_id, graph_exec_id, user_id ) @staticmethod async def test_get_graph_run_node_execution_results( graph_id: str, graph_exec_id: str, user_id: str ): return await backend.server.routers.v1.get_graph_run_node_execution_results( graph_id, graph_exec_id, user_id ) @staticmethod async def test_delete_graph(graph_id: str, user_id: str): return await backend.server.routers.v1.delete_graph(graph_id, user_id) def set_test_dependency_overrides(self, overrides: dict): app.dependency_overrides.update(overrides)
import numpy as np from pydantic.tools import parse_obj_as, schema_json_of from docarray.document.io.json import orjson_dumps from docarray.typing import AnyEmbedding def test_proto_embedding(): embedding = parse_obj_as(AnyEmbedding, np.zeros((3, 224, 224))) embedding._to_node_protobuf() def test_json_schema(): schema_json_of(AnyEmbedding) def test_dump_json(): tensor = parse_obj_as(AnyEmbedding, np.zeros((3, 224, 224))) orjson_dumps(tensor)
import numpy as np from pydantic.tools import parse_obj_as, schema_json_of from docarray.document.io.json import orjson_dumps from docarray.typing import Embedding def test_proto_embedding(): embedding = parse_obj_as(Embedding, np.zeros((3, 224, 224))) embedding._to_node_protobuf() def test_json_schema(): schema_json_of(Embedding) def test_dump_json(): tensor = parse_obj_as(Embedding, np.zeros((3, 224, 224))) orjson_dumps(tensor)
from abc import ABC, abstractmethod from typing import Callable, List, Sequence, Optional, Union, Any from llama_index.core.agent.workflow.workflow_events import ( AgentOutput, ToolCallResult, ) from llama_index.core.bridge.pydantic import ( BaseModel, Field, ConfigDict, field_validator, ) from llama_index.core.llms import ChatMessage, LLM from llama_index.core.memory import BaseMemory from llama_index.core.prompts.mixin import PromptMixin, PromptMixinType, PromptDictType from llama_index.core.tools import BaseTool, AsyncBaseTool, FunctionTool from llama_index.core.workflow import Context from llama_index.core.objects import ObjectRetriever from llama_index.core.settings import Settings from llama_index.core.workflow.checkpointer import CheckpointCallback from llama_index.core.workflow.handler import WorkflowHandler def get_default_llm() -> LLM: return Settings.llm class BaseWorkflowAgent(BaseModel, PromptMixin, ABC): """Base class for all agents, combining config and logic.""" model_config = ConfigDict(arbitrary_types_allowed=True) name: str = Field(description="The name of the agent") description: str = Field( description="The description of what the agent does and is responsible for" ) system_prompt: Optional[str] = Field( default=None, description="The system prompt for the agent" ) tools: Optional[List[Union[BaseTool, Callable]]] = Field( default=None, description="The tools that the agent can use" ) tool_retriever: Optional[ObjectRetriever] = Field( default=None, description="The tool retriever for the agent, can be provided instead of tools", ) can_handoff_to: Optional[List[str]] = Field( default=None, description="The agent names that this agent can hand off to" ) llm: LLM = Field( default_factory=get_default_llm, description="The LLM that the agent uses" ) @field_validator("tools", mode="before") def validate_tools( cls, v: Optional[Sequence[Union[BaseTool, Callable]]] ) -> Optional[Sequence[BaseTool]]: """Validate tools. If tools are not of type BaseTool, they will be converted to FunctionTools. This assumes the inputs are tools or callable functions. """ if v is None: return None validated_tools: List[BaseTool] = [] for tool in v: if not isinstance(tool, BaseTool): validated_tools.append(FunctionTool.from_defaults(tool)) else: validated_tools.append(tool) for tool in validated_tools: if tool.metadata.name == "handoff": raise ValueError( "'handoff' is a reserved tool name. Please use a different name." ) return validated_tools # type: ignore[return-value] def _get_prompts(self) -> PromptDictType: """Get prompts.""" return {} def _get_prompt_modules(self) -> PromptMixinType: """Get prompt sub-modules.""" return {} def _update_prompts(self, prompts_dict: PromptDictType) -> None: """Update prompts.""" @abstractmethod async def take_step( self, ctx: Context, llm_input: List[ChatMessage], tools: Sequence[AsyncBaseTool], memory: BaseMemory, ) -> AgentOutput: """Take a single step with the agent.""" @abstractmethod async def handle_tool_call_results( self, ctx: Context, results: List[ToolCallResult], memory: BaseMemory ) -> None: """Handle tool call results.""" @abstractmethod async def finalize( self, ctx: Context, output: AgentOutput, memory: BaseMemory ) -> AgentOutput: """Finalize the agent's execution.""" @abstractmethod def run( self, user_msg: Optional[Union[str, ChatMessage]] = None, chat_history: Optional[List[ChatMessage]] = None, memory: Optional[BaseMemory] = None, ctx: Optional[Context] = None, stepwise: bool = False, checkpoint_callback: Optional[CheckpointCallback] = None, **workflow_kwargs: Any, ) -> WorkflowHandler: """Run the agent."""
from abc import ABC, abstractmethod from typing import Callable, List, Sequence, Optional, Union, Any from llama_index.core.agent.workflow.workflow_events import ( AgentOutput, ToolCallResult, ) from llama_index.core.bridge.pydantic import ( BaseModel, Field, ConfigDict, field_validator, ) from llama_index.core.llms import ChatMessage, LLM from llama_index.core.memory import BaseMemory from llama_index.core.prompts.mixin import PromptMixin, PromptMixinType, PromptDictType from llama_index.core.tools import BaseTool, AsyncBaseTool, FunctionTool from llama_index.core.workflow import Context from llama_index.core.objects import ObjectRetriever from llama_index.core.settings import Settings from llama_index.core.workflow.checkpointer import CheckpointCallback from llama_index.core.workflow.handler import WorkflowHandler def get_default_llm() -> LLM: return Settings.llm class BaseWorkflowAgent(BaseModel, PromptMixin, ABC): """Base class for all agents, combining config and logic.""" model_config = ConfigDict(arbitrary_types_allowed=True) name: str = Field(description="The name of the agent") description: str = Field( description="The description of what the agent does and is responsible for" ) system_prompt: Optional[str] = Field( default=None, description="The system prompt for the agent" ) tools: Optional[List[BaseTool]] = Field( default=None, description="The tools that the agent can use" ) tool_retriever: Optional[ObjectRetriever] = Field( default=None, description="The tool retriever for the agent, can be provided instead of tools", ) can_handoff_to: Optional[List[str]] = Field( default=None, description="The agent names that this agent can hand off to" ) llm: LLM = Field( default_factory=get_default_llm, description="The LLM that the agent uses" ) @field_validator("tools", mode="before") def validate_tools( cls, v: Optional[Sequence[Union[BaseTool, Callable]]] ) -> Optional[Sequence[BaseTool]]: """Validate tools. If tools are not of type BaseTool, they will be converted to FunctionTools. This assumes the inputs are tools or callable functions. """ if v is None: return None validated_tools: List[BaseTool] = [] for tool in v: if not isinstance(tool, BaseTool): validated_tools.append(FunctionTool.from_defaults(tool)) else: validated_tools.append(tool) for tool in validated_tools: if tool.metadata.name == "handoff": raise ValueError( "'handoff' is a reserved tool name. Please use a different name." ) return validated_tools # type: ignore[return-value] def _get_prompts(self) -> PromptDictType: """Get prompts.""" return {} def _get_prompt_modules(self) -> PromptMixinType: """Get prompt sub-modules.""" return {} def _update_prompts(self, prompts_dict: PromptDictType) -> None: """Update prompts.""" @abstractmethod async def take_step( self, ctx: Context, llm_input: List[ChatMessage], tools: Sequence[AsyncBaseTool], memory: BaseMemory, ) -> AgentOutput: """Take a single step with the agent.""" @abstractmethod async def handle_tool_call_results( self, ctx: Context, results: List[ToolCallResult], memory: BaseMemory ) -> None: """Handle tool call results.""" @abstractmethod async def finalize( self, ctx: Context, output: AgentOutput, memory: BaseMemory ) -> AgentOutput: """Finalize the agent's execution.""" @abstractmethod def run( self, user_msg: Optional[Union[str, ChatMessage]] = None, chat_history: Optional[List[ChatMessage]] = None, memory: Optional[BaseMemory] = None, ctx: Optional[Context] = None, stepwise: bool = False, checkpoint_callback: Optional[CheckpointCallback] = None, **workflow_kwargs: Any, ) -> WorkflowHandler: """Run the agent."""
# Copyright (c) OpenMMLab. All rights reserved. from .dist_utils import (DistOptimizerHook, all_reduce_dict, allreduce_grads, reduce_mean, sync_random_seed) from .misc import (center_of_mass, filter_scores_and_topk, flip_tensor, generate_coordinate, mask2ndarray, multi_apply, select_single_mlvl, stack_batch, unmap) __all__ = [ 'allreduce_grads', 'DistOptimizerHook', 'reduce_mean', 'multi_apply', 'unmap', 'mask2ndarray', 'flip_tensor', 'all_reduce_dict', 'center_of_mass', 'generate_coordinate', 'select_single_mlvl', 'filter_scores_and_topk', 'stack_batch' ]
# Copyright (c) OpenMMLab. All rights reserved. from .dist_utils import (DistOptimizerHook, all_reduce_dict, allreduce_grads, reduce_mean, sync_random_seed) from .misc import (center_of_mass, filter_scores_and_topk, flip_tensor, generate_coordinate, mask2ndarray, multi_apply, select_single_mlvl, unmap) __all__ = [ 'allreduce_grads', 'DistOptimizerHook', 'reduce_mean', 'multi_apply', 'unmap', 'mask2ndarray', 'flip_tensor', 'all_reduce_dict', 'center_of_mass', 'generate_coordinate', 'select_single_mlvl', 'filter_scores_and_topk', 'sync_random_seed' ]
# Copyright (c) OpenMMLab. All rights reserved. from .base_data_element import BaseDataElement from .instance_data import InstanceData from .sampler import DefaultSampler, InfiniteSampler from .utils import pseudo_collate, worker_init_fn __all__ = [ 'BaseDataElement', 'DefaultSampler', 'InfiniteSampler', 'worker_init_fn', 'pseudo_collate', 'InstanceData' ]
# Copyright (c) OpenMMLab. All rights reserved. from .base_data_element import BaseDataElement from .sampler import DefaultSampler, InfiniteSampler from .utils import pseudo_collate, worker_init_fn __all__ = [ 'BaseDataElement', 'DefaultSampler', 'InfiniteSampler', 'worker_init_fn', 'pseudo_collate' ]
# Copyright (c) OpenMMLab. All rights reserved. from .ade20k import (ADE20KInstanceDataset, ADE20KPanopticDataset, ADE20KSegDataset) from .base_det_dataset import BaseDetDataset from .base_semseg_dataset import BaseSegDataset from .base_video_dataset import BaseVideoDataset from .cityscapes import CityscapesDataset from .coco import CocoDataset from .coco_caption import CocoCaptionDataset from .coco_panoptic import CocoPanopticDataset from .coco_semantic import CocoSegDataset from .crowdhuman import CrowdHumanDataset from .dataset_wrappers import ConcatDataset, MultiImageMixDataset from .deepfashion import DeepFashionDataset from .dod import DODDataset from .dsdl import DSDLDetDataset from .flickr30k import Flickr30kDataset from .isaid import iSAIDDataset from .lvis import LVISDataset, LVISV1Dataset, LVISV05Dataset from .mdetr_style_refcoco import MDETRStyleRefCocoDataset from .mot_challenge_dataset import MOTChallengeDataset from .objects365 import Objects365V1Dataset, Objects365V2Dataset from .odvg import ODVGDataset from .openimages import OpenImagesChallengeDataset, OpenImagesDataset from .refcoco import RefCocoDataset from .reid_dataset import ReIDDataset from .samplers import (AspectRatioBatchSampler, ClassAwareSampler, CustomSampleSizeSampler, GroupMultiSourceSampler, MultiSourceSampler, TrackAspectRatioBatchSampler, TrackImgSampler) from .utils import get_loading_pipeline from .v3det import V3DetDataset from .voc import VOCDataset from .wider_face import WIDERFaceDataset from .xml_style import XMLDataset from .youtube_vis_dataset import YouTubeVISDataset __all__ = [ 'XMLDataset', 'CocoDataset', 'DeepFashionDataset', 'VOCDataset', 'CityscapesDataset', 'LVISDataset', 'LVISV05Dataset', 'LVISV1Dataset', 'WIDERFaceDataset', 'get_loading_pipeline', 'CocoPanopticDataset', 'MultiImageMixDataset', 'OpenImagesDataset', 'OpenImagesChallengeDataset', 'AspectRatioBatchSampler', 'ClassAwareSampler', 'MultiSourceSampler', 'GroupMultiSourceSampler', 'BaseDetDataset', 'CrowdHumanDataset', 'Objects365V1Dataset', 'Objects365V2Dataset', 'DSDLDetDataset', 'BaseVideoDataset', 'MOTChallengeDataset', 'TrackImgSampler', 'ReIDDataset', 'YouTubeVISDataset', 'TrackAspectRatioBatchSampler', 'ADE20KPanopticDataset', 'CocoCaptionDataset', 'RefCocoDataset', 'BaseSegDataset', 'ADE20KSegDataset', 'CocoSegDataset', 'ADE20KInstanceDataset', 'iSAIDDataset', 'V3DetDataset', 'ConcatDataset', 'ODVGDataset', 'MDETRStyleRefCocoDataset', 'DODDataset', 'CustomSampleSizeSampler', 'Flickr30kDataset' ]
# Copyright (c) OpenMMLab. All rights reserved. from .ade20k import (ADE20KInstanceDataset, ADE20KPanopticDataset, ADE20KSegDataset) from .base_det_dataset import BaseDetDataset from .base_semseg_dataset import BaseSegDataset from .base_video_dataset import BaseVideoDataset from .cityscapes import CityscapesDataset from .coco import CocoDataset from .coco_caption import CocoCaptionDataset from .coco_panoptic import CocoPanopticDataset from .coco_semantic import CocoSegDataset from .crowdhuman import CrowdHumanDataset from .dataset_wrappers import ConcatDataset, MultiImageMixDataset from .deepfashion import DeepFashionDataset from .dsdl import DSDLDetDataset from .isaid import iSAIDDataset from .lvis import LVISDataset, LVISV1Dataset, LVISV05Dataset from .mot_challenge_dataset import MOTChallengeDataset from .objects365 import Objects365V1Dataset, Objects365V2Dataset from .openimages import OpenImagesChallengeDataset, OpenImagesDataset from .refcoco import RefCocoDataset from .reid_dataset import ReIDDataset from .samplers import (AspectRatioBatchSampler, ClassAwareSampler, GroupMultiSourceSampler, MultiSourceSampler, TrackAspectRatioBatchSampler, TrackImgSampler) from .utils import get_loading_pipeline from .v3det import V3DetDataset from .voc import VOCDataset from .wider_face import WIDERFaceDataset from .xml_style import XMLDataset from .youtube_vis_dataset import YouTubeVISDataset __all__ = [ 'XMLDataset', 'CocoDataset', 'DeepFashionDataset', 'VOCDataset', 'CityscapesDataset', 'LVISDataset', 'LVISV05Dataset', 'LVISV1Dataset', 'WIDERFaceDataset', 'get_loading_pipeline', 'CocoPanopticDataset', 'MultiImageMixDataset', 'OpenImagesDataset', 'OpenImagesChallengeDataset', 'AspectRatioBatchSampler', 'ClassAwareSampler', 'MultiSourceSampler', 'GroupMultiSourceSampler', 'BaseDetDataset', 'CrowdHumanDataset', 'Objects365V1Dataset', 'Objects365V2Dataset', 'DSDLDetDataset', 'BaseVideoDataset', 'MOTChallengeDataset', 'TrackImgSampler', 'ReIDDataset', 'YouTubeVISDataset', 'TrackAspectRatioBatchSampler', 'ADE20KPanopticDataset', 'CocoCaptionDataset', 'RefCocoDataset', 'BaseSegDataset', 'ADE20KSegDataset', 'CocoSegDataset', 'ADE20KInstanceDataset', 'iSAIDDataset', 'V3DetDataset', 'ConcatDataset' ]
from tempfile import NamedTemporaryFile import huggingface_hub import pytest import requests from packaging import version from datasets.utils.file_utils import fsspec_get, fsspec_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline, require_not_windows @pytest.mark.integration @require_not_windows # fsspec get keeps a file handle on windows that raises PermissionError def test_offline_with_timeout(): with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT): with pytest.raises(RequestWouldHangIndefinitelyError): requests.request("GET", "https://huggingface.co") with pytest.raises(requests.exceptions.ConnectTimeout): requests.request("GET", "https://huggingface.co", timeout=1.0) # old versions of `huggingface_hub` don't have timeouts by default and don't allow to set timeouts in HfFileSystem if version.parse(huggingface_hub.__version__) >= version.parse("0.23.0"): with pytest.raises(requests.exceptions.ConnectTimeout), NamedTemporaryFile() as temp_file: fsspec_get("hf://dummy", temp_file=temp_file) @pytest.mark.integration @require_not_windows # fsspec get keeps a file handle on windows that raises PermissionError def test_offline_with_connection_error(): with offline(OfflineSimulationMode.CONNECTION_FAILS): with pytest.raises(requests.exceptions.ConnectionError): requests.request("GET", "https://huggingface.co") with pytest.raises(requests.exceptions.ConnectionError), NamedTemporaryFile() as temp_file: fsspec_get("hf://dummy", temp_file=temp_file) def test_offline_with_datasets_offline_mode_enabled(): with offline(OfflineSimulationMode.HF_HUB_OFFLINE_SET_TO_1): with pytest.raises(ConnectionError): fsspec_head("hf://dummy") with pytest.raises(ConnectionError), NamedTemporaryFile() as temp_file: fsspec_get("hf://dummy", temp_file=temp_file)
import pytest import requests from datasets.utils.file_utils import http_head from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline @pytest.mark.integration def test_offline_with_timeout(): with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT): with pytest.raises(RequestWouldHangIndefinitelyError): requests.request("GET", "https://huggingface.co") with pytest.raises(requests.exceptions.ConnectTimeout): requests.request("GET", "https://huggingface.co", timeout=1.0) @pytest.mark.integration def test_offline_with_connection_error(): with offline(OfflineSimulationMode.CONNECTION_FAILS): with pytest.raises(requests.exceptions.ConnectionError): requests.request("GET", "https://huggingface.co") def test_offline_with_datasets_offline_mode_enabled(): with offline(OfflineSimulationMode.HF_HUB_OFFLINE_SET_TO_1): with pytest.raises(ConnectionError): http_head("https://huggingface.co")
""" Computes embeddings """ import numpy as np from sentence_transformers import SentenceTransformer def test_encode_token_embeddings(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: """ Test that encode(output_value='token_embeddings') works """ model = paraphrase_distilroberta_base_v1_model sent = [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", "Sentences", "Sentence five five five five five five five", ] emb = model.encode(sent, output_value="token_embeddings", batch_size=2) assert len(emb) == len(sent) for s, e in zip(sent, emb): assert len(model.tokenize([s])["input_ids"][0]) == e.shape[0] def test_encode_single_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Single sentence emb = model.encode("Hello Word, a test sentence") assert emb.shape == (768,) assert abs(np.sum(emb) - 7.9811716) < 0.002 # Single sentence as list emb = model.encode(["Hello Word, a test sentence"]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 7.9811716) < 0.002 # Sentence list emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 22.968266) < 0.007 def test_encode_normalize(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ], normalize_embeddings=True, ) assert emb.shape == (3, 768) for norm in np.linalg.norm(emb, axis=1): assert abs(norm - 1) < 0.001 def test_encode_tuple_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Input a sentence tuple emb = model.encode([("Hello Word, a test sentence", "Second input for model")]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 9.503508) < 0.002 # List of sentence tuples emb = model.encode( [ ("Hello Word, a test sentence", "Second input for model"), ("My second tuple", "With two inputs"), ("Final tuple", "final test"), ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 32.14627) < 0.002
""" Computes embeddings """ import numpy as np from sentence_transformers import SentenceTransformer def test_encode_token_embeddings(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: """ Test that encode(output_value='token_embeddings') works :return: """ model = paraphrase_distilroberta_base_v1_model sent = [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", "Sentences", "Sentence five five five five five five five", ] emb = model.encode(sent, output_value="token_embeddings", batch_size=2) assert len(emb) == len(sent) for s, e in zip(sent, emb): assert len(model.tokenize([s])["input_ids"][0]) == e.shape[0] def test_encode_single_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Single sentence emb = model.encode("Hello Word, a test sentence") assert emb.shape == (768,) assert abs(np.sum(emb) - 7.9811716) < 0.002 # Single sentence as list emb = model.encode(["Hello Word, a test sentence"]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 7.9811716) < 0.002 # Sentence list emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 22.968266) < 0.007 def test_encode_normalize(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ], normalize_embeddings=True, ) assert emb.shape == (3, 768) for norm in np.linalg.norm(emb, axis=1): assert abs(norm - 1) < 0.001 def test_encode_tuple_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Input a sentence tuple emb = model.encode([("Hello Word, a test sentence", "Second input for model")]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 9.503508) < 0.002 # List of sentence tuples emb = model.encode( [ ("Hello Word, a test sentence", "Second input for model"), ("My second tuple", "With two inputs"), ("Final tuple", "final test"), ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 32.14627) < 0.002
import copy from typing import Dict, Tuple _SPECIFIC_EXECUTOR_SEPARATOR = '__' def _spit_key_and_executor_name(key_name: str) -> Tuple[str]: """Split a specific key into a key, name pair ex: 'key__my_executor' will be split into 'key', 'my_executor' :param key_name: key name of the param :return: return the split 'key', 'executor_name' for the key_name """ key_split = key_name.split(_SPECIFIC_EXECUTOR_SEPARATOR) new_key_name = key_split.pop(-1) executor_name = ''.join(key_split) return new_key_name, executor_name def _get_name_from_replicas_name(name: str) -> Tuple[str]: """return the original name without the replicas ex: 'exec1/rep-0' will be transform into 'exec1' :param name: name of the DataRequest :return: return the original name without the replicas """ return name.split('/')[0] def _is_param_for_specific_executor(key_name: str) -> bool: """Tell if a key is for a specific Executor ex: 'key' is for every Executor whereas 'my_executor__key' is only for 'my_executor' :param key_name: key name of the param :return: return True if key_name is for specific Executor, False otherwise """ if _SPECIFIC_EXECUTOR_SEPARATOR in key_name: if key_name.startswith(_SPECIFIC_EXECUTOR_SEPARATOR) or key_name.endswith( _SPECIFIC_EXECUTOR_SEPARATOR ): return False return True else: return False def _parse_specific_params(parameters: Dict, executor_name: str): """Parse the parameters dictionary to filter executor specific parameters :param parameters: dictionary container the parameters :param executor_name: name of the Executor :returns: the parsed parameters after applying filtering for the specific Executor """ parsed_params = copy.deepcopy(parameters) for key in parameters: if _is_param_for_specific_executor(key): ( key_name, key_executor_name, ) = _spit_key_and_executor_name(key) if key_executor_name == executor_name: parsed_params[key_name] = parameters[key] del parsed_params[key] specific_parameters = parameters.get(executor_name, None) if specific_parameters: parsed_params.update(**specific_parameters) return parsed_params
import copy from typing import Any, Dict, List, Tuple _SPECIFIC_EXECUTOR_SEPARATOR = '__' def _spit_key_and_executor_name(key_name: str) -> Tuple[str]: """Split a specific key into a key, name pair ex: 'key__my_executor' will be split into 'key', 'my_executor' :param key_name: key name of the param :return: return the split 'key', 'executor_name' for the key_name """ key_split = key_name.split(_SPECIFIC_EXECUTOR_SEPARATOR) new_key_name = key_split.pop(-1) executor_name = ''.join(key_split) return new_key_name, executor_name def _get_name_from_replicas_name(name: str) -> Tuple[str]: """return the original name without the replicas ex: 'exec1/rep-0' will be transform into 'exec1' :param name: name of the DataRequest :return: return the original name without the replicas """ return name.split('/')[0] def _is_param_for_specific_executor(key_name: str) -> bool: """Tell if a key is for a specific Executor ex: 'key' is for every Executor whereas 'my_executor__key' is only for 'my_executor' :param key_name: key name of the param :return: return True if key_name is for specific Executor, False otherwise """ if _SPECIFIC_EXECUTOR_SEPARATOR in key_name: if key_name.startswith(_SPECIFIC_EXECUTOR_SEPARATOR) or key_name.endswith( _SPECIFIC_EXECUTOR_SEPARATOR ): return False return True else: return False def _parse_specific_params(parameters: Dict, executor_name: str): """Parse the parameters dictionary to filter executor specific parameters :param parameters: dictionary container the parameters :param executor_name: name of the Executor :returns: the parsed parameters after applying filtering for the specific Executor """ parsed_params = copy.deepcopy(parameters) for key in parameters: if _is_param_for_specific_executor(key): ( key_name, key_executor_name, ) = _spit_key_and_executor_name(key) if key_executor_name == executor_name: parsed_params[key_name] = parameters[key] del parsed_params[key] specific_parameters = parameters.get(executor_name, None) if specific_parameters: parsed_params.update(**specific_parameters) return parsed_params _DEFAULT_GRPC_OPTION = { 'grpc.max_send_message_length': -1, 'grpc.max_receive_message_length': -1, # for the following see this blog post for the choice of default value https://cs.mcgill.ca/~mxia3/2019/02/23/Using-gRPC-in-Production/ 'grpc.keepalive_time_ms': 10000, # send keepalive ping every 10 second, default is 2 hours. 'grpc.keepalive_timeout_ms': 5000, # keepalive ping time out after 5 seconds, default is 20 seconds 'grpc.keepalive_permit_without_calls': True, # allow keepalive pings when there's no gRPC calls 'grpc.http2.max_pings_without_data': 0, # allow unlimited amount of keepalive pings without data 'grpc.http2.min_time_between_pings_ms': 10000, # allow grpc pings from client every 10 seconds 'grpc.http2.min_ping_interval_without_data_ms': 5000, # allow grpc pings from client without data every 5 seconds } def _get_grpc_server_options(option_from_args: Dict) -> List[Tuple[str, Any]]: """transform dict of args into grpc option, will merge the args wit the default args :param option_from_args: a dict of argument :return: grpc option i.e a list of tuple of key value """ option_from_args = ( {**_DEFAULT_GRPC_OPTION, **option_from_args} if option_from_args else _DEFAULT_GRPC_OPTION ) # merge new and default args return list(option_from_args.items())
import os import pytest import torch import whisper @pytest.mark.parametrize("model_name", whisper.available_models()) def test_transcribe(model_name: str): device = "cuda" if torch.cuda.is_available() else "cpu" model = whisper.load_model(model_name).to(device) audio_path = os.path.join(os.path.dirname(__file__), "jfk.flac") language = "en" if model_name.endswith(".en") else None result = model.transcribe(audio_path, language=language, temperature=0.0) assert result["language"] == "en" transcription = result["text"].lower() assert "my fellow americans" in transcription assert "your country" in transcription assert "do for you" in transcription
import os import pytest import whisper @pytest.mark.parametrize('model_name', whisper.available_models()) def test_transcribe(model_name: str): model = whisper.load_model(model_name).cuda() audio_path = os.path.join(os.path.dirname(__file__), "jfk.flac") language = "en" if model_name.endswith(".en") else None result = model.transcribe(audio_path, language=language, temperature=0.0) assert result["language"] == "en" transcription = result["text"].lower() assert "my fellow americans" in transcription assert "your country" in transcription assert "do for you" in transcription
# Copyright 2020 The HuggingFace Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING import numpy as np import pyarrow as pa from .. import config from ..utils.py_utils import map_nested from .formatting import TensorFormatter if TYPE_CHECKING: import torch class TorchFormatter(TensorFormatter[Mapping, "torch.Tensor", Mapping]): def __init__(self, features=None, **torch_tensor_kwargs): super().__init__(features=features) self.torch_tensor_kwargs = torch_tensor_kwargs import torch # noqa import torch at initialization def _consolidate(self, column): import torch if isinstance(column, list) and column: if all( isinstance(x, torch.Tensor) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return torch.stack(column) return column def _tensorize(self, value): import torch if isinstance(value, (str, bytes, type(None))): return value elif isinstance(value, (np.character, np.ndarray)) and np.issubdtype(value.dtype, np.character): return value.tolist() default_dtype = {} if isinstance(value, (np.number, np.ndarray)) and np.issubdtype(value.dtype, np.integer): default_dtype = {"dtype": torch.int64} elif isinstance(value, (np.number, np.ndarray)) and np.issubdtype(value.dtype, np.floating): default_dtype = {"dtype": torch.float32} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(value, PIL.Image.Image): value = np.asarray(value) return torch.tensor(value, **{**default_dtype, **self.torch_tensor_kwargs}) def _recursive_tensorize(self, data_struct): import torch # support for torch, tf, jax etc. if hasattr(data_struct, "__array__") and not isinstance(data_struct, torch.Tensor): data_struct = data_struct.__array__() # support for nested types like struct of list of struct if isinstance(data_struct, np.ndarray): if data_struct.dtype == object: # torch tensors cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(substruct) for substruct in data_struct]) elif isinstance(data_struct, (list, tuple)): return self._consolidate([self.recursive_tensorize(substruct) for substruct in data_struct]) return self._tensorize(data_struct) def recursive_tensorize(self, data_struct: dict): return map_nested(self._recursive_tensorize, data_struct, map_list=False) def format_row(self, pa_table: pa.Table) -> Mapping: row = self.numpy_arrow_extractor().extract_row(pa_table) row = self.python_features_decoder.decode_row(row) return self.recursive_tensorize(row) def format_column(self, pa_table: pa.Table) -> "torch.Tensor": column = self.numpy_arrow_extractor().extract_column(pa_table) column = self.python_features_decoder.decode_column(column, pa_table.column_names[0]) column = self.recursive_tensorize(column) column = self._consolidate(column) return column def format_batch(self, pa_table: pa.Table) -> Mapping: batch = self.numpy_arrow_extractor().extract_batch(pa_table) batch = self.python_features_decoder.decode_batch(batch) batch = self.recursive_tensorize(batch) for column_name in batch: batch[column_name] = self._consolidate(batch[column_name]) return batch
# Copyright 2020 The HuggingFace Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Lint as: python3 import sys from collections.abc import Mapping from typing import TYPE_CHECKING import numpy as np import pyarrow as pa from .. import config from ..utils.py_utils import map_nested from .formatting import Formatter if TYPE_CHECKING: import torch class TorchFormatter(Formatter[Mapping, "torch.Tensor", Mapping]): def __init__(self, features=None, **torch_tensor_kwargs): super().__init__(features=features) self.torch_tensor_kwargs = torch_tensor_kwargs import torch # noqa import torch at initialization def _consolidate(self, column): import torch if isinstance(column, list) and column: if all( isinstance(x, torch.Tensor) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ): return torch.stack(column) return column def _tensorize(self, value): import torch if isinstance(value, (str, bytes, type(None))): return value elif isinstance(value, (np.character, np.ndarray)) and np.issubdtype(value.dtype, np.character): return value.tolist() default_dtype = {} if isinstance(value, (np.number, np.ndarray)) and np.issubdtype(value.dtype, np.integer): default_dtype = {"dtype": torch.int64} elif isinstance(value, (np.number, np.ndarray)) and np.issubdtype(value.dtype, np.floating): default_dtype = {"dtype": torch.float32} elif config.PIL_AVAILABLE and "PIL" in sys.modules: import PIL.Image if isinstance(value, PIL.Image.Image): value = np.asarray(value) return torch.tensor(value, **{**default_dtype, **self.torch_tensor_kwargs}) def _recursive_tensorize(self, data_struct: dict): # support for nested types like struct of list of struct if isinstance(data_struct, np.ndarray): if data_struct.dtype == object: # torch tensors cannot be instantied from an array of objects return self._consolidate([self.recursive_tensorize(substruct) for substruct in data_struct]) return self._tensorize(data_struct) def recursive_tensorize(self, data_struct: dict): return map_nested(self._recursive_tensorize, data_struct) def format_row(self, pa_table: pa.Table) -> Mapping: row = self.numpy_arrow_extractor().extract_row(pa_table) row = self.python_features_decoder.decode_row(row) return self.recursive_tensorize(row) def format_column(self, pa_table: pa.Table) -> "torch.Tensor": column = self.numpy_arrow_extractor().extract_column(pa_table) column = self.python_features_decoder.decode_column(column, pa_table.column_names[0]) column = self.recursive_tensorize(column) column = self._consolidate(column) return column def format_batch(self, pa_table: pa.Table) -> Mapping: batch = self.numpy_arrow_extractor().extract_batch(pa_table) batch = self.python_features_decoder.decode_batch(batch) batch = self.recursive_tensorize(batch) for column_name in batch: batch[column_name] = self._consolidate(batch[column_name]) return batch
__version__ = '0.18.0' import os from docarray.document import Document from docarray.array import DocumentArray from docarray.dataclasses import dataclass, field if 'DA_RICH_HANDLER' in os.environ: from rich.traceback import install install()
__version__ = '0.17.1' import os from docarray.document import Document from docarray.array import DocumentArray from docarray.dataclasses import dataclass, field if 'DA_RICH_HANDLER' in os.environ: from rich.traceback import install install()
from typing import Any, Optional from typing_extensions import get_origin from typing_inspect import get_args, is_typevar, is_union_type from docarray.typing.id import ID from docarray.typing.tensor.abstract_tensor import AbstractTensor def is_type_tensor(type_: Any) -> bool: """Return True if type is a type Tensor or an Optional Tensor type.""" return isinstance(type_, type) and issubclass(type_, AbstractTensor) def is_tensor_union(type_: Any) -> bool: """Return True if type is a Union of type Tensors.""" is_union = is_union_type(type_) if is_union is None: return False else: return is_union and all( (is_type_tensor(t) or issubclass(t, type(None))) for t in get_args(type_) ) def change_cls_name(cls: type, new_name: str, scope: Optional[dict] = None) -> None: """Change the name of a class. :param cls: the class to change the name of :param new_name: the new name :param scope: the scope in which the class is defined """ if scope: scope[new_name] = cls cls.__qualname__ = cls.__qualname__[: -len(cls.__name__)] + new_name cls.__name__ = new_name def safe_issubclass(x: type, a_tuple: type) -> bool: """ This is a modified version of the built-in 'issubclass' function to support non-class input. Traditional 'issubclass' calls can result in a crash if the input is non-class type (e.g. list/tuple). :param x: A class 'x' :param a_tuple: A class, or a tuple of classes. :return: A boolean value - 'True' if 'x' is a subclass of 'A_tuple', 'False' otherwise. Note that if the origin of 'x' is a list or tuple, the function immediately returns 'False'. """ if (get_origin(x) in (list, tuple, dict, set)) or is_typevar(x) or x == ID: return False return issubclass(x, a_tuple)
from typing import Any, Optional from typing_extensions import get_origin from typing_inspect import get_args, is_typevar, is_union_type from docarray.typing.tensor.abstract_tensor import AbstractTensor def is_type_tensor(type_: Any) -> bool: """Return True if type is a type Tensor or an Optional Tensor type.""" return isinstance(type_, type) and issubclass(type_, AbstractTensor) def is_tensor_union(type_: Any) -> bool: """Return True if type is a Union of type Tensors.""" is_union = is_union_type(type_) if is_union is None: return False else: return is_union and all( (is_type_tensor(t) or issubclass(t, type(None))) for t in get_args(type_) ) def change_cls_name(cls: type, new_name: str, scope: Optional[dict] = None) -> None: """Change the name of a class. :param cls: the class to change the name of :param new_name: the new name :param scope: the scope in which the class is defined """ if scope: scope[new_name] = cls cls.__qualname__ = cls.__qualname__[: -len(cls.__name__)] + new_name cls.__name__ = new_name def safe_issubclass(x: type, a_tuple: type) -> bool: """ This is a modified version of the built-in 'issubclass' function to support non-class input. Traditional 'issubclass' calls can result in a crash if the input is non-class type (e.g. list/tuple). :param x: A class 'x' :param a_tuple: A class, or a tuple of classes. :return: A boolean value - 'True' if 'x' is a subclass of 'A_tuple', 'False' otherwise. Note that if the origin of 'x' is a list or tuple, the function immediately returns 'False'. """ if (get_origin(x) in (list, tuple, dict, set)) or is_typevar(x): return False return issubclass(x, a_tuple)
_base_ = [ '../_base_/models/mask_rcnn_r50_fpn.py', '../common/lsj_100e_coco_instance.py' ] image_size = (1024, 1024) batch_augments = [dict(type='BatchFixedSizePad', size=image_size)] norm_cfg = dict(type='SyncBN', requires_grad=True) # Use MMSyncBN that handles empty tensor in head. It can be changed to # SyncBN after https://github.com/pytorch/pytorch/issues/36530 is fixed # Requires MMCV-full after https://github.com/open-mmlab/mmcv/pull/1205. head_norm_cfg = dict(type='MMSyncBN', requires_grad=True) model = dict( data_preprocessor=dict( type='DetDataPreprocessor', mean=[103.530, 116.280, 123.675], std=[1.0, 1.0, 1.0], bgr_to_rgb=False, # pad_size_divisor=32 is unnecessary in training but necessary # in testing. pad_size_divisor=32, batch_augments=batch_augments), backbone=dict( frozen_stages=-1, norm_eval=False, norm_cfg=norm_cfg, init_cfg=None, style='caffe'), neck=dict(norm_cfg=norm_cfg), rpn_head=dict(num_convs=2), roi_head=dict( bbox_head=dict( type='Shared4Conv1FCBBoxHead', conv_out_channels=256, norm_cfg=head_norm_cfg), mask_head=dict(norm_cfg=head_norm_cfg))) file_client_args = dict(backend='disk') # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict(type='RandomResize', scale=image_size, ratio_range=(0.1, 2.0)), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, recompute_bbox=True, allow_negative_crop=True), dict(type='FilterAnnotations', min_gt_bbox_wh=(1e-2, 1e-2)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] # Use RepeatDataset to speed up training train_dataloader = dict(dataset=dict(dataset=dict(pipeline=train_pipeline)))
_base_ = [ '../_base_/models/mask_rcnn_r50_fpn.py', '../common/lsj_100e_coco_instance.py' ] image_size = (1024, 1024) batch_augments = [dict(type='BatchFixedSizePad', size=image_size)] norm_cfg = dict(type='SyncBN', requires_grad=True) # Use MMSyncBN that handles empty tensor in head. It can be changed to # SyncBN after https://github.com/pytorch/pytorch/issues/36530 is fixed # Requires MMCV-full after https://github.com/open-mmlab/mmcv/pull/1205. head_norm_cfg = dict(type='MMSyncBN', requires_grad=True) model = dict( data_preprocessor=dict( type='DetDataPreprocessor', mean=[103.530, 116.280, 123.675], std=[1.0, 1.0, 1.0], bgr_to_rgb=False, batch_augments=batch_augments), backbone=dict( frozen_stages=-1, norm_eval=False, norm_cfg=norm_cfg, init_cfg=None, style='caffe'), neck=dict(norm_cfg=norm_cfg), rpn_head=dict(num_convs=2), roi_head=dict( bbox_head=dict( type='Shared4Conv1FCBBoxHead', conv_out_channels=256, norm_cfg=head_norm_cfg), mask_head=dict(norm_cfg=head_norm_cfg))) file_client_args = dict(backend='disk') # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict(type='RandomResize', scale=image_size, ratio_range=(0.1, 2.0)), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, recompute_bbox=True, allow_negative_crop=True), dict(type='FilterAnnotations', min_gt_bbox_wh=(1e-2, 1e-2)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] # Use RepeatDataset to speed up training train_dataloader = dict(dataset=dict(dataset=dict(pipeline=train_pipeline)))
from typing import Any, Optional, Type, TypeVar, Union import numpy as np from docarray.base_doc import BaseDoc from docarray.typing import AnyEmbedding, ImageBytes, ImageUrl from docarray.typing.tensor.abstract_tensor import AbstractTensor from docarray.typing.tensor.image.image_tensor import ImageTensor from docarray.utils.misc import is_tf_available, is_torch_available T = TypeVar('T', bound='ImageDoc') torch_available = is_torch_available() if torch_available: import torch tf_available = is_tf_available() if tf_available: import tensorflow as tf # type: ignore class ImageDoc(BaseDoc): """ Document for handling images. It can contain an ImageUrl (`Image.url`), an AnyTensor (`Image.tensor`), and an AnyEmbedding (`Image.embedding`). EXAMPLE USAGE: You can use this Document directly: .. code-block:: python from docarray.documents import ImageDoc # use it directly image = ImageDoc(url='http://www.jina.ai/image.jpg') image.tensor = image.url.load() model = MyEmbeddingModel() image.embedding = model(image.tensor) You can extend this Document: .. code-block:: python from docarray.documents import ImageDoc from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyImage(ImageDoc): second_embedding: Optional[AnyEmbedding] image = MyImage(url='http://www.jina.ai/image.jpg') image.tensor = image.url.load() model = MyEmbeddingModel() image.embedding = model(image.tensor) image.second_embedding = model(image.tensor) You can use this Document for composition: .. code-block:: python from docarray import BaseDoc from docarray.documents import ImageDoc, TextDoc # compose it class MultiModalDoc(BaseDoc): image: Image text: Text mmdoc = MultiModalDoc( image=Image(url="http://www.jina.ai/image.jpg"), text=Text(text="hello world, how are you doing?"), ) mmdoc.image.tensor = mmdoc.image.url.load() # or mmdoc.image.bytes_ = mmdoc.image.url.load_bytes() mmdoc.image.tensor = mmdoc.image.bytes.load() """ url: Optional[ImageUrl] tensor: Optional[ImageTensor] embedding: Optional[AnyEmbedding] bytes_: Optional[ImageBytes] @classmethod def validate( cls: Type[T], value: Union[str, AbstractTensor, Any], ) -> T: if isinstance(value, str): value = cls(url=value) elif ( isinstance(value, (AbstractTensor, np.ndarray)) or (torch_available and isinstance(value, torch.Tensor)) or (tf_available and isinstance(value, tf.Tensor)) ): value = cls(tensor=value) elif isinstance(value, bytes): value = cls(byte=value) return super().validate(value)
from typing import Any, Optional, Type, TypeVar, Union import numpy as np from docarray.base_document import BaseDocument from docarray.typing import AnyEmbedding, ImageBytes, ImageUrl from docarray.typing.tensor.abstract_tensor import AbstractTensor from docarray.typing.tensor.image.image_tensor import ImageTensor from docarray.utils.misc import is_tf_available, is_torch_available T = TypeVar('T', bound='ImageDoc') torch_available = is_torch_available() if torch_available: import torch tf_available = is_tf_available() if tf_available: import tensorflow as tf # type: ignore class ImageDoc(BaseDocument): """ Document for handling images. It can contain an ImageUrl (`Image.url`), an AnyTensor (`Image.tensor`), and an AnyEmbedding (`Image.embedding`). EXAMPLE USAGE: You can use this Document directly: .. code-block:: python from docarray.documents import ImageDoc # use it directly image = ImageDoc(url='http://www.jina.ai/image.jpg') image.tensor = image.url.load() model = MyEmbeddingModel() image.embedding = model(image.tensor) You can extend this Document: .. code-block:: python from docarray.documents import ImageDoc from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyImage(ImageDoc): second_embedding: Optional[AnyEmbedding] image = MyImage(url='http://www.jina.ai/image.jpg') image.tensor = image.url.load() model = MyEmbeddingModel() image.embedding = model(image.tensor) image.second_embedding = model(image.tensor) You can use this Document for composition: .. code-block:: python from docarray import BaseDocument from docarray.documents import ImageDoc, TextDoc # compose it class MultiModalDoc(BaseDocument): image: Image text: Text mmdoc = MultiModalDoc( image=Image(url="http://www.jina.ai/image.jpg"), text=Text(text="hello world, how are you doing?"), ) mmdoc.image.tensor = mmdoc.image.url.load() # or mmdoc.image.bytes_ = mmdoc.image.url.load_bytes() mmdoc.image.tensor = mmdoc.image.bytes.load() """ url: Optional[ImageUrl] tensor: Optional[ImageTensor] embedding: Optional[AnyEmbedding] bytes_: Optional[ImageBytes] @classmethod def validate( cls: Type[T], value: Union[str, AbstractTensor, Any], ) -> T: if isinstance(value, str): value = cls(url=value) elif ( isinstance(value, (AbstractTensor, np.ndarray)) or (torch_available and isinstance(value, torch.Tensor)) or (tf_available and isinstance(value, tf.Tensor)) ): value = cls(tensor=value) elif isinstance(value, bytes): value = cls(byte=value) return super().validate(value)
""" In SecGPT, all messages exchanged among spokes conform to predefined formats, encapsulated within the Message class. """ import json class Message: @staticmethod def function_probe_request(spoke_id, function): """ Create a function probe request message. Args: spoke_id (str): The ID of the spoke sending the request. function (str): The functionality being requested. Returns: bytes: The JSON-encoded function probe request message. """ message = {} message["message_type"] = "function_probe_request" message["spoke_id"] = spoke_id message["requested_functionality"] = function # functionality name str return json.dumps(message).encode("utf-8") @staticmethod def function_probe_response(spoke_id, function): """ Create a function probe response message. Args: spoke_id (str): The ID of the spoke sending the response. function (str): The functionality being offered (in JSON format). Returns: bytes: The JSON-encoded function probe response message. """ message = {} message["message_type"] = "function_probe_response" message["spoke_id"] = spoke_id message["functionality_offered"] = function # should be a json format return json.dumps(message).encode("utf-8") @staticmethod def app_request(spoke_id, function, functionality_request): """ Create an application request message. Args: spoke_id (str): The ID of the spoke sending the request. function (str): The functionality being requested. functionality_request (str): The request body formatted in JSON. Returns: bytes: The JSON-encoded application request message. """ message = {} message["message_type"] = "app_request" message["spoke_id"] = spoke_id message["functionality_request"] = function message["request_body"] = functionality_request # format the request with json return json.dumps(message).encode("utf-8") @staticmethod def app_response(spoke_id, functionality_response): """ Create an application response message. Args: spoke_id (str): The ID of the spoke sending the response. functionality_response (str): The response body. Returns: bytes: The JSON-encoded application response message. """ message = {} message["message_type"] = "app_response" message["spoke_id"] = spoke_id message["response"] = functionality_response return json.dumps(message).encode("utf-8") @staticmethod def final_response(spoke_id, final_response): """ Create a final response message. Args: spoke_id (str): The ID of the spoke sending the final response. final_response (str): The final response body. Returns: bytes: The JSON-encoded final response message. """ message = {} message["message_type"] = "final_response" message["spoke_id"] = spoke_id message["response"] = final_response return json.dumps(message).encode("utf-8") @staticmethod def no_functionality_response(spoke_id, functionality_request): """ Create a no functionality response message indicating the requested functionality was not found. Args: spoke_id (str): The ID of the spoke sending the response. functionality_request (str): The functionality request that was not found. Returns: bytes: The JSON-encoded no functionality response message. """ message = {} message["message_type"] = "no_functionality_response" message["spoke_id"] = spoke_id message["response"] = functionality_request + " not found" return json.dumps(message).encode("utf-8") @staticmethod def functionality_denial_response(spoke_id, functionality_request): """ Create a functionality denial response message indicating the requested functionality refuses to respond. Args: spoke_id (str): The ID of the spoke sending the response. functionality_request (str): The functionality request that is being denied. Returns: bytes: The JSON-encoded functionality denial response message. """ message = {} message["message_type"] = "functionality_denial_response" message["spoke_id"] = spoke_id message["response"] = functionality_request + " refuses to respond" return json.dumps(message).encode("utf-8")
""" In SecGPT, all messages exchanged among spokes conform to predefined formats, encapsulated within the Message class. """ import json class Message: @staticmethod def function_probe_request(spoke_id, function): """ Create a function probe request message. Args: spoke_id (str): The ID of the spoke sending the request. function (str): The functionality being requested. Returns: bytes: The JSON-encoded function probe request message. """ message = {} message["message_type"] = "function_probe_request" message["spoke_id"] = spoke_id message["requested_functionality"] = function # functionality name str return json.dumps(message).encode("utf-8") @staticmethod def function_probe_response(spoke_id, function): """ Create a function probe response message. Args: spoke_id (str): The ID of the spoke sending the response. function (str): The functionality being offered (in JSON format). Returns: bytes: The JSON-encoded function probe response message. """ message = {} message["message_type"] = "function_probe_response" message["spoke_id"] = spoke_id message["functionality_offered"] = function # should be a json format return json.dumps(message).encode("utf-8") @staticmethod def app_request(spoke_id, function, functionality_request): """ Create an application request message. Args: spoke_id (str): The ID of the spoke sending the request. function (str): The functionality being requested. functionality_request (str): The request body formatted in JSON. Returns: bytes: The JSON-encoded application request message. """ message = {} message["message_type"] = "app_request" message["spoke_id"] = spoke_id message["functionality_request"] = function message["request_body"] = functionality_request # format the request with json return json.dumps(message).encode("utf-8") @staticmethod def app_response(spoke_id, functionality_response): """ Create an application response message. Args: spoke_id (str): The ID of the spoke sending the response. functionality_response (str): The response body. Returns: bytes: The JSON-encoded application response message. """ message = {} message["message_type"] = "app_response" message["spoke_id"] = spoke_id message["response"] = functionality_response return json.dumps(message).encode("utf-8") @staticmethod def final_response(spoke_id, final_response): """ Create a final response message. Args: spoke_id (str): The ID of the spoke sending the final response. final_response (str): The final response body. Returns: bytes: The JSON-encoded final response message. """ message = {} message["message_type"] = "final_response" message["spoke_id"] = spoke_id message["response"] = final_response return json.dumps(message).encode("utf-8") @staticmethod def no_functionality_response(spoke_id, functionality_request): """ Create a no functionality response message indicating the requested functionality was not found. Args: spoke_id (str): The ID of the spoke sending the response. functionality_request (str): The functionality request that was not found. Returns: bytes: The JSON-encoded no functionality response message. """ message = {} message["message_type"] = "no_functionality_response" message["spoke_id"] = spoke_id message["response"] = functionality_request + " not found" return json.dumps(message).encode("utf-8") @staticmethod def functionality_denial_response(spoke_id, functionality_request): """ Create a functionality denial response message indicating the requested functionality refuses to respond. Args: spoke_id (str): The ID of the spoke sending the response. functionality_request (str): The functionality request that is being denied. Returns: bytes: The JSON-encoded functionality denial response message. """ message = {} message["message_type"] = "functionality_denial_response" message["spoke_id"] = spoke_id message["response"] = functionality_request + " refuses to respond" return json.dumps(message).encode("utf-8")
from __future__ import annotations import logging from typing import TYPE_CHECKING, Any from sentence_transformers.evaluation import TranslationEvaluator if TYPE_CHECKING: import numpy as np from torch import Tensor from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder logger = logging.getLogger(__name__) class SparseTranslationEvaluator(TranslationEvaluator): def __init__( self, source_sentences: list[str], target_sentences: list[str], show_progress_bar: bool = False, batch_size: int = 16, name: str = "", print_wrong_matches: bool = False, write_csv: bool = True, truncate_dim: int | None = None, ): return super().__init__( source_sentences, target_sentences, show_progress_bar=show_progress_bar, batch_size=batch_size, name=name, print_wrong_matches=print_wrong_matches, write_csv=write_csv, truncate_dim=truncate_dim, ) def __call__( self, model: SparseEncoder, output_path: str = None, epoch: int = -1, steps: int = -1 ) -> dict[str, float]: return super().__call__(model, output_path=output_path, epoch=epoch, steps=steps) def embed_inputs( self, model: SparseEncoder, sentences: str | list[str] | np.ndarray, **kwargs, ) -> list[Tensor]: kwargs["truncate_dim"] = self.truncate_dim return model.encode( sentences, batch_size=self.batch_size, show_progress_bar=self.show_progress_bar, convert_to_tensor=False, convert_to_sparse_tensor=True, save_on_cpu=True, **kwargs, ) def store_metrics_in_model_card_data( self, model: SparseEncoder, metrics: dict[str, Any], epoch: int = 0, step: int = 0 ) -> None: model.model_card_data.set_evaluation_metrics(self, metrics, epoch=epoch, step=step)
from __future__ import annotations import logging from typing import TYPE_CHECKING, Any from sentence_transformers.evaluation import TranslationEvaluator if TYPE_CHECKING: import numpy as np from torch import Tensor from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder logger = logging.getLogger(__name__) class SparseTranslationEvaluator(TranslationEvaluator): def __init__( self, source_sentences: list[str], target_sentences: list[str], show_progress_bar: bool = False, batch_size: int = 16, name: str = "", print_wrong_matches: bool = False, write_csv: bool = True, truncate_dim: int | None = None, ): return super().__init__( source_sentences, target_sentences, show_progress_bar=show_progress_bar, batch_size=batch_size, name=name, print_wrong_matches=print_wrong_matches, write_csv=write_csv, truncate_dim=truncate_dim, ) def __call__( self, model: SparseEncoder, output_path: str = None, epoch: int = -1, steps: int = -1 ) -> dict[str, float]: return super().__call__(model, output_path=output_path, epoch=epoch, steps=steps) def embed_inputs( self, model: SparseEncoder, sentences: str | list[str] | np.ndarray, **kwargs, ) -> list[Tensor]: kwargs["truncate_dim"] = self.truncate_dim return model.encode( sentences, batch_size=self.batch_size, show_progress_bar=self.show_progress_bar, convert_to_tensor=False, convert_to_sparse_tensor=True, **kwargs, ) def store_metrics_in_model_card_data( self, model: SparseEncoder, metrics: dict[str, Any], epoch: int = 0, step: int = 0 ) -> None: model.model_card_data.set_evaluation_metrics(self, metrics, epoch=epoch, step=step)
import logging import prisma.types logger = logging.getLogger(__name__) async def log_raw_analytics( user_id: str, type: str, data: dict, data_index: str, ): details = await prisma.models.AnalyticsDetails.prisma().create( data=prisma.types.AnalyticsDetailsCreateInput( userId=user_id, type=type, data=prisma.Json(data), dataIndex=data_index, ) ) return details async def log_raw_metric( user_id: str, metric_name: str, metric_value: float, data_string: str, ): if metric_value < 0: raise ValueError("metric_value must be non-negative") result = await prisma.models.AnalyticsMetrics.prisma().create( data=prisma.types.AnalyticsMetricsCreateInput( value=metric_value, analyticMetric=metric_name, userId=user_id, dataString=data_string, ) ) return result
import logging import prisma.types logger = logging.getLogger(__name__) async def log_raw_analytics( user_id: str, type: str, data: dict, data_index: str, ): details = await prisma.models.AnalyticsDetails.prisma().create( data={ "userId": user_id, "type": type, "data": prisma.Json(data), "dataIndex": data_index, } ) return details async def log_raw_metric( user_id: str, metric_name: str, metric_value: float, data_string: str, ): if metric_value < 0: raise ValueError("metric_value must be non-negative") result = await prisma.models.AnalyticsMetrics.prisma().create( data={ "value": metric_value, "analyticMetric": metric_name, "userId": user_id, "dataString": data_string, }, ) return result
"""Astra DB.""" from typing import Any, List, Optional import llama_index.core from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document class AstraDBReader(BaseReader): """ Astra DB reader. Retrieve documents from an Astra DB Instance. Args: collection_name (str): collection name to use. If not existing, it will be created. token (str): The Astra DB Application Token to use. api_endpoint (str): The Astra DB JSON API endpoint for your database. embedding_dimension (int): Length of the embedding vectors in use. namespace (Optional[str]): The namespace to use. If not provided, 'default_keyspace' client (Optional[Any]): Astra DB client to use. If not provided, one will be created. """ def __init__( self, *, collection_name: str, token: str, api_endpoint: str, embedding_dimension: int, namespace: Optional[str] = None, client: Optional[Any] = None, ) -> None: """Initialize with parameters.""" import_err_msg = ( "`astrapy` package not found, please run `pip install --upgrade astrapy`" ) # Try to import astrapy for use try: from astrapy.db import AstraDB except ImportError: raise ImportError(import_err_msg) if client is not None: self._client = client.copy() self._client.set_caller( caller_name=getattr(llama_index, "__name__", "llama_index"), caller_version=getattr(llama_index.core, "__version__", None), ) else: # Build the Astra DB object self._client = AstraDB( api_endpoint=api_endpoint, token=token, namespace=namespace, caller_name=getattr(llama_index, "__name__", "llama_index"), caller_version=getattr(llama_index.core, "__version__", None), ) self._collection = self._client.create_collection( collection_name=collection_name, dimension=embedding_dimension ) def load_data(self, vector: List[float], limit: int = 10, **kwargs: Any) -> Any: """ Load data from Astra DB. Args: vector (Any): Query limit (int): Number of results to return. kwargs (Any): Additional arguments to pass to the Astra DB query. Returns: List[Document]: A list of documents. """ results = self._collection.vector_find( vector, limit=limit, fields=["*"], **kwargs, ) documents: List[Document] = [] for result in results: document = Document( doc_id=result["_id"], text=result["content"], embedding=result["$vector"], ) documents.append(document) return documents
"""Astra DB.""" from typing import Any, List, Optional import llama_index.core from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document class AstraDBReader(BaseReader): """Astra DB reader. Retrieve documents from an Astra DB Instance. Args: collection_name (str): collection name to use. If not existing, it will be created. token (str): The Astra DB Application Token to use. api_endpoint (str): The Astra DB JSON API endpoint for your database. embedding_dimension (int): Length of the embedding vectors in use. namespace (Optional[str]): The namespace to use. If not provided, 'default_keyspace' client (Optional[Any]): Astra DB client to use. If not provided, one will be created. """ def __init__( self, *, collection_name: str, token: str, api_endpoint: str, embedding_dimension: int, namespace: Optional[str] = None, client: Optional[Any] = None, ) -> None: """Initialize with parameters.""" import_err_msg = ( "`astrapy` package not found, please run `pip install --upgrade astrapy`" ) # Try to import astrapy for use try: from astrapy.db import AstraDB except ImportError: raise ImportError(import_err_msg) if client is not None: self._client = client.copy() self._client.set_caller( caller_name=getattr(llama_index, "__name__", "llama_index"), caller_version=getattr(llama_index.core, "__version__", None), ) else: # Build the Astra DB object self._client = AstraDB( api_endpoint=api_endpoint, token=token, namespace=namespace, caller_name=getattr(llama_index, "__name__", "llama_index"), caller_version=getattr(llama_index.core, "__version__", None), ) self._collection = self._client.create_collection( collection_name=collection_name, dimension=embedding_dimension ) def load_data(self, vector: List[float], limit: int = 10, **kwargs: Any) -> Any: """Load data from Astra DB. Args: vector (Any): Query limit (int): Number of results to return. kwargs (Any): Additional arguments to pass to the Astra DB query. Returns: List[Document]: A list of documents. """ results = self._collection.vector_find( vector, limit=limit, fields=["*"], **kwargs, ) documents: List[Document] = [] for result in results: document = Document( doc_id=result["_id"], text=result["content"], embedding=result["$vector"], ) documents.append(document) return documents
# dataset settings dataset_type = 'CocoPanopticDataset' data_root = 'data/coco/' # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) file_client_args = dict(backend='disk') train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadPanopticAnnotations'), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='SegRescale', scale_factor=1 / 4), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), batch_sampler=dict(type='AspectRatioBatchSampler'), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/panoptic_train2017.json', seg_prefix=data_root + 'annotations/panoptic_train2017/', data_prefix=dict(img='train2017/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline)) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/panoptic_val2017.json', seg_prefix=data_root + 'annotations/panoptic_val2017/', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=test_pipeline)) test_dataloader = val_dataloader val_evaluator = dict( type='CocoPanopticMetric', ann_file=data_root + 'annotations/panoptic_val2017.json', seg_prefix=data_root + 'annotations/panoptic_val2017/', metric='PQ') test_evaluator = val_evaluator # TODO add setting on test dataset after panoptic fpn # inference on test dataset and # format the output results for submission. # test_dataloader = None # test_evaluator = None
# dataset settings dataset_type = 'CocoPanopticDataset' data_root = 'data/coco/' # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) file_client_args = dict(backend='disk') train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadPanopticAnnotations'), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='SegRescale', scale_factor=1 / 4), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), batch_sampler=dict(type='AspectRatioBatchSampler'), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/panoptic_train2017.json', seg_prefix=data_root + 'annotations/panoptic_train2017/', data_prefix=dict(img='train2017/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline)) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/panoptic_val2017.json', seg_prefix=data_root + 'annotations/panoptic_val2017/', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=test_pipeline)) test_dataloader = val_dataloader val_evaluator = dict( type='CocoPanopticMetric', ann_file=data_root + 'annotations/panoptic_val2017.json', seg_prefix=data_root + 'annotations/panoptic_val2017/', metric='PQ') test_evaluator = val_evaluator
# Copyright (c) OpenMMLab. All rights reserved. import argparse import numpy as np import torch from mmengine.config import Config, DictAction from mmdet.registry import MODELS from mmdet.utils import register_all_modules try: from mmcv.cnn import get_model_complexity_info except ImportError: raise ImportError('Please upgrade mmcv to >0.6.2') def parse_args(): parser = argparse.ArgumentParser(description='Train a detector') parser.add_argument('config', help='train config file path') parser.add_argument( '--shape', type=int, nargs='+', default=[1280, 800], help='input image size') parser.add_argument( '--cfg-options', nargs='+', action=DictAction, help='override some settings in the used config, the key-value pair ' 'in xxx=yyy format will be merged into config file. If the value to ' 'be overwritten is a list, it should be like key="[a,b]" or key=a,b ' 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" ' 'Note that the quotation marks are necessary and that no white space ' 'is allowed.') parser.add_argument( '--size-divisor', type=int, default=32, help='Pad the input image, the minimum size that is divisible ' 'by size_divisor, -1 means do not pad the image.') args = parser.parse_args() return args def main(): register_all_modules() args = parse_args() if len(args.shape) == 1: h = w = args.shape[0] elif len(args.shape) == 2: h, w = args.shape else: raise ValueError('invalid input shape') ori_shape = (3, h, w) divisor = args.size_divisor if divisor > 0: h = int(np.ceil(h / divisor)) * divisor w = int(np.ceil(w / divisor)) * divisor input_shape = (3, h, w) cfg = Config.fromfile(args.config) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) model = MODELS.build(cfg.model) if torch.cuda.is_available(): model.cuda() model.eval() flops, params = get_model_complexity_info(model, input_shape) split_line = '=' * 30 if divisor > 0 and \ input_shape != ori_shape: print(f'{split_line}\nUse size divisor set input shape ' f'from {ori_shape} to {input_shape}\n') print(f'{split_line}\nInput shape: {input_shape}\n' f'Flops: {flops}\nParams: {params}\n{split_line}') print('!!!Please be cautious if you use the results in papers. ' 'You may need to check if all ops are supported and verify that the ' 'flops computation is correct.') if __name__ == '__main__': main()
# Copyright (c) OpenMMLab. All rights reserved. import argparse import numpy as np import torch from mmengine.config import Config, DictAction from mmdet.models import build_detector try: from mmcv.cnn import get_model_complexity_info except ImportError: raise ImportError('Please upgrade mmcv to >0.6.2') def parse_args(): parser = argparse.ArgumentParser(description='Train a detector') parser.add_argument('config', help='train config file path') parser.add_argument( '--shape', type=int, nargs='+', default=[1280, 800], help='input image size') parser.add_argument( '--cfg-options', nargs='+', action=DictAction, help='override some settings in the used config, the key-value pair ' 'in xxx=yyy format will be merged into config file. If the value to ' 'be overwritten is a list, it should be like key="[a,b]" or key=a,b ' 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" ' 'Note that the quotation marks are necessary and that no white space ' 'is allowed.') parser.add_argument( '--size-divisor', type=int, default=32, help='Pad the input image, the minimum size that is divisible ' 'by size_divisor, -1 means do not pad the image.') args = parser.parse_args() return args def main(): args = parse_args() if len(args.shape) == 1: h = w = args.shape[0] elif len(args.shape) == 2: h, w = args.shape else: raise ValueError('invalid input shape') ori_shape = (3, h, w) divisor = args.size_divisor if divisor > 0: h = int(np.ceil(h / divisor)) * divisor w = int(np.ceil(w / divisor)) * divisor input_shape = (3, h, w) cfg = Config.fromfile(args.config) if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) model = build_detector( cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg')) if torch.cuda.is_available(): model.cuda() model.eval() if hasattr(model, 'forward_dummy'): model.forward = model.forward_dummy else: raise NotImplementedError( 'FLOPs counter is currently not currently supported with {}'. format(model.__class__.__name__)) flops, params = get_model_complexity_info(model, input_shape) split_line = '=' * 30 if divisor > 0 and \ input_shape != ori_shape: print(f'{split_line}\nUse size divisor set input shape ' f'from {ori_shape} to {input_shape}\n') print(f'{split_line}\nInput shape: {input_shape}\n' f'Flops: {flops}\nParams: {params}\n{split_line}') print('!!!Please be cautious if you use the results in papers. ' 'You may need to check if all ops are supported and verify that the ' 'flops computation is correct.') if __name__ == '__main__': main()
from langchain_core.prompts import PromptTemplate prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. {context} Question: {question} Helpful Answer:""" # noqa: E501 PROMPT = PromptTemplate( template=prompt_template, input_variables=["context", "question"] )
# flake8: noqa from langchain_core.prompts import PromptTemplate prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. {context} Question: {question} Helpful Answer:""" PROMPT = PromptTemplate( template=prompt_template, input_variables=["context", "question"] )
# Copyright (c) OpenMMLab. All rights reserved. import argparse import torch.nn.functional as F import torchvision import torchvision.transforms as transforms from torch.optim import SGD from mmengine.evaluator import BaseMetric from mmengine.model import BaseModel from mmengine.runner import Runner class MMResNet50(BaseModel): def __init__(self): super().__init__() self.resnet = torchvision.models.resnet50() def forward(self, imgs, labels, mode): x = self.resnet(imgs) if mode == 'loss': return {'loss': F.cross_entropy(x, labels)} elif mode == 'predict': return x, labels class Accuracy(BaseMetric): def process(self, data_batch, data_samples): score, gt = data_samples self.results.append({ 'batch_size': len(gt), 'correct': (score.argmax(dim=1) == gt).sum().cpu(), }) def compute_metrics(self, results): total_correct = sum(item['correct'] for item in results) total_size = sum(item['batch_size'] for item in results) return dict(accuracy=100 * total_correct / total_size) def parse_args(): parser = argparse.ArgumentParser(description='Distributed Training') parser.add_argument( '--launcher', choices=['none', 'pytorch', 'slurm', 'mpi'], default='none', help='job launcher') parser.add_argument('--local_rank', type=int, default=0) args = parser.parse_args() return args def main(): args = parse_args() norm_cfg = dict(mean=[0.491, 0.482, 0.447], std=[0.202, 0.199, 0.201]) train_set = torchvision.datasets.CIFAR10( 'data/cifar10', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(**norm_cfg) ])) valid_set = torchvision.datasets.CIFAR10( 'data/cifar10', train=False, download=True, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize(**norm_cfg)])) train_dataloader = dict( batch_size=32, dataset=train_set, sampler=dict(type='DefaultSampler', shuffle=True), collate_fn=dict(type='default_collate')) val_dataloader = dict( batch_size=32, dataset=valid_set, sampler=dict(type='DefaultSampler', shuffle=False), collate_fn=dict(type='default_collate')) runner = Runner( model=MMResNet50(), work_dir='./work_dirs', train_dataloader=train_dataloader, optim_wrapper=dict(optimizer=dict(type=SGD, lr=0.001, momentum=0.9)), train_cfg=dict(by_epoch=True, max_epochs=2, val_interval=1), val_dataloader=val_dataloader, val_cfg=dict(), val_evaluator=dict(type=Accuracy), launcher=args.launcher, ) runner.train() if __name__ == '__main__': main()
# Copyright (c) OpenMMLab. All rights reserved. import argparse import torch.nn.functional as F import torchvision import torchvision.transforms as transforms from torch.optim import SGD from mmengine.evaluator import BaseMetric from mmengine.model import BaseModel from mmengine.runner import Runner class MMResNet50(BaseModel): def __init__(self): super().__init__() self.resnet = torchvision.models.resnet50() def forward(self, imgs, labels, mode): x = self.resnet(imgs) if mode == 'loss': return {'loss': F.cross_entropy(x, labels)} elif mode == 'predict': return x, labels class Accuracy(BaseMetric): def process(self, data_batch, data_samples): score, gt = data_samples self.results.append({ 'batch_size': len(gt), 'correct': (score.argmax(dim=1) == gt).sum().cpu(), }) def compute_metrics(self, results): total_correct = sum(item['correct'] for item in results) total_size = sum(item['batch_size'] for item in results) return dict(accuracy=100 * total_correct / total_size) def parse_args(): parser = argparse.ArgumentParser(description='Distributed Training') parser.add_argument( '--launcher', choices=['none', 'pytorch', 'slurm', 'mpi'], default='none', help='job launcher') parser.add_argument('--local_rank', type=int, default=0) args = parser.parse_args() return args def main(): args = parse_args() norm_cfg = dict(mean=[0.491, 0.482, 0.447], std=[0.202, 0.199, 0.201]) train_set = torchvision.datasets.CIFAR10( 'data/cifar10', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(**norm_cfg) ])) valid_set = torchvision.datasets.CIFAR10( 'data/cifar10', train=False, download=True, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize(**norm_cfg)])) train_dataloader = dict( batch_size=32, dataset=train_set, sampler=dict(type='DefaultSampler', shuffle=True), collate_fn=dict(type='default_collate')) val_dataloader = dict( batch_size=32, dataset=valid_set, sampler=dict(type='DefaultSampler', shuffle=False), collate_fn=dict(type='default_collate')) runner = Runner( model=MMResNet50(), work_dir='./work_dir', train_dataloader=train_dataloader, optim_wrapper=dict(optimizer=dict(type=SGD, lr=0.001, momentum=0.9)), train_cfg=dict(by_epoch=True, max_epochs=2, val_interval=1), val_dataloader=val_dataloader, val_cfg=dict(), val_evaluator=dict(type=Accuracy), launcher=args.launcher, ) runner.train() if __name__ == '__main__': main()
from ._source_separation_pipeline import ( CONVTASNET_BASE_LIBRI2MIX, HDEMUCS_HIGH_MUSDB, HDEMUCS_HIGH_MUSDB_PLUS, SourceSeparationBundle, ) from ._squim_pipeline import SQUIM_OBJECTIVE, SQUIM_SUBJECTIVE, SquimObjectiveBundle, SquimSubjectiveBundle from ._tts import ( TACOTRON2_GRIFFINLIM_CHAR_LJSPEECH, TACOTRON2_GRIFFINLIM_PHONE_LJSPEECH, TACOTRON2_WAVERNN_CHAR_LJSPEECH, TACOTRON2_WAVERNN_PHONE_LJSPEECH, Tacotron2TTSBundle, ) from ._wav2vec2.impl import ( HUBERT_ASR_LARGE, HUBERT_ASR_XLARGE, HUBERT_BASE, HUBERT_LARGE, HUBERT_XLARGE, MMS_FA, VOXPOPULI_ASR_BASE_10K_DE, VOXPOPULI_ASR_BASE_10K_EN, VOXPOPULI_ASR_BASE_10K_ES, VOXPOPULI_ASR_BASE_10K_FR, VOXPOPULI_ASR_BASE_10K_IT, WAV2VEC2_ASR_BASE_100H, WAV2VEC2_ASR_BASE_10M, WAV2VEC2_ASR_BASE_960H, WAV2VEC2_ASR_LARGE_100H, WAV2VEC2_ASR_LARGE_10M, WAV2VEC2_ASR_LARGE_960H, WAV2VEC2_ASR_LARGE_LV60K_100H, WAV2VEC2_ASR_LARGE_LV60K_10M, WAV2VEC2_ASR_LARGE_LV60K_960H, WAV2VEC2_BASE, WAV2VEC2_LARGE, WAV2VEC2_LARGE_LV60K, WAV2VEC2_XLSR53, WAV2VEC2_XLSR_1B, WAV2VEC2_XLSR_2B, WAV2VEC2_XLSR_300M, Wav2Vec2ASRBundle, Wav2Vec2Bundle, Wav2Vec2FABundle, WAVLM_BASE, WAVLM_BASE_PLUS, WAVLM_LARGE, ) from .rnnt_pipeline import EMFORMER_RNNT_BASE_LIBRISPEECH, RNNTBundle __all__ = [ "Wav2Vec2Bundle", "Wav2Vec2ASRBundle", "Wav2Vec2FABundle", "WAV2VEC2_BASE", "WAV2VEC2_LARGE", "WAV2VEC2_LARGE_LV60K", "WAV2VEC2_ASR_BASE_10M", "WAV2VEC2_ASR_BASE_100H", "WAV2VEC2_ASR_BASE_960H", "WAV2VEC2_ASR_LARGE_10M", "WAV2VEC2_ASR_LARGE_100H", "WAV2VEC2_ASR_LARGE_960H", "WAV2VEC2_ASR_LARGE_LV60K_10M", "WAV2VEC2_ASR_LARGE_LV60K_100H", "WAV2VEC2_ASR_LARGE_LV60K_960H", "WAV2VEC2_XLSR53", "WAV2VEC2_XLSR_300M", "WAV2VEC2_XLSR_1B", "WAV2VEC2_XLSR_2B", "VOXPOPULI_ASR_BASE_10K_EN", "VOXPOPULI_ASR_BASE_10K_ES", "VOXPOPULI_ASR_BASE_10K_DE", "VOXPOPULI_ASR_BASE_10K_FR", "VOXPOPULI_ASR_BASE_10K_IT", "HUBERT_BASE", "HUBERT_LARGE", "HUBERT_XLARGE", "HUBERT_ASR_LARGE", "HUBERT_ASR_XLARGE", "MMS_FA", "WAVLM_BASE", "WAVLM_BASE_PLUS", "WAVLM_LARGE", "Tacotron2TTSBundle", "TACOTRON2_GRIFFINLIM_CHAR_LJSPEECH", "TACOTRON2_GRIFFINLIM_PHONE_LJSPEECH", "TACOTRON2_WAVERNN_CHAR_LJSPEECH", "TACOTRON2_WAVERNN_PHONE_LJSPEECH", "RNNTBundle", "EMFORMER_RNNT_BASE_LIBRISPEECH", "SourceSeparationBundle", "CONVTASNET_BASE_LIBRI2MIX", "HDEMUCS_HIGH_MUSDB_PLUS", "HDEMUCS_HIGH_MUSDB", "SQUIM_OBJECTIVE", "SQUIM_SUBJECTIVE", "SquimObjectiveBundle", "SquimSubjectiveBundle", ]
from ._source_separation_pipeline import ( CONVTASNET_BASE_LIBRI2MIX, HDEMUCS_HIGH_MUSDB, HDEMUCS_HIGH_MUSDB_PLUS, SourceSeparationBundle, ) from ._squim_pipeline import SQUIM_OBJECTIVE, SQUIM_SUBJECTIVE, SquimObjectiveBundle, SquimSubjectiveBundle from ._tts import ( TACOTRON2_GRIFFINLIM_CHAR_LJSPEECH, TACOTRON2_GRIFFINLIM_PHONE_LJSPEECH, TACOTRON2_WAVERNN_CHAR_LJSPEECH, TACOTRON2_WAVERNN_PHONE_LJSPEECH, Tacotron2TTSBundle, ) from ._wav2vec2.impl import ( HUBERT_ASR_LARGE, HUBERT_ASR_XLARGE, HUBERT_BASE, HUBERT_LARGE, HUBERT_XLARGE, VOXPOPULI_ASR_BASE_10K_DE, VOXPOPULI_ASR_BASE_10K_EN, VOXPOPULI_ASR_BASE_10K_ES, VOXPOPULI_ASR_BASE_10K_FR, VOXPOPULI_ASR_BASE_10K_IT, WAV2VEC2_ASR_BASE_100H, WAV2VEC2_ASR_BASE_10M, WAV2VEC2_ASR_BASE_960H, WAV2VEC2_ASR_LARGE_100H, WAV2VEC2_ASR_LARGE_10M, WAV2VEC2_ASR_LARGE_960H, WAV2VEC2_ASR_LARGE_LV60K_100H, WAV2VEC2_ASR_LARGE_LV60K_10M, WAV2VEC2_ASR_LARGE_LV60K_960H, WAV2VEC2_BASE, WAV2VEC2_LARGE, WAV2VEC2_LARGE_LV60K, WAV2VEC2_XLSR53, WAV2VEC2_XLSR_1B, WAV2VEC2_XLSR_2B, WAV2VEC2_XLSR_300M, Wav2Vec2ASRBundle, Wav2Vec2Bundle, WAVLM_BASE, WAVLM_BASE_PLUS, WAVLM_LARGE, ) from .rnnt_pipeline import EMFORMER_RNNT_BASE_LIBRISPEECH, RNNTBundle __all__ = [ "Wav2Vec2Bundle", "Wav2Vec2ASRBundle", "WAV2VEC2_BASE", "WAV2VEC2_LARGE", "WAV2VEC2_LARGE_LV60K", "WAV2VEC2_ASR_BASE_10M", "WAV2VEC2_ASR_BASE_100H", "WAV2VEC2_ASR_BASE_960H", "WAV2VEC2_ASR_LARGE_10M", "WAV2VEC2_ASR_LARGE_100H", "WAV2VEC2_ASR_LARGE_960H", "WAV2VEC2_ASR_LARGE_LV60K_10M", "WAV2VEC2_ASR_LARGE_LV60K_100H", "WAV2VEC2_ASR_LARGE_LV60K_960H", "WAV2VEC2_XLSR53", "WAV2VEC2_XLSR_300M", "WAV2VEC2_XLSR_1B", "WAV2VEC2_XLSR_2B", "VOXPOPULI_ASR_BASE_10K_EN", "VOXPOPULI_ASR_BASE_10K_ES", "VOXPOPULI_ASR_BASE_10K_DE", "VOXPOPULI_ASR_BASE_10K_FR", "VOXPOPULI_ASR_BASE_10K_IT", "HUBERT_BASE", "HUBERT_LARGE", "HUBERT_XLARGE", "HUBERT_ASR_LARGE", "HUBERT_ASR_XLARGE", "WAVLM_BASE", "WAVLM_BASE_PLUS", "WAVLM_LARGE", "Tacotron2TTSBundle", "TACOTRON2_GRIFFINLIM_CHAR_LJSPEECH", "TACOTRON2_GRIFFINLIM_PHONE_LJSPEECH", "TACOTRON2_WAVERNN_CHAR_LJSPEECH", "TACOTRON2_WAVERNN_PHONE_LJSPEECH", "RNNTBundle", "EMFORMER_RNNT_BASE_LIBRISPEECH", "SourceSeparationBundle", "CONVTASNET_BASE_LIBRI2MIX", "HDEMUCS_HIGH_MUSDB_PLUS", "HDEMUCS_HIGH_MUSDB", "SQUIM_OBJECTIVE", "SQUIM_SUBJECTIVE", "SquimObjectiveBundle", "SquimSubjectiveBundle", ]
"""Argparser module for WorkerRuntime""" from jina.parsers.helper import KVAppendAction, add_arg_group from jina.parsers.orchestrate.runtimes.grpc_channel import ( mixin_grpc_channel_options_parser, ) from jina.parsers.orchestrate.runtimes.runtime import ( mixin_base_runtime_parser, mixin_raft_parser, ) def mixin_worker_runtime_parser(parser): """Mixing in arguments required by :class:`WorkerRuntime` into the given parser. :param parser: the parser instance to which we add arguments """ gp = add_arg_group(parser, title='WorkerRuntime') from jina.constants import __default_executor__ gp.add_argument( '--uses', type=str, default=__default_executor__, help=''' The config of the executor, it could be one of the followings: * the string literal of an Executor class name * an Executor YAML file (.yml, .yaml, .jaml) * a Jina Hub Executor (must start with `jinahub://` or `jinahub+docker://`) * a docker image (must start with `docker://`) * the string literal of a YAML config (must start with `!` or `jtype: `) * the string literal of a JSON config When use it under Python, one can use the following values additionally: - a Python dict that represents the config - a text file stream has `.read()` interface ''', ) gp.add_argument( '--uses-with', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `with` configuration in `uses` ''', ) gp.add_argument( '--uses-metas', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `metas` configuration in `uses` ''', ) gp.add_argument( '--uses-requests', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `requests` configuration in `uses` ''', ) gp.add_argument( '--uses-dynamic-batching', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `dynamic_batching` configuration in `uses` ''', ) gp.add_argument( '--py-modules', type=str, nargs='*', metavar='PATH', help=''' The customized python modules need to be imported before loading the executor Note that the recommended way is to only import a single module - a simple python file, if your executor can be defined in a single file, or an ``__init__.py`` file if you have multiple files, which should be structured as a python package. For more details, please see the `Executor cookbook <https://jina.ai/serve/concepts/executor/executor-files/>`__ ''', ) gp.add_argument( '--output-array-type', type=str, default=None, help=''' The type of array `tensor` and `embedding` will be serialized to. Supports the same types as `docarray.to_protobuf(.., ndarray_type=...)`, which can be found `here <https://docarray.jina.ai/fundamentals/document/serialization/#from-to-protobuf>`. Defaults to retaining whatever type is returned by the Executor. ''', ) gp.add_argument( '--exit-on-exceptions', type=str, default=[], nargs='*', help='List of exceptions that will cause the Executor to shut down.', ) gp.add_argument( '--no-reduce', '--disable-reduce', action='store_true', default=False, help='Disable the built-in reduction mechanism. Set this if the reduction is to be handled by the Executor ' 'itself by operating on a `docs_matrix` or `docs_map`', ) gp.add_argument( '--allow-concurrent', action='store_true', default=False, help='Allow concurrent requests to be processed by the Executor. This is only recommended if the Executor is thread-safe.', ) mixin_base_runtime_parser(gp) mixin_raft_parser(gp) mixin_grpc_channel_options_parser(gp)
"""Argparser module for WorkerRuntime""" from jina.parsers.helper import KVAppendAction, add_arg_group from jina.parsers.orchestrate.runtimes.grpc_channel import ( mixin_grpc_channel_options_parser, ) from jina.parsers.orchestrate.runtimes.runtime import ( mixin_base_runtime_parser, mixin_raft_parser, ) def mixin_worker_runtime_parser(parser): """Mixing in arguments required by :class:`WorkerRuntime` into the given parser. :param parser: the parser instance to which we add arguments """ gp = add_arg_group(parser, title='WorkerRuntime') from jina.constants import __default_executor__ gp.add_argument( '--uses', type=str, default=__default_executor__, help=''' The config of the executor, it could be one of the followings: * the string literal of an Executor class name * an Executor YAML file (.yml, .yaml, .jaml) * a Jina Hub Executor (must start with `jinahub://` or `jinahub+docker://`) * a docker image (must start with `docker://`) * the string literal of a YAML config (must start with `!` or `jtype: `) * the string literal of a JSON config When use it under Python, one can use the following values additionally: - a Python dict that represents the config - a text file stream has `.read()` interface ''', ) gp.add_argument( '--uses-with', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `with` configuration in `uses` ''', ) gp.add_argument( '--uses-metas', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `metas` configuration in `uses` ''', ) gp.add_argument( '--uses-requests', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `requests` configuration in `uses` ''', ) gp.add_argument( '--uses-dynamic-batching', action=KVAppendAction, metavar='KEY: VALUE', nargs='*', help=''' Dictionary of keyword arguments that will override the `dynamic_batching` configuration in `uses` ''', ) gp.add_argument( '--py-modules', type=str, nargs='*', metavar='PATH', help=''' The customized python modules need to be imported before loading the executor Note that the recommended way is to only import a single module - a simple python file, if your executor can be defined in a single file, or an ``__init__.py`` file if you have multiple files, which should be structured as a python package. For more details, please see the `Executor cookbook <https://docs.jina.ai/concepts/executor/executor-files/>`__ ''', ) gp.add_argument( '--output-array-type', type=str, default=None, help=''' The type of array `tensor` and `embedding` will be serialized to. Supports the same types as `docarray.to_protobuf(.., ndarray_type=...)`, which can be found `here <https://docarray.jina.ai/fundamentals/document/serialization/#from-to-protobuf>`. Defaults to retaining whatever type is returned by the Executor. ''', ) gp.add_argument( '--exit-on-exceptions', type=str, default=[], nargs='*', help='List of exceptions that will cause the Executor to shut down.', ) gp.add_argument( '--no-reduce', '--disable-reduce', action='store_true', default=False, help='Disable the built-in reduction mechanism. Set this if the reduction is to be handled by the Executor ' 'itself by operating on a `docs_matrix` or `docs_map`', ) gp.add_argument( '--allow-concurrent', action='store_true', default=False, help='Allow concurrent requests to be processed by the Executor. This is only recommended if the Executor is thread-safe.', ) mixin_base_runtime_parser(gp) mixin_raft_parser(gp) mixin_grpc_channel_options_parser(gp)
""" Official evaluation script for ReCoRD v1.0. (Some functions are adopted from the SQuAD evaluation script.) """ import argparse import json import re import string import sys from collections import Counter def normalize_answer(s): """Lower text and remove punctuation, articles and extra whitespace.""" def remove_articles(text): return re.sub(r"\b(a|an|the)\b", " ", text) def white_space_fix(text): return " ".join(text.split()) def remove_punc(text): exclude = set(string.punctuation) return "".join(ch for ch in text if ch not in exclude) def lower(text): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(s)))) def f1_score(prediction, ground_truth): prediction_tokens = normalize_answer(prediction).split() ground_truth_tokens = normalize_answer(ground_truth).split() common = Counter(prediction_tokens) & Counter(ground_truth_tokens) num_same = sum(common.values()) if num_same == 0: return 0 precision = 1.0 * num_same / len(prediction_tokens) recall = 1.0 * num_same / len(ground_truth_tokens) f1 = (2 * precision * recall) / (precision + recall) return f1 def exact_match_score(prediction, ground_truth): return normalize_answer(prediction) == normalize_answer(ground_truth) def metric_max_over_ground_truths(metric_fn, prediction, ground_truths): scores_for_ground_truths = [] for ground_truth in ground_truths: score = metric_fn(prediction, ground_truth) scores_for_ground_truths.append(score) return max(scores_for_ground_truths) def evaluate(dataset, predictions): f1 = exact_match = total = 0 correct_ids = [] for passage in dataset: for qa in passage["qas"]: total += 1 if qa["id"] not in predictions: message = f'Unanswered question {qa["id"]} will receive score 0.' print(message, file=sys.stderr) continue ground_truths = [x["text"] for x in qa["answers"]] prediction = predictions[qa["id"]] _exact_match = metric_max_over_ground_truths(exact_match_score, prediction, ground_truths) if int(_exact_match) == 1: correct_ids.append(qa["id"]) exact_match += _exact_match f1 += metric_max_over_ground_truths(f1_score, prediction, ground_truths) exact_match = exact_match / total f1 = f1 / total return {"exact_match": exact_match, "f1": f1}, correct_ids if __name__ == "__main__": expected_version = "1.0" parser = argparse.ArgumentParser("Official evaluation script for ReCoRD v1.0.") parser.add_argument("data_file", help="The dataset file in JSON format.") parser.add_argument("pred_file", help="The model prediction file in JSON format.") parser.add_argument("--output_correct_ids", action="store_true", help="Output the correctly answered query IDs.") args = parser.parse_args() with open(args.data_file) as data_file: dataset_json = json.load(data_file) if dataset_json["version"] != expected_version: print( f'Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json["version"]}', file=sys.stderr, ) dataset = dataset_json["data"] with open(args.pred_file) as pred_file: predictions = json.load(pred_file) metrics, correct_ids = evaluate(dataset, predictions) if args.output_correct_ids: print(f"Output {len(correct_ids)} correctly answered question IDs.") with open("correct_ids.json", "w") as f: json.dump(correct_ids, f)
""" Official evaluation script for ReCoRD v1.0. (Some functions are adopted from the SQuAD evaluation script.) """ import argparse import json import re import string import sys from collections import Counter def normalize_answer(s): """Lower text and remove punctuation, articles and extra whitespace.""" def remove_articles(text): return re.sub(r"\b(a|an|the)\b", " ", text) def white_space_fix(text): return " ".join(text.split()) def remove_punc(text): exclude = set(string.punctuation) return "".join(ch for ch in text if ch not in exclude) def lower(text): return text.lower() return white_space_fix(remove_articles(remove_punc(lower(s)))) def f1_score(prediction, ground_truth): prediction_tokens = normalize_answer(prediction).split() ground_truth_tokens = normalize_answer(ground_truth).split() common = Counter(prediction_tokens) & Counter(ground_truth_tokens) num_same = sum(common.values()) if num_same == 0: return 0 precision = 1.0 * num_same / len(prediction_tokens) recall = 1.0 * num_same / len(ground_truth_tokens) f1 = (2 * precision * recall) / (precision + recall) return f1 def exact_match_score(prediction, ground_truth): return normalize_answer(prediction) == normalize_answer(ground_truth) def metric_max_over_ground_truths(metric_fn, prediction, ground_truths): scores_for_ground_truths = [] for ground_truth in ground_truths: score = metric_fn(prediction, ground_truth) scores_for_ground_truths.append(score) return max(scores_for_ground_truths) def evaluate(dataset, predictions): f1 = exact_match = total = 0 correct_ids = [] for passage in dataset: for qa in passage["qas"]: total += 1 if qa["id"] not in predictions: message = f'Unanswered question {qa["id"]} will receive score 0.' print(message, file=sys.stderr) continue ground_truths = [x["text"] for x in qa["answers"]] prediction = predictions[qa["id"]] _exact_match = metric_max_over_ground_truths(exact_match_score, prediction, ground_truths) if int(_exact_match) == 1: correct_ids.append(qa["id"]) exact_match += _exact_match f1 += metric_max_over_ground_truths(f1_score, prediction, ground_truths) exact_match = exact_match / total f1 = f1 / total return {"exact_match": exact_match, "f1": f1}, correct_ids if __name__ == "__main__": expected_version = "1.0" parser = argparse.ArgumentParser("Official evaluation script for ReCoRD v1.0.") parser.add_argument("data_file", help="The dataset file in JSON format.") parser.add_argument("pred_file", help="The model prediction file in JSON format.") parser.add_argument("--output_correct_ids", action="store_true", help="Output the correctly answered query IDs.") args = parser.parse_args() with open(args.data_file) as data_file: dataset_json = json.load(data_file) if dataset_json["version"] != expected_version: print( f'Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json["version"]}', file=sys.stderr, ) dataset = dataset_json["data"] with open(args.pred_file) as pred_file: predictions = json.load(pred_file) metrics, correct_ids = evaluate(dataset, predictions) if args.output_correct_ids: print(f"Output {len(correct_ids)} correctly answered question IDs.") with open("correct_ids.json", "w") as f: json.dump(correct_ids, f)
import re import unicodedata import regex # non-ASCII letters that are not separated by "NFKD" normalization ADDITIONAL_DIACRITICS = { "œ": "oe", "Œ": "OE", "ø": "o", "Ø": "O", "æ": "ae", "Æ": "AE", "ß": "ss", "ẞ": "SS", "đ": "d", "Đ": "D", "ð": "d", "Ð": "D", "þ": "th", "Þ": "th", "ł": "l", "Ł": "L", } def remove_symbols_and_diacritics(s: str, keep=""): """ Replace any other markers, symbols, and punctuations with a space, and drop any diacritics (category 'Mn' and some manual mappings) """ return "".join( ( c if c in keep else ( ADDITIONAL_DIACRITICS[c] if c in ADDITIONAL_DIACRITICS else ( "" if unicodedata.category(c) == "Mn" else " " if unicodedata.category(c)[0] in "MSP" else c ) ) ) for c in unicodedata.normalize("NFKD", s) ) def remove_symbols(s: str): """ Replace any other markers, symbols, punctuations with a space, keeping diacritics """ return "".join( " " if unicodedata.category(c)[0] in "MSP" else c for c in unicodedata.normalize("NFKC", s) ) class BasicTextNormalizer: def __init__(self, remove_diacritics: bool = False, split_letters: bool = False): self.clean = ( remove_symbols_and_diacritics if remove_diacritics else remove_symbols ) self.split_letters = split_letters def __call__(self, s: str): s = s.lower() s = re.sub(r"[<\[][^>\]]*[>\]]", "", s) # remove words between brackets s = re.sub(r"\(([^)]+?)\)", "", s) # remove words between parenthesis s = self.clean(s).lower() if self.split_letters: s = " ".join(regex.findall(r"\X", s, regex.U)) s = re.sub( r"\s+", " ", s ) # replace any successive whitespace characters with a space return s
import re import unicodedata import regex # non-ASCII letters that are not separated by "NFKD" normalization ADDITIONAL_DIACRITICS = { "œ": "oe", "Œ": "OE", "ø": "o", "Ø": "O", "æ": "ae", "Æ": "AE", "ß": "ss", "ẞ": "SS", "đ": "d", "Đ": "D", "ð": "d", "Ð": "D", "þ": "th", "Þ": "th", "ł": "l", "Ł": "L", } def remove_symbols_and_diacritics(s: str, keep=""): """ Replace any other markers, symbols, and punctuations with a space, and drop any diacritics (category 'Mn' and some manual mappings) """ return "".join( c if c in keep else ADDITIONAL_DIACRITICS[c] if c in ADDITIONAL_DIACRITICS else "" if unicodedata.category(c) == "Mn" else " " if unicodedata.category(c)[0] in "MSP" else c for c in unicodedata.normalize("NFKD", s) ) def remove_symbols(s: str): """ Replace any other markers, symbols, punctuations with a space, keeping diacritics """ return "".join( " " if unicodedata.category(c)[0] in "MSP" else c for c in unicodedata.normalize("NFKC", s) ) class BasicTextNormalizer: def __init__(self, remove_diacritics: bool = False, split_letters: bool = False): self.clean = ( remove_symbols_and_diacritics if remove_diacritics else remove_symbols ) self.split_letters = split_letters def __call__(self, s: str): s = s.lower() s = re.sub(r"[<\[][^>\]]*[>\]]", "", s) # remove words between brackets s = re.sub(r"\(([^)]+?)\)", "", s) # remove words between parenthesis s = self.clean(s).lower() if self.split_letters: s = " ".join(regex.findall(r"\X", s, regex.U)) s = re.sub( r"\s+", " ", s ) # replace any successive whitespace characters with a space return s
# Copyright (c) OpenMMLab. All rights reserved. """Collecting some commonly used type hint in mmdetection.""" from typing import List, Optional, Sequence, Tuple, Union from mmengine.config import ConfigDict from mmengine.structures import InstanceData, PixelData # TODO: Need to avoid circular import with assigner and sampler # Type hint of config data ConfigType = Union[ConfigDict, dict] OptConfigType = Optional[ConfigType] # Type hint of one or more config data MultiConfig = Union[ConfigType, List[ConfigType]] OptMultiConfig = Optional[MultiConfig] InstanceList = List[InstanceData] OptInstanceList = Optional[InstanceList] PixelList = List[PixelData] OptPixelList = Optional[PixelList] RangeType = Sequence[Tuple[int, int]]
# Copyright (c) OpenMMLab. All rights reserved. """Collecting some commonly used type hint in mmdetection.""" from typing import List, Optional, Sequence, Tuple, Union from mmengine.config import ConfigDict from mmengine.data import InstanceData, PixelData # TODO: Need to avoid circular import with assigner and sampler # Type hint of config data ConfigType = Union[ConfigDict, dict] OptConfigType = Optional[ConfigType] # Type hint of one or more config data MultiConfig = Union[ConfigType, List[ConfigType]] OptMultiConfig = Optional[MultiConfig] InstanceList = List[InstanceData] OptInstanceList = Optional[InstanceList] PixelList = List[PixelData] OptPixelList = Optional[PixelList] RangeType = Sequence[Tuple[int, int]]
""" ===================================== How to write your own Datapoint class ===================================== This guide is intended for downstream library maintainers. We explain how to write your own datapoint class, and how to make it compatible with the built-in Torchvision v2 transforms. Before continuing, make sure you have read :ref:`sphx_glr_auto_examples_plot_datapoints.py`. """ # %% import torch import torchvision # We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that # some APIs may slightly change in the future torchvision.disable_beta_transforms_warning() from torchvision import datapoints from torchvision.transforms import v2 # %% # We will create a very simple class that just inherits from the base # :class:`~torchvision.datapoints.Datapoint` class. It will be enough to cover # what you need to know to implement your more elaborate uses-cases. If you need # to create a class that carries meta-data, take a look at how the # :class:`~torchvision.datapoints.BoundingBoxes` class is `implemented # <https://github.com/pytorch/vision/blob/main/torchvision/datapoints/_bounding_box.py>`_. class MyDatapoint(datapoints.Datapoint): pass my_dp = MyDatapoint([1, 2, 3]) my_dp # %% # Now that we have defined our custom Datapoint class, we want it to be # compatible with the built-in torchvision transforms, and the functional API. # For that, we need to implement a kernel which performs the core of the # transformation, and then "hook" it to the functional that we want to support # via :func:`~torchvision.transforms.v2.functional.register_kernel`. # # We illustrate this process below: we create a kernel for the "horizontal flip" # operation of our MyDatapoint class, and register it to the functional API. from torchvision.transforms.v2 import functional as F @F.register_kernel(functional="hflip", datapoint_cls=MyDatapoint) def hflip_my_datapoint(my_dp, *args, **kwargs): print("Flipping!") out = my_dp.flip(-1) return MyDatapoint.wrap_like(my_dp, out) # %% # To understand why ``wrap_like`` is used, see # :ref:`datapoint_unwrapping_behaviour`. Ignore the ``*args, **kwargs`` for now, # we will explain it below in :ref:`param_forwarding`. # # .. note:: # # In our call to ``register_kernel`` above we used a string # ``functional="hflip"`` to refer to the functional we want to hook into. We # could also have used the functional *itself*, i.e. # ``@register_kernel(functional=F.hflip, ...)``. # # The functionals that you can be hooked into are the ones in # ``torchvision.transforms.v2.functional`` and they are documented in # :ref:`functional_transforms`. # # Now that we have registered our kernel, we can call the functional API on a # ``MyDatapoint`` instance: my_dp = MyDatapoint(torch.rand(3, 256, 256)) _ = F.hflip(my_dp) # %% # And we can also use the # :class:`~torchvision.transforms.v2.RandomHorizontalFlip` transform, since it relies on :func:`~torchvision.transforms.v2.functional.hflip` internally: t = v2.RandomHorizontalFlip(p=1) _ = t(my_dp) # %% # .. note:: # # We cannot register a kernel for a transform class, we can only register a # kernel for a **functional**. The reason we can't register a transform # class is because one transform may internally rely on more than one # functional, so in general we can't register a single kernel for a given # class. # # .. _param_forwarding: # # Parameter forwarding, and ensuring future compatibility of your kernels # ----------------------------------------------------------------------- # # The functional API that you're hooking into is public and therefore # **backward** compatible: we guarantee that the parameters of these functionals # won't be removed or renamed without a proper deprecation cycle. However, we # don't guarantee **forward** compatibility, and we may add new parameters in # the future. # # Imagine that in a future version, Torchvision adds a new ``inplace`` parameter # to its :func:`~torchvision.transforms.v2.functional.hflip` functional. If you # already defined and registered your own kernel as def hflip_my_datapoint(my_dp): # noqa print("Flipping!") out = my_dp.flip(-1) return MyDatapoint.wrap_like(my_dp, out) # %% # then calling ``F.hflip(my_dp)`` will **fail**, because ``hflip`` will try to # pass the new ``inplace`` parameter to your kernel, but your kernel doesn't # accept it. # # For this reason, we recommend to always define your kernels with # ``*args, **kwargs`` in their signature, as done above. This way, your kernel # will be able to accept any new parameter that we may add in the future. # (Technically, adding `**kwargs` only should be enough).
""" ===================================== How to write your own Datapoint class ===================================== This guide is intended for downstream library maintainers. We explain how to write your own datapoint class, and how to make it compatible with the built-in Torchvision v2 transforms. Before continuing, make sure you have read :ref:`sphx_glr_auto_examples_plot_datapoints.py`. """ # %% import torch import torchvision # We are using BETA APIs, so we deactivate the associated warning, thereby acknowledging that # some APIs may slightly change in the future torchvision.disable_beta_transforms_warning() from torchvision import datapoints from torchvision.transforms import v2 # %% # We will create a very simple class that just inherits from the base # :class:`~torchvision.datapoints.Datapoint` class. It will be enough to cover # what you need to know to implement your more elaborate uses-cases. If you need # to create a class that carries meta-data, take a look at how the # :class:`~torchvision.datapoints.BoundingBoxes` class is `implemented # <https://github.com/pytorch/vision/blob/main/torchvision/datapoints/_bounding_box.py>`_. class MyDatapoint(datapoints.Datapoint): pass my_dp = MyDatapoint([1, 2, 3]) my_dp # %% # Now that we have defined our custom Datapoint class, we want it to be # compatible with the built-in torchvision transforms, and the functional API. # For that, we need to implement a kernel which performs the core of the # transformation, and then "hook" it to the functional that we want to support # via :func:`~torchvision.transforms.v2.functional.register_kernel`. # # We illustrate this process below: we create a kernel for the "horizontal flip" # operation of our MyDatapoint class, and register it to the functional API. from torchvision.transforms.v2 import functional as F @F.register_kernel(dispatcher="hflip", datapoint_cls=MyDatapoint) def hflip_my_datapoint(my_dp, *args, **kwargs): print("Flipping!") out = my_dp.flip(-1) return MyDatapoint.wrap_like(my_dp, out) # %% # To understand why ``wrap_like`` is used, see # :ref:`datapoint_unwrapping_behaviour`. Ignore the ``*args, **kwargs`` for now, # we will explain it below in :ref:`param_forwarding`. # # .. note:: # # In our call to ``register_kernel`` above we used a string # ``dispatcher="hflip"`` to refer to the functional we want to hook into. We # could also have used the functional *itself*, i.e. # ``@register_kernel(dispatcher=F.hflip, ...)``. # # The functionals that you can be hooked into are the ones in # ``torchvision.transforms.v2.functional`` and they are documented in # :ref:`functional_transforms`. # # Now that we have registered our kernel, we can call the functional API on a # ``MyDatapoint`` instance: my_dp = MyDatapoint(torch.rand(3, 256, 256)) _ = F.hflip(my_dp) # %% # And we can also use the # :class:`~torchvision.transforms.v2.RandomHorizontalFlip` transform, since it relies on :func:`~torchvision.transforms.v2.functional.hflip` internally: t = v2.RandomHorizontalFlip(p=1) _ = t(my_dp) # %% # .. note:: # # We cannot register a kernel for a transform class, we can only register a # kernel for a **functional**. The reason we can't register a transform # class is because one transform may internally rely on more than one # functional, so in general we can't register a single kernel for a given # class. # # .. _param_forwarding: # # Parameter forwarding, and ensuring future compatibility of your kernels # ----------------------------------------------------------------------- # # The functional API that you're hooking into is public and therefore # **backward** compatible: we guarantee that the parameters of these functionals # won't be removed or renamed without a proper deprecation cycle. However, we # don't guarantee **forward** compatibility, and we may add new parameters in # the future. # # Imagine that in a future version, Torchvision adds a new ``inplace`` parameter # to its :func:`~torchvision.transforms.v2.functional.hflip` functional. If you # already defined and registered your own kernel as def hflip_my_datapoint(my_dp): # noqa print("Flipping!") out = my_dp.flip(-1) return MyDatapoint.wrap_like(my_dp, out) # %% # then calling ``F.hflip(my_dp)`` will **fail**, because ``hflip`` will try to # pass the new ``inplace`` parameter to your kernel, but your kernel doesn't # accept it. # # For this reason, we recommend to always define your kernels with # ``*args, **kwargs`` in their signature, as done above. This way, your kernel # will be able to accept any new parameter that we may add in the future. # (Technically, adding `**kwargs` only should be enough).
import os from source_separation.utils.dataset import wsj0mix from torchaudio_unittest.common_utils import ( get_whitenoise, normalize_wav, save_wav, TempDirMixin, TorchaudioTestCase, ) _FILENAMES = [ "012c0207_1.9952_01cc0202_-1.9952.wav", "01co0302_1.63_014c020q_-1.63.wav", "01do0316_0.24011_205a0104_-0.24011.wav", "01lc020x_1.1301_027o030r_-1.1301.wav", "01mc0202_0.34056_205o0106_-0.34056.wav", "01nc020t_0.53821_018o030w_-0.53821.wav", "01po030f_2.2136_40ko031a_-2.2136.wav", "01ra010o_2.4098_403a010f_-2.4098.wav", "01xo030b_0.22377_016o031a_-0.22377.wav", "02ac020x_0.68566_01ec020b_-0.68566.wav", "20co010m_0.82801_019c0212_-0.82801.wav", "20da010u_1.2483_017c0211_-1.2483.wav", "20oo010d_1.0631_01ic020s_-1.0631.wav", "20sc0107_2.0222_20fo010h_-2.0222.wav", "20tc010f_0.051456_404a0110_-0.051456.wav", "407c0214_1.1712_02ca0113_-1.1712.wav", "40ao030w_2.4697_20vc010a_-2.4697.wav", "40pa0101_1.1087_40ea0107_-1.1087.wav", ] def _mock_dataset(root_dir, num_speaker): dirnames = ["mix"] + [f"s{i+1}" for i in range(num_speaker)] for dirname in dirnames: os.makedirs(os.path.join(root_dir, dirname), exist_ok=True) seed = 0 sample_rate = 8000 expected = [] for filename in _FILENAMES: mix = None src = [] for dirname in dirnames: waveform = get_whitenoise(sample_rate=8000, duration=1, n_channels=1, dtype="int16", seed=seed) seed += 1 path = os.path.join(root_dir, dirname, filename) save_wav(path, waveform, sample_rate) waveform = normalize_wav(waveform) if dirname == "mix": mix = waveform else: src.append(waveform) expected.append((sample_rate, mix, src)) return expected class TestWSJ0Mix2(TempDirMixin, TorchaudioTestCase): backend = "default" root_dir = None expected = None @classmethod def setUpClass(cls): cls.root_dir = cls.get_base_temp_dir() cls.expected = _mock_dataset(cls.root_dir, 2) def test_wsj0mix(self): dataset = wsj0mix.WSJ0Mix(self.root_dir, num_speakers=2, sample_rate=8000) n_ite = 0 for i, sample in enumerate(dataset): (_, sample_mix, sample_src) = sample (_, expected_mix, expected_src) = self.expected[i] self.assertEqual(sample_mix, expected_mix, atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[0], expected_src[0], atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[1], expected_src[1], atol=5e-5, rtol=1e-8) n_ite += 1 assert n_ite == len(self.expected) class TestWSJ0Mix3(TempDirMixin, TorchaudioTestCase): backend = "default" root_dir = None expected = None @classmethod def setUpClass(cls): cls.root_dir = cls.get_base_temp_dir() cls.expected = _mock_dataset(cls.root_dir, 3) def test_wsj0mix(self): dataset = wsj0mix.WSJ0Mix(self.root_dir, num_speakers=3, sample_rate=8000) n_ite = 0 for i, sample in enumerate(dataset): (_, sample_mix, sample_src) = sample (_, expected_mix, expected_src) = self.expected[i] self.assertEqual(sample_mix, expected_mix, atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[0], expected_src[0], atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[1], expected_src[1], atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[2], expected_src[2], atol=5e-5, rtol=1e-8) n_ite += 1 assert n_ite == len(self.expected)
import os from source_separation.utils.dataset import wsj0mix from torchaudio_unittest.common_utils import ( TempDirMixin, TorchaudioTestCase, get_whitenoise, save_wav, normalize_wav, ) _FILENAMES = [ "012c0207_1.9952_01cc0202_-1.9952.wav", "01co0302_1.63_014c020q_-1.63.wav", "01do0316_0.24011_205a0104_-0.24011.wav", "01lc020x_1.1301_027o030r_-1.1301.wav", "01mc0202_0.34056_205o0106_-0.34056.wav", "01nc020t_0.53821_018o030w_-0.53821.wav", "01po030f_2.2136_40ko031a_-2.2136.wav", "01ra010o_2.4098_403a010f_-2.4098.wav", "01xo030b_0.22377_016o031a_-0.22377.wav", "02ac020x_0.68566_01ec020b_-0.68566.wav", "20co010m_0.82801_019c0212_-0.82801.wav", "20da010u_1.2483_017c0211_-1.2483.wav", "20oo010d_1.0631_01ic020s_-1.0631.wav", "20sc0107_2.0222_20fo010h_-2.0222.wav", "20tc010f_0.051456_404a0110_-0.051456.wav", "407c0214_1.1712_02ca0113_-1.1712.wav", "40ao030w_2.4697_20vc010a_-2.4697.wav", "40pa0101_1.1087_40ea0107_-1.1087.wav", ] def _mock_dataset(root_dir, num_speaker): dirnames = ["mix"] + [f"s{i+1}" for i in range(num_speaker)] for dirname in dirnames: os.makedirs(os.path.join(root_dir, dirname), exist_ok=True) seed = 0 sample_rate = 8000 expected = [] for filename in _FILENAMES: mix = None src = [] for dirname in dirnames: waveform = get_whitenoise(sample_rate=8000, duration=1, n_channels=1, dtype="int16", seed=seed) seed += 1 path = os.path.join(root_dir, dirname, filename) save_wav(path, waveform, sample_rate) waveform = normalize_wav(waveform) if dirname == "mix": mix = waveform else: src.append(waveform) expected.append((sample_rate, mix, src)) return expected class TestWSJ0Mix2(TempDirMixin, TorchaudioTestCase): backend = "default" root_dir = None expected = None @classmethod def setUpClass(cls): cls.root_dir = cls.get_base_temp_dir() cls.expected = _mock_dataset(cls.root_dir, 2) def test_wsj0mix(self): dataset = wsj0mix.WSJ0Mix(self.root_dir, num_speakers=2, sample_rate=8000) n_ite = 0 for i, sample in enumerate(dataset): (_, sample_mix, sample_src) = sample (_, expected_mix, expected_src) = self.expected[i] self.assertEqual(sample_mix, expected_mix, atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[0], expected_src[0], atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[1], expected_src[1], atol=5e-5, rtol=1e-8) n_ite += 1 assert n_ite == len(self.expected) class TestWSJ0Mix3(TempDirMixin, TorchaudioTestCase): backend = "default" root_dir = None expected = None @classmethod def setUpClass(cls): cls.root_dir = cls.get_base_temp_dir() cls.expected = _mock_dataset(cls.root_dir, 3) def test_wsj0mix(self): dataset = wsj0mix.WSJ0Mix(self.root_dir, num_speakers=3, sample_rate=8000) n_ite = 0 for i, sample in enumerate(dataset): (_, sample_mix, sample_src) = sample (_, expected_mix, expected_src) = self.expected[i] self.assertEqual(sample_mix, expected_mix, atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[0], expected_src[0], atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[1], expected_src[1], atol=5e-5, rtol=1e-8) self.assertEqual(sample_src[2], expected_src[2], atol=5e-5, rtol=1e-8) n_ite += 1 assert n_ite == len(self.expected)
from langchain_core.agents import AgentActionMessageLog from langchain_core.messages import AIMessage, FunctionMessage from langchain.agents.format_scratchpad.openai_functions import ( format_to_openai_function_messages, ) def test_calls_convert_agent_action_to_messages() -> None: additional_kwargs1 = { "function_call": { "name": "tool1", "arguments": "input1", }, } message1 = AIMessage(content="", additional_kwargs=additional_kwargs1) action1 = AgentActionMessageLog( tool="tool1", tool_input="input1", log="log1", message_log=[message1], ) additional_kwargs2 = { "function_call": { "name": "tool2", "arguments": "input2", }, } message2 = AIMessage(content="", additional_kwargs=additional_kwargs2) action2 = AgentActionMessageLog( tool="tool2", tool_input="input2", log="log2", message_log=[message2], ) additional_kwargs3 = { "function_call": { "name": "tool3", "arguments": "input3", }, } message3 = AIMessage(content="", additional_kwargs=additional_kwargs3) action3 = AgentActionMessageLog( tool="tool3", tool_input="input3", log="log3", message_log=[message3], ) intermediate_steps = [ (action1, "observation1"), (action2, "observation2"), (action3, "observation3"), ] expected_messages = [ message1, FunctionMessage(name="tool1", content="observation1"), message2, FunctionMessage(name="tool2", content="observation2"), message3, FunctionMessage(name="tool3", content="observation3"), ] output = format_to_openai_function_messages(intermediate_steps) assert output == expected_messages def test_handles_empty_input_list() -> None: output = format_to_openai_function_messages([]) assert output == []
from langchain_core.agents import AgentActionMessageLog from langchain_core.messages import AIMessage, FunctionMessage from langchain.agents.format_scratchpad.openai_functions import ( format_to_openai_function_messages, ) def test_calls_convert_agent_action_to_messages() -> None: additional_kwargs1 = { "function_call": { "name": "tool1", "arguments": "input1", } } message1 = AIMessage(content="", additional_kwargs=additional_kwargs1) action1 = AgentActionMessageLog( tool="tool1", tool_input="input1", log="log1", message_log=[message1] ) additional_kwargs2 = { "function_call": { "name": "tool2", "arguments": "input2", } } message2 = AIMessage(content="", additional_kwargs=additional_kwargs2) action2 = AgentActionMessageLog( tool="tool2", tool_input="input2", log="log2", message_log=[message2] ) additional_kwargs3 = { "function_call": { "name": "tool3", "arguments": "input3", } } message3 = AIMessage(content="", additional_kwargs=additional_kwargs3) action3 = AgentActionMessageLog( tool="tool3", tool_input="input3", log="log3", message_log=[message3] ) intermediate_steps = [ (action1, "observation1"), (action2, "observation2"), (action3, "observation3"), ] expected_messages = [ message1, FunctionMessage(name="tool1", content="observation1"), message2, FunctionMessage(name="tool2", content="observation2"), message3, FunctionMessage(name="tool3", content="observation3"), ] output = format_to_openai_function_messages(intermediate_steps) assert output == expected_messages def test_handles_empty_input_list() -> None: output = format_to_openai_function_messages([]) assert output == []
from __future__ import annotations import re import pytest from sentence_transformers import SentenceTransformer from sentence_transformers.evaluation import NanoBEIREvaluator from sentence_transformers.util import is_datasets_available if not is_datasets_available(): pytest.skip( reason="Datasets are not installed. Please install `datasets` with `pip install datasets`", allow_module_level=True, ) def test_nanobeir_evaluator(): """Tests that the NanoBERTEvaluator can be loaded and produces expected metrics""" datasets = ["QuoraRetrieval", "MSMARCO"] query_prompts = { "QuoraRetrieval": "Instruct: Given a question, retrieve questions that are semantically equivalent to the given question\\nQuery: ", "MSMARCO": "Instruct: Given a web search query, retrieve relevant passages that answer the query\\nQuery: ", } model = SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") evaluator = NanoBEIREvaluator( dataset_names=datasets, query_prompts=query_prompts, ) results = evaluator(model) assert len(results) > 0 assert all(isinstance(results[metric], float) for metric in results) def test_nanobeir_evaluator_with_invalid_dataset(): """Test that NanoBEIREvaluator raises an error for invalid dataset names.""" invalid_datasets = ["invalidDataset"] with pytest.raises( ValueError, match=re.escape( r"Dataset(s) ['invalidDataset'] not found in the NanoBEIR collection. " r"Valid dataset names are: ['climatefever', 'dbpedia', 'fever', 'fiqa2018', 'hotpotqa', 'msmarco', 'nfcorpus', 'nq', 'quoraretrieval', 'scidocs', 'arguana', 'scifact', 'touche2020']" ), ): NanoBEIREvaluator(dataset_names=invalid_datasets) def test_nanobeir_evaluator_empty_inputs(): """Test that NanoBEIREvaluator behaves correctly with empty datasets.""" with pytest.raises(ValueError, match="dataset_names cannot be empty. Use None to evaluate on all datasets."): NanoBEIREvaluator(dataset_names=[])
from __future__ import annotations import re import pytest from sentence_transformers import SentenceTransformer from sentence_transformers.evaluation import NanoBEIREvaluator def test_nanobeir_evaluator(): """Tests that the NanoBERTEvaluator can be loaded and produces expected metrics""" datasets = ["QuoraRetrieval", "MSMARCO"] query_prompts = { "QuoraRetrieval": "Instruct: Given a question, retrieve questions that are semantically equivalent to the given question\\nQuery: ", "MSMARCO": "Instruct: Given a web search query, retrieve relevant passages that answer the query\\nQuery: ", } model = SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") evaluator = NanoBEIREvaluator( dataset_names=datasets, query_prompts=query_prompts, ) results = evaluator(model) assert len(results) > 0 assert all(isinstance(results[metric], float) for metric in results) def test_nanobeir_evaluator_with_invalid_dataset(): """Test that NanoBEIREvaluator raises an error for invalid dataset names.""" invalid_datasets = ["invalidDataset"] with pytest.raises( ValueError, match=re.escape( r"Dataset(s) ['invalidDataset'] not found in the NanoBEIR collection. " r"Valid dataset names are: ['climatefever', 'dbpedia', 'fever', 'fiqa2018', 'hotpotqa', 'msmarco', 'nfcorpus', 'nq', 'quoraretrieval', 'scidocs', 'arguana', 'scifact', 'touche2020']" ), ): NanoBEIREvaluator(dataset_names=invalid_datasets) def test_nanobeir_evaluator_empty_inputs(): """Test that NanoBEIREvaluator behaves correctly with empty datasets.""" with pytest.raises(ValueError, match="dataset_names cannot be empty. Use None to evaluate on all datasets."): NanoBEIREvaluator(dataset_names=[])
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Optional import torch from jina import DocumentArray, Executor, requests from .audio_clip.model import AudioCLIP class AudioCLIPTextEncoder(Executor): """ Encode text data with the AudioCLIP model """ def __init__( self, model_path: str = '.cache/AudioCLIP-Full-Training.pt', tokenizer_path: str = '.cache/bpe_simple_vocab_16e6.txt.gz', traversal_paths: str = 'r', batch_size: int = 32, device: str = 'cpu', download_model: bool = False, *args, **kwargs ): """ :param model_path: path to the pre-trained AudioCLIP model. :param traversal_paths: default traversal path (used if not specified in request's parameters) :param batch_size: default batch size (used if not specified in request's parameters) :param device: device that the model is on (should be "cpu", "cuda" or "cuda:X", where X is the index of the GPU on the machine) :param download_model: whether to download the model at start-up """ super().__init__(*args, **kwargs) if download_model: import os import subprocess root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) script_name = 'scripts/download_full.sh' if 'Partial' in model_path: script_name = 'scripts/download_partial.sh' subprocess.call(['sh', script_name], cwd=root_path) try: self.model = ( AudioCLIP( pretrained=model_path, bpe_path=tokenizer_path, ) .to(device) .eval() ) except FileNotFoundError: raise FileNotFoundError( 'Please download AudioCLIP model and set the `model_path` argument.' ) self.traversal_paths = traversal_paths self.batch_size = batch_size @requests def encode( self, docs: Optional[DocumentArray] = None, parameters: dict = {}, *args, **kwargs ) -> None: """ Method to create embeddings for documents by encoding their text. :param docs: A document array with documents to create embeddings for. Only the documents that have the ``text`` attribute will get embeddings. :param parameters: A dictionary that contains parameters to control encoding. The accepted keys are ``traversal_paths`` and ``batch_size`` - in their absence their corresponding default values are used. """ if not docs: return batch_generator = docs.traverse_flat( traversal_paths=parameters.get('traversal_paths', self.traversal_paths), filter_fn=lambda doc: len(doc.text) > 0, ).batch( batch_size=parameters.get('batch_size', self.batch_size), ) with torch.inference_mode(): for batch in batch_generator: embeddings = self.model.encode_text(text=[[doc.text] for doc in batch]) embeddings = embeddings.cpu().numpy() for idx, doc in enumerate(batch): doc.embedding = embeddings[idx]
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Iterable, Optional import torch from jina import DocumentArray, Executor, requests from .audio_clip.model import AudioCLIP class AudioCLIPTextEncoder(Executor): """ Encode text data with the AudioCLIP model """ def __init__( self, model_path: str = '.cache/AudioCLIP-Full-Training.pt', tokenizer_path: str = '.cache/bpe_simple_vocab_16e6.txt.gz', traversal_paths: Iterable[str] = ('r',), batch_size: int = 32, device: str = 'cpu', download_model: bool = True, *args, **kwargs ): """ :param model_path: path to the pre-trained AudioCLIP model. :param traversal_paths: default traversal path (used if not specified in request's parameters) :param batch_size: default batch size (used if not specified in request's parameters) :param device: device that the model is on (should be "cpu", "cuda" or "cuda:X", where X is the index of the GPU on the machine) """ super().__init__(*args, **kwargs) if download_model: import os import subprocess root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) subprocess.call(['sh', 'scripts/download_full.sh'], cwd=root_path) self.model = ( AudioCLIP( pretrained=model_path, bpe_path=tokenizer_path, ) .to(device) .eval() ) self.traversal_paths = traversal_paths self.batch_size = batch_size @requests def encode( self, docs: Optional[DocumentArray] = None, parameters: dict = {}, *args, **kwargs ) -> None: """ Method to create embeddings for documents by encoding their text. :param docs: A document array with documents to create embeddings for. Only the documents that have the ``text`` attribute will get embeddings. :param parameters: A dictionary that contains parameters to control encoding. The accepted keys are ``traversal_paths`` and ``batch_size`` - in their absence their corresponding default values are used. """ if not docs: return batch_generator = docs.traverse_flat( traversal_paths=parameters.get('traversal_paths', self.traversal_paths), filter_fn=lambda doc: len(doc.text)>0 ).batch( batch_size=parameters.get('batch_size', self.batch_size), ) with torch.inference_mode(): for batch in batch_generator: embeddings = self.model.encode_text(text=[[doc.text] for doc in batch]) embeddings = embeddings.cpu().numpy() for idx, doc in enumerate(batch): doc.embedding = embeddings[idx]
from typing import Any, Optional, Sequence from llama_index.core.evaluation.base import BaseEvaluator, EvaluationResult from llama_index.core.prompts.mixin import PromptDictType, PromptMixinType from tonic_validate.metrics.augmentation_precision_metric import ( AugmentationPrecisionMetric, ) from tonic_validate.services.openai_service import OpenAIService class AugmentationPrecisionEvaluator(BaseEvaluator): """ Tonic Validate's augmentation precision metric. The output score is a float between 0.0 and 1.0. See https://docs.tonic.ai/validate/ for more details. Args: openai_service(OpenAIService): The OpenAI service to use. Specifies the chat completion model to use as the LLM evaluator. Defaults to "gpt-4". """ def __init__(self, openai_service: Optional[Any] = None): if openai_service is None: openai_service = OpenAIService("gpt-4") self.openai_service = openai_service self.metric = AugmentationPrecisionMetric() async def aevaluate( self, query: Optional[str] = None, response: Optional[str] = None, contexts: Optional[Sequence[str]] = None, **kwargs: Any, ) -> EvaluationResult: from tonic_validate.classes.benchmark import BenchmarkItem from tonic_validate.classes.llm_response import LLMResponse benchmark_item = BenchmarkItem(question=query) llm_response = LLMResponse( llm_answer=response, llm_context_list=contexts, benchmark_item=benchmark_item, ) score = self.metric.score(llm_response, self.openai_service) return EvaluationResult( query=query, contexts=contexts, response=response, score=score ) def _get_prompts(self) -> PromptDictType: return {} def _get_prompt_modules(self) -> PromptMixinType: return {} def _update_prompts(self, prompts_dict: PromptDictType) -> None: return
from typing import Any, Optional, Sequence from llama_index.core.evaluation.base import BaseEvaluator, EvaluationResult from llama_index.core.prompts.mixin import PromptDictType, PromptMixinType from tonic_validate.metrics.augmentation_precision_metric import ( AugmentationPrecisionMetric, ) from tonic_validate.services.openai_service import OpenAIService class AugmentationPrecisionEvaluator(BaseEvaluator): """ Tonic Validate's augmentation precision metric. The output score is a float between 0.0 and 1.0. See https://docs.tonic.ai/validate/ for more details. Args: openai_service(OpenAIService): The OpenAI service to use. Specifies the chat completion model to use as the LLM evaluator. Defaults to "gpt-4". """ def __init__(self, openai_service: Optional[Any] = None): if openai_service is None: openai_service = OpenAIService("gpt-4") self.openai_service = openai_service self.metric = AugmentationPrecisionMetric() async def aevaluate( self, query: Optional[str] = None, response: Optional[str] = None, contexts: Optional[Sequence[str]] = None, **kwargs: Any ) -> EvaluationResult: from tonic_validate.classes.benchmark import BenchmarkItem from tonic_validate.classes.llm_response import LLMResponse benchmark_item = BenchmarkItem(question=query) llm_response = LLMResponse( llm_answer=response, llm_context_list=contexts, benchmark_item=benchmark_item, ) score = self.metric.score(llm_response, self.openai_service) return EvaluationResult( query=query, contexts=contexts, response=response, score=score ) def _get_prompts(self) -> PromptDictType: return {} def _get_prompt_modules(self) -> PromptMixinType: return {} def _update_prompts(self, prompts_dict: PromptDictType) -> None: return
"""Module containing the base parser for arguments of Jina.""" import argparse from jina.parsers.helper import _chf def set_base_parser(): """Set the base parser :return: the parser """ from jina import __version__ from jina.helper import colored, format_full_version_info, get_full_version # create the top-level parser urls = { 'Code': ('💻', 'https://oss.jina.ai'), 'Docs': ('📖', 'https://docs.jina.ai'), 'Help': ('💬', 'https://slack.jina.ai'), 'Hiring!': ('🙌', 'https://jobs.jina.ai'), } url_str = '\n'.join( f'- {v[0]:<10} {k:10.10}\t{colored(v[1], "cyan", attrs=["underline"])}' for k, v in urls.items() ) parser = argparse.ArgumentParser( epilog=f''' Jina v{colored(__version__, "green")}: build cross-modal and multimodal applications on the cloud. {url_str} ''', formatter_class=_chf, ) parser.add_argument( '-v', '--version', action='version', version=__version__, help='Show Jina version', ) parser.add_argument( '-vf', '--version-full', action='version', version=format_full_version_info(*get_full_version()), help='Show Jina and all dependencies\' versions', ) return parser
"""Module containing the base parser for arguments of Jina.""" import argparse from jina.parsers.helper import _chf def set_base_parser(): """Set the base parser :return: the parser """ from jina import __version__ from jina.helper import colored, format_full_version_info, get_full_version # create the top-level parser urls = { 'Code': ('💻', 'https://github.com/jina-ai/jina'), 'Docs': ('📖', 'https://docs.jina.ai'), 'Help': ('💬', 'https://slack.jina.ai'), 'Hiring!': ('🙌', 'https://career.jina.ai'), } url_str = '\n'.join( f'- {v[0]:<10} {k:10.10}\t{colored(v[1], "cyan", attrs=["underline"])}' for k, v in urls.items() ) parser = argparse.ArgumentParser( epilog=f''' Jina (v{colored(__version__, "green")}) is the cloud-native neural search framework powered by deep learning. {url_str} ''', formatter_class=_chf, ) parser.add_argument( '-v', '--version', action='version', version=__version__, help='Show Jina version', ) parser.add_argument( '-vf', '--version-full', action='version', version=format_full_version_info(*get_full_version()), help='Show Jina and all dependencies\' versions', ) return parser
import PIL.Image import pytest import torch import torchvision.transforms.v2.utils from common_utils import DEFAULT_SIZE, make_bounding_boxes, make_detection_mask, make_image from torchvision import datapoints from torchvision.transforms.v2.functional import to_pil_image from torchvision.transforms.v2.utils import has_all, has_any IMAGE = make_image(DEFAULT_SIZE, color_space="RGB") BOUNDING_BOX = make_bounding_boxes(DEFAULT_SIZE, format=datapoints.BoundingBoxFormat.XYXY) MASK = make_detection_mask(DEFAULT_SIZE) @pytest.mark.parametrize( ("sample", "types", "expected"), [ ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Mask,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.Mask), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes, datapoints.Mask), True), ((MASK,), (datapoints.Image, datapoints.BoundingBoxes), False), ((BOUNDING_BOX,), (datapoints.Image, datapoints.Mask), False), ((IMAGE,), (datapoints.BoundingBoxes, datapoints.Mask), False), ( (IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), True, ), ((), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ((IMAGE, BOUNDING_BOX, MASK), (lambda obj: isinstance(obj, datapoints.Image),), True), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: False,), False), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: True,), True), ((IMAGE,), (datapoints.Image, PIL.Image.Image, torchvision.transforms.v2.utils.is_pure_tensor), True), ( (torch.Tensor(IMAGE),), (datapoints.Image, PIL.Image.Image, torchvision.transforms.v2.utils.is_pure_tensor), True, ), ( (to_pil_image(IMAGE),), (datapoints.Image, PIL.Image.Image, torchvision.transforms.v2.utils.is_pure_tensor), True, ), ], ) def test_has_any(sample, types, expected): assert has_any(sample, *types) is expected @pytest.mark.parametrize( ("sample", "types", "expected"), [ ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Mask,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.Mask), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes, datapoints.Mask), True), ( (IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), True, ), ((BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes), False), ((BOUNDING_BOX, MASK), (datapoints.Image, datapoints.Mask), False), ((IMAGE, MASK), (datapoints.BoundingBoxes, datapoints.Mask), False), ( (IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), True, ), ((BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ((IMAGE, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ((IMAGE, BOUNDING_BOX), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ( (IMAGE, BOUNDING_BOX, MASK), (lambda obj: isinstance(obj, (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask)),), True, ), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: False,), False), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: True,), True), ], ) def test_has_all(sample, types, expected): assert has_all(sample, *types) is expected
import PIL.Image import pytest import torch import torchvision.transforms.v2.utils from common_utils import DEFAULT_SIZE, make_bounding_box, make_detection_mask, make_image from torchvision import datapoints from torchvision.transforms.v2.functional import to_pil_image from torchvision.transforms.v2.utils import has_all, has_any IMAGE = make_image(DEFAULT_SIZE, color_space="RGB") BOUNDING_BOX = make_bounding_box(DEFAULT_SIZE, format=datapoints.BoundingBoxFormat.XYXY) MASK = make_detection_mask(DEFAULT_SIZE) @pytest.mark.parametrize( ("sample", "types", "expected"), [ ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Mask,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.Mask), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes, datapoints.Mask), True), ((MASK,), (datapoints.Image, datapoints.BoundingBoxes), False), ((BOUNDING_BOX,), (datapoints.Image, datapoints.Mask), False), ((IMAGE,), (datapoints.BoundingBoxes, datapoints.Mask), False), ( (IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), True, ), ((), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ((IMAGE, BOUNDING_BOX, MASK), (lambda obj: isinstance(obj, datapoints.Image),), True), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: False,), False), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: True,), True), ((IMAGE,), (datapoints.Image, PIL.Image.Image, torchvision.transforms.v2.utils.is_pure_tensor), True), ( (torch.Tensor(IMAGE),), (datapoints.Image, PIL.Image.Image, torchvision.transforms.v2.utils.is_pure_tensor), True, ), ( (to_pil_image(IMAGE),), (datapoints.Image, PIL.Image.Image, torchvision.transforms.v2.utils.is_pure_tensor), True, ), ], ) def test_has_any(sample, types, expected): assert has_any(sample, *types) is expected @pytest.mark.parametrize( ("sample", "types", "expected"), [ ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Mask,), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.Mask), True), ((IMAGE, BOUNDING_BOX, MASK), (datapoints.BoundingBoxes, datapoints.Mask), True), ( (IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), True, ), ((BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes), False), ((BOUNDING_BOX, MASK), (datapoints.Image, datapoints.Mask), False), ((IMAGE, MASK), (datapoints.BoundingBoxes, datapoints.Mask), False), ( (IMAGE, BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), True, ), ((BOUNDING_BOX, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ((IMAGE, MASK), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ((IMAGE, BOUNDING_BOX), (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask), False), ( (IMAGE, BOUNDING_BOX, MASK), (lambda obj: isinstance(obj, (datapoints.Image, datapoints.BoundingBoxes, datapoints.Mask)),), True, ), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: False,), False), ((IMAGE, BOUNDING_BOX, MASK), (lambda _: True,), True), ], ) def test_has_all(sample, types, expected): assert has_all(sample, *types) is expected
# Licensed to the LF AI & Data foundation under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDoc from docarray.documents import ImageDoc from docarray.typing import ImageBytes from docarray.utils._internal.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp REMOTE_JPG = ( 'https://upload.wikimedia.org/wikipedia/commons/8/80/' 'Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg' ) pytestmark = [pytest.mark.image] @pytest.mark.slow @pytest.mark.internet def test_image(): image = ImageDoc(url=REMOTE_JPG) image.tensor = image.url.load() assert isinstance(image.tensor, np.ndarray) def test_image_str(): image = parse_obj_as(ImageDoc, 'http://myurl.jpg') assert image.url == 'http://myurl.jpg' def test_image_np(): image = parse_obj_as(ImageDoc, np.zeros((10, 10, 3))) assert (image.tensor == np.zeros((10, 10, 3))).all() def test_image_torch(): image = parse_obj_as(ImageDoc, torch.zeros(10, 10, 3)) assert (image.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.tensorflow def test_image_tensorflow(): image = ImageDoc(tensor=tf.zeros((10, 10, 3))) assert tnp.allclose(image.tensor.tensor, tf.zeros((10, 10, 3))) def test_image_shortcut_doc(): class MyDoc(BaseDoc): image: ImageDoc image2: ImageDoc image3: ImageDoc doc = MyDoc( image='http://myurl.jpg', image2=np.zeros((10, 10, 3)), image3=torch.zeros(10, 10, 3), ) assert doc.image.url == 'http://myurl.jpg' assert (doc.image2.tensor == np.zeros((10, 10, 3))).all() assert (doc.image3.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.slow @pytest.mark.internet def test_byte(): img = ImageDoc(url=REMOTE_JPG) img.bytes_ = img.url.load_bytes() assert isinstance(img.bytes_, ImageBytes) @pytest.mark.slow @pytest.mark.internet def test_byte_from_tensor(): img = ImageDoc(url=REMOTE_JPG) img.tensor = img.url.load() img.bytes_ = img.tensor.to_bytes() assert isinstance(img.bytes_, bytes) assert isinstance(img.bytes_, ImageBytes) assert len(img.bytes_) > 0
import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDoc from docarray.documents import ImageDoc from docarray.typing import ImageBytes from docarray.utils._internal.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp REMOTE_JPG = ( 'https://upload.wikimedia.org/wikipedia/commons/8/80/' 'Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg' ) pytestmark = [pytest.mark.image] @pytest.mark.slow @pytest.mark.internet def test_image(): image = ImageDoc(url=REMOTE_JPG) image.tensor = image.url.load() assert isinstance(image.tensor, np.ndarray) def test_image_str(): image = parse_obj_as(ImageDoc, 'http://myurl.jpg') assert image.url == 'http://myurl.jpg' def test_image_np(): image = parse_obj_as(ImageDoc, np.zeros((10, 10, 3))) assert (image.tensor == np.zeros((10, 10, 3))).all() def test_image_torch(): image = parse_obj_as(ImageDoc, torch.zeros(10, 10, 3)) assert (image.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.tensorflow def test_image_tensorflow(): image = ImageDoc(tensor=tf.zeros((10, 10, 3))) assert tnp.allclose(image.tensor.tensor, tf.zeros((10, 10, 3))) def test_image_shortcut_doc(): class MyDoc(BaseDoc): image: ImageDoc image2: ImageDoc image3: ImageDoc doc = MyDoc( image='http://myurl.jpg', image2=np.zeros((10, 10, 3)), image3=torch.zeros(10, 10, 3), ) assert doc.image.url == 'http://myurl.jpg' assert (doc.image2.tensor == np.zeros((10, 10, 3))).all() assert (doc.image3.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.slow @pytest.mark.internet def test_byte(): img = ImageDoc(url=REMOTE_JPG) img.bytes_ = img.url.load_bytes() assert isinstance(img.bytes_, ImageBytes) @pytest.mark.slow @pytest.mark.internet def test_byte_from_tensor(): img = ImageDoc(url=REMOTE_JPG) img.tensor = img.url.load() img.bytes_ = img.tensor.to_bytes() assert isinstance(img.bytes_, bytes) assert isinstance(img.bytes_, ImageBytes) assert len(img.bytes_) > 0
import pytest @pytest.mark.compile def test_placeholder() -> None: """Used for compiling integration tests without running any real tests."""
import pytest @pytest.mark.compile def test_placeholder() -> None: """Used for compiling integration tests without running any real tests.""" pass
from __future__ import annotations try: from typing import Self except ImportError: from typing_extensions import Self import torch import transformers from PIL import Image from sentence_transformers.models.Asym import InputModule class CLIPModel(InputModule): save_in_root: bool = True def __init__(self, model_name: str = "openai/clip-vit-base-patch32", processor_name=None) -> None: super().__init__() if processor_name is None: processor_name = model_name self.model = transformers.CLIPModel.from_pretrained(model_name) self.processor = transformers.CLIPProcessor.from_pretrained(processor_name) def __repr__(self) -> str: return "CLIPModel()" @property def max_seq_length(self) -> int: return self.processor.tokenizer.model_max_length @max_seq_length.setter def max_seq_length(self, value: int) -> None: self.processor.tokenizer.model_max_length = value def forward(self, features: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: image_embeds = [] text_embeds = [] if "pixel_values" in features: vision_outputs = self.model.vision_model(pixel_values=features["pixel_values"]) image_embeds = self.model.visual_projection(vision_outputs[1]) if "input_ids" in features: text_outputs = self.model.text_model( input_ids=features.get("input_ids"), attention_mask=features.get("attention_mask", None), position_ids=features.get("position_ids", None), output_attentions=features.get("output_attentions", None), output_hidden_states=features.get("output_hidden_states", None), ) text_embeds = self.model.text_projection(text_outputs[1]) sentence_embedding = [] image_features = iter(image_embeds) text_features = iter(text_embeds) for idx, input_type in enumerate(features["image_text_info"]): if input_type == 0: sentence_embedding.append(next(image_features)) else: sentence_embedding.append(next(text_features)) features["sentence_embedding"] = torch.stack(sentence_embedding).float() return features def tokenize(self, texts, padding: str | bool = True) -> dict[str, torch.Tensor]: images = [] texts_values = [] image_text_info = [] for idx, data in enumerate(texts): if isinstance(data, Image.Image): # An Image images.append(data) image_text_info.append(0) else: # A text texts_values.append(data) image_text_info.append(1) encoding = {} if len(texts_values): encoding = self.processor.tokenizer(texts_values, padding=padding, truncation=True, return_tensors="pt") if len(images): image_features = self.processor.image_processor(images, return_tensors="pt") encoding["pixel_values"] = image_features.pixel_values encoding["image_text_info"] = image_text_info return dict(encoding) @property def tokenizer(self) -> transformers.CLIPProcessor: return self.processor def save(self, output_path: str, *args, safe_serialization: bool = True, **kwargs) -> None: self.model.save_pretrained(output_path, safe_serialization=safe_serialization) self.processor.save_pretrained(output_path) @classmethod def load( cls, model_name_or_path: str, subfolder: str = "", token: bool | str | None = None, cache_folder: str | None = None, revision: str | None = None, local_files_only: bool = False, **kwargs, ) -> Self: local_path = cls.load_dir_path( model_name_or_path=model_name_or_path, subfolder=subfolder, token=token, cache_folder=cache_folder, revision=revision, local_files_only=local_files_only, ) return cls(local_path)
from __future__ import annotations import torch import transformers from PIL import Image from torch import nn class CLIPModel(nn.Module): save_in_root: bool = True def __init__(self, model_name: str = "openai/clip-vit-base-patch32", processor_name=None) -> None: super().__init__() if processor_name is None: processor_name = model_name self.model = transformers.CLIPModel.from_pretrained(model_name) self.processor = transformers.CLIPProcessor.from_pretrained(processor_name) def __repr__(self) -> str: return "CLIPModel()" @property def max_seq_length(self) -> int: return self.processor.tokenizer.model_max_length @max_seq_length.setter def max_seq_length(self, value: int) -> None: self.processor.tokenizer.model_max_length = value def forward(self, features: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: image_embeds = [] text_embeds = [] if "pixel_values" in features: vision_outputs = self.model.vision_model(pixel_values=features["pixel_values"]) image_embeds = self.model.visual_projection(vision_outputs[1]) if "input_ids" in features: text_outputs = self.model.text_model( input_ids=features.get("input_ids"), attention_mask=features.get("attention_mask", None), position_ids=features.get("position_ids", None), output_attentions=features.get("output_attentions", None), output_hidden_states=features.get("output_hidden_states", None), ) text_embeds = self.model.text_projection(text_outputs[1]) sentence_embedding = [] image_features = iter(image_embeds) text_features = iter(text_embeds) for idx, input_type in enumerate(features["image_text_info"]): if input_type == 0: sentence_embedding.append(next(image_features)) else: sentence_embedding.append(next(text_features)) features["sentence_embedding"] = torch.stack(sentence_embedding).float() return features def tokenize(self, texts, padding: str | bool = True) -> dict[str, torch.Tensor]: images = [] texts_values = [] image_text_info = [] for idx, data in enumerate(texts): if isinstance(data, Image.Image): # An Image images.append(data) image_text_info.append(0) else: # A text texts_values.append(data) image_text_info.append(1) encoding = {} if len(texts_values): encoding = self.processor.tokenizer(texts_values, padding=padding, truncation=True, return_tensors="pt") if len(images): image_features = self.processor.image_processor(images, return_tensors="pt") encoding["pixel_values"] = image_features.pixel_values encoding["image_text_info"] = image_text_info return dict(encoding) @property def tokenizer(self) -> transformers.CLIPProcessor: return self.processor def save(self, output_path: str) -> None: self.model.save_pretrained(output_path) self.processor.save_pretrained(output_path) @staticmethod def load(input_path: str) -> CLIPModel: return CLIPModel(model_name=input_path)
import importlib.util import warnings from functools import wraps from typing import Optional import torch def is_module_available(*modules: str) -> bool: r"""Returns if a top-level module with :attr:`name` exists *without** importing it. This is generally safer than try-catch block around a `import X`. It avoids third party libraries breaking assumptions of some of our tests, e.g., setting multiprocessing start method when imported (see librosa/#747, torchvision/#544). """ return all(importlib.util.find_spec(m) is not None for m in modules) def requires_module(*modules: str): """Decorate function to give error message if invoked without required optional modules. This decorator is to give better error message to users rather than raising ``NameError: name 'module' is not defined`` at random places. """ missing = [m for m in modules if not is_module_available(m)] if not missing: # fall through. If all the modules are available, no need to decorate def decorator(func): return func else: req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}") return wrapped return decorator def deprecated(direction: str, version: Optional[str] = None): """Decorator to add deprecation message Args: direction (str): Migration steps to be given to users. version (str or int): The version when the object will be removed """ def decorator(func): @wraps(func) def wrapped(*args, **kwargs): message = ( f"{func.__module__}.{func.__name__} has been deprecated " f'and will be removed from {"future" if version is None else version} release. ' f"{direction}" ) warnings.warn(message, stacklevel=2) return func(*args, **kwargs) return wrapped return decorator def is_kaldi_available(): try: return torch.ops.torchaudio.is_kaldi_available() except Exception: return False def requires_kaldi(): if is_kaldi_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires kaldi") return wrapped return decorator def _check_soundfile_importable(): if not is_module_available("soundfile"): return False try: import soundfile # noqa: F401 return True except Exception: warnings.warn("Failed to import soundfile. 'soundfile' backend is not available.") return False _is_soundfile_importable = _check_soundfile_importable() def is_soundfile_available(): return _is_soundfile_importable def requires_soundfile(): if is_soundfile_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires soundfile") return wrapped return decorator def is_sox_available(): return is_module_available("torchaudio.lib._torchaudio_sox") def requires_sox(): if is_sox_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires sox") return wrapped return decorator
import importlib.util import warnings from functools import wraps from typing import Optional import torch def is_module_available(*modules: str) -> bool: r"""Returns if a top-level module with :attr:`name` exists *without** importing it. This is generally safer than try-catch block around a `import X`. It avoids third party libraries breaking assumptions of some of our tests, e.g., setting multiprocessing start method when imported (see librosa/#747, torchvision/#544). """ return all(importlib.util.find_spec(m) is not None for m in modules) def requires_module(*modules: str): """Decorate function to give error message if invoked without required optional modules. This decorator is to give better error message to users rather than raising ``NameError: name 'module' is not defined`` at random places. """ missing = [m for m in modules if not is_module_available(m)] if not missing: # fall through. If all the modules are available, no need to decorate def decorator(func): return func else: req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}") return wrapped return decorator def deprecated(direction: str, version: Optional[str] = None): """Decorator to add deprecation message Args: direction (str): Migration steps to be given to users. version (str or int): The version when the object will be removed """ def decorator(func): @wraps(func) def wrapped(*args, **kwargs): message = ( f"{func.__module__}.{func.__name__} has been deprecated " f'and will be removed from {"future" if version is None else version} release. ' f"{direction}" ) warnings.warn(message, stacklevel=2) return func(*args, **kwargs) return wrapped return decorator def is_kaldi_available(): return is_module_available("torchaudio._torchaudio") and torch.ops.torchaudio.is_kaldi_available() def requires_kaldi(): if is_kaldi_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires kaldi") return wrapped return decorator def _check_soundfile_importable(): if not is_module_available("soundfile"): return False try: import soundfile # noqa: F401 return True except Exception: warnings.warn("Failed to import soundfile. 'soundfile' backend is not available.") return False _is_soundfile_importable = _check_soundfile_importable() def is_soundfile_available(): return _is_soundfile_importable def requires_soundfile(): if is_soundfile_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires soundfile") return wrapped return decorator def is_sox_available(): return is_module_available("torchaudio._torchaudio") and torch.ops.torchaudio.is_sox_available() def requires_sox(): if is_sox_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires sox") return wrapped return decorator
"""Init file of LlamaIndex.""" __version__ = "0.12.19" import logging from logging import NullHandler from typing import Callable, Optional try: # Force pants to install eval_type_backport on 3.9 import eval_type_backport # noqa # type: ignore except ImportError: pass # response from llama_index.core.base.response.schema import Response # import global eval handler from llama_index.core.callbacks.global_handlers import set_global_handler from llama_index.core.data_structs.struct_type import IndexStructType from llama_index.core.embeddings.mock_embed_model import MockEmbedding # indices # loading from llama_index.core.indices import ( ComposableGraph, DocumentSummaryIndex, GPTDocumentSummaryIndex, GPTKeywordTableIndex, GPTListIndex, GPTRAKEKeywordTableIndex, GPTSimpleKeywordTableIndex, GPTTreeIndex, GPTVectorStoreIndex, KeywordTableIndex, KnowledgeGraphIndex, ListIndex, PropertyGraphIndex, RAKEKeywordTableIndex, SimpleKeywordTableIndex, SummaryIndex, TreeIndex, VectorStoreIndex, load_graph_from_storage, load_index_from_storage, load_indices_from_storage, ) # structured from llama_index.core.indices.common.struct_store.base import ( SQLDocumentContextBuilder, ) # prompt helper from llama_index.core.indices.prompt_helper import PromptHelper # prompts from llama_index.core.prompts import ( BasePromptTemplate, ChatPromptTemplate, # backwards compatibility Prompt, PromptTemplate, SelectorPromptTemplate, ) from llama_index.core.readers import SimpleDirectoryReader, download_loader # Response Synthesizer from llama_index.core.response_synthesizers.factory import get_response_synthesizer from llama_index.core.schema import Document, QueryBundle from llama_index.core.service_context import ( ServiceContext, set_global_service_context, ) # global settings from llama_index.core.settings import Settings # storage from llama_index.core.storage.storage_context import StorageContext # sql wrapper from llama_index.core.utilities.sql_wrapper import SQLDatabase # global tokenizer from llama_index.core.utils import get_tokenizer, set_global_tokenizer # best practices for library logging: # https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library logging.getLogger(__name__).addHandler(NullHandler()) __all__ = [ "StorageContext", "ServiceContext", "ComposableGraph", # indices "SummaryIndex", "VectorStoreIndex", "SimpleKeywordTableIndex", "KeywordTableIndex", "RAKEKeywordTableIndex", "TreeIndex", "DocumentSummaryIndex", "KnowledgeGraphIndex", "PropertyGraphIndex", # indices - legacy names "GPTKeywordTableIndex", "GPTKnowledgeGraphIndex", "GPTSimpleKeywordTableIndex", "GPTRAKEKeywordTableIndex", "GPTListIndex", "ListIndex", "GPTTreeIndex", "GPTVectorStoreIndex", "GPTDocumentSummaryIndex", "Prompt", "PromptTemplate", "BasePromptTemplate", "ChatPromptTemplate", "SelectorPromptTemplate", "SummaryPrompt", "TreeInsertPrompt", "TreeSelectPrompt", "TreeSelectMultiplePrompt", "RefinePrompt", "QuestionAnswerPrompt", "KeywordExtractPrompt", "QueryKeywordExtractPrompt", "Response", "Document", "SimpleDirectoryReader", "VellumPredictor", "VellumPromptRegistry", "MockEmbedding", "SQLDatabase", "SQLDocumentContextBuilder", "SQLContextBuilder", "PromptHelper", "IndexStructType", "download_loader", "load_graph_from_storage", "load_index_from_storage", "load_indices_from_storage", "QueryBundle", "get_response_synthesizer", "set_global_service_context", "set_global_handler", "set_global_tokenizer", "get_tokenizer", "Settings", ] # eval global toggle from llama_index.core.callbacks.base_handler import BaseCallbackHandler global_handler: Optional[BaseCallbackHandler] = None # NOTE: keep for backwards compatibility SQLContextBuilder = SQLDocumentContextBuilder # global tokenizer global_tokenizer: Optional[Callable[[str], list]] = None
"""Init file of LlamaIndex.""" __version__ = "0.12.18" import logging from logging import NullHandler from typing import Callable, Optional try: # Force pants to install eval_type_backport on 3.9 import eval_type_backport # noqa # type: ignore except ImportError: pass # response from llama_index.core.base.response.schema import Response # import global eval handler from llama_index.core.callbacks.global_handlers import set_global_handler from llama_index.core.data_structs.struct_type import IndexStructType from llama_index.core.embeddings.mock_embed_model import MockEmbedding # indices # loading from llama_index.core.indices import ( ComposableGraph, DocumentSummaryIndex, GPTDocumentSummaryIndex, GPTKeywordTableIndex, GPTListIndex, GPTRAKEKeywordTableIndex, GPTSimpleKeywordTableIndex, GPTTreeIndex, GPTVectorStoreIndex, KeywordTableIndex, KnowledgeGraphIndex, ListIndex, PropertyGraphIndex, RAKEKeywordTableIndex, SimpleKeywordTableIndex, SummaryIndex, TreeIndex, VectorStoreIndex, load_graph_from_storage, load_index_from_storage, load_indices_from_storage, ) # structured from llama_index.core.indices.common.struct_store.base import ( SQLDocumentContextBuilder, ) # prompt helper from llama_index.core.indices.prompt_helper import PromptHelper # prompts from llama_index.core.prompts import ( BasePromptTemplate, ChatPromptTemplate, # backwards compatibility Prompt, PromptTemplate, SelectorPromptTemplate, ) from llama_index.core.readers import SimpleDirectoryReader, download_loader # Response Synthesizer from llama_index.core.response_synthesizers.factory import get_response_synthesizer from llama_index.core.schema import Document, QueryBundle from llama_index.core.service_context import ( ServiceContext, set_global_service_context, ) # global settings from llama_index.core.settings import Settings # storage from llama_index.core.storage.storage_context import StorageContext # sql wrapper from llama_index.core.utilities.sql_wrapper import SQLDatabase # global tokenizer from llama_index.core.utils import get_tokenizer, set_global_tokenizer # best practices for library logging: # https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library logging.getLogger(__name__).addHandler(NullHandler()) __all__ = [ "StorageContext", "ServiceContext", "ComposableGraph", # indices "SummaryIndex", "VectorStoreIndex", "SimpleKeywordTableIndex", "KeywordTableIndex", "RAKEKeywordTableIndex", "TreeIndex", "DocumentSummaryIndex", "KnowledgeGraphIndex", "PropertyGraphIndex", # indices - legacy names "GPTKeywordTableIndex", "GPTKnowledgeGraphIndex", "GPTSimpleKeywordTableIndex", "GPTRAKEKeywordTableIndex", "GPTListIndex", "ListIndex", "GPTTreeIndex", "GPTVectorStoreIndex", "GPTDocumentSummaryIndex", "Prompt", "PromptTemplate", "BasePromptTemplate", "ChatPromptTemplate", "SelectorPromptTemplate", "SummaryPrompt", "TreeInsertPrompt", "TreeSelectPrompt", "TreeSelectMultiplePrompt", "RefinePrompt", "QuestionAnswerPrompt", "KeywordExtractPrompt", "QueryKeywordExtractPrompt", "Response", "Document", "SimpleDirectoryReader", "VellumPredictor", "VellumPromptRegistry", "MockEmbedding", "SQLDatabase", "SQLDocumentContextBuilder", "SQLContextBuilder", "PromptHelper", "IndexStructType", "download_loader", "load_graph_from_storage", "load_index_from_storage", "load_indices_from_storage", "QueryBundle", "get_response_synthesizer", "set_global_service_context", "set_global_handler", "set_global_tokenizer", "get_tokenizer", "Settings", ] # eval global toggle from llama_index.core.callbacks.base_handler import BaseCallbackHandler global_handler: Optional[BaseCallbackHandler] = None # NOTE: keep for backwards compatibility SQLContextBuilder = SQLDocumentContextBuilder # global tokenizer global_tokenizer: Optional[Callable[[str], list]] = None
from __future__ import annotations import random import pytest import torch from datasets import Dataset from torch.utils.data import ConcatDataset from sentence_transformers.sampler import NoDuplicatesBatchSampler, ProportionalBatchSampler @pytest.fixture def dummy_dataset() -> Dataset: """ Dummy dataset for testing purposes. The dataset looks as follows: { "data": [0, 47, 3, 30, 3, ... 2], "label": [0, 1, 0, 1, ..., 0, 1], } """ # Create a list of two 0's, two 1's, two 2's, ... two 49's. Then shuffle. values = [j for i in range(50) for j in (i, i)] random.shuffle(values) data = {"data": values, "label": [i % 2 for i in range(100)]} return Dataset.from_dict(data) @pytest.fixture def dummy_duplicates_dataset() -> Dataset: """ Dummy dataset for testing purposes. The dataset looks as follows: { "anchor": ["anchor_1", "anchor_1", "anchor_1", ... "anchor_2", "anchor_2"], "positive": ["positive_1", "positive_1", "positive_1", ... "positive_2", "positive_2"], } """ values = [{"anchor": "anchor_1", "positive": "positive_1"}] * 10 + [ {"anchor": "anchor_2", "positive": "positive_2"} ] * 8 return Dataset.from_list(values) def test_group_by_label_batch_sampler_label_a(dummy_dataset: Dataset) -> None: batch_size = 10 sampler = NoDuplicatesBatchSampler( dataset=dummy_dataset, batch_size=batch_size, drop_last=True, valid_label_columns=["label"] ) batches = list(iter(sampler)) # Assert all batch sizes are correct assert all(len(batch) == batch_size for batch in batches) # Assert batches contain no duplicate values for batch in batches: batch_values = [dummy_dataset[i]["data"] for i in batch] assert len(batch_values) == len(set(batch_values)), f"Batch {batch} contains duplicate values: {batch_values}" @pytest.mark.parametrize("drop_last", [True, False]) def test_proportional_no_duplicates(dummy_duplicates_dataset: Dataset, drop_last: bool) -> None: batch_size = 2 sampler_1 = NoDuplicatesBatchSampler( dataset=dummy_duplicates_dataset, batch_size=batch_size, drop_last=drop_last, valid_label_columns=["anchor"] ) sampler_2 = NoDuplicatesBatchSampler( dataset=dummy_duplicates_dataset, batch_size=batch_size, drop_last=drop_last, valid_label_columns=["positive"] ) concat_dataset = ConcatDataset([dummy_duplicates_dataset, dummy_duplicates_dataset]) batch_sampler = ProportionalBatchSampler( concat_dataset, [sampler_1, sampler_2], generator=torch.Generator(), seed=12 ) batches = list(iter(batch_sampler)) if drop_last: # If we drop the last batch (i.e. incomplete batches), we should have 16 batches out of the 18 possible, # because of the duplicates being skipped by the NoDuplicatesBatchSampler. # Notably, we should not crash like reported in #2816. assert len(batches) == 16 # All batches are the same size: 2 assert all(len(batch) == batch_size for batch in batches) assert len(sum(batches, [])) == 32 else: # If we don't drop incomplete batches, we should be able to do 18 batches, and get more data. # Note: we don't get all data, because the NoDuplicatesBatchSampler will estimate the number of batches # and it would require more (non-complete) batches to get all data. assert len(batches) == 18 assert len(sum(batches, [])) == 34
from __future__ import annotations import random import pytest from datasets import Dataset from sentence_transformers.sampler import NoDuplicatesBatchSampler @pytest.fixture def dummy_dataset(): """ Dummy dataset for testing purposes. The dataset looks as follows: { "data": [0, 47, 3, 30, 3, ... 2], "label": [0, 1, 0, 1, ..., 0, 1], } """ # Create a list of two 0's, two 1's, two 2's, ... two 49's. Then shuffle. values = [j for i in range(50) for j in (i, i)] random.shuffle(values) data = {"data": values, "label": [i % 2 for i in range(100)]} return Dataset.from_dict(data) def test_group_by_label_batch_sampler_label_a(dummy_dataset): batch_size = 10 sampler = NoDuplicatesBatchSampler( dataset=dummy_dataset, batch_size=batch_size, drop_last=True, valid_label_columns=["label"] ) batches = list(iter(sampler)) # Assert all batch sizes are correct assert all(len(batch) == batch_size for batch in batches) # Assert batches contain no duplicate values for batch in batches: batch_values = [dummy_dataset[i]["data"] for i in batch] assert len(batch_values) == len(set(batch_values)), f"Batch {batch} contains duplicate values: {batch_values}"
# Copyright (c) OpenMMLab. All rights reserved. import mmcv import torch from mmdet.models.dense_heads import FSAFHead def test_fsaf_head_loss(): """Tests anchor head loss when truth is empty and non-empty.""" s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'scale_factor': 1, 'pad_shape': (s, s, 3) }] cfg = dict( reg_decoded_bbox=True, anchor_generator=dict( type='AnchorGenerator', octave_base_scale=1, scales_per_octave=1, ratios=[1.0], strides=[8, 16, 32, 64, 128]), bbox_coder=dict(type='TBLRBBoxCoder', normalizer=4.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0, reduction='none'), loss_bbox=dict( type='IoULoss', eps=1e-6, loss_weight=1.0, reduction='none')) train_cfg = mmcv.Config( dict( assigner=dict( type='CenterRegionAssigner', pos_scale=0.2, neg_scale=0.2, min_pos_iof=0.01), allowed_border=-1, pos_weight=-1, debug=False)) head = FSAFHead(num_classes=4, in_channels=1, train_cfg=train_cfg, **cfg) if torch.cuda.is_available(): head.cuda() # FSAF head expects a multiple levels of features per image feat = [ torch.rand(1, 1, s // (2**(i + 2)), s // (2**(i + 2))).cuda() for i in range(len(head.anchor_generator.strides)) ] cls_scores, bbox_preds = head.forward(feat) gt_bboxes_ignore = None # When truth is non-empty then both cls and box loss should be nonzero # for random inputs gt_bboxes = [ torch.Tensor([[23.6667, 23.8757, 238.6326, 151.8874]]).cuda(), ] gt_labels = [torch.LongTensor([2]).cuda()] one_gt_losses = head.loss(cls_scores, bbox_preds, gt_bboxes, gt_labels, img_metas, gt_bboxes_ignore) onegt_cls_loss = sum(one_gt_losses['loss_cls']) onegt_box_loss = sum(one_gt_losses['loss_bbox']) assert onegt_cls_loss.item() > 0, 'cls loss should be non-zero' assert onegt_box_loss.item() > 0, 'box loss should be non-zero' # Test that empty ground truth encourages the network to predict bkg gt_bboxes = [torch.empty((0, 4)).cuda()] gt_labels = [torch.LongTensor([]).cuda()] empty_gt_losses = head.loss(cls_scores, bbox_preds, gt_bboxes, gt_labels, img_metas, gt_bboxes_ignore) # When there is no truth, the cls loss should be nonzero but there # should be no box loss. empty_cls_loss = sum(empty_gt_losses['loss_cls']) empty_box_loss = sum(empty_gt_losses['loss_bbox']) assert empty_cls_loss.item() > 0, 'cls loss should be non-zero' assert empty_box_loss.item() == 0, ( 'there should be no box loss when there are no true boxes')
import mmcv import torch from mmdet.models.dense_heads import FSAFHead def test_fsaf_head_loss(): """Tests anchor head loss when truth is empty and non-empty.""" s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'scale_factor': 1, 'pad_shape': (s, s, 3) }] cfg = dict( reg_decoded_bbox=True, anchor_generator=dict( type='AnchorGenerator', octave_base_scale=1, scales_per_octave=1, ratios=[1.0], strides=[8, 16, 32, 64, 128]), bbox_coder=dict(type='TBLRBBoxCoder', normalizer=4.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0, reduction='none'), loss_bbox=dict( type='IoULoss', eps=1e-6, loss_weight=1.0, reduction='none')) train_cfg = mmcv.Config( dict( assigner=dict( type='CenterRegionAssigner', pos_scale=0.2, neg_scale=0.2, min_pos_iof=0.01), allowed_border=-1, pos_weight=-1, debug=False)) head = FSAFHead(num_classes=4, in_channels=1, train_cfg=train_cfg, **cfg) if torch.cuda.is_available(): head.cuda() # FSAF head expects a multiple levels of features per image feat = [ torch.rand(1, 1, s // (2**(i + 2)), s // (2**(i + 2))).cuda() for i in range(len(head.anchor_generator.strides)) ] cls_scores, bbox_preds = head.forward(feat) gt_bboxes_ignore = None # When truth is non-empty then both cls and box loss should be nonzero # for random inputs gt_bboxes = [ torch.Tensor([[23.6667, 23.8757, 238.6326, 151.8874]]).cuda(), ] gt_labels = [torch.LongTensor([2]).cuda()] one_gt_losses = head.loss(cls_scores, bbox_preds, gt_bboxes, gt_labels, img_metas, gt_bboxes_ignore) onegt_cls_loss = sum(one_gt_losses['loss_cls']) onegt_box_loss = sum(one_gt_losses['loss_bbox']) assert onegt_cls_loss.item() > 0, 'cls loss should be non-zero' assert onegt_box_loss.item() > 0, 'box loss should be non-zero' # Test that empty ground truth encourages the network to predict bkg gt_bboxes = [torch.empty((0, 4)).cuda()] gt_labels = [torch.LongTensor([]).cuda()] empty_gt_losses = head.loss(cls_scores, bbox_preds, gt_bboxes, gt_labels, img_metas, gt_bboxes_ignore) # When there is no truth, the cls loss should be nonzero but there # should be no box loss. empty_cls_loss = sum(empty_gt_losses['loss_cls']) empty_box_loss = sum(empty_gt_losses['loss_bbox']) assert empty_cls_loss.item() > 0, 'cls loss should be non-zero' assert empty_box_loss.item() == 0, ( 'there should be no box loss when there are no true boxes')
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.utilities import ArceeWrapper from langchain_community.utilities.arcee import ( ArceeDocument, ArceeDocumentAdapter, ArceeDocumentSource, ArceeRoute, DALMFilter, DALMFilterType, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "ArceeRoute": "langchain_community.utilities.arcee", "DALMFilterType": "langchain_community.utilities.arcee", "DALMFilter": "langchain_community.utilities.arcee", "ArceeDocumentSource": "langchain_community.utilities.arcee", "ArceeDocument": "langchain_community.utilities.arcee", "ArceeDocumentAdapter": "langchain_community.utilities.arcee", "ArceeWrapper": "langchain_community.utilities", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "ArceeDocument", "ArceeDocumentAdapter", "ArceeDocumentSource", "ArceeRoute", "ArceeWrapper", "DALMFilter", "DALMFilterType", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.utilities import ArceeWrapper from langchain_community.utilities.arcee import ( ArceeDocument, ArceeDocumentAdapter, ArceeDocumentSource, ArceeRoute, DALMFilter, DALMFilterType, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "ArceeRoute": "langchain_community.utilities.arcee", "DALMFilterType": "langchain_community.utilities.arcee", "DALMFilter": "langchain_community.utilities.arcee", "ArceeDocumentSource": "langchain_community.utilities.arcee", "ArceeDocument": "langchain_community.utilities.arcee", "ArceeDocumentAdapter": "langchain_community.utilities.arcee", "ArceeWrapper": "langchain_community.utilities", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "ArceeRoute", "DALMFilterType", "DALMFilter", "ArceeDocumentSource", "ArceeDocument", "ArceeDocumentAdapter", "ArceeWrapper", ]
"""Snowflake Query Engine Pack.""" import os from typing import Any, Dict, List from llama_index.core import SQLDatabase from llama_index.core.indices.struct_store.sql_query import NLSQLTableQueryEngine from llama_index.core.llama_pack.base import BaseLlamaPack from sqlalchemy import create_engine class SnowflakeQueryEnginePack(BaseLlamaPack): """ Snowflake query engine pack. It uses snowflake-sqlalchemy to connect to Snowflake, then calls NLSQLTableQueryEngine to query data. """ def __init__( self, user: str, password: str, account: str, database: str, schema: str, warehouse: str, role: str, tables: List[str], **kwargs: Any, ) -> None: """Init params.""" # workaround for https://github.com/snowflakedb/snowflake-sqlalchemy/issues/380. try: snowflake_sqlalchemy_20_monkey_patches() except Exception: raise ImportError("Please run `pip install snowflake-sqlalchemy`") if not os.environ.get("OPENAI_API_KEY", None): raise ValueError("OpenAI API Token is missing or blank.") snowflake_uri = f"snowflake://{user}:{password}@{account}/{database}/{schema}?warehouse={warehouse}&role={role}" engine = create_engine(snowflake_uri) self._sql_database = SQLDatabase(engine) self.tables = tables self.query_engine = NLSQLTableQueryEngine( sql_database=self._sql_database, tables=self.tables ) def get_modules(self) -> Dict[str, Any]: """Get modules.""" return { "sql_database": self._sql_database, "query_engine": self.query_engine, } def run(self, *args: Any, **kwargs: Any) -> Any: """Run the pipeline.""" return self.query_engine.query(*args, **kwargs) def snowflake_sqlalchemy_20_monkey_patches(): import sqlalchemy.util.compat # make strings always return unicode strings sqlalchemy.util.compat.string_types = (str,) sqlalchemy.types.String.RETURNS_UNICODE = True import snowflake.sqlalchemy.snowdialect snowflake.sqlalchemy.snowdialect.SnowflakeDialect.returns_unicode_strings = True # make has_table() support the `info_cache` kwarg import snowflake.sqlalchemy.snowdialect def has_table(self, connection, table_name, schema=None, info_cache=None): """ Checks if the table exists. """ return self._has_object(connection, "TABLE", table_name, schema) snowflake.sqlalchemy.snowdialect.SnowflakeDialect.has_table = has_table
"""Snowflake Query Engine Pack.""" import os from typing import Any, Dict, List from llama_index.core import SQLDatabase from llama_index.core.indices.struct_store.sql_query import NLSQLTableQueryEngine from llama_index.core.llama_pack.base import BaseLlamaPack from sqlalchemy import create_engine class SnowflakeQueryEnginePack(BaseLlamaPack): """Snowflake query engine pack. It uses snowflake-sqlalchemy to connect to Snowflake, then calls NLSQLTableQueryEngine to query data. """ def __init__( self, user: str, password: str, account: str, database: str, schema: str, warehouse: str, role: str, tables: List[str], **kwargs: Any, ) -> None: """Init params.""" # workaround for https://github.com/snowflakedb/snowflake-sqlalchemy/issues/380. try: snowflake_sqlalchemy_20_monkey_patches() except Exception: raise ImportError("Please run `pip install snowflake-sqlalchemy`") if not os.environ.get("OPENAI_API_KEY", None): raise ValueError("OpenAI API Token is missing or blank.") snowflake_uri = f"snowflake://{user}:{password}@{account}/{database}/{schema}?warehouse={warehouse}&role={role}" engine = create_engine(snowflake_uri) self._sql_database = SQLDatabase(engine) self.tables = tables self.query_engine = NLSQLTableQueryEngine( sql_database=self._sql_database, tables=self.tables ) def get_modules(self) -> Dict[str, Any]: """Get modules.""" return { "sql_database": self._sql_database, "query_engine": self.query_engine, } def run(self, *args: Any, **kwargs: Any) -> Any: """Run the pipeline.""" return self.query_engine.query(*args, **kwargs) def snowflake_sqlalchemy_20_monkey_patches(): import sqlalchemy.util.compat # make strings always return unicode strings sqlalchemy.util.compat.string_types = (str,) sqlalchemy.types.String.RETURNS_UNICODE = True import snowflake.sqlalchemy.snowdialect snowflake.sqlalchemy.snowdialect.SnowflakeDialect.returns_unicode_strings = True # make has_table() support the `info_cache` kwarg import snowflake.sqlalchemy.snowdialect def has_table(self, connection, table_name, schema=None, info_cache=None): """ Checks if the table exists. """ return self._has_object(connection, "TABLE", table_name, schema) snowflake.sqlalchemy.snowdialect.SnowflakeDialect.has_table = has_table
import wave from typing import TYPE_CHECKING, Any, Type, TypeVar, Union import numpy as np from pydantic import parse_obj_as from docarray.typing.tensor.audio.audio_ndarray import MAX_INT_16, AudioNdArray from docarray.typing.url.any_url import AnyUrl if TYPE_CHECKING: from pydantic import BaseConfig from pydantic.fields import ModelField from docarray.proto import NodeProto T = TypeVar('T', bound='AudioUrl') AUDIO_FILE_FORMATS = ['wav'] class AudioUrl(AnyUrl): """ URL to a .wav file. Can be remote (web) URL, or a local file path. """ def _to_node_protobuf(self: T) -> 'NodeProto': """Convert Document into a NodeProto protobuf message. This function should be called when the Document is nested into another Document that needs to be converted into a protobuf :return: the nested item protobuf message """ from docarray.proto import NodeProto return NodeProto(audio_url=str(self)) @classmethod def validate( cls: Type[T], value: Union[T, np.ndarray, Any], field: 'ModelField', config: 'BaseConfig', ) -> T: url = super().validate(value, field, config) # basic url validation has_audio_extension = any(ext in url for ext in AUDIO_FILE_FORMATS) if not has_audio_extension: raise ValueError( f'Audio URL must have one of the following extensions:' f'{AUDIO_FILE_FORMATS}' ) return cls(str(url), scheme=None) def load(self: T, dtype: str = 'float32') -> AudioNdArray: """ Load the data from the url into an AudioNdArray. :param dtype: Data-type of the returned array; default: float32. :return: AudioNdArray representing the audio file content. EXAMPLE USAGE .. code-block:: python from docarray import BaseDocument import numpy as np from docarray.typing import AudioUrl class MyDoc(Document): audio_url: AudioUrl audio_tensor: AudioNdArray doc = MyDoc(audio_url="toydata/hello.wav") doc.audio_tensor = doc.audio_url.load() assert isinstance(doc.audio_tensor, np.ndarray) """ import io file: Union[io.BytesIO, T] if self.startswith('http'): import requests resp = requests.get(self) resp.raise_for_status() file = io.BytesIO() file.write(resp.content) file.seek(0) else: file = self # note wave is Python built-in mod. https://docs.python.org/3/library/wave.html with wave.open(file) as ifile: samples = ifile.getnframes() audio = ifile.readframes(samples) # Convert buffer to float32 using NumPy audio_as_np_int16 = np.frombuffer(audio, dtype=np.int16) audio_as_np_float32 = audio_as_np_int16.astype(dtype=dtype) # Normalise float32 array so that values are between -1.0 and +1.0 audio_norm = audio_as_np_float32 / MAX_INT_16 channels = ifile.getnchannels() if channels == 2: # 1 for mono, 2 for stereo audio_stereo = np.empty((int(len(audio_norm) / channels), channels)) audio_stereo[:, 0] = audio_norm[range(0, len(audio_norm), 2)] audio_stereo[:, 1] = audio_norm[range(1, len(audio_norm), 2)] return parse_obj_as(AudioNdArray, audio_stereo) else: return parse_obj_as(AudioNdArray, audio_norm)
import wave from typing import TYPE_CHECKING, Any, Type, TypeVar, Union import numpy as np from pydantic import parse_obj_as from docarray.typing.tensor.audio.audio_ndarray import MAX_INT_16, AudioNdArray from docarray.typing.url.any_url import AnyUrl if TYPE_CHECKING: from pydantic import BaseConfig from pydantic.fields import ModelField from docarray.proto import NodeProto T = TypeVar('T', bound='AudioUrl') AUDIO_FILE_FORMATS = ['wav'] class AudioUrl(AnyUrl): """ URL to a .wav file. Can be remote (web) URL, or a local file path. """ def _to_node_protobuf(self: T) -> 'NodeProto': """Convert Document into a NodeProto protobuf message. This function should be called when the Document is nested into another Document that needs to be converted into a protobuf :return: the nested item protobuf message """ from docarray.proto import NodeProto return NodeProto(audio_url=str(self)) @classmethod def validate( cls: Type[T], value: Union[T, np.ndarray, Any], field: 'ModelField', config: 'BaseConfig', ) -> T: url = super().validate(value, field, config) # basic url validation has_audio_extension = any(ext in url for ext in AUDIO_FILE_FORMATS) if not has_audio_extension: raise ValueError( f'Audio URL must have one of the following extensions:' f'{AUDIO_FILE_FORMATS}' ) return cls(str(url), scheme=None) def load(self: T, dtype: str = 'float32') -> AudioNdArray: """ Load the data from the url into an AudioNdArray. :param dtype: Data-type of the returned array; default: float32. :return: AudioNdArray representing the audio file content. EXAMPLE USAGE .. code-block:: python from docarray import Document import numpy as np from docarray.typing import AudioUrl class MyDoc(Document): audio_url: AudioUrl audio_tensor: AudioNdArray doc = MyDoc(audio_url="toydata/hello.wav") doc.audio_tensor = doc.audio_url.load() assert isinstance(doc.audio_tensor, np.ndarray) """ import io file: Union[io.BytesIO, T] if self.startswith('http'): import requests resp = requests.get(self) resp.raise_for_status() file = io.BytesIO() file.write(resp.content) file.seek(0) else: file = self # note wave is Python built-in mod. https://docs.python.org/3/library/wave.html with wave.open(file) as ifile: samples = ifile.getnframes() audio = ifile.readframes(samples) # Convert buffer to float32 using NumPy audio_as_np_int16 = np.frombuffer(audio, dtype=np.int16) audio_as_np_float32 = audio_as_np_int16.astype(dtype=dtype) # Normalise float32 array so that values are between -1.0 and +1.0 audio_norm = audio_as_np_float32 / MAX_INT_16 channels = ifile.getnchannels() if channels == 2: # 1 for mono, 2 for stereo audio_stereo = np.empty((int(len(audio_norm) / channels), channels)) audio_stereo[:, 0] = audio_norm[range(0, len(audio_norm), 2)] audio_stereo[:, 1] = audio_norm[range(1, len(audio_norm), 2)] return parse_obj_as(AudioNdArray, audio_stereo) else: return parse_obj_as(AudioNdArray, audio_norm)
""" This file loads sentences from a provided text file. It is expected, that the there is one sentence per line in that text file. TSDAE will be training using these sentences. Checkpoints are stored every 500 steps to the output folder. Usage: python train_tsdae_from_file.py path/to/sentences.txt """ import gzip import logging import sys from datetime import datetime import tqdm from torch.utils.data import DataLoader from sentence_transformers import LoggingHandler, SentenceTransformer, datasets, losses, models #### Just some code to print debug information to stdout logging.basicConfig( format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO, handlers=[LoggingHandler()] ) #### /print debug information to stdout # Train Parameters model_name = "bert-base-uncased" batch_size = 8 # Input file path (a text file, each line a sentence) if len(sys.argv) < 2: print("Run this script with: python {} path/to/sentences.txt".format(sys.argv[0])) exit() filepath = sys.argv[1] # Save path to store our model output_name = "" if len(sys.argv) >= 3: output_name = "-" + sys.argv[2].replace(" ", "_").replace("/", "_").replace("\\", "_") model_output_path = "output/train_tsdae{}-{}".format(output_name, datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) ################# Read the train corpus ################# train_sentences = [] with gzip.open(filepath, "rt", encoding="utf8") if filepath.endswith(".gz") else open( filepath, encoding="utf8" ) as fIn: for line in tqdm.tqdm(fIn, desc="Read file"): line = line.strip() if len(line) >= 10: train_sentences.append(line) logging.info("{} train sentences".format(len(train_sentences))) ################# Initialize an SBERT model ################# word_embedding_model = models.Transformer(model_name) # Apply **cls** pooling to get one fixed sized sentence vector pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(), "cls") model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) ################# Train and evaluate the model (it needs about 1 hour for one epoch of AskUbuntu) ################# # We wrap our training sentences in the DenoisingAutoEncoderDataset to add deletion noise on the fly train_dataset = datasets.DenoisingAutoEncoderDataset(train_sentences) train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, drop_last=True) train_loss = losses.DenoisingAutoEncoderLoss(model, decoder_name_or_path=model_name, tie_encoder_decoder=True) logging.info("Start training") model.fit( train_objectives=[(train_dataloader, train_loss)], epochs=1, weight_decay=0, scheduler="constantlr", optimizer_params={"lr": 3e-5}, show_progress_bar=True, checkpoint_path=model_output_path, use_amp=False, # Set to True, if your GPU supports FP16 cores )
""" This file loads sentences from a provided text file. It is expected, that the there is one sentence per line in that text file. TSDAE will be training using these sentences. Checkpoints are stored every 500 steps to the output folder. Usage: python train_tsdae_from_file.py path/to/sentences.txt """ from sentence_transformers import SentenceTransformer, LoggingHandler from sentence_transformers import models, datasets, losses import logging import gzip from torch.utils.data import DataLoader from datetime import datetime import sys import tqdm #### Just some code to print debug information to stdout logging.basicConfig( format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO, handlers=[LoggingHandler()] ) #### /print debug information to stdout # Train Parameters model_name = "bert-base-uncased" batch_size = 8 # Input file path (a text file, each line a sentence) if len(sys.argv) < 2: print("Run this script with: python {} path/to/sentences.txt".format(sys.argv[0])) exit() filepath = sys.argv[1] # Save path to store our model output_name = "" if len(sys.argv) >= 3: output_name = "-" + sys.argv[2].replace(" ", "_").replace("/", "_").replace("\\", "_") model_output_path = "output/train_tsdae{}-{}".format(output_name, datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) ################# Read the train corpus ################# train_sentences = [] with gzip.open(filepath, "rt", encoding="utf8") if filepath.endswith(".gz") else open( filepath, encoding="utf8" ) as fIn: for line in tqdm.tqdm(fIn, desc="Read file"): line = line.strip() if len(line) >= 10: train_sentences.append(line) logging.info("{} train sentences".format(len(train_sentences))) ################# Initialize an SBERT model ################# word_embedding_model = models.Transformer(model_name) # Apply **cls** pooling to get one fixed sized sentence vector pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension(), "cls") model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) ################# Train and evaluate the model (it needs about 1 hour for one epoch of AskUbuntu) ################# # We wrap our training sentences in the DenoisingAutoEncoderDataset to add deletion noise on the fly train_dataset = datasets.DenoisingAutoEncoderDataset(train_sentences) train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, drop_last=True) train_loss = losses.DenoisingAutoEncoderLoss(model, decoder_name_or_path=model_name, tie_encoder_decoder=True) logging.info("Start training") model.fit( train_objectives=[(train_dataloader, train_loss)], epochs=1, weight_decay=0, scheduler="constantlr", optimizer_params={"lr": 3e-5}, show_progress_bar=True, checkpoint_path=model_output_path, use_amp=False, # Set to True, if your GPU supports FP16 cores )
from typing import Any, Dict, List, Optional, Tuple from copy import deepcopy from presidio_anonymizer.operators import Operator, OperatorType from llama_index.core.postprocessor.types import BaseNodePostprocessor from llama_index.core.schema import MetadataMode, NodeWithScore, QueryBundle from presidio_analyzer import AnalyzerEngine from presidio_anonymizer import AnonymizerEngine from presidio_anonymizer.entities import OperatorConfig class EntityTypeCountAnonymizer(Operator): """ Anonymizer which replaces the entity value with an type counter per entity. """ REPLACING_FORMAT = "<{entity_type}_{index}>" def operate(self, text: str, params: Dict[str, Any]) -> str: """Anonymize the input text.""" entity_type: str = params["entity_type"] entity_mapping: Dict[str, Dict] = params["entity_mapping"] deanonymize_mapping: Dict[str, str] = params["deanonymize_mapping"] entity_mapping_for_type = entity_mapping.get(entity_type) if not entity_mapping_for_type: entity_mapping_for_type = entity_mapping[entity_type] = {} if text in entity_mapping_for_type: return entity_mapping_for_type[text] new_text = self.REPLACING_FORMAT.format( entity_type=entity_type, index=len(entity_mapping_for_type) + 1 ) entity_mapping[entity_type][text] = new_text deanonymize_mapping[new_text] = text return new_text def validate(self, params: Dict[str, Any]) -> None: """Validate operator parameters.""" if "entity_mapping" not in params: raise ValueError("An input Dict called `entity_mapping` is required.") if "entity_type" not in params: raise ValueError("An entity_type param is required.") if "deanonymize_mapping" not in params: raise ValueError("A deanonymize_mapping param is required.") def operator_name(self) -> str: return self.__class__.__name__ def operator_type(self) -> OperatorType: return OperatorType.Anonymize class PresidioPIINodePostprocessor(BaseNodePostprocessor): """ presidio PII Node processor. Uses a presidio to analyse PIIs. """ pii_node_info_key: str = "__pii_node_info__" entity_mapping: Dict[str, Dict] = {} mapping: Dict[str, str] = {} @classmethod def class_name(cls) -> str: return "PresidioPIINodePostprocessor" def mask_pii(self, text: str) -> Tuple[str, Dict]: analyzer = AnalyzerEngine() results = analyzer.analyze(text=text, language="en") engine = AnonymizerEngine() engine.add_anonymizer(EntityTypeCountAnonymizer) new_text = engine.anonymize( text=text, analyzer_results=results, operators={ "DEFAULT": OperatorConfig( "EntityTypeCountAnonymizer", { "entity_mapping": self.entity_mapping, "deanonymize_mapping": self.mapping, }, ) }, ) return new_text.text def _postprocess_nodes( self, nodes: List[NodeWithScore], query_bundle: Optional[QueryBundle] = None, ) -> List[NodeWithScore]: """Postprocess nodes.""" # swap out text from nodes, with the original node mappings new_nodes = [] for node_with_score in nodes: node = node_with_score.node new_text = self.mask_pii(node.get_content(metadata_mode=MetadataMode.LLM)) new_node = deepcopy(node) new_node.excluded_embed_metadata_keys.append(self.pii_node_info_key) new_node.excluded_llm_metadata_keys.append(self.pii_node_info_key) new_node.metadata[self.pii_node_info_key] = self.mapping new_node.set_content(new_text) new_nodes.append(NodeWithScore(node=new_node, score=node_with_score.score)) return new_nodes
from typing import Any, Dict, List, Optional, Tuple from copy import deepcopy from presidio_anonymizer.operators import Operator, OperatorType from llama_index.core.postprocessor.types import BaseNodePostprocessor from llama_index.core.schema import MetadataMode, NodeWithScore, QueryBundle from presidio_analyzer import AnalyzerEngine from presidio_anonymizer import AnonymizerEngine from presidio_anonymizer.entities import OperatorConfig class EntityTypeCountAnonymizer(Operator): """ Anonymizer which replaces the entity value with an type counter per entity. """ REPLACING_FORMAT = "<{entity_type}_{index}>" def operate(self, text: str, params: Dict[str, Any]) -> str: """Anonymize the input text.""" entity_type: str = params["entity_type"] entity_mapping: Dict[str, Dict] = params["entity_mapping"] deanonymize_mapping: Dict[str, str] = params["deanonymize_mapping"] entity_mapping_for_type = entity_mapping.get(entity_type) if not entity_mapping_for_type: entity_mapping_for_type = entity_mapping[entity_type] = {} if text in entity_mapping_for_type: return entity_mapping_for_type[text] new_text = self.REPLACING_FORMAT.format( entity_type=entity_type, index=len(entity_mapping_for_type) + 1 ) entity_mapping[entity_type][text] = new_text deanonymize_mapping[new_text] = text return new_text def validate(self, params: Dict[str, Any]) -> None: """Validate operator parameters.""" if "entity_mapping" not in params: raise ValueError("An input Dict called `entity_mapping` is required.") if "entity_type" not in params: raise ValueError("An entity_type param is required.") if "deanonymize_mapping" not in params: raise ValueError("A deanonymize_mapping param is required.") def operator_name(self) -> str: return self.__class__.__name__ def operator_type(self) -> OperatorType: return OperatorType.Anonymize class PresidioPIINodePostprocessor(BaseNodePostprocessor): """presidio PII Node processor. Uses a presidio to analyse PIIs. """ pii_node_info_key: str = "__pii_node_info__" entity_mapping: Dict[str, Dict] = {} mapping: Dict[str, str] = {} @classmethod def class_name(cls) -> str: return "PresidioPIINodePostprocessor" def mask_pii(self, text: str) -> Tuple[str, Dict]: analyzer = AnalyzerEngine() results = analyzer.analyze(text=text, language="en") engine = AnonymizerEngine() engine.add_anonymizer(EntityTypeCountAnonymizer) new_text = engine.anonymize( text=text, analyzer_results=results, operators={ "DEFAULT": OperatorConfig( "EntityTypeCountAnonymizer", { "entity_mapping": self.entity_mapping, "deanonymize_mapping": self.mapping, }, ) }, ) return new_text.text def _postprocess_nodes( self, nodes: List[NodeWithScore], query_bundle: Optional[QueryBundle] = None, ) -> List[NodeWithScore]: """Postprocess nodes.""" # swap out text from nodes, with the original node mappings new_nodes = [] for node_with_score in nodes: node = node_with_score.node new_text = self.mask_pii(node.get_content(metadata_mode=MetadataMode.LLM)) new_node = deepcopy(node) new_node.excluded_embed_metadata_keys.append(self.pii_node_info_key) new_node.excluded_llm_metadata_keys.append(self.pii_node_info_key) new_node.metadata[self.pii_node_info_key] = self.mapping new_node.set_content(new_text) new_nodes.append(NodeWithScore(node=new_node, score=node_with_score.score)) return new_nodes
from __future__ import annotations import logging from typing import TYPE_CHECKING, Any from sentence_transformers.evaluation import TranslationEvaluator if TYPE_CHECKING: import numpy as np from torch import Tensor from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder logger = logging.getLogger(__name__) class SparseTranslationEvaluator(TranslationEvaluator): """ This evaluator extends :class:`TranslationEvaluator` but is specifically designed for sparse encoder models. Given two sets of sentences in different languages, e.g. (en_1, en_2, en_3...) and (fr_1, fr_2, fr_3, ...), and assuming that fr_i is the translation of en_i. Checks if vec(en_i) has the highest similarity to vec(fr_i). Computes the accuracy in both directions The labels need to indicate the similarity between the sentences. Args: source_sentences (List[str]): List of sentences in the source language. target_sentences (List[str]): List of sentences in the target language. show_progress_bar (bool): Whether to show a progress bar when computing embeddings. Defaults to False. batch_size (int): The batch size to compute sentence embeddings. Defaults to 16. name (str): The name of the evaluator. Defaults to an empty string. print_wrong_matches (bool): Whether to print incorrect matches. Defaults to False. write_csv (bool): Whether to write the evaluation results to a CSV file. Defaults to True. truncate_dim (int, optional): The dimension to truncate sentence embeddings to. If None, the model's current truncation dimension will be used. Defaults to None. Example: :: import logging from datasets import load_dataset from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseTranslationEvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model, not mutilingual but hope to see some on the hub soon model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load a parallel sentences dataset dataset = load_dataset("sentence-transformers/parallel-sentences-news-commentary", "en-nl", split="train[:1000]") # Initialize the TranslationEvaluator using the same texts from two languages translation_evaluator = SparseTranslationEvaluator( source_sentences=dataset["english"], target_sentences=dataset["non_english"], name="news-commentary-en-nl", ) results = translation_evaluator(model) ''' Evaluating translation matching Accuracy of the model on the news-commentary-en-nl dataset: Accuracy src2trg: 41.40 Accuracy trg2src: 47.70 ''' # Print the results print(f"Primary metric: {translation_evaluator.primary_metric}") # => Primary metric: news-commentary-en-nl_mean_accuracy print(f"Primary metric value: {results[translation_evaluator.primary_metric]:.4f}") # => Primary metric value: 0.4455 """ def __init__( self, source_sentences: list[str], target_sentences: list[str], show_progress_bar: bool = False, batch_size: int = 16, name: str = "", print_wrong_matches: bool = False, write_csv: bool = True, truncate_dim: int | None = None, ): return super().__init__( source_sentences, target_sentences, show_progress_bar=show_progress_bar, batch_size=batch_size, name=name, print_wrong_matches=print_wrong_matches, write_csv=write_csv, truncate_dim=truncate_dim, ) def __call__( self, model: SparseEncoder, output_path: str = None, epoch: int = -1, steps: int = -1 ) -> dict[str, float]: return super().__call__(model, output_path=output_path, epoch=epoch, steps=steps) def embed_inputs( self, model: SparseEncoder, sentences: str | list[str] | np.ndarray, **kwargs, ) -> list[Tensor]: kwargs["truncate_dim"] = self.truncate_dim return model.encode( sentences, batch_size=self.batch_size, show_progress_bar=self.show_progress_bar, convert_to_tensor=False, convert_to_sparse_tensor=True, save_on_cpu=True, **kwargs, ) def store_metrics_in_model_card_data( self, model: SparseEncoder, metrics: dict[str, Any], epoch: int = 0, step: int = 0 ) -> None: model.model_card_data.set_evaluation_metrics(self, metrics, epoch=epoch, step=step)
from __future__ import annotations import logging from typing import TYPE_CHECKING, Any from sentence_transformers.evaluation import TranslationEvaluator if TYPE_CHECKING: import numpy as np from torch import Tensor from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder logger = logging.getLogger(__name__) class SparseTranslationEvaluator(TranslationEvaluator): """ This evaluator extends :class:`TranslationEvaluator` but is specifically designed for sparse encoder models. Given two sets of sentences in different languages, e.g. (en_1, en_2, en_3...) and (fr_1, fr_2, fr_3, ...), and assuming that fr_i is the translation of en_i. Checks if vec(en_i) has the highest similarity to vec(fr_i). Computes the accuracy in both directions The labels need to indicate the similarity between the sentences. Args: source_sentences (List[str]): List of sentences in the source language. target_sentences (List[str]): List of sentences in the target language. show_progress_bar (bool): Whether to show a progress bar when computing embeddings. Defaults to False. batch_size (int): The batch size to compute sentence embeddings. Defaults to 16. name (str): The name of the evaluator. Defaults to an empty string. print_wrong_matches (bool): Whether to print incorrect matches. Defaults to False. write_csv (bool): Whether to write the evaluation results to a CSV file. Defaults to True. truncate_dim (int, optional): The dimension to truncate sentence embeddings to. If None, the model's current truncation dimension will be used. Defaults to None. Example: :: import logging from datasets import load_dataset from sentence_transformers.sparse_encoder import ( SparseEncoder, SparseTranslationEvaluator, ) logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model, not mutilingual but hope to see some on the hub soon model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load a parallel sentences dataset dataset = load_dataset("sentence-transformers/parallel-sentences-news-commentary", "en-nl", split="train[:1000]") # Initialize the TranslationEvaluator using the same texts from two languages translation_evaluator = SparseTranslationEvaluator( source_sentences=dataset["english"], target_sentences=dataset["non_english"], name="news-commentary-en-nl", ) results = translation_evaluator(model) ''' Evaluating translation matching Accuracy of the model on the news-commentary-en-nl dataset: Accuracy src2trg: 41.40 Accuracy trg2src: 47.70 ''' # Print the results print(f"Primary metric: {translation_evaluator.primary_metric}") # => Primary metric: news-commentary-en-nl_mean_accuracy print(f"Primary metric value: {results[translation_evaluator.primary_metric]:.4f}") # => Primary metric value: 0.4455 """ def __init__( self, source_sentences: list[str], target_sentences: list[str], show_progress_bar: bool = False, batch_size: int = 16, name: str = "", print_wrong_matches: bool = False, write_csv: bool = True, truncate_dim: int | None = None, ): return super().__init__( source_sentences, target_sentences, show_progress_bar=show_progress_bar, batch_size=batch_size, name=name, print_wrong_matches=print_wrong_matches, write_csv=write_csv, truncate_dim=truncate_dim, ) def __call__( self, model: SparseEncoder, output_path: str = None, epoch: int = -1, steps: int = -1 ) -> dict[str, float]: return super().__call__(model, output_path=output_path, epoch=epoch, steps=steps) def embed_inputs( self, model: SparseEncoder, sentences: str | list[str] | np.ndarray, **kwargs, ) -> list[Tensor]: kwargs["truncate_dim"] = self.truncate_dim return model.encode( sentences, batch_size=self.batch_size, show_progress_bar=self.show_progress_bar, convert_to_tensor=False, convert_to_sparse_tensor=True, save_on_cpu=True, **kwargs, ) def store_metrics_in_model_card_data( self, model: SparseEncoder, metrics: dict[str, Any], epoch: int = 0, step: int = 0 ) -> None: model.model_card_data.set_evaluation_metrics(self, metrics, epoch=epoch, step=step)
import logging from langchain_core.callbacks import ( AsyncCallbackManagerForRetrieverRun, CallbackManagerForRetrieverRun, ) from langchain_core.documents import Document from langchain_core.language_models import BaseLLM from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import BasePromptTemplate from langchain_core.prompts.prompt import PromptTemplate from langchain_core.retrievers import BaseRetriever from langchain_core.runnables import Runnable logger = logging.getLogger(__name__) # Default template DEFAULT_TEMPLATE = """You are an assistant tasked with taking a natural language \ query from a user and converting it into a query for a vectorstore. \ In this process, you strip out information that is not relevant for \ the retrieval task. Here is the user query: {question}""" # Default prompt DEFAULT_QUERY_PROMPT = PromptTemplate.from_template(DEFAULT_TEMPLATE) class RePhraseQueryRetriever(BaseRetriever): """Given a query, use an LLM to re-phrase it. Then, retrieve docs for the re-phrased query.""" retriever: BaseRetriever llm_chain: Runnable @classmethod def from_llm( cls, retriever: BaseRetriever, llm: BaseLLM, prompt: BasePromptTemplate = DEFAULT_QUERY_PROMPT, ) -> "RePhraseQueryRetriever": """Initialize from llm using default template. The prompt used here expects a single input: `question` Args: retriever: retriever to query documents from llm: llm for query generation using DEFAULT_QUERY_PROMPT prompt: prompt template for query generation Returns: RePhraseQueryRetriever """ llm_chain = prompt | llm | StrOutputParser() return cls( retriever=retriever, llm_chain=llm_chain, ) def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun, ) -> list[Document]: """Get relevant documents given a user question. Args: query: user question Returns: Relevant documents for re-phrased question """ re_phrased_question = self.llm_chain.invoke( query, {"callbacks": run_manager.get_child()} ) logger.info(f"Re-phrased question: {re_phrased_question}") return self.retriever.invoke( re_phrased_question, config={"callbacks": run_manager.get_child()} ) async def _aget_relevant_documents( self, query: str, *, run_manager: AsyncCallbackManagerForRetrieverRun, ) -> list[Document]: raise NotImplementedError
import logging from langchain_core.callbacks import ( AsyncCallbackManagerForRetrieverRun, CallbackManagerForRetrieverRun, ) from langchain_core.documents import Document from langchain_core.language_models import BaseLLM from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import BasePromptTemplate from langchain_core.prompts.prompt import PromptTemplate from langchain_core.retrievers import BaseRetriever from langchain_core.runnables import Runnable logger = logging.getLogger(__name__) # Default template DEFAULT_TEMPLATE = """You are an assistant tasked with taking a natural language \ query from a user and converting it into a query for a vectorstore. \ In this process, you strip out information that is not relevant for \ the retrieval task. Here is the user query: {question}""" # Default prompt DEFAULT_QUERY_PROMPT = PromptTemplate.from_template(DEFAULT_TEMPLATE) class RePhraseQueryRetriever(BaseRetriever): """Given a query, use an LLM to re-phrase it. Then, retrieve docs for the re-phrased query.""" retriever: BaseRetriever llm_chain: Runnable @classmethod def from_llm( cls, retriever: BaseRetriever, llm: BaseLLM, prompt: BasePromptTemplate = DEFAULT_QUERY_PROMPT, ) -> "RePhraseQueryRetriever": """Initialize from llm using default template. The prompt used here expects a single input: `question` Args: retriever: retriever to query documents from llm: llm for query generation using DEFAULT_QUERY_PROMPT prompt: prompt template for query generation Returns: RePhraseQueryRetriever """ llm_chain = prompt | llm | StrOutputParser() return cls( retriever=retriever, llm_chain=llm_chain, ) def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun, ) -> list[Document]: """Get relevant documents given a user question. Args: query: user question Returns: Relevant documents for re-phrased question """ re_phrased_question = self.llm_chain.invoke( query, {"callbacks": run_manager.get_child()} ) logger.info(f"Re-phrased question: {re_phrased_question}") docs = self.retriever.invoke( re_phrased_question, config={"callbacks": run_manager.get_child()} ) return docs async def _aget_relevant_documents( self, query: str, *, run_manager: AsyncCallbackManagerForRetrieverRun, ) -> list[Document]: raise NotImplementedError
from dataclasses import dataclass from functools import partial from typing import Callable import torch import torchaudio from torchaudio.models import conv_tasnet_base, hdemucs_high @dataclass class SourceSeparationBundle: """torchaudio.pipelines.SourceSeparationBundle() Dataclass that bundles components for performing source separation. Example >>> import torchaudio >>> from torchaudio.pipelines import CONVTASNET_BASE_LIBRI2MIX >>> import torch >>> >>> # Build the separation model. >>> model = CONVTASNET_BASE_LIBRI2MIX.get_model() >>> 100%|███████████████████████████████|19.1M/19.1M [00:04<00:00, 4.93MB/s] >>> >>> # Instantiate the test set of Libri2Mix dataset. >>> dataset = torchaudio.datasets.LibriMix("/home/datasets/", subset="test") >>> >>> # Apply source separation on mixture audio. >>> for i, data in enumerate(dataset): >>> sample_rate, mixture, clean_sources = data >>> # Make sure the shape of input suits the model requirement. >>> mixture = mixture.reshape(1, 1, -1) >>> estimated_sources = model(mixture) >>> score = si_snr_pit(estimated_sources, clean_sources) # for demonstration >>> print(f"Si-SNR score is : {score}.) >>> break >>> Si-SNR score is : 16.24. >>> """ _model_path: str _model_factory_func: Callable[[], torch.nn.Module] _sample_rate: int @property def sample_rate(self) -> int: """Sample rate of the audio that the model is trained on. :type: int """ return self._sample_rate def get_model(self) -> torch.nn.Module: """Construct the model and load the pretrained weight.""" model = self._model_factory_func() path = torchaudio.utils.download_asset(self._model_path) state_dict = torch.load(path) model.load_state_dict(state_dict) model.eval() return model CONVTASNET_BASE_LIBRI2MIX = SourceSeparationBundle( _model_path="models/conv_tasnet_base_libri2mix.pt", _model_factory_func=partial(conv_tasnet_base, num_sources=2), _sample_rate=8000, ) CONVTASNET_BASE_LIBRI2MIX.__doc__ = """Pre-trained Source Separation pipeline with *ConvTasNet* :cite:`Luo_2019` trained on *Libri2Mix dataset* :cite:`cosentino2020librimix`. The source separation model is constructed by :py:func:`torchaudio.models.conv_tasnet_base` and is trained using the training script ``lightning_train.py`` `here <https://github.com/pytorch/audio/tree/release/0.12/examples/source_separation/>`__ with default arguments. Please refer to :py:class:`SourceSeparationBundle` for usage instructions. """ HDEMUCS_HIGH_MUSDB_PLUS = SourceSeparationBundle( _model_path="models/hdemucs_high_trained.pt", _model_factory_func=partial(hdemucs_high, sources=["drums", "bass", "other", "vocals"]), _sample_rate=44100, ) HDEMUCS_HIGH_MUSDB_PLUS.__doc__ = """Pre-trained *Hybrid Demucs* :cite:`defossez2021hybrid` pipeline for music source separation trained on MUSDB-HQ :cite:`MUSDB18HQ` and additional internal training data. The model is constructed by :py:func:`torchaudio.prototype.models.hdemucs_high`. Training was performed in the original HDemucs repository `here <https://github.com/facebookresearch/demucs/>`__. Please refer to :py:class:`SourceSeparationBundle` for usage instructions. """ HDEMUCS_HIGH_MUSDB = SourceSeparationBundle( _model_path="models/hdemucs_high_musdbhq_only.pt", _model_factory_func=partial(hdemucs_high, sources=["drums", "bass", "other", "vocals"]), _sample_rate=44100, ) HDEMUCS_HIGH_MUSDB.__doc__ = """Pre-trained *Hybrid Demucs* :cite:`defossez2021hybrid` pipeline for music source separation trained on MUSDB-HQ :cite:`MUSDB18HQ`. The model is constructed by :py:func:`torchaudio.prototype.models.hdemucs_high`. Training was performed in the original HDemucs repository `here <https://github.com/facebookresearch/demucs/>`__. Please refer to :py:class:`SourceSeparationBundle` for usage instructions. """
from dataclasses import dataclass from functools import partial from typing import Callable import torch import torchaudio from torchaudio.models import conv_tasnet_base, hdemucs_high @dataclass class SourceSeparationBundle: """torchaudio.pipelines.SourceSeparationBundle() Dataclass that bundles components for performing source separation. Example >>> import torchaudio >>> from torchaudio.pipelines import CONVTASNET_BASE_LIBRI2MIX >>> import torch >>> >>> # Build the separation model. >>> model = CONVTASNET_BASE_LIBRI2MIX.get_model() >>> 100%|███████████████████████████████|19.1M/19.1M [00:04<00:00, 4.93MB/s] >>> >>> # Instantiate the test set of Libri2Mix dataset. >>> dataset = torchaudio.datasets.LibriMix("/home/datasets/", subset="test") >>> >>> # Apply source separation on mixture audio. >>> for i, data in enumerate(dataset): >>> sample_rate, mixture, clean_sources = data >>> # Make sure the shape of input suits the model requirement. >>> mixture = mixture.reshape(1, 1, -1) >>> estimated_sources = model(mixture) >>> score = si_snr_pit(estimated_sources, clean_sources) # for demonstration >>> print(f"Si-SNR score is : {score}.) >>> break >>> Si-SNR score is : 16.24. >>> """ _model_path: str _model_factory_func: Callable[[], torch.nn.Module] _sample_rate: int @property def sample_rate(self) -> int: """Sample rate of the audio that the model is trained on. :type: int """ return self._sample_rate def get_model(self) -> torch.nn.Module: """Construct the model and load the pretrained weight.""" model = self._model_factory_func() path = torchaudio.utils.download_asset(self._model_path) state_dict = torch.load(path) model.load_state_dict(state_dict) model.eval() return model CONVTASNET_BASE_LIBRI2MIX = SourceSeparationBundle( _model_path="models/conv_tasnet_base_libri2mix.pt", _model_factory_func=partial(conv_tasnet_base, num_sources=2), _sample_rate=8000, ) CONVTASNET_BASE_LIBRI2MIX.__doc__ = """Pre-trained Source Separation pipeline with *ConvTasNet* [:footcite:`Luo_2019`] trained on *Libri2Mix dataset* [:footcite:`cosentino2020librimix`]. The source separation model is constructed by :py:func:`torchaudio.models.conv_tasnet_base` and is trained using the training script ``lightning_train.py`` `here <https://github.com/pytorch/audio/tree/release/0.12/examples/source_separation/>`__ with default arguments. Please refer to :py:class:`SourceSeparationBundle` for usage instructions. """ HDEMUCS_HIGH_MUSDB_PLUS = SourceSeparationBundle( _model_path="models/hdemucs_high_trained.pt", _model_factory_func=partial(hdemucs_high, sources=["drums", "bass", "other", "vocals"]), _sample_rate=44100, ) HDEMUCS_HIGH_MUSDB_PLUS.__doc__ = """Pre-trained *Hybrid Demucs* [:footcite:`defossez2021hybrid`] pipeline for music source separation trained on MUSDB-HQ [:footcite:`MUSDB18HQ`] and additional internal training data. The model is constructed by :py:func:`torchaudio.prototype.models.hdemucs_high`. Training was performed in the original HDemucs repository `here <https://github.com/facebookresearch/demucs/>`__. Please refer to :py:class:`SourceSeparationBundle` for usage instructions. """ HDEMUCS_HIGH_MUSDB = SourceSeparationBundle( _model_path="models/hdemucs_high_musdbhq_only.pt", _model_factory_func=partial(hdemucs_high, sources=["drums", "bass", "other", "vocals"]), _sample_rate=44100, ) HDEMUCS_HIGH_MUSDB.__doc__ = """Pre-trained *Hybrid Demucs* [:footcite:`defossez2021hybrid`] pipeline for music source separation trained on MUSDB-HQ [:footcite:`MUSDB18HQ`]. The model is constructed by :py:func:`torchaudio.prototype.models.hdemucs_high`. Training was performed in the original HDemucs repository `here <https://github.com/facebookresearch/demucs/>`__. Please refer to :py:class:`SourceSeparationBundle` for usage instructions. """
"""MistralAI embeddings file.""" from typing import Any, List, Optional from llama_index.core.base.embeddings.base import ( DEFAULT_EMBED_BATCH_SIZE, BaseEmbedding, ) from llama_index.core.bridge.pydantic import PrivateAttr from llama_index.core.callbacks.base import CallbackManager from llama_index.core.base.llms.generic_utils import get_from_param_or_env from mistralai import Mistral class MistralAIEmbedding(BaseEmbedding): """ Class for MistralAI embeddings. Args: model_name (str): Model for embedding. Defaults to "mistral-embed". api_key (Optional[str]): API key to access the model. Defaults to None. """ # Instance variables initialized via Pydantic's mechanism _client: Mistral = PrivateAttr() def __init__( self, model_name: str = "mistral-embed", api_key: Optional[str] = None, embed_batch_size: int = DEFAULT_EMBED_BATCH_SIZE, callback_manager: Optional[CallbackManager] = None, **kwargs: Any, ): super().__init__( model_name=model_name, embed_batch_size=embed_batch_size, callback_manager=callback_manager, **kwargs, ) api_key = get_from_param_or_env("api_key", api_key, "MISTRAL_API_KEY", "") if not api_key: raise ValueError( "You must provide an API key to use mistralai. " "You can either pass it in as an argument or set it `MISTRAL_API_KEY`." ) self._client = Mistral(api_key=api_key) @classmethod def class_name(cls) -> str: return "MistralAIEmbedding" def _get_query_embedding(self, query: str) -> List[float]: """Get query embedding.""" return ( self._client.embeddings.create(model=self.model_name, inputs=[query]) .data[0] .embedding ) async def _aget_query_embedding(self, query: str) -> List[float]: """The asynchronous version of _get_query_embedding.""" return ( ( await self._client.embeddings.create_async( model=self.model_name, inputs=[query] ) ) .data[0] .embedding ) def _get_text_embedding(self, text: str) -> List[float]: """Get text embedding.""" return ( self._client.embeddings.create(model=self.model_name, inputs=[text]) .data[0] .embedding ) async def _aget_text_embedding(self, text: str) -> List[float]: """Asynchronously get text embedding.""" return ( await self._client.embeddings.create( model=self.model_name, inputs=[text], ) .data[0] .embedding ) def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]: """Get text embeddings.""" embedding_response = self._client.embeddings.create( model=self.model_name, inputs=texts ).data return [embed.embedding for embed in embedding_response] async def _aget_text_embeddings(self, texts: List[str]) -> List[List[float]]: """Asynchronously get text embeddings.""" embedding_response = await self._client.embeddings.create_async( model=self.model_name, inputs=texts ) return [embed.embedding for embed in embedding_response.data]
"""MistralAI embeddings file.""" from typing import Any, List, Optional from llama_index.core.base.embeddings.base import ( DEFAULT_EMBED_BATCH_SIZE, BaseEmbedding, ) from llama_index.core.bridge.pydantic import PrivateAttr from llama_index.core.callbacks.base import CallbackManager from llama_index.core.base.llms.generic_utils import get_from_param_or_env from mistralai import Mistral class MistralAIEmbedding(BaseEmbedding): """Class for MistralAI embeddings. Args: model_name (str): Model for embedding. Defaults to "mistral-embed". api_key (Optional[str]): API key to access the model. Defaults to None. """ # Instance variables initialized via Pydantic's mechanism _client: Mistral = PrivateAttr() def __init__( self, model_name: str = "mistral-embed", api_key: Optional[str] = None, embed_batch_size: int = DEFAULT_EMBED_BATCH_SIZE, callback_manager: Optional[CallbackManager] = None, **kwargs: Any, ): super().__init__( model_name=model_name, embed_batch_size=embed_batch_size, callback_manager=callback_manager, **kwargs, ) api_key = get_from_param_or_env("api_key", api_key, "MISTRAL_API_KEY", "") if not api_key: raise ValueError( "You must provide an API key to use mistralai. " "You can either pass it in as an argument or set it `MISTRAL_API_KEY`." ) self._client = Mistral(api_key=api_key) @classmethod def class_name(cls) -> str: return "MistralAIEmbedding" def _get_query_embedding(self, query: str) -> List[float]: """Get query embedding.""" return ( self._client.embeddings.create(model=self.model_name, inputs=[query]) .data[0] .embedding ) async def _aget_query_embedding(self, query: str) -> List[float]: """The asynchronous version of _get_query_embedding.""" return ( ( await self._client.embeddings.create_async( model=self.model_name, inputs=[query] ) ) .data[0] .embedding ) def _get_text_embedding(self, text: str) -> List[float]: """Get text embedding.""" return ( self._client.embeddings.create(model=self.model_name, inputs=[text]) .data[0] .embedding ) async def _aget_text_embedding(self, text: str) -> List[float]: """Asynchronously get text embedding.""" return ( await self._client.embeddings.create( model=self.model_name, inputs=[text], ) .data[0] .embedding ) def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]: """Get text embeddings.""" embedding_response = self._client.embeddings.create( model=self.model_name, inputs=texts ).data return [embed.embedding for embed in embedding_response] async def _aget_text_embeddings(self, texts: List[str]) -> List[List[float]]: """Asynchronously get text embeddings.""" embedding_response = await self._client.embeddings.create_async( model=self.model_name, inputs=texts ) return [embed.embedding for embed in embedding_response.data]
# Copyright (c) OpenMMLab. All rights reserved. from mmcv.utils import collect_env as collect_base_env from mmcv.utils import get_git_hash import mmdet def collect_env(): """Collect the information of the running environments.""" env_info = collect_base_env() env_info['MMDetection'] = mmdet.__version__ + '+' + get_git_hash()[:7] return env_info if __name__ == '__main__': for name, val in collect_env().items(): print(f'{name}: {val}')
from mmcv.utils import collect_env as collect_base_env from mmcv.utils import get_git_hash import mmdet def collect_env(): """Collect the information of the running environments.""" env_info = collect_base_env() env_info['MMDetection'] = mmdet.__version__ + '+' + get_git_hash()[:7] return env_info if __name__ == '__main__': for name, val in collect_env().items(): print(f'{name}: {val}')
from docarray import Document, DocumentArray import numpy as np def find_random(da, target_certainty): return da.find( DocumentArray([Document(embedding=np.random.randint(10, size=10))]), query_params={"certainty": target_certainty}, additional=['certainty'], )[0] def test_certainty_filter(start_storage): nrof_docs = 100 target_certainty = 0.98 da = DocumentArray(storage='weaviate', config={'n_dim': 10, 'distance': 'cosine'}) with da: da.extend( [ Document(embedding=np.random.randint(10, size=10)) for i in range(1, nrof_docs) ], ) results = [] while len(results) == 0: results = find_random(da, target_certainty) for res in results: assert res.scores["weaviate_certainty"].value >= target_certainty
from docarray import Document, DocumentArray import numpy as np def find_random(da, target_certainty): return da.find( DocumentArray([Document(embedding=np.random.randint(10, size=10))]), query_params={"certainty": target_certainty}, )[0] def test_certainty_filter(start_storage): nrof_docs = 100 target_certainty = 0.98 da = DocumentArray(storage="weaviate", config={"n_dim": 10}) with da: da.extend( [ Document(embedding=np.random.randint(10, size=10)) for i in range(1, nrof_docs) ], ) results = [] while len(results) == 0: results = find_random(da, target_certainty) for res in results: assert res.scores["weaviate_certainty"].value >= target_certainty
""" Computes embeddings """ import numpy as np import pytest from typing import Optional from sentence_transformers import SentenceTransformer @pytest.mark.parametrize("normalize_embeddings", (False, True)) @pytest.mark.parametrize("prompt_name", (None, "retrieval")) def test_encode_multi_process( stsb_bert_tiny_model: SentenceTransformer, normalize_embeddings: bool, prompt_name: Optional[str] ) -> None: model = stsb_bert_tiny_model model.prompts = {"retrieval": "Represent this sentence for searching relevant passages: "} sentences = ["This is sentence {}".format(i) for i in range(40)] # Start the multi-process pool on e.g. two CPU devices & compute the embeddings using the pool pool = model.start_multi_process_pool(["cpu", "cpu"]) emb = model.encode_multi_process( sentences, pool, chunk_size=10, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name ) model.stop_multi_process_pool(pool) assert emb.shape == (len(sentences), 128) # Make sure the embeddings aren't just all 0 assert emb.sum() != 0.0 # Compare against normal embeddings emb_normal = model.encode(sentences, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name) diff = np.max(np.abs(emb - emb_normal)) assert diff < 0.001 # Ensure that after normalizing, the means are all almost 0, and otherwise not assert np.all(np.abs(emb.mean(1)) < 0.01) == normalize_embeddings
""" Computes embeddings """ import numpy as np import pytest from typing import Optional from sentence_transformers import SentenceTransformer @pytest.mark.parametrize("normalize_embeddings", (False, True)) @pytest.mark.parametrize("prompt_name", (None, "retrieval")) def test_encode_multi_process( stsb_bert_tiny_model: SentenceTransformer, normalize_embeddings: bool, prompt_name: Optional[str] ) -> None: model = stsb_bert_tiny_model model.prompts = {"retrieval": "Represent this sentence for searching relevant passages: "} sentences = ["This is sentence {}".format(i) for i in range(40)] # Start the multi-process pool on e.g. two CPU devices & compute the embeddings using the pool pool = model.start_multi_process_pool(["cpu", "cpu"]) emb = model.encode_multi_process( sentences, pool, chunk_size=10, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name ) model.stop_multi_process_pool(pool) assert emb.shape == (len(sentences), 128) # Make sure the embeddings aren't just all 0 assert emb.sum() != 0.0 # Compare against normal embeddings emb_normal = model.encode(sentences, normalize_embeddings=normalize_embeddings, prompt_name=prompt_name) diff = np.max(np.abs(emb - emb_normal)) assert diff < 0.001 # Ensure that after normalizing, the means are all almost 0, and otherwise not assert np.all(np.abs(emb.mean(1)) < 0.01) == normalize_embeddings
from docarray.typing.id import ID from docarray.typing.tensor.audio import AudioNdArray from docarray.typing.tensor.embedding.embedding import AnyEmbedding from docarray.typing.tensor.ndarray import NdArray from docarray.typing.tensor.tensor import AnyTensor from docarray.typing.url import ( AnyUrl, AudioUrl, ImageUrl, Mesh3DUrl, PointCloud3DUrl, TextUrl, ) __all__ = [ 'AudioNdArray', 'NdArray', 'AnyEmbedding', 'ImageUrl', 'AudioUrl', 'TextUrl', 'Mesh3DUrl', 'PointCloud3DUrl', 'AnyUrl', 'ID', 'AnyTensor', ] try: import torch # noqa: F401 except ImportError: pass else: from docarray.typing.tensor import TorchEmbedding, TorchTensor # noqa: F401 from docarray.typing.tensor.audio.audio_torch_tensor import AudioTorchTensor # noqa __all__.extend(['AudioTorchTensor', 'TorchEmbedding', 'TorchTensor'])
from docarray.typing.id import ID from docarray.typing.tensor.audio import AudioNdArray from docarray.typing.tensor.embedding.embedding import Embedding from docarray.typing.tensor.ndarray import NdArray from docarray.typing.tensor.tensor import AnyTensor from docarray.typing.url import ( AnyUrl, AudioUrl, ImageUrl, Mesh3DUrl, PointCloud3DUrl, TextUrl, ) __all__ = [ 'AudioNdArray', 'NdArray', 'Embedding', 'ImageUrl', 'AudioUrl', 'TextUrl', 'Mesh3DUrl', 'PointCloud3DUrl', 'AnyUrl', 'ID', 'AnyTensor', ] try: import torch # noqa: F401 except ImportError: pass else: from docarray.typing.tensor import TorchEmbedding, TorchTensor # noqa: F401 from docarray.typing.tensor.audio.audio_torch_tensor import AudioTorchTensor # noqa __all__.extend(['AudioTorchTensor', 'TorchEmbedding', 'TorchTensor'])
import json import os from typing import Optional, Type from llama_index.core.download.integration import download_integration from llama_index.core.download.pack import ( LLAMA_PACKS_CONTENTS_URL, download_llama_pack_template, track_download, ) from llama_index.core.llama_pack.base import BaseLlamaPack def download_llama_pack( llama_pack_class: str, download_dir: Optional[str] = None, llama_pack_url: str = LLAMA_PACKS_CONTENTS_URL, refresh_cache: bool = True, ) -> Optional[Type[BaseLlamaPack]]: """ Download a single LlamaPack PyPi Package. Args: llama_pack_class: The name of the LlamaPack class you want to download, such as `GmailOpenAIAgentPack`. refresh_cache: If true, the local cache will be skipped and the loader will be fetched directly from the remote repo. download_dir: Custom dirpath to download the pack into. Returns: A Loader. """ pack_cls = None mappings_path = os.path.join( os.path.abspath( os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir) ), "command_line/mappings.json", ) with open(mappings_path) as f: mappings = json.load(f) if llama_pack_class in mappings: new_import_parent = mappings[llama_pack_class] new_install_parent = new_import_parent.replace(".", "-").replace("_", "-") else: raise ValueError(f"Failed to find python package for class {llama_pack_class}") if not download_dir: pack_cls = download_integration( module_str=new_install_parent, module_import_str=new_import_parent, cls_name=llama_pack_class, ) else: pack_cls = download_llama_pack_template( new_install_parent=new_install_parent, llama_pack_class=llama_pack_class, llama_pack_url=llama_pack_url, refresh_cache=refresh_cache, custom_path=download_dir, ) track_download(llama_pack_class, "llamapack") if pack_cls is None: return None if not issubclass(pack_cls, BaseLlamaPack): raise ValueError( f"Pack class {pack_cls} must be a subclass of BaseLlamaPack." ) return pack_cls
import json import os from typing import Optional, Type from llama_index.core.download.integration import download_integration from llama_index.core.download.pack import ( LLAMA_PACKS_CONTENTS_URL, download_llama_pack_template, track_download, ) from llama_index.core.llama_pack.base import BaseLlamaPack def download_llama_pack( llama_pack_class: str, download_dir: Optional[str] = None, llama_pack_url: str = LLAMA_PACKS_CONTENTS_URL, refresh_cache: bool = True, ) -> Optional[Type[BaseLlamaPack]]: """Download a single LlamaPack PyPi Package. Args: llama_pack_class: The name of the LlamaPack class you want to download, such as `GmailOpenAIAgentPack`. refresh_cache: If true, the local cache will be skipped and the loader will be fetched directly from the remote repo. download_dir: Custom dirpath to download the pack into. Returns: A Loader. """ pack_cls = None mappings_path = os.path.join( os.path.abspath( os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir) ), "command_line/mappings.json", ) with open(mappings_path) as f: mappings = json.load(f) if llama_pack_class in mappings: new_import_parent = mappings[llama_pack_class] new_install_parent = new_import_parent.replace(".", "-").replace("_", "-") else: raise ValueError(f"Failed to find python package for class {llama_pack_class}") if not download_dir: pack_cls = download_integration( module_str=new_install_parent, module_import_str=new_import_parent, cls_name=llama_pack_class, ) else: pack_cls = download_llama_pack_template( new_install_parent=new_install_parent, llama_pack_class=llama_pack_class, llama_pack_url=llama_pack_url, refresh_cache=refresh_cache, custom_path=download_dir, ) track_download(llama_pack_class, "llamapack") if pack_cls is None: return None if not issubclass(pack_cls, BaseLlamaPack): raise ValueError( f"Pack class {pack_cls} must be a subclass of BaseLlamaPack." ) return pack_cls
# Licensed to the LF AI & Data foundation under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import List, Optional, TypeVar from docarray.typing.proto_register import _register_proto from docarray.typing.url.any_url import AnyUrl from docarray.typing.url.mimetypes import TEXT_EXTRA_EXTENSIONS, TEXT_MIMETYPE T = TypeVar('T', bound='TextUrl') @_register_proto(proto_type_name='text_url') class TextUrl(AnyUrl): """ URL to a text file. Can be remote (web) URL, or a local file path. """ @classmethod def mime_type(cls) -> str: return TEXT_MIMETYPE @classmethod def extra_extensions(cls) -> List[str]: """ Returns a list of additional file extensions that are valid for this class but cannot be identified by the mimetypes library. """ return TEXT_EXTRA_EXTENSIONS def load(self, charset: str = 'utf-8', timeout: Optional[float] = None) -> str: """ Load the text file into a string. --- ```python from docarray import BaseDoc from docarray.typing import TextUrl class MyDoc(BaseDoc): remote_url: TextUrl doc = MyDoc( remote_url='https://de.wikipedia.org/wiki/Brixen', ) remote_txt = doc.remote_url.load() ``` --- :param timeout: timeout (sec) for urlopen network request. Only relevant if URL is not local :param charset: decoding charset; may be any character set registered with IANA :return: the text file content """ _bytes = self.load_bytes(timeout=timeout) return _bytes.decode(charset)
from typing import List, Optional, TypeVar from docarray.typing.proto_register import _register_proto from docarray.typing.url.any_url import AnyUrl from docarray.typing.url.mimetypes import TEXT_EXTRA_EXTENSIONS, TEXT_MIMETYPE T = TypeVar('T', bound='TextUrl') @_register_proto(proto_type_name='text_url') class TextUrl(AnyUrl): """ URL to a text file. Can be remote (web) URL, or a local file path. """ @classmethod def mime_type(cls) -> str: return TEXT_MIMETYPE @classmethod def extra_extensions(cls) -> List[str]: """ Returns a list of additional file extensions that are valid for this class but cannot be identified by the mimetypes library. """ return TEXT_EXTRA_EXTENSIONS def load(self, charset: str = 'utf-8', timeout: Optional[float] = None) -> str: """ Load the text file into a string. --- ```python from docarray import BaseDoc from docarray.typing import TextUrl class MyDoc(BaseDoc): remote_url: TextUrl doc = MyDoc( remote_url='https://de.wikipedia.org/wiki/Brixen', ) remote_txt = doc.remote_url.load() ``` --- :param timeout: timeout (sec) for urlopen network request. Only relevant if URL is not local :param charset: decoding charset; may be any character set registered with IANA :return: the text file content """ _bytes = self.load_bytes(timeout=timeout) return _bytes.decode(charset)
import numpy as np from docarray import BaseDoc from docarray.array.stacked.array_stacked import DocArrayStacked from docarray.typing import AnyTensor, NdArray def test_da_init(): class MyDoc(BaseDoc): tensor: AnyTensor name: str docs = [MyDoc(tensor=np.zeros(10), name='hello') for _ in range(4)] da = DocArrayStacked[MyDoc](docs, tensor_type=NdArray) assert (da._storage.tensor_columns['tensor'] == np.zeros((4, 10))).all() assert da._storage.any_columns['name']._data == ['hello' for _ in range(4)] def test_da_iter(): class MyDoc(BaseDoc): tensor: AnyTensor name: str docs = [MyDoc(tensor=i * np.zeros((10, 10)), name=f'hello{i}') for i in range(4)] da = DocArrayStacked[MyDoc](docs, tensor_type=NdArray) for i, doc in enumerate(da): assert isinstance(doc, MyDoc) assert (doc.tensor == i * np.zeros((10, 10))).all() assert doc.name == f'hello{i}'
import numpy as np from docarray import BaseDocument from docarray.array.stacked.array_stacked import DocumentArrayStacked from docarray.typing import AnyTensor, NdArray def test_da_init(): class MyDoc(BaseDocument): tensor: AnyTensor name: str docs = [MyDoc(tensor=np.zeros(10), name='hello') for _ in range(4)] da = DocumentArrayStacked[MyDoc](docs, tensor_type=NdArray) assert (da._storage.tensor_columns['tensor'] == np.zeros((4, 10))).all() assert da._storage.any_columns['name']._data == ['hello' for _ in range(4)] def test_da_iter(): class MyDoc(BaseDocument): tensor: AnyTensor name: str docs = [MyDoc(tensor=i * np.zeros((10, 10)), name=f'hello{i}') for i in range(4)] da = DocumentArrayStacked[MyDoc](docs, tensor_type=NdArray) for i, doc in enumerate(da): assert isinstance(doc, MyDoc) assert (doc.tensor == i * np.zeros((10, 10))).all() assert doc.name == f'hello{i}'
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import numpy as np import pytest from jina import Document, DocumentArray from jina.executors.metas import get_default_metas from jina_commons.indexers.dump import import_vectors from .. import AnnoySearcher # fix the seed here np.random.seed(500) docs = DocumentArray([Document(embedding=np.random.random(10)) for i in range(10)]) search_doc = DocumentArray([Document(embedding=np.random.random(10))]) DUMP_PATH = 'tests/dump1' TOP_K = 5 @pytest.fixture(scope='function', autouse=True) def metas(tmpdir): os.environ['TEST_WORKSPACE'] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ['TEST_WORKSPACE'] yield metas del os.environ['TEST_WORKSPACE'] def test_simple_annoy(): from annoy import AnnoyIndex _index = AnnoyIndex(5, 'angular') for j in range(3): _index.add_item(j, np.random.random((5,))) _index.build(4) idx1, _ = _index.get_nns_by_vector( np.random.random((5,)), 3, include_distances=True ) assert len(idx1) == 3 @pytest.mark.parametrize(['metric', 'is_distance'], [('angular', True), ('euclidean', True), ('manhattan', True), ('hamming', True), ('dot', True), ('angular', False), ('euclidean', False), ('manhattan', False), ('hamming', False), ('dot', False)]) def test_metric(tmpdir, metric, is_distance): metas = {'workspace': str(tmpdir), 'name': 'searcher', 'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(dump_path=DUMP_PATH, default_top_k=TOP_K, metas=metas, metric=metric, is_distance=is_distance) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) assert len(docs[0].matches) == TOP_K for i in range(len(docs[0].matches) - 1): if not is_distance: assert docs[0].matches[i].scores[metric].value >= docs[0].matches[i + 1].scores[metric].value else: assert docs[0].matches[i].scores[metric].value <= docs[0].matches[i + 1].scores[metric].value def test_query_vector(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher', 'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(dump_path=DUMP_PATH, default_top_k=TOP_K, metas=metas) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) ids, vecs = import_vectors(DUMP_PATH, str(0)) ids = np.array(list(ids)) vecs = np.array(list(vecs)) assert len(docs) == 1 assert len(docs[0].matches) == TOP_K assert docs[0].matches[0].id in ids assert len(docs[0].matches[0].embedding) == 7 assert docs[0].matches[0].embedding in vecs da = DocumentArray([Document(id=0), Document(id=1), Document(id=2)]) indexer.fill_embedding(da) for i, doc in enumerate(da): assert list(doc.embedding) def test_query_vector_empty(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher', 'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(default_top_k=TOP_K, metas=metas) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) assert len(docs[0].matches) == 0
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import numpy as np import pytest from jina import Document, DocumentArray from jina.executors.metas import get_default_metas from jina_commons.indexers.dump import import_vectors from .. import AnnoySearcher # fix the seed here np.random.seed(500) docs = DocumentArray([Document(embedding=np.random.random(10)) for i in range(10)]) search_doc = DocumentArray([Document(embedding=np.random.random(10))]) DUMP_PATH = 'tests/dump1' TOP_K = 5 @pytest.fixture(scope='function', autouse=True) def metas(tmpdir): os.environ['TEST_WORKSPACE'] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ['TEST_WORKSPACE'] yield metas del os.environ['TEST_WORKSPACE'] def test_simple_annoy(): from annoy import AnnoyIndex _index = AnnoyIndex(5, 'angular') for j in range(3): _index.add_item(j, np.random.random((5,))) _index.build(4) idx1, _ = _index.get_nns_by_vector( np.random.random((5,)), 3, include_distances=True ) assert len(idx1) == 3 def test_query_vector(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher', 'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(dump_path=DUMP_PATH, top_k=TOP_K, metas=metas) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) ids, vecs = import_vectors(DUMP_PATH, str(0)) ids = np.array(list(ids)) vecs = np.array(list(vecs)) assert len(docs) == 1 assert len(docs[0].matches) == TOP_K assert docs[0].matches[0].id in ids assert len(docs[0].matches[0].embedding) == 7 assert docs[0].matches[0].embedding in vecs da = DocumentArray([Document(id=0), Document(id=1), Document(id=2)]) indexer.fill_embedding(da) for i, doc in enumerate(da): assert list(doc.embedding) def test_query_vector_empty(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher', 'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(top_k=TOP_K, metas=metas) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) assert len(docs[0].matches) == 0
from llama_index.llms.huggingface.base import ( HuggingFaceInferenceAPI, HuggingFaceLLM, TextGenerationInference, ) __all__ = ["HuggingFaceLLM", "HuggingFaceInferenceAPI", "TextGenerationInference"]
from llama_index.llms.huggingface.base import ( HuggingFaceLLM, ) __all__ = ["HuggingFaceLLM"]
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.ops.nn import average_pool from keras.src.ops.nn import batch_normalization from keras.src.ops.nn import binary_crossentropy from keras.src.ops.nn import categorical_crossentropy from keras.src.ops.nn import celu from keras.src.ops.nn import conv from keras.src.ops.nn import conv_transpose from keras.src.ops.nn import ctc_decode from keras.src.ops.nn import ctc_loss from keras.src.ops.nn import depthwise_conv from keras.src.ops.nn import dot_product_attention from keras.src.ops.nn import elu from keras.src.ops.nn import gelu from keras.src.ops.nn import hard_sigmoid from keras.src.ops.nn import hard_silu from keras.src.ops.nn import hard_silu as hard_swish from keras.src.ops.nn import leaky_relu from keras.src.ops.nn import log_sigmoid from keras.src.ops.nn import log_softmax from keras.src.ops.nn import max_pool from keras.src.ops.nn import moments from keras.src.ops.nn import multi_hot from keras.src.ops.nn import normalize from keras.src.ops.nn import one_hot from keras.src.ops.nn import psnr from keras.src.ops.nn import relu from keras.src.ops.nn import relu6 from keras.src.ops.nn import selu from keras.src.ops.nn import separable_conv from keras.src.ops.nn import sigmoid from keras.src.ops.nn import silu from keras.src.ops.nn import silu as swish from keras.src.ops.nn import softmax from keras.src.ops.nn import softplus from keras.src.ops.nn import softsign from keras.src.ops.nn import sparse_categorical_crossentropy
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.ops.nn import average_pool from keras.src.ops.nn import batch_normalization from keras.src.ops.nn import binary_crossentropy from keras.src.ops.nn import categorical_crossentropy from keras.src.ops.nn import conv from keras.src.ops.nn import conv_transpose from keras.src.ops.nn import ctc_decode from keras.src.ops.nn import ctc_loss from keras.src.ops.nn import depthwise_conv from keras.src.ops.nn import dot_product_attention from keras.src.ops.nn import elu from keras.src.ops.nn import gelu from keras.src.ops.nn import hard_sigmoid from keras.src.ops.nn import hard_silu from keras.src.ops.nn import hard_silu as hard_swish from keras.src.ops.nn import leaky_relu from keras.src.ops.nn import log_sigmoid from keras.src.ops.nn import log_softmax from keras.src.ops.nn import max_pool from keras.src.ops.nn import moments from keras.src.ops.nn import multi_hot from keras.src.ops.nn import normalize from keras.src.ops.nn import one_hot from keras.src.ops.nn import psnr from keras.src.ops.nn import relu from keras.src.ops.nn import relu6 from keras.src.ops.nn import selu from keras.src.ops.nn import separable_conv from keras.src.ops.nn import sigmoid from keras.src.ops.nn import silu from keras.src.ops.nn import silu as swish from keras.src.ops.nn import softmax from keras.src.ops.nn import softplus from keras.src.ops.nn import softsign from keras.src.ops.nn import sparse_categorical_crossentropy
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, TypeVar, Union import numpy as np from pydantic import Field from docarray.base_doc import BaseDoc from docarray.typing import AnyEmbedding, ImageBytes, ImageUrl from docarray.typing.tensor.abstract_tensor import AbstractTensor from docarray.typing.tensor.image.image_tensor import ImageTensor from docarray.utils._internal.misc import import_library from docarray.utils._internal.pydantic import is_pydantic_v2 if is_pydantic_v2: from pydantic import model_validator if TYPE_CHECKING: import tensorflow as tf # type: ignore import torch else: tf = import_library('tensorflow', raise_error=False) torch = import_library('torch', raise_error=False) T = TypeVar('T', bound='ImageDoc') class ImageDoc(BaseDoc): """ Document for handling images. It can contain: - an [`ImageUrl`][docarray.typing.url.ImageUrl] (`Image.url`) - an [`ImageTensor`](../../../api_references/typing/tensor/image) (`Image.tensor`) - an [`AnyEmbedding`](../../../api_references/typing/tensor/embedding) (`Image.embedding`) - an [`ImageBytes`][docarray.typing.bytes.ImageBytes] object (`ImageDoc.bytes_`) You can use this Document directly: ```python from docarray.documents import ImageDoc # use it directly image = ImageDoc( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ) image.tensor = image.url.load() # model = MyEmbeddingModel() # image.embedding = model(image.tensor) ``` You can extend this Document: ```python from docarray.documents import ImageDoc from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyImage(ImageDoc): second_embedding: Optional[AnyEmbedding] = None image = MyImage( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ) image.tensor = image.url.load() # model = MyEmbeddingModel() # image.embedding = model(image.tensor) # image.second_embedding = model(image.tensor) ``` You can use this Document for composition: ```python from docarray import BaseDoc from docarray.documents import ImageDoc, TextDoc # compose it class MultiModalDoc(BaseDoc): image: ImageDoc text: TextDoc mmdoc = MultiModalDoc( image=ImageDoc( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ), text=TextDoc(text='hello world, how are you doing?'), ) mmdoc.image.tensor = mmdoc.image.url.load() # or mmdoc.image.bytes_ = mmdoc.image.url.load_bytes() mmdoc.image.tensor = mmdoc.image.bytes_.load() ``` """ url: Optional[ImageUrl] = Field( description='URL to a (potentially remote) image file that needs to be loaded', example='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true', default=None, ) tensor: Optional[ImageTensor] = Field( description='Tensor object of the image which can be specifed to one of `ImageNdArray`, `ImageTorchTensor`, `ImageTensorflowTensor`.', default=None, ) embedding: Optional[AnyEmbedding] = Field( description='Store an embedding: a vector representation of the image.', example=[1, 0, 1], default=None, ) bytes_: Optional[ImageBytes] = Field( description='Bytes object of the image which is an instance of `ImageBytes`.', default=None, ) @classmethod def _validate(cls, value) -> Dict[str, Any]: if isinstance(value, str): value = dict(url=value) elif ( isinstance(value, (AbstractTensor, np.ndarray)) or (torch is not None and isinstance(value, torch.Tensor)) or (tf is not None and isinstance(value, tf.Tensor)) ): value = dict(tensor=value) elif isinstance(value, bytes): value = dict(byte=value) return value if is_pydantic_v2: @model_validator(mode='before') @classmethod def validate_model_before(cls, value): return cls._validate(value) else: @classmethod def validate( cls: Type[T], value: Union[str, AbstractTensor, Any], ) -> T: return super().validate(cls._validate(value))
from typing import TYPE_CHECKING, Any, Dict, Optional, Type, TypeVar, Union import numpy as np from pydantic import Field from docarray.base_doc import BaseDoc from docarray.typing import AnyEmbedding, ImageBytes, ImageUrl from docarray.typing.tensor.abstract_tensor import AbstractTensor from docarray.typing.tensor.image.image_tensor import ImageTensor from docarray.utils._internal.misc import import_library from docarray.utils._internal.pydantic import is_pydantic_v2 if is_pydantic_v2: from pydantic import model_validator if TYPE_CHECKING: import tensorflow as tf # type: ignore import torch else: tf = import_library('tensorflow', raise_error=False) torch = import_library('torch', raise_error=False) T = TypeVar('T', bound='ImageDoc') class ImageDoc(BaseDoc): """ Document for handling images. It can contain: - an [`ImageUrl`][docarray.typing.url.ImageUrl] (`Image.url`) - an [`ImageTensor`](../../../api_references/typing/tensor/image) (`Image.tensor`) - an [`AnyEmbedding`](../../../api_references/typing/tensor/embedding) (`Image.embedding`) - an [`ImageBytes`][docarray.typing.bytes.ImageBytes] object (`ImageDoc.bytes_`) You can use this Document directly: ```python from docarray.documents import ImageDoc # use it directly image = ImageDoc( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ) image.tensor = image.url.load() # model = MyEmbeddingModel() # image.embedding = model(image.tensor) ``` You can extend this Document: ```python from docarray.documents import ImageDoc from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyImage(ImageDoc): second_embedding: Optional[AnyEmbedding] image = MyImage( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ) image.tensor = image.url.load() # model = MyEmbeddingModel() # image.embedding = model(image.tensor) # image.second_embedding = model(image.tensor) ``` You can use this Document for composition: ```python from docarray import BaseDoc from docarray.documents import ImageDoc, TextDoc # compose it class MultiModalDoc(BaseDoc): image: ImageDoc text: TextDoc mmdoc = MultiModalDoc( image=ImageDoc( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ), text=TextDoc(text='hello world, how are you doing?'), ) mmdoc.image.tensor = mmdoc.image.url.load() # or mmdoc.image.bytes_ = mmdoc.image.url.load_bytes() mmdoc.image.tensor = mmdoc.image.bytes_.load() ``` """ url: Optional[ImageUrl] = Field( description='URL to a (potentially remote) image file that needs to be loaded', example='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true', default=None, ) tensor: Optional[ImageTensor] = Field( description='Tensor object of the image which can be specifed to one of `ImageNdArray`, `ImageTorchTensor`, `ImageTensorflowTensor`.', default=None, ) embedding: Optional[AnyEmbedding] = Field( description='Store an embedding: a vector representation of the image.', example=[1, 0, 1], default=None, ) bytes_: Optional[ImageBytes] = Field( description='Bytes object of the image which is an instance of `ImageBytes`.', default=None, ) @classmethod def _validate(cls, value) -> Dict[str, Any]: if isinstance(value, str): value = dict(url=value) elif ( isinstance(value, (AbstractTensor, np.ndarray)) or (torch is not None and isinstance(value, torch.Tensor)) or (tf is not None and isinstance(value, tf.Tensor)) ): value = dict(tensor=value) elif isinstance(value, bytes): value = dict(byte=value) return value if is_pydantic_v2: @model_validator(mode='before') @classmethod def validate_model_before(cls, value): return cls._validate(value) else: @classmethod def validate( cls: Type[T], value: Union[str, AbstractTensor, Any], ) -> T: return super().validate(cls._validate(value))
from docarray.array.document import DocumentArray
from .document import DocumentArray
from docarray.typing.id import ID from docarray.typing.tensor.embedding.embedding import Embedding from docarray.typing.tensor.ndarray import NdArray from docarray.typing.tensor.tensor import AnyTensor from docarray.typing.url import AnyUrl, ImageUrl, Mesh3DUrl, PointCloud3DUrl, TextUrl __all__ = [ 'NdArray', 'Embedding', 'ImageUrl', 'TextUrl', 'Mesh3DUrl', 'PointCloud3DUrl', 'AnyUrl', 'ID', 'AnyTensor', ] try: import torch # noqa: F401 except ImportError: pass else: from docarray.typing.tensor import TorchEmbedding, TorchTensor # noqa: F401 __all__.extend(['TorchEmbedding', 'TorchTensor'])
from docarray.typing.id import ID from docarray.typing.tensor import AnyTensor, NdArray from docarray.typing.tensor.embedding import Embedding from docarray.typing.url import AnyUrl, ImageUrl, Mesh3DUrl, PointCloud3DUrl, TextUrl __all__ = [ 'NdArray', 'Embedding', 'ImageUrl', 'TextUrl', 'Mesh3DUrl', 'PointCloud3DUrl', 'AnyUrl', 'ID', 'AnyTensor', ] try: import torch # noqa: F401 except ImportError: pass else: from docarray.typing.tensor import TorchEmbedding, TorchTensor # noqa: F401 __all__.extend(['TorchEmbedding', 'TorchTensor'])
# Copyright (c) OpenMMLab. All rights reserved. from mmdet.registry import MODELS from .single_stage import SingleStageDetector @MODELS.register_module() class FCOS(SingleStageDetector): """Implementation of `FCOS <https://arxiv.org/abs/1904.01355>`_""" def __init__(self, backbone, neck, bbox_head, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(FCOS, self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg)
# Copyright (c) OpenMMLab. All rights reserved. from ..builder import DETECTORS from .single_stage import SingleStageDetector @DETECTORS.register_module() class FCOS(SingleStageDetector): """Implementation of `FCOS <https://arxiv.org/abs/1904.01355>`_""" def __init__(self, backbone, neck, bbox_head, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(FCOS, self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg)
from .image_tf_encoder import ImageTFEncoder
from .image_tf_encoder import ImageTFEncoder
import importlib.util import warnings from functools import wraps from typing import Optional def is_module_available(*modules: str) -> bool: r"""Returns if a top-level module with :attr:`name` exists *without** importing it. This is generally safer than try-catch block around a `import X`. It avoids third party libraries breaking assumptions of some of our tests, e.g., setting multiprocessing start method when imported (see librosa/#747, torchvision/#544). """ return all(importlib.util.find_spec(m) is not None for m in modules) def requires_module(*modules: str): """Decorate function to give error message if invoked without required optional modules. This decorator is to give better error message to users rather than raising ``NameError: name 'module' is not defined`` at random places. """ missing = [m for m in modules if not is_module_available(m)] if not missing: # fall through. If all the modules are available, no need to decorate def decorator(func): return func else: req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}") return wrapped return decorator def deprecated(direction: str, version: Optional[str] = None, remove: bool = False): """Decorator to add deprecation message Args: direction (str): Migration steps to be given to users. version (str or int): The version when the object will be removed remove (bool): If enabled, append future removal message. """ def decorator(func): @wraps(func) def wrapped(*args, **kwargs): message = f"{func.__module__}.{func.__name__} has been deprecated. {direction}" if remove: message += f' It will be removed from {"future" if version is None else version} release. ' warnings.warn(message, stacklevel=2) return func(*args, **kwargs) return wrapped return decorator def fail_with_message(message): """Generate decorator to give users message about missing TorchAudio extension.""" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} {message}") return wrapped return decorator def no_op(func): """Op-op decorator. Used in place of fail_with_message when a functionality that requires extension works fine.""" return func
import importlib.util import warnings from functools import wraps from typing import Optional def is_module_available(*modules: str) -> bool: r"""Returns if a top-level module with :attr:`name` exists *without** importing it. This is generally safer than try-catch block around a `import X`. It avoids third party libraries breaking assumptions of some of our tests, e.g., setting multiprocessing start method when imported (see librosa/#747, torchvision/#544). """ return all(importlib.util.find_spec(m) is not None for m in modules) def requires_module(*modules: str): """Decorate function to give error message if invoked without required optional modules. This decorator is to give better error message to users rather than raising ``NameError: name 'module' is not defined`` at random places. """ missing = [m for m in modules if not is_module_available(m)] if not missing: # fall through. If all the modules are available, no need to decorate def decorator(func): return func else: req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}") return wrapped return decorator def deprecated(direction: str, version: Optional[str] = None, remove: bool = False): """Decorator to add deprecation message Args: direction (str): Migration steps to be given to users. version (str or int): The version when the object will be removed """ def decorator(func): @wraps(func) def wrapped(*args, **kwargs): message = f"{func.__module__}.{func.__name__} has been deprecated. {direction}" if remove: message += f' It will be removed from {"future" if version is None else version} release. ' warnings.warn(message, stacklevel=2) return func(*args, **kwargs) return wrapped return decorator def fail_with_message(message): """Generate decorator to give users message about missing TorchAudio extension.""" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} {message}") return wrapped return decorator def no_op(func): """Op-op decorator. Used in place of fail_with_message when a functionality that requires extension works fine.""" return func
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Iterable, Optional import torch from jina import DocumentArray, Executor, requests from jina_commons.batching import get_docs_batch_generator from .audio_clip.model import AudioCLIP class AudioCLIPTextEncoder(Executor): """ Encode text data with the AudioCLIP model """ def __init__( self, model_path: str = '.cache/AudioCLIP-Full-Training.pt', tokenizer_path: str = '.cache/bpe_simple_vocab_16e6.txt.gz', default_traversal_paths: Iterable[str] = ('r',), default_batch_size: int = 32, device: str = 'cpu', *args, **kwargs ): """ :param model_path: path to the pre-trained AudioCLIP model. :param default_traversal_paths: default traversal path (used if not specified in request's parameters) :param default_batch_size: default batch size (used if not specified in request's parameters) :param device: device that the model is on (should be "cpu", "cuda" or "cuda:X", where X is the index of the GPU on the machine) """ super().__init__(*args, **kwargs) self.model = ( AudioCLIP( pretrained=model_path, bpe_path=tokenizer_path, ) .to(device) .eval() ) self.default_traversal_paths = default_traversal_paths self.default_batch_size = default_batch_size @requests def encode( self, docs: Optional[DocumentArray], parameters: dict, *args, **kwargs ) -> None: """ Method to create embedddings for documents by encoding their text. :param docs: A document array with documents to create embeddings for. Only the documents that have the ``text`` attribute will get embeddings. :param parameters: A dictionary that contains parameters to control encoding. The accepted keys are ``traversal_paths`` and ``batch_size`` - in their absence their corresponding default values are used. """ batch_generator = get_docs_batch_generator( docs, traversal_path=parameters.get( 'traversal_paths', self.default_traversal_paths ), batch_size=parameters.get('batch_size', self.default_batch_size), needs_attr='text', ) with torch.no_grad(): for batch in batch_generator: embeddings = self.model.encode_text(text=[[doc.text] for doc in batch]) embeddings = embeddings.cpu().numpy() for idx, doc in enumerate(batch): doc.embedding = embeddings[idx]
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Iterable, Optional import torch from jina import DocumentArray, Executor, requests from jina_commons.batching import get_docs_batch_generator from .audio_clip.model import AudioCLIP class AudioCLIPTextEncoder(Executor): """ Encode text data with the AudioCLIP model :param model_path: path to the pre-trained AudioCLIP model. :param default_traversal_paths: default traversal path (used if not specified in request's parameters) :param default_batch_size: default batch size (used if not specified in request's parameters) :param device: device that the model is on (should be "cpu", "cuda" or "cuda:X", where X is the index of the GPU on the machine) """ def __init__( self, model_path: str = '.cache/AudioCLIP-Full-Training.pt', tokenizer_path: str = '.cache/bpe_simple_vocab_16e6.txt.gz', default_traversal_paths: Iterable[str] = ('r',), default_batch_size: int = 32, device: str = 'cpu', *args, **kwargs ): super().__init__(*args, **kwargs) self.model = ( AudioCLIP( pretrained=model_path, bpe_path=tokenizer_path, ) .to(device) .eval() ) self.default_traversal_paths = default_traversal_paths self.default_batch_size = default_batch_size @requests def encode( self, docs: Optional[DocumentArray], parameters: dict, *args, **kwargs ) -> None: """ Method to create embedddings for documents by encoding their text. :param docs: A document array with documents to create embeddings for. Only the documents that have the ``text`` attribute will get embeddings. :param parameters: A dictionary that contains parameters to control encoding. The accepted keys are ``traversal_paths`` and ``batch_size`` - in their absence their corresponding default values are used. """ batch_generator = get_docs_batch_generator( docs, traversal_path=parameters.get( 'traversal_paths', self.default_traversal_paths ), batch_size=parameters.get('batch_size', self.default_batch_size), needs_attr='text', ) with torch.no_grad(): for batch in batch_generator: embeddings = self.model.encode_text(text=[[doc.text] for doc in batch]) embeddings = embeddings.cpu().numpy() for idx, doc in enumerate(batch): doc.embedding = embeddings[idx]
from unittest.mock import MagicMock, patch from langchain_huggingface import HuggingFacePipeline DEFAULT_MODEL_ID = "gpt2" def test_initialization_default() -> None: """Test default initialization.""" llm = HuggingFacePipeline() assert llm.model_id == DEFAULT_MODEL_ID @patch("transformers.pipeline") def test_initialization_with_pipeline(mock_pipeline: MagicMock) -> None: """Test initialization with a pipeline object.""" mock_pipe = MagicMock() mock_pipe.model.name_or_path = "mock-model-id" mock_pipeline.return_value = mock_pipe llm = HuggingFacePipeline(pipeline=mock_pipe) assert llm.model_id == "mock-model-id" @patch("transformers.AutoTokenizer.from_pretrained") @patch("transformers.AutoModelForCausalLM.from_pretrained") @patch("transformers.pipeline") def test_initialization_with_from_model_id( mock_pipeline: MagicMock, mock_model: MagicMock, mock_tokenizer: MagicMock ) -> None: """Test initialization with the from_model_id method.""" mock_tokenizer.return_value = MagicMock(pad_token_id=0) mock_model.return_value = MagicMock() mock_pipe = MagicMock() mock_pipe.task = "text-generation" mock_pipe.model = mock_model.return_value mock_pipeline.return_value = mock_pipe llm = HuggingFacePipeline.from_model_id( model_id="mock-model-id", task="text-generation", ) assert llm.model_id == "mock-model-id"
from unittest.mock import MagicMock, patch from langchain_huggingface import HuggingFacePipeline DEFAULT_MODEL_ID = "gpt2" def test_initialization_default() -> None: """Test default initialization""" llm = HuggingFacePipeline() assert llm.model_id == DEFAULT_MODEL_ID @patch("transformers.pipeline") def test_initialization_with_pipeline(mock_pipeline: MagicMock) -> None: """Test initialization with a pipeline object""" mock_pipe = MagicMock() mock_pipe.model.name_or_path = "mock-model-id" mock_pipeline.return_value = mock_pipe llm = HuggingFacePipeline(pipeline=mock_pipe) assert llm.model_id == "mock-model-id" @patch("transformers.AutoTokenizer.from_pretrained") @patch("transformers.AutoModelForCausalLM.from_pretrained") @patch("transformers.pipeline") def test_initialization_with_from_model_id( mock_pipeline: MagicMock, mock_model: MagicMock, mock_tokenizer: MagicMock ) -> None: """Test initialization with the from_model_id method""" mock_tokenizer.return_value = MagicMock(pad_token_id=0) mock_model.return_value = MagicMock() mock_pipe = MagicMock() mock_pipe.task = "text-generation" mock_pipe.model = mock_model.return_value mock_pipeline.return_value = mock_pipe llm = HuggingFacePipeline.from_model_id( model_id="mock-model-id", task="text-generation", ) assert llm.model_id == "mock-model-id"
_base_ = './retinanet_r50-caffe_fpn_1x_coco.py' train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomChoiceResize', scales=[(1333, 640), (1333, 672), (1333, 704), (1333, 736), (1333, 768), (1333, 800)], keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline))
_base_ = './retinanet_r50-caffe_fpn_1x_coco.py' train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomChoiceResize', scale=[(1333, 640), (1333, 672), (1333, 704), (1333, 736), (1333, 768), (1333, 800)], keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline))
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.ops.nn import average_pool from keras.src.ops.nn import batch_normalization from keras.src.ops.nn import binary_crossentropy from keras.src.ops.nn import categorical_crossentropy from keras.src.ops.nn import celu from keras.src.ops.nn import conv from keras.src.ops.nn import conv_transpose from keras.src.ops.nn import ctc_decode from keras.src.ops.nn import ctc_loss from keras.src.ops.nn import depthwise_conv from keras.src.ops.nn import dot_product_attention from keras.src.ops.nn import elu from keras.src.ops.nn import gelu from keras.src.ops.nn import glu from keras.src.ops.nn import hard_shrink from keras.src.ops.nn import hard_sigmoid from keras.src.ops.nn import hard_silu from keras.src.ops.nn import hard_silu as hard_swish from keras.src.ops.nn import hard_tanh from keras.src.ops.nn import leaky_relu from keras.src.ops.nn import log_sigmoid from keras.src.ops.nn import log_softmax from keras.src.ops.nn import max_pool from keras.src.ops.nn import moments from keras.src.ops.nn import multi_hot from keras.src.ops.nn import normalize from keras.src.ops.nn import one_hot from keras.src.ops.nn import psnr from keras.src.ops.nn import relu from keras.src.ops.nn import relu6 from keras.src.ops.nn import selu from keras.src.ops.nn import separable_conv from keras.src.ops.nn import sigmoid from keras.src.ops.nn import silu from keras.src.ops.nn import silu as swish from keras.src.ops.nn import soft_shrink from keras.src.ops.nn import softmax from keras.src.ops.nn import softplus from keras.src.ops.nn import softsign from keras.src.ops.nn import sparse_categorical_crossentropy from keras.src.ops.nn import sparse_plus from keras.src.ops.nn import sparsemax from keras.src.ops.nn import squareplus from keras.src.ops.nn import tanh_shrink from keras.src.ops.nn import threshold
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.ops.nn import average_pool from keras.src.ops.nn import batch_normalization from keras.src.ops.nn import binary_crossentropy from keras.src.ops.nn import categorical_crossentropy from keras.src.ops.nn import celu from keras.src.ops.nn import conv from keras.src.ops.nn import conv_transpose from keras.src.ops.nn import ctc_decode from keras.src.ops.nn import ctc_loss from keras.src.ops.nn import depthwise_conv from keras.src.ops.nn import dot_product_attention from keras.src.ops.nn import elu from keras.src.ops.nn import gelu from keras.src.ops.nn import glu from keras.src.ops.nn import hard_shrink from keras.src.ops.nn import hard_sigmoid from keras.src.ops.nn import hard_silu from keras.src.ops.nn import hard_silu as hard_swish from keras.src.ops.nn import hard_tanh from keras.src.ops.nn import leaky_relu from keras.src.ops.nn import log_sigmoid from keras.src.ops.nn import log_softmax from keras.src.ops.nn import max_pool from keras.src.ops.nn import moments from keras.src.ops.nn import multi_hot from keras.src.ops.nn import normalize from keras.src.ops.nn import one_hot from keras.src.ops.nn import psnr from keras.src.ops.nn import relu from keras.src.ops.nn import relu6 from keras.src.ops.nn import selu from keras.src.ops.nn import separable_conv from keras.src.ops.nn import sigmoid from keras.src.ops.nn import silu from keras.src.ops.nn import silu as swish from keras.src.ops.nn import soft_shrink from keras.src.ops.nn import softmax from keras.src.ops.nn import softplus from keras.src.ops.nn import softsign from keras.src.ops.nn import sparse_categorical_crossentropy from keras.src.ops.nn import sparse_plus from keras.src.ops.nn import sparsemax from keras.src.ops.nn import squareplus from keras.src.ops.nn import tanh_shrink
import requests from packaging import version from typing import Union, List, Optional from llama_index.core.base.llms.types import ( ChatResponse, ) def get_max_input_tokens(url: str) -> Union[int, None]: url = f"{url}/info" model_info = dict(requests.get(url).json()) tgi_version = model_info.get("version", None) if version.parse(tgi_version) >= version.parse("2.1.0"): return model_info.get("max_input_tokens", None) else: return model_info.get("max_input_length", None) def get_max_total_tokens(url: str) -> Union[int, None]: url = f"{url}/info" model_info = dict(requests.get(url).json()) return model_info.get("max_total_tokens", None) def force_single_tool_call(response: ChatResponse) -> None: tool_calls = response.message.additional_kwargs.get("tool_calls", []) if len(tool_calls) > 1: response.message.additional_kwargs["tool_calls"] = [tool_calls[0]] def resolve_tool_choice( tools: Optional[List[dict]] = None, tool_choice: str = "none" ) -> Union[str, dict]: """Resolve tool choice. Check if tool_name exists in tools. Note that unlike in OpenAI specification, 'auto' will ALWAYS choose the tool for you. Set to 'none' explicitly if do not wish to use tool. """ valid_tool_choices = ["none", "auto"] + [t["function"]["name"] for t in tools or []] if tool_choice not in valid_tool_choices: raise ValueError( f"{tool_choice} is not a valid tool_choice. Must be one of {valid_tool_choices}" ) return tool_choice
import requests from packaging import version from typing import Sequence, Union, List, Optional from llama_index.core.base.llms.types import ( ChatMessage, ChatResponse, ) from text_generation.types import ( Message, ) def resolve_tgi_function_call(url: str) -> bool: url = f"{url}/info" model_info = dict(requests.get(url).json()) tgi_version = model_info.get("version", None) if version.parse(tgi_version) >= version.parse("2.0.1"): return True else: raise ValueError( "'text-generation-inference' version ", f"incompatible with function call: {tgi_version}. ", "Function call support was added in v2.0.1", ) def get_max_input_tokens(url: str) -> Union[int, None]: url = f"{url}/info" model_info = dict(requests.get(url).json()) tgi_version = model_info.get("version", None) if version.parse(tgi_version) >= version.parse("2.1.0"): return model_info.get("max_input_tokens", None) else: return model_info.get("max_input_length", None) def get_max_total_tokens(url: str) -> Union[int, None]: url = f"{url}/info" model_info = dict(requests.get(url).json()) return model_info.get("max_total_tokens", None) def to_tgi_messages(messages: Sequence[ChatMessage]) -> Sequence[Message]: out_messages = [] for m in messages: tool_calls = m.additional_kwargs.get("tool_calls") out_messages.append( Message(role=m.role.value, content=m.content, tool_calls=tool_calls) ) return out_messages def force_single_tool_call(response: ChatResponse) -> None: tool_calls = response.message.additional_kwargs.get("tool_calls", []) if len(tool_calls) > 1: response.message.additional_kwargs["tool_calls"] = [tool_calls[0]] def resolve_tool_choice( tools: Optional[List[dict]] = None, tool_choice: str = "none" ) -> Union[str, dict]: """Resolve tool choice. Check if tool_name exists in tools. Note that unlike in OpenAI specification, 'auto' will ALWAYS choose the tool for you. Set to 'none' explicitly if do not wish to use tool. """ valid_tool_choices = ["none", "auto"] + [t["function"]["name"] for t in tools or []] if tool_choice not in valid_tool_choices: raise ValueError( f"{tool_choice} is not a valid tool_choice. Must be one of {valid_tool_choices}" ) return tool_choice