input
stringlengths
33
5k
output
stringlengths
32
5k
from docarray.typing.proto_register import _register_proto from docarray.typing.tensor.audio.abstract_audio_tensor import AbstractAudioTensor from docarray.typing.tensor.ndarray import NdArray @_register_proto(proto_type_name='audio_ndarray') class AudioNdArray(AbstractAudioTensor, NdArray): """ Subclass of NdArray, to represent an audio tensor. Adds audio-specific features to the tensor. EXAMPLE USAGE .. code-block:: python from typing import Optional from docarray import BaseDocument from docarray.typing import AudioNdArray, AudioUrl import numpy as np class MyAudioDoc(BaseDocument): title: str audio_tensor: Optional[AudioNdArray] url: Optional[AudioUrl] bytes_: Optional[bytes] # from tensor doc_1 = MyAudioDoc( title='my_first_audio_doc', audio_tensor=np.random.rand(1000, 2), ) doc_1.audio_tensor.save(file_path='path/to/file_1.wav') doc_1.bytes_ = doc_1.audio_tensor.to_bytes() # from url doc_2 = MyAudioDoc( title='my_second_audio_doc', url='https://www.kozco.com/tech/piano2.wav', ) doc_2.audio_tensor = doc_2.url.load() doc_2.audio_tensor.save(file_path='path/to/file_2.wav') doc_2.bytes_ = doc_1.audio_tensor.to_bytes() """ ...
from docarray.typing.proto_register import _register_proto from docarray.typing.tensor.audio.abstract_audio_tensor import AbstractAudioTensor from docarray.typing.tensor.ndarray import NdArray @_register_proto(proto_type_name='audio_ndarray') class AudioNdArray(AbstractAudioTensor, NdArray): """ Subclass of NdArray, to represent an audio tensor. Adds audio-specific features to the tensor. EXAMPLE USAGE .. code-block:: python from typing import Optional from docarray import BaseDocument from docarray.typing import AudioNdArray, AudioUrl import numpy as np class MyAudioDoc(BaseDocument): title: str audio_tensor: Optional[AudioNdArray] url: Optional[AudioUrl] bytes_: Optional[bytes] # from tensor doc_1 = MyAudioDoc( title='my_first_audio_doc', audio_tensor=np.random.rand(1000, 2), ) doc_1.audio_tensor.save_to_wav_file(file_path='path/to/file_1.wav') doc_1.bytes_ = doc_1.audio_tensor.to_bytes() # from url doc_2 = MyAudioDoc( title='my_second_audio_doc', url='https://www.kozco.com/tech/piano2.wav', ) doc_2.audio_tensor = doc_2.url.load() doc_2.audio_tensor.save_to_wav_file(file_path='path/to/file_2.wav') doc_2.bytes_ = doc_1.audio_tensor.to_bytes() """ ...
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.callbacks.tracers.comet import ( CometTracer, import_comet_llm_api, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "import_comet_llm_api": "langchain_community.callbacks.tracers.comet", "CometTracer": "langchain_community.callbacks.tracers.comet", } _import_attribute = create_importer(__file__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "CometTracer", "import_comet_llm_api", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.callbacks.tracers.comet import ( CometTracer, import_comet_llm_api, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "import_comet_llm_api": "langchain_community.callbacks.tracers.comet", "CometTracer": "langchain_community.callbacks.tracers.comet", } _import_attribute = create_importer(__file__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "import_comet_llm_api", "CometTracer", ]
from typing import Optional, List, Dict, Any, TYPE_CHECKING, Union from pydantic import BaseModel, validator from ..math.ndarray import to_list if TYPE_CHECKING: from ..typing import ArrayType # this order must be preserved: https://pydantic-docs.helpmanual.io/usage/types/#unions _ProtoValueType = Optional[Union[bool, float, str, list, dict]] _StructValueType = Union[ _ProtoValueType, List[_ProtoValueType], Dict[str, _ProtoValueType] ] _MetadataType = Dict[str, _StructValueType] def _convert_ndarray_to_list(v: 'ArrayType'): if v is not None: return to_list(v) class _NamedScore(BaseModel): value: Optional[float] = None op_name: Optional[str] = None description: Optional[str] = None ref_id: Optional[str] = None class _MetadataModel(BaseModel): metadata: _MetadataType class PydanticDocument(BaseModel): id: Optional[str] parent_id: Optional[str] granularity: Optional[int] adjacency: Optional[int] blob: Optional[str] tensor: Optional[Any] mime_type: Optional[str] text: Optional[str] weight: Optional[float] uri: Optional[str] tags: Optional[Dict[str, '_StructValueType']] _metadata: Optional[Dict[str, '_StructValueType']] offset: Optional[float] location: Optional[List[float]] embedding: Optional[Any] modality: Optional[str] evaluations: Optional[Dict[str, '_NamedScore']] scores: Optional[Dict[str, '_NamedScore']] chunks: Optional[List['PydanticDocument']] matches: Optional[List['PydanticDocument']] _tensor2list = validator('tensor', allow_reuse=True)(_convert_ndarray_to_list) _embedding2list = validator('embedding', allow_reuse=True)(_convert_ndarray_to_list) class Config: smart_union = True def __init__(self, **data): super().__init__(**data) # underscore attributes need to be set and validated manually _metadata = data.get('_metadata', None) if _metadata is not None: _md_model = _MetadataModel(metadata=_metadata) # validate _metadata object.__setattr__(self, '_metadata', _md_model.metadata) PydanticDocument.update_forward_refs() PydanticDocumentArray = List[PydanticDocument]
from typing import Optional, List, Dict, Any, TYPE_CHECKING, Union from pydantic import BaseModel, validator from ..math.ndarray import to_list if TYPE_CHECKING: from ..typing import ArrayType # this order must be preserved: https://pydantic-docs.helpmanual.io/usage/types/#unions _ProtoValueType = Optional[Union[bool, float, str, list, dict]] _StructValueType = Union[ _ProtoValueType, List[_ProtoValueType], Dict[str, _ProtoValueType] ] def _convert_ndarray_to_list(v: 'ArrayType'): if v is not None: return to_list(v) class _NamedScore(BaseModel): value: Optional[float] = None op_name: Optional[str] = None description: Optional[str] = None ref_id: Optional[str] = None class PydanticDocument(BaseModel): id: Optional[str] parent_id: Optional[str] granularity: Optional[int] adjacency: Optional[int] blob: Optional[str] tensor: Optional[Any] mime_type: Optional[str] text: Optional[str] weight: Optional[float] uri: Optional[str] tags: Optional[Dict[str, '_StructValueType']] _metadata: Optional[Dict[str, '_StructValueType']] offset: Optional[float] location: Optional[List[float]] embedding: Optional[Any] modality: Optional[str] evaluations: Optional[Dict[str, '_NamedScore']] scores: Optional[Dict[str, '_NamedScore']] chunks: Optional[List['PydanticDocument']] matches: Optional[List['PydanticDocument']] _tensor2list = validator('tensor', allow_reuse=True)(_convert_ndarray_to_list) _embedding2list = validator('embedding', allow_reuse=True)(_convert_ndarray_to_list) class Config: smart_union = True PydanticDocument.update_forward_refs() PydanticDocumentArray = List[PydanticDocument]
""" ========================= Tensor transforms and JIT ========================= This example illustrates various features that are now supported by the :ref:`image transformations <transforms>` on Tensor images. In particular, we show how image transforms can be performed on GPU, and how one can also script them using JIT compilation. Prior to v0.8.0, transforms in torchvision have traditionally been PIL-centric and presented multiple limitations due to that. Now, since v0.8.0, transforms implementations are Tensor and PIL compatible, and we can achieve the following new features: - transform multi-band torch tensor images (with more than 3-4 channels) - torchscript transforms together with your model for deployment - support for GPU acceleration - batched transformation such as for videos - read and decode data directly as torch tensor with torchscript support (for PNG and JPEG image formats) .. note:: These features are only possible with **Tensor** images. """ from pathlib import Path import matplotlib.pyplot as plt import numpy as np import torch import torchvision.transforms as T from torchvision.io import read_image plt.rcParams["savefig.bbox"] = 'tight' torch.manual_seed(1) def show(imgs): fix, axs = plt.subplots(ncols=len(imgs), squeeze=False) for i, img in enumerate(imgs): img = T.ToPILImage()(img.to('cpu')) axs[0, i].imshow(np.asarray(img)) axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) # %% # The :func:`~torchvision.io.read_image` function allows to read an image and # directly load it as a tensor dog1 = read_image(str(Path('assets') / 'dog1.jpg')) dog2 = read_image(str(Path('assets') / 'dog2.jpg')) show([dog1, dog2]) # %% # Transforming images on GPU # -------------------------- # Most transforms natively support tensors on top of PIL images (to visualize # the effect of the transforms, you may refer to see # :ref:`sphx_glr_auto_examples_plot_transforms.py`). # Using tensor images, we can run the transforms on GPUs if cuda is available! import torch.nn as nn transforms = torch.nn.Sequential( T.RandomCrop(224), T.RandomHorizontalFlip(p=0.3), ) device = 'cuda' if torch.cuda.is_available() else 'cpu' dog1 = dog1.to(device) dog2 = dog2.to(device) transformed_dog1 = transforms(dog1) transformed_dog2 = transforms(dog2) show([transformed_dog1, transformed_dog2]) # %% # Scriptable transforms for easier deployment via torchscript # ----------------------------------------------------------- # We now show how to combine image transformations and a model forward pass, # while using ``torch.jit.script`` to obtain a single scripted module. # # Let's define a ``Predictor`` module that transforms the input tensor and then # applies an ImageNet model on it. from torchvision.models import resnet18, ResNet18_Weights class Predictor(nn.Module): def __init__(self): super().__init__() weights = ResNet18_Weights.DEFAULT self.resnet18 = resnet18(weights=weights, progress=False).eval() self.transforms = weights.transforms() def forward(self, x: torch.Tensor) -> torch.Tensor: with torch.no_grad(): x = self.transforms(x) y_pred = self.resnet18(x) return y_pred.argmax(dim=1) # %% # Now, let's define scripted and non-scripted instances of ``Predictor`` and # apply it on multiple tensor images of the same size predictor = Predictor().to(device) scripted_predictor = torch.jit.script(predictor).to(device) batch = torch.stack([dog1, dog2]).to(device) res = predictor(batch) res_scripted = scripted_predictor(batch) # %% # We can verify that the prediction of the scripted and non-scripted models are # the same: import json with open(Path('assets') / 'imagenet_class_index.json') as labels_file: labels = json.load(labels_file) for i, (pred, pred_scripted) in enumerate(zip(res, res_scripted)): assert pred == pred_scripted print(f"Prediction for Dog {i + 1}: {labels[str(pred.item())]}") # %% # Since the model is scripted, it can be easily dumped on disk and re-used import tempfile with tempfile.NamedTemporaryFile() as f: scripted_predictor.save(f.name) dumped_scripted_predictor = torch.jit.load(f.name) res_scripted_dumped = dumped_scripted_predictor(batch) assert (res_scripted_dumped == res_scripted).all()
""" ========================= Tensor transforms and JIT ========================= This example illustrates various features that are now supported by the :ref:`image transformations <transforms>` on Tensor images. In particular, we show how image transforms can be performed on GPU, and how one can also script them using JIT compilation. Prior to v0.8.0, transforms in torchvision have traditionally been PIL-centric and presented multiple limitations due to that. Now, since v0.8.0, transforms implementations are Tensor and PIL compatible, and we can achieve the following new features: - transform multi-band torch tensor images (with more than 3-4 channels) - torchscript transforms together with your model for deployment - support for GPU acceleration - batched transformation such as for videos - read and decode data directly as torch tensor with torchscript support (for PNG and JPEG image formats) .. note:: These features are only possible with **Tensor** images. """ from pathlib import Path import matplotlib.pyplot as plt import numpy as np import torch import torchvision.transforms as T from torchvision.io import read_image plt.rcParams["savefig.bbox"] = 'tight' torch.manual_seed(1) def show(imgs): fix, axs = plt.subplots(ncols=len(imgs), squeeze=False) for i, img in enumerate(imgs): img = T.ToPILImage()(img.to('cpu')) axs[0, i].imshow(np.asarray(img)) axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[]) #################################### # The :func:`~torchvision.io.read_image` function allows to read an image and # directly load it as a tensor dog1 = read_image(str(Path('assets') / 'dog1.jpg')) dog2 = read_image(str(Path('assets') / 'dog2.jpg')) show([dog1, dog2]) #################################### # Transforming images on GPU # -------------------------- # Most transforms natively support tensors on top of PIL images (to visualize # the effect of the transforms, you may refer to see # :ref:`sphx_glr_auto_examples_plot_transforms.py`). # Using tensor images, we can run the transforms on GPUs if cuda is available! import torch.nn as nn transforms = torch.nn.Sequential( T.RandomCrop(224), T.RandomHorizontalFlip(p=0.3), ) device = 'cuda' if torch.cuda.is_available() else 'cpu' dog1 = dog1.to(device) dog2 = dog2.to(device) transformed_dog1 = transforms(dog1) transformed_dog2 = transforms(dog2) show([transformed_dog1, transformed_dog2]) #################################### # Scriptable transforms for easier deployment via torchscript # ----------------------------------------------------------- # We now show how to combine image transformations and a model forward pass, # while using ``torch.jit.script`` to obtain a single scripted module. # # Let's define a ``Predictor`` module that transforms the input tensor and then # applies an ImageNet model on it. from torchvision.models import resnet18, ResNet18_Weights class Predictor(nn.Module): def __init__(self): super().__init__() weights = ResNet18_Weights.DEFAULT self.resnet18 = resnet18(weights=weights, progress=False).eval() self.transforms = weights.transforms() def forward(self, x: torch.Tensor) -> torch.Tensor: with torch.no_grad(): x = self.transforms(x) y_pred = self.resnet18(x) return y_pred.argmax(dim=1) #################################### # Now, let's define scripted and non-scripted instances of ``Predictor`` and # apply it on multiple tensor images of the same size predictor = Predictor().to(device) scripted_predictor = torch.jit.script(predictor).to(device) batch = torch.stack([dog1, dog2]).to(device) res = predictor(batch) res_scripted = scripted_predictor(batch) #################################### # We can verify that the prediction of the scripted and non-scripted models are # the same: import json with open(Path('assets') / 'imagenet_class_index.json') as labels_file: labels = json.load(labels_file) for i, (pred, pred_scripted) in enumerate(zip(res, res_scripted)): assert pred == pred_scripted print(f"Prediction for Dog {i + 1}: {labels[str(pred.item())]}") #################################### # Since the model is scripted, it can be easily dumped on disk and re-used import tempfile with tempfile.NamedTemporaryFile() as f: scripted_predictor.save(f.name) dumped_scripted_predictor = torch.jit.load(f.name) res_scripted_dumped = dumped_scripted_predictor(batch) assert (res_scripted_dumped == res_scripted).all()
from typing import Annotated, Optional import typer from langchain_cli._version import __version__ from langchain_cli.namespaces import app as app_namespace from langchain_cli.namespaces import integration as integration_namespace from langchain_cli.namespaces import template as template_namespace from langchain_cli.namespaces.migrate import main as migrate_namespace from langchain_cli.utils.packages import get_langserve_export, get_package_root app = typer.Typer(no_args_is_help=True, add_completion=False) app.add_typer( template_namespace.package_cli, name="template", help=template_namespace.__doc__, ) app.add_typer(app_namespace.app_cli, name="app", help=app_namespace.__doc__) app.add_typer( integration_namespace.integration_cli, name="integration", help=integration_namespace.__doc__, ) app.command( name="migrate", context_settings={ # Let Grit handle the arguments "allow_extra_args": True, "ignore_unknown_options": True, }, )( migrate_namespace.migrate, ) def version_callback(show_version: bool) -> None: # noqa: FBT001 if show_version: typer.echo(f"langchain-cli {__version__}") raise typer.Exit @app.callback() def main( version: bool = typer.Option( # noqa: FBT001 False, # noqa: FBT003 "--version", "-v", help="Print the current CLI version.", callback=version_callback, is_eager=True, ), ) -> None: pass @app.command() def serve( *, port: Annotated[ Optional[int], typer.Option(help="The port to run the server on"), ] = None, host: Annotated[ Optional[str], typer.Option(help="The host to run the server on"), ] = None, ) -> None: """Start the LangServe app, whether it's a template or an app.""" # see if is a template try: project_dir = get_package_root() pyproject = project_dir / "pyproject.toml" get_langserve_export(pyproject) except KeyError: # not a template app_namespace.serve(port=port, host=host) else: # is a template template_namespace.serve(port=port, host=host) if __name__ == "__main__": app()
from typing import Annotated, Optional import typer from langchain_cli._version import __version__ from langchain_cli.namespaces import app as app_namespace from langchain_cli.namespaces import integration as integration_namespace from langchain_cli.namespaces import template as template_namespace from langchain_cli.namespaces.migrate import main as migrate_namespace from langchain_cli.utils.packages import get_langserve_export, get_package_root app = typer.Typer(no_args_is_help=True, add_completion=False) app.add_typer( template_namespace.package_cli, name="template", help=template_namespace.__doc__ ) app.add_typer(app_namespace.app_cli, name="app", help=app_namespace.__doc__) app.add_typer( integration_namespace.integration_cli, name="integration", help=integration_namespace.__doc__, ) app.command( name="migrate", context_settings={ # Let Grit handle the arguments "allow_extra_args": True, "ignore_unknown_options": True, }, )( migrate_namespace.migrate, ) def version_callback(show_version: bool) -> None: if show_version: typer.echo(f"langchain-cli {__version__}") raise typer.Exit @app.callback() def main( version: bool = typer.Option( False, "--version", "-v", help="Print the current CLI version.", callback=version_callback, is_eager=True, ), ) -> None: pass @app.command() def serve( *, port: Annotated[ Optional[int], typer.Option(help="The port to run the server on") ] = None, host: Annotated[ Optional[str], typer.Option(help="The host to run the server on") ] = None, ) -> None: """Start the LangServe app, whether it's a template or an app.""" # see if is a template try: project_dir = get_package_root() pyproject = project_dir / "pyproject.toml" get_langserve_export(pyproject) except KeyError: # not a template app_namespace.serve(port=port, host=host) else: # is a template template_namespace.serve(port=port, host=host) if __name__ == "__main__": app()
# Copyright (c) OpenMMLab. All rights reserved. import torch from mmdet.structures.bbox import BaseBoxes def anchor_inside_flags(flat_anchors, valid_flags, img_shape, allowed_border=0): """Check whether the anchors are inside the border. Args: flat_anchors (torch.Tensor): Flatten anchors, shape (n, 4). valid_flags (torch.Tensor): An existing valid flags of anchors. img_shape (tuple(int)): Shape of current image. allowed_border (int, optional): The border to allow the valid anchor. Defaults to 0. Returns: torch.Tensor: Flags indicating whether the anchors are inside a \ valid range. """ img_h, img_w = img_shape[:2] if allowed_border >= 0: if isinstance(flat_anchors, BaseBoxes): inside_flags = valid_flags & \ flat_anchors.is_inside([img_h, img_w], all_inside=True, allowed_border=allowed_border) else: inside_flags = valid_flags & \ (flat_anchors[:, 0] >= -allowed_border) & \ (flat_anchors[:, 1] >= -allowed_border) & \ (flat_anchors[:, 2] < img_w + allowed_border) & \ (flat_anchors[:, 3] < img_h + allowed_border) else: inside_flags = valid_flags return inside_flags def calc_region(bbox, ratio, featmap_size=None): """Calculate a proportional bbox region. The bbox center are fixed and the new h' and w' is h * ratio and w * ratio. Args: bbox (Tensor): Bboxes to calculate regions, shape (n, 4). ratio (float): Ratio of the output region. featmap_size (tuple): Feature map size used for clipping the boundary. Returns: tuple: x1, y1, x2, y2 """ x1 = torch.round((1 - ratio) * bbox[0] + ratio * bbox[2]).long() y1 = torch.round((1 - ratio) * bbox[1] + ratio * bbox[3]).long() x2 = torch.round(ratio * bbox[0] + (1 - ratio) * bbox[2]).long() y2 = torch.round(ratio * bbox[1] + (1 - ratio) * bbox[3]).long() if featmap_size is not None: x1 = x1.clamp(min=0, max=featmap_size[1]) y1 = y1.clamp(min=0, max=featmap_size[0]) x2 = x2.clamp(min=0, max=featmap_size[1]) y2 = y2.clamp(min=0, max=featmap_size[0]) return (x1, y1, x2, y2)
# Copyright (c) OpenMMLab. All rights reserved. import torch def anchor_inside_flags(flat_anchors, valid_flags, img_shape, allowed_border=0): """Check whether the anchors are inside the border. Args: flat_anchors (torch.Tensor): Flatten anchors, shape (n, 4). valid_flags (torch.Tensor): An existing valid flags of anchors. img_shape (tuple(int)): Shape of current image. allowed_border (int, optional): The border to allow the valid anchor. Defaults to 0. Returns: torch.Tensor: Flags indicating whether the anchors are inside a \ valid range. """ img_h, img_w = img_shape[:2] if allowed_border >= 0: inside_flags = valid_flags & \ (flat_anchors[:, 0] >= -allowed_border) & \ (flat_anchors[:, 1] >= -allowed_border) & \ (flat_anchors[:, 2] < img_w + allowed_border) & \ (flat_anchors[:, 3] < img_h + allowed_border) else: inside_flags = valid_flags return inside_flags def calc_region(bbox, ratio, featmap_size=None): """Calculate a proportional bbox region. The bbox center are fixed and the new h' and w' is h * ratio and w * ratio. Args: bbox (Tensor): Bboxes to calculate regions, shape (n, 4). ratio (float): Ratio of the output region. featmap_size (tuple): Feature map size used for clipping the boundary. Returns: tuple: x1, y1, x2, y2 """ x1 = torch.round((1 - ratio) * bbox[0] + ratio * bbox[2]).long() y1 = torch.round((1 - ratio) * bbox[1] + ratio * bbox[3]).long() x2 = torch.round(ratio * bbox[0] + (1 - ratio) * bbox[2]).long() y2 = torch.round(ratio * bbox[1] + (1 - ratio) * bbox[3]).long() if featmap_size is not None: x1 = x1.clamp(min=0, max=featmap_size[1]) y1 = y1.clamp(min=0, max=featmap_size[0]) x2 = x2.clamp(min=0, max=featmap_size[1]) y2 = y2.clamp(min=0, max=featmap_size[0]) return (x1, y1, x2, y2)
# Owner(s): ["oncall: distributed"] from unittest import mock import torch.distributed as c10d from torch.distributed.collective_utils import all_gather, broadcast from torch.testing._internal.common_distributed import MultiProcessTestCase from torch.testing._internal.common_utils import run_tests class TestCollectiveUtils(MultiProcessTestCase): def setUp(self): super().setUp() self._spawn_processes() def tearDown(self) -> None: super().tearDown() def opts(self, threads=2): opts = c10d.ProcessGroupGloo._Options() opts._timeout = 50.0 opts._threads = threads return opts def test_broadcast_result(self) -> None: """ Basic unit test for broadcast using a process group of default world size. """ store = c10d.FileStore(self.file_name, self.world_size) c10d.init_process_group( backend="gloo", store=store, rank=self.rank, world_size=self.world_size ) pg = c10d.new_group(pg_options=self.opts()) func = mock.MagicMock() func.return_value = pg.rank() res = broadcast(data_or_fn=func, rank=0, pg=pg) assert res == 0, f"Expect res to be 0 (got {res})" if pg.rank() == 0: func.assert_called_once() else: func.assert_not_called() func.reset_mock() res = broadcast(data_or_fn=func, rank=1, pg=pg) assert res == 1, f"Expect res to be 1 (got {res})" if pg.rank() == 1: func.assert_called_once() else: func.assert_not_called() def test_broadcast_result_no_pg(self) -> None: """ Ensure broadcast has no dependency on torch.distributed when run in single process. """ func = mock.MagicMock() broadcast(data_or_fn=func, rank=0) func.assert_called_once() def test_broadcast_result_raises_exceptions_from_func( self, ) -> None: """ Ensure broadcast exception is propagated properly. """ # no process group func = mock.MagicMock() exc = Exception("test exception") func.side_effect = exc expected_exception = "test exception" with self.assertRaisesRegex(Exception, expected_exception): broadcast(data_or_fn=func, rank=0) def test_all_gather_result(self) -> None: """ Basic unit test for all_gather using a process group of default world size. """ store = c10d.FileStore(self.file_name, self.world_size) c10d.init_process_group( backend="gloo", store=store, rank=self.rank, world_size=self.world_size ) pg = c10d.new_group(pg_options=self.opts()) func = mock.MagicMock() func.return_value = pg.rank() res = all_gather(data_or_fn=func, pg=pg) func.assert_called_once() assert res == list(range(self.world_size)), ( f"Expect res to be list of 0 through {self.world_size} (got {res})" ) def test_all_gather_result_no_pg(self) -> None: """ Ensure all_gather has no dependency on torch.distributed when run in single process. """ func = mock.MagicMock() all_gather(data_or_fn=func) func.assert_called_once() def test_all_gather_result_raises_exceptions_from_func( self, ) -> None: """ Ensure all_gather exception is propagated properly. """ # no process group func = mock.MagicMock() exc = Exception("test exception") func.side_effect = exc expected_exception = "test exception" with self.assertRaisesRegex(Exception, expected_exception): all_gather(data_or_fn=func) if __name__ == "__main__": run_tests()
# Owner(s): ["oncall: distributed"] from unittest import mock import torch.distributed as c10d from torch.distributed.collective_utils import all_gather, broadcast from torch.testing._internal.common_distributed import MultiProcessTestCase from torch.testing._internal.common_utils import run_tests class TestCollectiveUtils(MultiProcessTestCase): def setUp(self): super().setUp() self._spawn_processes() def tearDown(self) -> None: super().tearDown() def opts(self, threads=2): opts = c10d.ProcessGroupGloo._Options() opts._timeout = 50.0 opts._threads = threads return opts def test_broadcast_result(self) -> None: """ Basic unit test for broadcast using a process group of default world size. """ store = c10d.FileStore(self.file_name, self.world_size) c10d.init_process_group( backend="gloo", store=store, rank=self.rank, world_size=self.world_size ) pg = c10d.new_group(pg_options=self.opts()) func = mock.MagicMock() func.return_value = pg.rank() res = broadcast(data_or_fn=func, rank=0, pg=pg) assert res == 0, f"Expect res to be 0 (got {res})" if pg.rank() == 0: func.assert_called_once() else: func.assert_not_called() func.reset_mock() res = broadcast(data_or_fn=func, rank=1, pg=pg) assert res == 1, f"Expect res to be 1 (got {res})" if pg.rank() == 1: func.assert_called_once() else: func.assert_not_called() def test_broadcast_result_no_pg(self) -> None: """ Ensure broadcast has no dependency on torch.distributed when run in single process. """ func = mock.MagicMock() broadcast(data_or_fn=func, rank=0) func.assert_called_once() def test_broadcast_result_raises_exceptions_from_func( self, ) -> None: """ Ensure broadcast exception is propagated properly. """ # no process group func = mock.MagicMock() exc = Exception("test exception") func.side_effect = exc expected_exception = "test exception" with self.assertRaisesRegex(Exception, expected_exception): broadcast(data_or_fn=func, rank=0) def test_all_gather_result(self) -> None: """ Basic unit test for all_gather using a process group of default world size. """ store = c10d.FileStore(self.file_name, self.world_size) c10d.init_process_group( backend="gloo", store=store, rank=self.rank, world_size=self.world_size ) pg = c10d.new_group(pg_options=self.opts()) func = mock.MagicMock() func.return_value = pg.rank() res = all_gather(data_or_fn=func, pg=pg) func.assert_called_once() assert res == list( range(self.world_size) ), f"Expect res to be list of 0 through {self.world_size} (got {res})" def test_all_gather_result_no_pg(self) -> None: """ Ensure all_gather has no dependency on torch.distributed when run in single process. """ func = mock.MagicMock() all_gather(data_or_fn=func) func.assert_called_once() def test_all_gather_result_raises_exceptions_from_func( self, ) -> None: """ Ensure all_gather exception is propagated properly. """ # no process group func = mock.MagicMock() exc = Exception("test exception") func.side_effect = exc expected_exception = "test exception" with self.assertRaisesRegex(Exception, expected_exception): all_gather(data_or_fn=func) if __name__ == "__main__": run_tests()
from typing import Any, Optional, Type, TypeVar, Union from docarray.base_doc import BaseDoc from docarray.typing import TextUrl from docarray.typing.tensor.embedding import AnyEmbedding T = TypeVar('T', bound='TextDoc') class TextDoc(BaseDoc): """ Document for handling text. It can contain: - a [`TextUrl`][docarray.typing.url.TextUrl] (`TextDoc.url`) - a `str` (`TextDoc.text`) - an [`AnyEmbedding`](../../../api_references/typing/tensor/embedding) (`TextDoc.embedding`) - a `bytes` object (`TextDoc.bytes_`) You can use this Document directly: ```python from docarray.documents import TextDoc # use it directly txt_doc = TextDoc(url='https://www.gutenberg.org/files/1065/1065-0.txt') txt_doc.text = txt_doc.url.load() # model = MyEmbeddingModel() # txt_doc.embedding = model(txt_doc.text) ``` You can initialize directly from a string: ```python from docarray.documents import TextDoc txt_doc = TextDoc('hello world') ``` You can extend this Document: ```python from docarray.documents import TextDoc from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyText(TextDoc): second_embedding: Optional[AnyEmbedding] txt_doc = MyText(url='https://www.gutenberg.org/files/1065/1065-0.txt') txt_doc.text = txt_doc.url.load() # model = MyEmbeddingModel() # txt_doc.embedding = model(txt_doc.text) # txt_doc.second_embedding = model(txt_doc.text) ``` You can use this Document for composition: ```python from docarray import BaseDoc from docarray.documents import ImageDoc, TextDoc # compose it class MultiModalDoc(BaseDoc): image_doc: ImageDoc text_doc: TextDoc mmdoc = MultiModalDoc( image_doc=ImageDoc( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ), text_doc=TextDoc(text='hello world, how are you doing?'), ) mmdoc.image_doc.tensor = mmdoc.image_doc.url.load() # or mmdoc.image_doc.bytes_ = mmdoc.image_doc.url.load_bytes() mmdoc.image_doc.tensor = mmdoc.image_doc.bytes_.load() ``` This Document can be compared against another Document of the same type or a string. When compared against another object of the same type, the pydantic BaseModel equality check will apply which checks the equality of every attribute, excluding `id`. When compared against a str, it will check the equality of the `text` attribute against the given string. ```python from docarray.documents import TextDoc doc = TextDoc(text='This is the main text', url='exampleurl.com/file') doc2 = TextDoc(text='This is the main text', url='exampleurl.com/file') doc == 'This is the main text' # True doc == doc2 # True ``` """ text: Optional[str] = None url: Optional[TextUrl] = None embedding: Optional[AnyEmbedding] = None bytes_: Optional[bytes] = None def __init__(self, text: Optional[str] = None, **kwargs): if 'text' not in kwargs: kwargs['text'] = text super().__init__(**kwargs) @classmethod def validate( cls: Type[T], value: Union[str, Any], ) -> T: if isinstance(value, str): value = cls(text=value) return super().validate(value) def __eq__(self, other: Any) -> bool: if isinstance(other, str): return self.text == other else: # BaseModel has a default equality return super().__eq__(other) def __contains__(self, item: str) -> bool: """ This method makes `TextDoc` behave the same as an `str`. :param item: A string to be checked if is a substring of `text` attribute :return: A boolean determining the presence of `item` as a substring in `text` ```python from docarray.documents import TextDoc t = TextDoc(text='this is my text document') assert 'text' in t assert 'docarray' not in t ``` """ if self.text is not None: return self.text.__contains__(item) else: return False def _get_string_for_regex_filter(self): return self.text
from typing import Any, Optional, Type, TypeVar, Union from docarray.base_doc import BaseDoc from docarray.typing import TextUrl from docarray.typing.tensor.embedding import AnyEmbedding T = TypeVar('T', bound='TextDoc') class TextDoc(BaseDoc): """ Document for handling text. It can contain: - a [`TextUrl`][docarray.typing.url.TextUrl] (`TextDoc.url`) - a `str` (`TextDoc.text`) - an [`AnyEmbedding`](../../../api_references/typing/tensor/embedding) (`TextDoc.embedding`) - a `bytes` object (`TextDoc.bytes_`) You can use this Document directly: ```python from docarray.documents import TextDoc # use it directly txt_doc = TextDoc(url='https://www.gutenberg.org/files/1065/1065-0.txt') txt_doc.text = txt_doc.url.load() # model = MyEmbeddingModel() # txt_doc.embedding = model(txt_doc.text) ``` You can initialize directly from a string: ```python from docarray.documents import TextDoc txt_doc = TextDoc('hello world') ``` You can extend this Document: ```python from docarray.documents import TextDoc from docarray.typing import AnyEmbedding from typing import Optional # extend it class MyText(TextDoc): second_embedding: Optional[AnyEmbedding] txt_doc = MyText(url='https://www.gutenberg.org/files/1065/1065-0.txt') txt_doc.text = txt_doc.url.load() # model = MyEmbeddingModel() # txt_doc.embedding = model(txt_doc.text) # txt_doc.second_embedding = model(txt_doc.text) ``` You can use this Document for composition: ```python from docarray import BaseDoc from docarray.documents import ImageDoc, TextDoc # compose it class MultiModalDoc(BaseDoc): image_doc: ImageDoc text_doc: TextDoc mmdoc = MultiModalDoc( image_doc=ImageDoc( url='https://github.com/docarray/docarray/blob/main/tests/toydata/image-data/apple.png?raw=true' ), text_doc=TextDoc(text='hello world, how are you doing?'), ) mmdoc.image_doc.tensor = mmdoc.image_doc.url.load() # or mmdoc.image_doc.bytes_ = mmdoc.image_doc.url.load_bytes() mmdoc.image_doc.tensor = mmdoc.image_doc.bytes_.load() ``` This Document can be compared against another Document of the same type or a string. When compared against another object of the same type, the pydantic BaseModel equality check will apply which checks the equality of every attribute, excluding `id`. When compared against a str, it will check the equality of the `text` attribute against the given string. ```python from docarray.documents import TextDoc doc = TextDoc(text='This is the main text', url='exampleurl.com/file') doc2 = TextDoc(text='This is the main text', url='exampleurl.com/file') doc == 'This is the main text' # True doc == doc2 # True ``` """ text: Optional[str] url: Optional[TextUrl] embedding: Optional[AnyEmbedding] bytes_: Optional[bytes] def __init__(self, text: Optional[str] = None, **kwargs): if 'text' not in kwargs: kwargs['text'] = text super().__init__(**kwargs) @classmethod def validate( cls: Type[T], value: Union[str, Any], ) -> T: if isinstance(value, str): value = cls(text=value) return super().validate(value) def __eq__(self, other: Any) -> bool: if isinstance(other, str): return self.text == other else: # BaseModel has a default equality return super().__eq__(other) def __contains__(self, item: str) -> bool: """ This method makes `TextDoc` behave the same as an `str`. :param item: A string to be checked if is a substring of `text` attribute :return: A boolean determining the presence of `item` as a substring in `text` ```python from docarray.documents import TextDoc t = TextDoc(text='this is my text document') assert 'text' in t assert 'docarray' not in t ``` """ if self.text is not None: return self.text.__contains__(item) else: return False def _get_string_for_regex_filter(self): return self.text
_base_ = [ '../_base_/models/mask-rcnn_r50_fpn.py', '../_base_/datasets/deepfashion.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict( roi_head=dict( bbox_head=dict(num_classes=15), mask_head=dict(num_classes=15))) # runtime settings max_epochs = 15 train_cfg = dict( type='EpochBasedTrainLoop', max_epochs=max_epochs, val_interval=1) 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=[8, 11], gamma=0.1) ]
_base_ = [ '../_base_/models/mask_rcnn_r50_fpn.py', '../_base_/datasets/deepfashion.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict( roi_head=dict( bbox_head=dict(num_classes=15), mask_head=dict(num_classes=15))) # runtime settings max_epochs = 15 train_cfg = dict( type='EpochBasedTrainLoop', max_epochs=max_epochs, val_interval=1) 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=[8, 11], gamma=0.1) ]
# Copyright (c) OpenMMLab. All rights reserved. from ..builder import DETECTORS from .single_stage import SingleStageDetector @DETECTORS.register_module() class RepPointsDetector(SingleStageDetector): """RepPoints: Point Set Representation for Object Detection. This detector is the implementation of: - RepPoints detector (https://arxiv.org/pdf/1904.11490) """ def __init__(self, backbone, neck, bbox_head, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(RepPointsDetector, self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg)
from ..builder import DETECTORS from .single_stage import SingleStageDetector @DETECTORS.register_module() class RepPointsDetector(SingleStageDetector): """RepPoints: Point Set Representation for Object Detection. This detector is the implementation of: - RepPoints detector (https://arxiv.org/pdf/1904.11490) """ def __init__(self, backbone, neck, bbox_head, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(RepPointsDetector, self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg)
import math import os import pytest import torch import torchvision from torchvision import _HAS_GPU_VIDEO_DECODER from torchvision.io import VideoReader try: import av except ImportError: av = None VIDEO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "videos") @pytest.mark.skipif(_HAS_GPU_VIDEO_DECODER is False, reason="Didn't compile with support for gpu decoder") class TestVideoGPUDecoder: @pytest.mark.skipif(av is None, reason="PyAV unavailable") @pytest.mark.parametrize( "video_file", [ "RATRACE_wave_f_nm_np1_fr_goo_37.avi", "TrumanShow_wave_f_nm_np1_fr_med_26.avi", "v_SoccerJuggling_g23_c01.avi", "v_SoccerJuggling_g24_c01.avi", "R6llTwEh07w.mp4", "SOX5yA1l24A.mp4", "WUzgd7C1pWA.mp4", ], ) def test_frame_reading(self, video_file): torchvision.set_video_backend("cuda") full_path = os.path.join(VIDEO_DIR, video_file) decoder = VideoReader(full_path) with av.open(full_path) as container: for av_frame in container.decode(container.streams.video[0]): av_frames = torch.tensor(av_frame.to_rgb(src_colorspace="ITU709").to_ndarray()) vision_frames = next(decoder)["data"] mean_delta = torch.mean(torch.abs(av_frames.float() - vision_frames.cpu().float())) assert mean_delta < 0.75 @pytest.mark.skipif(av is None, reason="PyAV unavailable") @pytest.mark.parametrize("keyframes", [True, False]) @pytest.mark.parametrize( "full_path, duration", [ (os.path.join(VIDEO_DIR, x), y) for x, y in [ ("v_SoccerJuggling_g23_c01.avi", 8.0), ("v_SoccerJuggling_g24_c01.avi", 8.0), ("R6llTwEh07w.mp4", 10.0), ("SOX5yA1l24A.mp4", 11.0), ("WUzgd7C1pWA.mp4", 11.0), ] ], ) def test_seek_reading(self, keyframes, full_path, duration): torchvision.set_video_backend("cuda") decoder = VideoReader(full_path) time = duration / 2 decoder.seek(time, keyframes_only=keyframes) with av.open(full_path) as container: container.seek(int(time * 1000000), any_frame=not keyframes, backward=False) for av_frame in container.decode(container.streams.video[0]): av_frames = torch.tensor(av_frame.to_rgb(src_colorspace="ITU709").to_ndarray()) vision_frames = next(decoder)["data"] mean_delta = torch.mean(torch.abs(av_frames.float() - vision_frames.cpu().float())) assert mean_delta < 0.75 @pytest.mark.skipif(av is None, reason="PyAV unavailable") @pytest.mark.parametrize( "video_file", [ "RATRACE_wave_f_nm_np1_fr_goo_37.avi", "TrumanShow_wave_f_nm_np1_fr_med_26.avi", "v_SoccerJuggling_g23_c01.avi", "v_SoccerJuggling_g24_c01.avi", "R6llTwEh07w.mp4", "SOX5yA1l24A.mp4", "WUzgd7C1pWA.mp4", ], ) def test_metadata(self, video_file): torchvision.set_video_backend("cuda") full_path = os.path.join(VIDEO_DIR, video_file) decoder = VideoReader(full_path) video_metadata = decoder.get_metadata()["video"] with av.open(full_path) as container: video = container.streams.video[0] av_duration = float(video.duration * video.time_base) assert math.isclose(video_metadata["duration"], av_duration, rel_tol=1e-2) assert math.isclose(video_metadata["fps"], video.base_rate, rel_tol=1e-2) if __name__ == "__main__": pytest.main([__file__])
import math import os import pytest import torch from torchvision.io import _HAS_GPU_VIDEO_DECODER, VideoReader try: import av except ImportError: av = None VIDEO_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "videos") @pytest.mark.skipif(_HAS_GPU_VIDEO_DECODER is False, reason="Didn't compile with support for gpu decoder") class TestVideoGPUDecoder: @pytest.mark.skipif(av is None, reason="PyAV unavailable") @pytest.mark.parametrize( "video_file", [ "RATRACE_wave_f_nm_np1_fr_goo_37.avi", "TrumanShow_wave_f_nm_np1_fr_med_26.avi", "v_SoccerJuggling_g23_c01.avi", "v_SoccerJuggling_g24_c01.avi", "R6llTwEh07w.mp4", "SOX5yA1l24A.mp4", "WUzgd7C1pWA.mp4", ], ) def test_frame_reading(self, video_file): full_path = os.path.join(VIDEO_DIR, video_file) decoder = VideoReader(full_path, device="cuda") with av.open(full_path) as container: for av_frame in container.decode(container.streams.video[0]): av_frames = torch.tensor(av_frame.to_rgb(src_colorspace="ITU709").to_ndarray()) vision_frames = next(decoder)["data"] mean_delta = torch.mean(torch.abs(av_frames.float() - vision_frames.cpu().float())) assert mean_delta < 0.75 @pytest.mark.skipif(av is None, reason="PyAV unavailable") @pytest.mark.parametrize("keyframes", [True, False]) @pytest.mark.parametrize( "full_path, duration", [ (os.path.join(VIDEO_DIR, x), y) for x, y in [ ("v_SoccerJuggling_g23_c01.avi", 8.0), ("v_SoccerJuggling_g24_c01.avi", 8.0), ("R6llTwEh07w.mp4", 10.0), ("SOX5yA1l24A.mp4", 11.0), ("WUzgd7C1pWA.mp4", 11.0), ] ], ) def test_seek_reading(self, keyframes, full_path, duration): decoder = VideoReader(full_path, device="cuda") time = duration / 2 decoder.seek(time, keyframes_only=keyframes) with av.open(full_path) as container: container.seek(int(time * 1000000), any_frame=not keyframes, backward=False) for av_frame in container.decode(container.streams.video[0]): av_frames = torch.tensor(av_frame.to_rgb(src_colorspace="ITU709").to_ndarray()) vision_frames = next(decoder)["data"] mean_delta = torch.mean(torch.abs(av_frames.float() - vision_frames.cpu().float())) assert mean_delta < 0.75 @pytest.mark.skipif(av is None, reason="PyAV unavailable") @pytest.mark.parametrize( "video_file", [ "RATRACE_wave_f_nm_np1_fr_goo_37.avi", "TrumanShow_wave_f_nm_np1_fr_med_26.avi", "v_SoccerJuggling_g23_c01.avi", "v_SoccerJuggling_g24_c01.avi", "R6llTwEh07w.mp4", "SOX5yA1l24A.mp4", "WUzgd7C1pWA.mp4", ], ) def test_metadata(self, video_file): full_path = os.path.join(VIDEO_DIR, video_file) decoder = VideoReader(full_path, device="cuda") video_metadata = decoder.get_metadata()["video"] with av.open(full_path) as container: video = container.streams.video[0] av_duration = float(video.duration * video.time_base) assert math.isclose(video_metadata["duration"], av_duration, rel_tol=1e-2) assert math.isclose(video_metadata["fps"], video.base_rate, rel_tol=1e-2) if __name__ == "__main__": pytest.main([__file__])
# Copyright (c) OpenMMLab. All rights reserved. from .base_det_dataset import BaseDetDataset from .base_video_dataset import BaseVideoDataset from .cityscapes import CityscapesDataset from .coco import CocoDataset from .coco_panoptic import CocoPanopticDataset from .crowdhuman import CrowdHumanDataset from .dataset_wrappers import MultiImageMixDataset from .deepfashion import DeepFashionDataset from .dsdl import DSDLDetDataset from .lvis import LVISDataset, LVISV1Dataset, LVISV05Dataset from .mot_challenge_dataset import MOTChallengeDataset from .objects365 import Objects365V1Dataset, Objects365V2Dataset from .openimages import OpenImagesChallengeDataset, OpenImagesDataset from .reid_dataset import ReIDDataset from .samplers import (AspectRatioBatchSampler, ClassAwareSampler, GroupMultiSourceSampler, MultiSourceSampler, TrackImgSampler) from .utils import get_loading_pipeline from .voc import VOCDataset from .wider_face import WIDERFaceDataset from .xml_style import XMLDataset from .youtube_vis_dataset import YouTubeVISDataset __all__ = [ <<<<<<< HEAD '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' ======= 'XMLDataset', 'CocoDataset', 'DeepFashionDataset', 'VOCDataset', 'CityscapesDataset', 'LVISDataset', 'LVISV05Dataset', 'LVISV1Dataset', 'WIDERFaceDataset', 'get_loading_pipeline', 'CocoPanopticDataset', 'MultiImageMixDataset', 'OpenImagesDataset', 'OpenImagesChallengeDataset', 'AspectRatioBatchSampler', 'ClassAwareSampler', 'MultiSourceSampler', 'GroupMultiSourceSampler', 'BaseDetDataset', 'CrowdHumanDataset', 'Objects365V1Dataset', 'Objects365V2Dataset', 'BaseVideoDataset', 'MOTChallengeDataset', 'TrackImgSampler', 'ReIDDataset', 'YouTubeVISDataset' >>>>>>> [Feature] support mask2former for vis (#10245) ]
# Copyright (c) OpenMMLab. All rights reserved. from .base_det_dataset import BaseDetDataset from .base_video_dataset import BaseVideoDataset from .cityscapes import CityscapesDataset from .coco import CocoDataset from .coco_panoptic import CocoPanopticDataset from .crowdhuman import CrowdHumanDataset from .dataset_wrappers import MultiImageMixDataset from .deepfashion import DeepFashionDataset from .dsdl import DSDLDetDataset from .lvis import LVISDataset, LVISV1Dataset, LVISV05Dataset from .mot_challenge_dataset import MOTChallengeDataset from .objects365 import Objects365V1Dataset, Objects365V2Dataset from .openimages import OpenImagesChallengeDataset, OpenImagesDataset from .reid_dataset import ReIDDataset from .samplers import (AspectRatioBatchSampler, ClassAwareSampler, GroupMultiSourceSampler, MultiSourceSampler, TrackImgSampler) from .utils import get_loading_pipeline from .voc import VOCDataset from .wider_face import WIDERFaceDataset from .xml_style import XMLDataset __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' ]
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from mmengine.config import ConfigDict from mmdet.structures import DetDataSample from mmdet.testing import demo_mm_inputs, get_detector_cfg from mmdet.utils import register_all_modules class TestCornerNet(TestCase): def setUp(self) -> None: register_all_modules() model_cfg = get_detector_cfg( 'cornernet/cornernet_hourglass104_8xb6-210e-mstest_coco.py') backbone = dict( type='ResNet', depth=18, num_stages=4, out_indices=(3, ), norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch') neck = dict( type='FPN', in_channels=[512], out_channels=256, start_level=0, add_extra_convs='on_input', num_outs=1) model_cfg.backbone = ConfigDict(**backbone) model_cfg.neck = ConfigDict(**neck) model_cfg.bbox_head.num_feat_levels = 1 self.model_cfg = model_cfg def test_init(self): model = get_detector_cfg( 'cornernet/cornernet_hourglass104_8xb6-210e-mstest_coco.py') model.backbone.init_cfg = None from mmdet.models import build_detector detector = build_detector(model) self.assertTrue(detector.bbox_head is not None) self.assertTrue(detector.backbone is not None) self.assertTrue(not hasattr(detector, 'neck')) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_loss_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 511, 511], [3, 511, 511]]) data = detector.data_preprocessor(packed_inputs, True) losses = detector.forward(**data, mode='loss') assert isinstance(losses, dict) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_predict_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) data = detector.data_preprocessor(packed_inputs, False) # Test forward test detector.eval() with torch.no_grad(): batch_results = detector.forward(**data, mode='predict') assert len(batch_results) == 2 assert isinstance(batch_results[0], DetDataSample) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_tensor_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) data = detector.data_preprocessor(packed_inputs, False) batch_results = detector.forward(**data, mode='tensor') assert isinstance(batch_results, tuple)
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from mmengine.config import ConfigDict from mmdet.structures import DetDataSample from mmdet.testing import demo_mm_inputs, get_detector_cfg from mmdet.utils import register_all_modules class TestCornerNet(TestCase): def setUp(self) -> None: register_all_modules() model_cfg = get_detector_cfg( 'cornernet/cornernet_hourglass104_mstest_8x6_210e_coco.py') backbone = dict( type='ResNet', depth=18, num_stages=4, out_indices=(3, ), norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch') neck = dict( type='FPN', in_channels=[512], out_channels=256, start_level=0, add_extra_convs='on_input', num_outs=1) model_cfg.backbone = ConfigDict(**backbone) model_cfg.neck = ConfigDict(**neck) model_cfg.bbox_head.num_feat_levels = 1 self.model_cfg = model_cfg def test_init(self): model = get_detector_cfg( 'cornernet/cornernet_hourglass104_mstest_8x6_210e_coco.py') model.backbone.init_cfg = None from mmdet.models import build_detector detector = build_detector(model) self.assertTrue(detector.bbox_head is not None) self.assertTrue(detector.backbone is not None) self.assertTrue(not hasattr(detector, 'neck')) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_loss_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 511, 511], [3, 511, 511]]) data = detector.data_preprocessor(packed_inputs, True) losses = detector.forward(**data, mode='loss') assert isinstance(losses, dict) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_predict_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) data = detector.data_preprocessor(packed_inputs, False) # Test forward test detector.eval() with torch.no_grad(): batch_results = detector.forward(**data, mode='predict') assert len(batch_results) == 2 assert isinstance(batch_results[0], DetDataSample) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_tensor_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) data = detector.data_preprocessor(packed_inputs, False) batch_results = detector.forward(**data, mode='tensor') assert isinstance(batch_results, tuple)
import json import pytest from langchain_core.agents import AgentFinish from langchain_core.exceptions import OutputParserException from langchain_core.messages import AIMessage, SystemMessage from langchain.agents.openai_functions_multi_agent.base import ( _FunctionsAgentAction, _parse_ai_message, ) # Test: _parse_ai_message() function. class TestParseAIMessage: # Test: Pass Non-AIMessage. def test_not_an_ai(self) -> None: err = f"Expected an AI message got {SystemMessage!s}" with pytest.raises(TypeError, match=err): _parse_ai_message(SystemMessage(content="x")) # Test: Model response (not a function call). def test_model_response(self) -> None: msg = AIMessage(content="Model response.") result = _parse_ai_message(msg) assert isinstance(result, AgentFinish) assert result.return_values == {"output": "Model response."} assert result.log == "Model response." # Test: Model response with a function call. def test_func_call(self) -> None: act = json.dumps([{"action_name": "foo", "action": {"param": 42}}]) msg = AIMessage( content="LLM thoughts.", additional_kwargs={ "function_call": {"name": "foo", "arguments": f'{{"actions": {act}}}'}, }, ) result = _parse_ai_message(msg) assert isinstance(result, list) assert len(result) == 1 action = result[0] assert isinstance(action, _FunctionsAgentAction) assert action.tool == "foo" assert action.tool_input == {"param": 42} assert action.log == ( "\nInvoking: `foo` with `{'param': 42}`\nresponded: LLM thoughts.\n\n" ) assert action.message_log == [msg] # Test: Model response with a function call (old style tools). def test_func_call_oldstyle(self) -> None: act = json.dumps([{"action_name": "foo", "action": {"__arg1": "42"}}]) msg = AIMessage( content="LLM thoughts.", additional_kwargs={ "function_call": {"name": "foo", "arguments": f'{{"actions": {act}}}'}, }, ) result = _parse_ai_message(msg) assert isinstance(result, list) assert len(result) == 1 action = result[0] assert isinstance(action, _FunctionsAgentAction) assert action.tool == "foo" assert action.tool_input == "42" assert action.log == ( "\nInvoking: `foo` with `42`\nresponded: LLM thoughts.\n\n" ) assert action.message_log == [msg] # Test: Invalid function call args. def test_func_call_invalid(self) -> None: msg = AIMessage( content="LLM thoughts.", additional_kwargs={"function_call": {"name": "foo", "arguments": "{42]"}}, ) err = ( "Could not parse tool input: {'name': 'foo', 'arguments': '{42]'} " "because the `arguments` is not valid JSON." ) with pytest.raises(OutputParserException, match=err): _parse_ai_message(msg)
import json import pytest from langchain_core.agents import AgentFinish from langchain_core.exceptions import OutputParserException from langchain_core.messages import AIMessage, SystemMessage from langchain.agents.openai_functions_multi_agent.base import ( _FunctionsAgentAction, _parse_ai_message, ) # Test: _parse_ai_message() function. class TestParseAIMessage: # Test: Pass Non-AIMessage. def test_not_an_ai(self) -> None: err = f"Expected an AI message got {SystemMessage!s}" with pytest.raises(TypeError, match=err): _parse_ai_message(SystemMessage(content="x")) # Test: Model response (not a function call). def test_model_response(self) -> None: msg = AIMessage(content="Model response.") result = _parse_ai_message(msg) assert isinstance(result, AgentFinish) assert result.return_values == {"output": "Model response."} assert result.log == "Model response." # Test: Model response with a function call. def test_func_call(self) -> None: act = json.dumps([{"action_name": "foo", "action": {"param": 42}}]) msg = AIMessage( content="LLM thoughts.", additional_kwargs={ "function_call": {"name": "foo", "arguments": f'{{"actions": {act}}}'} }, ) result = _parse_ai_message(msg) assert isinstance(result, list) assert len(result) == 1 action = result[0] assert isinstance(action, _FunctionsAgentAction) assert action.tool == "foo" assert action.tool_input == {"param": 42} assert action.log == ( "\nInvoking: `foo` with `{'param': 42}`\nresponded: LLM thoughts.\n\n" ) assert action.message_log == [msg] # Test: Model response with a function call (old style tools). def test_func_call_oldstyle(self) -> None: act = json.dumps([{"action_name": "foo", "action": {"__arg1": "42"}}]) msg = AIMessage( content="LLM thoughts.", additional_kwargs={ "function_call": {"name": "foo", "arguments": f'{{"actions": {act}}}'} }, ) result = _parse_ai_message(msg) assert isinstance(result, list) assert len(result) == 1 action = result[0] assert isinstance(action, _FunctionsAgentAction) assert action.tool == "foo" assert action.tool_input == "42" assert action.log == ( "\nInvoking: `foo` with `42`\nresponded: LLM thoughts.\n\n" ) assert action.message_log == [msg] # Test: Invalid function call args. def test_func_call_invalid(self) -> None: msg = AIMessage( content="LLM thoughts.", additional_kwargs={"function_call": {"name": "foo", "arguments": "{42]"}}, ) err = ( "Could not parse tool input: {'name': 'foo', 'arguments': '{42]'} " "because the `arguments` is not valid JSON." ) with pytest.raises(OutputParserException, match=err): _parse_ai_message(msg)
"""The k-nearest neighbors algorithms.""" # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause from ._ball_tree import BallTree from ._base import VALID_METRICS, VALID_METRICS_SPARSE, sort_graph_by_row_values from ._classification import KNeighborsClassifier, RadiusNeighborsClassifier from ._graph import ( KNeighborsTransformer, RadiusNeighborsTransformer, kneighbors_graph, radius_neighbors_graph, ) from ._kd_tree import KDTree from ._kde import KernelDensity from ._lof import LocalOutlierFactor from ._nca import NeighborhoodComponentsAnalysis from ._nearest_centroid import NearestCentroid from ._regression import KNeighborsRegressor, RadiusNeighborsRegressor from ._unsupervised import NearestNeighbors __all__ = [ "VALID_METRICS", "VALID_METRICS_SPARSE", "BallTree", "KDTree", "KNeighborsClassifier", "KNeighborsRegressor", "KNeighborsTransformer", "KernelDensity", "LocalOutlierFactor", "NearestCentroid", "NearestNeighbors", "NeighborhoodComponentsAnalysis", "RadiusNeighborsClassifier", "RadiusNeighborsRegressor", "RadiusNeighborsTransformer", "kneighbors_graph", "radius_neighbors_graph", "sort_graph_by_row_values", ]
"""The k-nearest neighbors algorithms.""" # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause from ._ball_tree import BallTree from ._base import VALID_METRICS, VALID_METRICS_SPARSE, sort_graph_by_row_values from ._classification import KNeighborsClassifier, RadiusNeighborsClassifier from ._graph import ( KNeighborsTransformer, RadiusNeighborsTransformer, kneighbors_graph, radius_neighbors_graph, ) from ._kd_tree import KDTree from ._kde import KernelDensity from ._lof import LocalOutlierFactor from ._nca import NeighborhoodComponentsAnalysis from ._nearest_centroid import NearestCentroid from ._regression import KNeighborsRegressor, RadiusNeighborsRegressor from ._unsupervised import NearestNeighbors __all__ = [ "BallTree", "KDTree", "KNeighborsClassifier", "KNeighborsRegressor", "KNeighborsTransformer", "NearestCentroid", "NearestNeighbors", "RadiusNeighborsClassifier", "RadiusNeighborsRegressor", "RadiusNeighborsTransformer", "kneighbors_graph", "radius_neighbors_graph", "KernelDensity", "LocalOutlierFactor", "NeighborhoodComponentsAnalysis", "sort_graph_by_row_values", "VALID_METRICS", "VALID_METRICS_SPARSE", ]
import jax import jax.numpy as jnp import jax.scipy as jsp from keras.src.backend import config from keras.src.backend import standardize_dtype from keras.src.backend.common import dtypes from keras.src.backend.jax.core import cast from keras.src.backend.jax.core import convert_to_tensor def cholesky(a): out = jnp.linalg.cholesky(a) try: # In eager mode, raise for nan to # achieve behavior consistency with numpy if jnp.any(jnp.isnan(out)): raise ValueError( "Cholesky decomposition failed. " "The input might not be a valid " "positive definite matrix." ) except jax.errors.TracerBoolConversionError: # Cannot raise for nan in tracing mode pass return out def det(a): return jnp.linalg.det(a) def eig(x): return jnp.linalg.eig(x) def eigh(x): return jnp.linalg.eigh(x) def inv(a): return jnp.linalg.inv(a) def lu_factor(x): lu_factor_fn = jsp.linalg.lu_factor if x.ndim > 2: for i in range(x.ndim - 2): lu_factor_fn = jax.vmap(lu_factor_fn) return lu_factor_fn(x) def norm(x, ord=None, axis=None, keepdims=False): x = convert_to_tensor(x) if standardize_dtype(x.dtype) == "int64": dtype = config.floatx() else: dtype = dtypes.result_type(x.dtype, float) x = cast(x, dtype) return jnp.linalg.norm(x, ord=ord, axis=axis, keepdims=keepdims) def qr(x, mode="reduced"): if mode not in {"reduced", "complete"}: raise ValueError( "`mode` argument value not supported. " "Expected one of {'reduced', 'complete'}. " f"Received: mode={mode}" ) return jnp.linalg.qr(x, mode=mode) def solve(a, b): return jnp.linalg.solve(a, b) def solve_triangular(a, b, lower=False): return jsp.linalg.solve_triangular(a, b, lower=lower) def svd(x, full_matrices=True, compute_uv=True): return jnp.linalg.svd(x, full_matrices=full_matrices, compute_uv=compute_uv)
import jax import jax.numpy as jnp import jax.scipy as jsp from keras.src.backend import config from keras.src.backend import standardize_dtype from keras.src.backend.common import dtypes from keras.src.backend.jax.core import cast from keras.src.backend.jax.core import convert_to_tensor def cholesky(a): out = jnp.linalg.cholesky(a) if jnp.any(jnp.isnan(out)): raise ValueError( "Cholesky decomposition failed. " "The input might not be a valid positive definite matrix." ) return out def det(a): return jnp.linalg.det(a) def eig(x): return jnp.linalg.eig(x) def eigh(x): return jnp.linalg.eigh(x) def inv(a): return jnp.linalg.inv(a) def lu_factor(x): lu_factor_fn = jsp.linalg.lu_factor if x.ndim > 2: for i in range(x.ndim - 2): lu_factor_fn = jax.vmap(lu_factor_fn) return lu_factor_fn(x) def norm(x, ord=None, axis=None, keepdims=False): x = convert_to_tensor(x) if standardize_dtype(x.dtype) == "int64": dtype = config.floatx() else: dtype = dtypes.result_type(x.dtype, float) x = cast(x, dtype) return jnp.linalg.norm(x, ord=ord, axis=axis, keepdims=keepdims) def qr(x, mode="reduced"): if mode not in {"reduced", "complete"}: raise ValueError( "`mode` argument value not supported. " "Expected one of {'reduced', 'complete'}. " f"Received: mode={mode}" ) return jnp.linalg.qr(x, mode=mode) def solve(a, b): return jnp.linalg.solve(a, b) def solve_triangular(a, b, lower=False): return jsp.linalg.solve_triangular(a, b, lower=lower) def svd(x, full_matrices=True, compute_uv=True): return jnp.linalg.svd(x, full_matrices=full_matrices, compute_uv=compute_uv)
""" Top-level module of Jina. The primary function of this module is to import all of the public Jina interfaces into a single place. The interfaces themselves are located in sub-modules, as described below. """ import os as _os import platform as _platform import signal as _signal import sys as _sys import warnings as _warnings import docarray as _docarray if _sys.version_info < (3, 7, 0): raise OSError(f'Jina requires Python >= 3.7, but yours is {_sys.version_info}') def _warning_on_one_line(message, category, filename, lineno, *args, **kwargs): return '\033[1;33m%s: %s\033[0m \033[1;30m(raised from %s:%s)\033[0m\n' % ( category.__name__, message, filename, lineno, ) _warnings.formatwarning = _warning_on_one_line _warnings.simplefilter('always', DeprecationWarning) # fix fork error on MacOS but seems no effect? must do EXPORT manually before jina start _os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES' # JINA_MP_START_METHOD has higher priority than os-patch _start_method = _os.environ.get('JINA_MP_START_METHOD', None) if _start_method and _start_method.lower() in {'fork', 'spawn', 'forkserver'}: from multiprocessing import set_start_method as _set_start_method try: _set_start_method(_start_method.lower()) _warnings.warn( f'multiprocessing start method is set to `{_start_method.lower()}`' ) except Exception as e: _warnings.warn( f'failed to set multiprocessing start_method to `{_start_method.lower()}`: {e!r}' ) elif _sys.version_info >= (3, 8, 0) and _platform.system() == 'Darwin': # DO SOME OS-WISE PATCHES # temporary fix for python 3.8 on macos where the default start is set to "spawn" # https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods from multiprocessing import set_start_method as _set_start_method _set_start_method('fork') # do not change this line manually # this is managed by git tag and updated on every release # NOTE: this represents the NEXT release version __version__ = '3.14.2' # do not change this line manually # this is managed by proto/build-proto.sh and updated on every execution __proto_version__ = '0.1.13' try: __docarray_version__ = _docarray.__version__ except AttributeError as e: raise RuntimeError( '`docarray` dependency is not installed correctly, please reinstall with `pip install -U --force-reinstall docarray`' ) try: _signal.signal(_signal.SIGINT, _signal.default_int_handler) except Exception as exc: _warnings.warn(f'failed to set default signal handler: {exc!r}`') def _set_nofile(nofile_atleast=4096): """ Set nofile soft limit to at least 4096, useful for running matlplotlib/seaborn on parallel executing plot generators vs. Ubuntu default ulimit -n 1024 or OS X El Captian 256 temporary setting extinguishing with Python session. :param nofile_atleast: nofile soft limit :return: nofile soft limit and nofile hard limit """ try: import resource as res except ImportError: # Windows res = None if res is None: return (None,) * 2 soft, ohard = res.getrlimit(res.RLIMIT_NOFILE) hard = ohard if soft < nofile_atleast: soft = nofile_atleast if hard < soft: hard = soft try: res.setrlimit(res.RLIMIT_NOFILE, (soft, hard)) except (ValueError, res.error): try: hard = soft print(f'trouble with max limit, retrying with soft,hard {soft},{hard}') res.setrlimit(res.RLIMIT_NOFILE, (soft, hard)) except Exception: print('failed to set ulimit, giving up') soft, hard = res.getrlimit(res.RLIMIT_NOFILE) return soft, hard _set_nofile() # ONLY FIRST CLASS CITIZENS ARE ALLOWED HERE, namely Document, Executor Flow # Document from jina._docarray import Document, DocumentArray # Client from jina.clients import Client # Deployment from jina.orchestrate.deployments import Deployment from jina.orchestrate.flow.asyncio import AsyncFlow # Flow from jina.orchestrate.flow.base import Flow # Executor from jina.serve.executors import BaseExecutor as Executor from jina.serve.executors.decorators import dynamic_batching, monitor, requests # Custom Gateway from jina.serve.gateway import BaseGateway as Gateway
""" Top-level module of Jina. The primary function of this module is to import all of the public Jina interfaces into a single place. The interfaces themselves are located in sub-modules, as described below. """ import os as _os import platform as _platform import signal as _signal import sys as _sys import warnings as _warnings import docarray as _docarray if _sys.version_info < (3, 7, 0): raise OSError(f'Jina requires Python >= 3.7, but yours is {_sys.version_info}') def _warning_on_one_line(message, category, filename, lineno, *args, **kwargs): return '\033[1;33m%s: %s\033[0m \033[1;30m(raised from %s:%s)\033[0m\n' % ( category.__name__, message, filename, lineno, ) _warnings.formatwarning = _warning_on_one_line _warnings.simplefilter('always', DeprecationWarning) # fix fork error on MacOS but seems no effect? must do EXPORT manually before jina start _os.environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES' # JINA_MP_START_METHOD has higher priority than os-patch _start_method = _os.environ.get('JINA_MP_START_METHOD', None) if _start_method and _start_method.lower() in {'fork', 'spawn', 'forkserver'}: from multiprocessing import set_start_method as _set_start_method try: _set_start_method(_start_method.lower()) _warnings.warn( f'multiprocessing start method is set to `{_start_method.lower()}`' ) except Exception as e: _warnings.warn( f'failed to set multiprocessing start_method to `{_start_method.lower()}`: {e!r}' ) elif _sys.version_info >= (3, 8, 0) and _platform.system() == 'Darwin': # DO SOME OS-WISE PATCHES # temporary fix for python 3.8 on macos where the default start is set to "spawn" # https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods from multiprocessing import set_start_method as _set_start_method _set_start_method('fork') # do not change this line manually # this is managed by git tag and updated on every release # NOTE: this represents the NEXT release version __version__ = '3.14.1' # do not change this line manually # this is managed by proto/build-proto.sh and updated on every execution __proto_version__ = '0.1.13' try: __docarray_version__ = _docarray.__version__ except AttributeError as e: raise RuntimeError( '`docarray` dependency is not installed correctly, please reinstall with `pip install -U --force-reinstall docarray`' ) try: _signal.signal(_signal.SIGINT, _signal.default_int_handler) except Exception as exc: _warnings.warn(f'failed to set default signal handler: {exc!r}`') def _set_nofile(nofile_atleast=4096): """ Set nofile soft limit to at least 4096, useful for running matlplotlib/seaborn on parallel executing plot generators vs. Ubuntu default ulimit -n 1024 or OS X El Captian 256 temporary setting extinguishing with Python session. :param nofile_atleast: nofile soft limit :return: nofile soft limit and nofile hard limit """ try: import resource as res except ImportError: # Windows res = None if res is None: return (None,) * 2 soft, ohard = res.getrlimit(res.RLIMIT_NOFILE) hard = ohard if soft < nofile_atleast: soft = nofile_atleast if hard < soft: hard = soft try: res.setrlimit(res.RLIMIT_NOFILE, (soft, hard)) except (ValueError, res.error): try: hard = soft print(f'trouble with max limit, retrying with soft,hard {soft},{hard}') res.setrlimit(res.RLIMIT_NOFILE, (soft, hard)) except Exception: print('failed to set ulimit, giving up') soft, hard = res.getrlimit(res.RLIMIT_NOFILE) return soft, hard _set_nofile() # ONLY FIRST CLASS CITIZENS ARE ALLOWED HERE, namely Document, Executor Flow # Document from jina._docarray import Document, DocumentArray # Client from jina.clients import Client # Deployment from jina.orchestrate.deployments import Deployment from jina.orchestrate.flow.asyncio import AsyncFlow # Flow from jina.orchestrate.flow.base import Flow # Executor from jina.serve.executors import BaseExecutor as Executor from jina.serve.executors.decorators import dynamic_batching, monitor, requests # Custom Gateway from jina.serve.gateway import BaseGateway as Gateway
# Copyright (c) OpenMMLab. All rights reserved. from abc import ABCMeta, abstractmethod from mmcv.runner import BaseModule from ..builder import build_shared_head class BaseRoIHead(BaseModule, metaclass=ABCMeta): """Base class for RoIHeads.""" def __init__(self, bbox_roi_extractor=None, bbox_head=None, mask_roi_extractor=None, mask_head=None, shared_head=None, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(BaseRoIHead, self).__init__(init_cfg) self.train_cfg = train_cfg self.test_cfg = test_cfg if shared_head is not None: shared_head.pretrained = pretrained self.shared_head = build_shared_head(shared_head) if bbox_head is not None: self.init_bbox_head(bbox_roi_extractor, bbox_head) if mask_head is not None: self.init_mask_head(mask_roi_extractor, mask_head) self.init_assigner_sampler() @property def with_bbox(self): """bool: whether the RoI head contains a `bbox_head`""" return hasattr(self, 'bbox_head') and self.bbox_head is not None @property def with_mask(self): """bool: whether the RoI head contains a `mask_head`""" return hasattr(self, 'mask_head') and self.mask_head is not None @property def with_shared_head(self): """bool: whether the RoI head contains a `shared_head`""" return hasattr(self, 'shared_head') and self.shared_head is not None @abstractmethod def init_bbox_head(self): """Initialize ``bbox_head``""" pass @abstractmethod def init_mask_head(self): """Initialize ``mask_head``""" pass @abstractmethod def init_assigner_sampler(self): """Initialize assigner and sampler.""" pass @abstractmethod def forward_train(self, x, img_meta, proposal_list, gt_bboxes, gt_labels, gt_bboxes_ignore=None, gt_masks=None, **kwargs): """Forward function during training.""" async def async_simple_test(self, x, proposal_list, img_metas, proposals=None, rescale=False, **kwargs): """Asynchronized test function.""" raise NotImplementedError def simple_test(self, x, proposal_list, img_meta, proposals=None, rescale=False, **kwargs): """Test without augmentation.""" def aug_test(self, x, proposal_list, img_metas, rescale=False, **kwargs): """Test with augmentations. If rescale is False, then returned bboxes and masks will fit the scale of imgs[0]. """
from abc import ABCMeta, abstractmethod from mmcv.runner import BaseModule from ..builder import build_shared_head class BaseRoIHead(BaseModule, metaclass=ABCMeta): """Base class for RoIHeads.""" def __init__(self, bbox_roi_extractor=None, bbox_head=None, mask_roi_extractor=None, mask_head=None, shared_head=None, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(BaseRoIHead, self).__init__(init_cfg) self.train_cfg = train_cfg self.test_cfg = test_cfg if shared_head is not None: shared_head.pretrained = pretrained self.shared_head = build_shared_head(shared_head) if bbox_head is not None: self.init_bbox_head(bbox_roi_extractor, bbox_head) if mask_head is not None: self.init_mask_head(mask_roi_extractor, mask_head) self.init_assigner_sampler() @property def with_bbox(self): """bool: whether the RoI head contains a `bbox_head`""" return hasattr(self, 'bbox_head') and self.bbox_head is not None @property def with_mask(self): """bool: whether the RoI head contains a `mask_head`""" return hasattr(self, 'mask_head') and self.mask_head is not None @property def with_shared_head(self): """bool: whether the RoI head contains a `shared_head`""" return hasattr(self, 'shared_head') and self.shared_head is not None @abstractmethod def init_bbox_head(self): """Initialize ``bbox_head``""" pass @abstractmethod def init_mask_head(self): """Initialize ``mask_head``""" pass @abstractmethod def init_assigner_sampler(self): """Initialize assigner and sampler.""" pass @abstractmethod def forward_train(self, x, img_meta, proposal_list, gt_bboxes, gt_labels, gt_bboxes_ignore=None, gt_masks=None, **kwargs): """Forward function during training.""" async def async_simple_test(self, x, proposal_list, img_metas, proposals=None, rescale=False, **kwargs): """Asynchronized test function.""" raise NotImplementedError def simple_test(self, x, proposal_list, img_meta, proposals=None, rescale=False, **kwargs): """Test without augmentation.""" def aug_test(self, x, proposal_list, img_metas, rescale=False, **kwargs): """Test with augmentations. If rescale is False, then returned bboxes and masks will fit the scale of imgs[0]. """
from __future__ import annotations from collections.abc import Iterable import torch import torch.nn as nn from sentence_transformers.sparse_encoder.losses.CSRReconstructionLoss import CSRReconstructionLoss from sentence_transformers.sparse_encoder.losses.SparseMultipleNegativesRankingLoss import ( SparseMultipleNegativesRankingLoss, ) from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class CSRLoss(nn.Module): """ CSR Loss module that combines the CSRReconstruction Loss and Sparse Multiple Negatives Ranking Loss MRL (InfoNCE). Based on the paper: Beyond Matryoshka: Revisiting Sparse Coding for Adaptive Representation, https://arxiv.org/abs/2503.01776 This module computes the combined loss according to the formula: L_CSR = L_recon + γ * L_MRL where: - L_recon = L(k) + L(4k)/8 + β*L_aux - L_MRL is the Multiple Negatives Ranking Loss """ def __init__(self, model: SparseEncoder, beta: float = 0.1, gamma: float = 1.0, scale: float = 20.0): super().__init__() self.model = model self.beta = beta self.gamma = gamma self.scale = scale # Initialize the component losses self.reconstruction_loss = CSRReconstructionLoss(model, beta) self.ranking_loss = SparseMultipleNegativesRankingLoss(model, scale) def forward( self, sentence_features: Iterable[dict[str, torch.Tensor]], labels: torch.Tensor = None ) -> dict[str, torch.Tensor]: """ Forward pass of the CSR Loss module. This method is used when the loss is computed as part of the model's forward pass. Args: sentence_features: Iterable of dictionaries containing sentence embeddings labels: Optional tensor of labels (not used in this implementation) Returns: Dictionary containing the total loss and individual loss components """ # Compute embeddings using the model outputs = [self.model(sentence_feature) for sentence_feature in sentence_features] sentence_embedding = [output["sentence_embedding"] for output in outputs] recon_loss = self.reconstruction_loss.compute_loss_from_embeddings(outputs) ranking_loss = self.ranking_loss.compute_loss_from_embeddings(sentence_embedding, labels) # Compute total loss: L_CSR = L_recon + γ * L_MRL total_loss = recon_loss + self.gamma * ranking_loss return total_loss def get_config_dict(self): """ Get the configuration dictionary. Returns: Dictionary containing the configuration parameters """ return {"beta": self.beta, "gamma": self.gamma, "scale": self.scale} @property def citation(self) -> str: return """ @misc{wen2025matryoshkarevisitingsparsecoding, title={Beyond Matryoshka: Revisiting Sparse Coding for Adaptive Representation}, author={Tiansheng Wen and Yifei Wang and Zequn Zeng and Zhong Peng and Yudi Su and Xinyang Liu and Bo Chen and Hongwei Liu and Stefanie Jegelka and Chenyu You}, year={2025}, eprint={2503.01776}, archivePrefix={arXiv}, primaryClass={cs.LG}, url={https://arxiv.org/abs/2503.01776}, } """
from __future__ import annotations from collections.abc import Iterable import torch import torch.nn as nn from sentence_transformers.sparse_encoder.losses.CSRReconstructionLoss import CSRReconstructionLoss from sentence_transformers.sparse_encoder.losses.SparseMultipleNegativesRankingLoss import ( SparseMultipleNegativesRankingLoss, ) from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class CSRLoss(nn.Module): """ CSR Loss module that combines the CSRReconstruction Loss and Sparse Multiple Negatives Ranking Loss MRL (InfoNCE). Based on the paper: Beyond Matryoshka: Revisiting Sparse Coding for Adaptive Representation, https://arxiv.org/abs/2503.01776 This module computes the combined loss according to the formula: L_CSR = L_recon + γ * L_MRL where: - L_recon = L(k) + L(4k)/8 + β*L_aux - L_MRL is the Multiple Negatives Ranking Loss """ def __init__(self, model: SparseEncoder, beta: float = 0.1, gamma: float = 1.0, scale: float = 20.0): super().__init__() self.model = model self.beta = beta self.gamma = gamma self.scale = scale # Initialize the component losses self.reconstruction_loss = CSRReconstructionLoss(model, beta) self.ranking_loss = SparseMultipleNegativesRankingLoss(model, scale) def forward( self, sentence_features: Iterable[dict[str, torch.Tensor]], labels: torch.Tensor = None ) -> dict[str, torch.Tensor]: """ Forward pass of the CSR Loss module. This method is used when the loss is computed as part of the model's forward pass. Args: sentence_features: Iterable of dictionaries containing sentence embeddings labels: Optional tensor of labels (not used in this implementation) Returns: Dictionary containing the total loss and individual loss components """ # Compute embeddings using the model outputs = [self.model(sentence_feature) for sentence_feature in sentence_features] sentence_embedding = [output["sentence_embedding"] for output in outputs] recon_loss = self.reconstruction_loss.compute_loss_from_embeddings(outputs) ranking_loss = self.ranking_loss.compute_loss_from_embeddings(sentence_embedding) # Compute total loss: L_CSR = L_recon + γ * L_MRL total_loss = recon_loss + self.gamma * ranking_loss return total_loss def get_config_dict(self): """ Get the configuration dictionary. Returns: Dictionary containing the configuration parameters """ return {"beta": self.beta, "gamma": self.gamma, "scale": self.scale}
import os import httpx import pytest from llama_index.core.base.embeddings.base import BaseEmbedding from llama_index.embeddings.cohere import CohereEmbedding def test_embedding_class(): emb = CohereEmbedding(api_key="token") assert isinstance(emb, BaseEmbedding) @pytest.mark.skipif( os.environ.get("CO_API_KEY") is None, reason="Cohere API key required" ) def test_sync_embedding(): emb = CohereEmbedding( api_key=os.environ["CO_API_KEY"], model_name="embed-english-v3.0", input_type="clustering", embedding_type="float", httpx_client=httpx.Client(), ) emb.get_query_embedding("I love Cohere!") @pytest.mark.skipif( os.environ.get("CO_API_KEY") is None, reason="Cohere API key required" ) @pytest.mark.asyncio async def test_async_embedding(): emb = CohereEmbedding( api_key=os.environ["CO_API_KEY"], model_name="embed-english-v3.0", input_type="clustering", embedding_type="float", httpx_async_client=httpx.AsyncClient(), ) await emb.aget_query_embedding("I love Cohere!") def test_cohere_embeddings_custom_endpoint_multiprocessing(): """ When used in multiprocessing, the CohereEmbedding instance will be serialized and deserialized. This test verifies, that custom base_url's are retained in the spawned processes. """ # Arrange: Create a CohereEmbeddings instance with a custom base_url custom_base_url = "test_endpoint" api_key = "test_api_key" embeddings = CohereEmbedding(api_key=api_key, base_url=custom_base_url) # Act: Simulate serialization and deserialization serialized_data = embeddings.__getstate__() deserialized_embeddings = CohereEmbedding.__new__(CohereEmbedding) deserialized_embeddings.__setstate__(serialized_data) # Assert: Verify that the deserialized instance retains the correct base_url assert deserialized_embeddings.base_url == custom_base_url
import os import httpx import pytest from llama_index.core.base.embeddings.base import BaseEmbedding from llama_index.embeddings.cohere import CohereEmbedding def test_embedding_class(): emb = CohereEmbedding(api_key="token") assert isinstance(emb, BaseEmbedding) @pytest.mark.skipif( os.environ.get("CO_API_KEY") is None, reason="Cohere API key required" ) def test_sync_embedding(): emb = CohereEmbedding( api_key=os.environ["CO_API_KEY"], model_name="embed-english-v3.0", input_type="clustering", embedding_type="float", httpx_client=httpx.Client(), ) emb.get_query_embedding("I love Cohere!") @pytest.mark.skipif( os.environ.get("CO_API_KEY") is None, reason="Cohere API key required" ) @pytest.mark.asyncio() async def test_async_embedding(): emb = CohereEmbedding( api_key=os.environ["CO_API_KEY"], model_name="embed-english-v3.0", input_type="clustering", embedding_type="float", httpx_async_client=httpx.AsyncClient(), ) await emb.aget_query_embedding("I love Cohere!") def test_cohere_embeddings_custom_endpoint_multiprocessing(): """When used in multiprocessing, the CohereEmbedding instance will be serialized and deserialized. This test verifies, that custom base_url's are retained in the spawned processes. """ # Arrange: Create a CohereEmbeddings instance with a custom base_url custom_base_url = "test_endpoint" api_key = "test_api_key" embeddings = CohereEmbedding(api_key=api_key, base_url=custom_base_url) # Act: Simulate serialization and deserialization serialized_data = embeddings.__getstate__() deserialized_embeddings = CohereEmbedding.__new__(CohereEmbedding) deserialized_embeddings.__setstate__(serialized_data) # Assert: Verify that the deserialized instance retains the correct base_url assert deserialized_embeddings.base_url == custom_base_url
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from mmengine.config import ConfigDict from mmdet.core import DetDataSample from mmdet.testing import demo_mm_inputs, get_detector_cfg from mmdet.utils import register_all_modules class TestCornerNet(TestCase): def setUp(self) -> None: register_all_modules() model_cfg = get_detector_cfg( 'cornernet/cornernet_hourglass104_mstest_8x6_210e_coco.py') backbone = dict( type='ResNet', depth=18, num_stages=4, out_indices=(3, ), norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch') neck = dict( type='FPN', in_channels=[512], out_channels=256, start_level=0, add_extra_convs='on_input', num_outs=1) model_cfg.backbone = ConfigDict(**backbone) model_cfg.neck = ConfigDict(**neck) model_cfg.bbox_head.num_feat_levels = 1 self.model_cfg = model_cfg def test_init(self): model = get_detector_cfg( 'cornernet/cornernet_hourglass104_mstest_8x6_210e_coco.py') model.backbone.init_cfg = None from mmdet.models import build_detector detector = build_detector(model) self.assertTrue(detector.bbox_head is not None) self.assertTrue(detector.backbone is not None) self.assertTrue(not hasattr(detector, 'neck')) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_loss_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 511, 511], [3, 511, 511]]) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, True) losses = detector.forward(batch_inputs, data_samples, mode='loss') assert isinstance(losses, dict) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_predict_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) # Test forward test detector.eval() with torch.no_grad(): batch_results = detector.forward( batch_inputs, data_samples, mode='predict') assert len(batch_results) == 2 assert isinstance(batch_results[0], DetDataSample) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_tensor_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) batch_results = detector.forward( batch_inputs, data_samples, mode='tensor') assert isinstance(batch_results, tuple)
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from mmengine.config import ConfigDict from mmdet.core import DetDataSample from mmdet.testing import demo_mm_inputs, get_detector_cfg from mmdet.utils import register_all_modules register_all_modules() class TestCornerNet(TestCase): def setUp(self) -> None: model_cfg = get_detector_cfg( 'cornernet/cornernet_hourglass104_mstest_8x6_210e_coco.py') backbone = dict( type='ResNet', depth=18, num_stages=4, out_indices=(3, ), norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch') neck = dict( type='FPN', in_channels=[512], out_channels=256, start_level=0, add_extra_convs='on_input', num_outs=1) model_cfg.backbone = ConfigDict(**backbone) model_cfg.neck = ConfigDict(**neck) model_cfg.bbox_head.num_feat_levels = 1 self.model_cfg = model_cfg def test_init(self): model = get_detector_cfg( 'cornernet/cornernet_hourglass104_mstest_8x6_210e_coco.py') model.backbone.init_cfg = None from mmdet.models import build_detector detector = build_detector(model) self.assertTrue(detector.bbox_head is not None) self.assertTrue(detector.backbone is not None) self.assertTrue(not hasattr(detector, 'neck')) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_loss_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 511, 511], [3, 511, 511]]) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, True) losses = detector.forward(batch_inputs, data_samples, mode='loss') assert isinstance(losses, dict) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_predict_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) # Test forward test detector.eval() with torch.no_grad(): batch_results = detector.forward( batch_inputs, data_samples, mode='predict') assert len(batch_results) == 2 assert isinstance(batch_results[0], DetDataSample) @unittest.skipIf(not torch.cuda.is_available(), 'test requires GPU and torch+cuda') def test_cornernet_forward_tensor_mode(self): from mmdet.models import build_detector detector = build_detector(self.model_cfg) detector.init_weights() packed_inputs = demo_mm_inputs(2, [[3, 512, 512], [3, 512, 512]]) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) batch_results = detector.forward( batch_inputs, data_samples, mode='tensor') assert isinstance(batch_results, tuple)
""" This file loads sentences from a provided text file. It is expected, that the there is one sentence per line in that text file. CT will be training using these sentences. Checkpoints are stored every 500 steps to the output folder. Usage: python train_ct_from_file.py path/to/sentences.txt """ import gzip import logging import math import sys from datetime import datetime import tqdm from sentence_transformers import LoggingHandler, SentenceTransformer, 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 ## Training parameters model_name = "distilbert-base-uncased" batch_size = 16 pos_neg_ratio = 8 # batch_size must be devisible by pos_neg_ratio num_epochs = 1 max_seq_length = 75 # 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_ct{}-{}".format(output_name, datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) # Use Huggingface/transformers model (like BERT, RoBERTa, XLNet, XLM-R) for mapping tokens to embeddings word_embedding_model = models.Transformer(model_name, max_seq_length=max_seq_length) # Apply mean pooling to get one fixed sized sentence vector pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension()) model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) ################# 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))) # For ContrastiveTension we need a special data loader to construct batches with the desired properties train_dataloader = losses.ContrastiveTensionDataLoader( train_sentences, batch_size=batch_size, pos_neg_ratio=pos_neg_ratio ) # As loss, we losses.ContrastiveTensionLoss train_loss = losses.ContrastiveTensionLoss(model) warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) # 10% of train data for warm-up logging.info("Warmup-steps: {}".format(warmup_steps)) # Train the model model.fit( train_objectives=[(train_dataloader, train_loss)], epochs=num_epochs, warmup_steps=warmup_steps, optimizer_params={"lr": 5e-5}, checkpoint_path=model_output_path, show_progress_bar=True, 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. CT will be training using these sentences. Checkpoints are stored every 500 steps to the output folder. Usage: python train_ct_from_file.py path/to/sentences.txt """ import math from sentence_transformers import models, losses from sentence_transformers import LoggingHandler, SentenceTransformer import logging from datetime import datetime import gzip 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 ## Training parameters model_name = "distilbert-base-uncased" batch_size = 16 pos_neg_ratio = 8 # batch_size must be devisible by pos_neg_ratio num_epochs = 1 max_seq_length = 75 # 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_ct{}-{}".format(output_name, datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) # Use Huggingface/transformers model (like BERT, RoBERTa, XLNet, XLM-R) for mapping tokens to embeddings word_embedding_model = models.Transformer(model_name, max_seq_length=max_seq_length) # Apply mean pooling to get one fixed sized sentence vector pooling_model = models.Pooling(word_embedding_model.get_word_embedding_dimension()) model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) ################# 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))) # For ContrastiveTension we need a special data loader to construct batches with the desired properties train_dataloader = losses.ContrastiveTensionDataLoader( train_sentences, batch_size=batch_size, pos_neg_ratio=pos_neg_ratio ) # As loss, we losses.ContrastiveTensionLoss train_loss = losses.ContrastiveTensionLoss(model) warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) # 10% of train data for warm-up logging.info("Warmup-steps: {}".format(warmup_steps)) # Train the model model.fit( train_objectives=[(train_dataloader, train_loss)], epochs=num_epochs, warmup_steps=warmup_steps, optimizer_params={"lr": 5e-5}, checkpoint_path=model_output_path, show_progress_bar=True, use_amp=False, # Set to True, if your GPU supports FP16 cores )
"""Standard LangChain interface tests""" from typing import Optional, Type import pytest from langchain_core.language_models import BaseChatModel from langchain_core.rate_limiters import InMemoryRateLimiter from langchain_core.tools import BaseTool from langchain_tests.integration_tests import ( ChatModelIntegrationTests, ) from langchain_groq import ChatGroq rate_limiter = InMemoryRateLimiter(requests_per_second=0.2) class BaseTestGroq(ChatModelIntegrationTests): @property def chat_model_class(self) -> Type[BaseChatModel]: return ChatGroq @pytest.mark.xfail(reason="Not yet implemented.") def test_tool_message_histories_list_content( self, model: BaseChatModel, my_adder_tool: BaseTool ) -> None: super().test_tool_message_histories_list_content(model, my_adder_tool) @property def supports_json_mode(self) -> bool: return True class TestGroqLlama(BaseTestGroq): @property def chat_model_params(self) -> dict: return { "model": "llama-3.1-8b-instant", "temperature": 0, "rate_limiter": rate_limiter, } @property def tool_choice_value(self) -> Optional[str]: """Value to use for tool choice when used in tests.""" return "any" @property def supports_json_mode(self) -> bool: return True
"""Standard LangChain interface tests""" from typing import Optional, Type import pytest from langchain_core.language_models import BaseChatModel from langchain_core.rate_limiters import InMemoryRateLimiter from langchain_core.tools import BaseTool from langchain_tests.integration_tests import ( ChatModelIntegrationTests, ) from langchain_groq import ChatGroq rate_limiter = InMemoryRateLimiter(requests_per_second=0.2) class BaseTestGroq(ChatModelIntegrationTests): @property def chat_model_class(self) -> Type[BaseChatModel]: return ChatGroq @pytest.mark.xfail(reason="Not yet implemented.") def test_tool_message_histories_list_content( self, model: BaseChatModel, my_adder_tool: BaseTool ) -> None: super().test_tool_message_histories_list_content(model, my_adder_tool) @property def supports_json_mode(self) -> bool: return True class TestGroqLlama(BaseTestGroq): @property def chat_model_params(self) -> dict: return { "model": "llama-3.1-8b-instant", "temperature": 0, "rate_limiter": rate_limiter, } @property def tool_choice_value(self) -> Optional[str]: """Value to use for tool choice when used in tests.""" return "any" @property def supports_json_mode(self) -> bool: return False # Not supported in streaming mode
import logging import os from langchain_qdrant.qdrant import RetrievalMode from tests.integration_tests.common import qdrant_running_locally logger = logging.getLogger(__name__) def qdrant_locations(use_in_memory: bool = True) -> list[str]: locations = [] if use_in_memory: logger.info("Running Qdrant tests with in-memory mode.") locations.append(":memory:") if qdrant_running_locally(): logger.info("Running Qdrant tests with local Qdrant instance.") locations.append("http://localhost:6333") if qdrant_url := os.getenv("QDRANT_URL"): logger.info(f"Running Qdrant tests with Qdrant instance at {qdrant_url}.") locations.append(qdrant_url) return locations def retrieval_modes( *, dense: bool = True, sparse: bool = True, hybrid: bool = True ) -> list[RetrievalMode]: modes = [] if dense: modes.append(RetrievalMode.DENSE) if sparse: modes.append(RetrievalMode.SPARSE) if hybrid: modes.append(RetrievalMode.HYBRID) return modes
import logging import os from typing import List from langchain_qdrant.qdrant import RetrievalMode from tests.integration_tests.common import qdrant_running_locally logger = logging.getLogger(__name__) def qdrant_locations(use_in_memory: bool = True) -> List[str]: locations = [] if use_in_memory: logger.info("Running Qdrant tests with in-memory mode.") locations.append(":memory:") if qdrant_running_locally(): logger.info("Running Qdrant tests with local Qdrant instance.") locations.append("http://localhost:6333") if qdrant_url := os.getenv("QDRANT_URL"): logger.info(f"Running Qdrant tests with Qdrant instance at {qdrant_url}.") locations.append(qdrant_url) return locations def retrieval_modes( *, dense: bool = True, sparse: bool = True, hybrid: bool = True ) -> List[RetrievalMode]: modes = [] if dense: modes.append(RetrievalMode.DENSE) if sparse: modes.append(RetrievalMode.SPARSE) if hybrid: modes.append(RetrievalMode.HYBRID) return modes
from typing import Optional import numpy as np import pytest import torch from docarray import BaseDoc, DocList, DocVec from docarray.documents import ImageDoc from docarray.typing import NdArray, TorchTensor class MyDoc(BaseDoc): embedding: NdArray text: str image: ImageDoc def test_from_to_json_doclist(): da = DocList[MyDoc]( [ MyDoc( embedding=[1, 2, 3, 4, 5], text='hello', image=ImageDoc(url='aux.png') ), MyDoc(embedding=[5, 4, 3, 2, 1], text='hello world', image=ImageDoc()), ] ) json_da = da.to_json() da2 = DocList[MyDoc].from_json(json_da) assert len(da2) == 2 assert len(da) == len(da2) for d1, d2 in zip(da, da2): assert d1.embedding.tolist() == d2.embedding.tolist() assert d1.text == d2.text assert d1.image.url == d2.image.url assert da[1].image.url is None assert da2[1].image.url is None @pytest.mark.parametrize('tensor_type', [TorchTensor, NdArray]) def test_from_to_json_docvec(tensor_type): def generate_docs(tensor_type): class InnerDoc(BaseDoc): tens: tensor_type class MyDoc(BaseDoc): text: str num: Optional[int] tens: tensor_type tens_none: Optional[tensor_type] inner: InnerDoc inner_none: Optional[InnerDoc] inner_vec: DocVec[InnerDoc] inner_vec_none: Optional[DocVec[InnerDoc]] def _rand_vec_gen(tensor_type): arr = np.random.rand(5) if tensor_type == TorchTensor: arr = torch.from_numpy(arr).to(torch.float32) return arr inner = InnerDoc(tens=_rand_vec_gen(tensor_type)) inner_vec = DocVec[InnerDoc]([inner, inner], tensor_type=tensor_type) vec = DocVec[MyDoc]( [ MyDoc( text=str(i), num=None, tens=_rand_vec_gen(tensor_type), inner=inner, inner_none=None, inner_vec=inner_vec, inner_vec_none=None, ) for i in range(5) ], tensor_type=tensor_type, ) return vec v = generate_docs(tensor_type) bytes_ = v.to_json() v_after = DocVec[v.doc_type].from_json(bytes_, tensor_type=tensor_type) assert v_after.tensor_type == v.tensor_type assert set(v_after._storage.columns.keys()) == set(v._storage.columns.keys()) assert v_after._storage == v._storage @pytest.mark.tensorflow def test_from_to_json_docvec_tf(): from docarray.typing import TensorFlowTensor def generate_docs(): class InnerDoc(BaseDoc): tens: TensorFlowTensor class MyDoc(BaseDoc): text: str num: Optional[int] tens: TensorFlowTensor tens_none: Optional[TensorFlowTensor] inner: InnerDoc inner_none: Optional[InnerDoc] inner_vec: DocVec[InnerDoc] inner_vec_none: Optional[DocVec[InnerDoc]] inner = InnerDoc(tens=np.random.rand(5)) inner_vec = DocVec[InnerDoc]([inner, inner], tensor_type=TensorFlowTensor) vec = DocVec[MyDoc]( [ MyDoc( text=str(i), num=None, tens=np.random.rand(5), inner=inner, inner_none=None, inner_vec=inner_vec, inner_vec_none=None, ) for i in range(5) ], tensor_type=TensorFlowTensor, ) return vec v = generate_docs() bytes_ = v.to_json() v_after = DocVec[v.doc_type].from_json(bytes_, tensor_type=TensorFlowTensor) assert v_after.tensor_type == v.tensor_type assert set(v_after._storage.columns.keys()) == set(v._storage.columns.keys()) assert v_after._storage == v._storage def test_union_type(): from typing import Union from docarray.documents import TextDoc class CustomDoc(BaseDoc): ud: Union[TextDoc, ImageDoc] = TextDoc(text='union type') docs = DocList[CustomDoc]([CustomDoc(ud=TextDoc(text='union type'))]) docs_copy = docs.from_json(docs.to_json()) assert docs == docs_copy
from docarray import BaseDoc, DocList from docarray.documents import ImageDoc from docarray.typing import NdArray class MyDoc(BaseDoc): embedding: NdArray text: str image: ImageDoc def test_from_to_json(): da = DocList[MyDoc]( [ MyDoc( embedding=[1, 2, 3, 4, 5], text='hello', image=ImageDoc(url='aux.png') ), MyDoc(embedding=[5, 4, 3, 2, 1], text='hello world', image=ImageDoc()), ] ) json_da = da.to_json() da2 = DocList[MyDoc].from_json(json_da) assert len(da2) == 2 assert len(da) == len(da2) for d1, d2 in zip(da, da2): assert d1.embedding.tolist() == d2.embedding.tolist() assert d1.text == d2.text assert d1.image.url == d2.image.url assert da[1].image.url is None assert da2[1].image.url is None def test_union_type(): from typing import Union from docarray.documents import TextDoc class CustomDoc(BaseDoc): ud: Union[TextDoc, ImageDoc] = TextDoc(text='union type') docs = DocList[CustomDoc]([CustomDoc(ud=TextDoc(text='union type'))]) docs_copy = docs.from_json(docs.to_json()) assert docs == docs_copy
from typing import Union import numpy as np import PIL.Image import torch from torchvision import tv_tensors from torchvision.transforms import functional as _F @torch.jit.unused def to_image(inpt: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> tv_tensors.Image: """See :class:`~torchvision.transforms.v2.ToImage` for details.""" if isinstance(inpt, np.ndarray): output = torch.from_numpy(inpt).permute((2, 0, 1)).contiguous() elif isinstance(inpt, PIL.Image.Image): output = pil_to_tensor(inpt) elif isinstance(inpt, torch.Tensor): output = inpt else: raise TypeError( f"Input can either be a pure Tensor, a numpy array, or a PIL image, but got {type(inpt)} instead." ) return tv_tensors.Image(output) to_pil_image = _F.to_pil_image pil_to_tensor = _F.pil_to_tensor
from typing import Union import numpy as np import PIL.Image import torch from torchvision import tv_tensors from torchvision.transforms import functional as _F @torch.jit.unused def to_image(inpt: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> tv_tensors.Image: """[BETA] See :class:`~torchvision.transforms.v2.ToImage` for details.""" if isinstance(inpt, np.ndarray): output = torch.from_numpy(inpt).permute((2, 0, 1)).contiguous() elif isinstance(inpt, PIL.Image.Image): output = pil_to_tensor(inpt) elif isinstance(inpt, torch.Tensor): output = inpt else: raise TypeError( f"Input can either be a pure Tensor, a numpy array, or a PIL image, but got {type(inpt)} instead." ) return tv_tensors.Image(output) to_pil_image = _F.to_pil_image pil_to_tensor = _F.pil_to_tensor
# Copyright (c) OpenMMLab. All rights reserved. from .anchor_free_head import AnchorFreeHead from .anchor_head import AnchorHead from .atss_head import ATSSHead from .autoassign_head import AutoAssignHead from .cascade_rpn_head import CascadeRPNHead, StageCascadeRPNHead from .centernet_head import CenterNetHead from .centripetal_head import CentripetalHead from .corner_head import CornerHead from .deformable_detr_head import DeformableDETRHead from .detr_head import DETRHead from .embedding_rpn_head import EmbeddingRPNHead from .fcos_head import FCOSHead from .fovea_head import FoveaHead from .free_anchor_retina_head import FreeAnchorRetinaHead from .fsaf_head import FSAFHead from .ga_retina_head import GARetinaHead from .ga_rpn_head import GARPNHead from .gfl_head import GFLHead from .guided_anchor_head import FeatureAdaption, GuidedAnchorHead from .lad_head import LADHead from .ld_head import LDHead from .maskformer_head import MaskFormerHead from .nasfcos_head import NASFCOSHead from .paa_head import PAAHead from .pisa_retinanet_head import PISARetinaHead from .pisa_ssd_head import PISASSDHead from .reppoints_head import RepPointsHead from .retina_head import RetinaHead from .retina_sepbn_head import RetinaSepBNHead from .rpn_head import RPNHead from .sabl_retina_head import SABLRetinaHead from .solo_head import DecoupledSOLOHead, DecoupledSOLOLightHead, SOLOHead from .ssd_head import SSDHead from .tood_head import TOODHead from .vfnet_head import VFNetHead from .yolact_head import YOLACTHead, YOLACTProtonet, YOLACTSegmHead from .yolo_head import YOLOV3Head from .yolof_head import YOLOFHead from .yolox_head import YOLOXHead __all__ = [ 'AnchorFreeHead', 'AnchorHead', 'GuidedAnchorHead', 'FeatureAdaption', 'RPNHead', 'GARPNHead', 'RetinaHead', 'RetinaSepBNHead', 'GARetinaHead', 'SSDHead', 'FCOSHead', 'RepPointsHead', 'FoveaHead', 'FreeAnchorRetinaHead', 'ATSSHead', 'FSAFHead', 'NASFCOSHead', 'PISARetinaHead', 'PISASSDHead', 'GFLHead', 'CornerHead', 'YOLACTHead', 'YOLACTSegmHead', 'YOLACTProtonet', 'YOLOV3Head', 'PAAHead', 'SABLRetinaHead', 'CentripetalHead', 'VFNetHead', 'StageCascadeRPNHead', 'CascadeRPNHead', 'EmbeddingRPNHead', 'LDHead', 'CascadeRPNHead', 'AutoAssignHead', 'DETRHead', 'YOLOFHead', 'DeformableDETRHead', 'SOLOHead', 'DecoupledSOLOHead', 'CenterNetHead', 'YOLOXHead', 'DecoupledSOLOLightHead', 'LADHead', 'TOODHead', 'MaskFormerHead' ]
# Copyright (c) OpenMMLab. All rights reserved. from .anchor_free_head import AnchorFreeHead from .anchor_head import AnchorHead from .atss_head import ATSSHead from .autoassign_head import AutoAssignHead from .cascade_rpn_head import CascadeRPNHead, StageCascadeRPNHead from .centernet_head import CenterNetHead from .centripetal_head import CentripetalHead from .corner_head import CornerHead from .deformable_detr_head import DeformableDETRHead from .detr_head import DETRHead from .embedding_rpn_head import EmbeddingRPNHead from .fcos_head import FCOSHead from .fovea_head import FoveaHead from .free_anchor_retina_head import FreeAnchorRetinaHead from .fsaf_head import FSAFHead from .ga_retina_head import GARetinaHead from .ga_rpn_head import GARPNHead from .gfl_head import GFLHead from .guided_anchor_head import FeatureAdaption, GuidedAnchorHead from .lad_head import LADHead from .ld_head import LDHead from .nasfcos_head import NASFCOSHead from .paa_head import PAAHead from .pisa_retinanet_head import PISARetinaHead from .pisa_ssd_head import PISASSDHead from .reppoints_head import RepPointsHead from .retina_head import RetinaHead from .retina_sepbn_head import RetinaSepBNHead from .rpn_head import RPNHead from .sabl_retina_head import SABLRetinaHead from .solo_head import DecoupledSOLOHead, DecoupledSOLOLightHead, SOLOHead from .ssd_head import SSDHead from .tood_head import TOODHead from .vfnet_head import VFNetHead from .yolact_head import YOLACTHead, YOLACTProtonet, YOLACTSegmHead from .yolo_head import YOLOV3Head from .yolof_head import YOLOFHead from .yolox_head import YOLOXHead __all__ = [ 'AnchorFreeHead', 'AnchorHead', 'GuidedAnchorHead', 'FeatureAdaption', 'RPNHead', 'GARPNHead', 'RetinaHead', 'RetinaSepBNHead', 'GARetinaHead', 'SSDHead', 'FCOSHead', 'RepPointsHead', 'FoveaHead', 'FreeAnchorRetinaHead', 'ATSSHead', 'FSAFHead', 'NASFCOSHead', 'PISARetinaHead', 'PISASSDHead', 'GFLHead', 'CornerHead', 'YOLACTHead', 'YOLACTSegmHead', 'YOLACTProtonet', 'YOLOV3Head', 'PAAHead', 'SABLRetinaHead', 'CentripetalHead', 'VFNetHead', 'StageCascadeRPNHead', 'CascadeRPNHead', 'EmbeddingRPNHead', 'LDHead', 'CascadeRPNHead', 'AutoAssignHead', 'DETRHead', 'YOLOFHead', 'DeformableDETRHead', 'SOLOHead', 'DecoupledSOLOHead', 'CenterNetHead', 'YOLOXHead', 'DecoupledSOLOLightHead', 'LADHead', 'TOODHead' ]
"""Base interface class for storing chat history per user.""" import asyncio from abc import abstractmethod from typing import List, Optional from llama_index.core.llms import ChatMessage from llama_index.core.schema import BaseComponent class BaseChatStore(BaseComponent): @classmethod def class_name(cls) -> str: """Get class name.""" return "BaseChatStore" @abstractmethod def set_messages(self, key: str, messages: List[ChatMessage]) -> None: """Set messages for a key.""" ... @abstractmethod def get_messages(self, key: str) -> List[ChatMessage]: """Get messages for a key.""" ... @abstractmethod def add_message(self, key: str, message: ChatMessage) -> None: """Add a message for a key.""" ... @abstractmethod def delete_messages(self, key: str) -> Optional[List[ChatMessage]]: """Delete messages for a key.""" ... @abstractmethod def delete_message(self, key: str, idx: int) -> Optional[ChatMessage]: """Delete specific message for a key.""" ... @abstractmethod def delete_last_message(self, key: str) -> Optional[ChatMessage]: """Delete last message for a key.""" ... @abstractmethod def get_keys(self) -> List[str]: """Get all keys.""" ... async def aset_messages(self, key: str, messages: List[ChatMessage]) -> None: """Async version of Get messages for a key.""" await asyncio.to_thread(self.set_messages, key, messages) async def aget_messages(self, key: str) -> List[ChatMessage]: """Async version of Get messages for a key.""" return await asyncio.to_thread(self.get_messages, key) async def async_add_message(self, key: str, message: ChatMessage) -> None: """Async version of Add a message for a key.""" await asyncio.to_thread(self.add_message, key, message) async def adelete_messages(self, key: str) -> Optional[List[ChatMessage]]: """Async version of Delete messages for a key.""" return await asyncio.to_thread(self.delete_messages, key) async def adelete_message(self, key: str, idx: int) -> Optional[ChatMessage]: """Async version of Delete specific message for a key.""" return await asyncio.to_thread(self.delete_message, key, idx) async def adelete_last_message(self, key: str) -> Optional[ChatMessage]: """Async version of Delete last message for a key.""" return await asyncio.to_thread(self.delete_last_message, key) async def aget_keys(self) -> List[str]: """Async version of Get all keys.""" return await asyncio.to_thread(self.get_keys)
"""Base interface class for storing chat history per user.""" from abc import abstractmethod from typing import List, Optional from llama_index.core.llms import ChatMessage from llama_index.core.schema import BaseComponent class BaseChatStore(BaseComponent): @classmethod def class_name(cls) -> str: """Get class name.""" return "BaseChatStore" @abstractmethod def set_messages(self, key: str, messages: List[ChatMessage]) -> None: """Set messages for a key.""" ... @abstractmethod def get_messages(self, key: str) -> List[ChatMessage]: """Get messages for a key.""" ... @abstractmethod def add_message(self, key: str, message: ChatMessage) -> None: """Add a message for a key.""" ... @abstractmethod def delete_messages(self, key: str) -> Optional[List[ChatMessage]]: """Delete messages for a key.""" ... @abstractmethod def delete_message(self, key: str, idx: int) -> Optional[ChatMessage]: """Delete specific message for a key.""" ... @abstractmethod def delete_last_message(self, key: str) -> Optional[ChatMessage]: """Delete last message for a key.""" ... @abstractmethod def get_keys(self) -> List[str]: """Get all keys.""" ... async def aset_messages(self, key: str, messages: List[ChatMessage]) -> None: """Async version of Get messages for a key.""" self.set_messages(key, messages) async def aget_messages(self, key: str) -> List[ChatMessage]: """Async version of Get messages for a key.""" return self.get_messages(key) async def async_add_message(self, key: str, message: ChatMessage) -> None: """Async version of Add a message for a key.""" self.add_message(key, message) async def adelete_messages(self, key: str) -> Optional[List[ChatMessage]]: """Async version of Delete messages for a key.""" return self.delete_messages(key) async def adelete_message(self, key: str, idx: int) -> Optional[ChatMessage]: """Async version of Delete specific message for a key.""" return self.delete_message(key, idx) async def adelete_last_message(self, key: str) -> Optional[ChatMessage]: """Async version of Delete last message for a key.""" return self.delete_last_message(key) async def aget_keys(self) -> List[str]: """Async version of Get all keys.""" return self.get_keys()
import os import warnings from modulefinder import Module import torch from torchvision import datasets, io, models, ops, transforms, utils from .extension import _HAS_OPS try: from .version import __version__ # noqa: F401 except ImportError: pass # Check if torchvision is being imported within the root folder if not _HAS_OPS and os.path.dirname(os.path.realpath(__file__)) == os.path.join( os.path.realpath(os.getcwd()), "torchvision" ): message = ( "You are importing torchvision within its own root folder ({}). " "This is not expected to work and may give errors. Please exit the " "torchvision project source and relaunch your python interpreter." ) warnings.warn(message.format(os.getcwd())) _image_backend = "PIL" _video_backend = "pyav" def set_image_backend(backend): """ Specifies the package used to load images. Args: backend (string): Name of the image backend. one of {'PIL', 'accimage'}. The :mod:`accimage` package uses the Intel IPP library. It is generally faster than PIL, but does not support as many operations. """ global _image_backend if backend not in ["PIL", "accimage"]: raise ValueError(f"Invalid backend '{backend}'. Options are 'PIL' and 'accimage'") _image_backend = backend def get_image_backend(): """ Gets the name of the package used to load images """ return _image_backend def set_video_backend(backend): """ Specifies the package used to decode videos. Args: backend (string): Name of the video backend. one of {'pyav', 'video_reader'}. The :mod:`pyav` package uses the 3rd party PyAv library. It is a Pythonic binding for the FFmpeg libraries. The :mod:`video_reader` package includes a native C++ implementation on top of FFMPEG libraries, and a python API of TorchScript custom operator. It generally decodes faster than :mod:`pyav`, but is perhaps less robust. .. note:: Building with FFMPEG is disabled by default in the latest `main`. If you want to use the 'video_reader' backend, please compile torchvision from source. """ global _video_backend if backend not in ["pyav", "video_reader", "cuda"]: raise ValueError("Invalid video backend '%s'. Options are 'pyav', 'video_reader' and 'cuda'" % backend) if backend == "video_reader" and not io._HAS_VIDEO_OPT: # TODO: better messages message = "video_reader video backend is not available. Please compile torchvision from source and try again" raise RuntimeError(message) elif backend == "cuda" and not io._HAS_GPU_VIDEO_DECODER: # TODO: better messages message = "cuda video backend is not available." raise RuntimeError(message) else: _video_backend = backend def get_video_backend(): """ Returns the currently active video backend used to decode videos. Returns: str: Name of the video backend. one of {'pyav', 'video_reader'}. """ return _video_backend def _is_tracing(): return torch._C._get_tracing_state() _WARN_ABOUT_BETA_TRANSFORMS = True _BETA_TRANSFORMS_WARNING = ( "The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. " "While we do not expect major breaking changes, some APIs may still change " "according to user feedback. Please submit any feedback you may have in " "this issue: https://github.com/pytorch/vision/issues/6753, and you can also " "check out https://github.com/pytorch/vision/issues/7319 to learn more about " "the APIs that we suspect might involve future changes. " "You can silence this warning by calling torchvision.disable_beta_transform_warning()." ) def disable_beta_transforms_warning(): global _WARN_ABOUT_BETA_TRANSFORMS _WARN_ABOUT_BETA_TRANSFORMS = False
import os import warnings from modulefinder import Module import torch from torchvision import datasets, io, models, ops, transforms, utils from .extension import _HAS_OPS try: from .version import __version__ # noqa: F401 except ImportError: pass # Check if torchvision is being imported within the root folder if not _HAS_OPS and os.path.dirname(os.path.realpath(__file__)) == os.path.join( os.path.realpath(os.getcwd()), "torchvision" ): message = ( "You are importing torchvision within its own root folder ({}). " "This is not expected to work and may give errors. Please exit the " "torchvision project source and relaunch your python interpreter." ) warnings.warn(message.format(os.getcwd())) _image_backend = "PIL" _video_backend = "pyav" def set_image_backend(backend): """ Specifies the package used to load images. Args: backend (string): Name of the image backend. one of {'PIL', 'accimage'}. The :mod:`accimage` package uses the Intel IPP library. It is generally faster than PIL, but does not support as many operations. """ global _image_backend if backend not in ["PIL", "accimage"]: raise ValueError(f"Invalid backend '{backend}'. Options are 'PIL' and 'accimage'") _image_backend = backend def get_image_backend(): """ Gets the name of the package used to load images """ return _image_backend def set_video_backend(backend): """ Specifies the package used to decode videos. Args: backend (string): Name of the video backend. one of {'pyav', 'video_reader'}. The :mod:`pyav` package uses the 3rd party PyAv library. It is a Pythonic binding for the FFmpeg libraries. The :mod:`video_reader` package includes a native C++ implementation on top of FFMPEG libraries, and a python API of TorchScript custom operator. It generally decodes faster than :mod:`pyav`, but is perhaps less robust. .. note:: Building with FFMPEG is disabled by default in the latest `main`. If you want to use the 'video_reader' backend, please compile torchvision from source. """ global _video_backend if backend not in ["pyav", "video_reader", "cuda"]: raise ValueError("Invalid video backend '%s'. Options are 'pyav', 'video_reader' and 'cuda'" % backend) if backend == "video_reader" and not io._HAS_VIDEO_OPT: # TODO: better messages message = "video_reader video backend is not available. Please compile torchvision from source and try again" raise RuntimeError(message) elif backend == "cuda" and not io._HAS_GPU_VIDEO_DECODER: # TODO: better messages message = "cuda video backend is not available." raise RuntimeError(message) else: _video_backend = backend def get_video_backend(): """ Returns the currently active video backend used to decode videos. Returns: str: Name of the video backend. one of {'pyav', 'video_reader'}. """ return _video_backend def _is_tracing(): return torch._C._get_tracing_state() _WARN_ABOUT_BETA_TRANSFORMS = True _BETA_TRANSFORMS_WARNING = ( "The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. " "While we will try our best to maintain backward compatibility, " "some APIs or behaviors might change without a deprecation cycle. " "To help us improve these new features, please provide your feedback " "here: https://github.com/pytorch/vision/issues/6753." "You can silence this warning by calling torchvision.disable_beta_transform_warning()." ) def disable_beta_transforms_warning(): global _WARN_ABOUT_BETA_TRANSFORMS _WARN_ABOUT_BETA_TRANSFORMS = False
import numpy as np from tensorflow import data as tf_data from keras.src import backend from keras.src import layers from keras.src import testing class IntegerLookupTest(testing.TestCase): # TODO: increase coverage. Most features aren't being tested. def test_config(self): layer = layers.IntegerLookup( output_mode="int", vocabulary=[1, 2, 3], oov_token=1, mask_token=0, ) self.run_class_serialization_test(layer) def test_adapt_flow(self): adapt_data = [1, 1, 1, 2, 2, 3] single_sample_input_data = [1, 2, 4] batch_input_data = [[1, 2, 4], [2, 3, 5]] # int mode layer = layers.IntegerLookup( output_mode="int", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([1, 2, 0])) output = layer(batch_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([[1, 2, 0], [2, 3, 0]])) # one_hot mode layer = layers.IntegerLookup( output_mode="one_hot", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose( output, np.array([[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]]) ) # multi_hot mode layer = layers.IntegerLookup( output_mode="multi_hot", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([1, 1, 1, 0])) # tf_idf mode layer = layers.IntegerLookup( output_mode="tf_idf", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose( output, np.array([1.133732, 0.916291, 1.098612, 0.0]) ) # count mode layer = layers.IntegerLookup( output_mode="count", ) layer.adapt(adapt_data) output = layer([1, 2, 3, 4, 1, 2, 1]) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([1, 3, 2, 1])) def test_fixed_vocabulary(self): layer = layers.IntegerLookup( output_mode="int", vocabulary=[1, 2, 3, 4], ) input_data = [2, 3, 4, 5] output = layer(input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([2, 3, 4, 0])) def test_set_vocabulary(self): layer = layers.IntegerLookup( output_mode="int", ) layer.set_vocabulary([1, 2, 3, 4]) input_data = [2, 3, 4, 5] output = layer(input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([2, 3, 4, 0])) def test_tf_data_compatibility(self): layer = layers.IntegerLookup( output_mode="int", vocabulary=[1, 2, 3, 4], ) input_data = [2, 3, 4, 5] ds = tf_data.Dataset.from_tensor_slices(input_data).batch(4).map(layer) output = next(iter(ds)).numpy() self.assertAllClose(output, np.array([2, 3, 4, 0]))
import numpy as np from tensorflow import data as tf_data from keras.src import backend from keras.src import layers from keras.src import testing class IntegerLookupTest(testing.TestCase): # TODO: increase coverage. Most features aren't being tested. def test_config(self): layer = layers.IntegerLookup( output_mode="int", vocabulary=[1, 2, 3], oov_token=1, mask_token=0, ) self.run_class_serialization_test(layer) def test_adapt_flow(self): adapt_data = [1, 1, 1, 2, 2, 3] single_sample_input_data = [1, 2, 4] batch_input_data = [[1, 2, 4], [2, 3, 5]] # int mode layer = layers.IntegerLookup( output_mode="int", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([1, 2, 0])) output = layer(batch_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([[1, 2, 0], [2, 3, 0]])) # one_hot mode layer = layers.IntegerLookup( output_mode="one_hot", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose( output, np.array([[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]]) ) # multi_hot mode layer = layers.IntegerLookup( output_mode="multi_hot", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([1, 1, 1, 0])) # tf_idf mode layer = layers.IntegerLookup( output_mode="tf_idf", ) layer.adapt(adapt_data) output = layer(single_sample_input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose( output, np.array([1.133732, 0.916291, 1.098612, 0.0]) ) # count mode layer = layers.IntegerLookup( output_mode="count", ) layer.adapt(adapt_data) output = layer([1, 2, 3, 4, 1, 2, 1]) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([1, 3, 2, 1])) def test_fixed_vocabulary(self): layer = layers.IntegerLookup( output_mode="int", vocabulary=[1, 2, 3, 4], ) input_data = [2, 3, 4, 5] output = layer(input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([2, 3, 4, 0])) def test_set_vocabulary(self): layer = layers.IntegerLookup( output_mode="int", ) layer.set_vocabulary([1, 2, 3, 4]) input_data = [2, 3, 4, 5] output = layer(input_data) self.assertTrue(backend.is_tensor(output)) self.assertAllClose(output, np.array([2, 3, 4, 0])) def test_tf_data_compatibility(self): layer = layers.IntegerLookup( output_mode="int", vocabulary=[1, 2, 3, 4], ) input_data = [2, 3, 4, 5] ds = tf_data.Dataset.from_tensor_slices(input_data).batch(4).map(layer) for output in ds.take(1): output = output.numpy() self.assertAllClose(output, np.array([2, 3, 4, 0]))
"""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 multimodal AI services via cloud native technologies. {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://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
__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', *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) 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.batch( traversal_paths=parameters.get('traversal_paths', self.traversal_paths), batch_size=parameters.get('batch_size', self.batch_size), require_attr='text', ) 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 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', traversal_paths: Iterable[str] = ('r',), batch_size: int = 32, device: str = 'cpu', *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) 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 = get_docs_batch_generator( docs, traversal_path=parameters.get('traversal_paths', self.traversal_paths), batch_size=parameters.get('batch_size', self.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]
import os from pathlib import Path from typing import List, Tuple, Union from torch import Tensor from torch.hub import download_url_to_file from torch.utils.data import Dataset from torchaudio.datasets.librispeech import load_librispeech_item from torchaudio.datasets.utils import extract_archive _ARCHIVE_NAME = "librispeech_finetuning" _URL = "https://dl.fbaipublicfiles.com/librilight/data/librispeech_finetuning.tgz" _CHECKSUM = "5d1efdc777b548194d7e09ba89126e2188026df9fd57aa57eb14408d2b2342af" def _get_fileids_paths(path, subset, _ext_audio) -> List[Tuple[str, str]]: """Get the file names and the corresponding file paths without `speaker_id` and `chapter_id` directories. The format of path is like: {root}/{_ARCHIVE_NAME}/1h/[0-5]/[clean, other] or {root}/{_ARCHIVE_NAME}/9h/[clean, other] """ if subset == "10min": files_paths = [ (os.path.join(os.path.dirname(p), "..", ".."), str(p.stem)) for p in Path(path).glob("1h/0/*/*/*/*" + _ext_audio) ] elif subset in ["1h", "10h"]: files_paths = [ (os.path.join(os.path.dirname(p), "..", ".."), str(p.stem)) for p in Path(path).glob("1h/*/*/*/*/*" + _ext_audio) ] if subset == "10h": files_paths += [ (os.path.join(os.path.dirname(p), "..", ".."), str(p.stem)) for p in Path(path).glob("9h/*/*/*/*" + _ext_audio) ] else: raise ValueError(f"Unsupported subset value. Found {subset}.") files_paths = sorted(files_paths, key=lambda x: x[0] + x[1]) return files_paths class LibriLightLimited(Dataset): """Create a Dataset for LibriLightLimited, which is the supervised subset of LibriLight dataset. Args: root (str or Path): Path to the directory where the dataset is found or downloaded. subset (str, optional): The subset to use. Options: [``10min``, ``1h``, ``10h``] (Default: ``10min``). download (bool, optional): Whether to download the dataset if it is not found at root path. (default: ``False``). """ _ext_txt = ".trans.txt" _ext_audio = ".flac" def __init__( self, root: Union[str, Path], subset: str = "10min", download: bool = False, ) -> None: if subset not in ["10min", "1h", "10h"]: raise ValueError("`subset` must be one of ['10min', '1h', '10h']") root = os.fspath(root) self._path = os.path.join(root, _ARCHIVE_NAME) archive = os.path.join(root, f"{_ARCHIVE_NAME}.tgz") if not os.path.isdir(self._path): if not download: raise RuntimeError("Dataset not found. Please use `download=True` to download") if not os.path.isfile(archive): download_url_to_file(_URL, archive, hash_prefix=_CHECKSUM) extract_archive(archive) self._fileids_paths = _get_fileids_paths(self._path, subset, self._ext_audio) def __getitem__(self, n: int) -> Tuple[Tensor, int, str, int, int, int]: """Load the n-th sample from the dataset. Args: n (int): The index of the sample to be loaded Returns: (Tensor, int, str, int, int, int): ``(waveform, sample_rate, transcript, speaker_id, chapter_id, utterance_id)`` """ file_path, fileid = self._fileids_paths[n] return load_librispeech_item(fileid, file_path, self._ext_audio, self._ext_txt) def __len__(self) -> int: return len(self._fileids_paths)
import os from pathlib import Path from typing import List, Tuple, Union from torch import Tensor from torch.hub import download_url_to_file from torch.utils.data import Dataset from torchaudio.datasets.librispeech import load_librispeech_item from torchaudio.datasets.utils import extract_archive _ARCHIVE_NAME = "librispeech_finetuning" _URL = "https://dl.fbaipublicfiles.com/librilight/data/librispeech_finetuning.tgz" _CHECKSUM = "5d1efdc777b548194d7e09ba89126e2188026df9fd57aa57eb14408d2b2342af" def _get_fileids_paths(path, subset, _ext_audio) -> List[Tuple[str, str]]: """Get the file names and the corresponding file paths without `speaker_id` and `chapter_id` directories. The format of path is like: {root}/{_ARCHIVE_NAME}/1h/[0-5]/[clean, other] or {root}/{_ARCHIVE_NAME}/9h/[clean, other] """ if subset == "10min": files_paths = [ (os.path.join(os.path.dirname(p), "..", ".."), str(p.stem)) for p in Path(path).glob("1h/0/*/*/*/*" + _ext_audio) ] elif subset in ["1h", "10h"]: files_paths = [ (os.path.join(os.path.dirname(p), "..", ".."), str(p.stem)) for p in Path(path).glob("1h/*/*/*/*/*" + _ext_audio) ] if subset == "10h": files_paths += [ (os.path.join(os.path.dirname(p), "..", ".."), str(p.stem)) for p in Path(path).glob("9h/*/*/*/*" + _ext_audio) ] else: raise ValueError(f"Unsupported subset value. Found {subset}.") files_paths = sorted(files_paths, key=lambda x: x[0] + x[1]) return files_paths class LibriLightLimited(Dataset): """Create a Dataset for LibriLightLimited, which is the supervised subset of LibriLight dataset. Args: root (str or Path): Path to the directory where the dataset is found or downloaded. subset (str, optional): The subset to use. Options: [``10min``, ``1h``, ``10h``] (Default: ``10min``). download (bool, optional): Whether to download the dataset if it is not found at root path. (default: ``False``). """ _ext_txt = ".trans.txt" _ext_audio = ".flac" def __init__( self, root: Union[str, Path], subset: str = "10min", download: bool = False, ) -> None: if subset not in ["10min", "1h", "10h"]: raise ValueError("`subset` must be one of ['10min', '1h', '10h']") root = os.fspath(root) self._path = os.path.join(root, _ARCHIVE_NAME) archive = os.path.join(root, f"{_ARCHIVE_NAME}.tgz") if not os.path.isdir(self._path): if not download: raise RuntimeError("Dataset not found. Please use `download=True` to download") if not os.path.isfile(archive): download_url_to_file(_URL, archive, hash_prefix=_CHECKSUM) extract_archive(archive) self._fileids_paths = _get_fileids_paths(self._path, subset, self._ext_audio) def __getitem__(self, n: int) -> Tuple[Tensor, int, str, int, int, int]: """Load the n-th sample from the dataset. Args: n (int): The index of the sample to be loaded Returns: (Tensor, int, str, int, int, int): ``(waveform, sample_rate, transcript, speaker_id, chapter_id, utterance_id)`` """ file_path, fileid = self._fileids_paths[n] return load_librispeech_item(fileid, file_path, self._ext_audio, self._ext_txt) def __len__(self) -> int: return len(self._fileids_paths)
from __future__ import annotations import torch.nn as nn from sentence_transformers.losses.CosineSimilarityLoss import CosineSimilarityLoss from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class SparseCosineSimilarityLoss(CosineSimilarityLoss): def __init__( self, model: SparseEncoder, loss_fct: nn.Module = nn.MSELoss(), cos_score_transformation: nn.Module = nn.Identity(), ) -> None: """ SparseCosineSimilarityLoss expects that the InputExamples consists of two texts and a float label. It computes the vectors ``u = model(sentence_A)`` and ``v = model(sentence_B)`` and measures the cosine-similarity between the two. By default, it minimizes the following loss: ``||input_label - cos_score_transformation(cosine_sim(u,v))||_2``. Args: model: SparseEncoder model loss_fct: Which pytorch loss function should be used to compare the ``cosine_similarity(u, v)`` with the input_label? By default, MSE is used: ``||input_label - cosine_sim(u, v)||_2`` cos_score_transformation: The cos_score_transformation function is applied on top of cosine_similarity. By default, the identify function is used (i.e. no change). References: # TODO: Not yet for sparse might want it ? - `Training Examples > Semantic Textual Similarity <../../../examples/sentence_transformer/training/sts/README.html>`_ Requirements: 1. Sentence pairs with corresponding similarity scores in range `[0, 1]` Inputs: +--------------------------------+------------------------+ | Texts | Labels | +================================+========================+ | (sentence_A, sentence_B) pairs | float similarity score | +--------------------------------+------------------------+ Relations: - :class:`SparseCoSENTLoss` seems to produce a stronger training signal than CosineSimilarityLoss. In our experiments, SparseCoSENTLoss is recommended. - :class:`SparseAnglELoss` is :class:`SparseCoSENTLoss` with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``. It also produces a stronger training signal than SparseCosineSimilarityLoss. Example: :: from datasets import Dataset from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses model = SparseEncoder("distilbert/distilbert-base-uncased") train_dataset = Dataset.from_dict( { "sentence1": ["It's nice weather outside today.", "He drove to work."], "sentence2": ["It's so sunny.", "She walked to the store."], "score": [1.0, 0.3], } ) loss = losses.SpladeLoss(model=model, loss=losses.SparseCosineSimilarityLoss(model), lambda_corpus=5e-5, all_docs=True) trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss) trainer.train() """ model.similarity_fn_name = "cosine" return super().__init__(model, loss_fct=loss_fct, cos_score_transformation=cos_score_transformation)
from __future__ import annotations import torch.nn as nn from sentence_transformers.losses.CosineSimilarityLoss import CosineSimilarityLoss from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class SparseCosineSimilarityLoss(CosineSimilarityLoss): def __init__( self, model: SparseEncoder, loss_fct: nn.Module = nn.MSELoss(), cos_score_transformation: nn.Module = nn.Identity(), ) -> None: """ SparseCosineSimilarityLoss expects that the InputExamples consists of two texts and a float label. It computes the vectors ``u = model(sentence_A)`` and ``v = model(sentence_B)`` and measures the cosine-similarity between the two. By default, it minimizes the following loss: ``||input_label - cos_score_transformation(cosine_sim(u,v))||_2``. Args: model: SparseEncoder model loss_fct: Which pytorch loss function should be used to compare the ``cosine_similarity(u, v)`` with the input_label? By default, MSE is used: ``||input_label - cosine_sim(u, v)||_2`` cos_score_transformation: The cos_score_transformation function is applied on top of cosine_similarity. By default, the identify function is used (i.e. no change). References: # TODO: Not yet for sparse might want it ? - `Training Examples > Semantic Textual Similarity <../../../examples/sentence_transformer/training/sts/README.html>`_ Requirements: 1. Sentence pairs with corresponding similarity scores in range `[0, 1]` Inputs: +--------------------------------+------------------------+ | Texts | Labels | +================================+========================+ | (sentence_A, sentence_B) pairs | float similarity score | +--------------------------------+------------------------+ Relations: - :class:`SparseCoSENTLoss` seems to produce a stronger training signal than CosineSimilarityLoss. In our experiments, SparseCoSENTLoss is recommended. - :class:`SparseAnglELoss` is :class:`SparseCoSENTLoss` with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``. It also produces a stronger training signal than SparseCosineSimilarityLoss. Example: :: from datasets import Dataset from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses model = SparseEncoder("distilbert/distilbert-base-uncased") train_dataset = Dataset.from_dict( { "sentence1": ["It's nice weather outside today.", "He drove to work."], "sentence2": ["It's so sunny.", "She walked to the store."], "score": [1.0, 0.3], } ) loss = losses.SparseCosineSimilarityLoss(model) trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss) trainer.train() """ return super().__init__(model, loss_fct=loss_fct, cos_score_transformation=cos_score_transformation)
"""Toolkit for interacting with a vector store.""" from langchain_core.language_models import BaseLanguageModel from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit from langchain_core.vectorstores import VectorStore from pydantic import BaseModel, ConfigDict, Field class VectorStoreInfo(BaseModel): """Information about a VectorStore.""" vectorstore: VectorStore = Field(exclude=True) name: str description: str model_config = ConfigDict( arbitrary_types_allowed=True, ) class VectorStoreToolkit(BaseToolkit): """Toolkit for interacting with a Vector Store.""" vectorstore_info: VectorStoreInfo = Field(exclude=True) llm: BaseLanguageModel model_config = ConfigDict( arbitrary_types_allowed=True, ) def get_tools(self) -> list[BaseTool]: """Get the tools in the toolkit.""" try: from langchain_community.tools.vectorstore.tool import ( VectorStoreQATool, VectorStoreQAWithSourcesTool, ) except ImportError: msg = "You need to install langchain-community to use this toolkit." raise ImportError(msg) description = VectorStoreQATool.get_description( self.vectorstore_info.name, self.vectorstore_info.description, ) qa_tool = VectorStoreQATool( name=self.vectorstore_info.name, description=description, vectorstore=self.vectorstore_info.vectorstore, llm=self.llm, ) description = VectorStoreQAWithSourcesTool.get_description( self.vectorstore_info.name, self.vectorstore_info.description, ) qa_with_sources_tool = VectorStoreQAWithSourcesTool( name=f"{self.vectorstore_info.name}_with_sources", description=description, vectorstore=self.vectorstore_info.vectorstore, llm=self.llm, ) return [qa_tool, qa_with_sources_tool] class VectorStoreRouterToolkit(BaseToolkit): """Toolkit for routing between Vector Stores.""" vectorstores: list[VectorStoreInfo] = Field(exclude=True) llm: BaseLanguageModel model_config = ConfigDict( arbitrary_types_allowed=True, ) def get_tools(self) -> list[BaseTool]: """Get the tools in the toolkit.""" tools: list[BaseTool] = [] try: from langchain_community.tools.vectorstore.tool import ( VectorStoreQATool, ) except ImportError: msg = "You need to install langchain-community to use this toolkit." raise ImportError(msg) for vectorstore_info in self.vectorstores: description = VectorStoreQATool.get_description( vectorstore_info.name, vectorstore_info.description, ) qa_tool = VectorStoreQATool( name=vectorstore_info.name, description=description, vectorstore=vectorstore_info.vectorstore, llm=self.llm, ) tools.append(qa_tool) return tools
"""Toolkit for interacting with a vector store.""" from langchain_core.language_models import BaseLanguageModel from langchain_core.tools import BaseTool from langchain_core.tools.base import BaseToolkit from langchain_core.vectorstores import VectorStore from pydantic import BaseModel, ConfigDict, Field class VectorStoreInfo(BaseModel): """Information about a VectorStore.""" vectorstore: VectorStore = Field(exclude=True) name: str description: str model_config = ConfigDict( arbitrary_types_allowed=True, ) class VectorStoreToolkit(BaseToolkit): """Toolkit for interacting with a Vector Store.""" vectorstore_info: VectorStoreInfo = Field(exclude=True) llm: BaseLanguageModel model_config = ConfigDict( arbitrary_types_allowed=True, ) def get_tools(self) -> list[BaseTool]: """Get the tools in the toolkit.""" try: from langchain_community.tools.vectorstore.tool import ( VectorStoreQATool, VectorStoreQAWithSourcesTool, ) except ImportError: msg = "You need to install langchain-community to use this toolkit." raise ImportError(msg) description = VectorStoreQATool.get_description( self.vectorstore_info.name, self.vectorstore_info.description ) qa_tool = VectorStoreQATool( name=self.vectorstore_info.name, description=description, vectorstore=self.vectorstore_info.vectorstore, llm=self.llm, ) description = VectorStoreQAWithSourcesTool.get_description( self.vectorstore_info.name, self.vectorstore_info.description ) qa_with_sources_tool = VectorStoreQAWithSourcesTool( name=f"{self.vectorstore_info.name}_with_sources", description=description, vectorstore=self.vectorstore_info.vectorstore, llm=self.llm, ) return [qa_tool, qa_with_sources_tool] class VectorStoreRouterToolkit(BaseToolkit): """Toolkit for routing between Vector Stores.""" vectorstores: list[VectorStoreInfo] = Field(exclude=True) llm: BaseLanguageModel model_config = ConfigDict( arbitrary_types_allowed=True, ) def get_tools(self) -> list[BaseTool]: """Get the tools in the toolkit.""" tools: list[BaseTool] = [] try: from langchain_community.tools.vectorstore.tool import ( VectorStoreQATool, ) except ImportError: msg = "You need to install langchain-community to use this toolkit." raise ImportError(msg) for vectorstore_info in self.vectorstores: description = VectorStoreQATool.get_description( vectorstore_info.name, vectorstore_info.description ) qa_tool = VectorStoreQATool( name=vectorstore_info.name, description=description, vectorstore=vectorstore_info.vectorstore, llm=self.llm, ) tools.append(qa_tool) return tools
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Dict, List, Optional import numpy as np from annoy import AnnoyIndex from jina import Document, DocumentArray, Executor, requests from jina_commons import get_logger from jina_commons.indexers.dump import import_vectors class AnnoySearcher(Executor): """Annoy powered vector indexer For more information about the Annoy supported parameters, please consult: - https://github.com/spotify/annoy .. note:: Annoy package dependency is only required at the query time. """ def __init__( self, default_top_k: int = 10, metric: str = "euclidean", num_trees: int = 10, dump_path: Optional[str] = None, default_traversal_paths: List[str] = ["r"], is_distance: bool = False, **kwargs, ): """ Initialize an AnnoyIndexer :param default_top_k: get tok k vectors :param metric: Metric can be "angular", "euclidean", "manhattan", "hamming", or "dot" :param num_trees: builds a forest of n_trees trees. More trees gives higher precision when querying. :param dump_path: the path to load ids and vecs :param traverse_path: traverse path on docs, e.g. ['r'], ['c'] :param is_distance: Boolean flag that describes if distance metric need to be reinterpreted as similarities. """ super().__init__(**kwargs) self.default_top_k = default_top_k self.metric = metric self.num_trees = num_trees self.default_traversal_paths = default_traversal_paths self.is_distance = is_distance self.logger = get_logger(self) self._doc_id_to_offset = {} dump_path = dump_path or kwargs.get("runtime_args", {}).get("dump_path", None) if dump_path is not None: self.logger.info('Start building "AnnoyIndexer" from dump data') ids, vecs = import_vectors(dump_path, str(self.runtime_args.pea_id)) self._ids = np.array(list(ids)) self._vecs = np.array(list(vecs)) num_dim = self._vecs.shape[1] self._indexer = AnnoyIndex(num_dim, self.metric) self._load_index(self._ids, self._vecs) self.logger.info("Done building Annoy index") else: self.logger.warning( 'No data loaded in "AnnoyIndexer". Use .rolling_update() to re-initialize it...' ) def _load_index(self, ids, vecs): for idx, v in enumerate(vecs): self._indexer.add_item(idx, v.astype(np.float32)) self._doc_id_to_offset[ids[idx]] = idx self._indexer.build(self.num_trees) @requests(on="/search") def search(self, docs: DocumentArray, parameters: Dict, **kwargs): if not hasattr(self, "_indexer"): self.logger.warning("Querying against an empty index") return traversal_paths = parameters.get( "traversal_paths", self.default_traversal_paths ) top_k = parameters.get("top_k", self.default_top_k) for doc in docs.traverse_flat(traversal_paths): indices, dists = self._indexer.get_nns_by_vector( doc.embedding, top_k, include_distances=True ) for idx, dist in zip(indices, dists): match = Document(id=self._ids[idx], embedding=self._vecs[idx]) if self.is_distance: if self.metric == "dot": match.scores[self.metric] = 1 - dist else: match.scores[self.metric] = dist else: if self.metric == "dot": match.scores[self.metric] = dist elif self.metric == "angular" or self.metric == "hamming": match.scores[self.metric] = 1 - dist else: match.scores[self.metric] = 1 / (1 + dist) doc.matches.append(match) @requests(on="/fill_embedding") def fill_embedding(self, query_da: DocumentArray, **kwargs): for doc in query_da: doc_idx = self._doc_id_to_offset.get(doc.id) if doc_idx is not None: doc.embedding = np.array(self._indexer.get_item_vector(int(doc_idx))) else: self.logger.warning(f"Document {doc.id} not found in index")
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Dict, List, Optional import numpy as np from annoy import AnnoyIndex from jina import Document, DocumentArray, Executor, requests from jina_commons import get_logger from jina_commons.indexers.dump import import_vectors class AnnoySearcher(Executor): """Annoy powered vector indexer For more information about the Annoy supported parameters, please consult: - https://github.com/spotify/annoy .. note:: Annoy package dependency is only required at the query time. """ def __init__( self, default_top_k: int = 10, metric: str = "euclidean", num_trees: int = 10, dump_path: Optional[str] = None, default_traversal_paths: List[str] = ["r"], is_distance: bool = False, **kwargs, ): """ Initialize an AnnoyIndexer :param default_top_k: get tok k vectors :param metric: Metric can be "angular", "euclidean", "manhattan", "hamming", or "dot" :param num_trees: builds a forest of n_trees trees. More trees gives higher precision when querying. :param dump_path: the path to load ids and vecs :param traverse_path: traverse path on docs, e.g. ['r'], ['c'] :param is_distance: Boolean flag that describes if distance metric need to be reinterpreted as similarities. :param args: :param kwargs: """ super().__init__(**kwargs) self.default_top_k = default_top_k self.metric = metric self.num_trees = num_trees self.default_traversal_paths = default_traversal_paths self.is_distance = is_distance self.logger = get_logger(self) self._doc_id_to_offset = {} dump_path = dump_path or kwargs.get("runtime_args", {}).get("dump_path", None) if dump_path is not None: self.logger.info('Start building "AnnoyIndexer" from dump data') ids, vecs = import_vectors(dump_path, str(self.runtime_args.pea_id)) self._ids = np.array(list(ids)) self._vecs = np.array(list(vecs)) num_dim = self._vecs.shape[1] self._indexer = AnnoyIndex(num_dim, self.metric) self._load_index(self._ids, self._vecs) self.logger.info("Done building Annoy index") else: self.logger.warning( 'No data loaded in "AnnoyIndexer". Use .rolling_update() to re-initialize it...' ) def _load_index(self, ids, vecs): for idx, v in enumerate(vecs): self._indexer.add_item(idx, v.astype(np.float32)) self._doc_id_to_offset[ids[idx]] = idx self._indexer.build(self.num_trees) @requests(on="/search") def search(self, docs: DocumentArray, parameters: Dict, **kwargs): if not hasattr(self, "_indexer"): self.logger.warning("Querying against an empty index") return traversal_paths = parameters.get( "traversal_paths", self.default_traversal_paths ) top_k = parameters.get("top_k", self.default_top_k) for doc in docs.traverse_flat(traversal_paths): indices, dists = self._indexer.get_nns_by_vector( doc.embedding, top_k, include_distances=True ) for idx, dist in zip(indices, dists): match = Document(id=self._ids[idx], embedding=self._vecs[idx]) if self.is_distance: if self.metric == "dot": match.scores[self.metric] = 1 - dist else: match.scores[self.metric] = dist else: if self.metric == "dot": match.scores[self.metric] = dist elif self.metric == "angular" or self.metric == "hamming": match.scores[self.metric] = 1 - dist else: match.scores[self.metric] = 1 / (1 + dist) doc.matches.append(match) @requests(on="/fill_embedding") def fill_embedding(self, query_da: DocumentArray, **kwargs): for doc in query_da: doc_idx = self._doc_id_to_offset.get(doc.id) if doc_idx is not None: doc.embedding = np.array(self._indexer.get_item_vector(int(doc_idx))) else: self.logger.warning(f"Document {doc.id} not found in index")
# Copyright (c) OpenMMLab. All rights reserved. from unittest import TestCase import torch from mmengine.structures import InstanceData from mmdet.registry import MODELS from mmdet.structures import DetDataSample from mmdet.testing import get_detector_cfg from mmdet.utils import register_all_modules class TestDABDETR(TestCase): def setUp(self) -> None: register_all_modules() def test_dab_detr_head_loss(self): """Tests transformer head loss when truth is empty and non-empty.""" s = 256 metainfo = { 'img_shape': (s, s), 'scale_factor': (1, 1), 'pad_shape': (s, s), 'batch_input_shape': (s, s) } img_metas = DetDataSample() img_metas.set_metainfo(metainfo) batch_data_samples = [] batch_data_samples.append(img_metas) config = get_detector_cfg('dab_detr/dab-detr_r50_8xb2-50e_coco.py') model = MODELS.build(config) model.init_weights() random_image = torch.rand(1, 3, s, s) # Test that empty ground truth encourages the network to # predict background gt_instances = InstanceData() gt_instances.bboxes = torch.empty((0, 4)) gt_instances.labels = torch.LongTensor([]) img_metas.gt_instances = gt_instances batch_data_samples1 = [] batch_data_samples1.append(img_metas) empty_gt_losses = model.loss( random_image, batch_data_samples=batch_data_samples1) # When there is no truth, the cls loss should be nonzero but there # should be no box loss. for key, loss in empty_gt_losses.items(): if 'cls' in key: self.assertGreater(loss.item(), 0, 'cls loss should be non-zero') elif 'bbox' in key: self.assertEqual( loss.item(), 0, 'there should be no box loss when no ground true boxes') elif 'iou' in key: self.assertEqual( loss.item(), 0, 'there should be no iou loss when there are no true boxes') # When truth is non-empty then both cls and box loss should be nonzero # for random inputs gt_instances = InstanceData() gt_instances.bboxes = torch.Tensor( [[23.6667, 23.8757, 238.6326, 151.8874]]) gt_instances.labels = torch.LongTensor([2]) img_metas.gt_instances = gt_instances batch_data_samples2 = [] batch_data_samples2.append(img_metas) one_gt_losses = model.loss( random_image, batch_data_samples=batch_data_samples2) for loss in one_gt_losses.values(): self.assertGreater( loss.item(), 0, 'cls loss, or box loss, or iou loss should be non-zero') model.eval() # test _forward model._forward(random_image, batch_data_samples=batch_data_samples2) # test only predict model.predict( random_image, batch_data_samples=batch_data_samples2, rescale=True)
# Copyright (c) OpenMMLab. All rights reserved. from unittest import TestCase import torch from mmengine.structures import InstanceData from mmdet.models import build_detector from mmdet.structures import DetDataSample from mmdet.testing import get_detector_cfg from mmdet.utils import register_all_modules class TestDABDETR(TestCase): def setUp(self) -> None: register_all_modules() def test_dab_detr_head_loss(self): """Tests transformer head loss when truth is empty and non-empty.""" s = 256 metainfo = { 'img_shape': (s, s), 'scale_factor': (1, 1), 'pad_shape': (s, s), 'batch_input_shape': (s, s) } img_metas = DetDataSample() img_metas.set_metainfo(metainfo) batch_data_samples = [] batch_data_samples.append(img_metas) config = get_detector_cfg('dab_detr/dab-detr_r50_8xb2-50e_coco.py') model = build_detector(config) model.init_weights() random_image = torch.rand(1, 3, s, s) # Test that empty ground truth encourages the network to # predict background gt_instances = InstanceData() gt_instances.bboxes = torch.empty((0, 4)) gt_instances.labels = torch.LongTensor([]) img_metas.gt_instances = gt_instances batch_data_samples1 = [] batch_data_samples1.append(img_metas) empty_gt_losses = model.loss( random_image, batch_data_samples=batch_data_samples1) # When there is no truth, the cls loss should be nonzero but there # should be no box loss. for key, loss in empty_gt_losses.items(): if 'cls' in key: self.assertGreater(loss.item(), 0, 'cls loss should be non-zero') elif 'bbox' in key: self.assertEqual( loss.item(), 0, 'there should be no box loss when no ground true boxes') elif 'iou' in key: self.assertEqual( loss.item(), 0, 'there should be no iou loss when there are no true boxes') # When truth is non-empty then both cls and box loss should be nonzero # for random inputs gt_instances = InstanceData() gt_instances.bboxes = torch.Tensor( [[23.6667, 23.8757, 238.6326, 151.8874]]) gt_instances.labels = torch.LongTensor([2]) img_metas.gt_instances = gt_instances batch_data_samples2 = [] batch_data_samples2.append(img_metas) one_gt_losses = model.loss( random_image, batch_data_samples=batch_data_samples2) for loss in one_gt_losses.values(): self.assertGreater( loss.item(), 0, 'cls loss, or box loss, or iou loss should be non-zero') model.eval() # test _forward model._forward(random_image, batch_data_samples=batch_data_samples2) # test only predict model.predict( random_image, batch_data_samples=batch_data_samples2, rescale=True)
""" The system trains BERT (or any other transformer model like RoBERTa, DistilBERT etc.) on the SNLI + MultiNLI (AllNLI) dataset with softmax loss function. At every 1000 training steps, the model is evaluated on the STS benchmark dataset Usage: python training_nli.py OR python training_nli.py pretrained_transformer_model_name """ from torch.utils.data import DataLoader import math from sentence_transformers import models, losses from sentence_transformers import LoggingHandler, SentenceTransformer, util, InputExample from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator import logging from datetime import datetime import sys import os import gzip import csv #### 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 # Check if dataset exists. If not, download and extract it nli_dataset_path = "data/AllNLI.tsv.gz" sts_dataset_path = "data/stsbenchmark.tsv.gz" if not os.path.exists(nli_dataset_path): util.http_get("https://sbert.net/datasets/AllNLI.tsv.gz", nli_dataset_path) if not os.path.exists(sts_dataset_path): util.http_get("https://sbert.net/datasets/stsbenchmark.tsv.gz", sts_dataset_path) # You can specify any huggingface/transformers pre-trained model here, for example, bert-base-uncased, roberta-base, xlm-roberta-base model_name = sys.argv[1] if len(sys.argv) > 1 else "bert-base-uncased" # Read the dataset train_batch_size = 16 model_save_path = ( "output/training_nli_" + model_name.replace("/", "-") + "-" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") ) # Use Huggingface/transformers model (like BERT, RoBERTa, XLNet, XLM-R) for mapping tokens to embeddings word_embedding_model = models.Transformer(model_name) # Apply mean pooling to get one fixed sized sentence vector pooling_model = models.Pooling( word_embedding_model.get_word_embedding_dimension(), pooling_mode_mean_tokens=True, pooling_mode_cls_token=False, pooling_mode_max_tokens=False, ) model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) # Read the AllNLI.tsv.gz file and create the training dataset logging.info("Read AllNLI train dataset") label2int = {"contradiction": 0, "entailment": 1, "neutral": 2} train_samples = [] with gzip.open(nli_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: if row["split"] == "train": label_id = label2int[row["label"]] train_samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=label_id)) train_dataloader = DataLoader(train_samples, shuffle=True, batch_size=train_batch_size) train_loss = losses.SoftmaxLoss( model=model, sentence_embedding_dimension=model.get_sentence_embedding_dimension(), num_labels=len(label2int) ) # Read STSbenchmark dataset and use it as development set logging.info("Read STSbenchmark dev dataset") dev_samples = [] with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: if row["split"] == "dev": score = float(row["score"]) / 5.0 # Normalize score to range 0 ... 1 dev_samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=score)) dev_evaluator = EmbeddingSimilarityEvaluator.from_input_examples( dev_samples, batch_size=train_batch_size, name="sts-dev" ) # Configure the training num_epochs = 1 warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) # 10% of train data for warm-up logging.info("Warmup-steps: {}".format(warmup_steps)) # Train the model model.fit( train_objectives=[(train_dataloader, train_loss)], evaluator=dev_evaluator, epochs=num_epochs, evaluation_steps=1000, warmup_steps=warmup_steps, output_path=model_save_path, ) ############################################################################## # # Load the stored model and evaluate its performance on STS benchmark dataset # ############################################################################## test_samples = [] with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: if row["split"] == "test": score = float(row["score"]) / 5.0 # Normalize score to range 0 ... 1 test_samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=score)) model = SentenceTransformer(model_save_path) test_evaluator = EmbeddingSimilarityEvaluator.from_input_examples( test_samples, batch_size=train_batch_size, name="sts-test" ) test_evaluator(model, output_path=model_save_path)
""" The system trains BERT (or any other transformer model like RoBERTa, DistilBERT etc.) on the SNLI + MultiNLI (AllNLI) dataset with softmax loss function. At every 1000 training steps, the model is evaluated on the STS benchmark dataset Usage: python training_nli.py OR python training_nli.py pretrained_transformer_model_name """ from torch.utils.data import DataLoader import math from sentence_transformers import models, losses from sentence_transformers import LoggingHandler, SentenceTransformer, util, InputExample from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator import logging from datetime import datetime import sys import os import gzip import csv #### 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 # Check if dataset exists. If not, download and extract it nli_dataset_path = "data/AllNLI.tsv.gz" sts_dataset_path = "data/stsbenchmark.tsv.gz" if not os.path.exists(nli_dataset_path): util.http_get("https://sbert.net/datasets/AllNLI.tsv.gz", nli_dataset_path) if not os.path.exists(sts_dataset_path): util.http_get("https://sbert.net/datasets/stsbenchmark.tsv.gz", sts_dataset_path) # You can specify any huggingface/transformers pre-trained model here, for example, bert-base-uncased, roberta-base, xlm-roberta-base model_name = sys.argv[1] if len(sys.argv) > 1 else "bert-base-uncased" # Read the dataset train_batch_size = 16 model_save_path = ( "output/training_nli_" + model_name.replace("/", "-") + "-" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") ) # Use Huggingface/transformers model (like BERT, RoBERTa, XLNet, XLM-R) for mapping tokens to embeddings word_embedding_model = models.Transformer(model_name) # Apply mean pooling to get one fixed sized sentence vector pooling_model = models.Pooling( word_embedding_model.get_word_embedding_dimension(), pooling_mode_mean_tokens=True, pooling_mode_cls_token=False, pooling_mode_max_tokens=False, ) model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) # Read the AllNLI.tsv.gz file and create the training dataset logging.info("Read AllNLI train dataset") label2int = {"contradiction": 0, "entailment": 1, "neutral": 2} train_samples = [] with gzip.open(nli_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: if row["split"] == "train": label_id = label2int[row["label"]] train_samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=label_id)) train_dataloader = DataLoader(train_samples, shuffle=True, batch_size=train_batch_size) train_loss = losses.SoftmaxLoss( model=model, sentence_embedding_dimension=model.get_sentence_embedding_dimension(), num_labels=len(label2int) ) # Read STSbenchmark dataset and use it as development set logging.info("Read STSbenchmark dev dataset") dev_samples = [] with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: if row["split"] == "dev": score = float(row["score"]) / 5.0 # Normalize score to range 0 ... 1 dev_samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=score)) dev_evaluator = EmbeddingSimilarityEvaluator.from_input_examples( dev_samples, batch_size=train_batch_size, name="sts-dev" ) # Configure the training num_epochs = 1 warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) # 10% of train data for warm-up logging.info("Warmup-steps: {}".format(warmup_steps)) # Train the model model.fit( train_objectives=[(train_dataloader, train_loss)], evaluator=dev_evaluator, epochs=num_epochs, evaluation_steps=1000, warmup_steps=warmup_steps, output_path=model_save_path, ) ############################################################################## # # Load the stored model and evaluate its performance on STS benchmark dataset # ############################################################################## test_samples = [] with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: if row["split"] == "test": score = float(row["score"]) / 5.0 # Normalize score to range 0 ... 1 test_samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=score)) model = SentenceTransformer(model_save_path) test_evaluator = EmbeddingSimilarityEvaluator.from_input_examples( test_samples, batch_size=train_batch_size, name="sts-test" ) test_evaluator(model, output_path=model_save_path)
_base_ = ['../common/ms_3x_coco.py', '../_base_/models/faster-rcnn_r50_fpn.py'] model = dict( backbone=dict( type='ResNeXt', depth=101, groups=64, base_width=4, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://resnext101_64x4d')))
_base_ = [ '../common/mstrain_3x_coco.py', '../_base_/models/faster_rcnn_r50_fpn.py' ] model = dict( backbone=dict( type='ResNeXt', depth=101, groups=64, base_width=4, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://resnext101_64x4d')))
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from parameterized import parameterized from mmdet.registry import MODELS from mmdet.testing import demo_mm_inputs, demo_mm_proposals, get_roi_head_cfg from mmdet.utils import register_all_modules class TestPISARoIHead(TestCase): def setUp(self): register_all_modules() self.roi_head_cfg = get_roi_head_cfg( 'pisa/pisa_faster_rcnn_r50_fpn_1x_coco.py') def test_init(self): roi_head = MODELS.build(self.roi_head_cfg) self.assertTrue(roi_head.with_bbox) @parameterized.expand(['cpu', 'cuda']) def test_pisa_roi_head(self, device): """Tests trident roi head predict.""" if not torch.cuda.is_available() and device == 'cuda': # RoI pooling only support in GPU return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[1], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) loss_cls = out['loss_cls'] loss_bbox = out['loss_bbox'] self.assertGreater(loss_cls.sum(), 0, 'cls loss should be non-zero') self.assertGreater(loss_bbox.sum(), 0, 'box loss should be non-zero') batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[0], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) empty_cls_loss = out['loss_cls'] empty_bbox_loss = out['loss_bbox'] self.assertGreater(empty_cls_loss.sum(), 0, 'cls loss should be non-zero') self.assertEqual( empty_bbox_loss.sum(), 0, 'there should be no box loss when there are no true boxes')
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from parameterized import parameterized from mmdet.registry import MODELS from mmdet.testing import demo_mm_inputs, demo_mm_proposals, get_roi_head_cfg from mmdet.utils import register_all_modules class TestPISARoIHead(TestCase): def setUp(self): register_all_modules() self.roi_head_cfg = get_roi_head_cfg( 'pisa/pisa_faster_rcnn_r50_fpn_1x_coco.py') def test_init(self): roi_head = MODELS.build(self.roi_head_cfg) self.assertTrue(roi_head.with_bbox) @parameterized.expand(['cpu', 'cuda']) def test_pisa_roi_head(self, device): """Tests trident roi head predict.""" if not torch.cuda.is_available() and device == 'cuda': # RoI pooling only support in GPU return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] packed_inputs = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[1], num_classes=4, with_mask=True) proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100) batch_data_samples = [] for i in range(len(packed_inputs)): batch_data_samples.append( packed_inputs[i]['data_sample'].to(device=device)) proposals_list[i] = proposals_list[i].to(device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) loss_cls = out['loss_cls'] loss_bbox = out['loss_bbox'] self.assertGreater(loss_cls.sum(), 0, 'cls loss should be non-zero') self.assertGreater(loss_bbox.sum(), 0, 'box loss should be non-zero') packed_inputs = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[0], num_classes=4, with_mask=True) proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100) batch_data_samples = [] for i in range(len(packed_inputs)): batch_data_samples.append( packed_inputs[i]['data_sample'].to(device=device)) proposals_list[i] = proposals_list[i].to(device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) empty_cls_loss = out['loss_cls'] empty_bbox_loss = out['loss_bbox'] self.assertGreater(empty_cls_loss.sum(), 0, 'cls loss should be non-zero') self.assertEqual( empty_bbox_loss.sum(), 0, 'there should be no box loss when there are no true boxes')
from datetime import timedelta from typing import Optional from torch._C._distributed_c10d import _DEFAULT_PG_TIMEOUT __all__ = ["default_pg_timeout", "default_pg_nccl_timeout"] # Default process group wide timeout, if applicable. # This only applies to the non-nccl backends # To make an attempt at backwards compatibility with THD, we use an # extraordinarily high default timeout, given that THD did not have timeouts. default_pg_timeout: timedelta = _DEFAULT_PG_TIMEOUT # Separate timeout for PGNCCL mainly becuase it's always been that way in the C++ layer, but until recently # there was one default that applied across all backends in the python layer. # Later, we could consider merging them back together at the c++ layer if we can align on a same value. # (only if TORCH_NCCL_BLOCKING_WAIT or TORCH_NCCL_ASYNC_ERROR_HANDLING is set to 1). try: from torch._C._distributed_c10d import _DEFAULT_PG_NCCL_TIMEOUT default_pg_nccl_timeout: Optional[timedelta] = _DEFAULT_PG_NCCL_TIMEOUT except ImportError: # if C++ NCCL support is not compiled, we don't have access to the default nccl value. # if anyone is actually trying to use nccl in this state, it should error. default_pg_nccl_timeout = None
from datetime import timedelta from typing import Optional from torch._C._distributed_c10d import _DEFAULT_PG_TIMEOUT __all__ = ["default_pg_timeout", "default_pg_nccl_timeout"] # Default process group wide timeout, if applicable. # This only applies to the non-nccl backends # To make an attempt at backwards compatibility with THD, we use an # extraordinarily high default timeout, given that THD did not have timeouts. default_pg_timeout: timedelta = _DEFAULT_PG_TIMEOUT # Separate timeout for PGNCCL mainly because it's always been that way in the C++ layer, but until recently # there was one default that applied across all backends in the python layer. # Later, we could consider merging them back together at the c++ layer if we can align on a same value. # (only if TORCH_NCCL_BLOCKING_WAIT or TORCH_NCCL_ASYNC_ERROR_HANDLING is set to 1). try: from torch._C._distributed_c10d import _DEFAULT_PG_NCCL_TIMEOUT default_pg_nccl_timeout: Optional[timedelta] = _DEFAULT_PG_NCCL_TIMEOUT except ImportError: # if C++ NCCL support is not compiled, we don't have access to the default nccl value. # if anyone is actually trying to use nccl in this state, it should error. default_pg_nccl_timeout = None
from typing import Any, ForwardRef, 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) or (type(x) == ForwardRef) ): 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 from typing import ForwardRef 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 (type(x) == ForwardRef): return False return issubclass(x, a_tuple)
# Copyright (c) OpenMMLab. All rights reserved. import os.path as osp import xml.etree.ElementTree as ET from mmengine.fileio import list_from_file from mmdet.registry import DATASETS from .xml_style import XMLDataset @DATASETS.register_module() class WIDERFaceDataset(XMLDataset): """Reader for the WIDER Face dataset in PASCAL VOC format. Conversion scripts can be found in https://github.com/sovrasov/wider-face-pascal-voc-annotations """ METAINFO = {'classes': ('face', ), 'palette': [(0, 255, 0)]} def __init__(self, **kwargs): super(WIDERFaceDataset, self).__init__(**kwargs) def load_annotations(self, ann_file): """Load annotation from WIDERFace XML style annotation file. Args: ann_file (str): Path of XML file. Returns: list[dict]: Annotation info from XML file. """ data_infos = [] img_ids = list_from_file(ann_file) for img_id in img_ids: filename = f'{img_id}.jpg' xml_path = osp.join(self.img_prefix, 'Annotations', f'{img_id}.xml') tree = ET.parse(xml_path) root = tree.getroot() size = root.find('size') width = int(size.find('width').text) height = int(size.find('height').text) folder = root.find('folder').text data_infos.append( dict( id=img_id, filename=osp.join(folder, filename), width=width, height=height)) return data_infos
# Copyright (c) OpenMMLab. All rights reserved. import os.path as osp import xml.etree.ElementTree as ET from mmengine.fileio import list_from_file from mmdet.registry import DATASETS from .xml_style import XMLDataset @DATASETS.register_module() class WIDERFaceDataset(XMLDataset): """Reader for the WIDER Face dataset in PASCAL VOC format. Conversion scripts can be found in https://github.com/sovrasov/wider-face-pascal-voc-annotations """ CLASSES = ('face', ) PALETTE = [(0, 255, 0)] def __init__(self, **kwargs): super(WIDERFaceDataset, self).__init__(**kwargs) def load_annotations(self, ann_file): """Load annotation from WIDERFace XML style annotation file. Args: ann_file (str): Path of XML file. Returns: list[dict]: Annotation info from XML file. """ data_infos = [] img_ids = list_from_file(ann_file) for img_id in img_ids: filename = f'{img_id}.jpg' xml_path = osp.join(self.img_prefix, 'Annotations', f'{img_id}.xml') tree = ET.parse(xml_path) root = tree.getroot() size = root.find('size') width = int(size.find('width').text) height = int(size.find('height').text) folder = root.find('folder').text data_infos.append( dict( id=img_id, filename=osp.join(folder, filename), width=width, height=height)) return data_infos
from typing import TYPE_CHECKING, List, Union from docarray.array.abstract_array import AbstractDocumentArray from docarray.document import BaseDocument if TYPE_CHECKING: from docarray.typing import NdArray, TorchTensor class GetAttributeArrayMixin(AbstractDocumentArray): """Helpers that provide attributes getter in bulk""" def _get_array_attribute( self, field: str, ) -> Union[List, AbstractDocumentArray, 'TorchTensor', 'NdArray']: """Return all values of the fields from all docs this array contains :param field: name of the fields to extract :return: Returns a list of the field value for each document in the array like container """ field_type = self.__class__.document_type._get_nested_document_class(field) if ( self.is_stacked() and field in self._column_fields() and self._columns is not None ): attributes = self._columns[field] if attributes is not None: return attributes else: raise ValueError( f'The DocumentArray is in stacked mode, but no stacked data is ' f'present for {field}. This is inconsistent' ) elif issubclass(field_type, BaseDocument): # calling __class_getitem__ ourselves is a hack otherwise mypy complain # most likely a bug in mypy though # bug reported here https://github.com/python/mypy/issues/14111 return self.__class__.__class_getitem__(field_type)( (getattr(doc, field) for doc in self) ) else: return [getattr(doc, field) for doc in self] def _set_array_attribute( self, field: str, values: Union[List, AbstractDocumentArray, 'TorchTensor', 'NdArray'], ): """Set all Documents in this DocumentArray using the passed values :param field: name of the fields to extract :values: the values to set at the DocumentArray level """ if ( self.is_stacked() and self._columns is not None and field in self._column_fields() and not isinstance(values, List) ): self._columns[field] = values else: for doc, value in zip(self, values): setattr(doc, field, value)
from typing import List, Union from docarray.array.abstract_array import AbstractDocumentArray from docarray.document import BaseDocument class GetAttributeArrayMixin(AbstractDocumentArray): """Helpers that provide attributes getter in bulk""" def _get_documents_attribute( self, field: str ) -> Union[List, AbstractDocumentArray]: """Return all values of the fields from all docs this array contains :param field: name of the fields to extract :return: Returns a list of the field value for each document in the array like container """ field_type = self.__class__.document_type._get_nested_document_class(field) if issubclass(field_type, BaseDocument): # calling __class_getitem__ ourselves is a hack otherwise mypy complain # most likely a bug in mypy though # bug reported here https://github.com/python/mypy/issues/14111 return self.__class__.__class_getitem__(field_type)( (getattr(doc, field) for doc in self) ) else: return [getattr(doc, field) for doc in self]
import os.path from pathlib import Path from typing import Callable, Optional, Union import numpy as np import torch from torchvision.datasets.utils import download_url, verify_str_arg from torchvision.datasets.vision import VisionDataset class MovingMNIST(VisionDataset): """`MovingMNIST <http://www.cs.toronto.edu/~nitish/unsupervised_video/>`_ Dataset. Args: root (str or ``pathlib.Path``): Root directory of dataset where ``MovingMNIST/mnist_test_seq.npy`` exists. split (string, optional): The dataset split, supports ``None`` (default), ``"train"`` and ``"test"``. If ``split=None``, the full data is returned. split_ratio (int, optional): The split ratio of number of frames. If ``split="train"``, the first split frames ``data[:, :split_ratio]`` is returned. If ``split="test"``, the last split frames ``data[:, split_ratio:]`` is returned. If ``split=None``, this parameter is ignored and the all frames data is returned. download (bool, optional): If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again. transform (callable, optional): A function/transform that takes in a torch Tensor and returns a transformed version. E.g, ``transforms.RandomCrop`` """ _URL = "http://www.cs.toronto.edu/~nitish/unsupervised_video/mnist_test_seq.npy" def __init__( self, root: Union[str, Path], split: Optional[str] = None, split_ratio: int = 10, download: bool = False, transform: Optional[Callable] = None, ) -> None: super().__init__(root, transform=transform) self._base_folder = os.path.join(self.root, self.__class__.__name__) self._filename = self._URL.split("/")[-1] if split is not None: verify_str_arg(split, "split", ("train", "test")) self.split = split if not isinstance(split_ratio, int): raise TypeError(f"`split_ratio` should be an integer, but got {type(split_ratio)}") elif not (1 <= split_ratio <= 19): raise ValueError(f"`split_ratio` should be `1 <= split_ratio <= 19`, but got {split_ratio} instead.") self.split_ratio = split_ratio if download: self.download() if not self._check_exists(): raise RuntimeError("Dataset not found. You can use download=True to download it.") data = torch.from_numpy(np.load(os.path.join(self._base_folder, self._filename))) if self.split == "train": data = data[: self.split_ratio] elif self.split == "test": data = data[self.split_ratio :] self.data = data.transpose(0, 1).unsqueeze(2).contiguous() def __getitem__(self, idx: int) -> torch.Tensor: """ Args: idx (int): Index Returns: torch.Tensor: Video frames (torch Tensor[T, C, H, W]). The `T` is the number of frames. """ data = self.data[idx] if self.transform is not None: data = self.transform(data) return data def __len__(self) -> int: return len(self.data) def _check_exists(self) -> bool: return os.path.exists(os.path.join(self._base_folder, self._filename)) def download(self) -> None: if self._check_exists(): return download_url( url=self._URL, root=self._base_folder, filename=self._filename, md5="be083ec986bfe91a449d63653c411eb2", )
import os.path from pathlib import Path from typing import Callable, Optional, Union import numpy as np import torch from torchvision.datasets.utils import download_url, verify_str_arg from torchvision.datasets.vision import VisionDataset class MovingMNIST(VisionDataset): """`MovingMNIST <http://www.cs.toronto.edu/~nitish/unsupervised_video/>`_ Dataset. Args: root (str or ``pathlib.Path``): Root directory of dataset where ``MovingMNIST/mnist_test_seq.npy`` exists. split (string, optional): The dataset split, supports ``None`` (default), ``"train"`` and ``"test"``. If ``split=None``, the full data is returned. split_ratio (int, optional): The split ratio of number of frames. If ``split="train"``, the first split frames ``data[:, :split_ratio]`` is returned. If ``split="test"``, the last split frames ``data[:, split_ratio:]`` is returned. If ``split=None``, this parameter is ignored and the all frames data is returned. transform (callable, optional): A function/transform that takes in a torch Tensor and returns a transformed version. E.g, ``transforms.RandomCrop`` download (bool, optional): If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again. """ _URL = "http://www.cs.toronto.edu/~nitish/unsupervised_video/mnist_test_seq.npy" def __init__( self, root: Union[str, Path], split: Optional[str] = None, split_ratio: int = 10, download: bool = False, transform: Optional[Callable] = None, ) -> None: super().__init__(root, transform=transform) self._base_folder = os.path.join(self.root, self.__class__.__name__) self._filename = self._URL.split("/")[-1] if split is not None: verify_str_arg(split, "split", ("train", "test")) self.split = split if not isinstance(split_ratio, int): raise TypeError(f"`split_ratio` should be an integer, but got {type(split_ratio)}") elif not (1 <= split_ratio <= 19): raise ValueError(f"`split_ratio` should be `1 <= split_ratio <= 19`, but got {split_ratio} instead.") self.split_ratio = split_ratio if download: self.download() if not self._check_exists(): raise RuntimeError("Dataset not found. You can use download=True to download it.") data = torch.from_numpy(np.load(os.path.join(self._base_folder, self._filename))) if self.split == "train": data = data[: self.split_ratio] elif self.split == "test": data = data[self.split_ratio :] self.data = data.transpose(0, 1).unsqueeze(2).contiguous() def __getitem__(self, idx: int) -> torch.Tensor: """ Args: idx (int): Index Returns: torch.Tensor: Video frames (torch Tensor[T, C, H, W]). The `T` is the number of frames. """ data = self.data[idx] if self.transform is not None: data = self.transform(data) return data def __len__(self) -> int: return len(self.data) def _check_exists(self) -> bool: return os.path.exists(os.path.join(self._base_folder, self._filename)) def download(self) -> None: if self._check_exists(): return download_url( url=self._URL, root=self._base_folder, filename=self._filename, md5="be083ec986bfe91a449d63653c411eb2", )
from typing import List, Sequence from llama_index.core.agent.workflow.base_agent import BaseWorkflowAgent from llama_index.core.agent.workflow.single_agent_workflow import SingleAgentRunnerMixin from llama_index.core.agent.workflow.workflow_events import ( AgentInput, AgentOutput, AgentStream, ToolCallResult, ) from llama_index.core.base.llms.types import ChatResponse from llama_index.core.bridge.pydantic import BaseModel from llama_index.core.llms import ChatMessage from llama_index.core.memory import BaseMemory from llama_index.core.tools import AsyncBaseTool from llama_index.core.workflow import Context class FunctionAgent(SingleAgentRunnerMixin, BaseWorkflowAgent): """Function calling agent implementation.""" scratchpad_key: str = "scratchpad" async def take_step( self, ctx: Context, llm_input: List[ChatMessage], tools: Sequence[AsyncBaseTool], memory: BaseMemory, ) -> AgentOutput: """Take a single step with the function calling agent.""" if not self.llm.metadata.is_function_calling_model: raise ValueError("LLM must be a FunctionCallingLLM") scratchpad: List[ChatMessage] = await ctx.get(self.scratchpad_key, default=[]) current_llm_input = [*llm_input, *scratchpad] ctx.write_event_to_stream( AgentInput(input=current_llm_input, current_agent_name=self.name) ) response = await self.llm.astream_chat_with_tools( # type: ignore tools, chat_history=current_llm_input, allow_parallel_tool_calls=True ) # last_chat_response will be used later, after the loop. # We initialize it so it's valid even when 'response' is empty last_chat_response = ChatResponse(message=ChatMessage()) async for last_chat_response in response: tool_calls = self.llm.get_tool_calls_from_response( # type: ignore last_chat_response, error_on_no_tool_call=False ) raw = ( last_chat_response.raw.model_dump() if isinstance(last_chat_response.raw, BaseModel) else last_chat_response.raw ) ctx.write_event_to_stream( AgentStream( delta=last_chat_response.delta or "", response=last_chat_response.message.content or "", tool_calls=tool_calls or [], raw=raw, current_agent_name=self.name, ) ) tool_calls = self.llm.get_tool_calls_from_response( # type: ignore last_chat_response, error_on_no_tool_call=False ) # only add to scratchpad if we didn't select the handoff tool scratchpad.append(last_chat_response.message) await ctx.set(self.scratchpad_key, scratchpad) raw = ( last_chat_response.raw.model_dump() if isinstance(last_chat_response.raw, BaseModel) else last_chat_response.raw ) return AgentOutput( response=last_chat_response.message, tool_calls=tool_calls or [], raw=raw, current_agent_name=self.name, ) async def handle_tool_call_results( self, ctx: Context, results: List[ToolCallResult], memory: BaseMemory ) -> None: """Handle tool call results for function calling agent.""" scratchpad: List[ChatMessage] = await ctx.get(self.scratchpad_key, default=[]) for tool_call_result in results: scratchpad.append( ChatMessage( role="tool", content=str(tool_call_result.tool_output.content), additional_kwargs={"tool_call_id": tool_call_result.tool_id}, ) ) if ( tool_call_result.return_direct and tool_call_result.tool_name != "handoff" ): scratchpad.append( ChatMessage( role="assistant", content=str(tool_call_result.tool_output.content), additional_kwargs={"tool_call_id": tool_call_result.tool_id}, ) ) break await ctx.set(self.scratchpad_key, scratchpad) async def finalize( self, ctx: Context, output: AgentOutput, memory: BaseMemory ) -> AgentOutput: """Finalize the function calling agent. Adds all in-progress messages to memory. """ scratchpad: List[ChatMessage] = await ctx.get(self.scratchpad_key, default=[]) for msg in scratchpad: await memory.aput(msg) # reset scratchpad await ctx.set(self.scratchpad_key, []) return output
from typing import List, Sequence from llama_index.core.agent.workflow.base_agent import BaseWorkflowAgent from llama_index.core.agent.workflow.workflow_events import ( AgentInput, AgentOutput, AgentStream, ToolCallResult, ) from llama_index.core.base.llms.types import ChatResponse from llama_index.core.bridge.pydantic import BaseModel from llama_index.core.llms import ChatMessage from llama_index.core.memory import BaseMemory from llama_index.core.tools import AsyncBaseTool from llama_index.core.workflow import Context class FunctionAgent(BaseWorkflowAgent): """Function calling agent implementation.""" scratchpad_key: str = "scratchpad" async def take_step( self, ctx: Context, llm_input: List[ChatMessage], tools: Sequence[AsyncBaseTool], memory: BaseMemory, ) -> AgentOutput: """Take a single step with the function calling agent.""" if not self.llm.metadata.is_function_calling_model: raise ValueError("LLM must be a FunctionCallingLLM") scratchpad: List[ChatMessage] = await ctx.get(self.scratchpad_key, default=[]) current_llm_input = [*llm_input, *scratchpad] ctx.write_event_to_stream( AgentInput(input=current_llm_input, current_agent_name=self.name) ) response = await self.llm.astream_chat_with_tools( # type: ignore tools, chat_history=current_llm_input, allow_parallel_tool_calls=True ) # last_chat_response will be used later, after the loop. # We initialize it so it's valid even when 'response' is empty last_chat_response = ChatResponse(message=ChatMessage()) async for last_chat_response in response: tool_calls = self.llm.get_tool_calls_from_response( # type: ignore last_chat_response, error_on_no_tool_call=False ) raw = ( last_chat_response.raw.model_dump() if isinstance(last_chat_response.raw, BaseModel) else last_chat_response.raw ) ctx.write_event_to_stream( AgentStream( delta=last_chat_response.delta or "", response=last_chat_response.message.content or "", tool_calls=tool_calls or [], raw=raw, current_agent_name=self.name, ) ) tool_calls = self.llm.get_tool_calls_from_response( # type: ignore last_chat_response, error_on_no_tool_call=False ) # only add to scratchpad if we didn't select the handoff tool scratchpad.append(last_chat_response.message) await ctx.set(self.scratchpad_key, scratchpad) raw = ( last_chat_response.raw.model_dump() if isinstance(last_chat_response.raw, BaseModel) else last_chat_response.raw ) return AgentOutput( response=last_chat_response.message, tool_calls=tool_calls or [], raw=raw, current_agent_name=self.name, ) async def handle_tool_call_results( self, ctx: Context, results: List[ToolCallResult], memory: BaseMemory ) -> None: """Handle tool call results for function calling agent.""" scratchpad: List[ChatMessage] = await ctx.get(self.scratchpad_key, default=[]) for tool_call_result in results: scratchpad.append( ChatMessage( role="tool", content=str(tool_call_result.tool_output.content), additional_kwargs={"tool_call_id": tool_call_result.tool_id}, ) ) if ( tool_call_result.return_direct and tool_call_result.tool_name != "handoff" ): scratchpad.append( ChatMessage( role="assistant", content=str(tool_call_result.tool_output.content), additional_kwargs={"tool_call_id": tool_call_result.tool_id}, ) ) break await ctx.set(self.scratchpad_key, scratchpad) async def finalize( self, ctx: Context, output: AgentOutput, memory: BaseMemory ) -> AgentOutput: """Finalize the function calling agent. Adds all in-progress messages to memory. """ scratchpad: List[ChatMessage] = await ctx.get(self.scratchpad_key, default=[]) for msg in scratchpad: await memory.aput(msg) # reset scratchpad await ctx.set(self.scratchpad_key, []) return output
from __future__ import annotations from collections.abc import Iterable from typing import Any import torch from torch import Tensor, nn from sentence_transformers.SentenceTransformer import SentenceTransformer from sentence_transformers.util import fullname class CosineSimilarityLoss(nn.Module): def __init__( self, model: SentenceTransformer, loss_fct: nn.Module = nn.MSELoss(), cos_score_transformation: nn.Module = nn.Identity(), ) -> None: """ CosineSimilarityLoss expects that the InputExamples consists of two texts and a float label. It computes the vectors ``u = model(sentence_A)`` and ``v = model(sentence_B)`` and measures the cosine-similarity between the two. By default, it minimizes the following loss: ``||input_label - cos_score_transformation(cosine_sim(u,v))||_2``. Args: model: SentenceTransformer model loss_fct: Which pytorch loss function should be used to compare the ``cosine_similarity(u, v)`` with the input_label? By default, MSE is used: ``||input_label - cosine_sim(u, v)||_2`` cos_score_transformation: The cos_score_transformation function is applied on top of cosine_similarity. By default, the identify function is used (i.e. no change). References: - `Training Examples > Semantic Textual Similarity <../../../examples/sentence_transformer/training/sts/README.html>`_ Requirements: 1. Sentence pairs with corresponding similarity scores in range `[0, 1]` Inputs: +--------------------------------+------------------------+ | Texts | Labels | +================================+========================+ | (sentence_A, sentence_B) pairs | float similarity score | +--------------------------------+------------------------+ Relations: - :class:`CoSENTLoss` seems to produce a stronger training signal than CosineSimilarityLoss. In our experiments, CoSENTLoss is recommended. - :class:`AnglELoss` is :class:`CoSENTLoss` with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``. It also produces a stronger training signal than CosineSimilarityLoss. Example: :: from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses from datasets import Dataset model = SentenceTransformer("microsoft/mpnet-base") train_dataset = Dataset.from_dict({ "sentence1": ["It's nice weather outside today.", "He drove to work."], "sentence2": ["It's so sunny.", "She walked to the store."], "score": [1.0, 0.3], }) loss = losses.CosineSimilarityLoss(model) trainer = SentenceTransformerTrainer( model=model, train_dataset=train_dataset, loss=loss, ) trainer.train() """ super().__init__() self.model = model self.loss_fct = loss_fct self.cos_score_transformation = cos_score_transformation def forward(self, sentence_features: Iterable[dict[str, Tensor]], labels: Tensor) -> Tensor: embeddings = [self.model(sentence_feature)["sentence_embedding"] for sentence_feature in sentence_features] return self.compute_loss_from_embeddings(embeddings, labels) def compute_loss_from_embeddings(self, embeddings: list[Tensor], labels: Tensor) -> Tensor: """ Compute the CosineSimilarity loss from embeddings. Args: embeddings: List of embeddings labels: Labels indicating the similarity scores of the pairs Returns: Loss value """ output = self.cos_score_transformation(torch.cosine_similarity(embeddings[0], embeddings[1])) return self.loss_fct(output, labels.float().view(-1)) def get_config_dict(self) -> dict[str, Any]: return {"loss_fct": fullname(self.loss_fct)}
from __future__ import annotations from collections.abc import Iterable from typing import Any import torch from torch import Tensor, nn from sentence_transformers.SentenceTransformer import SentenceTransformer from sentence_transformers.util import fullname class CosineSimilarityLoss(nn.Module): def __init__( self, model: SentenceTransformer, loss_fct: nn.Module = nn.MSELoss(), cos_score_transformation: nn.Module = nn.Identity(), ) -> None: """ CosineSimilarityLoss expects that the InputExamples consists of two texts and a float label. It computes the vectors ``u = model(sentence_A)`` and ``v = model(sentence_B)`` and measures the cosine-similarity between the two. By default, it minimizes the following loss: ``||input_label - cos_score_transformation(cosine_sim(u,v))||_2``. Args: model: SentenceTransformer model loss_fct: Which pytorch loss function should be used to compare the ``cosine_similarity(u, v)`` with the input_label? By default, MSE is used: ``||input_label - cosine_sim(u, v)||_2`` cos_score_transformation: The cos_score_transformation function is applied on top of cosine_similarity. By default, the identify function is used (i.e. no change). References: - `Training Examples > Semantic Textual Similarity <../../../examples/sentence_transformer/training/sts/README.html>`_ Requirements: 1. Sentence pairs with corresponding similarity scores in range `[0, 1]` Inputs: +--------------------------------+------------------------+ | Texts | Labels | +================================+========================+ | (sentence_A, sentence_B) pairs | float similarity score | +--------------------------------+------------------------+ Relations: - :class:`CoSENTLoss` seems to produce a stronger training signal than CosineSimilarityLoss. In our experiments, CoSENTLoss is recommended. - :class:`AnglELoss` is :class:`CoSENTLoss` with ``pairwise_angle_sim`` as the metric, rather than ``pairwise_cos_sim``. It also produces a stronger training signal than CosineSimilarityLoss. Example: :: from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses from datasets import Dataset model = SentenceTransformer("microsoft/mpnet-base") train_dataset = Dataset.from_dict({ "sentence1": ["It's nice weather outside today.", "He drove to work."], "sentence2": ["It's so sunny.", "She walked to the store."], "score": [1.0, 0.3], }) loss = losses.CosineSimilarityLoss(model) trainer = SentenceTransformerTrainer( model=model, train_dataset=train_dataset, loss=loss, ) trainer.train() """ super().__init__() self.model = model self.loss_fct = loss_fct self.cos_score_transformation = cos_score_transformation def forward(self, sentence_features: Iterable[dict[str, Tensor]], labels: Tensor) -> Tensor: embeddings = [self.model(sentence_feature)["sentence_embedding"] for sentence_feature in sentence_features] output = self.cos_score_transformation(torch.cosine_similarity(embeddings[0], embeddings[1])) return self.loss_fct(output, labels.float().view(-1)) def get_config_dict(self) -> dict[str, Any]: return {"loss_fct": fullname(self.loss_fct)}
# Copyright (c) OpenMMLab. All rights reserved. from mmdet.core import ConfigType, OptConfigType, OptMultiConfig from mmdet.registry import MODELS from .single_stage import SingleStageDetector @MODELS.register_module() class VFNet(SingleStageDetector): """Implementation of `VarifocalNet (VFNet).<https://arxiv.org/abs/2008.13367>`_ Args: backbone (:obj:`ConfigDict` or dict): The backbone module. neck (:obj:`ConfigDict` or dict): The neck module. bbox_head (:obj:`ConfigDict` or dict): The bbox head module. train_cfg (:obj:`ConfigDict` or dict, optional): The training config of VFNet. Defaults to None. test_cfg (:obj:`ConfigDict` or dict, optional): The testing config of VFNet. Defaults to None. data_preprocessor (:obj:`ConfigDict` or dict, optional): Config of :class:`DetDataPreprocessor` to process the input data. Defaults to None. init_cfg (:obj:`ConfigDict` or dict, optional): the config to control the initialization. Defaults to None. """ def __init__(self, backbone: ConfigType, neck: ConfigType, bbox_head: ConfigType, train_cfg: OptConfigType = None, test_cfg: OptConfigType = None, data_preprocessor: OptConfigType = None, init_cfg: OptMultiConfig = None) -> None: super().__init__( backbone=backbone, neck=neck, bbox_head=bbox_head, train_cfg=train_cfg, test_cfg=test_cfg, data_preprocessor=data_preprocessor, init_cfg=init_cfg)
# Copyright (c) OpenMMLab. All rights reserved. from mmdet.registry import MODELS from .single_stage import SingleStageDetector @MODELS.register_module() class VFNet(SingleStageDetector): """Implementation of `VarifocalNet (VFNet).<https://arxiv.org/abs/2008.13367>`_""" def __init__(self, backbone, neck, bbox_head, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(VFNet, self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg)
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] # model settings norm_cfg = dict(type='GN', num_groups=32, requires_grad=True) model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( _delete_=True, type='SABLRetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, approx_anchor_generator=dict( type='AnchorGenerator', octave_base_scale=4, scales_per_octave=3, ratios=[0.5, 1.0, 2.0], strides=[8, 16, 32, 64, 128]), square_anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], scales=[4], strides=[8, 16, 32, 64, 128]), norm_cfg=norm_cfg, bbox_coder=dict( type='BucketingBBoxCoder', num_buckets=14, scale_factor=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.5), loss_bbox_reg=dict( type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.5)), # training and testing settings train_cfg=dict( assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False)) # dataset settings train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=[(1333, 480), (1333, 800)], resize_cfg=dict(type='Resize', keep_ratio=True)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline)) # optimizer optim_wrapper = dict( optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001))
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] # model settings norm_cfg = dict(type='GN', num_groups=32, requires_grad=True) model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( _delete_=True, type='SABLRetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, approx_anchor_generator=dict( type='AnchorGenerator', octave_base_scale=4, scales_per_octave=3, ratios=[0.5, 1.0, 2.0], strides=[8, 16, 32, 64, 128]), square_anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], scales=[4], strides=[8, 16, 32, 64, 128]), norm_cfg=norm_cfg, bbox_coder=dict( type='BucketingBBoxCoder', num_buckets=14, scale_factor=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.5), loss_bbox_reg=dict( type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.5)), # training and testing settings train_cfg=dict( assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False)) img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='Resize', img_scale=[(1333, 640), (1333, 800)], multiscale_mode='range', keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']), ] data = dict(train=dict(pipeline=train_pipeline)) # optimizer optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)
# Copyright (c) Meta Platforms, Inc. and affiliates. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. import fire from llama import Llama def main( ckpt_dir: str, tokenizer_path: str, temperature: float = 0.6, top_p: float = 0.9, max_seq_len: int = 128, max_gen_len: int = 64, max_batch_size: int = 4, ): """ Entry point of the program for generating text using a pretrained model. Args: ckpt_dir (str): The directory containing checkpoint files for the pretrained model. tokenizer_path (str): The path to the tokenizer model used for text encoding/decoding. temperature (float, optional): The temperature value for controlling randomness in generation. Defaults to 0.6. top_p (float, optional): The top-p sampling parameter for controlling diversity in generation. Defaults to 0.9. max_seq_len (int, optional): The maximum sequence length for input prompts. Defaults to 128. max_gen_len (int, optional): The maximum length of generated sequences. Defaults to 64. max_batch_size (int, optional): The maximum batch size for generating sequences. Defaults to 4. """ generator = Llama.build( ckpt_dir=ckpt_dir, tokenizer_path=tokenizer_path, max_seq_len=max_seq_len, max_batch_size=max_batch_size, ) prompts = [ # For these prompts, the expected answer is the natural continuation of the prompt "I believe the meaning of life is", "Simply put, the theory of relativity states that ", """A brief message congratulating the team on the launch: Hi everyone, I just """, # Few shot prompt (providing a few examples before asking model to complete more); """Translate English to French: sea otter => loutre de mer peppermint => menthe poivrée plush girafe => girafe peluche cheese =>""", ] results = generator.text_completion( prompts, max_gen_len=max_gen_len, temperature=temperature, top_p=top_p, ) for prompt, result in zip(prompts, results): print(prompt) print(f"> {result['generation']}") print("\n==================================\n") if __name__ == "__main__": fire.Fire(main)
# Copyright (c) Meta Platforms, Inc. and affiliates. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. import fire from llama import Llama def main( ckpt_dir: str, tokenizer_path: str, temperature: float = 0.6, top_p: float = 0.9, max_seq_len: int = 128, max_gen_len: int = 64, max_batch_size: int = 4, ): generator = Llama.build( ckpt_dir=ckpt_dir, tokenizer_path=tokenizer_path, max_seq_len=max_seq_len, max_batch_size=max_batch_size, ) prompts = [ # For these prompts, the expected answer is the natural continuation of the prompt "I believe the meaning of life is", "Simply put, the theory of relativity states that ", """A brief message congratulating the team on the launch: Hi everyone, I just """, # Few shot prompt (providing a few examples before asking model to complete more); """Translate English to French: sea otter => loutre de mer peppermint => menthe poivrée plush girafe => girafe peluche cheese =>""", ] results = generator.text_completion( prompts, max_gen_len=max_gen_len, temperature=temperature, top_p=top_p, ) for prompt, result in zip(prompts, results): print(prompt) print(f"> {result['generation']}") print("\n==================================\n") if __name__ == "__main__": fire.Fire(main)
import datetime from typing import List import prisma.enums import pydantic class Pagination(pydantic.BaseModel): total_items: int = pydantic.Field( description="Total number of items.", examples=[42] ) total_pages: int = pydantic.Field( description="Total number of pages.", examples=[97] ) current_page: int = pydantic.Field( description="Current_page page number.", examples=[1] ) page_size: int = pydantic.Field( description="Number of items per page.", examples=[25] ) class MyAgent(pydantic.BaseModel): agent_id: str agent_version: int agent_name: str description: str last_edited: datetime.datetime class MyAgentsResponse(pydantic.BaseModel): agents: list[MyAgent] pagination: Pagination class StoreAgent(pydantic.BaseModel): slug: str agent_name: str agent_image: str creator: str creator_avatar: str sub_heading: str description: str runs: int rating: float class StoreAgentsResponse(pydantic.BaseModel): agents: list[StoreAgent] pagination: Pagination class StoreAgentDetails(pydantic.BaseModel): store_listing_version_id: str slug: str agent_name: str agent_video: str agent_image: list[str] creator: str creator_avatar: str sub_heading: str description: str categories: list[str] runs: int rating: float versions: list[str] last_updated: datetime.datetime class Creator(pydantic.BaseModel): name: str username: str description: str avatar_url: str num_agents: int agent_rating: float agent_runs: int is_featured: bool class CreatorsResponse(pydantic.BaseModel): creators: List[Creator] pagination: Pagination class CreatorDetails(pydantic.BaseModel): name: str username: str description: str links: list[str] avatar_url: str agent_rating: float agent_runs: int top_categories: list[str] class Profile(pydantic.BaseModel): name: str username: str description: str links: list[str] avatar_url: str is_featured: bool = False class StoreSubmission(pydantic.BaseModel): agent_id: str agent_version: int name: str sub_heading: str slug: str description: str image_urls: list[str] date_submitted: datetime.datetime status: prisma.enums.SubmissionStatus runs: int rating: float class StoreSubmissionsResponse(pydantic.BaseModel): submissions: list[StoreSubmission] pagination: Pagination class StoreSubmissionRequest(pydantic.BaseModel): agent_id: str agent_version: int slug: str name: str sub_heading: str video_url: str | None = None image_urls: list[str] = [] description: str = "" categories: list[str] = [] class ProfileDetails(pydantic.BaseModel): name: str username: str description: str links: list[str] avatar_url: str | None = None class StoreReview(pydantic.BaseModel): score: int comments: str | None = None class StoreReviewCreate(pydantic.BaseModel): store_listing_version_id: str score: int comments: str | None = None
import datetime from typing import List import prisma.enums import pydantic class Pagination(pydantic.BaseModel): total_items: int = pydantic.Field( description="Total number of items.", examples=[42] ) total_pages: int = pydantic.Field( description="Total number of pages.", examples=[97] ) current_page: int = pydantic.Field( description="Current_page page number.", examples=[1] ) page_size: int = pydantic.Field( description="Number of items per page.", examples=[25] ) class MyAgent(pydantic.BaseModel): agent_id: str agent_version: int agent_name: str description: str last_edited: datetime.datetime class MyAgentsResponse(pydantic.BaseModel): agents: list[MyAgent] pagination: Pagination class StoreAgent(pydantic.BaseModel): slug: str agent_name: str agent_image: str creator: str creator_avatar: str sub_heading: str description: str runs: int rating: float class StoreAgentsResponse(pydantic.BaseModel): agents: list[StoreAgent] pagination: Pagination class StoreAgentDetails(pydantic.BaseModel): store_listing_version_id: str slug: str agent_name: str agent_video: str agent_image: list[str] creator: str creator_avatar: str sub_heading: str description: str categories: list[str] runs: int rating: float versions: list[str] last_updated: datetime.datetime class Creator(pydantic.BaseModel): name: str username: str description: str avatar_url: str num_agents: int agent_rating: float agent_runs: int is_featured: bool class CreatorsResponse(pydantic.BaseModel): creators: List[Creator] pagination: Pagination class CreatorDetails(pydantic.BaseModel): name: str username: str description: str links: list[str] avatar_url: str agent_rating: float agent_runs: int top_categories: list[str] class Profile(pydantic.BaseModel): name: str username: str description: str links: list[str] avatar_url: str is_featured: bool = False class StoreSubmission(pydantic.BaseModel): agent_id: str agent_version: int name: str sub_heading: str slug: str description: str image_urls: list[str] date_submitted: datetime.datetime status: prisma.enums.SubmissionStatus runs: int rating: float store_listing_version_id: str | None = None class StoreSubmissionsResponse(pydantic.BaseModel): submissions: list[StoreSubmission] pagination: Pagination class StoreSubmissionRequest(pydantic.BaseModel): agent_id: str agent_version: int slug: str name: str sub_heading: str video_url: str | None = None image_urls: list[str] = [] description: str = "" categories: list[str] = [] class ProfileDetails(pydantic.BaseModel): name: str username: str description: str links: list[str] avatar_url: str | None = None class StoreReview(pydantic.BaseModel): score: int comments: str | None = None class StoreReviewCreate(pydantic.BaseModel): store_listing_version_id: str score: int comments: str | None = None class ReviewSubmissionRequest(pydantic.BaseModel): store_listing_version_id: str isApproved: bool comments: str
# Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause from .glm import ( GammaRegressor, PoissonRegressor, TweedieRegressor, _GeneralizedLinearRegressor, ) __all__ = [ "GammaRegressor", "PoissonRegressor", "TweedieRegressor", "_GeneralizedLinearRegressor", ]
# Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause from .glm import ( GammaRegressor, PoissonRegressor, TweedieRegressor, _GeneralizedLinearRegressor, ) __all__ = [ "_GeneralizedLinearRegressor", "PoissonRegressor", "GammaRegressor", "TweedieRegressor", ]
import os import re from typing import Type from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField class BlockInstallationBlock(Block): """ This block allows the verification and installation of other blocks in the system. NOTE: This block allows remote code execution on the server, and it should be used for development purposes only. """ class Input(BlockSchema): code: str = SchemaField( description="Python code of the block to be installed", ) class Output(BlockSchema): success: str = SchemaField( description="Success message if the block is installed successfully", ) error: str = SchemaField( description="Error message if the block installation fails", ) def __init__(self): super().__init__( id="45e78db5-03e9-447f-9395-308d712f5f08", description="Given a code string, this block allows the verification and installation of a block code into the system.", categories={BlockCategory.BASIC}, input_schema=BlockInstallationBlock.Input, output_schema=BlockInstallationBlock.Output, disabled=True, ) async def run(self, input_data: Input, **kwargs) -> BlockOutput: code = input_data.code if search := re.search(r"class (\w+)\(Block\):", code): class_name = search.group(1) else: raise RuntimeError("No class found in the code.") if search := re.search(r"id=\"(\w+-\w+-\w+-\w+-\w+)\"", code): file_name = search.group(1) else: raise RuntimeError("No UUID found in the code.") block_dir = os.path.dirname(__file__) file_path = f"{block_dir}/{file_name}.py" module_name = f"backend.blocks.{file_name}" with open(file_path, "w") as f: f.write(code) try: module = __import__(module_name, fromlist=[class_name]) block_class: Type[Block] = getattr(module, class_name) block = block_class() from backend.util.test import execute_block_test await execute_block_test(block) yield "success", "Block installed successfully." except Exception as e: os.remove(file_path) raise RuntimeError(f"[Code]\n{code}\n\n[Error]\n{str(e)}")
import os import re from typing import Type from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField class BlockInstallationBlock(Block): """ This block allows the verification and installation of other blocks in the system. NOTE: This block allows remote code execution on the server, and it should be used for development purposes only. """ class Input(BlockSchema): code: str = SchemaField( description="Python code of the block to be installed", ) class Output(BlockSchema): success: str = SchemaField( description="Success message if the block is installed successfully", ) error: str = SchemaField( description="Error message if the block installation fails", ) def __init__(self): super().__init__( id="45e78db5-03e9-447f-9395-308d712f5f08", description="Given a code string, this block allows the verification and installation of a block code into the system.", categories={BlockCategory.BASIC}, input_schema=BlockInstallationBlock.Input, output_schema=BlockInstallationBlock.Output, disabled=True, ) def run(self, input_data: Input, **kwargs) -> BlockOutput: code = input_data.code if search := re.search(r"class (\w+)\(Block\):", code): class_name = search.group(1) else: raise RuntimeError("No class found in the code.") if search := re.search(r"id=\"(\w+-\w+-\w+-\w+-\w+)\"", code): file_name = search.group(1) else: raise RuntimeError("No UUID found in the code.") block_dir = os.path.dirname(__file__) file_path = f"{block_dir}/{file_name}.py" module_name = f"backend.blocks.{file_name}" with open(file_path, "w") as f: f.write(code) try: module = __import__(module_name, fromlist=[class_name]) block_class: Type[Block] = getattr(module, class_name) block = block_class() from backend.util.test import execute_block_test execute_block_test(block) yield "success", "Block installed successfully." except Exception as e: os.remove(file_path) raise RuntimeError(f"[Code]\n{code}\n\n[Error]\n{str(e)}")
import importlib from base64 import b64encode import pytest from fastapi.testclient import TestClient from ...utils import needs_py39 @pytest.fixture( name="client", params=[ "tutorial006", "tutorial006_an", pytest.param("tutorial006_an_py39", marks=needs_py39), ], ) def get_client(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.security.{request.param}") client = TestClient(mod.app) return client def test_security_http_basic(client: TestClient): response = client.get("/users/me", auth=("john", "secret")) assert response.status_code == 200, response.text assert response.json() == {"username": "john", "password": "secret"} def test_security_http_basic_no_credentials(client: TestClient): response = client.get("/users/me") assert response.json() == {"detail": "Not authenticated"} assert response.status_code == 401, response.text assert response.headers["WWW-Authenticate"] == "Basic" def test_security_http_basic_invalid_credentials(client: TestClient): response = client.get( "/users/me", headers={"Authorization": "Basic notabase64token"} ) assert response.status_code == 401, response.text assert response.headers["WWW-Authenticate"] == "Basic" assert response.json() == {"detail": "Invalid authentication credentials"} def test_security_http_basic_non_basic_credentials(client: TestClient): payload = b64encode(b"johnsecret").decode("ascii") auth_header = f"Basic {payload}" response = client.get("/users/me", headers={"Authorization": auth_header}) assert response.status_code == 401, response.text assert response.headers["WWW-Authenticate"] == "Basic" assert response.json() == {"detail": "Invalid authentication credentials"} def test_openapi_schema(client: TestClient): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/users/me": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, "summary": "Read Current User", "operationId": "read_current_user_users_me_get", "security": [{"HTTPBasic": []}], } } }, "components": { "securitySchemes": {"HTTPBasic": {"type": "http", "scheme": "basic"}} }, }
from base64 import b64encode from fastapi.testclient import TestClient from docs_src.security.tutorial006 import app client = TestClient(app) def test_security_http_basic(): response = client.get("/users/me", auth=("john", "secret")) assert response.status_code == 200, response.text assert response.json() == {"username": "john", "password": "secret"} def test_security_http_basic_no_credentials(): response = client.get("/users/me") assert response.json() == {"detail": "Not authenticated"} assert response.status_code == 401, response.text assert response.headers["WWW-Authenticate"] == "Basic" def test_security_http_basic_invalid_credentials(): response = client.get( "/users/me", headers={"Authorization": "Basic notabase64token"} ) assert response.status_code == 401, response.text assert response.headers["WWW-Authenticate"] == "Basic" assert response.json() == {"detail": "Invalid authentication credentials"} def test_security_http_basic_non_basic_credentials(): payload = b64encode(b"johnsecret").decode("ascii") auth_header = f"Basic {payload}" response = client.get("/users/me", headers={"Authorization": auth_header}) assert response.status_code == 401, response.text assert response.headers["WWW-Authenticate"] == "Basic" assert response.json() == {"detail": "Invalid authentication credentials"} def test_openapi_schema(): response = client.get("/openapi.json") assert response.status_code == 200, response.text assert response.json() == { "openapi": "3.1.0", "info": {"title": "FastAPI", "version": "0.1.0"}, "paths": { "/users/me": { "get": { "responses": { "200": { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, } }, "summary": "Read Current User", "operationId": "read_current_user_users_me_get", "security": [{"HTTPBasic": []}], } } }, "components": { "securitySchemes": {"HTTPBasic": {"type": "http", "scheme": "basic"}} }, }
from __future__ import annotations from typing import Optional from ..common import ( DeviceOpOverrides, register_device_op_overrides, TritonScratchWorkspace, ) class XPUDeviceOpOverrides(DeviceOpOverrides): def import_get_raw_stream_as(self, name: str) -> str: return f"from torch._C import _xpu_getCurrentRawStream as {name}" def set_device(self, device_idx: int) -> str: return f"torch.xpu.set_device({device_idx})" def synchronize(self) -> str: return "torch.xpu.synchronize()" def device_guard(self, device_idx: int) -> str: return f"torch.xpu._DeviceGuard({device_idx})" def cpp_device_guard(self) -> str: return "at::DeviceGuard" def cpp_aoti_device_guard(self) -> str: return "AOTIXpuGuard" def cpp_stream_guard(self) -> str: return "at::xpu::XPUStreamGuard" def cpp_aoti_stream_guard(self) -> str: return "AOTIXpuStreamGuard" def cpp_getStreamFromExternal(self) -> str: return "at::xpu::getStreamFromExternal" def kernel_header(self) -> str: source_codes = """ #include <torch/csrc/inductor/aoti_runtime/sycl_runtime_wrappers.h> """ return source_codes def kernel_driver(self) -> str: return "" def cpp_stream_type(self) -> str: return "sycl::queue*" def aoti_get_stream(self) -> str: return "aoti_torch_get_current_xpu_stream" def cpp_kernel_type(self) -> str: return "std::unique_ptr<sycl::kernel>" def cpp_device_ptr(self) -> str: return "void *" def cpp_global_scratch( self, idx: int, workspace: TritonScratchWorkspace ) -> Optional[tuple[list[str], str]]: return None register_device_op_overrides("xpu", XPUDeviceOpOverrides())
from __future__ import annotations from typing import Optional from ..common import DeviceOpOverrides, register_device_op_overrides class XPUDeviceOpOverrides(DeviceOpOverrides): def import_get_raw_stream_as(self, name: str) -> str: return f"from torch._C import _xpu_getCurrentRawStream as {name}" def set_device(self, device_idx: int) -> str: return f"torch.xpu.set_device({device_idx})" def synchronize(self) -> str: return "torch.xpu.synchronize()" def device_guard(self, device_idx: int) -> str: return f"torch.xpu._DeviceGuard({device_idx})" def cpp_device_guard(self) -> str: return "at::DeviceGuard" def cpp_aoti_device_guard(self) -> str: return "AOTIXpuGuard" def cpp_stream_guard(self) -> str: return "at::xpu::XPUStreamGuard" def cpp_aoti_stream_guard(self) -> str: return "AOTIXpuStreamGuard" def cpp_getStreamFromExternal(self) -> str: return "at::xpu::getStreamFromExternal" def kernel_header(self) -> str: source_codes = """ #include <torch/csrc/inductor/aoti_runtime/sycl_runtime_wrappers.h> """ return source_codes def kernel_driver(self) -> str: return "" def cpp_stream_type(self) -> str: return "sycl::queue*" def aoti_get_stream(self) -> str: return "aoti_torch_get_current_xpu_stream" def cpp_kernel_type(self) -> str: return "std::unique_ptr<sycl::kernel>" def cpp_device_ptr(self) -> str: return "void *" def cpp_global_scratch(self, idx: int) -> Optional[tuple[str, str]]: return None register_device_op_overrides("xpu", XPUDeviceOpOverrides())
import functools import hashlib from typing import Any @functools.cache def has_triton_package() -> bool: try: import triton # noqa: F401 return True except ImportError: return False @functools.cache def _device_supports_tma() -> bool: import torch return ( torch.cuda.is_available() and torch.cuda.get_device_capability() >= (9, 0) and not torch.version.hip ) @functools.cache def has_triton_experimental_host_tma() -> bool: if has_triton_package(): if _device_supports_tma(): try: from triton.tools.experimental_descriptor import ( # noqa: F401 create_1d_tma_descriptor, create_2d_tma_descriptor, ) return True except ImportError: pass return False @functools.cache def has_triton_tensor_descriptor_host_tma() -> bool: if has_triton_package(): if _device_supports_tma(): try: from triton.tools.tensor_descriptor import ( # noqa: F401 TensorDescriptor, ) return True except ImportError: pass return False @functools.cache def has_triton_tma() -> bool: return has_triton_tensor_descriptor_host_tma() or has_triton_experimental_host_tma() @functools.cache def has_triton_tma_device() -> bool: if has_triton_package(): import torch if ( torch.cuda.is_available() and torch.cuda.get_device_capability() >= (9, 0) and not torch.version.hip ): # old API try: from triton.language.extra.cuda import ( # noqa: F401 experimental_device_tensormap_create1d, experimental_device_tensormap_create2d, ) return True except ImportError: pass # new API try: from triton.language import make_tensor_descriptor # noqa: F401 return True except ImportError: pass return False @functools.lru_cache(None) def has_triton_stable_tma_api() -> bool: if has_triton_package(): import torch if ( torch.cuda.is_available() and torch.cuda.get_device_capability() >= (9, 0) and not torch.version.hip ): try: from triton.language import make_tensor_descriptor # noqa: F401 return True except ImportError: pass return False @functools.cache def has_triton() -> bool: if not has_triton_package(): return False from torch._dynamo.device_interface import get_interface_for_device def cuda_extra_check(device_interface: Any) -> bool: return device_interface.Worker.get_device_properties().major >= 7 def cpu_extra_check(device_interface: Any) -> bool: import triton.backends return "cpu" in triton.backends.backends def _return_true(device_interface: Any) -> bool: return True triton_supported_devices = { "cuda": cuda_extra_check, "xpu": _return_true, "cpu": cpu_extra_check, } def is_device_compatible_with_triton() -> bool: for device, extra_check in triton_supported_devices.items(): device_interface = get_interface_for_device(device) if device_interface.is_available() and extra_check(device_interface): return True return False return is_device_compatible_with_triton() @functools.cache def triton_backend() -> Any: from triton.compiler.compiler import make_backend from triton.runtime.driver import driver target = driver.active.get_current_target() return make_backend(target) @functools.cache def triton_hash_with_backend() -> str: from torch._inductor.runtime.triton_compat import triton_key backend = triton_backend() key = f"{triton_key()}-{backend.hash()}" # Hash is upper case so that it can't contain any Python keywords. return hashlib.sha256(key.encode("utf-8")).hexdigest().upper()
import functools import hashlib from typing import Any @functools.cache def has_triton_package() -> bool: try: from triton.compiler.compiler import triton_key return triton_key is not None except ImportError: return False except RuntimeError: return False @functools.cache def _device_supports_tma() -> bool: import torch return ( torch.cuda.is_available() and torch.cuda.get_device_capability() >= (9, 0) and not torch.version.hip ) @functools.cache def has_triton_experimental_host_tma() -> bool: if has_triton_package(): if _device_supports_tma(): try: from triton.tools.experimental_descriptor import ( # noqa: F401 create_1d_tma_descriptor, create_2d_tma_descriptor, ) return True except ImportError: pass return False @functools.cache def has_triton_tensor_descriptor_host_tma() -> bool: if has_triton_package(): if _device_supports_tma(): try: from triton.tools.tensor_descriptor import ( # noqa: F401 TensorDescriptor, ) return True except ImportError: pass return False @functools.cache def has_triton_tma() -> bool: return has_triton_tensor_descriptor_host_tma() or has_triton_experimental_host_tma() @functools.cache def has_triton_tma_device() -> bool: if has_triton_package(): import torch if ( torch.cuda.is_available() and torch.cuda.get_device_capability() >= (9, 0) and not torch.version.hip ): # old API try: from triton.language.extra.cuda import ( # noqa: F401 experimental_device_tensormap_create1d, experimental_device_tensormap_create2d, ) return True except ImportError: pass # new API try: from triton.language import make_tensor_descriptor # noqa: F401 return True except ImportError: pass return False @functools.lru_cache(None) def has_triton_stable_tma_api() -> bool: if has_triton_package(): import torch if ( torch.cuda.is_available() and torch.cuda.get_device_capability() >= (9, 0) and not torch.version.hip ): try: from triton.language import make_tensor_descriptor # noqa: F401 return True except ImportError: pass return False @functools.cache def has_triton() -> bool: if not has_triton_package(): return False from torch._dynamo.device_interface import get_interface_for_device def cuda_extra_check(device_interface: Any) -> bool: return device_interface.Worker.get_device_properties().major >= 7 def cpu_extra_check(device_interface: Any) -> bool: import triton.backends return "cpu" in triton.backends.backends def _return_true(device_interface: Any) -> bool: return True triton_supported_devices = { "cuda": cuda_extra_check, "xpu": _return_true, "cpu": cpu_extra_check, } def is_device_compatible_with_triton() -> bool: for device, extra_check in triton_supported_devices.items(): device_interface = get_interface_for_device(device) if device_interface.is_available() and extra_check(device_interface): return True return False return is_device_compatible_with_triton() @functools.cache def triton_backend() -> Any: from triton.compiler.compiler import make_backend from triton.runtime.driver import driver target = driver.active.get_current_target() return make_backend(target) @functools.cache def triton_hash_with_backend() -> str: from triton.compiler.compiler import triton_key backend = triton_backend() key = f"{triton_key()}-{backend.hash()}" # Hash is upper case so that it can't contain any Python keywords. return hashlib.sha256(key.encode("utf-8")).hexdigest().upper()
import json import os import requests import sys import time from typing import Dict, List, Tuple CHECK_INTERVAL = 30 def get_environment_variables() -> Tuple[str, str, str, str, str]: """Retrieve and return necessary environment variables.""" try: with open(os.environ["GITHUB_EVENT_PATH"]) as f: event = json.load(f) # Handle both PR and merge group events if "pull_request" in event: sha = event["pull_request"]["head"]["sha"] else: sha = os.environ["GITHUB_SHA"] return ( os.environ["GITHUB_API_URL"], os.environ["GITHUB_REPOSITORY"], sha, os.environ["GITHUB_TOKEN"], os.environ["GITHUB_RUN_ID"], ) except KeyError as e: print(f"Error: Missing required environment variable or event data: {e}") sys.exit(1) def make_api_request(url: str, headers: Dict[str, str]) -> Dict: """Make an API request and return the JSON response.""" try: print("Making API request to:", url) response = requests.get(url, headers=headers, timeout=10) response.raise_for_status() return response.json() except requests.RequestException as e: print(f"Error: API request failed. {e}") sys.exit(1) def process_check_runs(check_runs: List[Dict]) -> Tuple[bool, bool]: """Process check runs and return their status.""" runs_in_progress = False all_others_passed = True for run in check_runs: if str(run["name"]) != "Check PR Status": status = run["status"] conclusion = run["conclusion"] if status == "completed": if conclusion not in ["success", "skipped", "neutral"]: all_others_passed = False print( f"Check run {run['name']} (ID: {run['id']}) has conclusion: {conclusion}" ) else: runs_in_progress = True print(f"Check run {run['name']} (ID: {run['id']}) is still {status}.") all_others_passed = False else: print( f"Skipping check run {run['name']} (ID: {run['id']}) as it is the current run." ) return runs_in_progress, all_others_passed def main(): api_url, repo, sha, github_token, current_run_id = get_environment_variables() endpoint = f"{api_url}/repos/{repo}/commits/{sha}/check-runs" headers = { "Accept": "application/vnd.github.v3+json", } if github_token: headers["Authorization"] = f"token {github_token}" print(f"Current run ID: {current_run_id}") while True: data = make_api_request(endpoint, headers) check_runs = data["check_runs"] print("Processing check runs...") print(check_runs) runs_in_progress, all_others_passed = process_check_runs(check_runs) if not runs_in_progress: break print( "Some check runs are still in progress. " f"Waiting {CHECK_INTERVAL} seconds before checking again..." ) time.sleep(CHECK_INTERVAL) if all_others_passed: print("All other completed check runs have passed. This check passes.") sys.exit(0) else: print("Some check runs have failed or have not completed. This check fails.") sys.exit(1) if __name__ == "__main__": main()
import json import os import requests import sys import time from typing import Dict, List, Tuple CHECK_INTERVAL = 30 def get_environment_variables() -> Tuple[str, str, str, str, str]: """Retrieve and return necessary environment variables.""" try: with open(os.environ["GITHUB_EVENT_PATH"]) as f: event = json.load(f) sha = event["pull_request"]["head"]["sha"] return ( os.environ["GITHUB_API_URL"], os.environ["GITHUB_REPOSITORY"], sha, os.environ["GITHUB_TOKEN"], os.environ["GITHUB_RUN_ID"], ) except KeyError as e: print(f"Error: Missing required environment variable or event data: {e}") sys.exit(1) def make_api_request(url: str, headers: Dict[str, str]) -> Dict: """Make an API request and return the JSON response.""" try: print("Making API request to:", url) response = requests.get(url, headers=headers, timeout=10) response.raise_for_status() return response.json() except requests.RequestException as e: print(f"Error: API request failed. {e}") sys.exit(1) def process_check_runs(check_runs: List[Dict]) -> Tuple[bool, bool]: """Process check runs and return their status.""" runs_in_progress = False all_others_passed = True for run in check_runs: if str(run["name"]) != "Check PR Status": status = run["status"] conclusion = run["conclusion"] if status == "completed": if conclusion not in ["success", "skipped", "neutral"]: all_others_passed = False print( f"Check run {run['name']} (ID: {run['id']}) has conclusion: {conclusion}" ) else: runs_in_progress = True print(f"Check run {run['name']} (ID: {run['id']}) is still {status}.") all_others_passed = False else: print( f"Skipping check run {run['name']} (ID: {run['id']}) as it is the current run." ) return runs_in_progress, all_others_passed def main(): api_url, repo, sha, github_token, current_run_id = get_environment_variables() endpoint = f"{api_url}/repos/{repo}/commits/{sha}/check-runs" headers = { "Accept": "application/vnd.github.v3+json", } if github_token: headers["Authorization"] = f"token {github_token}" print(f"Current run ID: {current_run_id}") while True: data = make_api_request(endpoint, headers) check_runs = data["check_runs"] print("Processing check runs...") print(check_runs) runs_in_progress, all_others_passed = process_check_runs(check_runs) if not runs_in_progress: break print( "Some check runs are still in progress. " f"Waiting {CHECK_INTERVAL} seconds before checking again..." ) time.sleep(CHECK_INTERVAL) if all_others_passed: print("All other completed check runs have passed. This check passes.") sys.exit(0) else: print("Some check runs have failed or have not completed. This check fails.") sys.exit(1) if __name__ == "__main__": main()
# Copyright (c) OpenMMLab. All rights reserved. from .atss import ATSS from .autoassign import AutoAssign from .base import BaseDetector from .base_detr import DetectionTransformer from .boxinst import BoxInst from .cascade_rcnn import CascadeRCNN from .centernet import CenterNet from .condinst import CondInst from .conditional_detr import ConditionalDETR from .cornernet import CornerNet from .crowddet import CrowdDet from .d2_wrapper import Detectron2Wrapper from .dab_detr import DABDETR from .ddod import DDOD from .deformable_detr import DeformableDETR from .detr import DETR from .dino import DINO from .fast_rcnn import FastRCNN from .faster_rcnn import FasterRCNN from .fcos import FCOS from .fovea import FOVEA from .fsaf import FSAF from .gfl import GFL from .glip import GLIP from .grid_rcnn import GridRCNN from .htc import HybridTaskCascade from .kd_one_stage import KnowledgeDistillationSingleStageDetector from .lad import LAD from .mask2former import Mask2Former from .mask_rcnn import MaskRCNN from .mask_scoring_rcnn import MaskScoringRCNN from .maskformer import MaskFormer from .nasfcos import NASFCOS from .paa import PAA from .panoptic_fpn import PanopticFPN from .panoptic_two_stage_segmentor import TwoStagePanopticSegmentor from .point_rend import PointRend from .queryinst import QueryInst from .reppoints_detector import RepPointsDetector from .retinanet import RetinaNet from .rpn import RPN from .rtmdet import RTMDet from .scnet import SCNet from .semi_base import SemiBaseDetector from .single_stage import SingleStageDetector from .soft_teacher import SoftTeacher from .solo import SOLO from .solov2 import SOLOv2 from .sparse_rcnn import SparseRCNN from .tood import TOOD from .trident_faster_rcnn import TridentFasterRCNN from .two_stage import TwoStageDetector from .vfnet import VFNet from .yolact import YOLACT from .yolo import YOLOV3 from .yolof import YOLOF from .yolox import YOLOX __all__ = [ 'ATSS', 'BaseDetector', 'SingleStageDetector', 'TwoStageDetector', 'RPN', 'KnowledgeDistillationSingleStageDetector', 'FastRCNN', 'FasterRCNN', 'MaskRCNN', 'CascadeRCNN', 'HybridTaskCascade', 'RetinaNet', 'FCOS', 'GridRCNN', 'MaskScoringRCNN', 'RepPointsDetector', 'FOVEA', 'FSAF', 'NASFCOS', 'PointRend', 'GFL', 'CornerNet', 'PAA', 'YOLOV3', 'YOLACT', 'VFNet', 'DETR', 'TridentFasterRCNN', 'SparseRCNN', 'SCNet', 'SOLO', 'SOLOv2', 'DeformableDETR', 'AutoAssign', 'YOLOF', 'CenterNet', 'YOLOX', 'TwoStagePanopticSegmentor', 'PanopticFPN', 'QueryInst', 'LAD', 'TOOD', 'MaskFormer', 'DDOD', 'Mask2Former', 'SemiBaseDetector', 'SoftTeacher', 'RTMDet', 'Detectron2Wrapper', 'CrowdDet', 'CondInst', 'BoxInst', 'DetectionTransformer', 'ConditionalDETR', 'DINO', 'DABDETR', 'GLIP' ]
# Copyright (c) OpenMMLab. All rights reserved. from .atss import ATSS from .autoassign import AutoAssign from .base import BaseDetector from .base_detr import DetectionTransformer from .boxinst import BoxInst from .cascade_rcnn import CascadeRCNN from .centernet import CenterNet from .condinst import CondInst from .conditional_detr import ConditionalDETR from .cornernet import CornerNet from .crowddet import CrowdDet from .d2_wrapper import Detectron2Wrapper from .dab_detr import DABDETR from .ddod import DDOD from .deformable_detr import DeformableDETR from .detr import DETR from .dino import DINO from .fast_rcnn import FastRCNN from .faster_rcnn import FasterRCNN from .fcos import FCOS from .fovea import FOVEA from .fsaf import FSAF from .gfl import GFL from .grid_rcnn import GridRCNN from .htc import HybridTaskCascade from .kd_one_stage import KnowledgeDistillationSingleStageDetector from .lad import LAD from .mask2former import Mask2Former from .mask_rcnn import MaskRCNN from .mask_scoring_rcnn import MaskScoringRCNN from .maskformer import MaskFormer from .nasfcos import NASFCOS from .paa import PAA from .panoptic_fpn import PanopticFPN from .panoptic_two_stage_segmentor import TwoStagePanopticSegmentor from .point_rend import PointRend from .queryinst import QueryInst from .reppoints_detector import RepPointsDetector from .retinanet import RetinaNet from .rpn import RPN from .rtmdet import RTMDet from .scnet import SCNet from .semi_base import SemiBaseDetector from .single_stage import SingleStageDetector from .soft_teacher import SoftTeacher from .solo import SOLO from .solov2 import SOLOv2 from .sparse_rcnn import SparseRCNN from .tood import TOOD from .trident_faster_rcnn import TridentFasterRCNN from .two_stage import TwoStageDetector from .vfnet import VFNet from .yolact import YOLACT from .yolo import YOLOV3 from .yolof import YOLOF from .yolox import YOLOX __all__ = [ 'ATSS', 'BaseDetector', 'SingleStageDetector', 'TwoStageDetector', 'RPN', 'KnowledgeDistillationSingleStageDetector', 'FastRCNN', 'FasterRCNN', 'MaskRCNN', 'CascadeRCNN', 'HybridTaskCascade', 'RetinaNet', 'FCOS', 'GridRCNN', 'MaskScoringRCNN', 'RepPointsDetector', 'FOVEA', 'FSAF', 'NASFCOS', 'PointRend', 'GFL', 'CornerNet', 'PAA', 'YOLOV3', 'YOLACT', 'VFNet', 'DETR', 'TridentFasterRCNN', 'SparseRCNN', 'SCNet', 'SOLO', 'SOLOv2', 'DeformableDETR', 'AutoAssign', 'YOLOF', 'CenterNet', 'YOLOX', 'TwoStagePanopticSegmentor', 'PanopticFPN', 'QueryInst', 'LAD', 'TOOD', 'MaskFormer', 'DDOD', 'Mask2Former', 'SemiBaseDetector', 'SoftTeacher', 'RTMDet', 'Detectron2Wrapper', 'CrowdDet', 'CondInst', 'BoxInst', 'DetectionTransformer', 'ConditionalDETR', 'DINO', 'DABDETR' ]
from docarray.typing.proto_register import _register_proto from docarray.typing.tensor.audio.abstract_audio_tensor import AbstractAudioTensor from docarray.typing.tensor.ndarray import NdArray @_register_proto(proto_type_name='audio_ndarray') class AudioNdArray(AbstractAudioTensor, NdArray): """ Subclass of [`NdArray`][docarray.typing.NdArray], to represent an audio tensor. Adds audio-specific features to the tensor. --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioBytes, AudioNdArray, AudioUrl import numpy as np class MyAudioDoc(BaseDoc): title: str audio_tensor: Optional[AudioNdArray] url: Optional[AudioUrl] bytes_: Optional[AudioBytes] # from tensor doc_1 = MyAudioDoc( title='my_first_audio_doc', audio_tensor=np.random.rand(1000, 2), ) # doc_1.audio_tensor.save(file_path='/tmp/file_1.wav') doc_1.bytes_ = doc_1.audio_tensor.to_bytes() # from url doc_2 = MyAudioDoc( title='my_second_audio_doc', url='https://www.kozco.com/tech/piano2.wav', ) doc_2.audio_tensor, _ = doc_2.url.load() # doc_2.audio_tensor.save(file_path='/tmp/file_2.wav') doc_2.bytes_ = doc_1.audio_tensor.to_bytes() ``` --- """ ...
from docarray.typing.proto_register import _register_proto from docarray.typing.tensor.audio.abstract_audio_tensor import AbstractAudioTensor from docarray.typing.tensor.ndarray import NdArray @_register_proto(proto_type_name='audio_ndarray') class AudioNdArray(AbstractAudioTensor, NdArray): """ Subclass of NdArray, to represent an audio tensor. Adds audio-specific features to the tensor. --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioBytes, AudioNdArray, AudioUrl import numpy as np class MyAudioDoc(BaseDoc): title: str audio_tensor: Optional[AudioNdArray] url: Optional[AudioUrl] bytes_: Optional[AudioBytes] # from tensor doc_1 = MyAudioDoc( title='my_first_audio_doc', audio_tensor=np.random.rand(1000, 2), ) doc_1.audio_tensor.save(file_path='/tmp/file_1.wav') doc_1.bytes_ = doc_1.audio_tensor.to_bytes() # from url doc_2 = MyAudioDoc( title='my_second_audio_doc', url='https://www.kozco.com/tech/piano2.wav', ) doc_2.audio_tensor, _ = doc_2.url.load() doc_2.audio_tensor.save(file_path='/tmp/file_2.wav') doc_2.bytes_ = doc_1.audio_tensor.to_bytes() ``` --- """ ...
import os from pathlib import Path from typing import Any, Callable, Optional, Union from .folder import default_loader, ImageFolder from .utils import download_and_extract_archive class EuroSAT(ImageFolder): """RGB version of the `EuroSAT <https://github.com/phelber/eurosat>`_ Dataset. For the MS version of the dataset, see `TorchGeo <https://torchgeo.readthedocs.io/en/stable/api/datasets.html#eurosat>`__. Args: root (str or ``pathlib.Path``): Root directory of dataset where ``root/eurosat`` exists. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` target_transform (callable, optional): A function/transform that takes in the target and transforms it. download (bool, optional): If True, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again. Default is False. loader (callable, optional): A function to load an image given its path. By default, it uses PIL as its image loader, but users could also pass in ``torchvision.io.decode_image`` for decoding image data into tensors directly. """ def __init__( self, root: Union[str, Path], transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False, loader: Callable[[str], Any] = default_loader, ) -> None: self.root = os.path.expanduser(root) self._base_folder = os.path.join(self.root, "eurosat") self._data_folder = os.path.join(self._base_folder, "2750") if download: self.download() if not self._check_exists(): raise RuntimeError("Dataset not found. You can use download=True to download it") super().__init__( self._data_folder, transform=transform, target_transform=target_transform, loader=loader, ) self.root = os.path.expanduser(root) def __len__(self) -> int: return len(self.samples) def _check_exists(self) -> bool: return os.path.exists(self._data_folder) def download(self) -> None: if self._check_exists(): return os.makedirs(self._base_folder, exist_ok=True) download_and_extract_archive( "https://huggingface.co/datasets/torchgeo/eurosat/resolve/c877bcd43f099cd0196738f714544e355477f3fd/EuroSAT.zip", download_root=self._base_folder, md5="c8fa014336c82ac7804f0398fcb19387", )
import os from pathlib import Path from typing import Callable, Optional, Union from .folder import ImageFolder from .utils import download_and_extract_archive class EuroSAT(ImageFolder): """RGB version of the `EuroSAT <https://github.com/phelber/eurosat>`_ Dataset. For the MS version of the dataset, see `TorchGeo <https://torchgeo.readthedocs.io/en/stable/api/datasets.html#eurosat>`__. Args: root (str or ``pathlib.Path``): Root directory of dataset where ``root/eurosat`` exists. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop`` target_transform (callable, optional): A function/transform that takes in the target and transforms it. download (bool, optional): If True, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again. Default is False. """ def __init__( self, root: Union[str, Path], transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False, ) -> None: self.root = os.path.expanduser(root) self._base_folder = os.path.join(self.root, "eurosat") self._data_folder = os.path.join(self._base_folder, "2750") if download: self.download() if not self._check_exists(): raise RuntimeError("Dataset not found. You can use download=True to download it") super().__init__(self._data_folder, transform=transform, target_transform=target_transform) self.root = os.path.expanduser(root) def __len__(self) -> int: return len(self.samples) def _check_exists(self) -> bool: return os.path.exists(self._data_folder) def download(self) -> None: if self._check_exists(): return os.makedirs(self._base_folder, exist_ok=True) download_and_extract_archive( "https://huggingface.co/datasets/torchgeo/eurosat/resolve/c877bcd43f099cd0196738f714544e355477f3fd/EuroSAT.zip", download_root=self._base_folder, md5="c8fa014336c82ac7804f0398fcb19387", )
from langchain_core.embeddings import Embeddings from langchain_tests.unit_tests.embeddings import EmbeddingsTests class EmbeddingsIntegrationTests(EmbeddingsTests): """Base class for embeddings integration tests. Test subclasses must implement the ``embeddings_class`` property to specify the embeddings model to be tested. You can also override the ``embedding_model_params`` property to specify initialization parameters. Example: .. code-block:: python from typing import Type from langchain_tests.integration_tests import EmbeddingsIntegrationTests from my_package.embeddings import MyEmbeddingsModel class TestMyEmbeddingsModelIntegration(EmbeddingsIntegrationTests): @property def embeddings_class(self) -> Type[MyEmbeddingsModel]: # Return the embeddings model class to test here return MyEmbeddingsModel @property def embedding_model_params(self) -> dict: # Return initialization parameters for the model. return {"model": "model-001"} .. note:: API references for individual test methods include troubleshooting tips. """ def test_embed_query(self, model: Embeddings) -> None: """Test embedding a string query. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of floats when calling ``.embed_query`` on a string. 2. The length of the list is consistent across different inputs. """ # noqa: E501 embedding_1 = model.embed_query("foo") assert isinstance(embedding_1, list) assert isinstance(embedding_1[0], float) embedding_2 = model.embed_query("bar") assert len(embedding_1) > 0 assert len(embedding_1) == len(embedding_2) def test_embed_documents(self, model: Embeddings) -> None: """Test embedding a list of strings. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of lists of floats when calling ``.embed_documents`` on a list of strings. 2. The length of each list is the same. """ # noqa: E501 documents = ["foo", "bar", "baz"] embeddings = model.embed_documents(documents) assert len(embeddings) == len(documents) assert all(isinstance(embedding, list) for embedding in embeddings) assert all(isinstance(embedding[0], float) for embedding in embeddings) assert len(embeddings[0]) > 0 assert all(len(embedding) == len(embeddings[0]) for embedding in embeddings) async def test_aembed_query(self, model: Embeddings) -> None: """Test embedding a string query async. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of floats when calling ``.aembed_query`` on a string. 2. The length of the list is consistent across different inputs. """ # noqa: E501 embedding_1 = await model.aembed_query("foo") assert isinstance(embedding_1, list) assert isinstance(embedding_1[0], float) embedding_2 = await model.aembed_query("bar") assert len(embedding_1) > 0 assert len(embedding_1) == len(embedding_2) async def test_aembed_documents(self, model: Embeddings) -> None: """Test embedding a list of strings async. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of lists of floats when calling ``.aembed_documents`` on a list of strings. 2. The length of each list is the same. """ # noqa: E501 documents = ["foo", "bar", "baz"] embeddings = await model.aembed_documents(documents) assert len(embeddings) == len(documents) assert all(isinstance(embedding, list) for embedding in embeddings) assert all(isinstance(embedding[0], float) for embedding in embeddings) assert len(embeddings[0]) > 0 assert all(len(embedding) == len(embeddings[0]) for embedding in embeddings)
from typing import List from langchain_core.embeddings import Embeddings from langchain_tests.unit_tests.embeddings import EmbeddingsTests class EmbeddingsIntegrationTests(EmbeddingsTests): """Base class for embeddings integration tests. Test subclasses must implement the ``embeddings_class`` property to specify the embeddings model to be tested. You can also override the ``embedding_model_params`` property to specify initialization parameters. Example: .. code-block:: python from typing import Type from langchain_tests.integration_tests import EmbeddingsIntegrationTests from my_package.embeddings import MyEmbeddingsModel class TestMyEmbeddingsModelIntegration(EmbeddingsIntegrationTests): @property def embeddings_class(self) -> Type[MyEmbeddingsModel]: # Return the embeddings model class to test here return MyEmbeddingsModel @property def embedding_model_params(self) -> dict: # Return initialization parameters for the model. return {"model": "model-001"} .. note:: API references for individual test methods include troubleshooting tips. """ def test_embed_query(self, model: Embeddings) -> None: """Test embedding a string query. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of floats when calling ``.embed_query`` on a string. 2. The length of the list is consistent across different inputs. """ # noqa: E501 embedding_1 = model.embed_query("foo") assert isinstance(embedding_1, List) assert isinstance(embedding_1[0], float) embedding_2 = model.embed_query("bar") assert len(embedding_1) > 0 assert len(embedding_1) == len(embedding_2) def test_embed_documents(self, model: Embeddings) -> None: """Test embedding a list of strings. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of lists of floats when calling ``.embed_documents`` on a list of strings. 2. The length of each list is the same. """ # noqa: E501 documents = ["foo", "bar", "baz"] embeddings = model.embed_documents(documents) assert len(embeddings) == len(documents) assert all(isinstance(embedding, List) for embedding in embeddings) assert all(isinstance(embedding[0], float) for embedding in embeddings) assert len(embeddings[0]) > 0 assert all(len(embedding) == len(embeddings[0]) for embedding in embeddings) async def test_aembed_query(self, model: Embeddings) -> None: """Test embedding a string query async. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of floats when calling ``.aembed_query`` on a string. 2. The length of the list is consistent across different inputs. """ # noqa: E501 embedding_1 = await model.aembed_query("foo") assert isinstance(embedding_1, List) assert isinstance(embedding_1[0], float) embedding_2 = await model.aembed_query("bar") assert len(embedding_1) > 0 assert len(embedding_1) == len(embedding_2) async def test_aembed_documents(self, model: Embeddings) -> None: """Test embedding a list of strings async. .. dropdown:: Troubleshooting If this test fails, check that: 1. The model will generate a list of lists of floats when calling ``.aembed_documents`` on a list of strings. 2. The length of each list is the same. """ # noqa: E501 documents = ["foo", "bar", "baz"] embeddings = await model.aembed_documents(documents) assert len(embeddings) == len(documents) assert all(isinstance(embedding, List) for embedding in embeddings) assert all(isinstance(embedding[0], float) for embedding in embeddings) assert len(embeddings[0]) > 0 assert all(len(embedding) == len(embeddings[0]) for embedding in embeddings)
# Copyright (c) OpenMMLab. All rights reserved. import argparse import mmcv from mmcv import Config, DictAction from mmdet.datasets import build_dataset from mmdet.utils import update_data_root def parse_args(): parser = argparse.ArgumentParser(description='Evaluate metric of the ' 'results saved in pkl format') parser.add_argument('config', help='Config of the model') parser.add_argument('pkl_results', help='Results in pickle format') parser.add_argument( '--format-only', action='store_true', help='Format the output results without perform evaluation. It is' 'useful when you want to format the result to a specific format and ' 'submit it to the test server') parser.add_argument( '--eval', type=str, nargs='+', help='Evaluation metrics, which depends on the dataset, e.g., "bbox",' ' "segm", "proposal" for COCO, and "mAP", "recall" for PASCAL VOC') 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( '--eval-options', nargs='+', action=DictAction, help='custom options for evaluation, the key-value pair in xxx=yyy ' 'format will be kwargs for dataset.evaluate() function') args = parser.parse_args() return args def main(): args = parse_args() cfg = Config.fromfile(args.config) # update data root according to MMDET_DATASETS update_data_root(cfg) assert args.eval or args.format_only, ( 'Please specify at least one operation (eval/format the results) with ' 'the argument "--eval", "--format-only"') if args.eval and args.format_only: raise ValueError('--eval and --format_only cannot be both specified') if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) cfg.data.test.test_mode = True dataset = build_dataset(cfg.data.test) outputs = mmcv.load(args.pkl_results) kwargs = {} if args.eval_options is None else args.eval_options if args.format_only: dataset.format_results(outputs, **kwargs) if args.eval: eval_kwargs = cfg.get('evaluation', {}).copy() # hard-code way to remove EvalHook args for key in [ 'interval', 'tmpdir', 'start', 'gpu_collect', 'save_best', 'rule' ]: eval_kwargs.pop(key, None) eval_kwargs.update(dict(metric=args.eval, **kwargs)) print(dataset.evaluate(outputs, **eval_kwargs)) if __name__ == '__main__': main()
# Copyright (c) OpenMMLab. All rights reserved. import argparse import mmcv from mmcv import Config, DictAction from mmdet.datasets import build_dataset def parse_args(): parser = argparse.ArgumentParser(description='Evaluate metric of the ' 'results saved in pkl format') parser.add_argument('config', help='Config of the model') parser.add_argument('pkl_results', help='Results in pickle format') parser.add_argument( '--format-only', action='store_true', help='Format the output results without perform evaluation. It is' 'useful when you want to format the result to a specific format and ' 'submit it to the test server') parser.add_argument( '--eval', type=str, nargs='+', help='Evaluation metrics, which depends on the dataset, e.g., "bbox",' ' "segm", "proposal" for COCO, and "mAP", "recall" for PASCAL VOC') 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( '--eval-options', nargs='+', action=DictAction, help='custom options for evaluation, the key-value pair in xxx=yyy ' 'format will be kwargs for dataset.evaluate() function') args = parser.parse_args() return args def main(): args = parse_args() cfg = Config.fromfile(args.config) assert args.eval or args.format_only, ( 'Please specify at least one operation (eval/format the results) with ' 'the argument "--eval", "--format-only"') if args.eval and args.format_only: raise ValueError('--eval and --format_only cannot be both specified') if args.cfg_options is not None: cfg.merge_from_dict(args.cfg_options) cfg.data.test.test_mode = True dataset = build_dataset(cfg.data.test) outputs = mmcv.load(args.pkl_results) kwargs = {} if args.eval_options is None else args.eval_options if args.format_only: dataset.format_results(outputs, **kwargs) if args.eval: eval_kwargs = cfg.get('evaluation', {}).copy() # hard-code way to remove EvalHook args for key in [ 'interval', 'tmpdir', 'start', 'gpu_collect', 'save_best', 'rule' ]: eval_kwargs.pop(key, None) eval_kwargs.update(dict(metric=args.eval, **kwargs)) print(dataset.evaluate(outputs, **eval_kwargs)) if __name__ == '__main__': main()
_base_ = [ '../_base_/models/ssd300.py', '../_base_/datasets/voc0712.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] model = dict( bbox_head=dict( num_classes=20, anchor_generator=dict(basesize_ratio_range=(0.2, 0.9)))) # dataset settings dataset_type = 'VOCDataset' data_root = 'data/VOCdevkit/' input_size = 300 train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='Expand', mean={{_base_.model.data_preprocessor.mean}}, to_rgb={{_base_.model.data_preprocessor.bgr_to_rgb}}, ratio_range=(1, 4)), dict( type='MinIoURandomCrop', min_ious=(0.1, 0.3, 0.5, 0.7, 0.9), min_crop_size=0.3), dict(type='Resize', scale=(input_size, input_size), keep_ratio=False), dict(type='RandomFlip', prob=0.5), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile'), dict(type='Resize', scale=(input_size, input_size), keep_ratio=False), # avoid bboxes being resized dict(type='LoadAnnotations', with_bbox=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=8, num_workers=3, dataset=dict( # RepeatDataset # the dataset is repeated 10 times, and the training schedule is 2x, # so the actual epoch = 12 * 10 = 120. times=10, dataset=dict( # ConcatDataset # VOCDataset will add different `DATASET_TYPE` in dataset.metainfo, # which will get error if using ConcatDataset. Adding # `ignore_keys` can avoid this error. ignore_keys=['DATASET_TYPE'], datasets=[ dict( type=dataset_type, data_root=data_root, ann_file='VOC2007/ImageSets/Main/trainval.txt', data_prefix=dict(sub_data_root='VOC2007/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline), dict( type=dataset_type, data_root=data_root, ann_file='VOC2012/ImageSets/Main/trainval.txt', data_prefix=dict(sub_data_root='VOC2012/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline) ]))) val_dataloader = dict(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader custom_hooks = [ dict(type='NumClassCheckHook'), dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW') ] # optimizer optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=1e-3, momentum=0.9, weight_decay=5e-4)) # learning policy param_scheduler = [ dict( type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=24, by_epoch=True, milestones=[16, 20], gamma=0.1) ] # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (8 GPUs) x (8 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = [ '../_base_/models/ssd300.py', '../_base_/datasets/voc0712.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] model = dict( bbox_head=dict( num_classes=20, anchor_generator=dict(basesize_ratio_range=(0.2, 0.9)))) # dataset settings dataset_type = 'VOCDataset' data_root = 'data/VOCdevkit/' input_size = 300 train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='Expand', mean={{_base_.model.data_preprocessor.mean}}, to_rgb={{_base_.model.data_preprocessor.bgr_to_rgb}}, ratio_range=(1, 4)), dict( type='MinIoURandomCrop', min_ious=(0.1, 0.3, 0.5, 0.7, 0.9), min_crop_size=0.3), dict(type='Resize', scale=(input_size, input_size), keep_ratio=False), dict(type='RandomFlip', prob=0.5), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile'), dict(type='Resize', scale=(input_size, input_size), keep_ratio=False), # avoid bboxes being resized dict(type='LoadAnnotations', with_bbox=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=8, num_workers=3, dataset=dict( # RepeatDataset # the dataset is repeated 10 times, and the training schedule is 2x, # so the actual epoch = 12 * 10 = 120. times=10, dataset=dict( # ConcatDataset datasets=[ dict( type=dataset_type, data_root=data_root, ann_file='VOC2007/ImageSets/Main/trainval.txt', data_prefix=dict(sub_data_root='VOC2007/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline), dict( type=dataset_type, data_root=data_root, ann_file='VOC2012/ImageSets/Main/trainval.txt', data_prefix=dict(sub_data_root='VOC2012/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline) ]))) val_dataloader = dict(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader custom_hooks = [ dict(type='NumClassCheckHook'), dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW') ] # optimizer optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=1e-3, momentum=0.9, weight_decay=5e-4)) # learning policy param_scheduler = [ dict( type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=24, by_epoch=True, milestones=[16, 20], gamma=0.1) ] # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (8 GPUs) x (8 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
""" This example uses average word embeddings (for example from GloVe). It adds two fully-connected feed-forward layers (dense layers) to create a Deep Averaging Network (DAN). If 'glove.6B.300d.txt.gz' does not exist, it tries to download it from our server. See https://public.ukp.informatik.tu-darmstadt.de/reimers/embeddings/ for available word embeddings files """ from torch.utils.data import DataLoader import math from sentence_transformers import models, losses, util from sentence_transformers import LoggingHandler, SentenceTransformer from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator from sentence_transformers.readers import InputExample import logging from datetime import datetime import os import csv import gzip #### 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 # Read the dataset batch_size = 32 model_save_path = "output/training_stsbenchmark_avg_word_embeddings-" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") # Check if dataset exists. If not, download and extract it sts_dataset_path = "datasets/stsbenchmark.tsv.gz" if not os.path.exists(sts_dataset_path): util.http_get("https://sbert.net/datasets/stsbenchmark.tsv.gz", sts_dataset_path) logging.info("Read STSbenchmark train dataset") train_samples = [] dev_samples = [] test_samples = [] with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: score = float(row["score"]) / 5.0 # Normalize score to range 0 ... 1 inp_example = InputExample(texts=[row["sentence1"], row["sentence2"]], label=score) if row["split"] == "dev": dev_samples.append(inp_example) elif row["split"] == "test": test_samples.append(inp_example) else: train_samples.append(inp_example) # Map tokens to traditional word embeddings like GloVe word_embedding_model = models.WordEmbeddings.from_text_file("glove.6B.300d.txt.gz") # Apply mean pooling to get one fixed sized sentence vector pooling_model = models.Pooling( word_embedding_model.get_word_embedding_dimension(), pooling_mode_mean_tokens=True, pooling_mode_cls_token=False, pooling_mode_max_tokens=False, ) # Add two trainable feed-forward networks (DAN) sent_embeddings_dimension = pooling_model.get_sentence_embedding_dimension() dan1 = models.Dense(in_features=sent_embeddings_dimension, out_features=sent_embeddings_dimension) dan2 = models.Dense(in_features=sent_embeddings_dimension, out_features=sent_embeddings_dimension) model = SentenceTransformer(modules=[word_embedding_model, pooling_model, dan1, dan2]) # Convert the dataset to a DataLoader ready for training logging.info("Read STSbenchmark train dataset") train_dataloader = DataLoader(train_samples, shuffle=True, batch_size=batch_size) train_loss = losses.CosineSimilarityLoss(model=model) logging.info("Read STSbenchmark dev dataset") evaluator = EmbeddingSimilarityEvaluator.from_input_examples(dev_samples, name="sts-dev") # Configure the training num_epochs = 10 warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) # 10% of train data for warm-up logging.info("Warmup-steps: {}".format(warmup_steps)) # Train the model model.fit( train_objectives=[(train_dataloader, train_loss)], evaluator=evaluator, epochs=num_epochs, warmup_steps=warmup_steps, output_path=model_save_path, ) ############################################################################## # # Load the stored model and evaluate its performance on STS benchmark dataset # ############################################################################## model = SentenceTransformer(model_save_path) test_evaluator = EmbeddingSimilarityEvaluator.from_input_examples(test_samples, name="sts-test") model.evaluate(evaluator)
""" This example uses average word embeddings (for example from GloVe). It adds two fully-connected feed-forward layers (dense layers) to create a Deep Averaging Network (DAN). If 'glove.6B.300d.txt.gz' does not exist, it tries to download it from our server. See https://public.ukp.informatik.tu-darmstadt.de/reimers/embeddings/ for available word embeddings files """ from torch.utils.data import DataLoader import math from sentence_transformers import models, losses, util from sentence_transformers import LoggingHandler, SentenceTransformer from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator from sentence_transformers.readers import InputExample import logging from datetime import datetime import os import csv import gzip #### 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 # Read the dataset batch_size = 32 model_save_path = "output/training_stsbenchmark_avg_word_embeddings-" + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") # Check if dataset exists. If not, download and extract it sts_dataset_path = "datasets/stsbenchmark.tsv.gz" if not os.path.exists(sts_dataset_path): util.http_get("https://sbert.net/datasets/stsbenchmark.tsv.gz", sts_dataset_path) logging.info("Read STSbenchmark train dataset") train_samples = [] dev_samples = [] test_samples = [] with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn: reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE) for row in reader: score = float(row["score"]) / 5.0 # Normalize score to range 0 ... 1 inp_example = InputExample(texts=[row["sentence1"], row["sentence2"]], label=score) if row["split"] == "dev": dev_samples.append(inp_example) elif row["split"] == "test": test_samples.append(inp_example) else: train_samples.append(inp_example) # Map tokens to traditional word embeddings like GloVe word_embedding_model = models.WordEmbeddings.from_text_file("glove.6B.300d.txt.gz") # Apply mean pooling to get one fixed sized sentence vector pooling_model = models.Pooling( word_embedding_model.get_word_embedding_dimension(), pooling_mode_mean_tokens=True, pooling_mode_cls_token=False, pooling_mode_max_tokens=False, ) # Add two trainable feed-forward networks (DAN) sent_embeddings_dimension = pooling_model.get_sentence_embedding_dimension() dan1 = models.Dense(in_features=sent_embeddings_dimension, out_features=sent_embeddings_dimension) dan2 = models.Dense(in_features=sent_embeddings_dimension, out_features=sent_embeddings_dimension) model = SentenceTransformer(modules=[word_embedding_model, pooling_model, dan1, dan2]) # Convert the dataset to a DataLoader ready for training logging.info("Read STSbenchmark train dataset") train_dataloader = DataLoader(train_samples, shuffle=True, batch_size=batch_size) train_loss = losses.CosineSimilarityLoss(model=model) logging.info("Read STSbenchmark dev dataset") evaluator = EmbeddingSimilarityEvaluator.from_input_examples(dev_samples, name="sts-dev") # Configure the training num_epochs = 10 warmup_steps = math.ceil(len(train_dataloader) * num_epochs * 0.1) # 10% of train data for warm-up logging.info("Warmup-steps: {}".format(warmup_steps)) # Train the model model.fit( train_objectives=[(train_dataloader, train_loss)], evaluator=evaluator, epochs=num_epochs, warmup_steps=warmup_steps, output_path=model_save_path, ) ############################################################################## # # Load the stored model and evaluate its performance on STS benchmark dataset # ############################################################################## model = SentenceTransformer(model_save_path) test_evaluator = EmbeddingSimilarityEvaluator.from_input_examples(test_samples, name="sts-test") model.evaluate(evaluator)
from torch.hub import download_url_to_file, load_state_dict_from_url __all__ = [ "load_state_dict_from_url", "download_url_to_file", ]
from torch.hub import load_state_dict_from_url, download_url_to_file __all__ = [ "load_state_dict_from_url", "download_url_to_file", ]
import os import pytest from jina import Flow cur_dir = os.path.dirname(os.path.abspath(__file__)) def _validate_flow(f): graph_dict = f._get_graph_representation() addresses = f._get_deployments_addresses() for name, pod in f: if name != 'gateway': for n in pod.needs: assert name in graph_dict[n if n != 'gateway' else 'start-gateway'] else: for n in pod.needs: assert 'end-gateway' in graph_dict[n] @pytest.mark.slow def test_index(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faiss/flow-index.yml') ) with f: _validate_flow(f) def test_query(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faiss/flow-query.yml') ) with f: _validate_flow(f) @pytest.mark.slow def test_index(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faces/flow-index.yml') ) with f: _validate_flow(f) @pytest.mark.slow def test_query(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faces/flow-query.yml') ) with f: _validate_flow(f) @pytest.mark.parametrize('override_executor_log_config', [False, True]) def test_custom_logging(monkeypatch, override_executor_log_config): monkeypatch.delenv('JINA_LOG_LEVEL', raising=True) # ignore global env log_config_path = os.path.join(cur_dir, '../../../logging/yaml/file.yml') f = Flow(log_config=log_config_path) if override_executor_log_config: f = f.add(log_config='default') else: f = f.add() with f: assert f.args.log_config.endswith('logging/yaml/file.yml') for name, pod in f: print(name, pod) if override_executor_log_config and name.startswith('executor'): assert pod.args.log_config == 'default' else: assert pod.args.log_config.endswith('logging/yaml/file.yml')
import os import pytest from jina import Flow cur_dir = os.path.dirname(os.path.abspath(__file__)) def _validate_flow(f): graph_dict = f._get_graph_representation() addresses = f._get_deployments_addresses() for name, pod in f: if name != 'gateway': for n in pod.needs: assert name in graph_dict[n if n != 'gateway' else 'start-gateway'] else: for n in pod.needs: assert 'end-gateway' in graph_dict[n] @pytest.mark.slow def test_index(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faiss/flow-index.yml') ) with f: _validate_flow(f) def test_query(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faiss/flow-query.yml') ) with f: _validate_flow(f) @pytest.mark.slow def test_index(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faces/flow-index.yml') ) with f: _validate_flow(f) @pytest.mark.slow def test_query(): f = Flow.load_config( os.path.join(cur_dir, '../../../yaml/examples/faces/flow-query.yml') ) with f: _validate_flow(f)
import argparse from jina.enums import GatewayProtocolType from jina.helper import parse_host_scheme from jina.logging.predefined import default_logger class NetworkChecker: """Check if a Deployment is running or not.""" def __init__(self, args: 'argparse.Namespace'): """ Create a new :class:`NetworkChecker`. :param args: args provided by the CLI. """ import time from jina.clients import Client from jina.logging.profile import TimeContext from jina.serve.runtimes.gateway import GatewayRuntime from jina.serve.runtimes.worker import WorkerRuntime try: total_time = 0 total_success = 0 timeout = args.timeout / 1000 if args.timeout != -1 else None for j in range(args.attempts): with TimeContext( f'ping {args.target} on {args.host} at {j} round', default_logger ) as tc: if args.target == 'executor': hostname, port, protocol, _ = parse_host_scheme(args.host) r = WorkerRuntime.is_ready( ctrl_address=f'{hostname}:{port}', timeout=timeout, ) elif args.target == 'gateway': hostname, port, protocol, _ = parse_host_scheme(args.host) r = GatewayRuntime.is_ready( f'{hostname}:{port}', protocol=GatewayProtocolType.from_string(protocol), timeout=timeout, ) elif args.target == 'flow': r = Client(host=args.host).is_flow_ready(timeout=timeout) if not r: default_logger.warning( 'not responding, attempt (%d/%d) in 1s' % (j + 1, args.attempts) ) else: total_success += 1 total_time += tc.duration if args.attempts > 0: time.sleep(1) if total_success < args.attempts: default_logger.debug( 'message lost %.0f%% (%d/%d) ' % ( (1 - total_success / args.attempts) * 100, args.attempts - total_success, args.attempts, ) ) if total_success > 0: default_logger.debug( 'avg. latency: %.0f ms' % (total_time / total_success * 1000) ) if total_success >= args.min_successful_attempts: default_logger.debug( f'readiness check succeeded {total_success} times!!!' ) exit(0) else: default_logger.debug( f'readiness check succeeded {total_success} times, less than {args.min_successful_attempts}' ) except KeyboardInterrupt: pass # returns 1 (anomaly) when it comes to here exit(1)
import argparse from jina.enums import GatewayProtocolType from jina.helper import parse_host_scheme from jina.logging.predefined import default_logger class NetworkChecker: """Check if a Deployment is running or not.""" def __init__(self, args: 'argparse.Namespace'): """ Create a new :class:`NetworkChecker`. :param args: args provided by the CLI. """ import time from jina.clients import Client from jina.logging.profile import TimeContext from jina.serve.runtimes.gateway import GatewayRuntime from jina.serve.runtimes.worker import WorkerRuntime try: total_time = 0 total_success = 0 for j in range(args.attempts): with TimeContext( f'ping {args.target} on {args.host} at {j} round', default_logger ) as tc: if args.target == 'executor': hostname, port, protocol, _ = parse_host_scheme(args.host) r = WorkerRuntime.is_ready(ctrl_address=f'{hostname}:{port}') elif args.target == 'gateway': hostname, port, protocol, _ = parse_host_scheme(args.host) r = GatewayRuntime.is_ready( f'{hostname}:{port}', protocol=GatewayProtocolType.from_string(protocol) ) elif args.target == 'flow': r = Client(host=args.host).is_flow_ready(timeout=args.timeout / 1000) if not r: default_logger.warning( 'not responding, attempt (%d/%d) in 1s' % (j + 1, args.attempts) ) else: total_success += 1 total_time += tc.duration if args.attempts > 0: time.sleep(1) if total_success < args.attempts: default_logger.debug( 'message lost %.0f%% (%d/%d) ' % ( (1 - total_success / args.attempts) * 100, args.attempts - total_success, args.attempts, ) ) if total_success > 0: default_logger.debug( 'avg. latency: %.0f ms' % (total_time / total_success * 1000) ) if total_success >= args.min_successful_attempts: default_logger.debug( f'readiness check succeeded {total_success} times!!!' ) exit(0) else: default_logger.debug( f'readiness check succeeded {total_success} times, less than {args.min_successful_attempts}' ) except KeyboardInterrupt: pass # returns 1 (anomaly) when it comes to here exit(1)
import datetime import autogpt_libs.auth.depends import autogpt_libs.auth.middleware import fastapi import fastapi.testclient import pytest import pytest_mock import backend.server.v2.library.db import backend.server.v2.library.model import backend.server.v2.library.routes app = fastapi.FastAPI() app.include_router(backend.server.v2.library.routes.router) client = fastapi.testclient.TestClient(app) def override_auth_middleware(): """Override auth middleware for testing""" return {"sub": "test-user-id"} def override_get_user_id(): """Override get_user_id for testing""" return "test-user-id" app.dependency_overrides[autogpt_libs.auth.middleware.auth_middleware] = ( override_auth_middleware ) app.dependency_overrides[autogpt_libs.auth.depends.get_user_id] = override_get_user_id def test_get_library_agents_success(mocker: pytest_mock.MockFixture): mocked_value = [ backend.server.v2.library.model.LibraryAgent( id="test-agent-1", agent_id="test-agent-1", agent_version=1, preset_id="preset-1", updated_at=datetime.datetime(2023, 1, 1, 0, 0, 0), is_favorite=False, is_created_by_user=True, is_latest_version=True, name="Test Agent 1", description="Test Description 1", input_schema={"type": "object", "properties": {}}, output_schema={"type": "object", "properties": {}}, ), backend.server.v2.library.model.LibraryAgent( id="test-agent-2", agent_id="test-agent-2", agent_version=1, preset_id="preset-2", updated_at=datetime.datetime(2023, 1, 1, 0, 0, 0), is_favorite=False, is_created_by_user=False, is_latest_version=True, name="Test Agent 2", description="Test Description 2", input_schema={"type": "object", "properties": {}}, output_schema={"type": "object", "properties": {}}, ), ] mock_db_call = mocker.patch("backend.server.v2.library.db.get_library_agents") mock_db_call.return_value = mocked_value response = client.get("/agents") assert response.status_code == 200 data = [ backend.server.v2.library.model.LibraryAgent.model_validate(agent) for agent in response.json() ] assert len(data) == 2 assert data[0].agent_id == "test-agent-1" assert data[0].is_created_by_user is True assert data[1].agent_id == "test-agent-2" assert data[1].is_created_by_user is False mock_db_call.assert_called_once_with("test-user-id") def test_get_library_agents_error(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.get_library_agents") mock_db_call.side_effect = Exception("Test error") response = client.get("/agents") assert response.status_code == 500 mock_db_call.assert_called_once_with("test-user-id") @pytest.mark.skip(reason="Mocker Not implemented") def test_add_agent_to_library_success(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.add_agent_to_library") mock_db_call.return_value = None response = client.post("/agents/test-version-id") assert response.status_code == 201 mock_db_call.assert_called_once_with( store_listing_version_id="test-version-id", user_id="test-user-id" ) @pytest.mark.skip(reason="Mocker Not implemented") def test_add_agent_to_library_error(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.add_agent_to_library") mock_db_call.side_effect = Exception("Test error") response = client.post("/agents/test-version-id") assert response.status_code == 500 assert response.json()["detail"] == "Failed to add agent to library" mock_db_call.assert_called_once_with( store_listing_version_id="test-version-id", user_id="test-user-id" )
import autogpt_libs.auth.depends import autogpt_libs.auth.middleware import fastapi import fastapi.testclient import pytest import pytest_mock import backend.server.v2.library.db import backend.server.v2.library.model import backend.server.v2.library.routes app = fastapi.FastAPI() app.include_router(backend.server.v2.library.routes.router) client = fastapi.testclient.TestClient(app) def override_auth_middleware(): """Override auth middleware for testing""" return {"sub": "test-user-id"} def override_get_user_id(): """Override get_user_id for testing""" return "test-user-id" app.dependency_overrides[autogpt_libs.auth.middleware.auth_middleware] = ( override_auth_middleware ) app.dependency_overrides[autogpt_libs.auth.depends.get_user_id] = override_get_user_id def test_get_library_agents_success(mocker: pytest_mock.MockFixture): mocked_value = [ backend.server.v2.library.model.LibraryAgent( id="test-agent-1", version=1, is_active=True, name="Test Agent 1", description="Test Description 1", isCreatedByUser=True, input_schema={"type": "object", "properties": {}}, output_schema={"type": "object", "properties": {}}, ), backend.server.v2.library.model.LibraryAgent( id="test-agent-2", version=1, is_active=True, name="Test Agent 2", description="Test Description 2", isCreatedByUser=False, input_schema={"type": "object", "properties": {}}, output_schema={"type": "object", "properties": {}}, ), ] mock_db_call = mocker.patch("backend.server.v2.library.db.get_library_agents") mock_db_call.return_value = mocked_value response = client.get("/agents") assert response.status_code == 200 data = [ backend.server.v2.library.model.LibraryAgent.model_validate(agent) for agent in response.json() ] assert len(data) == 2 assert data[0].id == "test-agent-1" assert data[0].isCreatedByUser is True assert data[1].id == "test-agent-2" assert data[1].isCreatedByUser is False mock_db_call.assert_called_once_with("test-user-id") def test_get_library_agents_error(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.get_library_agents") mock_db_call.side_effect = Exception("Test error") response = client.get("/agents") assert response.status_code == 500 mock_db_call.assert_called_once_with("test-user-id") @pytest.mark.skip(reason="Mocker Not implemented") def test_add_agent_to_library_success(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.add_agent_to_library") mock_db_call.return_value = None response = client.post("/agents/test-version-id") assert response.status_code == 201 mock_db_call.assert_called_once_with( store_listing_version_id="test-version-id", user_id="test-user-id" ) @pytest.mark.skip(reason="Mocker Not implemented") def test_add_agent_to_library_error(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.add_agent_to_library") mock_db_call.side_effect = Exception("Test error") response = client.post("/agents/test-version-id") assert response.status_code == 500 assert response.json()["detail"] == "Failed to add agent to library" mock_db_call.assert_called_once_with( store_listing_version_id="test-version-id", user_id="test-user-id" )
import os from typing import Optional import pytest from docarray import BaseDoc, DocList from docarray.documents import ImageDoc from tests import TOYDATA_DIR @pytest.fixture() def nested_doc_cls(): class MyDoc(BaseDoc): count: Optional[int] text: str class MyDocNested(MyDoc): image: ImageDoc image2: ImageDoc return MyDocNested def test_to_from_csv(tmpdir, nested_doc_cls): da = DocList[nested_doc_cls]( [ nested_doc_cls( count=0, text='hello', image=ImageDoc(url='aux.png'), image2=ImageDoc(url='aux.png'), ), nested_doc_cls(text='hello world', image=ImageDoc(), image2=ImageDoc()), ] ) tmp_file = str(tmpdir / 'tmp.csv') da.to_csv(tmp_file) assert os.path.isfile(tmp_file) da_from = DocList[nested_doc_cls].from_csv(tmp_file) for doc1, doc2 in zip(da, da_from): assert doc1 == doc2 def test_from_csv_nested(nested_doc_cls): da = DocList[nested_doc_cls].from_csv( file_path=str(TOYDATA_DIR / 'docs_nested.csv') ) assert len(da) == 3 for i, doc in enumerate(da): assert doc.count.__class__ == int assert doc.count == int(f'{i}{i}{i}') assert doc.text.__class__ == str assert doc.text == f'hello {i}' assert doc.image.__class__ == ImageDoc assert doc.image.tensor is None assert doc.image.embedding is None assert doc.image.bytes_ is None assert doc.image2.__class__ == ImageDoc assert doc.image2.tensor is None assert doc.image2.embedding is None assert doc.image2.bytes_ is None assert da[0].image2.url == 'image_10.png' assert da[1].image2.url is None assert da[2].image2.url is None @pytest.fixture() def nested_doc(): class Inner(BaseDoc): img: Optional[ImageDoc] class Middle(BaseDoc): img: Optional[ImageDoc] inner: Optional[Inner] class Outer(BaseDoc): img: Optional[ImageDoc] middle: Optional[Middle] doc = Outer( img=ImageDoc(), middle=Middle(img=ImageDoc(), inner=Inner(img=ImageDoc())) ) return doc def test_from_csv_without_schema_raise_exception(): with pytest.raises(TypeError, match='no document schema defined'): DocList.from_csv(file_path=str(TOYDATA_DIR / 'docs_nested.csv')) def test_from_csv_with_wrong_schema_raise_exception(nested_doc): with pytest.raises(ValueError, match='Column names do not match the schema'): DocList[nested_doc.__class__].from_csv(file_path=str(TOYDATA_DIR / 'docs.csv')) def test_from_remote_csv_file(): remote_url = 'https://github.com/docarray/docarray/blob/main/tests/toydata/books.csv?raw=true' class Book(BaseDoc): title: str author: str year: int books = DocList[Book].from_csv(file_path=remote_url) assert len(books) == 3
import os from typing import Optional import pytest from docarray import BaseDoc, DocList from docarray.documents import ImageDoc from tests import TOYDATA_DIR @pytest.fixture() def nested_doc_cls(): class MyDoc(BaseDoc): count: Optional[int] text: str class MyDocNested(MyDoc): image: ImageDoc image2: ImageDoc return MyDocNested def test_to_from_csv(tmpdir, nested_doc_cls): da = DocList[nested_doc_cls]( [ nested_doc_cls( count=0, text='hello', image=ImageDoc(url='aux.png'), image2=ImageDoc(url='aux.png'), ), nested_doc_cls(text='hello world', image=ImageDoc(), image2=ImageDoc()), ] ) tmp_file = str(tmpdir / 'tmp.csv') da.to_csv(tmp_file) assert os.path.isfile(tmp_file) da_from = DocList[nested_doc_cls].from_csv(tmp_file) for doc1, doc2 in zip(da, da_from): assert doc1 == doc2 def test_from_csv_nested(nested_doc_cls): da = DocList[nested_doc_cls].from_csv( file_path=str(TOYDATA_DIR / 'docs_nested.csv') ) assert len(da) == 3 for i, doc in enumerate(da): assert doc.count.__class__ == int assert doc.count == int(f'{i}{i}{i}') assert doc.text.__class__ == str assert doc.text == f'hello {i}' assert doc.image.__class__ == ImageDoc assert doc.image.tensor is None assert doc.image.embedding is None assert doc.image.bytes_ is None assert doc.image2.__class__ == ImageDoc assert doc.image2.tensor is None assert doc.image2.embedding is None assert doc.image2.bytes_ is None assert da[0].image2.url == 'image_10.png' assert da[1].image2.url is None assert da[2].image2.url is None @pytest.fixture() def nested_doc(): class Inner(BaseDoc): img: Optional[ImageDoc] class Middle(BaseDoc): img: Optional[ImageDoc] inner: Optional[Inner] class Outer(BaseDoc): img: Optional[ImageDoc] middle: Optional[Middle] doc = Outer( img=ImageDoc(), middle=Middle(img=ImageDoc(), inner=Inner(img=ImageDoc())) ) return doc def test_from_csv_without_schema_raise_exception(): with pytest.raises(TypeError, match='no document schema defined'): DocList.from_csv(file_path=str(TOYDATA_DIR / 'docs_nested.csv')) def test_from_csv_with_wrong_schema_raise_exception(nested_doc): with pytest.raises(ValueError, match='Column names do not match the schema'): DocList[nested_doc.__class__].from_csv(file_path=str(TOYDATA_DIR / 'docs.csv')) def test_from_remote_csv_file(): remote_url = 'https://github.com/docarray/docarray/blob/feat-rewrite-v2/tests/toydata/books.csv?raw=true' class Book(BaseDoc): title: str author: str year: int books = DocList[Book].from_csv(file_path=remote_url) assert len(books) == 3
""" The hub handles the moderation of inter-spoke communication. As the hub and spokes operate in isolated processes, sockets are employed to transmit messages between these processes. Consequently, a Socket class is defined for facilitating communication. """ import json class Socket: """ A class to facilitate communication between isolated processes using sockets. Attributes: sock (socket.socket): The socket object used for communication. """ def __init__(self, sock) -> None: """ Initialize the Socket with a given socket object. Args: sock (socket.socket): The socket object used for communication. """ self.sock = sock def send(self, msg): """ Send a message through the socket. Args: msg (bytes): The message to be sent. """ self.sock.sendall(msg) self.sock.sendall(b"\n") def recv(self, length=1024): """ Receive a message from the socket. Args: length (int, optional): The maximum amount of data to be received at once. Default is 1024. Returns: dict: The deserialized JSON message received, or None if the message is not well-formed. """ # The length parameter can be altered to fit the size of the message buffer = "" while True: msg = self.sock.recv(length).decode("utf-8") if not msg: break buffer += msg if "\n" in buffer: # Split the buffer at the newline to process the complete message complete_msg, _, buffer = buffer.partition("\n") # Attempt to deserialize the JSON data try: return json.loads( complete_msg ) # Return the deserialized dictionary except json.JSONDecodeError: # Handle error if JSON is not well-formed break # Or handle error accordingly return None def close(self): """ Close the socket. """ self.sock.close()
""" The hub handles the moderation of inter-spoke communication. As the hub and spokes operate in isolated processes, sockets are employed to transmit messages between these processes. Consequently, a Socket class is defined for facilitating communication. """ import json class Socket: """ A class to facilitate communication between isolated processes using sockets. Attributes: sock (socket.socket): The socket object used for communication. """ def __init__(self, sock) -> None: """ Initialize the Socket with a given socket object. Args: sock (socket.socket): The socket object used for communication. """ self.sock = sock def send(self, msg): """ Send a message through the socket. Args: msg (bytes): The message to be sent. """ self.sock.sendall(msg) self.sock.sendall(b"\n") def recv(self, length=1024): """ Receive a message from the socket. Args: length (int, optional): The maximum amount of data to be received at once. Default is 1024. Returns: dict: The deserialized JSON message received, or None if the message is not well-formed. """ # The length parameter can be altered to fit the size of the message buffer = "" while True: msg = self.sock.recv(length).decode("utf-8") if not msg: break buffer += msg if "\n" in buffer: # Split the buffer at the newline to process the complete message complete_msg, _, buffer = buffer.partition("\n") # Attempt to deserialize the JSON data try: return json.loads( complete_msg ) # Return the deserialized dictionary except json.JSONDecodeError: # Handle error if JSON is not well-formed break # Or handle error accordingly return None def close(self): """ Close the socket. """ self.sock.close()
# This is different from the TTA of official CenterNet. tta_model = dict( type='DetTTAModel', tta_cfg=dict(nms=dict(type='nms', iou_threshold=0.5), max_per_img=100)) tta_pipeline = [ dict(type='LoadImageFromFile', to_float32=True, backend_args=None), dict( type='TestTimeAug', transforms=[ [ # ``RandomFlip`` must be placed before ``RandomCenterCropPad``, # otherwise bounding box coordinates after flipping cannot be # recovered correctly. dict(type='RandomFlip', prob=1.), dict(type='RandomFlip', prob=0.) ], [ dict( type='RandomCenterCropPad', ratios=None, border=None, mean=[0, 0, 0], std=[1, 1, 1], to_rgb=True, test_mode=True, test_pad_mode=['logical_or', 31], test_pad_add_pix=1), ], [dict(type='LoadAnnotations', with_bbox=True)], [ dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'flip', 'flip_direction', 'border')) ] ]) ]
# This is different from the TTA of official CenterNet. tta_model = dict( type='DetTTAModel', tta_cfg=dict(nms=dict(type='nms', iou_threshold=0.5), max_per_img=100)) tta_pipeline = [ dict( type='LoadImageFromFile', to_float32=True, file_client_args=dict(backend='disk')), dict( type='TestTimeAug', transforms=[ [ # ``RandomFlip`` must be placed before ``RandomCenterCropPad``, # otherwise bounding box coordinates after flipping cannot be # recovered correctly. dict(type='RandomFlip', prob=1.), dict(type='RandomFlip', prob=0.) ], [ dict( type='RandomCenterCropPad', ratios=None, border=None, mean=[0, 0, 0], std=[1, 1, 1], to_rgb=True, test_mode=True, test_pad_mode=['logical_or', 31], test_pad_add_pix=1), ], [dict(type='LoadAnnotations', with_bbox=True)], [ dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'flip', 'flip_direction', 'border')) ] ]) ]
import io from typing import TYPE_CHECKING, Any, Tuple, Type, TypeVar import numpy as np from pydantic import parse_obj_as from pydantic.validators import bytes_validator from docarray.typing.abstract_type import AbstractType from docarray.typing.proto_register import _register_proto from docarray.typing.tensor.audio import AudioNdArray from docarray.utils._internal.misc import import_library if TYPE_CHECKING: from pydantic.fields import BaseConfig, ModelField from docarray.proto import NodeProto T = TypeVar('T', bound='AudioBytes') @_register_proto(proto_type_name='audio_bytes') class AudioBytes(bytes, AbstractType): """ Bytes that store an audio and that can be load into an Audio tensor """ @classmethod def validate( cls: Type[T], value: Any, field: 'ModelField', config: 'BaseConfig', ) -> T: value = bytes_validator(value) return cls(value) @classmethod def from_protobuf(cls: Type[T], pb_msg: T) -> T: return parse_obj_as(cls, pb_msg) def _to_node_protobuf(self: T) -> 'NodeProto': from docarray.proto import NodeProto return NodeProto(blob=self, type=self._proto_type_name) def load(self) -> Tuple[AudioNdArray, int]: """ Load the Audio from the [`AudioBytes`][docarray.typing.AudioBytes] into an [`AudioNdArray`][docarray.typing.AudioNdArray]. --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioBytes, AudioNdArray, AudioUrl class MyAudio(BaseDoc): url: AudioUrl tensor: Optional[AudioNdArray] bytes_: Optional[AudioBytes] frame_rate: Optional[float] doc = MyAudio(url='https://www.kozco.com/tech/piano2.wav') doc.bytes_ = doc.url.load_bytes() doc.tensor, doc.frame_rate = doc.bytes_.load() # Note this is equivalent to do doc.tensor, doc.frame_rate = doc.url.load() assert isinstance(doc.tensor, AudioNdArray) ``` --- :return: tuple of an [`AudioNdArray`][docarray.typing.AudioNdArray] representing the audio bytes content, and an integer representing the frame rate. """ pydub = import_library('pydub', raise_error=True) # noqa: F841 from pydub import AudioSegment segment = AudioSegment.from_file(io.BytesIO(self)) # Convert to float32 using NumPy samples = np.array(segment.get_array_of_samples()) # Normalise float32 array so that values are between -1.0 and +1.0 samples_norm = samples / 2 ** (segment.sample_width * 8 - 1) return parse_obj_as(AudioNdArray, samples_norm), segment.frame_rate
import io from typing import TYPE_CHECKING, Any, Tuple, Type, TypeVar import numpy as np from pydantic import parse_obj_as from pydantic.validators import bytes_validator from docarray.typing.abstract_type import AbstractType from docarray.typing.proto_register import _register_proto from docarray.typing.tensor.audio import AudioNdArray from docarray.utils._internal.misc import import_library if TYPE_CHECKING: from pydantic.fields import BaseConfig, ModelField from docarray.proto import NodeProto T = TypeVar('T', bound='AudioBytes') @_register_proto(proto_type_name='audio_bytes') class AudioBytes(bytes, AbstractType): """ Bytes that store an audio and that can be load into an Audio tensor """ @classmethod def validate( cls: Type[T], value: Any, field: 'ModelField', config: 'BaseConfig', ) -> T: value = bytes_validator(value) return cls(value) @classmethod def from_protobuf(cls: Type[T], pb_msg: T) -> T: return parse_obj_as(cls, pb_msg) def _to_node_protobuf(self: T) -> 'NodeProto': from docarray.proto import NodeProto return NodeProto(blob=self, type=self._proto_type_name) def load(self) -> Tuple[AudioNdArray, int]: """ Load the Audio from the AudioBytes into an AudioNdArray --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioBytes, AudioNdArray, AudioUrl class MyAudio(BaseDoc): url: AudioUrl tensor: Optional[AudioNdArray] bytes_: Optional[AudioBytes] frame_rate: Optional[float] doc = MyAudio(url='https://www.kozco.com/tech/piano2.wav') doc.bytes_ = doc.url.load_bytes() doc.tensor, doc.frame_rate = doc.bytes_.load() # Note this is equivalent to do doc.tensor, doc.frame_rate = doc.url.load() assert isinstance(doc.tensor, AudioNdArray) ``` --- :return: tuple of an AudioNdArray representing the audio bytes content, and an integer representing the frame rate. """ pydub = import_library('pydub', raise_error=True) # noqa: F841 from pydub import AudioSegment segment = AudioSegment.from_file(io.BytesIO(self)) # Convert to float32 using NumPy samples = np.array(segment.get_array_of_samples()) # Normalise float32 array so that values are between -1.0 and +1.0 samples_norm = samples / 2 ** (segment.sample_width * 8 - 1) return parse_obj_as(AudioNdArray, samples_norm), segment.frame_rate
import json import os import pickle import numpy as np import xgboost as xgb kRows = 100 kCols = 10 def generate_data(): X = np.random.randn(kRows, kCols) y = np.random.randn(kRows) return X, y class TestPickling: def run_model_pickling(self, xgb_params) -> str: X, y = generate_data() dtrain = xgb.DMatrix(X, y) bst = xgb.train(xgb_params, dtrain) dump_0 = bst.get_dump(dump_format="json") assert dump_0 config_0 = bst.save_config() filename = "model.pkl" with open(filename, "wb") as fd: pickle.dump(bst, fd) with open(filename, "rb") as fd: bst = pickle.load(fd) with open(filename, "wb") as fd: pickle.dump(bst, fd) with open(filename, "rb") as fd: bst = pickle.load(fd) assert bst.get_dump(dump_format="json") == dump_0 if os.path.exists(filename): os.remove(filename) config_1 = bst.save_config() assert config_0 == config_1 return json.loads(config_0) def test_model_pickling_json(self): def check(config): tree_param = config["learner"]["gradient_booster"]["tree_train_param"] subsample = tree_param["subsample"] assert float(subsample) == 0.5 params = {"nthread": 8, "tree_method": "hist", "subsample": 0.5} config = self.run_model_pickling(params) check(config) params = {"nthread": 8, "tree_method": "exact", "subsample": 0.5} config = self.run_model_pickling(params) check(config)
import json import os import pickle import numpy as np import xgboost as xgb kRows = 100 kCols = 10 def generate_data(): X = np.random.randn(kRows, kCols) y = np.random.randn(kRows) return X, y class TestPickling: def run_model_pickling(self, xgb_params) -> str: X, y = generate_data() dtrain = xgb.DMatrix(X, y) bst = xgb.train(xgb_params, dtrain) dump_0 = bst.get_dump(dump_format='json') assert dump_0 config_0 = bst.save_config() filename = 'model.pkl' with open(filename, 'wb') as fd: pickle.dump(bst, fd) with open(filename, 'rb') as fd: bst = pickle.load(fd) with open(filename, 'wb') as fd: pickle.dump(bst, fd) with open(filename, 'rb') as fd: bst = pickle.load(fd) assert bst.get_dump(dump_format='json') == dump_0 if os.path.exists(filename): os.remove(filename) config_1 = bst.save_config() assert config_0 == config_1 return json.loads(config_0) def test_model_pickling_json(self): def check(config): tree_param = config["learner"]["gradient_booster"]["tree_train_param"] subsample = tree_param["subsample"] assert float(subsample) == 0.5 params = {"nthread": 8, "tree_method": "hist", "subsample": 0.5} config = self.run_model_pickling(params) check(config) params = {"nthread": 8, "tree_method": "exact", "subsample": 0.5} config = self.run_model_pickling(params) check(config)
# Copyright (c) OpenMMLab. All rights reserved. import torch.nn as nn from mmcv.cnn import ConvModule, is_norm from mmengine.model.utils import caffe2_xavier_init, constant_init, normal_init from torch.nn import BatchNorm2d from mmdet.registry import MODELS class Bottleneck(nn.Module): """Bottleneck block for DilatedEncoder used in `YOLOF. <https://arxiv.org/abs/2103.09460>`. The Bottleneck contains three ConvLayers and one residual connection. Args: in_channels (int): The number of input channels. mid_channels (int): The number of middle output channels. dilation (int): Dilation rate. norm_cfg (dict): Dictionary to construct and config norm layer. """ def __init__(self, in_channels, mid_channels, dilation, norm_cfg=dict(type='BN', requires_grad=True)): super(Bottleneck, self).__init__() self.conv1 = ConvModule( in_channels, mid_channels, 1, norm_cfg=norm_cfg) self.conv2 = ConvModule( mid_channels, mid_channels, 3, padding=dilation, dilation=dilation, norm_cfg=norm_cfg) self.conv3 = ConvModule( mid_channels, in_channels, 1, norm_cfg=norm_cfg) def forward(self, x): identity = x out = self.conv1(x) out = self.conv2(out) out = self.conv3(out) out = out + identity return out @MODELS.register_module() class DilatedEncoder(nn.Module): """Dilated Encoder for YOLOF <https://arxiv.org/abs/2103.09460>`. This module contains two types of components: - the original FPN lateral convolution layer and fpn convolution layer, which are 1x1 conv + 3x3 conv - the dilated residual block Args: in_channels (int): The number of input channels. out_channels (int): The number of output channels. block_mid_channels (int): The number of middle block output channels num_residual_blocks (int): The number of residual blocks. block_dilations (list): The list of residual blocks dilation. """ def __init__(self, in_channels, out_channels, block_mid_channels, num_residual_blocks, block_dilations): super(DilatedEncoder, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.block_mid_channels = block_mid_channels self.num_residual_blocks = num_residual_blocks self.block_dilations = block_dilations self._init_layers() def _init_layers(self): self.lateral_conv = nn.Conv2d( self.in_channels, self.out_channels, kernel_size=1) self.lateral_norm = BatchNorm2d(self.out_channels) self.fpn_conv = nn.Conv2d( self.out_channels, self.out_channels, kernel_size=3, padding=1) self.fpn_norm = BatchNorm2d(self.out_channels) encoder_blocks = [] for i in range(self.num_residual_blocks): dilation = self.block_dilations[i] encoder_blocks.append( Bottleneck( self.out_channels, self.block_mid_channels, dilation=dilation)) self.dilated_encoder_blocks = nn.Sequential(*encoder_blocks) def init_weights(self): caffe2_xavier_init(self.lateral_conv) caffe2_xavier_init(self.fpn_conv) for m in [self.lateral_norm, self.fpn_norm]: constant_init(m, 1) for m in self.dilated_encoder_blocks.modules(): if isinstance(m, nn.Conv2d): normal_init(m, mean=0, std=0.01) if is_norm(m): constant_init(m, 1) def forward(self, feature): out = self.lateral_norm(self.lateral_conv(feature[-1])) out = self.fpn_norm(self.fpn_conv(out)) return self.dilated_encoder_blocks(out),
# Copyright (c) OpenMMLab. All rights reserved. import torch.nn as nn from mmcv.cnn import (ConvModule, caffe2_xavier_init, constant_init, is_norm, normal_init) from torch.nn import BatchNorm2d from mmdet.registry import MODELS class Bottleneck(nn.Module): """Bottleneck block for DilatedEncoder used in `YOLOF. <https://arxiv.org/abs/2103.09460>`. The Bottleneck contains three ConvLayers and one residual connection. Args: in_channels (int): The number of input channels. mid_channels (int): The number of middle output channels. dilation (int): Dilation rate. norm_cfg (dict): Dictionary to construct and config norm layer. """ def __init__(self, in_channels, mid_channels, dilation, norm_cfg=dict(type='BN', requires_grad=True)): super(Bottleneck, self).__init__() self.conv1 = ConvModule( in_channels, mid_channels, 1, norm_cfg=norm_cfg) self.conv2 = ConvModule( mid_channels, mid_channels, 3, padding=dilation, dilation=dilation, norm_cfg=norm_cfg) self.conv3 = ConvModule( mid_channels, in_channels, 1, norm_cfg=norm_cfg) def forward(self, x): identity = x out = self.conv1(x) out = self.conv2(out) out = self.conv3(out) out = out + identity return out @MODELS.register_module() class DilatedEncoder(nn.Module): """Dilated Encoder for YOLOF <https://arxiv.org/abs/2103.09460>`. This module contains two types of components: - the original FPN lateral convolution layer and fpn convolution layer, which are 1x1 conv + 3x3 conv - the dilated residual block Args: in_channels (int): The number of input channels. out_channels (int): The number of output channels. block_mid_channels (int): The number of middle block output channels num_residual_blocks (int): The number of residual blocks. block_dilations (list): The list of residual blocks dilation. """ def __init__(self, in_channels, out_channels, block_mid_channels, num_residual_blocks, block_dilations): super(DilatedEncoder, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.block_mid_channels = block_mid_channels self.num_residual_blocks = num_residual_blocks self.block_dilations = block_dilations self._init_layers() def _init_layers(self): self.lateral_conv = nn.Conv2d( self.in_channels, self.out_channels, kernel_size=1) self.lateral_norm = BatchNorm2d(self.out_channels) self.fpn_conv = nn.Conv2d( self.out_channels, self.out_channels, kernel_size=3, padding=1) self.fpn_norm = BatchNorm2d(self.out_channels) encoder_blocks = [] for i in range(self.num_residual_blocks): dilation = self.block_dilations[i] encoder_blocks.append( Bottleneck( self.out_channels, self.block_mid_channels, dilation=dilation)) self.dilated_encoder_blocks = nn.Sequential(*encoder_blocks) def init_weights(self): caffe2_xavier_init(self.lateral_conv) caffe2_xavier_init(self.fpn_conv) for m in [self.lateral_norm, self.fpn_norm]: constant_init(m, 1) for m in self.dilated_encoder_blocks.modules(): if isinstance(m, nn.Conv2d): normal_init(m, mean=0, std=0.01) if is_norm(m): constant_init(m, 1) def forward(self, feature): out = self.lateral_norm(self.lateral_conv(feature[-1])) out = self.fpn_norm(self.fpn_conv(out)) return self.dilated_encoder_blocks(out),
import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDoc from docarray.documents import PointCloud3D from docarray.utils._internal.misc import is_tf_available from docarray.utils._internal.pydantic import is_pydantic_v2 from tests import TOYDATA_DIR tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp LOCAL_OBJ_FILE = str(TOYDATA_DIR / 'tetrahedron.obj') REMOTE_OBJ_FILE = 'https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj' @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize('file_url', [LOCAL_OBJ_FILE, REMOTE_OBJ_FILE]) def test_point_cloud(file_url): print(f"file_url = {file_url}") point_cloud = PointCloud3D(url=file_url) point_cloud.tensors = point_cloud.url.load(samples=100) assert isinstance(point_cloud.tensors.points, np.ndarray) @pytest.mark.skipif(is_pydantic_v2, reason="Not working with pydantic v2 for now") def test_point_cloud_np(): pc = parse_obj_as(PointCloud3D, np.zeros((10, 3))) assert (pc.tensors.points == np.zeros((10, 3))).all() @pytest.mark.skipif(is_pydantic_v2, reason="Not working with pydantic v2 for now") def test_point_cloud_torch(): pc = parse_obj_as(PointCloud3D, torch.zeros(10, 3)) assert (pc.tensors.points == torch.zeros(10, 3)).all() @pytest.mark.skipif(is_pydantic_v2, reason="Not working with pydantic v2 for now") @pytest.mark.tensorflow def test_point_cloud_tensorflow(): pc = parse_obj_as(PointCloud3D, tf.zeros((10, 3))) assert tnp.allclose(pc.tensors.points.tensor, tf.zeros((10, 3))) @pytest.mark.skipif(is_pydantic_v2, reason="Not working with pydantic v2 for now") def test_point_cloud_shortcut_doc(): class MyDoc(BaseDoc): pc: PointCloud3D pc2: PointCloud3D pc3: PointCloud3D doc = MyDoc( pc='http://myurl.ply', pc2=np.zeros((10, 3)), pc3=torch.zeros(10, 3), ) assert doc.pc.url == 'http://myurl.ply' assert (doc.pc2.tensors.points == np.zeros((10, 3))).all() assert (doc.pc3.tensors.points == torch.zeros(10, 3)).all() @pytest.mark.skipif(is_pydantic_v2, reason="Not working with pydantic v2 for now") @pytest.mark.tensorflow def test_point_cloud_shortcut_doc_tf(): class MyDoc(BaseDoc): pc: PointCloud3D pc2: PointCloud3D doc = MyDoc( pc='http://myurl.ply', pc2=tf.zeros((10, 3)), ) assert doc.pc.url == 'http://myurl.ply' assert tnp.allclose(doc.pc2.tensors.points.tensor, tf.zeros((10, 3)))
import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDoc from docarray.documents import PointCloud3D from docarray.utils._internal.misc import is_tf_available from tests import TOYDATA_DIR tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp LOCAL_OBJ_FILE = str(TOYDATA_DIR / 'tetrahedron.obj') REMOTE_OBJ_FILE = 'https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj' @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize('file_url', [LOCAL_OBJ_FILE, REMOTE_OBJ_FILE]) def test_point_cloud(file_url): print(f"file_url = {file_url}") point_cloud = PointCloud3D(url=file_url) point_cloud.tensors = point_cloud.url.load(samples=100) assert isinstance(point_cloud.tensors.points, np.ndarray) def test_point_cloud_np(): pc = parse_obj_as(PointCloud3D, np.zeros((10, 3))) assert (pc.tensors.points == np.zeros((10, 3))).all() def test_point_cloud_torch(): pc = parse_obj_as(PointCloud3D, torch.zeros(10, 3)) assert (pc.tensors.points == torch.zeros(10, 3)).all() @pytest.mark.tensorflow def test_point_cloud_tensorflow(): pc = parse_obj_as(PointCloud3D, tf.zeros((10, 3))) assert tnp.allclose(pc.tensors.points.tensor, tf.zeros((10, 3))) def test_point_cloud_shortcut_doc(): class MyDoc(BaseDoc): pc: PointCloud3D pc2: PointCloud3D pc3: PointCloud3D doc = MyDoc( pc='http://myurl.ply', pc2=np.zeros((10, 3)), pc3=torch.zeros(10, 3), ) assert doc.pc.url == 'http://myurl.ply' assert (doc.pc2.tensors.points == np.zeros((10, 3))).all() assert (doc.pc3.tensors.points == torch.zeros(10, 3)).all() @pytest.mark.tensorflow def test_point_cloud_shortcut_doc_tf(): class MyDoc(BaseDoc): pc: PointCloud3D pc2: PointCloud3D doc = MyDoc( pc='http://myurl.ply', pc2=tf.zeros((10, 3)), ) assert doc.pc.url == 'http://myurl.ply' assert tnp.allclose(doc.pc2.tensors.points.tensor, tf.zeros((10, 3)))
# 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 docarray.data.torch_dataset import MultiModalDataset __all__ = ['MultiModalDataset']
from docarray.data.torch_dataset import MultiModalDataset __all__ = ['MultiModalDataset']
from __future__ import annotations from typing import TYPE_CHECKING, Literal, Union from pydantic import model_validator from langchain_core.messages import BaseMessage, BaseMessageChunk from langchain_core.outputs.generation import Generation from langchain_core.utils._merge import merge_dicts if TYPE_CHECKING: from typing_extensions import Self class ChatGeneration(Generation): """A single chat generation output. A subclass of Generation that represents the response from a chat model that generates chat messages. The `message` attribute is a structured representation of the chat message. Most of the time, the message will be of type `AIMessage`. Users working with chat models will usually access information via either `AIMessage` (returned from runnable interfaces) or `LLMResult` (available via callbacks). """ text: str = "" """*SHOULD NOT BE SET DIRECTLY* The text contents of the output message.""" message: BaseMessage """The message output by the chat model.""" # Override type to be ChatGeneration, ignore mypy error as this is intentional type: Literal["ChatGeneration"] = "ChatGeneration" # type: ignore[assignment] """Type is used exclusively for serialization purposes.""" @model_validator(mode="after") def set_text(self) -> Self: """Set the text attribute to be the contents of the message. Args: values: The values of the object. Returns: The values of the object with the text attribute set. Raises: ValueError: If the message is not a string or a list. """ try: text = "" if isinstance(self.message.content, str): text = self.message.content # HACK: Assumes text in content blocks in OpenAI format. # Uses first text block. elif isinstance(self.message.content, list): for block in self.message.content: if isinstance(block, str): text = block break elif isinstance(block, dict) and "text" in block: text = block["text"] break else: pass else: pass self.text = text except (KeyError, AttributeError) as e: msg = "Error while initializing ChatGeneration" raise ValueError(msg) from e return self @classmethod def get_lc_namespace(cls) -> list[str]: """Get the namespace of the langchain object.""" return ["langchain", "schema", "output"] class ChatGenerationChunk(ChatGeneration): """ChatGeneration chunk, which can be concatenated with other ChatGeneration chunks. """ message: BaseMessageChunk """The message chunk output by the chat model.""" # Override type to be ChatGeneration, ignore mypy error as this is intentional type: Literal["ChatGenerationChunk"] = "ChatGenerationChunk" # type: ignore[assignment] """Type is used exclusively for serialization purposes.""" @classmethod def get_lc_namespace(cls) -> list[str]: """Get the namespace of the langchain object.""" return ["langchain", "schema", "output"] def __add__( self, other: Union[ChatGenerationChunk, list[ChatGenerationChunk]] ) -> ChatGenerationChunk: if isinstance(other, ChatGenerationChunk): generation_info = merge_dicts( self.generation_info or {}, other.generation_info or {}, ) return ChatGenerationChunk( message=self.message + other.message, generation_info=generation_info or None, ) elif isinstance(other, list) and all( isinstance(x, ChatGenerationChunk) for x in other ): generation_info = merge_dicts( self.generation_info or {}, *[chunk.generation_info for chunk in other if chunk.generation_info], ) return ChatGenerationChunk( message=self.message + [chunk.message for chunk in other], generation_info=generation_info or None, ) else: msg = ( f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'" ) raise TypeError(msg)
from __future__ import annotations from typing import Literal, Union from pydantic import model_validator from typing_extensions import Self from langchain_core.messages import BaseMessage, BaseMessageChunk from langchain_core.outputs.generation import Generation from langchain_core.utils._merge import merge_dicts class ChatGeneration(Generation): """A single chat generation output. A subclass of Generation that represents the response from a chat model that generates chat messages. The `message` attribute is a structured representation of the chat message. Most of the time, the message will be of type `AIMessage`. Users working with chat models will usually access information via either `AIMessage` (returned from runnable interfaces) or `LLMResult` (available via callbacks). """ text: str = "" """*SHOULD NOT BE SET DIRECTLY* The text contents of the output message.""" message: BaseMessage """The message output by the chat model.""" # Override type to be ChatGeneration, ignore mypy error as this is intentional type: Literal["ChatGeneration"] = "ChatGeneration" # type: ignore[assignment] """Type is used exclusively for serialization purposes.""" @model_validator(mode="after") def set_text(self) -> Self: """Set the text attribute to be the contents of the message. Args: values: The values of the object. Returns: The values of the object with the text attribute set. Raises: ValueError: If the message is not a string or a list. """ try: text = "" if isinstance(self.message.content, str): text = self.message.content # HACK: Assumes text in content blocks in OpenAI format. # Uses first text block. elif isinstance(self.message.content, list): for block in self.message.content: if isinstance(block, str): text = block break elif isinstance(block, dict) and "text" in block: text = block["text"] break else: pass else: pass self.text = text except (KeyError, AttributeError) as e: msg = "Error while initializing ChatGeneration" raise ValueError(msg) from e return self @classmethod def get_lc_namespace(cls) -> list[str]: """Get the namespace of the langchain object.""" return ["langchain", "schema", "output"] class ChatGenerationChunk(ChatGeneration): """ChatGeneration chunk, which can be concatenated with other ChatGeneration chunks. """ message: BaseMessageChunk """The message chunk output by the chat model.""" # Override type to be ChatGeneration, ignore mypy error as this is intentional type: Literal["ChatGenerationChunk"] = "ChatGenerationChunk" # type: ignore[assignment] """Type is used exclusively for serialization purposes.""" @classmethod def get_lc_namespace(cls) -> list[str]: """Get the namespace of the langchain object.""" return ["langchain", "schema", "output"] def __add__( self, other: Union[ChatGenerationChunk, list[ChatGenerationChunk]] ) -> ChatGenerationChunk: if isinstance(other, ChatGenerationChunk): generation_info = merge_dicts( self.generation_info or {}, other.generation_info or {}, ) return ChatGenerationChunk( message=self.message + other.message, generation_info=generation_info or None, ) elif isinstance(other, list) and all( isinstance(x, ChatGenerationChunk) for x in other ): generation_info = merge_dicts( self.generation_info or {}, *[chunk.generation_info for chunk in other if chunk.generation_info], ) return ChatGenerationChunk( message=self.message + [chunk.message for chunk in other], generation_info=generation_info or None, ) else: msg = ( f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'" ) raise TypeError(msg)
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.applications.nasnet import NASNetLarge as NASNetLarge from keras.src.applications.nasnet import NASNetMobile as NASNetMobile from keras.src.applications.nasnet import ( decode_predictions as decode_predictions, ) from keras.src.applications.nasnet import preprocess_input as preprocess_input
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.applications.nasnet import NASNetLarge from keras.src.applications.nasnet import NASNetMobile from keras.src.applications.nasnet import decode_predictions from keras.src.applications.nasnet import preprocess_input
# Copyright (c) OpenMMLab. All rights reserved. from ..builder import DETECTORS from .two_stage import TwoStageDetector @DETECTORS.register_module() class SparseRCNN(TwoStageDetector): r"""Implementation of `Sparse R-CNN: End-to-End Object Detection with Learnable Proposals <https://arxiv.org/abs/2011.12450>`_""" def __init__(self, *args, **kwargs): super(SparseRCNN, self).__init__(*args, **kwargs) assert self.with_rpn, 'Sparse R-CNN do not support external proposals' def forward_train(self, img, img_metas, gt_bboxes, gt_labels, gt_bboxes_ignore=None, gt_masks=None, proposals=None, **kwargs): """Forward function of SparseR-CNN in train stage. Args: img (Tensor): of shape (N, C, H, W) encoding input images. Typically these should be mean centered and std scaled. img_metas (list[dict]): list of image info dict where each dict has: 'img_shape', 'scale_factor', 'flip', and may also contain 'filename', 'ori_shape', 'pad_shape', and 'img_norm_cfg'. For details on the values of these keys see :class:`mmdet.datasets.pipelines.Collect`. gt_bboxes (list[Tensor]): Ground truth bboxes for each image with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format. gt_labels (list[Tensor]): class indices corresponding to each box gt_bboxes_ignore (None | list[Tensor): specify which bounding boxes can be ignored when computing the loss. gt_masks (List[Tensor], optional) : Segmentation masks for each box. But we don't support it in this architecture. proposals (List[Tensor], optional): override rpn proposals with custom proposals. Use when `with_rpn` is False. Returns: dict[str, Tensor]: a dictionary of loss components """ assert proposals is None, 'Sparse R-CNN does not support' \ ' external proposals' assert gt_masks is None, 'Sparse R-CNN does not instance segmentation' x = self.extract_feat(img) proposal_boxes, proposal_features, imgs_whwh = \ self.rpn_head.forward_train(x, img_metas) roi_losses = self.roi_head.forward_train( x, proposal_boxes, proposal_features, img_metas, gt_bboxes, gt_labels, gt_bboxes_ignore=gt_bboxes_ignore, gt_masks=gt_masks, imgs_whwh=imgs_whwh) return roi_losses def simple_test(self, img, img_metas, rescale=False): """Test function without test time augmentation. Args: imgs (list[torch.Tensor]): List of multiple images img_metas (list[dict]): List of image information. rescale (bool): Whether to rescale the results. Defaults to False. Returns: list[list[np.ndarray]]: BBox results of each image and classes. The outer list corresponds to each image. The inner list corresponds to each class. """ x = self.extract_feat(img) proposal_boxes, proposal_features, imgs_whwh = \ self.rpn_head.simple_test_rpn(x, img_metas) bbox_results = self.roi_head.simple_test( x, proposal_boxes, proposal_features, img_metas, imgs_whwh=imgs_whwh, rescale=rescale) return bbox_results def forward_dummy(self, img): """Used for computing network flops. See `mmdetection/tools/analysis_tools/get_flops.py` """ # backbone x = self.extract_feat(img) # rpn num_imgs = len(img) dummy_img_metas = [ dict(img_shape=(800, 1333, 3)) for _ in range(num_imgs) ] proposal_boxes, proposal_features, imgs_whwh = \ self.rpn_head.simple_test_rpn(x, dummy_img_metas) # roi_head roi_outs = self.roi_head.forward_dummy(x, proposal_boxes, proposal_features, dummy_img_metas) return roi_outs
from ..builder import DETECTORS from .two_stage import TwoStageDetector @DETECTORS.register_module() class SparseRCNN(TwoStageDetector): r"""Implementation of `Sparse R-CNN: End-to-End Object Detection with Learnable Proposals <https://arxiv.org/abs/2011.12450>`_""" def __init__(self, *args, **kwargs): super(SparseRCNN, self).__init__(*args, **kwargs) assert self.with_rpn, 'Sparse R-CNN do not support external proposals' def forward_train(self, img, img_metas, gt_bboxes, gt_labels, gt_bboxes_ignore=None, gt_masks=None, proposals=None, **kwargs): """Forward function of SparseR-CNN in train stage. Args: img (Tensor): of shape (N, C, H, W) encoding input images. Typically these should be mean centered and std scaled. img_metas (list[dict]): list of image info dict where each dict has: 'img_shape', 'scale_factor', 'flip', and may also contain 'filename', 'ori_shape', 'pad_shape', and 'img_norm_cfg'. For details on the values of these keys see :class:`mmdet.datasets.pipelines.Collect`. gt_bboxes (list[Tensor]): Ground truth bboxes for each image with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format. gt_labels (list[Tensor]): class indices corresponding to each box gt_bboxes_ignore (None | list[Tensor): specify which bounding boxes can be ignored when computing the loss. gt_masks (List[Tensor], optional) : Segmentation masks for each box. But we don't support it in this architecture. proposals (List[Tensor], optional): override rpn proposals with custom proposals. Use when `with_rpn` is False. Returns: dict[str, Tensor]: a dictionary of loss components """ assert proposals is None, 'Sparse R-CNN does not support' \ ' external proposals' assert gt_masks is None, 'Sparse R-CNN does not instance segmentation' x = self.extract_feat(img) proposal_boxes, proposal_features, imgs_whwh = \ self.rpn_head.forward_train(x, img_metas) roi_losses = self.roi_head.forward_train( x, proposal_boxes, proposal_features, img_metas, gt_bboxes, gt_labels, gt_bboxes_ignore=gt_bboxes_ignore, gt_masks=gt_masks, imgs_whwh=imgs_whwh) return roi_losses def simple_test(self, img, img_metas, rescale=False): """Test function without test time augmentation. Args: imgs (list[torch.Tensor]): List of multiple images img_metas (list[dict]): List of image information. rescale (bool): Whether to rescale the results. Defaults to False. Returns: list[list[np.ndarray]]: BBox results of each image and classes. The outer list corresponds to each image. The inner list corresponds to each class. """ x = self.extract_feat(img) proposal_boxes, proposal_features, imgs_whwh = \ self.rpn_head.simple_test_rpn(x, img_metas) bbox_results = self.roi_head.simple_test( x, proposal_boxes, proposal_features, img_metas, imgs_whwh=imgs_whwh, rescale=rescale) return bbox_results def forward_dummy(self, img): """Used for computing network flops. See `mmdetection/tools/analysis_tools/get_flops.py` """ # backbone x = self.extract_feat(img) # rpn num_imgs = len(img) dummy_img_metas = [ dict(img_shape=(800, 1333, 3)) for _ in range(num_imgs) ] proposal_boxes, proposal_features, imgs_whwh = \ self.rpn_head.simple_test_rpn(x, dummy_img_metas) # roi_head roi_outs = self.roi_head.forward_dummy(x, proposal_boxes, proposal_features, dummy_img_metas) return roi_outs
# Copyright (c) OpenMMLab. All rights reserved. import asyncio from argparse import ArgumentParser from mmdet.apis import (async_inference_detector, inference_detector, init_detector, show_result_pyplot) def parse_args(): parser = ArgumentParser() parser.add_argument('img', help='Image file') parser.add_argument('config', help='Config file') parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument('--out-file', default=None, help='Path to output file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--palette', default='coco', choices=['coco', 'voc', 'citys', 'random'], help='Color palette used for visualization') parser.add_argument( '--score-thr', type=float, default=0.3, help='bbox score threshold') parser.add_argument( '--async-test', action='store_true', help='whether to set async options for async inference.') args = parser.parse_args() return args def main(args): # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single image result = inference_detector(model, args.img) # show the results show_result_pyplot( model, args.img, result, palette=args.palette, score_thr=args.score_thr, out_file=args.out_file) async def async_main(args): # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single image tasks = asyncio.create_task(async_inference_detector(model, args.img)) result = await asyncio.gather(tasks) # show the results show_result_pyplot( model, args.img, result[0], palette=args.palette, score_thr=args.score_thr, out_file=args.out_file) if __name__ == '__main__': args = parse_args() if args.async_test: asyncio.run(async_main(args)) else: main(args)
# Copyright (c) OpenMMLab. All rights reserved. import asyncio from argparse import ArgumentParser from mmdet.apis import (async_inference_detector, inference_detector, init_detector, show_result_pyplot) def parse_args(): parser = ArgumentParser() parser.add_argument('img', help='Image file') parser.add_argument('config', help='Config file') parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--palette', default='coco', choices=['coco', 'voc', 'citys', 'random'], help='Color palette used for visualization') parser.add_argument( '--score-thr', type=float, default=0.3, help='bbox score threshold') parser.add_argument( '--async-test', action='store_true', help='whether to set async options for async inference.') args = parser.parse_args() return args def main(args): # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single image result = inference_detector(model, args.img) # show the results show_result_pyplot( model, args.img, result, palette=args.palette, score_thr=args.score_thr) async def async_main(args): # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single image tasks = asyncio.create_task(async_inference_detector(model, args.img)) result = await asyncio.gather(tasks) # show the results show_result_pyplot( model, args.img, result[0], palette=args.palette, score_thr=args.score_thr) if __name__ == '__main__': args = parse_args() if args.async_test: asyncio.run(async_main(args)) else: main(args)
import datetime import sys from unittest import TestCase from mmengine import DefaultScope from mmdet.utils import register_all_modules class TestSetupEnv(TestCase): def test_register_all_modules(self): from mmdet.registry import DATASETS # not init default scope sys.modules.pop('mmdet.datasets', None) sys.modules.pop('mmdet.datasets.coco', None) DATASETS._module_dict.pop('CocoDataset', None) self.assertFalse('CocoDataset' in DATASETS.module_dict) register_all_modules(init_default_scope=False) self.assertTrue('CocoDataset' in DATASETS.module_dict) # init default scope sys.modules.pop('mmdet.datasets') sys.modules.pop('mmdet.datasets.coco') DATASETS._module_dict.pop('CocoDataset', None) self.assertFalse('CocoDataset' in DATASETS.module_dict) register_all_modules(init_default_scope=True) self.assertTrue('CocoDataset' in DATASETS.module_dict) self.assertEqual(DefaultScope.get_current_instance().scope_name, 'mmdet') # init default scope when another scope is init name = f'test-{datetime.datetime.now()}' DefaultScope.get_instance(name, scope_name='test') with self.assertWarnsRegex( Warning, 'The current default scope "test" is not "mmdet"'): register_all_modules(init_default_scope=True)
import sys from unittest import TestCase from mmengine import DefaultScope from mmdet.utils import register_all_modules class TestSetupEnv(TestCase): def test_register_all_modules(self): from mmdet.registry import DATASETS # not init default scope sys.modules.pop('mmdet.datasets', None) sys.modules.pop('mmdet.datasets.coco', None) DATASETS._module_dict.pop('CocoDataset', None) self.assertFalse('CocoDataset' in DATASETS.module_dict) register_all_modules(init_default_scope=False) self.assertTrue('CocoDataset' in DATASETS.module_dict) # init default scope sys.modules.pop('mmdet.datasets') sys.modules.pop('mmdet.datasets.coco') DATASETS._module_dict.pop('CocoDataset', None) self.assertFalse('CocoDataset' in DATASETS.module_dict) register_all_modules(init_default_scope=True) self.assertTrue('CocoDataset' in DATASETS.module_dict) self.assertEqual(DefaultScope.get_current_instance().scope_name, 'mmdet')
import json import os from typing import Dict import torch 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): with open(os.path.join(output_path, "config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) 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) model.load_state_dict( torch.load(os.path.join(input_path, "pytorch_model.bin"), map_location=torch.device("cpu")) ) return model
import torch from torch import Tensor from torch import nn from typing import Dict import os import json 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): with open(os.path.join(output_path, "config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) 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) model.load_state_dict( torch.load(os.path.join(input_path, "pytorch_model.bin"), map_location=torch.device("cpu")) ) return model
# Copyright (c) OpenMMLab. All rights reserved. from .hub import load_url from .misc import (check_prerequisites, concat_list, deprecated_api_warning, find_latest_checkpoint, has_method, import_modules_from_strings, is_list_of, is_method_overridden, is_seq_of, is_str, is_tuple_of, iter_cast, list_cast, mmcv_full_available, requires_executable, requires_package, slice_list, to_1tuple, to_2tuple, to_3tuple, to_4tuple, to_ntuple, tuple_cast) from .parrots_wrapper import TORCH_VERSION from .path import (check_file_exist, fopen, is_filepath, mkdir_or_exist, scandir, symlink) from .version_utils import digit_version, get_git_hash __all__ = [ 'is_str', 'iter_cast', 'list_cast', 'tuple_cast', 'is_seq_of', 'is_list_of', 'is_tuple_of', 'slice_list', 'concat_list', 'check_prerequisites', 'requires_package', 'requires_executable', 'is_filepath', 'fopen', 'check_file_exist', 'mkdir_or_exist', 'symlink', 'scandir', 'deprecated_api_warning', 'import_modules_from_strings', 'to_1tuple', 'to_2tuple', 'to_3tuple', 'to_4tuple', 'to_ntuple', 'is_method_overridden', 'has_method', 'mmcv_full_available', 'digit_version', 'get_git_hash', 'TORCH_VERSION', 'load_url', 'find_latest_checkpoint' ]
# Copyright (c) OpenMMLab. All rights reserved. from .hub import load_url from .misc import (check_prerequisites, concat_list, deprecated_api_warning, has_method, import_modules_from_strings, is_list_of, is_method_overridden, is_seq_of, is_str, is_tuple_of, iter_cast, list_cast, mmcv_full_available, requires_executable, requires_package, slice_list, to_1tuple, to_2tuple, to_3tuple, to_4tuple, to_ntuple, tuple_cast) from .parrots_wrapper import TORCH_VERSION from .path import (check_file_exist, fopen, is_filepath, mkdir_or_exist, scandir, symlink) from .version_utils import digit_version, get_git_hash __all__ = [ 'is_str', 'iter_cast', 'list_cast', 'tuple_cast', 'is_seq_of', 'is_list_of', 'is_tuple_of', 'slice_list', 'concat_list', 'check_prerequisites', 'requires_package', 'requires_executable', 'is_filepath', 'fopen', 'check_file_exist', 'mkdir_or_exist', 'symlink', 'scandir', 'deprecated_api_warning', 'import_modules_from_strings', 'to_1tuple', 'to_2tuple', 'to_3tuple', 'to_4tuple', 'to_ntuple', 'is_method_overridden', 'has_method', 'mmcv_full_available', 'digit_version', 'get_git_hash', 'TORCH_VERSION', 'load_url' ]
def __getattr__(name: str): import warnings warnings.warn( "Torchaudio's I/O functions now support per-call backend dispatch. " "Importing backend implementation directly is no longer guaranteed to work. " "Please use `backend` keyword with load/save/info function, instead of " "calling the underlying implementation directly.", stacklevel=2, ) from torchaudio._backend import soundfile_backend return getattr(soundfile_backend, name)
def __getattr__(name: str): import warnings warnings.warn( "Torchaudio's I/O functions now support par-call bakcend dispatch. " "Importing backend implementation directly is no longer guaranteed to work. " "Please use `backend` keyword with load/save/info function, instead of " "calling the udnerlying implementation directly.", stacklevel=2, ) from torchaudio._backend import soundfile_backend return getattr(soundfile_backend, name)
import numpy as np from docarray import BaseDocument, DocumentArray, Image, Text from docarray.array.array_stacked import DocumentArrayStacked from docarray.typing import NdArray def test_simple_proto(): class CustomDoc(BaseDocument): text: str tensor: NdArray da = DocumentArray( [CustomDoc(text='hello', tensor=np.zeros((3, 224, 224))) for _ in range(10)] ) new_da = DocumentArray[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() def test_nested_proto(): class CustomDocument(BaseDocument): text: Text image: Image da = DocumentArray[CustomDocument]( [ CustomDocument( text=Text(text='hello'), image=Image(tensor=np.zeros((3, 224, 224))) ) for _ in range(10) ] ) DocumentArray[CustomDocument].from_protobuf(da.to_protobuf()) def test_nested_proto_any_doc(): class CustomDocument(BaseDocument): text: Text image: Image da = DocumentArray[CustomDocument]( [ CustomDocument( text=Text(text='hello'), image=Image(tensor=np.zeros((3, 224, 224))) ) for _ in range(10) ] ) DocumentArray.from_protobuf(da.to_protobuf()) def test_stacked_proto(): class CustomDocument(BaseDocument): image: NdArray da = DocumentArray[CustomDocument]( [CustomDocument(image=np.zeros((3, 224, 224))) for _ in range(10)] ).stack() da2 = DocumentArrayStacked.from_protobuf(da.to_protobuf()) assert isinstance(da2, DocumentArrayStacked)
import numpy as np from docarray import Document, DocumentArray, Image, Text from docarray.array.array_stacked import DocumentArrayStacked from docarray.typing import NdArray def test_simple_proto(): class CustomDoc(Document): text: str tensor: NdArray da = DocumentArray( [CustomDoc(text='hello', tensor=np.zeros((3, 224, 224))) for _ in range(10)] ) new_da = DocumentArray[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() def test_nested_proto(): class CustomDocument(Document): text: Text image: Image da = DocumentArray[CustomDocument]( [ CustomDocument( text=Text(text='hello'), image=Image(tensor=np.zeros((3, 224, 224))) ) for _ in range(10) ] ) DocumentArray[CustomDocument].from_protobuf(da.to_protobuf()) def test_nested_proto_any_doc(): class CustomDocument(Document): text: Text image: Image da = DocumentArray[CustomDocument]( [ CustomDocument( text=Text(text='hello'), image=Image(tensor=np.zeros((3, 224, 224))) ) for _ in range(10) ] ) DocumentArray.from_protobuf(da.to_protobuf()) def test_stacked_proto(): class CustomDocument(Document): image: NdArray da = DocumentArray[CustomDocument]( [CustomDocument(image=np.zeros((3, 224, 224))) for _ in range(10)] ).stack() da2 = DocumentArrayStacked.from_protobuf(da.to_protobuf()) assert isinstance(da2, DocumentArrayStacked)
"""Tool for the Google Lens""" from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun from langchain_core.tools import BaseTool from langchain_community.utilities.google_lens import GoogleLensAPIWrapper class GoogleLensQueryRun(BaseTool): """Tool that queries the Google Lens API.""" name: str = "google_lens" description: str = ( "A wrapper around Google Lens Search. " "Useful for when you need to get information related" "to an image from Google Lens" "Input should be a url to an image." ) api_wrapper: GoogleLensAPIWrapper def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" return self.api_wrapper.run(query)
"""Tool for the Google Lens""" from typing import Optional from langchain_core.callbacks import CallbackManagerForToolRun from langchain_core.tools import BaseTool from langchain_community.utilities.google_lens import GoogleLensAPIWrapper class GoogleLensQueryRun(BaseTool): # type: ignore[override] """Tool that queries the Google Lens API.""" name: str = "google_lens" description: str = ( "A wrapper around Google Lens Search. " "Useful for when you need to get information related" "to an image from Google Lens" "Input should be a url to an image." ) api_wrapper: GoogleLensAPIWrapper def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" return self.api_wrapper.run(query)
import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder from datasets.utils._hf_hub_fixes import create_repo, delete_repo CI_HUB_USER = "__DUMMY_TRANSFORMERS_USER__" CI_HUB_USER_FULL_NAME = "Dummy User" CI_HUB_USER_TOKEN = "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" CI_HUB_ENDPOINT = "https://hub-ci.huggingface.co" CI_HUB_DATASETS_URL = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" CI_HFH_HUGGINGFACE_CO_URL_TEMPLATE = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" CI_HUB_TOKEN_PATH = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def ci_hfh_hf_hub_url(monkeypatch): monkeypatch.setattr( "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE", CI_HFH_HUGGINGFACE_CO_URL_TEMPLATE ) @pytest.fixture def ci_hub_config(monkeypatch): monkeypatch.setattr("datasets.config.HF_ENDPOINT", CI_HUB_ENDPOINT) monkeypatch.setattr("datasets.config.HUB_DATASETS_URL", CI_HUB_DATASETS_URL) @pytest.fixture def ci_hub_token_path(monkeypatch): monkeypatch.setattr("huggingface_hub.hf_api.HfFolder.path_token", CI_HUB_TOKEN_PATH) @pytest.fixture def set_ci_hub_access_token(ci_hub_config, ci_hub_token_path): HfFolder.save_token(CI_HUB_USER_TOKEN) yield HfFolder.delete_token() @pytest.fixture(scope="session") def hf_api(): return HfApi(endpoint=CI_HUB_ENDPOINT) @pytest.fixture(scope="session") def hf_token(hf_api: HfApi): previous_token = HfFolder.get_token() HfFolder.save_token(CI_HUB_USER_TOKEN) yield CI_HUB_USER_TOKEN if previous_token is not None: HfFolder.save_token(previous_token) @pytest.fixture def cleanup_repo(hf_api): def _cleanup_repo(repo_id): delete_repo(hf_api, repo_id, token=CI_HUB_USER_TOKEN, repo_type="dataset") return _cleanup_repo @pytest.fixture def temporary_repo(cleanup_repo): @contextmanager def _temporary_repo(repo_id): try: yield repo_id finally: cleanup_repo(repo_id) return _temporary_repo @pytest.fixture(scope="session") def hf_private_dataset_repo_txt_data_(hf_api: HfApi, hf_token, text_file): repo_name = f"repo_txt_data-{int(time.time() * 10e3)}" repo_id = f"{CI_HUB_USER}/{repo_name}" create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True) hf_api.upload_file( token=hf_token, path_or_fileobj=str(text_file), path_in_repo="data/text_data.txt", repo_id=repo_id, repo_type="dataset", ) yield repo_id try: delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset") except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def hf_private_dataset_repo_txt_data(hf_private_dataset_repo_txt_data_, ci_hub_config, ci_hfh_hf_hub_url): return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="session") def hf_private_dataset_repo_zipped_txt_data_(hf_api: HfApi, hf_token, zip_csv_with_dir_path): repo_name = f"repo_zipped_txt_data-{int(time.time() * 10e3)}" repo_id = f"{CI_HUB_USER}/{repo_name}" create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True) hf_api.upload_file( token=hf_token, path_or_fileobj=str(zip_csv_with_dir_path), path_in_repo="data.zip", repo_id=repo_id, repo_type="dataset", ) yield repo_id try: delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset") except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def hf_private_dataset_repo_zipped_txt_data( hf_private_dataset_repo_zipped_txt_data_, ci_hub_config, ci_hfh_hf_hub_url ): return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="session") def hf_private_dataset_repo_zipped_img_data_(hf_api: HfApi, hf_token, zip_image_path): repo_name = f"repo_zipped_img_data-{int(time.time() * 10e3)}" repo_id = f"{CI_HUB_USER}/{repo_name}" create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True) hf_api.upload_file( token=hf_token, path_or_fileobj=str(zip_image_path), path_in_repo="data.zip", repo_id=repo_id, repo_type="dataset", ) yield repo_id try: delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset") except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def hf_private_dataset_repo_zipped_img_data( hf_private_dataset_repo_zipped_img_data_, ci_hub_config, ci_hfh_hf_hub_url ): return hf_private_dataset_repo_zipped_img_data_
import time from contextlib import contextmanager from pathlib import Path import pytest import requests from huggingface_hub.hf_api import HfApi, HfFolder from datasets.utils._hf_hub_fixes import create_repo, delete_repo CI_HUB_USER = "__DUMMY_TRANSFORMERS_USER__" CI_HUB_USER_FULL_NAME = "Dummy User" CI_HUB_USER_TOKEN = "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" CI_HUB_ENDPOINT = "https://hub-ci.huggingface.co" CI_HUB_DATASETS_URL = CI_HUB_ENDPOINT + "/datasets/{repo_id}/resolve/{revision}/{path}" CI_HFH_HUGGINGFACE_CO_URL_TEMPLATE = CI_HUB_ENDPOINT + "/{repo_id}/resolve/{revision}/{filename}" CI_HUB_TOKEN_PATH = Path("~/.huggingface/hub_ci_token").expanduser() @pytest.fixture def ci_hfh_hf_hub_url(monkeypatch): monkeypatch.setattr( "huggingface_hub.file_download.HUGGINGFACE_CO_URL_TEMPLATE", CI_HFH_HUGGINGFACE_CO_URL_TEMPLATE ) @pytest.fixture def ci_hub_config(monkeypatch): monkeypatch.setattr("datasets.config.HF_ENDPOINT", CI_HUB_ENDPOINT) monkeypatch.setattr("datasets.config.HUB_DATASETS_URL", CI_HUB_DATASETS_URL) @pytest.fixture def ci_hub_token_path(monkeypatch): monkeypatch.setattr("huggingface_hub.hf_api.HfFolder.path_token", CI_HUB_TOKEN_PATH) @pytest.fixture def set_ci_hub_access_token(ci_hub_config, ci_hub_token_path): _api = HfApi(endpoint=CI_HUB_ENDPOINT) _api.set_access_token(CI_HUB_USER_TOKEN) HfFolder.save_token(CI_HUB_USER_TOKEN) yield HfFolder.delete_token() _api.unset_access_token() @pytest.fixture(scope="session") def hf_api(): return HfApi(endpoint=CI_HUB_ENDPOINT) @pytest.fixture(scope="session") def hf_token(hf_api: HfApi): hf_api.set_access_token(CI_HUB_USER_TOKEN) HfFolder.save_token(CI_HUB_USER_TOKEN) yield CI_HUB_USER_TOKEN try: hf_api.unset_access_token() except requests.exceptions.HTTPError: pass @pytest.fixture def cleanup_repo(hf_api): def _cleanup_repo(repo_id): delete_repo(hf_api, repo_id, token=CI_HUB_USER_TOKEN, repo_type="dataset") return _cleanup_repo @pytest.fixture def temporary_repo(cleanup_repo): @contextmanager def _temporary_repo(repo_id): try: yield repo_id finally: cleanup_repo(repo_id) return _temporary_repo @pytest.fixture(scope="session") def hf_private_dataset_repo_txt_data_(hf_api: HfApi, hf_token, text_file): repo_name = f"repo_txt_data-{int(time.time() * 10e3)}" repo_id = f"{CI_HUB_USER}/{repo_name}" create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True) hf_api.upload_file( token=hf_token, path_or_fileobj=str(text_file), path_in_repo="data/text_data.txt", repo_id=repo_id, repo_type="dataset", ) yield repo_id try: delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset") except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def hf_private_dataset_repo_txt_data(hf_private_dataset_repo_txt_data_, ci_hub_config, ci_hfh_hf_hub_url): return hf_private_dataset_repo_txt_data_ @pytest.fixture(scope="session") def hf_private_dataset_repo_zipped_txt_data_(hf_api: HfApi, hf_token, zip_csv_with_dir_path): repo_name = f"repo_zipped_txt_data-{int(time.time() * 10e3)}" repo_id = f"{CI_HUB_USER}/{repo_name}" create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True) hf_api.upload_file( token=hf_token, path_or_fileobj=str(zip_csv_with_dir_path), path_in_repo="data.zip", repo_id=repo_id, repo_type="dataset", ) yield repo_id try: delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset") except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def hf_private_dataset_repo_zipped_txt_data( hf_private_dataset_repo_zipped_txt_data_, ci_hub_config, ci_hfh_hf_hub_url ): return hf_private_dataset_repo_zipped_txt_data_ @pytest.fixture(scope="session") def hf_private_dataset_repo_zipped_img_data_(hf_api: HfApi, hf_token, zip_image_path): repo_name = f"repo_zipped_img_data-{int(time.time() * 10e3)}" repo_id = f"{CI_HUB_USER}/{repo_name}" create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True) hf_api.upload_file( token=hf_token, path_or_fileobj=str(zip_image_path), path_in_repo="data.zip", repo_id=repo_id, repo_type="dataset", ) yield repo_id try: delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset") except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error pass @pytest.fixture() def hf_private_dataset_repo_zipped_img_data( hf_private_dataset_repo_zipped_img_data_, ci_hub_config, ci_hfh_hf_hub_url ): return hf_private_dataset_repo_zipped_img_data_
"""Init file.""" from llama_index.readers.gpt_repo.base import ( GPTRepoReader, get_ignore_list, process_repository, should_ignore, ) __all__ = [ "GPTRepoReader", "get_ignore_list", "process_repository", "should_ignore", ]
"""Init file.""" from llama_index.readers.gpt_repo.base import ( GPTRepoReader, get_ignore_list, process_repository, should_ignore, ) __all__ = [ "GPTRepoReader", "get_ignore_list", "process_repository", "should_ignore", ]
from .PhraseTokenizer import PhraseTokenizer from .WhitespaceTokenizer import WhitespaceTokenizer from .WordTokenizer import ENGLISH_STOP_WORDS, WordTokenizer __all__ = ["WordTokenizer", "WhitespaceTokenizer", "PhraseTokenizer", "ENGLISH_STOP_WORDS"]
from .WordTokenizer import WordTokenizer, ENGLISH_STOP_WORDS from .WhitespaceTokenizer import WhitespaceTokenizer from .PhraseTokenizer import PhraseTokenizer __all__ = ["WordTokenizer", "WhitespaceTokenizer", "PhraseTokenizer", "ENGLISH_STOP_WORDS"]
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.dtype_policies import deserialize from keras.src.dtype_policies import get from keras.src.dtype_policies import serialize from keras.src.dtype_policies.dtype_policy import DTypePolicy from keras.src.dtype_policies.dtype_policy import FloatDTypePolicy from keras.src.dtype_policies.dtype_policy import QuantizedDTypePolicy from keras.src.dtype_policies.dtype_policy import QuantizedFloat8DTypePolicy from keras.src.dtype_policies.dtype_policy_map import DTypePolicyMap
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.dtype_policies import deserialize from keras.src.dtype_policies import get from keras.src.dtype_policies import serialize from keras.src.dtype_policies.dtype_policy import DTypePolicy from keras.src.dtype_policies.dtype_policy import FloatDTypePolicy from keras.src.dtype_policies.dtype_policy import QuantizedDTypePolicy from keras.src.dtype_policies.dtype_policy import QuantizedFloat8DTypePolicy
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.document_loaders import ( TelegramChatApiLoader, TelegramChatFileLoader, ) from langchain_community.document_loaders.telegram import ( concatenate_rows, text_to_docs, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "concatenate_rows": "langchain_community.document_loaders.telegram", "TelegramChatFileLoader": "langchain_community.document_loaders", "text_to_docs": "langchain_community.document_loaders.telegram", "TelegramChatApiLoader": "langchain_community.document_loaders", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "TelegramChatApiLoader", "TelegramChatFileLoader", "concatenate_rows", "text_to_docs", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.document_loaders import ( TelegramChatApiLoader, TelegramChatFileLoader, ) from langchain_community.document_loaders.telegram import ( concatenate_rows, text_to_docs, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "concatenate_rows": "langchain_community.document_loaders.telegram", "TelegramChatFileLoader": "langchain_community.document_loaders", "text_to_docs": "langchain_community.document_loaders.telegram", "TelegramChatApiLoader": "langchain_community.document_loaders", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "concatenate_rows", "TelegramChatFileLoader", "text_to_docs", "TelegramChatApiLoader", ]
from docarray import BaseDoc, DocList from docarray.documents import ImageDoc from docarray.typing import NdArray class MyDoc(BaseDoc): embedding: NdArray text: str image: ImageDoc def test_from_to_json(): da = DocList[MyDoc]( [ MyDoc( embedding=[1, 2, 3, 4, 5], text='hello', image=ImageDoc(url='aux.png') ), MyDoc(embedding=[5, 4, 3, 2, 1], text='hello world', image=ImageDoc()), ] ) json_da = da.to_json() da2 = DocList[MyDoc].from_json(json_da) assert len(da2) == 2 assert len(da) == len(da2) for d1, d2 in zip(da, da2): assert d1.embedding.tolist() == d2.embedding.tolist() assert d1.text == d2.text assert d1.image.url == d2.image.url assert da[1].image.url is None assert da2[1].image.url is None def test_union_type(): from typing import Union from docarray.documents import TextDoc class CustomDoc(BaseDoc): ud: Union[TextDoc, ImageDoc] = TextDoc(text='union type') docs = DocList[CustomDoc]([CustomDoc(ud=TextDoc(text='union type'))]) docs_copy = docs.from_json(docs.to_json()) assert docs == docs_copy
from docarray import BaseDoc, DocList from docarray.documents import ImageDoc from docarray.typing import NdArray class MyDoc(BaseDoc): embedding: NdArray text: str image: ImageDoc def test_from_to_json(): da = DocList[MyDoc]( [ MyDoc( embedding=[1, 2, 3, 4, 5], text='hello', image=ImageDoc(url='aux.png') ), MyDoc(embedding=[5, 4, 3, 2, 1], text='hello world', image=ImageDoc()), ] ) json_da = da.to_json() da2 = DocList[MyDoc].from_json(json_da) assert len(da2) == 2 assert len(da) == len(da2) for d1, d2 in zip(da, da2): assert d1.embedding.tolist() == d2.embedding.tolist() assert d1.text == d2.text assert d1.image.url == d2.image.url assert da[1].image.url is None assert da2[1].image.url is None
from typing import Union import PIL.Image import torch from torchvision.prototype import features from torchvision.transforms.functional import pil_to_tensor, to_pil_image def erase_image_tensor( image: torch.Tensor, i: int, j: int, h: int, w: int, v: torch.Tensor, inplace: bool = False ) -> torch.Tensor: if not inplace: image = image.clone() image[..., i : i + h, j : j + w] = v return image @torch.jit.unused def erase_image_pil( image: PIL.Image.Image, i: int, j: int, h: int, w: int, v: torch.Tensor, inplace: bool = False ) -> PIL.Image.Image: t_img = pil_to_tensor(image) output = erase_image_tensor(t_img, i=i, j=j, h=h, w=w, v=v, inplace=inplace) return to_pil_image(output, mode=image.mode) def erase_video( video: torch.Tensor, i: int, j: int, h: int, w: int, v: torch.Tensor, inplace: bool = False ) -> torch.Tensor: return erase_image_tensor(video, i=i, j=j, h=h, w=w, v=v, inplace=inplace) def erase( inpt: Union[features.ImageTypeJIT, features.VideoTypeJIT], i: int, j: int, h: int, w: int, v: torch.Tensor, inplace: bool = False, ) -> Union[features.ImageTypeJIT, features.VideoTypeJIT]: if isinstance(inpt, torch.Tensor) and ( torch.jit.is_scripting() or not isinstance(inpt, (features.Image, features.Video)) ): return erase_image_tensor(inpt, i=i, j=j, h=h, w=w, v=v, inplace=inplace) elif isinstance(inpt, features.Image): output = erase_image_tensor(inpt.as_subclass(torch.Tensor), i=i, j=j, h=h, w=w, v=v, inplace=inplace) return features.Image.wrap_like(inpt, output) elif isinstance(inpt, features.Video): output = erase_video(inpt.as_subclass(torch.Tensor), i=i, j=j, h=h, w=w, v=v, inplace=inplace) return features.Video.wrap_like(inpt, output) else: # isinstance(inpt, PIL.Image.Image): return erase_image_pil(inpt, i=i, j=j, h=h, w=w, v=v, inplace=inplace)
from typing import Union import PIL.Image import torch from torchvision.prototype import features from torchvision.transforms import functional_tensor as _FT from torchvision.transforms.functional import pil_to_tensor, to_pil_image erase_image_tensor = _FT.erase @torch.jit.unused def erase_image_pil( image: PIL.Image.Image, i: int, j: int, h: int, w: int, v: torch.Tensor, inplace: bool = False ) -> PIL.Image.Image: t_img = pil_to_tensor(image) output = erase_image_tensor(t_img, i=i, j=j, h=h, w=w, v=v, inplace=inplace) return to_pil_image(output, mode=image.mode) def erase_video( video: torch.Tensor, i: int, j: int, h: int, w: int, v: torch.Tensor, inplace: bool = False ) -> torch.Tensor: return erase_image_tensor(video, i=i, j=j, h=h, w=w, v=v, inplace=inplace) def erase( inpt: Union[features.ImageTypeJIT, features.VideoTypeJIT], i: int, j: int, h: int, w: int, v: torch.Tensor, inplace: bool = False, ) -> Union[features.ImageTypeJIT, features.VideoTypeJIT]: if isinstance(inpt, torch.Tensor) and ( torch.jit.is_scripting() or not isinstance(inpt, (features.Image, features.Video)) ): return erase_image_tensor(inpt, i=i, j=j, h=h, w=w, v=v, inplace=inplace) elif isinstance(inpt, features.Image): output = erase_image_tensor(inpt.as_subclass(torch.Tensor), i=i, j=j, h=h, w=w, v=v, inplace=inplace) return features.Image.wrap_like(inpt, output) elif isinstance(inpt, features.Video): output = erase_video(inpt.as_subclass(torch.Tensor), i=i, j=j, h=h, w=w, v=v, inplace=inplace) return features.Video.wrap_like(inpt, output) else: # isinstance(inpt, PIL.Image.Image): return erase_image_pil(inpt, i=i, j=j, h=h, w=w, v=v, inplace=inplace)
# In[1]: import pandas as pd # In[2]: data_filename = "data.json" df = pd.read_json(data_filename).T df.tail() # In[3]: all_labels = {lbl for labels in df["labels"] for lbl in labels} all_labels # In[4]: # Add one column per label for label in all_labels: df[label] = df["labels"].apply(lambda labels_list: label in labels_list) df.head() # In[5]: # Add a clean "module" column. It contains tuples since PRs can have more than one module. # Maybe we should include "topics" in that column as well? all_modules = { # mapping: full name -> clean name label: "".join(label.split(" ")[1:]) for label in all_labels if label.startswith("module") } # We use an ugly loop, but whatever ¯\_(ツ)_/¯ df["module"] = [[] for _ in range(len(df))] for i, row in df.iterrows(): for full_name, clean_name in all_modules.items(): if full_name in row["labels"]: row["module"].append(clean_name) df["module"] = df.module.apply(tuple) df.head() # In[6]: mod_df = df.set_index("module").sort_index() mod_df.tail() # In[7]: # All improvement PRs mod_df[mod_df["enhancement"]].head() # In[8]: # improvement f module # note: don't filter module name on the index as the index contain tuples with non-exclusive values # Use the boolean column instead mod_df[mod_df["enhancement"] & mod_df["module: transforms"]] # In[9]: def format_prs(mod_df, exclude_prototype=True): out = [] for idx, row in mod_df.iterrows(): if exclude_prototype and "prototype" in row and row["prototype"]: continue modules = idx # Put "documentation" and "tests" first for sorting to be dece for last_module in ("documentation", "tests"): if last_module in modules: modules = [m for m in modules if m != last_module] + [last_module] module = f"[{', '.join(modules)}]" module = module.replace("referencescripts", "reference scripts") module = module.replace("code", "reference scripts") out.append(f"{module} {row['title']}") return "\n".join(out) # In[10]: included_prs = pd.DataFrame() # If labels are accurate, this shouhld generate most of the release notes already # We keep track of the included PRs to figure out which ones are missing for section_title, module_idx in ( ("Backward-incompatible changes", "bc-breaking"), ("Deprecations", "deprecation"), ("New Features", "new feature"), ("Improvements", "enhancement"), ("Bug Fixes", "bug"), ("Code Quality", "code quality"), ): if module_idx in mod_df: print(f"## {section_title}") print() tmp_df = mod_df[mod_df[module_idx]] included_prs = pd.concat([included_prs, tmp_df]) print(format_prs(tmp_df)) print() # In[11]: # Missing PRs are these ones... classify them manually missing_prs = pd.concat([mod_df, included_prs]).drop_duplicates(subset="pr_number", keep=False) print(format_prs(missing_prs)) # In[12]: # Generate list of contributors print() print("## Contributors") previous_release = "c35d3855ccbfa6a36e6ae6337a1f2c721c1f1e78" current_release = "5181a854d8b127cf465cd22a67c1b5aaf6ccae05" print( f"{{ git shortlog -s {previous_release}..{current_release} | cut -f2- & git log -s {previous_release}..{current_release} | grep Co-authored | cut -f2- -d: | cut -f1 -d\\< | sed 's/^ *//;s/ *//' ; }} | sort --ignore-case | uniq | tr '\\n' ';' | sed 's/;/, /g;s/,//' | fold -s" ) # In[13]: # Utility to extract PR numbers only from multiple lines, useful to bundle all # the docs changes for example: import re s = """ [] Remove unnecessary dependency from macOS/Conda binaries (#8077) [rocm] [ROCm] remove HCC references (#8070) """ print(", ".join(re.findall("(#\\d+)", s)))
# In[1]: # imports and set configuration import pandas as pd from retrieve_prs_data import run exclude_prototype = True data_filename = "10.0_to_11.0-rc2.json" previous_release = "v10.0" current_release = "v11.0-rc2" # In[2]: df = pd.read_json(data_filename).T df.tail() # In[3]: all_labels = {lbl for labels in df["labels"] for lbl in labels} all_labels # In[4]: # Add one column per label for label in all_labels: df[label] = df["labels"].apply(lambda labels_list: label in labels_list) df.head() # In[5]: # Add a clean "module" column. It contains tuples since PRs can have more than one module. # Maybe we should include "topics" in that column as well? all_modules = { # mapping: full name -> clean name label: "".join(label.split(" ")[1:]) for label in all_labels if label.startswith("module") } # We use an ugly loop, but whatever ¯\_(ツ)_/¯ df["module"] = [[] for _ in range(len(df))] for i, row in df.iterrows(): for full_name, clean_name in all_modules.items(): if full_name in row["labels"]: row["module"].append(clean_name) df["module"] = df.module.apply(tuple) df.head() # In[6]: mod_df = df.set_index("module").sort_index() mod_df.tail() # In[7]: # All improvement PRs mod_df[mod_df["enhancement"]].head() # In[8]: # improvement f module # note: don't filter module name on the index as the index contain tuples with non-exclusive values # Use the boolean column instead mod_df[mod_df["enhancement"] & mod_df["module: transforms"]] # In[9]: def format_prs(mod_df): out = [] for idx, row in mod_df.iterrows(): if exclude_prototype and row["prototype"]: continue modules = idx # Put "documentation" and "tests" first for sorting to be dece for last_module in ("documentation", "tests"): if last_module in modules: modules = [m for m in modules if m != last_module] + [last_module] module = f"[{', '.join(modules)}]" module = module.replace("referencescripts", "reference scripts") module = module.replace("code", "reference scripts") out.append(f"{module} {row['title']}") return "\n".join(out) # In[10]: included_prs = pd.DataFrame() # If labels are accurate, this shouhld generate most of the release notes already # We keep track of the included PRs to figure out which ones are missing for section_title, module_idx in ( ("Backward-incompatible changes", "bc-breaking"), ("Deprecations", "deprecation"), ("New Features", "new feature"), ("Improvements", "enhancement"), ("Bug Fixes", "bug"), ("Code Quality", "code quality"), ): print(f"## {section_title}") print() tmp_df = mod_df[mod_df[module_idx]] included_prs = pd.concat([included_prs, tmp_df]) print(format_prs(tmp_df)) print() # In[11]: # Missing PRs are these ones... classify them manually missing_prs = pd.concat([mod_df, included_prs]).drop_duplicates(subset="pr_number", keep=False) print(format_prs(missing_prs)) # In[12]: # Generate list of contributors print() print("## Contributors") command_to_run = f"{{ git shortlog -s {previous_release}..{current_release} | cut -f2- & git log -s {previous_release}..{current_release} | grep Co-authored | cut -f2- -d: | cut -f1 -d\\< | sed 's/^ *//;s/ *$//' ; }} | sort --ignore-case | uniq | tr '\\n' ';' | sed 's/;/, /g;s/, $//' | fold -s" rc, output, err = run(command_to_run) print(output)
# Copyright 2020 The HuggingFace Datasets Authors and the TensorFlow Datasets 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. __version__ = "3.3.1.dev0" from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled from .info import DatasetInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_default_config_name, get_dataset_infos, get_dataset_split_names, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .utils import * from .utils import logging
# Copyright 2020 The HuggingFace Datasets Authors and the TensorFlow Datasets 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. __version__ = "3.3.0" from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled from .info import DatasetInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_default_config_name, get_dataset_infos, get_dataset_split_names, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .utils import * from .utils import logging
"""Test few shot prompt template.""" import re import pytest from langchain_core.prompts.few_shot_with_templates import FewShotPromptWithTemplates from langchain_core.prompts.prompt import PromptTemplate EXAMPLE_PROMPT = PromptTemplate( input_variables=["question", "answer"], template="{question}: {answer}" ) async def test_prompttemplate_prefix_suffix() -> None: """Test that few shot works when prefix and suffix are PromptTemplates.""" prefix = PromptTemplate( input_variables=["content"], template="This is a test about {content}." ) suffix = PromptTemplate( input_variables=["new_content"], template="Now you try to talk about {new_content}.", ) examples = [ {"question": "foo", "answer": "bar"}, {"question": "baz", "answer": "foo"}, ] prompt = FewShotPromptWithTemplates( suffix=suffix, prefix=prefix, input_variables=["content", "new_content"], examples=examples, example_prompt=EXAMPLE_PROMPT, example_separator="\n", ) expected_output = ( "This is a test about animals.\n" "foo: bar\n" "baz: foo\n" "Now you try to talk about party." ) output = prompt.format(content="animals", new_content="party") assert output == expected_output output = await prompt.aformat(content="animals", new_content="party") assert output == expected_output def test_prompttemplate_validation() -> None: """Test that few shot works when prefix and suffix are PromptTemplates.""" prefix = PromptTemplate( input_variables=["content"], template="This is a test about {content}." ) suffix = PromptTemplate( input_variables=["new_content"], template="Now you try to talk about {new_content}.", ) examples = [ {"question": "foo", "answer": "bar"}, {"question": "baz", "answer": "foo"}, ] with pytest.raises( ValueError, match=re.escape("Got input_variables=[], but based on prefix/suffix expected"), ): FewShotPromptWithTemplates( suffix=suffix, prefix=prefix, input_variables=[], examples=examples, example_prompt=EXAMPLE_PROMPT, example_separator="\n", validate_template=True, ) assert FewShotPromptWithTemplates( suffix=suffix, prefix=prefix, input_variables=[], examples=examples, example_prompt=EXAMPLE_PROMPT, example_separator="\n", ).input_variables == ["content", "new_content"]
"""Test few shot prompt template.""" import pytest from langchain_core.prompts.few_shot_with_templates import FewShotPromptWithTemplates from langchain_core.prompts.prompt import PromptTemplate EXAMPLE_PROMPT = PromptTemplate( input_variables=["question", "answer"], template="{question}: {answer}" ) async def test_prompttemplate_prefix_suffix() -> None: """Test that few shot works when prefix and suffix are PromptTemplates.""" prefix = PromptTemplate( input_variables=["content"], template="This is a test about {content}." ) suffix = PromptTemplate( input_variables=["new_content"], template="Now you try to talk about {new_content}.", ) examples = [ {"question": "foo", "answer": "bar"}, {"question": "baz", "answer": "foo"}, ] prompt = FewShotPromptWithTemplates( suffix=suffix, prefix=prefix, input_variables=["content", "new_content"], examples=examples, example_prompt=EXAMPLE_PROMPT, example_separator="\n", ) expected_output = ( "This is a test about animals.\n" "foo: bar\n" "baz: foo\n" "Now you try to talk about party." ) output = prompt.format(content="animals", new_content="party") assert output == expected_output output = await prompt.aformat(content="animals", new_content="party") assert output == expected_output def test_prompttemplate_validation() -> None: """Test that few shot works when prefix and suffix are PromptTemplates.""" prefix = PromptTemplate( input_variables=["content"], template="This is a test about {content}." ) suffix = PromptTemplate( input_variables=["new_content"], template="Now you try to talk about {new_content}.", ) examples = [ {"question": "foo", "answer": "bar"}, {"question": "baz", "answer": "foo"}, ] with pytest.raises(ValueError): FewShotPromptWithTemplates( suffix=suffix, prefix=prefix, input_variables=[], examples=examples, example_prompt=EXAMPLE_PROMPT, example_separator="\n", validate_template=True, ) assert FewShotPromptWithTemplates( suffix=suffix, prefix=prefix, input_variables=[], examples=examples, example_prompt=EXAMPLE_PROMPT, example_separator="\n", ).input_variables == ["content", "new_content"]
from typing import Type from .document import BaseDocument class AnyDocument(BaseDocument): """ AnyDocument is a Document that is not tied to any schema """ def __init__(self, **kwargs): super().__init__() self.__dict__.update(kwargs) @classmethod def _get_nested_document_class(cls, field: str) -> Type['BaseDocument']: """ Accessing the nested python Class define in the schema. Could be useful for reconstruction of Document in serialization/deserilization :param field: name of the field :return: """ return AnyDocument
from typing import Type from .document import BaseDocument class AnyDocument(BaseDocument): """ AnyDocument is a Document that is not tied to any schema """ def __init__(self, **kwargs): super().__init__() self.__dict__.update(kwargs) @classmethod def _get_nested_document_class(cls, field: str) -> Type['BaseDocument']: """ Accessing the nested python Class define in the schema. Could be useful for reconstruction of Document in serialization/deserilization :param field: name of the field :return: """ return AnyDocument
# Copyright (c) OpenMMLab. All rights reserved. from .bbox_overlaps import bbox_overlaps from .class_names import (cityscapes_classes, coco_classes, dataset_aliases, get_classes, imagenet_det_classes, imagenet_vid_classes, objects365v1_classes, objects365v2_classes, oid_challenge_classes, oid_v6_classes, voc_classes) from .mean_ap import average_precision, eval_map, print_map_summary from .panoptic_utils import (INSTANCE_OFFSET, pq_compute_multi_core, pq_compute_single_core) from .recall import (eval_recalls, plot_iou_recall, plot_num_recall, print_recall_summary) __all__ = [ 'voc_classes', 'imagenet_det_classes', 'imagenet_vid_classes', 'coco_classes', 'cityscapes_classes', 'dataset_aliases', 'get_classes', 'average_precision', 'eval_map', 'print_map_summary', 'eval_recalls', 'print_recall_summary', 'plot_num_recall', 'plot_iou_recall', 'oid_v6_classes', 'oid_challenge_classes', 'INSTANCE_OFFSET', 'pq_compute_single_core', 'pq_compute_multi_core', 'bbox_overlaps', 'objects365v1_classes', 'objects365v2_classes' ]
# Copyright (c) OpenMMLab. All rights reserved. from .bbox_overlaps import bbox_overlaps from .class_names import (cityscapes_classes, coco_classes, dataset_aliases, get_classes, imagenet_det_classes, imagenet_vid_classes, oid_challenge_classes, oid_v6_classes, voc_classes) from .mean_ap import average_precision, eval_map, print_map_summary from .panoptic_utils import (INSTANCE_OFFSET, pq_compute_multi_core, pq_compute_single_core) from .recall import (eval_recalls, plot_iou_recall, plot_num_recall, print_recall_summary) __all__ = [ 'voc_classes', 'imagenet_det_classes', 'imagenet_vid_classes', 'coco_classes', 'cityscapes_classes', 'dataset_aliases', 'get_classes', 'average_precision', 'eval_map', 'print_map_summary', 'eval_recalls', 'print_recall_summary', 'plot_num_recall', 'plot_iou_recall', 'oid_v6_classes', 'oid_challenge_classes', 'INSTANCE_OFFSET', 'pq_compute_single_core', 'pq_compute_multi_core', 'bbox_overlaps' ]
from keras.src.tree.tree_api import assert_same_paths from keras.src.tree.tree_api import assert_same_structure from keras.src.tree.tree_api import flatten from keras.src.tree.tree_api import flatten_with_path from keras.src.tree.tree_api import is_nested from keras.src.tree.tree_api import lists_to_tuples from keras.src.tree.tree_api import map_shape_structure from keras.src.tree.tree_api import map_structure from keras.src.tree.tree_api import map_structure_up_to from keras.src.tree.tree_api import pack_sequence_as from keras.src.tree.tree_api import register_tree_node_class from keras.src.tree.tree_api import traverse
from keras.src.tree.tree_api import assert_same_structure from keras.src.tree.tree_api import flatten from keras.src.tree.tree_api import is_nested from keras.src.tree.tree_api import lists_to_tuples from keras.src.tree.tree_api import map_shape_structure from keras.src.tree.tree_api import map_structure from keras.src.tree.tree_api import map_structure_up_to from keras.src.tree.tree_api import pack_sequence_as from keras.src.tree.tree_api import register_tree_node_class from keras.src.tree.tree_api import traverse
from __future__ import annotations import json from typing import ( Any, Union, ) from langchain_core._api import deprecated from pydantic import PrivateAttr from langchain_anthropic.chat_models import ChatAnthropic SYSTEM_PROMPT_FORMAT = """In this environment you have access to a set of tools you can use to answer the user's question. You may call them like this: <function_calls> <invoke> <tool_name>$TOOL_NAME</tool_name> <parameters> <$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME> ... </parameters> </invoke> </function_calls> Here are the tools available: <tools> {formatted_tools} </tools>""" # noqa: E501 TOOL_FORMAT = """<tool_description> <tool_name>{tool_name}</tool_name> <description>{tool_description}</description> <parameters> {formatted_parameters} </parameters> </tool_description>""" TOOL_PARAMETER_FORMAT = """<parameter> <name>{parameter_name}</name> <type>{parameter_type}</type> <description>{parameter_description}</description> </parameter>""" def _get_type(parameter: dict[str, Any]) -> str: if "type" in parameter: return parameter["type"] if "anyOf" in parameter: return json.dumps({"anyOf": parameter["anyOf"]}) if "allOf" in parameter: return json.dumps({"allOf": parameter["allOf"]}) return json.dumps(parameter) def get_system_message(tools: list[dict]) -> str: """Generate a system message that describes the available tools.""" tools_data: list[dict] = [ { "tool_name": tool["name"], "tool_description": tool["description"], "formatted_parameters": "\n".join( [ TOOL_PARAMETER_FORMAT.format( parameter_name=name, parameter_type=_get_type(parameter), parameter_description=parameter.get("description"), ) for name, parameter in tool["parameters"]["properties"].items() ], ), } for tool in tools ] tools_formatted = "\n".join( [ TOOL_FORMAT.format( tool_name=tool["tool_name"], tool_description=tool["tool_description"], formatted_parameters=tool["formatted_parameters"], ) for tool in tools_data ], ) return SYSTEM_PROMPT_FORMAT.format(formatted_tools=tools_formatted) def _xml_to_dict(t: Any) -> Union[str, dict[str, Any]]: # Base case: If the element has no children, return its text or an empty string. if len(t) == 0: return t.text or "" # Recursive case: The element has children. Convert them into a dictionary. d: dict[str, Any] = {} for child in t: if child.tag not in d: d[child.tag] = _xml_to_dict(child) else: # Handle multiple children with the same tag if not isinstance(d[child.tag], list): d[child.tag] = [d[child.tag]] # Convert existing entry into a list d[child.tag].append(_xml_to_dict(child)) return d def _xml_to_function_call(invoke: Any, tools: list[dict]) -> dict[str, Any]: name = invoke.find("tool_name").text arguments = _xml_to_dict(invoke.find("parameters")) # make list elements in arguments actually lists filtered_tools = [tool for tool in tools if tool["name"] == name] if len(filtered_tools) > 0 and not isinstance(arguments, str): tool = filtered_tools[0] for key, value in arguments.items(): if ( key in tool["parameters"]["properties"] and "type" in tool["parameters"]["properties"][key] ): if tool["parameters"]["properties"][key][ "type" ] == "array" and not isinstance(value, list): arguments[key] = [value] if ( tool["parameters"]["properties"][key]["type"] != "object" and isinstance(value, dict) and len(value.keys()) == 1 ): arguments[key] = next(iter(value.values())) return { "function": { "name": name, "arguments": json.dumps(arguments), }, "type": "function", } def _xml_to_tool_calls(elem: Any, tools: list[dict]) -> list[dict[str, Any]]: """Convert an XML element and its children into a dictionary of dictionaries.""" invokes = elem.findall("invoke") return [_xml_to_function_call(invoke, tools) for invoke in invokes] @deprecated( "0.1.5", removal="1.0.0", alternative="ChatAnthropic", message=( "Tool-calling is now officially supported by the Anthropic API so this " "workaround is no longer needed." ), ) class ChatAnthropicTools(ChatAnthropic): """Chat model for interacting with Anthropic functions.""" _xmllib: Any = PrivateAttr(default=None)
import json from typing import ( Any, Union, ) from langchain_core._api import deprecated from pydantic import PrivateAttr from langchain_anthropic.chat_models import ChatAnthropic SYSTEM_PROMPT_FORMAT = """In this environment you have access to a set of tools you can use to answer the user's question. You may call them like this: <function_calls> <invoke> <tool_name>$TOOL_NAME</tool_name> <parameters> <$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME> ... </parameters> </invoke> </function_calls> Here are the tools available: <tools> {formatted_tools} </tools>""" # noqa: E501 TOOL_FORMAT = """<tool_description> <tool_name>{tool_name}</tool_name> <description>{tool_description}</description> <parameters> {formatted_parameters} </parameters> </tool_description>""" TOOL_PARAMETER_FORMAT = """<parameter> <name>{parameter_name}</name> <type>{parameter_type}</type> <description>{parameter_description}</description> </parameter>""" def _get_type(parameter: dict[str, Any]) -> str: if "type" in parameter: return parameter["type"] if "anyOf" in parameter: return json.dumps({"anyOf": parameter["anyOf"]}) if "allOf" in parameter: return json.dumps({"allOf": parameter["allOf"]}) return json.dumps(parameter) def get_system_message(tools: list[dict]) -> str: """Generate a system message that describes the available tools.""" tools_data: list[dict] = [ { "tool_name": tool["name"], "tool_description": tool["description"], "formatted_parameters": "\n".join( [ TOOL_PARAMETER_FORMAT.format( parameter_name=name, parameter_type=_get_type(parameter), parameter_description=parameter.get("description"), ) for name, parameter in tool["parameters"]["properties"].items() ] ), } for tool in tools ] tools_formatted = "\n".join( [ TOOL_FORMAT.format( tool_name=tool["tool_name"], tool_description=tool["tool_description"], formatted_parameters=tool["formatted_parameters"], ) for tool in tools_data ] ) return SYSTEM_PROMPT_FORMAT.format(formatted_tools=tools_formatted) def _xml_to_dict(t: Any) -> Union[str, dict[str, Any]]: # Base case: If the element has no children, return its text or an empty string. if len(t) == 0: return t.text or "" # Recursive case: The element has children. Convert them into a dictionary. d: dict[str, Any] = {} for child in t: if child.tag not in d: d[child.tag] = _xml_to_dict(child) else: # Handle multiple children with the same tag if not isinstance(d[child.tag], list): d[child.tag] = [d[child.tag]] # Convert existing entry into a list d[child.tag].append(_xml_to_dict(child)) return d def _xml_to_function_call(invoke: Any, tools: list[dict]) -> dict[str, Any]: name = invoke.find("tool_name").text arguments = _xml_to_dict(invoke.find("parameters")) # make list elements in arguments actually lists filtered_tools = [tool for tool in tools if tool["name"] == name] if len(filtered_tools) > 0 and not isinstance(arguments, str): tool = filtered_tools[0] for key, value in arguments.items(): if key in tool["parameters"]["properties"]: if "type" in tool["parameters"]["properties"][key]: if tool["parameters"]["properties"][key][ "type" ] == "array" and not isinstance(value, list): arguments[key] = [value] if ( tool["parameters"]["properties"][key]["type"] != "object" and isinstance(value, dict) and len(value.keys()) == 1 ): arguments[key] = list(value.values())[0] return { "function": { "name": name, "arguments": json.dumps(arguments), }, "type": "function", } def _xml_to_tool_calls(elem: Any, tools: list[dict]) -> list[dict[str, Any]]: """ Convert an XML element and its children into a dictionary of dictionaries. """ invokes = elem.findall("invoke") return [_xml_to_function_call(invoke, tools) for invoke in invokes] @deprecated( "0.1.5", removal="1.0.0", alternative="ChatAnthropic", message=( "Tool-calling is now officially supported by the Anthropic API so this " "workaround is no longer needed." ), ) class ChatAnthropicTools(ChatAnthropic): """Chat model for interacting with Anthropic functions.""" _xmllib: Any = PrivateAttr(default=None)