input
stringlengths
33
5k
output
stringlengths
32
5k
_base_ = '../retinanet/retinanet_r50_fpn_1x_coco.py' # model settings model = dict( type='FSAF', bbox_head=dict( type='FSAFHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, reg_decoded_bbox=True, # Only anchor-free branch is implemented. The anchor generator only # generates 1 anchor at each feature point, as a substitute of the # grid of features. anchor_generator=dict( type='AnchorGenerator', octave_base_scale=1, scales_per_octave=1, ratios=[1.0], strides=[8, 16, 32, 64, 128]), bbox_coder=dict(_delete_=True, type='TBLRBBoxCoder', normalizer=4.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0, reduction='none'), loss_bbox=dict( _delete_=True, type='IoULoss', eps=1e-6, loss_weight=1.0, reduction='none')), # training and testing settings train_cfg=dict( assigner=dict( _delete_=True, type='CenterRegionAssigner', pos_scale=0.2, neg_scale=0.2, min_pos_iof=0.01), allowed_border=-1, pos_weight=-1, debug=False)) optim_wrapper = dict(clip_grad=dict(max_norm=10, norm_type=2))
_base_ = '../retinanet/retinanet_r50_fpn_1x_coco.py' # model settings model = dict( type='FSAF', bbox_head=dict( type='FSAFHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, reg_decoded_bbox=True, # Only anchor-free branch is implemented. The anchor generator only # generates 1 anchor at each feature point, as a substitute of the # grid of features. anchor_generator=dict( type='AnchorGenerator', octave_base_scale=1, scales_per_octave=1, ratios=[1.0], strides=[8, 16, 32, 64, 128]), bbox_coder=dict(_delete_=True, type='TBLRBBoxCoder', normalizer=4.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0, reduction='none'), loss_bbox=dict( _delete_=True, type='IoULoss', eps=1e-6, loss_weight=1.0, reduction='none')), # training and testing settings train_cfg=dict( assigner=dict( _delete_=True, type='CenterRegionAssigner', pos_scale=0.2, neg_scale=0.2, min_pos_iof=0.01), allowed_border=-1, pos_weight=-1, debug=False)) optim_wrapper = dict( optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)) default_hooks = dict( optimizer=dict( _delete_=True, type='OptimizerHook', grad_clip=dict(max_norm=10, norm_type=2)))
import platform import sys from pathlib import Path import pkg_resources from setuptools import find_packages, setup def read_version(fname="whisper/version.py"): exec(compile(open(fname, encoding="utf-8").read(), fname, "exec")) return locals()["__version__"] requirements = [] if sys.platform.startswith("linux") and platform.machine() == "x86_64": requirements.append("triton>=2.0.0,<3") setup( name="openai-whisper", py_modules=["whisper"], version=read_version(), description="Robust Speech Recognition via Large-Scale Weak Supervision", long_description=open("README.md", encoding="utf-8").read(), long_description_content_type="text/markdown", readme="README.md", python_requires=">=3.8", author="OpenAI", url="https://github.com/openai/whisper", license="MIT", packages=find_packages(exclude=["tests*"]), install_requires=[ str(r) for r in pkg_resources.parse_requirements( Path(__file__).with_name("requirements.txt").open() ) ], entry_points={ "console_scripts": ["whisper=whisper.transcribe:cli"], }, include_package_data=True, extras_require={"dev": ["pytest", "scipy", "black", "flake8", "isort"]}, )
import os import platform import sys import pkg_resources from setuptools import find_packages, setup def read_version(fname="whisper/version.py"): exec(compile(open(fname, encoding="utf-8").read(), fname, "exec")) return locals()["__version__"] requirements = [] if sys.platform.startswith("linux") and platform.machine() == "x86_64": requirements.append("triton>=2.0.0,<3") setup( name="openai-whisper", py_modules=["whisper"], version=read_version(), description="Robust Speech Recognition via Large-Scale Weak Supervision", long_description=open("README.md", encoding="utf-8").read(), long_description_content_type="text/markdown", readme="README.md", python_requires=">=3.8", author="OpenAI", url="https://github.com/openai/whisper", license="MIT", packages=find_packages(exclude=["tests*"]), install_requires=requirements + [ str(r) for r in pkg_resources.parse_requirements( open(os.path.join(os.path.dirname(__file__), "requirements.txt")) ) ], entry_points={ "console_scripts": ["whisper=whisper.transcribe:cli"], }, include_package_data=True, extras_require={"dev": ["pytest", "scipy", "black", "flake8", "isort"]}, )
from __future__ import annotations from typing import Any, Optional from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) from langchain_core.tools import BaseTool from langchain_community.utilities.mojeek_search import MojeekSearchAPIWrapper class MojeekSearch(BaseTool): name: str = "mojeek_search" description: str = ( "A wrapper around Mojeek Search. " "Useful for when you need to web search results. " "Input should be a search query." ) api_wrapper: MojeekSearchAPIWrapper @classmethod def config( cls, api_key: str, search_kwargs: Optional[dict] = None, **kwargs: Any ) -> MojeekSearch: wrapper = MojeekSearchAPIWrapper( api_key=api_key, search_kwargs=search_kwargs or {} ) return cls(api_wrapper=wrapper, **kwargs) def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: return self.api_wrapper.run(query) async def _arun( self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" raise NotImplementedError("MojeekSearch does not support async")
from __future__ import annotations from typing import Any, Optional from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) from langchain_core.tools import BaseTool from langchain_community.utilities.mojeek_search import MojeekSearchAPIWrapper class MojeekSearch(BaseTool): # type: ignore[override] name: str = "mojeek_search" description: str = ( "A wrapper around Mojeek Search. " "Useful for when you need to web search results. " "Input should be a search query." ) api_wrapper: MojeekSearchAPIWrapper @classmethod def config( cls, api_key: str, search_kwargs: Optional[dict] = None, **kwargs: Any ) -> MojeekSearch: wrapper = MojeekSearchAPIWrapper( api_key=api_key, search_kwargs=search_kwargs or {} ) return cls(api_wrapper=wrapper, **kwargs) def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: return self.api_wrapper.run(query) async def _arun( self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" raise NotImplementedError("MojeekSearch does not support async")
import pytest from backend.data import db from backend.executor import ExecutionScheduler from backend.server.model import CreateGraph from backend.usecases.sample import create_test_graph, create_test_user from backend.util.service import get_service_client from backend.util.test import SpinTestServer @pytest.mark.asyncio(scope="session") async def test_agent_schedule(server: SpinTestServer): await db.connect() test_user = await create_test_user() test_graph = await server.agent_server.test_create_graph( create_graph=CreateGraph(graph=create_test_graph()), is_template=False, user_id=test_user.id, ) scheduler = get_service_client(ExecutionScheduler) schedules = scheduler.get_execution_schedules(test_graph.id, test_user.id) assert len(schedules) == 0 schedule_id = scheduler.add_execution_schedule( graph_id=test_graph.id, user_id=test_user.id, graph_version=1, cron="0 0 * * *", input_data={"input": "data"}, ) assert schedule_id schedules = scheduler.get_execution_schedules(test_graph.id, test_user.id) assert len(schedules) == 1 assert schedules[schedule_id] == "0 0 * * *" scheduler.update_schedule(schedule_id, is_enabled=False, user_id=test_user.id) schedules = scheduler.get_execution_schedules(test_graph.id, user_id=test_user.id) assert len(schedules) == 0
import pytest from backend.data import db from backend.executor import ExecutionScheduler from backend.server.model import CreateGraph from backend.usecases.sample import create_test_graph, create_test_user from backend.util.service import get_service_client from backend.util.test import SpinTestServer @pytest.mark.asyncio(scope="session") async def test_agent_schedule(server: SpinTestServer): await db.connect() test_user = await create_test_user() test_graph = await server.agent_server.create_graph( create_graph=CreateGraph(graph=create_test_graph()), is_template=False, user_id=test_user.id, ) scheduler = get_service_client(ExecutionScheduler) schedules = scheduler.get_execution_schedules(test_graph.id, test_user.id) assert len(schedules) == 0 schedule_id = scheduler.add_execution_schedule( graph_id=test_graph.id, user_id=test_user.id, graph_version=1, cron="0 0 * * *", input_data={"input": "data"}, ) assert schedule_id schedules = scheduler.get_execution_schedules(test_graph.id, test_user.id) assert len(schedules) == 1 assert schedules[schedule_id] == "0 0 * * *" scheduler.update_schedule(schedule_id, is_enabled=False, user_id=test_user.id) schedules = scheduler.get_execution_schedules(test_graph.id, user_id=test_user.id) assert len(schedules) == 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) 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]))
import os import time import pytest from docarray import Document from jina import Flow from jina.constants import __cache_path__ cur_dir = os.path.dirname(os.path.abspath(__file__)) @pytest.fixture(scope='module') def filewriter_exec_docker_image_built(): import docker client = docker.from_env() client.images.build( path=os.path.join(cur_dir, 'filewriter-exec/'), tag='filewriter-exec' ) client.close() yield time.sleep(2) client = docker.from_env() client.containers.prune() @pytest.mark.parametrize( 'source,destination,workspace', [('test/dir', '/custom/app', '/custom/app')], ) def test_volumes_in_flow( tmpdir, source, destination, workspace, filewriter_exec_docker_image_built ): if source: # test manually set volume and workspace source = os.path.join(tmpdir, source) volumes = [str(source) + ':' + destination] else: # test auto volume and workspace source = __cache_path__ f = Flow().add( uses='docker://filewriter-exec', volumes=volumes, workspace=workspace ) with f: f.post(inputs=[Document()], on='/foo') assert os.path.exists(source) found_output_file = False # workspace has random element, so we search for it for cur_path, dirs, files in os.walk(source): if 'out.txt' in files: with open(os.path.join(cur_path, 'out.txt'), 'r', encoding='utf-8') as f: if f.read() == 'Filewriter was here': found_output_file = True assert found_output_file
import os import time import pytest from docarray import Document from jina import Flow from jina.constants import __cache_path__ cur_dir = os.path.dirname(os.path.abspath(__file__)) @pytest.fixture(scope='module') def filewriter_exec_docker_image_built(): import docker client = docker.from_env() client.images.build( path=os.path.join(cur_dir, 'filewriter-exec/'), tag='filewriter-exec' ) client.close() yield time.sleep(2) client = docker.from_env() client.containers.prune() @pytest.mark.parametrize( 'source,destination,workspace', [('test/dir', '/custom/app', '/custom/app')], ) def test_volumes_in_flow( tmpdir, source, destination, workspace, filewriter_exec_docker_image_built ): if source: # test manually set volume and workspace source = os.path.join(tmpdir, source) volumes = [str(source) + ':' + destination] else: # test auto volume and workspace source = __cache_path__ f = Flow().add( uses='docker://filewriter-exec', volumes=volumes, workspace=workspace ) with f: f.post(inputs=[Document()], on='/foo') assert os.path.exists(source) found_output_file = False # workspace has random element, so we search for it for cur_path, dirs, files in os.walk(source): if 'out.txt' in files: with open(os.path.join(cur_path, 'out.txt'), 'r') as f: if f.read() == 'Filewriter was here': found_output_file = True assert found_output_file
import torch from ._bounding_boxes import BoundingBoxes, BoundingBoxFormat, is_rotated_bounding_format from ._image import Image from ._mask import Mask from ._torch_function_helpers import set_return_type from ._tv_tensor import TVTensor from ._video import Video # TODO: Fix this. We skip this method as it leads to # RecursionError: maximum recursion depth exceeded while calling a Python object # Until `disable` is removed, there will be graph breaks after all calls to functional transforms @torch.compiler.disable def wrap(wrappee, *, like, **kwargs): """Convert a :class:`torch.Tensor` (``wrappee``) into the same :class:`~torchvision.tv_tensors.TVTensor` subclass as ``like``. If ``like`` is a :class:`~torchvision.tv_tensors.BoundingBoxes`, the ``format`` and ``canvas_size`` of ``like`` are assigned to ``wrappee``, unless they are passed as ``kwargs``. Args: wrappee (Tensor): The tensor to convert. like (:class:`~torchvision.tv_tensors.TVTensor`): The reference. ``wrappee`` will be converted into the same subclass as ``like``. kwargs: Can contain "format" and "canvas_size" if ``like`` is a :class:`~torchvision.tv_tensor.BoundingBoxes`. Ignored otherwise. """ if isinstance(like, BoundingBoxes): return BoundingBoxes._wrap( wrappee, format=kwargs.get("format", like.format), canvas_size=kwargs.get("canvas_size", like.canvas_size), ) else: return wrappee.as_subclass(type(like))
import torch from ._bounding_boxes import BoundingBoxes, BoundingBoxFormat from ._image import Image from ._mask import Mask from ._torch_function_helpers import set_return_type from ._tv_tensor import TVTensor from ._video import Video # TODO: Fix this. We skip this method as it leads to # RecursionError: maximum recursion depth exceeded while calling a Python object # Until `disable` is removed, there will be graph breaks after all calls to functional transforms @torch.compiler.disable def wrap(wrappee, *, like, **kwargs): """Convert a :class:`torch.Tensor` (``wrappee``) into the same :class:`~torchvision.tv_tensors.TVTensor` subclass as ``like``. If ``like`` is a :class:`~torchvision.tv_tensors.BoundingBoxes`, the ``format`` and ``canvas_size`` of ``like`` are assigned to ``wrappee``, unless they are passed as ``kwargs``. Args: wrappee (Tensor): The tensor to convert. like (:class:`~torchvision.tv_tensors.TVTensor`): The reference. ``wrappee`` will be converted into the same subclass as ``like``. kwargs: Can contain "format" and "canvas_size" if ``like`` is a :class:`~torchvision.tv_tensor.BoundingBoxes`. Ignored otherwise. """ if isinstance(like, BoundingBoxes): return BoundingBoxes._wrap( wrappee, format=kwargs.get("format", like.format), canvas_size=kwargs.get("canvas_size", like.canvas_size), ) else: return wrappee.as_subclass(type(like))
import warnings from typing import Any, List, Union import PIL.Image import torch from torchvision.prototype import datapoints from torchvision.transforms import functional as _F from ._utils import is_simple_tensor @torch.jit.unused def to_grayscale(inpt: PIL.Image.Image, num_output_channels: int = 1) -> PIL.Image.Image: call = ", num_output_channels=3" if num_output_channels == 3 else "" replacement = "convert_color_space(..., color_space=datapoints.ColorSpace.GRAY)" if num_output_channels == 3: replacement = f"convert_color_space({replacement}, color_space=datapoints.ColorSpace.RGB)" warnings.warn( f"The function `to_grayscale(...{call})` is deprecated in will be removed in a future release. " f"Instead, please use `{replacement}`.", ) return _F.to_grayscale(inpt, num_output_channels=num_output_channels) def rgb_to_grayscale( inpt: Union[datapoints.ImageTypeJIT, datapoints.VideoTypeJIT], num_output_channels: int = 1 ) -> Union[datapoints.ImageTypeJIT, datapoints.VideoTypeJIT]: old_color_space = None # TODO: remove when un-deprecating if not (torch.jit.is_scripting() or is_simple_tensor(inpt)) and isinstance( inpt, (datapoints.Image, datapoints.Video) ): inpt = inpt.as_subclass(torch.Tensor) call = ", num_output_channels=3" if num_output_channels == 3 else "" replacement = ( f"convert_color_space(..., color_space=datapoints.ColorSpace.GRAY" f"{f', old_color_space=datapoints.ColorSpace.{old_color_space}' if old_color_space is not None else ''})" ) if num_output_channels == 3: replacement = ( f"convert_color_space({replacement}, color_space=datapoints.ColorSpace.RGB" f"{f', old_color_space=datapoints.ColorSpace.GRAY' if old_color_space is not None else ''})" ) warnings.warn( f"The function `rgb_to_grayscale(...{call})` is deprecated in will be removed in a future release. " f"Instead, please use `{replacement}`.", ) return _F.rgb_to_grayscale(inpt, num_output_channels=num_output_channels) @torch.jit.unused def to_tensor(inpt: Any) -> torch.Tensor: warnings.warn( "The function `to_tensor(...)` is deprecated and will be removed in a future release. " "Instead, please use `to_image_tensor(...)` followed by `convert_image_dtype(...)`." ) return _F.to_tensor(inpt) def get_image_size(inpt: Union[datapoints.ImageTypeJIT, datapoints.VideoTypeJIT]) -> List[int]: warnings.warn( "The function `get_image_size(...)` is deprecated and will be removed in a future release. " "Instead, please use `get_spatial_size(...)` which returns `[h, w]` instead of `[w, h]`." ) return _F.get_image_size(inpt)
import warnings from typing import Any, List, Union import PIL.Image import torch from torchvision.prototype import datapoints from torchvision.transforms import functional as _F from ._utils import is_simple_tensor @torch.jit.unused def to_grayscale(inpt: PIL.Image.Image, num_output_channels: int = 1) -> PIL.Image.Image: call = ", num_output_channels=3" if num_output_channels == 3 else "" replacement = "convert_color_space(..., color_space=datapoints.ColorSpace.GRAY)" if num_output_channels == 3: replacement = f"convert_color_space({replacement}, color_space=datapoints.ColorSpace.RGB)" warnings.warn( f"The function `to_grayscale(...{call})` is deprecated in will be removed in a future release. " f"Instead, please use `{replacement}`.", ) return _F.to_grayscale(inpt, num_output_channels=num_output_channels) def rgb_to_grayscale( inpt: Union[datapoints.ImageTypeJIT, datapoints.VideoTypeJIT], num_output_channels: int = 1 ) -> Union[datapoints.ImageTypeJIT, datapoints.VideoTypeJIT]: if torch.jit.is_scripting() or is_simple_tensor(inpt): old_color_space = datapoints._image._from_tensor_shape(inpt.shape) # type: ignore[arg-type] else: old_color_space = None if isinstance(inpt, (datapoints.Image, datapoints.Video)): inpt = inpt.as_subclass(torch.Tensor) call = ", num_output_channels=3" if num_output_channels == 3 else "" replacement = ( f"convert_color_space(..., color_space=datapoints.ColorSpace.GRAY" f"{f', old_color_space=datapoints.ColorSpace.{old_color_space}' if old_color_space is not None else ''})" ) if num_output_channels == 3: replacement = ( f"convert_color_space({replacement}, color_space=datapoints.ColorSpace.RGB" f"{f', old_color_space=datapoints.ColorSpace.GRAY' if old_color_space is not None else ''})" ) warnings.warn( f"The function `rgb_to_grayscale(...{call})` is deprecated in will be removed in a future release. " f"Instead, please use `{replacement}`.", ) return _F.rgb_to_grayscale(inpt, num_output_channels=num_output_channels) @torch.jit.unused def to_tensor(inpt: Any) -> torch.Tensor: warnings.warn( "The function `to_tensor(...)` is deprecated and will be removed in a future release. " "Instead, please use `to_image_tensor(...)` followed by `convert_image_dtype(...)`." ) return _F.to_tensor(inpt) def get_image_size(inpt: Union[datapoints.ImageTypeJIT, datapoints.VideoTypeJIT]) -> List[int]: warnings.warn( "The function `get_image_size(...)` is deprecated and will be removed in a future release. " "Instead, please use `get_spatial_size(...)` which returns `[h, w]` instead of `[w, h]`." ) return _F.get_image_size(inpt)
from docarray.typing.tensor.tensor import Tensor from docarray.typing.tensor.torch_tensor import TorchTensor __all__ = ['Tensor', 'TorchTensor']
from docarray.typing.tensor.tensor import Tensor __all__ = ['Tensor']
from typing import Type from docarray.utils._internal.pydantic import is_pydantic_v2 from .doc import BaseDoc class AnyDoc(BaseDoc): """ AnyDoc is a Document that is not tied to any schema """ class Config: _load_extra_fields_from_protobuf = True # I introduce this variable to allow to load more that the fields defined in the schema # will documented this behavior later if this fix our problem def __init__(self, **kwargs): super().__init__() self.__dict__.update(kwargs) @classmethod def _get_field_annotation(cls, field: str) -> Type['BaseDoc']: """ 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 AnyDoc @classmethod def _get_field_annotation_array(cls, field: str) -> Type: from docarray import DocList return DocList if is_pydantic_v2: def dict(self, *args, **kwargs): raise NotImplementedError( "dict() method is not implemented for pydantic v2. Now pydantic requires a schema to dump the dict, but AnyDoc is schemaless" )
from typing import Type from .doc import BaseDoc class AnyDoc(BaseDoc): """ AnyDoc is a Document that is not tied to any schema """ class Config: _load_extra_fields_from_protobuf = True # I introduce this variable to allow to load more that the fields defined in the schema # will documented this behavior later if this fix our problem def __init__(self, **kwargs): super().__init__() self.__dict__.update(kwargs) @classmethod def _get_field_type(cls, field: str) -> Type['BaseDoc']: """ 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 AnyDoc @classmethod def _get_field_type_array(cls, field: str) -> Type: from docarray import DocList return DocList
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '3.0.0rc0' short_version = __version__ def parse_version_info(version_str): """Parse a version string into a tuple. Args: version_str (str): The version string. Returns: tuple[int | str]: The version info, e.g., "1.3.0" is parsed into (1, 3, 0), and "2.0.0rc1" is parsed into (2, 0, 0, 'rc1'). """ version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '2.25.0' short_version = __version__ def parse_version_info(version_str): version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
""" Base Managed Service index. An index that is built on top of a managed service. """ from abc import ABC, abstractmethod from typing import Any, Dict, List, Optional, Sequence, Type from llama_index.core.base.base_retriever import BaseRetriever from llama_index.core.callbacks.base import CallbackManager from llama_index.core.data_structs.data_structs import IndexDict from llama_index.core.indices.base import BaseIndex, IndexType from llama_index.core.schema import BaseNode, Document, TransformComponent from llama_index.core.storage.docstore.types import RefDocInfo from llama_index.core.storage.storage_context import StorageContext class BaseManagedIndex(BaseIndex[IndexDict], ABC): """ Managed Index. The managed service can index documents into a managed service. How documents are structured into nodes is a detail for the managed service, and not exposed in this interface (although could be controlled by configuration parameters). Args: show_progress (bool): Whether to show tqdm progress bars. Defaults to False. """ def __init__( self, nodes: Optional[Sequence[BaseNode]] = None, index_struct: Optional[IndexDict] = None, storage_context: Optional[StorageContext] = None, show_progress: bool = False, **kwargs: Any, ) -> None: """Initialize params.""" super().__init__( nodes=nodes, index_struct=index_struct, storage_context=storage_context, show_progress=show_progress, **kwargs, ) @abstractmethod def _insert(self, nodes: Sequence[BaseNode], **insert_kwargs: Any) -> None: """Insert a set of documents (each a node).""" @abstractmethod def delete_ref_doc( self, ref_doc_id: str, delete_from_docstore: bool = False, **delete_kwargs: Any ) -> None: """Delete a document and it's nodes by using ref_doc_id.""" @abstractmethod def update_ref_doc(self, document: Document, **update_kwargs: Any) -> None: """Update a document and it's corresponding nodes.""" @abstractmethod def as_retriever(self, **kwargs: Any) -> BaseRetriever: """Return a Retriever for this managed index.""" def _build_index_from_nodes( self, nodes: Sequence[BaseNode], **build_kwargs: Any ) -> IndexDict: """Build the index from nodes.""" raise NotImplementedError( "_build_index_from_nodes not implemented for BaseManagedIndex." ) def _delete_node(self, node_id: str, **delete_kwargs: Any) -> None: """Delete a node.""" raise NotImplementedError("_delete_node not implemented for BaseManagedIndex.") @property def ref_doc_info(self) -> Dict[str, RefDocInfo]: """Retrieve a dict mapping of ingested documents and their nodes+metadata.""" raise NotImplementedError("ref_doc_info not implemented for BaseManagedIndex.") @classmethod def from_documents( cls: Type[IndexType], documents: Sequence[Document], storage_context: Optional[StorageContext] = None, show_progress: bool = False, callback_manager: Optional[CallbackManager] = None, transformations: Optional[List[TransformComponent]] = None, **kwargs: Any, ) -> IndexType: """Build an index from a sequence of documents.""" raise NotImplementedError( "from_documents not implemented for BaseManagedIndex." )
"""Base Managed Service index. An index that is built on top of a managed service. """ from abc import ABC, abstractmethod from typing import Any, Dict, List, Optional, Sequence, Type from llama_index.core.base.base_retriever import BaseRetriever from llama_index.core.callbacks.base import CallbackManager from llama_index.core.data_structs.data_structs import IndexDict from llama_index.core.indices.base import BaseIndex, IndexType from llama_index.core.schema import BaseNode, Document, TransformComponent from llama_index.core.storage.docstore.types import RefDocInfo from llama_index.core.storage.storage_context import StorageContext class BaseManagedIndex(BaseIndex[IndexDict], ABC): """Managed Index. The managed service can index documents into a managed service. How documents are structured into nodes is a detail for the managed service, and not exposed in this interface (although could be controlled by configuration parameters). Args: show_progress (bool): Whether to show tqdm progress bars. Defaults to False. """ def __init__( self, nodes: Optional[Sequence[BaseNode]] = None, index_struct: Optional[IndexDict] = None, storage_context: Optional[StorageContext] = None, show_progress: bool = False, **kwargs: Any, ) -> None: """Initialize params.""" super().__init__( nodes=nodes, index_struct=index_struct, storage_context=storage_context, show_progress=show_progress, **kwargs, ) @abstractmethod def _insert(self, nodes: Sequence[BaseNode], **insert_kwargs: Any) -> None: """Insert a set of documents (each a node).""" @abstractmethod def delete_ref_doc( self, ref_doc_id: str, delete_from_docstore: bool = False, **delete_kwargs: Any ) -> None: """Delete a document and it's nodes by using ref_doc_id.""" @abstractmethod def update_ref_doc(self, document: Document, **update_kwargs: Any) -> None: """Update a document and it's corresponding nodes.""" @abstractmethod def as_retriever(self, **kwargs: Any) -> BaseRetriever: """Return a Retriever for this managed index.""" def _build_index_from_nodes( self, nodes: Sequence[BaseNode], **build_kwargs: Any ) -> IndexDict: """Build the index from nodes.""" raise NotImplementedError( "_build_index_from_nodes not implemented for BaseManagedIndex." ) def _delete_node(self, node_id: str, **delete_kwargs: Any) -> None: """Delete a node.""" raise NotImplementedError("_delete_node not implemented for BaseManagedIndex.") @property def ref_doc_info(self) -> Dict[str, RefDocInfo]: """Retrieve a dict mapping of ingested documents and their nodes+metadata.""" raise NotImplementedError("ref_doc_info not implemented for BaseManagedIndex.") @classmethod def from_documents( cls: Type[IndexType], documents: Sequence[Document], storage_context: Optional[StorageContext] = None, show_progress: bool = False, callback_manager: Optional[CallbackManager] = None, transformations: Optional[List[TransformComponent]] = None, **kwargs: Any, ) -> IndexType: """Build an index from a sequence of documents.""" raise NotImplementedError( "from_documents not implemented for BaseManagedIndex." )
# Copyright (c) OpenMMLab. All rights reserved. _base_ = './error_mix_using3.py'
# Copyright (c) OpenMMLab. All rights reserved. _base_ = './toy_model.py'
import sys import numpy as np import pytest from hypothesis import given, settings, strategies import xgboost as xgb from xgboost import testing as tm from xgboost.testing import no_cupy from xgboost.testing.updater import check_extmem_qdm, check_quantile_loss_extmem sys.path.append("tests/python") from test_data_iterator import run_data_iterator from test_data_iterator import test_single_batch as cpu_single_batch # There are lots of warnings if XGBoost is not running on ATS-enabled systems. pytestmark = pytest.mark.filterwarnings("ignore") def test_gpu_single_batch() -> None: cpu_single_batch("hist", "cuda") @pytest.mark.skipif(**no_cupy()) @given( strategies.integers(0, 1024), strategies.integers(1, 7), strategies.integers(0, 8), strategies.booleans(), strategies.booleans(), strategies.booleans(), ) @settings(deadline=None, max_examples=16, print_blob=True) def test_gpu_data_iterator( n_samples_per_batch: int, n_features: int, n_batches: int, subsample: bool, use_cupy: bool, on_host: bool, ) -> None: run_data_iterator( n_samples_per_batch, n_features, n_batches, "hist", subsample=subsample, device="cuda", use_cupy=use_cupy, on_host=on_host, ) def test_cpu_data_iterator() -> None: """Make sure CPU algorithm can handle GPU inputs""" run_data_iterator( 1024, 2, 3, "approx", device="cuda", subsample=False, use_cupy=True, on_host=False, ) @given( strategies.integers(1, 2048), strategies.integers(1, 8), strategies.integers(1, 4), strategies.integers(2, 16), strategies.booleans(), ) @settings(deadline=None, max_examples=10, print_blob=True) def test_extmem_qdm( n_samples_per_batch: int, n_features: int, n_batches: int, n_bins: int, on_host: bool, ) -> None: check_extmem_qdm( n_samples_per_batch, n_features, n_batches=n_batches, n_bins=n_bins, device="cuda", on_host=on_host, ) def test_invalid_device_extmem_qdm() -> None: it = tm.IteratorForTest( *tm.make_batches(16, 4, 2, use_cupy=False), cache="cache", on_host=True ) Xy = xgb.ExtMemQuantileDMatrix(it) with pytest.raises(ValueError, match="cannot be used for GPU"): xgb.train({"device": "cuda"}, Xy) it = tm.IteratorForTest( *tm.make_batches(16, 4, 2, use_cupy=True), cache="cache", on_host=True ) Xy = xgb.ExtMemQuantileDMatrix(it) with pytest.raises(ValueError, match="cannot be used for CPU"): xgb.train({"device": "cpu"}, Xy) def test_concat_pages_invalid() -> None: it = tm.IteratorForTest(*tm.make_batches(64, 16, 4, use_cupy=True), cache=None) Xy = xgb.ExtMemQuantileDMatrix(it) with pytest.raises(ValueError, match="can not be used with concatenated pages"): xgb.train( { "device": "cuda", "subsample": 0.5, "sampling_method": "gradient_based", "extmem_concat_pages": True, "objective": "reg:absoluteerror", }, Xy, ) def test_concat_pages() -> None: boosters = [] for min_cache_page_bytes in [0, 256, 386, np.iinfo(np.int64).max]: it = tm.IteratorForTest( *tm.make_batches(64, 16, 4, use_cupy=True), cache=None, min_cache_page_bytes=min_cache_page_bytes, on_host=True, ) Xy = xgb.ExtMemQuantileDMatrix(it) booster = xgb.train( { "device": "cuda", "objective": "reg:absoluteerror", }, Xy, ) boosters.append(booster.save_raw(raw_format="json")) for model in boosters[1:]: assert str(model) == str(boosters[0]) @given( strategies.integers(1, 64), strategies.integers(1, 8), strategies.integers(1, 4), ) @settings(deadline=None, max_examples=10, print_blob=True) def test_quantile_objective( n_samples_per_batch: int, n_features: int, n_batches: int ) -> None: check_quantile_loss_extmem( n_samples_per_batch, n_features, n_batches, "hist", "cuda", ) check_quantile_loss_extmem( n_samples_per_batch, n_features, n_batches, "approx", "cuda", )
import sys import pytest from hypothesis import given, settings, strategies import xgboost as xgb from xgboost import testing as tm from xgboost.testing import no_cupy from xgboost.testing.updater import check_extmem_qdm, check_quantile_loss_extmem sys.path.append("tests/python") from test_data_iterator import run_data_iterator from test_data_iterator import test_single_batch as cpu_single_batch def test_gpu_single_batch() -> None: cpu_single_batch("hist", "cuda") @pytest.mark.skipif(**no_cupy()) @given( strategies.integers(0, 1024), strategies.integers(1, 7), strategies.integers(0, 8), strategies.booleans(), strategies.booleans(), strategies.booleans(), ) @settings(deadline=None, max_examples=16, print_blob=True) def test_gpu_data_iterator( n_samples_per_batch: int, n_features: int, n_batches: int, subsample: bool, use_cupy: bool, on_host: bool, ) -> None: run_data_iterator( n_samples_per_batch, n_features, n_batches, "hist", subsample=subsample, device="cuda", use_cupy=use_cupy, on_host=on_host, ) def test_cpu_data_iterator() -> None: """Make sure CPU algorithm can handle GPU inputs""" run_data_iterator( 1024, 2, 3, "approx", device="cuda", subsample=False, use_cupy=True, on_host=False, ) @given( strategies.integers(1, 2048), strategies.integers(1, 8), strategies.integers(1, 4), strategies.integers(2, 16), strategies.booleans(), ) @settings(deadline=None, max_examples=10, print_blob=True) @pytest.mark.filterwarnings("ignore") def test_extmem_qdm( n_samples_per_batch: int, n_features: int, n_batches: int, n_bins: int, on_host: bool, ) -> None: check_extmem_qdm( n_samples_per_batch, n_features, n_batches=n_batches, n_bins=n_bins, device="cuda", on_host=on_host, ) @pytest.mark.filterwarnings("ignore") def test_invalid_device_extmem_qdm() -> None: it = tm.IteratorForTest( *tm.make_batches(16, 4, 2, use_cupy=False), cache="cache", on_host=True ) Xy = xgb.ExtMemQuantileDMatrix(it) with pytest.raises(ValueError, match="cannot be used for GPU"): xgb.train({"device": "cuda"}, Xy) it = tm.IteratorForTest( *tm.make_batches(16, 4, 2, use_cupy=True), cache="cache", on_host=True ) Xy = xgb.ExtMemQuantileDMatrix(it) with pytest.raises(ValueError, match="cannot be used for CPU"): xgb.train({"device": "cpu"}, Xy) def test_concat_pages() -> None: it = tm.IteratorForTest(*tm.make_batches(64, 16, 4, use_cupy=True), cache=None) Xy = xgb.ExtMemQuantileDMatrix(it) with pytest.raises(ValueError, match="can not be used with concatenated pages"): xgb.train( { "device": "cuda", "subsample": 0.5, "sampling_method": "gradient_based", "extmem_concat_pages": True, "objective": "reg:absoluteerror", }, Xy, ) @given( strategies.integers(1, 64), strategies.integers(1, 8), strategies.integers(1, 4), ) @settings(deadline=None, max_examples=10, print_blob=True) def test_quantile_objective( n_samples_per_batch: int, n_features: int, n_batches: int ) -> None: check_quantile_loss_extmem( n_samples_per_batch, n_features, n_batches, "hist", "cuda", ) check_quantile_loss_extmem( n_samples_per_batch, n_features, n_batches, "approx", "cuda", )
import logging from datasets import load_dataset from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseMSEEvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model student_model = SparseEncoder("prithivida/Splade_PP_en_v1") teacher_model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load any dataset with some texts dataset = load_dataset("sentence-transformers/stsb", split="validation") sentences = dataset["sentence1"] + dataset["sentence2"] # Given queries, a corpus and a mapping with relevant documents, the SparseMSEEvaluator computes different MSE metrics. mse_evaluator = SparseMSEEvaluator( source_sentences=sentences, target_sentences=sentences, teacher_model=teacher_model, name="stsb-dev", ) results = mse_evaluator(student_model) """ MSE evaluation (lower = better) on the stsb-dev dataset: MSE (*100): 0.035540 Model Sparsity: Active Dimensions: 55.6, Sparsity Ratio: 0.9982 """ # Print the results print(f"Primary metric: {mse_evaluator.primary_metric}") # => Primary metric: stsb-dev_negative_mse print(f"Primary metric value: {results[mse_evaluator.primary_metric]:.4f}") # => Primary metric value: -0.0355
import logging from datasets import load_dataset from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseMSEEvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model student_model = SparseEncoder("prithivida/Splade_PP_en_v1") teacher_model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load any dataset with some texts dataset = load_dataset("sentence-transformers/stsb", split="validation") sentences = dataset["sentence1"] + dataset["sentence2"] # Given queries, a corpus and a mapping with relevant documents, the SparseMSEEvaluator computes different MSE metrics. mse_evaluator = SparseMSEEvaluator( source_sentences=sentences, target_sentences=sentences, teacher_model=teacher_model, name="stsb-dev", ) results = mse_evaluator(student_model) """ MSE evaluation (lower = better) on the stsb-dev dataset: MSE (*100): 0.035540 Model Sparsity Stats: Row Non-Zero Mean: 55.60933303833008, Row Sparsity Mean: 0.9981780648231506 """ # Print the results print(f"Primary metric: {mse_evaluator.primary_metric}") # => Primary metric: stsb-dev_negative_mse print(f"Primary metric value: {results[mse_evaluator.primary_metric]:.4f}") # => Primary metric value: -0.0355
# Copyright (c) OpenMMLab. All rights reserved. from .manager import ManagerMeta, ManagerMixin from .misc import (apply_to, check_prerequisites, concat_list, deprecated_api_warning, deprecated_function, has_method, import_modules_from_strings, is_list_of, is_method_overridden, is_seq_of, is_str, is_tuple_of, iter_cast, list_cast, requires_executable, requires_package, slice_list, to_1tuple, to_2tuple, to_3tuple, to_4tuple, to_ntuple, tuple_cast) from .package_utils import (call_command, get_installed_path, install_package, is_installed) from .path import (check_file_exist, fopen, is_abs, is_filepath, mkdir_or_exist, scandir, symlink) from .progressbar import (ProgressBar, track_iter_progress, track_parallel_progress, track_progress) from .timer import Timer, TimerError, check_time 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_installed', 'call_command', 'get_installed_path', 'install_package', 'is_abs', 'is_method_overridden', 'has_method', 'digit_version', 'get_git_hash', 'ManagerMeta', 'ManagerMixin', 'Timer', 'check_time', 'TimerError', 'ProgressBar', 'track_iter_progress', 'track_parallel_progress', 'track_progress', 'deprecated_function', 'apply_to' ]
# Copyright (c) OpenMMLab. All rights reserved. from .manager import ManagerMeta, ManagerMixin from .misc import (check_prerequisites, concat_list, deprecated_api_warning, deprecated_function, has_method, import_modules_from_strings, is_list_of, is_method_overridden, is_seq_of, is_str, is_tuple_of, iter_cast, list_cast, requires_executable, requires_package, slice_list, to_1tuple, to_2tuple, to_3tuple, to_4tuple, to_ntuple, tuple_cast) from .package_utils import (call_command, get_installed_path, install_package, is_installed) from .path import (check_file_exist, fopen, is_abs, is_filepath, mkdir_or_exist, scandir, symlink) from .progressbar import (ProgressBar, track_iter_progress, track_parallel_progress, track_progress) from .timer import Timer, TimerError, check_time 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_installed', 'call_command', 'get_installed_path', 'install_package', 'is_abs', 'is_method_overridden', 'has_method', 'digit_version', 'get_git_hash', 'ManagerMeta', 'ManagerMixin', 'Timer', 'check_time', 'TimerError', 'ProgressBar', 'track_iter_progress', 'track_parallel_progress', 'track_progress', 'deprecated_function' ]
#!/usr/bin/env python import functools as func import glob import os.path as osp import re import numpy as np url_prefix = 'https://github.com/open-mmlab/mmdetection/blob/main/configs' files = sorted(glob.glob('../../configs/*/README.md')) stats = [] titles = [] num_ckpts = 0 for f in files: url = osp.dirname(f.replace('../../configs', url_prefix)) with open(f, 'r') as content_file: content = content_file.read() title = content.split('\n')[0].replace('# ', '').strip() ckpts = set(x.lower().strip() for x in re.findall(r'\[model\]\((https?.*)\)', content)) if len(ckpts) == 0: continue _papertype = [x for x in re.findall(r'\[([A-Z]+)\]', content)] assert len(_papertype) > 0 papertype = _papertype[0] paper = set([(papertype, title)]) titles.append(title) num_ckpts += len(ckpts) statsmsg = f""" \t* [{papertype}] [{title}]({url}) ({len(ckpts)} ckpts) """ stats.append((paper, ckpts, statsmsg)) allpapers = func.reduce(lambda a, b: a.union(b), [p for p, _, _ in stats]) msglist = '\n'.join(x for _, _, x in stats) papertypes, papercounts = np.unique([t for t, _ in allpapers], return_counts=True) countstr = '\n'.join( [f' - {t}: {c}' for t, c in zip(papertypes, papercounts)]) modelzoo = f""" # Model Zoo Statistics * Number of papers: {len(set(titles))} {countstr} * Number of checkpoints: {num_ckpts} {msglist} """ with open('modelzoo_statistics.md', 'w') as f: f.write(modelzoo)
#!/usr/bin/env python import functools as func import glob import os.path as osp import re import numpy as np url_prefix = 'https://github.com/open-mmlab/mmdetection/blob/3.x/configs' files = sorted(glob.glob('../../configs/*/README.md')) stats = [] titles = [] num_ckpts = 0 for f in files: url = osp.dirname(f.replace('../../configs', url_prefix)) with open(f, 'r') as content_file: content = content_file.read() title = content.split('\n')[0].replace('# ', '').strip() ckpts = set(x.lower().strip() for x in re.findall(r'\[model\]\((https?.*)\)', content)) if len(ckpts) == 0: continue _papertype = [x for x in re.findall(r'\[([A-Z]+)\]', content)] assert len(_papertype) > 0 papertype = _papertype[0] paper = set([(papertype, title)]) titles.append(title) num_ckpts += len(ckpts) statsmsg = f""" \t* [{papertype}] [{title}]({url}) ({len(ckpts)} ckpts) """ stats.append((paper, ckpts, statsmsg)) allpapers = func.reduce(lambda a, b: a.union(b), [p for p, _, _ in stats]) msglist = '\n'.join(x for _, _, x in stats) papertypes, papercounts = np.unique([t for t, _ in allpapers], return_counts=True) countstr = '\n'.join( [f' - {t}: {c}' for t, c in zip(papertypes, papercounts)]) modelzoo = f""" # Model Zoo Statistics * Number of papers: {len(set(titles))} {countstr} * Number of checkpoints: {num_ckpts} {msglist} """ with open('modelzoo_statistics.md', 'w') as f: f.write(modelzoo)
# Copyright (c) OpenMMLab. All rights reserved. from .optimizer import (OPTIM_WRAPPER_CONSTRUCTORS, OPTIMIZERS, AmpOptimWrapper, ApexOptimWrapper, DefaultOptimWrapperConstructor, OptimWrapper, OptimWrapperDict, ZeroRedundancyOptimizer, build_optim_wrapper) # yapf: disable from .scheduler import (ConstantLR, ConstantMomentum, ConstantParamScheduler, CosineAnnealingLR, CosineAnnealingMomentum, CosineAnnealingParamScheduler, ExponentialLR, ExponentialMomentum, ExponentialParamScheduler, LinearLR, LinearMomentum, LinearParamScheduler, MultiStepLR, MultiStepMomentum, MultiStepParamScheduler, OneCycleLR, OneCycleParamScheduler, PolyLR, PolyMomentum, PolyParamScheduler, ReduceOnPlateauLR, ReduceOnPlateauMomentum, ReduceOnPlateauParamScheduler, StepLR, StepMomentum, StepParamScheduler, _ParamScheduler) # yapf: enable __all__ = [ 'OPTIM_WRAPPER_CONSTRUCTORS', 'OPTIMIZERS', 'build_optim_wrapper', 'DefaultOptimWrapperConstructor', 'ConstantLR', 'CosineAnnealingLR', 'ExponentialLR', 'LinearLR', 'MultiStepLR', 'StepLR', 'ConstantMomentum', 'CosineAnnealingMomentum', 'ExponentialMomentum', 'LinearMomentum', 'MultiStepMomentum', 'StepMomentum', 'ConstantParamScheduler', 'CosineAnnealingParamScheduler', 'ExponentialParamScheduler', 'LinearParamScheduler', 'MultiStepParamScheduler', 'StepParamScheduler', '_ParamScheduler', 'OptimWrapper', 'AmpOptimWrapper', 'ApexOptimWrapper', 'OptimWrapperDict', 'OneCycleParamScheduler', 'OneCycleLR', 'PolyLR', 'PolyMomentum', 'PolyParamScheduler', 'ReduceOnPlateauLR', 'ReduceOnPlateauMomentum', 'ReduceOnPlateauParamScheduler', 'ZeroRedundancyOptimizer' ]
# Copyright (c) OpenMMLab. All rights reserved. from .optimizer import (OPTIM_WRAPPER_CONSTRUCTORS, OPTIMIZERS, AmpOptimWrapper, ApexOptimWrapper, DefaultOptimWrapperConstructor, OptimWrapper, OptimWrapperDict, build_optim_wrapper) # yapf: disable from .scheduler import (ConstantLR, ConstantMomentum, ConstantParamScheduler, CosineAnnealingLR, CosineAnnealingMomentum, CosineAnnealingParamScheduler, ExponentialLR, ExponentialMomentum, ExponentialParamScheduler, LinearLR, LinearMomentum, LinearParamScheduler, MultiStepLR, MultiStepMomentum, MultiStepParamScheduler, OneCycleLR, OneCycleParamScheduler, PolyLR, PolyMomentum, PolyParamScheduler, ReduceOnPlateauLR, ReduceOnPlateauMomentum, ReduceOnPlateauParamScheduler, StepLR, StepMomentum, StepParamScheduler, _ParamScheduler) # yapf: enable __all__ = [ 'OPTIM_WRAPPER_CONSTRUCTORS', 'OPTIMIZERS', 'build_optim_wrapper', 'DefaultOptimWrapperConstructor', 'ConstantLR', 'CosineAnnealingLR', 'ExponentialLR', 'LinearLR', 'MultiStepLR', 'StepLR', 'ConstantMomentum', 'CosineAnnealingMomentum', 'ExponentialMomentum', 'LinearMomentum', 'MultiStepMomentum', 'StepMomentum', 'ConstantParamScheduler', 'CosineAnnealingParamScheduler', 'ExponentialParamScheduler', 'LinearParamScheduler', 'MultiStepParamScheduler', 'StepParamScheduler', '_ParamScheduler', 'OptimWrapper', 'AmpOptimWrapper', 'ApexOptimWrapper', 'OptimWrapperDict', 'OneCycleParamScheduler', 'OneCycleLR', 'PolyLR', 'PolyMomentum', 'PolyParamScheduler', 'ReduceOnPlateauLR', 'ReduceOnPlateauMomentum', 'ReduceOnPlateauParamScheduler' ]
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '2.23.0' short_version = __version__ def parse_version_info(version_str): version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '2.22.0' short_version = __version__ def parse_version_info(version_str): version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
"""Memory modules for conversation prompts.""" from typing import TYPE_CHECKING, Any from langchain._api import create_importer from langchain.memory.buffer import ( ConversationBufferMemory, ConversationStringBufferMemory, ) from langchain.memory.buffer_window import ConversationBufferWindowMemory from langchain.memory.combined import CombinedMemory from langchain.memory.entity import ConversationEntityMemory from langchain.memory.summary import ConversationSummaryMemory from langchain.memory.summary_buffer import ConversationSummaryBufferMemory if TYPE_CHECKING: from langchain_community.memory.kg import ConversationKGMemory # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "ConversationKGMemory": "langchain_community.memory.kg", } _importer = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _importer(name) # This is only for backwards compatibility. __all__ = [ "CombinedMemory", "ConversationBufferMemory", "ConversationBufferWindowMemory", "ConversationEntityMemory", "ConversationKGMemory", "ConversationStringBufferMemory", "ConversationSummaryBufferMemory", "ConversationSummaryMemory", ]
"""Memory modules for conversation prompts.""" from typing import TYPE_CHECKING, Any from langchain._api import create_importer from langchain.memory.buffer import ( ConversationBufferMemory, ConversationStringBufferMemory, ) from langchain.memory.buffer_window import ConversationBufferWindowMemory from langchain.memory.combined import CombinedMemory from langchain.memory.entity import ConversationEntityMemory from langchain.memory.summary import ConversationSummaryMemory from langchain.memory.summary_buffer import ConversationSummaryBufferMemory if TYPE_CHECKING: from langchain_community.memory.kg import ConversationKGMemory # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "ConversationKGMemory": "langchain_community.memory.kg", } _importer = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _importer(name) # This is only for backwards compatibility. __all__ = [ "ConversationSummaryBufferMemory", "ConversationSummaryMemory", "ConversationKGMemory", "ConversationBufferWindowMemory", "ConversationEntityMemory", "ConversationBufferMemory", "CombinedMemory", "ConversationStringBufferMemory", ]
from __future__ import annotations import os import sys import warnings def which(thefile: str) -> str | None: warnings.warn( "tools.setup_helpers.which is deprecated and will be removed in a future version. " "Use shutil.which instead.", FutureWarning, stacklevel=2, ) path = os.environ.get("PATH", os.defpath).split(os.pathsep) for d in path: fname = os.path.join(d, thefile) fnames = [fname] if sys.platform == "win32": exts = os.environ.get("PATHEXT", "").split(os.pathsep) fnames += [fname + ext for ext in exts] for name in fnames: if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name): return name return None
from __future__ import annotations import os import sys def which(thefile: str) -> str | None: path = os.environ.get("PATH", os.defpath).split(os.pathsep) for d in path: fname = os.path.join(d, thefile) fnames = [fname] if sys.platform == "win32": exts = os.environ.get("PATHEXT", "").split(os.pathsep) fnames += [fname + ext for ext in exts] for name in fnames: if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name): return name return None
import numpy as np import pytest from numpy.testing import assert_allclose from pytest import approx from sklearn.utils.fixes import np_version, parse_version from sklearn.utils.stats import _averaged_weighted_percentile, _weighted_percentile def test_averaged_weighted_median(): y = np.array([0, 1, 2, 3, 4, 5]) sw = np.array([1, 1, 1, 1, 1, 1]) score = _averaged_weighted_percentile(y, sw, 50) assert score == np.median(y) # TODO: remove @pytest.mark.skipif when numpy min version >= 1.22. @pytest.mark.skipif( condition=np_version < parse_version("1.22"), reason="older numpy do not support the 'method' parameter", ) def test_averaged_weighted_percentile(): rng = np.random.RandomState(0) y = rng.randint(20, size=10) sw = np.ones(10) score = _averaged_weighted_percentile(y, sw, 20) assert score == np.percentile(y, 20, method="averaged_inverted_cdf") def test_averaged_and_weighted_percentile(): y = np.array([0, 1, 2]) sw = np.array([5, 1, 5]) q = 50 score_averaged = _averaged_weighted_percentile(y, sw, q) score = _weighted_percentile(y, sw, q) assert score_averaged == score def test_weighted_percentile(): y = np.empty(102, dtype=np.float64) y[:50] = 0 y[-51:] = 2 y[-1] = 100000 y[50] = 1 sw = np.ones(102, dtype=np.float64) sw[-1] = 0.0 score = _weighted_percentile(y, sw, 50) assert approx(score) == 1 def test_weighted_percentile_equal(): y = np.empty(102, dtype=np.float64) y.fill(0.0) sw = np.ones(102, dtype=np.float64) sw[-1] = 0.0 score = _weighted_percentile(y, sw, 50) assert score == 0 def test_weighted_percentile_zero_weight(): y = np.empty(102, dtype=np.float64) y.fill(1.0) sw = np.ones(102, dtype=np.float64) sw.fill(0.0) score = _weighted_percentile(y, sw, 50) assert approx(score) == 1.0 def test_weighted_percentile_zero_weight_zero_percentile(): y = np.array([0, 1, 2, 3, 4, 5]) sw = np.array([0, 0, 1, 1, 1, 0]) score = _weighted_percentile(y, sw, 0) assert approx(score) == 2 score = _weighted_percentile(y, sw, 50) assert approx(score) == 3 score = _weighted_percentile(y, sw, 100) assert approx(score) == 4 def test_weighted_median_equal_weights(): # Checks weighted percentile=0.5 is same as median when weights equal rng = np.random.RandomState(0) # Odd size as _weighted_percentile takes lower weighted percentile x = rng.randint(10, size=11) weights = np.ones(x.shape) median = np.median(x) w_median = _weighted_percentile(x, weights) assert median == approx(w_median) def test_weighted_median_integer_weights(): # Checks weighted percentile=0.5 is same as median when manually weight # data rng = np.random.RandomState(0) x = rng.randint(20, size=10) weights = rng.choice(5, size=10) x_manual = np.repeat(x, weights) median = np.median(x_manual) w_median = _weighted_percentile(x, weights) assert median == approx(w_median) def test_weighted_percentile_2d(): # Check for when array 2D and sample_weight 1D rng = np.random.RandomState(0) x1 = rng.randint(10, size=10) w1 = rng.choice(5, size=10) x2 = rng.randint(20, size=10) x_2d = np.vstack((x1, x2)).T w_median = _weighted_percentile(x_2d, w1) p_axis_0 = [_weighted_percentile(x_2d[:, i], w1) for i in range(x_2d.shape[1])] assert_allclose(w_median, p_axis_0) # Check when array and sample_weight boht 2D w2 = rng.choice(5, size=10) w_2d = np.vstack((w1, w2)).T w_median = _weighted_percentile(x_2d, w_2d) p_axis_0 = [ _weighted_percentile(x_2d[:, i], w_2d[:, i]) for i in range(x_2d.shape[1]) ] assert_allclose(w_median, p_axis_0)
import numpy as np from numpy.testing import assert_allclose from pytest import approx from sklearn.utils.stats import _weighted_percentile def test_weighted_percentile(): y = np.empty(102, dtype=np.float64) y[:50] = 0 y[-51:] = 2 y[-1] = 100000 y[50] = 1 sw = np.ones(102, dtype=np.float64) sw[-1] = 0.0 score = _weighted_percentile(y, sw, 50) assert approx(score) == 1 def test_weighted_percentile_equal(): y = np.empty(102, dtype=np.float64) y.fill(0.0) sw = np.ones(102, dtype=np.float64) sw[-1] = 0.0 score = _weighted_percentile(y, sw, 50) assert score == 0 def test_weighted_percentile_zero_weight(): y = np.empty(102, dtype=np.float64) y.fill(1.0) sw = np.ones(102, dtype=np.float64) sw.fill(0.0) score = _weighted_percentile(y, sw, 50) assert approx(score) == 1.0 def test_weighted_percentile_zero_weight_zero_percentile(): y = np.array([0, 1, 2, 3, 4, 5]) sw = np.array([0, 0, 1, 1, 1, 0]) score = _weighted_percentile(y, sw, 0) assert approx(score) == 2 score = _weighted_percentile(y, sw, 50) assert approx(score) == 3 score = _weighted_percentile(y, sw, 100) assert approx(score) == 4 def test_weighted_median_equal_weights(): # Checks weighted percentile=0.5 is same as median when weights equal rng = np.random.RandomState(0) # Odd size as _weighted_percentile takes lower weighted percentile x = rng.randint(10, size=11) weights = np.ones(x.shape) median = np.median(x) w_median = _weighted_percentile(x, weights) assert median == approx(w_median) def test_weighted_median_integer_weights(): # Checks weighted percentile=0.5 is same as median when manually weight # data rng = np.random.RandomState(0) x = rng.randint(20, size=10) weights = rng.choice(5, size=10) x_manual = np.repeat(x, weights) median = np.median(x_manual) w_median = _weighted_percentile(x, weights) assert median == approx(w_median) def test_weighted_percentile_2d(): # Check for when array 2D and sample_weight 1D rng = np.random.RandomState(0) x1 = rng.randint(10, size=10) w1 = rng.choice(5, size=10) x2 = rng.randint(20, size=10) x_2d = np.vstack((x1, x2)).T w_median = _weighted_percentile(x_2d, w1) p_axis_0 = [_weighted_percentile(x_2d[:, i], w1) for i in range(x_2d.shape[1])] assert_allclose(w_median, p_axis_0) # Check when array and sample_weight boht 2D w2 = rng.choice(5, size=10) w_2d = np.vstack((w1, w2)).T w_median = _weighted_percentile(x_2d, w_2d) p_axis_0 = [ _weighted_percentile(x_2d[:, i], w_2d[:, i]) for i in range(x_2d.shape[1]) ] assert_allclose(w_median, p_axis_0)
_base_ = [ '../common/mstrain_3x_coco_instance.py', '../_base_/models/cascade_mask_rcnn_r50_fpn.py' ] model = dict( # use caffe img_norm data_preprocessor=dict( type='DetDataPreprocessor', mean=[103.530, 116.280, 123.675], std=[1.0, 1.0, 1.0], to_rgb=False, pad_size_divisor=32), backbone=dict( norm_cfg=dict(requires_grad=False), norm_eval=True, style='caffe', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://detectron2/resnet50_caffe')))
_base_ = [ '../common/mstrain_3x_coco_instance.py', '../_base_/models/cascade_mask_rcnn_r50_fpn.py' ] preprocess_cfg = dict( mean=[103.530, 116.280, 123.675], std=[1.0, 1.0, 1.0], to_rgb=False, pad_size_divisor=32) model = dict( # use caffe img_norm preprocess_cfg=preprocess_cfg, backbone=dict( norm_cfg=dict(requires_grad=False), norm_eval=True, style='caffe', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://detectron2/resnet50_caffe')))
import os from pathlib import Path from typing import List, Tuple, Union import torch from torch.utils.data import Dataset from torchaudio.datasets.utils import _load_waveform _TASKS_TO_MIXTURE = { "sep_clean": "mix_clean", "enh_single": "mix_single", "enh_both": "mix_both", "sep_noisy": "mix_both", } class LibriMix(Dataset): r"""*LibriMix* :cite:`cosentino2020librimix` dataset. Args: root (str or Path): The path where the directory ``Libri2Mix`` or ``Libri3Mix`` is stored. Not the path of those directories. subset (str, optional): The subset to use. Options: [``"train-360"``, ``"train-100"``, ``"dev"``, and ``"test"``] (Default: ``"train-360"``). num_speakers (int, optional): The number of speakers, which determines the directories to traverse. The Dataset will traverse ``s1`` to ``sN`` directories to collect N source audios. (Default: 2) sample_rate (int, optional): Sample rate of audio files. The ``sample_rate`` determines which subdirectory the audio are fetched. If any of the audio has a different sample rate, raises ``ValueError``. Options: [8000, 16000] (Default: 8000) task (str, optional): The task of LibriMix. Options: [``"enh_single"``, ``"enh_both"``, ``"sep_clean"``, ``"sep_noisy"``] (Default: ``"sep_clean"``) mode (str, optional): The mode when creating the mixture. If set to ``"min"``, the lengths of mixture and sources are the minimum length of all sources. If set to ``"max"``, the lengths of mixture and sources are zero padded to the maximum length of all sources. Options: [``"min"``, ``"max"``] (Default: ``"min"``) Note: The LibriMix dataset needs to be manually generated. Please check https://github.com/JorisCos/LibriMix """ def __init__( self, root: Union[str, Path], subset: str = "train-360", num_speakers: int = 2, sample_rate: int = 8000, task: str = "sep_clean", mode: str = "min", ): self.root = Path(root) / f"Libri{num_speakers}Mix" if mode not in ["max", "min"]: raise ValueError(f'Expect ``mode`` to be one in ["min", "max"]. Found {mode}.') if sample_rate == 8000: mix_dir = self.root / "wav8k" / mode / subset elif sample_rate == 16000: mix_dir = self.root / "wav16k" / mode / subset else: raise ValueError(f"Unsupported sample rate. Found {sample_rate}.") self.sample_rate = sample_rate self.task = task self.mix_dir = mix_dir / _TASKS_TO_MIXTURE[task] if task == "enh_both": self.src_dirs = [(mix_dir / "mix_clean")] else: self.src_dirs = [(mix_dir / f"s{i+1}") for i in range(num_speakers)] self.files = [p.name for p in self.mix_dir.glob("*.wav")] self.files.sort() def _load_sample(self, key) -> Tuple[int, torch.Tensor, List[torch.Tensor]]: metadata = self.get_metadata(key) mixed = _load_waveform(self.root, metadata[1], metadata[0]) srcs = [] for i, path_ in enumerate(metadata[2]): src = _load_waveform(self.root, path_, metadata[0]) if mixed.shape != src.shape: raise ValueError(f"Different waveform shapes. mixed: {mixed.shape}, src[{i}]: {src.shape}") srcs.append(src) return self.sample_rate, mixed, srcs def get_metadata(self, key: int) -> Tuple[int, str, List[str]]: """Get metadata for the n-th sample from the dataset. Args: key (int): The index of the sample to be loaded Returns: Tuple of the following items; int: Sample rate str: Path to mixed audio List of str: List of paths to source audios """ filename = self.files[key] mixed_path = os.path.relpath(self.mix_dir / filename, self.root) srcs_paths = [] for dir_ in self.src_dirs: src = os.path.relpath(dir_ / filename, self.root) srcs_paths.append(src) return self.sample_rate, mixed_path, srcs_paths def __len__(self) -> int: return len(self.files) def __getitem__(self, key: int) -> Tuple[int, torch.Tensor, List[torch.Tensor]]: """Load the n-th sample from the dataset. Args: key (int): The index of the sample to be loaded Returns: Tuple of the following items; int: Sample rate Tensor: Mixture waveform List of Tensors: List of source waveforms """ return self._load_sample(key)
import os from pathlib import Path from typing import List, Tuple, Union import torch from torch.utils.data import Dataset from torchaudio.datasets.utils import _load_waveform _TASKS_TO_MIXTURE = { "sep_clean": "mix_clean", "enh_single": "mix_single", "enh_both": "mix_both", "sep_noisy": "mix_both", } class LibriMix(Dataset): r"""*LibriMix* :cite:`cosentino2020librimix` dataset. Args: root (str or Path): The path to the directory where the directory ``Libri2Mix`` or ``Libri3Mix`` is stored. subset (str, optional): The subset to use. Options: [``"train-360"``, ``"train-100"``, ``"dev"``, and ``"test"``] (Default: ``"train-360"``). num_speakers (int, optional): The number of speakers, which determines the directories to traverse. The Dataset will traverse ``s1`` to ``sN`` directories to collect N source audios. (Default: 2) sample_rate (int, optional): Sample rate of audio files. The ``sample_rate`` determines which subdirectory the audio are fetched. If any of the audio has a different sample rate, raises ``ValueError``. Options: [8000, 16000] (Default: 8000) task (str, optional): The task of LibriMix. Options: [``"enh_single"``, ``"enh_both"``, ``"sep_clean"``, ``"sep_noisy"``] (Default: ``"sep_clean"``) mode (str, optional): The mode when creating the mixture. If set to ``"min"``, the lengths of mixture and sources are the minimum length of all sources. If set to ``"max"``, the lengths of mixture and sources are zero padded to the maximum length of all sources. Options: [``"min"``, ``"max"``] (Default: ``"min"``) Note: The LibriMix dataset needs to be manually generated. Please check https://github.com/JorisCos/LibriMix """ def __init__( self, root: Union[str, Path], subset: str = "train-360", num_speakers: int = 2, sample_rate: int = 8000, task: str = "sep_clean", mode: str = "min", ): self.root = Path(root) / f"Libri{num_speakers}Mix" if mode not in ["max", "min"]: raise ValueError(f'Expect ``mode`` to be one in ["min", "max"]. Found {mode}.') if sample_rate == 8000: mix_dir = self.root / "wav8k" / mode / subset elif sample_rate == 16000: mix_dir = self.root / "wav16k" / mode / subset else: raise ValueError(f"Unsupported sample rate. Found {sample_rate}.") self.sample_rate = sample_rate self.task = task self.mix_dir = mix_dir / _TASKS_TO_MIXTURE[task] if task == "enh_both": self.src_dirs = [(mix_dir / "mix_clean")] else: self.src_dirs = [(mix_dir / f"s{i+1}") for i in range(num_speakers)] self.files = [p.name for p in self.mix_dir.glob("*.wav")] self.files.sort() def _load_sample(self, key) -> Tuple[int, torch.Tensor, List[torch.Tensor]]: metadata = self.get_metadata(key) mixed = _load_waveform(self.root, metadata[1], metadata[0]) srcs = [] for i, path_ in enumerate(metadata[2]): src = _load_waveform(self.root, path_, metadata[0]) if mixed.shape != src.shape: raise ValueError(f"Different waveform shapes. mixed: {mixed.shape}, src[{i}]: {src.shape}") srcs.append(src) return self.sample_rate, mixed, srcs def get_metadata(self, key: int) -> Tuple[int, str, List[str]]: """Get metadata for the n-th sample from the dataset. Args: key (int): The index of the sample to be loaded Returns: Tuple of the following items; int: Sample rate str: Path to mixed audio List of str: List of paths to source audios """ filename = self.files[key] mixed_path = os.path.relpath(self.mix_dir / filename, self.root) srcs_paths = [] for dir_ in self.src_dirs: src = os.path.relpath(dir_ / filename, self.root) srcs_paths.append(src) return self.sample_rate, mixed_path, srcs_paths def __len__(self) -> int: return len(self.files) def __getitem__(self, key: int) -> Tuple[int, torch.Tensor, List[torch.Tensor]]: """Load the n-th sample from the dataset. Args: key (int): The index of the sample to be loaded Returns: Tuple of the following items; int: Sample rate Tensor: Mixture waveform List of Tensors: List of source waveforms """ return self._load_sample(key)
""" This basic example loads a pre-trained model from the web and uses it to generate sentence embeddings for a given list of sentences. """ import logging import numpy as np from sentence_transformers import LoggingHandler, SentenceTransformer #### Just some code to print debug information to stdout np.set_printoptions(threshold=100) logging.basicConfig( format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO, handlers=[LoggingHandler()] ) #### /print debug information to stdout # Load pre-trained Sentence Transformer Model. It will be downloaded automatically model = SentenceTransformer("all-MiniLM-L6-v2") # Embed a list of sentences sentences = [ "This framework generates embeddings for each input sentence", "Sentences are passed as a list of string.", "The quick brown fox jumps over the lazy dog.", ] sentence_embeddings = model.encode(sentences) # The result is a list of sentence embeddings as numpy arrays for sentence, embedding in zip(sentences, sentence_embeddings): print("Sentence:", sentence) print("Embedding:", embedding) print("")
""" This basic example loads a pre-trained model from the web and uses it to generate sentence embeddings for a given list of sentences. """ from sentence_transformers import SentenceTransformer, LoggingHandler import numpy as np import logging #### Just some code to print debug information to stdout np.set_printoptions(threshold=100) logging.basicConfig( format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO, handlers=[LoggingHandler()] ) #### /print debug information to stdout # Load pre-trained Sentence Transformer Model. It will be downloaded automatically model = SentenceTransformer("all-MiniLM-L6-v2") # Embed a list of sentences sentences = [ "This framework generates embeddings for each input sentence", "Sentences are passed as a list of string.", "The quick brown fox jumps over the lazy dog.", ] sentence_embeddings = model.encode(sentences) # The result is a list of sentence embeddings as numpy arrays for sentence, embedding in zip(sentences, sentence_embeddings): print("Sentence:", sentence) print("Embedding:", embedding) print("")
# Copyright (c) OpenMMLab. All rights reserved. """copy from https://github.com/ZwwWayne/K-Net/blob/main/knet/det/mask_pseudo_sampler.py.""" import torch from mmengine.structures import InstanceData from mmdet.registry import TASK_UTILS from ..assigners import AssignResult from .base_sampler import BaseSampler from .mask_sampling_result import MaskSamplingResult @TASK_UTILS.register_module() class MaskPseudoSampler(BaseSampler): """A pseudo sampler that does not do sampling actually.""" def __init__(self, **kwargs): pass def _sample_pos(self, **kwargs): """Sample positive samples.""" raise NotImplementedError def _sample_neg(self, **kwargs): """Sample negative samples.""" raise NotImplementedError def sample(self, assign_result: AssignResult, pred_instances: InstanceData, gt_instances: InstanceData, *args, **kwargs): """Directly returns the positive and negative indices of samples. Args: assign_result (:obj:`AssignResult`): Mask assigning results. pred_instances (:obj:`InstanceData`): Instances of model predictions. It includes ``scores`` and ``masks`` predicted by the model. gt_instances (:obj:`InstanceData`): Ground truth of instance annotations. It usually includes ``labels`` and ``masks`` attributes. Returns: :obj:`SamplingResult`: sampler results """ pred_masks = pred_instances.masks gt_masks = gt_instances.masks pos_inds = torch.nonzero( assign_result.gt_inds > 0, as_tuple=False).squeeze(-1).unique() neg_inds = torch.nonzero( assign_result.gt_inds == 0, as_tuple=False).squeeze(-1).unique() gt_flags = pred_masks.new_zeros(pred_masks.shape[0], dtype=torch.uint8) sampling_result = MaskSamplingResult( pos_inds=pos_inds, neg_inds=neg_inds, masks=pred_masks, gt_masks=gt_masks, assign_result=assign_result, gt_flags=gt_flags, avg_factor_with_neg=False) return sampling_result
# Copyright (c) OpenMMLab. All rights reserved. """copy from https://github.com/ZwwWayne/K-Net/blob/main/knet/det/mask_pseudo_sampler.py.""" import torch from mmengine.data import InstanceData from mmdet.registry import TASK_UTILS from ..assigners import AssignResult from .base_sampler import BaseSampler from .mask_sampling_result import MaskSamplingResult @TASK_UTILS.register_module() class MaskPseudoSampler(BaseSampler): """A pseudo sampler that does not do sampling actually.""" def __init__(self, **kwargs): pass def _sample_pos(self, **kwargs): """Sample positive samples.""" raise NotImplementedError def _sample_neg(self, **kwargs): """Sample negative samples.""" raise NotImplementedError def sample(self, assign_result: AssignResult, pred_instances: InstanceData, gt_instances: InstanceData, *args, **kwargs): """Directly returns the positive and negative indices of samples. Args: assign_result (:obj:`AssignResult`): Mask assigning results. pred_instances (:obj:`InstanceData`): Instances of model predictions. It includes ``scores`` and ``masks`` predicted by the model. gt_instances (:obj:`InstanceData`): Ground truth of instance annotations. It usually includes ``labels`` and ``masks`` attributes. Returns: :obj:`SamplingResult`: sampler results """ pred_masks = pred_instances.masks gt_masks = gt_instances.masks pos_inds = torch.nonzero( assign_result.gt_inds > 0, as_tuple=False).squeeze(-1).unique() neg_inds = torch.nonzero( assign_result.gt_inds == 0, as_tuple=False).squeeze(-1).unique() gt_flags = pred_masks.new_zeros(pred_masks.shape[0], dtype=torch.uint8) sampling_result = MaskSamplingResult( pos_inds=pos_inds, neg_inds=neg_inds, masks=pred_masks, gt_masks=gt_masks, assign_result=assign_result, gt_flags=gt_flags, avg_factor_with_neg=False) return sampling_result
from __future__ import annotations from typing import Any from langchain_core.output_parsers import BaseOutputParser from langchain_core.utils import pre_init class CombiningOutputParser(BaseOutputParser[dict[str, Any]]): """Combine multiple output parsers into one.""" parsers: list[BaseOutputParser] @classmethod def is_lc_serializable(cls) -> bool: return True @pre_init def validate_parsers(cls, values: dict[str, Any]) -> dict[str, Any]: """Validate the parsers.""" parsers = values["parsers"] if len(parsers) < 2: msg = "Must have at least two parsers" raise ValueError(msg) for parser in parsers: if parser._type == "combining": msg = "Cannot nest combining parsers" raise ValueError(msg) if parser._type == "list": msg = "Cannot combine list parsers" raise ValueError(msg) return values @property def _type(self) -> str: """Return the type key.""" return "combining" def get_format_instructions(self) -> str: """Instructions on how the LLM output should be formatted.""" initial = f"For your first output: {self.parsers[0].get_format_instructions()}" subsequent = "\n".join( f"Complete that output fully. Then produce another output, separated by two newline characters: {p.get_format_instructions()}" # noqa: E501 for p in self.parsers[1:] ) return f"{initial}\n{subsequent}" def parse(self, text: str) -> dict[str, Any]: """Parse the output of an LLM call.""" texts = text.split("\n\n") output = dict() for txt, parser in zip(texts, self.parsers): output.update(parser.parse(txt.strip())) return output
from __future__ import annotations from typing import Any from langchain_core.output_parsers import BaseOutputParser from langchain_core.utils import pre_init class CombiningOutputParser(BaseOutputParser[dict[str, Any]]): """Combine multiple output parsers into one.""" parsers: list[BaseOutputParser] @classmethod def is_lc_serializable(cls) -> bool: return True @pre_init def validate_parsers(cls, values: dict[str, Any]) -> dict[str, Any]: """Validate the parsers.""" parsers = values["parsers"] if len(parsers) < 2: raise ValueError("Must have at least two parsers") for parser in parsers: if parser._type == "combining": raise ValueError("Cannot nest combining parsers") if parser._type == "list": raise ValueError("Cannot combine list parsers") return values @property def _type(self) -> str: """Return the type key.""" return "combining" def get_format_instructions(self) -> str: """Instructions on how the LLM output should be formatted.""" initial = f"For your first output: {self.parsers[0].get_format_instructions()}" subsequent = "\n".join( f"Complete that output fully. Then produce another output, separated by two newline characters: {p.get_format_instructions()}" # noqa: E501 for p in self.parsers[1:] ) return f"{initial}\n{subsequent}" def parse(self, text: str) -> dict[str, Any]: """Parse the output of an LLM call.""" texts = text.split("\n\n") output = dict() for txt, parser in zip(texts, self.parsers): output.update(parser.parse(txt.strip())) return output
_base_ = './faster-rcnn_r50_fpn_1x_coco.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_ = './faster_rcnn_r50_fpn_1x_coco.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')))
# model settings img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) model = dict( type='RetinaNet', img_norm_cfg=img_norm_cfg, backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, start_level=1, add_extra_convs='on_input', num_outs=5), bbox_head=dict( type='RetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, 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]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0]), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0)), # model training and testing settings train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False), test_cfg=dict( nms_pre=1000, min_bbox_size=0, score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100))
# model settings model = dict( type='RetinaNet', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, start_level=1, add_extra_convs='on_input', num_outs=5), bbox_head=dict( type='RetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, 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]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0]), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0)), # model training and testing settings train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False), test_cfg=dict( nms_pre=1000, min_bbox_size=0, score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100))
# Copyright (c) OpenMMLab. All rights reserved. import torch.nn as nn from mmcv.cnn import ConvModule from mmengine.model import BaseModule from mmdet.registry import MODELS @MODELS.register_module() class ChannelMapper(BaseModule): r"""Channel Mapper to reduce/increase channels of backbone features. This is used to reduce/increase channels of backbone features. Args: in_channels (List[int]): Number of input channels per scale. out_channels (int): Number of output channels (used at each scale). kernel_size (int, optional): kernel_size for reducing channels (used at each scale). Default: 3. conv_cfg (dict, optional): Config dict for convolution layer. Default: None. norm_cfg (dict, optional): Config dict for normalization layer. Default: None. act_cfg (dict, optional): Config dict for activation layer in ConvModule. Default: dict(type='ReLU'). num_outs (int, optional): Number of output feature maps. There would be extra_convs when num_outs larger than the length of in_channels. init_cfg (dict or list[dict], optional): Initialization config dict. Example: >>> import torch >>> in_channels = [2, 3, 5, 7] >>> scales = [340, 170, 84, 43] >>> inputs = [torch.rand(1, c, s, s) ... for c, s in zip(in_channels, scales)] >>> self = ChannelMapper(in_channels, 11, 3).eval() >>> outputs = self.forward(inputs) >>> for i in range(len(outputs)): ... print(f'outputs[{i}].shape = {outputs[i].shape}') outputs[0].shape = torch.Size([1, 11, 340, 340]) outputs[1].shape = torch.Size([1, 11, 170, 170]) outputs[2].shape = torch.Size([1, 11, 84, 84]) outputs[3].shape = torch.Size([1, 11, 43, 43]) """ def __init__(self, in_channels, out_channels, kernel_size=3, conv_cfg=None, norm_cfg=None, act_cfg=dict(type='ReLU'), num_outs=None, init_cfg=dict( type='Xavier', layer='Conv2d', distribution='uniform')): super(ChannelMapper, self).__init__(init_cfg) assert isinstance(in_channels, list) self.extra_convs = None if num_outs is None: num_outs = len(in_channels) self.convs = nn.ModuleList() for in_channel in in_channels: self.convs.append( ConvModule( in_channel, out_channels, kernel_size, padding=(kernel_size - 1) // 2, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) if num_outs > len(in_channels): self.extra_convs = nn.ModuleList() for i in range(len(in_channels), num_outs): if i == len(in_channels): in_channel = in_channels[-1] else: in_channel = out_channels self.extra_convs.append( ConvModule( in_channel, out_channels, 3, stride=2, padding=1, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) def forward(self, inputs): """Forward function.""" assert len(inputs) == len(self.convs) outs = [self.convs[i](inputs[i]) for i in range(len(inputs))] if self.extra_convs: for i in range(len(self.extra_convs)): if i == 0: outs.append(self.extra_convs[0](inputs[-1])) else: outs.append(self.extra_convs[i](outs[-1])) return tuple(outs)
# Copyright (c) OpenMMLab. All rights reserved. import torch.nn as nn from mmcv.cnn import ConvModule from mmcv.runner import BaseModule from mmdet.registry import MODELS @MODELS.register_module() class ChannelMapper(BaseModule): r"""Channel Mapper to reduce/increase channels of backbone features. This is used to reduce/increase channels of backbone features. Args: in_channels (List[int]): Number of input channels per scale. out_channels (int): Number of output channels (used at each scale). kernel_size (int, optional): kernel_size for reducing channels (used at each scale). Default: 3. conv_cfg (dict, optional): Config dict for convolution layer. Default: None. norm_cfg (dict, optional): Config dict for normalization layer. Default: None. act_cfg (dict, optional): Config dict for activation layer in ConvModule. Default: dict(type='ReLU'). num_outs (int, optional): Number of output feature maps. There would be extra_convs when num_outs larger than the length of in_channels. init_cfg (dict or list[dict], optional): Initialization config dict. Example: >>> import torch >>> in_channels = [2, 3, 5, 7] >>> scales = [340, 170, 84, 43] >>> inputs = [torch.rand(1, c, s, s) ... for c, s in zip(in_channels, scales)] >>> self = ChannelMapper(in_channels, 11, 3).eval() >>> outputs = self.forward(inputs) >>> for i in range(len(outputs)): ... print(f'outputs[{i}].shape = {outputs[i].shape}') outputs[0].shape = torch.Size([1, 11, 340, 340]) outputs[1].shape = torch.Size([1, 11, 170, 170]) outputs[2].shape = torch.Size([1, 11, 84, 84]) outputs[3].shape = torch.Size([1, 11, 43, 43]) """ def __init__(self, in_channels, out_channels, kernel_size=3, conv_cfg=None, norm_cfg=None, act_cfg=dict(type='ReLU'), num_outs=None, init_cfg=dict( type='Xavier', layer='Conv2d', distribution='uniform')): super(ChannelMapper, self).__init__(init_cfg) assert isinstance(in_channels, list) self.extra_convs = None if num_outs is None: num_outs = len(in_channels) self.convs = nn.ModuleList() for in_channel in in_channels: self.convs.append( ConvModule( in_channel, out_channels, kernel_size, padding=(kernel_size - 1) // 2, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) if num_outs > len(in_channels): self.extra_convs = nn.ModuleList() for i in range(len(in_channels), num_outs): if i == len(in_channels): in_channel = in_channels[-1] else: in_channel = out_channels self.extra_convs.append( ConvModule( in_channel, out_channels, 3, stride=2, padding=1, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) def forward(self, inputs): """Forward function.""" assert len(inputs) == len(self.convs) outs = [self.convs[i](inputs[i]) for i in range(len(inputs))] if self.extra_convs: for i in range(len(self.extra_convs)): if i == 0: outs.append(self.extra_convs[0](inputs[-1])) else: outs.append(self.extra_convs[i](outs[-1])) return tuple(outs)
"""Run smoke tests""" import os import sys from pathlib import Path import torch import torchvision from torchvision.io import decode_image, decode_jpeg, decode_webp, read_file from torchvision.models import resnet50, ResNet50_Weights SCRIPT_DIR = Path(__file__).parent def smoke_test_torchvision() -> None: print( "Is torchvision usable?", all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align]), ) def smoke_test_torchvision_read_decode() -> None: img_jpg = decode_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) if img_jpg.shape != (3, 606, 517): raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}") img_png = decode_image(str(SCRIPT_DIR / "assets" / "interlaced_png" / "wizard_low.png")) if img_png.shape != (4, 471, 354): raise RuntimeError(f"Unexpected shape of img_png: {img_png.shape}") img_webp = decode_image(str(SCRIPT_DIR / "assets/fakedata/logos/rgb_pytorch.webp")) if img_webp.shape != (3, 100, 100): raise RuntimeError(f"Unexpected shape of img_webp: {img_webp.shape}") def smoke_test_torchvision_decode_jpeg(device: str = "cpu"): img_jpg_data = read_file(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) img_jpg = decode_jpeg(img_jpg_data, device=device) if img_jpg.shape != (3, 606, 517): raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}") def smoke_test_compile() -> None: try: model = resnet50().cuda() model = torch.compile(model) x = torch.randn(1, 3, 224, 224, device="cuda") out = model(x) print(f"torch.compile model output: {out.shape}") except RuntimeError: if sys.platform == "win32": print("Successfully caught torch.compile RuntimeError on win") else: raise def smoke_test_torchvision_resnet50_classify(device: str = "cpu") -> None: img = decode_image(str(SCRIPT_DIR / ".." / "gallery" / "assets" / "dog2.jpg")).to(device) # Step 1: Initialize model with the best available weights weights = ResNet50_Weights.DEFAULT model = resnet50(weights=weights, progress=False).to(device) model.eval() # Step 2: Initialize the inference transforms preprocess = weights.transforms(antialias=(device != "mps")) # antialias not supported on MPS # Step 3: Apply inference preprocessing transforms batch = preprocess(img).unsqueeze(0) # Step 4: Use the model and print the predicted category prediction = model(batch).squeeze(0).softmax(0) class_id = prediction.argmax().item() score = prediction[class_id].item() category_name = weights.meta["categories"][class_id] expected_category = "German shepherd" print(f"{category_name} ({device}): {100 * score:.1f}%") if category_name != expected_category: raise RuntimeError(f"Failed ResNet50 classify {category_name} Expected: {expected_category}") def main() -> None: print(f"torchvision: {torchvision.__version__}") print(f"torch.cuda.is_available: {torch.cuda.is_available()}") print(f"{torch.ops.image._jpeg_version() = }") if not torch.ops.image._is_compiled_against_turbo(): msg = "Torchvision wasn't compiled against libjpeg-turbo" if os.getenv("IS_M1_CONDA_BUILD_JOB") == "1": # When building the conda package on M1, it's difficult to enforce # that we build against turbo due to interactions with the libwebp # package. So we just accept it, instead of raising an error. print(msg) else: raise ValueError(msg) smoke_test_torchvision() smoke_test_torchvision_read_decode() smoke_test_torchvision_resnet50_classify() smoke_test_torchvision_decode_jpeg() if torch.cuda.is_available(): smoke_test_torchvision_decode_jpeg("cuda") smoke_test_torchvision_resnet50_classify("cuda") # TODO: remove once pytorch/pytorch#110436 is resolved if sys.version_info < (3, 12, 0): smoke_test_compile() if torch.backends.mps.is_available(): smoke_test_torchvision_resnet50_classify("mps") if __name__ == "__main__": main()
"""Run smoke tests""" import os import sys from pathlib import Path import torch import torchvision from torchvision.io import decode_image, decode_jpeg, decode_webp, read_file from torchvision.models import resnet50, ResNet50_Weights SCRIPT_DIR = Path(__file__).parent def smoke_test_torchvision() -> None: print( "Is torchvision usable?", all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align]), ) def smoke_test_torchvision_read_decode() -> None: img_jpg = decode_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) if img_jpg.shape != (3, 606, 517): raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}") img_png = decode_image(str(SCRIPT_DIR / "assets" / "interlaced_png" / "wizard_low.png")) if img_png.shape != (4, 471, 354): raise RuntimeError(f"Unexpected shape of img_png: {img_png.shape}") img_webp = decode_image(str(SCRIPT_DIR / "assets/fakedata/logos/rgb_pytorch.webp")) if img_webp.shape != (3, 100, 100): raise RuntimeError(f"Unexpected shape of img_webp: {img_webp.shape}") def smoke_test_torchvision_decode_jpeg(device: str = "cpu"): img_jpg_data = read_file(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg")) img_jpg = decode_jpeg(img_jpg_data, device=device) if img_jpg.shape != (3, 606, 517): raise RuntimeError(f"Unexpected shape of img_jpg: {img_jpg.shape}") def smoke_test_compile() -> None: try: model = resnet50().cuda() model = torch.compile(model) x = torch.randn(1, 3, 224, 224, device="cuda") out = model(x) print(f"torch.compile model output: {out.shape}") except RuntimeError: if sys.platform == "win32": print("Successfully caught torch.compile RuntimeError on win") else: raise def smoke_test_torchvision_resnet50_classify(device: str = "cpu") -> None: img = decode_image(str(SCRIPT_DIR / ".." / "gallery" / "assets" / "dog2.jpg")).to(device) # Step 1: Initialize model with the best available weights weights = ResNet50_Weights.DEFAULT model = resnet50(weights=weights, progress=False).to(device) model.eval() # Step 2: Initialize the inference transforms preprocess = weights.transforms(antialias=(device != "mps")) # antialias not supported on MPS # Step 3: Apply inference preprocessing transforms batch = preprocess(img).unsqueeze(0) # Step 4: Use the model and print the predicted category prediction = model(batch).squeeze(0).softmax(0) class_id = prediction.argmax().item() score = prediction[class_id].item() category_name = weights.meta["categories"][class_id] expected_category = "German shepherd" print(f"{category_name} ({device}): {100 * score:.1f}%") if category_name != expected_category: raise RuntimeError(f"Failed ResNet50 classify {category_name} Expected: {expected_category}") def main() -> None: print(f"torchvision: {torchvision.__version__}") print(f"torch.cuda.is_available: {torch.cuda.is_available()}") print(f"{torch.ops.image._jpeg_version() = }") if not torch.ops.image._is_compiled_against_turbo(): msg = "Torchvision wasn't compiled against libjpeg-turbo" if os.getenv("IS_M1_CONDA_BUILD_JOB") == "1": # When building the conda package on M1, it's difficult to enforce # that we build against turbo due to interactions with the libwebp # package. So we just accept it, instead of raising an error. print(msg) else: raise ValueError(msg) smoke_test_torchvision() smoke_test_torchvision_read_decode() smoke_test_torchvision_resnet50_classify() smoke_test_torchvision_decode_jpeg() if torch.cuda.is_available(): smoke_test_torchvision_decode_jpeg("cuda") smoke_test_torchvision_resnet50_classify("cuda") # TODO: remove once pytorch/pytorch#110436 is resolved # Temporary Disabling compile test. Untill triton with Manylinux2014 is available # if sys.version_info < (3, 12, 0): # smoke_test_compile() if torch.backends.mps.is_available(): smoke_test_torchvision_resnet50_classify("mps") if __name__ == "__main__": main()
import os from typing import Callable, Iterator, Optional from langchain_core.documents import Document from langchain_community.document_loaders.base import BaseLoader class GitLoader(BaseLoader): """Load `Git` repository files. The Repository can be local on disk available at `repo_path`, or remote at `clone_url` that will be cloned to `repo_path`. Currently, supports only text files. Each document represents one file in the repository. The `path` points to the local Git repository, and the `branch` specifies the branch to load files from. By default, it loads from the `main` branch. """ def __init__( self, repo_path: str, clone_url: Optional[str] = None, branch: Optional[str] = "main", file_filter: Optional[Callable[[str], bool]] = None, ): """ Args: repo_path: The path to the Git repository. clone_url: Optional. The URL to clone the repository from. branch: Optional. The branch to load files from. Defaults to `main`. file_filter: Optional. A function that takes a file path and returns a boolean indicating whether to load the file. Defaults to None. """ self.repo_path = repo_path self.clone_url = clone_url self.branch = branch self.file_filter = file_filter def lazy_load(self) -> Iterator[Document]: try: from git import Blob, Repo except ImportError as ex: raise ImportError( "Could not import git python package. " "Please install it with `pip install GitPython`." ) from ex if not os.path.exists(self.repo_path) and self.clone_url is None: raise ValueError(f"Path {self.repo_path} does not exist") elif self.clone_url: # If the repo_path already contains a git repository, verify that it's the # same repository as the one we're trying to clone. if os.path.isdir(os.path.join(self.repo_path, ".git")): repo = Repo(self.repo_path) # If the existing repository is not the same as the one we're trying to # clone, raise an error. if repo.remotes.origin.url != self.clone_url: raise ValueError( "A different repository is already cloned at this path." ) else: repo = Repo.clone_from(self.clone_url, self.repo_path) repo.git.checkout(self.branch) else: repo = Repo(self.repo_path) repo.git.checkout(self.branch) for item in repo.tree().traverse(): if not isinstance(item, Blob): continue file_path = os.path.join(self.repo_path, item.path) ignored_files = repo.ignored([file_path]) if len(ignored_files): continue # uses filter to skip files if self.file_filter and not self.file_filter(file_path): continue rel_file_path = os.path.relpath(file_path, self.repo_path) try: with open(file_path, "rb") as f: content = f.read() file_type = os.path.splitext(item.name)[1] # loads only text files try: text_content = content.decode("utf-8") except UnicodeDecodeError: continue metadata = { "source": rel_file_path, "file_path": rel_file_path, "file_name": item.name, "file_type": file_type, } yield Document(page_content=text_content, metadata=metadata) except Exception as e: print(f"Error reading file {file_path}: {e}") # noqa: T201
import os from typing import Callable, Iterator, Optional from langchain_core.documents import Document from langchain_community.document_loaders.base import BaseLoader class GitLoader(BaseLoader): """Load `Git` repository files. The Repository can be local on disk available at `repo_path`, or remote at `clone_url` that will be cloned to `repo_path`. Currently, supports only text files. Each document represents one file in the repository. The `path` points to the local Git repository, and the `branch` specifies the branch to load files from. By default, it loads from the `main` branch. """ def __init__( self, repo_path: str, clone_url: Optional[str] = None, branch: Optional[str] = "main", file_filter: Optional[Callable[[str], bool]] = None, ): """ Args: repo_path: The path to the Git repository. clone_url: Optional. The URL to clone the repository from. branch: Optional. The branch to load files from. Defaults to `main`. file_filter: Optional. A function that takes a file path and returns a boolean indicating whether to load the file. Defaults to None. """ self.repo_path = repo_path self.clone_url = clone_url self.branch = branch self.file_filter = file_filter def lazy_load(self) -> Iterator[Document]: try: from git import Blob, Repo except ImportError as ex: raise ImportError( "Could not import git python package. " "Please install it with `pip install GitPython`." ) from ex if not os.path.exists(self.repo_path) and self.clone_url is None: raise ValueError(f"Path {self.repo_path} does not exist") elif self.clone_url: # If the repo_path already contains a git repository, verify that it's the # same repository as the one we're trying to clone. if os.path.isdir(os.path.join(self.repo_path, ".git")): repo = Repo(self.repo_path) # If the existing repository is not the same as the one we're trying to # clone, raise an error. if repo.remotes.origin.url != self.clone_url: raise ValueError( "A different repository is already cloned at this path." ) else: repo = Repo.clone_from(self.clone_url, self.repo_path) repo.git.checkout(self.branch) else: repo = Repo(self.repo_path) repo.git.checkout(self.branch) for item in repo.tree().traverse(): if not isinstance(item, Blob): continue file_path = os.path.join(self.repo_path, item.path) ignored_files = repo.ignored([file_path]) # type: ignore[arg-type] if len(ignored_files): continue # uses filter to skip files if self.file_filter and not self.file_filter(file_path): continue rel_file_path = os.path.relpath(file_path, self.repo_path) try: with open(file_path, "rb") as f: content = f.read() file_type = os.path.splitext(item.name)[1] # loads only text files try: text_content = content.decode("utf-8") except UnicodeDecodeError: continue metadata = { "source": rel_file_path, "file_path": rel_file_path, "file_name": item.name, "file_type": file_type, } yield Document(page_content=text_content, metadata=metadata) except Exception as e: print(f"Error reading file {file_path}: {e}") # noqa: T201
""" This directory contains deprecated code that can only be used with the old `model.fit`-style Sentence Transformers v2.X training. It exists for backwards compatibility with the `model.old_fit` method, but will be removed in a future version. Nowadays, with Sentence Transformers v3+, it is recommended to use the `SentenceTransformerTrainer` class to train models. See https://www.sbert.net/docs/sentence_transformer/training_overview.html for more information. """ from __future__ import annotations from .DenoisingAutoEncoderDataset import DenoisingAutoEncoderDataset from .NoDuplicatesDataLoader import NoDuplicatesDataLoader from .ParallelSentencesDataset import ParallelSentencesDataset from .SentenceLabelDataset import SentenceLabelDataset from .SentencesDataset import SentencesDataset __all__ = [ "DenoisingAutoEncoderDataset", "NoDuplicatesDataLoader", "ParallelSentencesDataset", "SentencesDataset", "SentenceLabelDataset", ]
from __future__ import annotations from .DenoisingAutoEncoderDataset import DenoisingAutoEncoderDataset from .NoDuplicatesDataLoader import NoDuplicatesDataLoader from .ParallelSentencesDataset import ParallelSentencesDataset from .SentenceLabelDataset import SentenceLabelDataset from .SentencesDataset import SentencesDataset __all__ = [ "DenoisingAutoEncoderDataset", "NoDuplicatesDataLoader", "ParallelSentencesDataset", "SentencesDataset", "SentenceLabelDataset", ]
"""Standard LangChain interface tests""" import base64 from pathlib import Path from typing import Literal, cast import httpx import pytest from langchain_core.language_models import BaseChatModel from langchain_core.messages import AIMessage, HumanMessage from langchain_tests.integration_tests import ChatModelIntegrationTests from langchain_openai import ChatOpenAI REPO_ROOT_DIR = Path(__file__).parents[6] class TestOpenAIStandard(ChatModelIntegrationTests): @property def chat_model_class(self) -> type[BaseChatModel]: return ChatOpenAI @property def chat_model_params(self) -> dict: return {"model": "gpt-4o-mini", "stream_usage": True} @property def supports_image_inputs(self) -> bool: return True @property def supports_image_urls(self) -> bool: return True @property def supports_json_mode(self) -> bool: return True @property def supports_anthropic_inputs(self) -> bool: return True @property def supported_usage_metadata_details( self, ) -> dict[ Literal["invoke", "stream"], list[ Literal[ "audio_input", "audio_output", "reasoning_output", "cache_read_input", "cache_creation_input", ] ], ]: return {"invoke": ["reasoning_output", "cache_read_input"], "stream": []} def invoke_with_cache_read_input(self, *, stream: bool = False) -> AIMessage: with open(REPO_ROOT_DIR / "README.md") as f: readme = f.read() input_ = f"""What's langchain? Here's the langchain README: {readme} """ llm = ChatOpenAI(model="gpt-4o-mini", stream_usage=True) _invoke(llm, input_, stream) # invoke twice so first invocation is cached return _invoke(llm, input_, stream) def invoke_with_reasoning_output(self, *, stream: bool = False) -> AIMessage: llm = ChatOpenAI(model="o1-mini", stream_usage=True, temperature=1) input_ = ( "explain the relationship between the 2008/9 economic crisis and the " "startup ecosystem in the early 2010s" ) return _invoke(llm, input_, stream) @property def supports_pdf_inputs(self) -> bool: # OpenAI requires a filename for PDF inputs # For now, we test with filename in OpenAI-specific tests return False def test_openai_pdf_inputs(self, model: BaseChatModel) -> None: """Test that the model can process PDF inputs.""" url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" pdf_data = base64.b64encode(httpx.get(url).content).decode("utf-8") message = HumanMessage( [ {"type": "text", "text": "Summarize this document:"}, { "type": "file", "source_type": "base64", "mime_type": "application/pdf", "data": pdf_data, "filename": "my-pdf", # OpenAI requires a filename }, ] ) _ = model.invoke([message]) # Test OpenAI Chat Completions format message = HumanMessage( [ {"type": "text", "text": "Summarize this document:"}, { "type": "file", "file": { "filename": "test file.pdf", "file_data": f"data:application/pdf;base64,{pdf_data}", }, }, ] ) _ = model.invoke([message]) def _invoke(llm: ChatOpenAI, input_: str, stream: bool) -> AIMessage: if stream: full = None for chunk in llm.stream(input_): full = full + chunk if full else chunk # type: ignore[operator] return cast(AIMessage, full) else: return cast(AIMessage, llm.invoke(input_)) @pytest.mark.skip() # Test either finishes in 5 seconds or 5 minutes. def test_audio_model() -> None: class AudioModelTests(ChatModelIntegrationTests): @property def chat_model_class(self) -> type[ChatOpenAI]: return ChatOpenAI @property def chat_model_params(self) -> dict: return { "model": "gpt-4o-audio-preview", "temperature": 0, "model_kwargs": { "modalities": ["text", "audio"], "audio": {"voice": "alloy", "format": "wav"}, }, } @property def supports_audio_inputs(self) -> bool: return True test_instance = AudioModelTests() model = test_instance.chat_model_class(**test_instance.chat_model_params) AudioModelTests().test_audio_inputs(model)
"""Standard LangChain interface tests""" import base64 from pathlib import Path from typing import Literal, cast import httpx import pytest from langchain_core.language_models import BaseChatModel from langchain_core.messages import AIMessage, HumanMessage from langchain_tests.integration_tests import ChatModelIntegrationTests from langchain_openai import ChatOpenAI REPO_ROOT_DIR = Path(__file__).parents[6] class TestOpenAIStandard(ChatModelIntegrationTests): @property def chat_model_class(self) -> type[BaseChatModel]: return ChatOpenAI @property def chat_model_params(self) -> dict: return {"model": "gpt-4o-mini", "stream_usage": True} @property def supports_image_inputs(self) -> bool: return True @property def supports_image_urls(self) -> bool: return True @property def supports_json_mode(self) -> bool: return True @property def supports_anthropic_inputs(self) -> bool: return True @property def supported_usage_metadata_details( self, ) -> dict[ Literal["invoke", "stream"], list[ Literal[ "audio_input", "audio_output", "reasoning_output", "cache_read_input", "cache_creation_input", ] ], ]: return {"invoke": ["reasoning_output", "cache_read_input"], "stream": []} def invoke_with_cache_read_input(self, *, stream: bool = False) -> AIMessage: with open(REPO_ROOT_DIR / "README.md") as f: readme = f.read() input_ = f"""What's langchain? Here's the langchain README: {readme} """ llm = ChatOpenAI(model="gpt-4o-mini", stream_usage=True) _invoke(llm, input_, stream) # invoke twice so first invocation is cached return _invoke(llm, input_, stream) def invoke_with_reasoning_output(self, *, stream: bool = False) -> AIMessage: llm = ChatOpenAI(model="o1-mini", stream_usage=True, temperature=1) input_ = ( "explain the relationship between the 2008/9 economic crisis and the " "startup ecosystem in the early 2010s" ) return _invoke(llm, input_, stream) @property def supports_pdf_inputs(self) -> bool: # OpenAI requires a filename for PDF inputs # For now, we test with filename in OpenAI-specific tests return False def test_openai_pdf_inputs(self, model: BaseChatModel) -> None: """Test that the model can process PDF inputs.""" url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" pdf_data = base64.b64encode(httpx.get(url).content).decode("utf-8") message = HumanMessage( [ {"type": "text", "text": "Summarize this document:"}, { "type": "file", "source_type": "base64", "mime_type": "application/pdf", "data": pdf_data, "filename": "my-pdf", # OpenAI requires a filename }, ] ) _ = model.invoke([message]) def _invoke(llm: ChatOpenAI, input_: str, stream: bool) -> AIMessage: if stream: full = None for chunk in llm.stream(input_): full = full + chunk if full else chunk # type: ignore[operator] return cast(AIMessage, full) else: return cast(AIMessage, llm.invoke(input_)) @pytest.mark.skip() # Test either finishes in 5 seconds or 5 minutes. def test_audio_model() -> None: class AudioModelTests(ChatModelIntegrationTests): @property def chat_model_class(self) -> type[ChatOpenAI]: return ChatOpenAI @property def chat_model_params(self) -> dict: return { "model": "gpt-4o-audio-preview", "temperature": 0, "model_kwargs": { "modalities": ["text", "audio"], "audio": {"voice": "alloy", "format": "wav"}, }, } @property def supports_audio_inputs(self) -> bool: return True test_instance = AudioModelTests() model = test_instance.chat_model_class(**test_instance.chat_model_params) AudioModelTests().test_audio_inputs(model)
from __future__ import annotations from .BinaryCrossEntropyLoss import BinaryCrossEntropyLoss from .CachedMultipleNegativesRankingLoss import CachedMultipleNegativesRankingLoss from .CrossEntropyLoss import CrossEntropyLoss from .LambdaLoss import ( LambdaLoss, LambdaRankScheme, NDCGLoss1Scheme, NDCGLoss2PPScheme, NDCGLoss2Scheme, NoWeightingScheme, ) from .ListNetLoss import ListNetLoss from .MarginMSELoss import MarginMSELoss from .MSELoss import MSELoss from .MultipleNegativesRankingLoss import MultipleNegativesRankingLoss __all__ = [ "BinaryCrossEntropyLoss", "CrossEntropyLoss", "MultipleNegativesRankingLoss", "CachedMultipleNegativesRankingLoss", "MarginMSELoss", "MSELoss", "ListNetLoss", "LambdaLoss", "NoWeightingScheme", "NDCGLoss1Scheme", "NDCGLoss2Scheme", "LambdaRankScheme", "NDCGLoss2PPScheme", ]
from __future__ import annotations from .BinaryCrossEntropyLoss import BinaryCrossEntropyLoss from .CachedMultipleNegativesRankingLoss import CachedMultipleNegativesRankingLoss from .CrossEntropyLoss import CrossEntropyLoss from .LambdaLoss import ( LambdaLoss, LambdaRankScheme, NDCGLoss1Scheme, NDCGLoss2PPScheme, NDCGLoss2Scheme, NoWeighingScheme, ) from .ListNetLoss import ListNetLoss from .MarginMSELoss import MarginMSELoss from .MSELoss import MSELoss from .MultipleNegativesRankingLoss import MultipleNegativesRankingLoss __all__ = [ "BinaryCrossEntropyLoss", "CrossEntropyLoss", "MultipleNegativesRankingLoss", "CachedMultipleNegativesRankingLoss", "MarginMSELoss", "MSELoss", "ListNetLoss", "LambdaLoss", "NoWeighingScheme", "NDCGLoss1Scheme", "NDCGLoss2Scheme", "LambdaRankScheme", "NDCGLoss2PPScheme", ]
import importlib from types import ModuleType import pytest from fastapi.testclient import TestClient from ...utils import needs_py39 @pytest.fixture( name="mod", params=[ "tutorial008d", "tutorial008d_an", pytest.param("tutorial008d_an_py39", marks=needs_py39), ], ) def get_mod(request: pytest.FixtureRequest): mod = importlib.import_module(f"docs_src.dependencies.{request.param}") return mod def test_get_no_item(mod: ModuleType): client = TestClient(mod.app) response = client.get("/items/foo") assert response.status_code == 404, response.text assert response.json() == {"detail": "Item not found, there's only a plumbus here"} def test_get(mod: ModuleType): client = TestClient(mod.app) response = client.get("/items/plumbus") assert response.status_code == 200, response.text assert response.json() == "plumbus" def test_internal_error(mod: ModuleType): client = TestClient(mod.app) with pytest.raises(mod.InternalError) as exc_info: client.get("/items/portal-gun") assert ( exc_info.value.args[0] == "The portal gun is too dangerous to be owned by Rick" ) def test_internal_server_error(mod: ModuleType): client = TestClient(mod.app, raise_server_exceptions=False) response = client.get("/items/portal-gun") assert response.status_code == 500, response.text assert response.text == "Internal Server Error"
import pytest from fastapi.testclient import TestClient @pytest.fixture(name="client") def get_client(): from docs_src.dependencies.tutorial008d import app client = TestClient(app) return client def test_get_no_item(client: TestClient): response = client.get("/items/foo") assert response.status_code == 404, response.text assert response.json() == {"detail": "Item not found, there's only a plumbus here"} def test_get(client: TestClient): response = client.get("/items/plumbus") assert response.status_code == 200, response.text assert response.json() == "plumbus" def test_internal_error(client: TestClient): from docs_src.dependencies.tutorial008d import InternalError with pytest.raises(InternalError) as exc_info: client.get("/items/portal-gun") assert ( exc_info.value.args[0] == "The portal gun is too dangerous to be owned by Rick" ) def test_internal_server_error(): from docs_src.dependencies.tutorial008d import app client = TestClient(app, raise_server_exceptions=False) response = client.get("/items/portal-gun") assert response.status_code == 500, response.text assert response.text == "Internal Server Error"
""" ===================================== How to write your own Datapoint class ===================================== .. note:: Try on `collab <https://colab.research.google.com/github/pytorch/vision/blob/gh-pages/main/_generated_ipynb_notebooks/plot_custom_datapoints.ipynb>`_ or :ref:`go to the end <sphx_glr_download_auto_examples_v2_transforms_plot_custom_datapoints.py>` to download the full example code. This guide is intended for advanced users and downstream library maintainers. We explain how to write your own datapoint class, and how to make it compatible with the built-in Torchvision v2 transforms. Before continuing, make sure you have read :ref:`sphx_glr_auto_examples_v2_transforms_plot_datapoints.py`. """ # %% import torch from torchvision import datapoints from torchvision.transforms import v2 # %% # We will create a very simple class that just inherits from the base # :class:`~torchvision.datapoints.Datapoint` class. It will be enough to cover # what you need to know to implement your more elaborate uses-cases. If you need # to create a class that carries meta-data, take a look at how the # :class:`~torchvision.datapoints.BoundingBoxes` class is `implemented # <https://github.com/pytorch/vision/blob/main/torchvision/datapoints/_bounding_box.py>`_. class MyDatapoint(datapoints.Datapoint): pass my_dp = MyDatapoint([1, 2, 3]) my_dp # %% # Now that we have defined our custom Datapoint class, we want it to be # compatible with the built-in torchvision transforms, and the functional API. # For that, we need to implement a kernel which performs the core of the # transformation, and then "hook" it to the functional that we want to support # via :func:`~torchvision.transforms.v2.functional.register_kernel`. # # We illustrate this process below: we create a kernel for the "horizontal flip" # operation of our MyDatapoint class, and register it to the functional API. from torchvision.transforms.v2 import functional as F @F.register_kernel(functional="hflip", datapoint_cls=MyDatapoint) def hflip_my_datapoint(my_dp, *args, **kwargs): print("Flipping!") out = my_dp.flip(-1) return datapoints.wrap(out, like=my_dp) # %% # To understand why :func:`~torchvision.datapoints.wrap` is used, see # :ref:`datapoint_unwrapping_behaviour`. Ignore the ``*args, **kwargs`` for now, # we will explain it below in :ref:`param_forwarding`. # # .. note:: # # In our call to ``register_kernel`` above we used a string # ``functional="hflip"`` to refer to the functional we want to hook into. We # could also have used the functional *itself*, i.e. # ``@register_kernel(functional=F.hflip, ...)``. # # Now that we have registered our kernel, we can call the functional API on a # ``MyDatapoint`` instance: my_dp = MyDatapoint(torch.rand(3, 256, 256)) _ = F.hflip(my_dp) # %% # And we can also use the # :class:`~torchvision.transforms.v2.RandomHorizontalFlip` transform, since it relies on :func:`~torchvision.transforms.v2.functional.hflip` internally: t = v2.RandomHorizontalFlip(p=1) _ = t(my_dp) # %% # .. note:: # # We cannot register a kernel for a transform class, we can only register a # kernel for a **functional**. The reason we can't register a transform # class is because one transform may internally rely on more than one # functional, so in general we can't register a single kernel for a given # class. # # .. _param_forwarding: # # Parameter forwarding, and ensuring future compatibility of your kernels # ----------------------------------------------------------------------- # # The functional API that you're hooking into is public and therefore # **backward** compatible: we guarantee that the parameters of these functionals # won't be removed or renamed without a proper deprecation cycle. However, we # don't guarantee **forward** compatibility, and we may add new parameters in # the future. # # Imagine that in a future version, Torchvision adds a new ``inplace`` parameter # to its :func:`~torchvision.transforms.v2.functional.hflip` functional. If you # already defined and registered your own kernel as def hflip_my_datapoint(my_dp): # noqa print("Flipping!") out = my_dp.flip(-1) return datapoints.wrap(out, like=my_dp) # %% # then calling ``F.hflip(my_dp)`` will **fail**, because ``hflip`` will try to # pass the new ``inplace`` parameter to your kernel, but your kernel doesn't # accept it. # # For this reason, we recommend to always define your kernels with # ``*args, **kwargs`` in their signature, as done above. This way, your kernel # will be able to accept any new parameter that we may add in the future. # (Technically, adding `**kwargs` only should be enough).
""" ===================================== How to write your own Datapoint class ===================================== This guide is intended for advanced users and downstream library maintainers. We explain how to write your own datapoint class, and how to make it compatible with the built-in Torchvision v2 transforms. Before continuing, make sure you have read :ref:`sphx_glr_auto_examples_v2_transforms_plot_datapoints.py`. """ # %% import torch from torchvision import datapoints from torchvision.transforms import v2 # %% # We will create a very simple class that just inherits from the base # :class:`~torchvision.datapoints.Datapoint` class. It will be enough to cover # what you need to know to implement your more elaborate uses-cases. If you need # to create a class that carries meta-data, take a look at how the # :class:`~torchvision.datapoints.BoundingBoxes` class is `implemented # <https://github.com/pytorch/vision/blob/main/torchvision/datapoints/_bounding_box.py>`_. class MyDatapoint(datapoints.Datapoint): pass my_dp = MyDatapoint([1, 2, 3]) my_dp # %% # Now that we have defined our custom Datapoint class, we want it to be # compatible with the built-in torchvision transforms, and the functional API. # For that, we need to implement a kernel which performs the core of the # transformation, and then "hook" it to the functional that we want to support # via :func:`~torchvision.transforms.v2.functional.register_kernel`. # # We illustrate this process below: we create a kernel for the "horizontal flip" # operation of our MyDatapoint class, and register it to the functional API. from torchvision.transforms.v2 import functional as F @F.register_kernel(functional="hflip", datapoint_cls=MyDatapoint) def hflip_my_datapoint(my_dp, *args, **kwargs): print("Flipping!") out = my_dp.flip(-1) return datapoints.wrap(out, like=my_dp) # %% # To understand why :func:`~torchvision.datapoints.wrap` is used, see # :ref:`datapoint_unwrapping_behaviour`. Ignore the ``*args, **kwargs`` for now, # we will explain it below in :ref:`param_forwarding`. # # .. note:: # # In our call to ``register_kernel`` above we used a string # ``functional="hflip"`` to refer to the functional we want to hook into. We # could also have used the functional *itself*, i.e. # ``@register_kernel(functional=F.hflip, ...)``. # # Now that we have registered our kernel, we can call the functional API on a # ``MyDatapoint`` instance: my_dp = MyDatapoint(torch.rand(3, 256, 256)) _ = F.hflip(my_dp) # %% # And we can also use the # :class:`~torchvision.transforms.v2.RandomHorizontalFlip` transform, since it relies on :func:`~torchvision.transforms.v2.functional.hflip` internally: t = v2.RandomHorizontalFlip(p=1) _ = t(my_dp) # %% # .. note:: # # We cannot register a kernel for a transform class, we can only register a # kernel for a **functional**. The reason we can't register a transform # class is because one transform may internally rely on more than one # functional, so in general we can't register a single kernel for a given # class. # # .. _param_forwarding: # # Parameter forwarding, and ensuring future compatibility of your kernels # ----------------------------------------------------------------------- # # The functional API that you're hooking into is public and therefore # **backward** compatible: we guarantee that the parameters of these functionals # won't be removed or renamed without a proper deprecation cycle. However, we # don't guarantee **forward** compatibility, and we may add new parameters in # the future. # # Imagine that in a future version, Torchvision adds a new ``inplace`` parameter # to its :func:`~torchvision.transforms.v2.functional.hflip` functional. If you # already defined and registered your own kernel as def hflip_my_datapoint(my_dp): # noqa print("Flipping!") out = my_dp.flip(-1) return datapoints.wrap(out, like=my_dp) # %% # then calling ``F.hflip(my_dp)`` will **fail**, because ``hflip`` will try to # pass the new ``inplace`` parameter to your kernel, but your kernel doesn't # accept it. # # For this reason, we recommend to always define your kernels with # ``*args, **kwargs`` in their signature, as done above. This way, your kernel # will be able to accept any new parameter that we may add in the future. # (Technically, adding `**kwargs` only should be enough).
import gc import unittest import numpy as np import pytest import torch from diffusers import FluxPipeline, FluxPriorReduxPipeline from diffusers.utils import load_image from diffusers.utils.testing_utils import ( Expectations, backend_empty_cache, numpy_cosine_similarity_distance, require_big_accelerator, slow, torch_device, ) @slow @require_big_accelerator @pytest.mark.big_accelerator class FluxReduxSlowTests(unittest.TestCase): pipeline_class = FluxPriorReduxPipeline repo_id = "black-forest-labs/FLUX.1-Redux-dev" base_pipeline_class = FluxPipeline base_repo_id = "black-forest-labs/FLUX.1-schnell" def setUp(self): super().setUp() gc.collect() backend_empty_cache(torch_device) def tearDown(self): super().tearDown() gc.collect() backend_empty_cache(torch_device) def get_inputs(self, device, seed=0): init_image = load_image( "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img5.png" ) return {"image": init_image} def get_base_pipeline_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device="cpu").manual_seed(seed) return { "num_inference_steps": 2, "guidance_scale": 2.0, "output_type": "np", "generator": generator, } def test_flux_redux_inference(self): pipe_redux = self.pipeline_class.from_pretrained(self.repo_id, torch_dtype=torch.bfloat16) pipe_base = self.base_pipeline_class.from_pretrained( self.base_repo_id, torch_dtype=torch.bfloat16, text_encoder=None, text_encoder_2=None ) pipe_redux.to(torch_device) pipe_base.enable_model_cpu_offload(device=torch_device) inputs = self.get_inputs(torch_device) base_pipeline_inputs = self.get_base_pipeline_inputs(torch_device) redux_pipeline_output = pipe_redux(**inputs) image = pipe_base(**base_pipeline_inputs, **redux_pipeline_output).images[0] image_slice = image[0, :10, :10] expected_slices = Expectations( { ("cuda", 7): np.array( [ 0.30078125, 0.37890625, 0.46875, 0.28125, 0.36914062, 0.47851562, 0.28515625, 0.375, 0.4765625, 0.28125, 0.375, 0.48046875, 0.27929688, 0.37695312, 0.47851562, 0.27734375, 0.38085938, 0.4765625, 0.2734375, 0.38085938, 0.47265625, 0.27539062, 0.37890625, 0.47265625, 0.27734375, 0.37695312, 0.47070312, 0.27929688, 0.37890625, 0.47460938, ], dtype=np.float32, ), ("xpu", 3): np.array( [ 0.20507812, 0.30859375, 0.3984375, 0.18554688, 0.30078125, 0.41015625, 0.19921875, 0.3125, 0.40625, 0.19726562, 0.3125, 0.41601562, 0.19335938, 0.31445312, 0.4140625, 0.1953125, 0.3203125, 0.41796875, 0.19726562, 0.32421875, 0.41992188, 0.19726562, 0.32421875, 0.41992188, 0.20117188, 0.32421875, 0.41796875, 0.203125, 0.32617188, 0.41796875, ], dtype=np.float32, ), } ) expected_slice = expected_slices.get_expectation() max_diff = numpy_cosine_similarity_distance(expected_slice.flatten(), image_slice.flatten()) assert max_diff < 1e-4
import gc import unittest import numpy as np import pytest import torch from diffusers import FluxPipeline, FluxPriorReduxPipeline from diffusers.utils import load_image from diffusers.utils.testing_utils import ( Expectations, backend_empty_cache, numpy_cosine_similarity_distance, require_big_accelerator, slow, torch_device, ) @slow @require_big_accelerator @pytest.mark.big_gpu_with_torch_cuda class FluxReduxSlowTests(unittest.TestCase): pipeline_class = FluxPriorReduxPipeline repo_id = "black-forest-labs/FLUX.1-Redux-dev" base_pipeline_class = FluxPipeline base_repo_id = "black-forest-labs/FLUX.1-schnell" def setUp(self): super().setUp() gc.collect() backend_empty_cache(torch_device) def tearDown(self): super().tearDown() gc.collect() backend_empty_cache(torch_device) def get_inputs(self, device, seed=0): init_image = load_image( "https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/style_ziggy/img5.png" ) return {"image": init_image} def get_base_pipeline_inputs(self, device, seed=0): if str(device).startswith("mps"): generator = torch.manual_seed(seed) else: generator = torch.Generator(device="cpu").manual_seed(seed) return { "num_inference_steps": 2, "guidance_scale": 2.0, "output_type": "np", "generator": generator, } def test_flux_redux_inference(self): pipe_redux = self.pipeline_class.from_pretrained(self.repo_id, torch_dtype=torch.bfloat16) pipe_base = self.base_pipeline_class.from_pretrained( self.base_repo_id, torch_dtype=torch.bfloat16, text_encoder=None, text_encoder_2=None ) pipe_redux.to(torch_device) pipe_base.enable_model_cpu_offload(device=torch_device) inputs = self.get_inputs(torch_device) base_pipeline_inputs = self.get_base_pipeline_inputs(torch_device) redux_pipeline_output = pipe_redux(**inputs) image = pipe_base(**base_pipeline_inputs, **redux_pipeline_output).images[0] image_slice = image[0, :10, :10] expected_slices = Expectations( { ("cuda", 7): np.array( [ 0.30078125, 0.37890625, 0.46875, 0.28125, 0.36914062, 0.47851562, 0.28515625, 0.375, 0.4765625, 0.28125, 0.375, 0.48046875, 0.27929688, 0.37695312, 0.47851562, 0.27734375, 0.38085938, 0.4765625, 0.2734375, 0.38085938, 0.47265625, 0.27539062, 0.37890625, 0.47265625, 0.27734375, 0.37695312, 0.47070312, 0.27929688, 0.37890625, 0.47460938, ], dtype=np.float32, ), ("xpu", 3): np.array( [ 0.20507812, 0.30859375, 0.3984375, 0.18554688, 0.30078125, 0.41015625, 0.19921875, 0.3125, 0.40625, 0.19726562, 0.3125, 0.41601562, 0.19335938, 0.31445312, 0.4140625, 0.1953125, 0.3203125, 0.41796875, 0.19726562, 0.32421875, 0.41992188, 0.19726562, 0.32421875, 0.41992188, 0.20117188, 0.32421875, 0.41796875, 0.203125, 0.32617188, 0.41796875, ], dtype=np.float32, ), } ) expected_slice = expected_slices.get_expectation() max_diff = numpy_cosine_similarity_distance(expected_slice.flatten(), image_slice.flatten()) assert max_diff < 1e-4
from typing import Optional import torch from docarray import BaseDoc, DocList from docarray.typing import TorchTensor def test_torch_train(): class Mmdoc(BaseDoc): text: str tensor: Optional[TorchTensor[3, 224, 224]] N = 10 batch = DocList[Mmdoc](Mmdoc(text=f'hello{i}') for i in range(N)) batch.tensor = torch.zeros(N, 3, 224, 224) batch = batch.stack() class Model(torch.nn.Module): def __init__(self): super().__init__() self.conv = torch.nn.Conv2d(3, 16, 3) def forward(self, x): return self.conv(x) model = Model() opt = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) for _ in range(2): loss = model(batch.tensor).sum() loss.backward() opt.step()
from typing import Optional import torch from docarray import BaseDoc, DocArray from docarray.typing import TorchTensor def test_torch_train(): class Mmdoc(BaseDoc): text: str tensor: Optional[TorchTensor[3, 224, 224]] N = 10 batch = DocArray[Mmdoc](Mmdoc(text=f'hello{i}') for i in range(N)) batch.tensor = torch.zeros(N, 3, 224, 224) batch = batch.stack() class Model(torch.nn.Module): def __init__(self): super().__init__() self.conv = torch.nn.Conv2d(3, 16, 3) def forward(self, x): return self.conv(x) model = Model() opt = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) for _ in range(2): loss = model(batch.tensor).sum() loss.backward() opt.step()
"""Gmail tool utils.""" from __future__ import annotations import logging import os from typing import TYPE_CHECKING, List, Optional, Tuple from langchain_core.utils import guard_import if TYPE_CHECKING: from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import Resource from googleapiclient.discovery import build as build_resource logger = logging.getLogger(__name__) def import_google() -> Tuple[Request, Credentials]: """Import google libraries. Returns: Tuple[Request, Credentials]: Request and Credentials classes. """ return ( guard_import( module_name="google.auth.transport.requests", pip_name="google-auth-httplib2", ).Request, guard_import( module_name="google.oauth2.credentials", pip_name="google-auth-httplib2" ).Credentials, ) def import_installed_app_flow() -> InstalledAppFlow: """Import InstalledAppFlow class. Returns: InstalledAppFlow: InstalledAppFlow class. """ return guard_import( module_name="google_auth_oauthlib.flow", pip_name="google-auth-oauthlib" ).InstalledAppFlow def import_googleapiclient_resource_builder() -> build_resource: """Import googleapiclient.discovery.build function. Returns: build_resource: googleapiclient.discovery.build function. """ return guard_import( module_name="googleapiclient.discovery", pip_name="google-api-python-client" ).build DEFAULT_SCOPES = ["https://mail.google.com/"] DEFAULT_CREDS_TOKEN_FILE = "token.json" DEFAULT_CLIENT_SECRETS_FILE = "credentials.json" def get_gmail_credentials( token_file: Optional[str] = None, client_secrets_file: Optional[str] = None, scopes: Optional[List[str]] = None, ) -> Credentials: """Get credentials.""" # From https://developers.google.com/gmail/api/quickstart/python Request, Credentials = import_google() InstalledAppFlow = import_installed_app_flow() creds = None scopes = scopes or DEFAULT_SCOPES token_file = token_file or DEFAULT_CREDS_TOKEN_FILE client_secrets_file = client_secrets_file or DEFAULT_CLIENT_SECRETS_FILE # The file token.json stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists(token_file): creds = Credentials.from_authorized_user_file(token_file, scopes) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: # https://developers.google.com/gmail/api/quickstart/python#authorize_credentials_for_a_desktop_application # noqa flow = InstalledAppFlow.from_client_secrets_file( client_secrets_file, scopes ) creds = flow.run_local_server(port=0, open_browser=False) # Save the credentials for the next run with open(token_file, "w") as token: token.write(creds.to_json()) return creds def build_resource_service( credentials: Optional[Credentials] = None, service_name: str = "gmail", service_version: str = "v1", ) -> Resource: """Build a Gmail service.""" credentials = credentials or get_gmail_credentials() builder = import_googleapiclient_resource_builder() return builder(service_name, service_version, credentials=credentials) def clean_email_body(body: str) -> str: """Clean email body.""" try: from bs4 import BeautifulSoup try: soup = BeautifulSoup(str(body), "html.parser") body = soup.get_text() return str(body) except Exception as e: logger.error(e) return str(body) except ImportError: logger.warning("BeautifulSoup not installed. Skipping cleaning.") return str(body)
"""Gmail tool utils.""" from __future__ import annotations import logging import os from typing import TYPE_CHECKING, List, Optional, Tuple from langchain_core.utils import guard_import if TYPE_CHECKING: from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import Resource from googleapiclient.discovery import build as build_resource logger = logging.getLogger(__name__) def import_google() -> Tuple[Request, Credentials]: """Import google libraries. Returns: Tuple[Request, Credentials]: Request and Credentials classes. """ return ( guard_import( module_name="google.auth.transport.requests", pip_name="google-auth-httplib2", ).Request, guard_import( module_name="google.oauth2.credentials", pip_name="google-auth-httplib2" ).Credentials, ) def import_installed_app_flow() -> InstalledAppFlow: """Import InstalledAppFlow class. Returns: InstalledAppFlow: InstalledAppFlow class. """ return guard_import( module_name="google_auth_oauthlib.flow", pip_name="google-auth-oauthlib" ).InstalledAppFlow def import_googleapiclient_resource_builder() -> build_resource: """Import googleapiclient.discovery.build function. Returns: build_resource: googleapiclient.discovery.build function. """ return guard_import( module_name="googleapiclient.discovery", pip_name="google-api-python-client" ).build DEFAULT_SCOPES = ["https://mail.google.com/"] DEFAULT_CREDS_TOKEN_FILE = "token.json" DEFAULT_CLIENT_SECRETS_FILE = "credentials.json" def get_gmail_credentials( token_file: Optional[str] = None, client_secrets_file: Optional[str] = None, scopes: Optional[List[str]] = None, ) -> Credentials: """Get credentials.""" # From https://developers.google.com/gmail/api/quickstart/python Request, Credentials = import_google() InstalledAppFlow = import_installed_app_flow() creds = None scopes = scopes or DEFAULT_SCOPES token_file = token_file or DEFAULT_CREDS_TOKEN_FILE client_secrets_file = client_secrets_file or DEFAULT_CLIENT_SECRETS_FILE # The file token.json stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists(token_file): creds = Credentials.from_authorized_user_file(token_file, scopes) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) # type: ignore[call-arg] else: # https://developers.google.com/gmail/api/quickstart/python#authorize_credentials_for_a_desktop_application # noqa flow = InstalledAppFlow.from_client_secrets_file( client_secrets_file, scopes ) creds = flow.run_local_server(port=0, open_browser=False) # Save the credentials for the next run with open(token_file, "w") as token: token.write(creds.to_json()) return creds def build_resource_service( credentials: Optional[Credentials] = None, service_name: str = "gmail", service_version: str = "v1", ) -> Resource: """Build a Gmail service.""" credentials = credentials or get_gmail_credentials() builder = import_googleapiclient_resource_builder() return builder(service_name, service_version, credentials=credentials) def clean_email_body(body: str) -> str: """Clean email body.""" try: from bs4 import BeautifulSoup try: soup = BeautifulSoup(str(body), "html.parser") body = soup.get_text() return str(body) except Exception as e: logger.error(e) return str(body) except ImportError: logger.warning("BeautifulSoup not installed. Skipping cleaning.") return str(body)
from enum import Enum from typing import Any from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField class ComparisonOperator(Enum): EQUAL = "==" NOT_EQUAL = "!=" GREATER_THAN = ">" LESS_THAN = "<" GREATER_THAN_OR_EQUAL = ">=" LESS_THAN_OR_EQUAL = "<=" class ConditionBlock(Block): class Input(BlockSchema): value1: Any = SchemaField( description="Enter the first value for comparison", placeholder="For example: 10 or 'hello' or True", ) operator: ComparisonOperator = SchemaField( description="Choose the comparison operator", placeholder="Select an operator", ) value2: Any = SchemaField( description="Enter the second value for comparison", placeholder="For example: 20 or 'world' or False", ) yes_value: Any = SchemaField( description="(Optional) Value to output if the condition is true. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) no_value: Any = SchemaField( description="(Optional) Value to output if the condition is false. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) class Output(BlockSchema): result: bool = SchemaField( description="The result of the condition evaluation (True or False)" ) yes_output: Any = SchemaField( description="The output value if the condition is true" ) no_output: Any = SchemaField( description="The output value if the condition is false" ) def __init__(self): super().__init__( id="715696a0-e1da-45c8-b209-c2fa9c3b0be6", input_schema=ConditionBlock.Input, output_schema=ConditionBlock.Output, description="Handles conditional logic based on comparison operators", categories={BlockCategory.LOGIC}, test_input={ "value1": 10, "operator": ComparisonOperator.GREATER_THAN.value, "value2": 5, "yes_value": "Greater", "no_value": "Not greater", }, test_output=[ ("result", True), ("yes_output", "Greater"), ], ) def run(self, input_data: Input, **kwargs) -> BlockOutput: operator = input_data.operator value1 = input_data.value1 if isinstance(value1, str): try: value1 = float(value1.strip()) except ValueError: value1 = value1.strip() value2 = input_data.value2 if isinstance(value2, str): try: value2 = float(value2.strip()) except ValueError: value2 = value2.strip() yes_value = input_data.yes_value if input_data.yes_value is not None else value1 no_value = input_data.no_value if input_data.no_value is not None else value2 comparison_funcs = { ComparisonOperator.EQUAL: lambda a, b: a == b, ComparisonOperator.NOT_EQUAL: lambda a, b: a != b, ComparisonOperator.GREATER_THAN: lambda a, b: a > b, ComparisonOperator.LESS_THAN: lambda a, b: a < b, ComparisonOperator.GREATER_THAN_OR_EQUAL: lambda a, b: a >= b, ComparisonOperator.LESS_THAN_OR_EQUAL: lambda a, b: a <= b, } result = comparison_funcs[operator](value1, value2) yield "result", result if result: yield "yes_output", yes_value else: yield "no_output", no_value
from enum import Enum from typing import Any from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField class ComparisonOperator(Enum): EQUAL = "==" NOT_EQUAL = "!=" GREATER_THAN = ">" LESS_THAN = "<" GREATER_THAN_OR_EQUAL = ">=" LESS_THAN_OR_EQUAL = "<=" class ConditionBlock(Block): class Input(BlockSchema): value1: Any = SchemaField( description="Enter the first value for comparison", placeholder="For example: 10 or 'hello' or True", ) operator: ComparisonOperator = SchemaField( description="Choose the comparison operator", placeholder="Select an operator", ) value2: Any = SchemaField( description="Enter the second value for comparison", placeholder="For example: 20 or 'world' or False", ) yes_value: Any = SchemaField( description="(Optional) Value to output if the condition is true. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) no_value: Any = SchemaField( description="(Optional) Value to output if the condition is false. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) class Output(BlockSchema): result: bool = SchemaField( description="The result of the condition evaluation (True or False)" ) yes_output: Any = SchemaField( description="The output value if the condition is true" ) no_output: Any = SchemaField( description="The output value if the condition is false" ) def __init__(self): super().__init__( id="715696a0-e1da-45c8-b209-c2fa9c3b0be6", input_schema=ConditionBlock.Input, output_schema=ConditionBlock.Output, description="Handles conditional logic based on comparison operators", categories={BlockCategory.LOGIC}, test_input={ "value1": 10, "operator": ComparisonOperator.GREATER_THAN.value, "value2": 5, "yes_value": "Greater", "no_value": "Not greater", }, test_output=[ ("result", True), ("yes_output", "Greater"), ], ) def run(self, input_data: Input, **kwargs) -> BlockOutput: operator = input_data.operator value1 = input_data.value1 if isinstance(value1, str): value1 = float(value1.strip()) value2 = input_data.value2 if isinstance(value2, str): value2 = float(value2.strip()) yes_value = input_data.yes_value if input_data.yes_value is not None else value1 no_value = input_data.no_value if input_data.no_value is not None else value2 comparison_funcs = { ComparisonOperator.EQUAL: lambda a, b: a == b, ComparisonOperator.NOT_EQUAL: lambda a, b: a != b, ComparisonOperator.GREATER_THAN: lambda a, b: a > b, ComparisonOperator.LESS_THAN: lambda a, b: a < b, ComparisonOperator.GREATER_THAN_OR_EQUAL: lambda a, b: a >= b, ComparisonOperator.LESS_THAN_OR_EQUAL: lambda a, b: a <= b, } result = comparison_funcs[operator](value1, value2) yield "result", result if result: yield "yes_output", yes_value else: yield "no_output", no_value
import numpy as np import pytest import torch from docarray import BaseDocument from docarray.typing import AnyTensor, NdArray, TorchTensor from docarray.utils.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp # type: ignore from docarray.typing import TensorFlowTensor else: TensorFlowTensor = None def test_set_tensor(): class MyDocument(BaseDocument): tensor: AnyTensor d = MyDocument(tensor=np.zeros((3, 224, 224))) assert isinstance(d.tensor, NdArray) assert isinstance(d.tensor, np.ndarray) assert (d.tensor == np.zeros((3, 224, 224))).all() d = MyDocument(tensor=torch.zeros((3, 224, 224))) assert isinstance(d.tensor, TorchTensor) assert isinstance(d.tensor, torch.Tensor) assert (d.tensor == torch.zeros((3, 224, 224))).all() @pytest.mark.tensorflow def test_set_tensor_tensorflow(): class MyDocument(BaseDocument): tensor: AnyTensor d = MyDocument(tensor=tf.zeros((3, 224, 224))) assert isinstance(d.tensor, TensorFlowTensor) assert isinstance(d.tensor.tensor, tf.Tensor) assert tnp.allclose(d.tensor.tensor, tf.zeros((3, 224, 224)))
import numpy as np import pytest import torch from docarray import BaseDocument from docarray.typing import AnyTensor, NdArray, TorchTensor try: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp # type: ignore from docarray.typing import TensorFlowTensor except (ImportError, TypeError): TensorFlowTensor = None def test_set_tensor(): class MyDocument(BaseDocument): tensor: AnyTensor d = MyDocument(tensor=np.zeros((3, 224, 224))) assert isinstance(d.tensor, NdArray) assert isinstance(d.tensor, np.ndarray) assert (d.tensor == np.zeros((3, 224, 224))).all() d = MyDocument(tensor=torch.zeros((3, 224, 224))) assert isinstance(d.tensor, TorchTensor) assert isinstance(d.tensor, torch.Tensor) assert (d.tensor == torch.zeros((3, 224, 224))).all() @pytest.mark.tensorflow def test_set_tensor(): class MyDocument(BaseDocument): tensor: AnyTensor d = MyDocument(tensor=tf.zeros((3, 224, 224))) assert isinstance(d.tensor, TensorFlowTensor) assert isinstance(d.tensor.tensor, tf.Tensor) assert tnp.allclose(d.tensor.tensor, tf.zeros((3, 224, 224)))
import logging from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseNanoBEIREvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") evaluator = SparseNanoBEIREvaluator( dataset_names=None, # None means evaluate on all datasets show_progress_bar=True, batch_size=16, ) # Run evaluation results = evaluator(model) """ Average Queries: 49.92307692307692 Average Corpus: 4334.7692307692305 Aggregated for Score Function: dot Accuracy@1: 59.18% Accuracy@3: 75.37% Accuracy@5: 80.76% Accuracy@10: 86.92% Precision@1: 59.18% Recall@1: 35.62% Precision@3: 36.26% Recall@3: 50.85% Precision@5: 27.75% Recall@5: 56.57% Precision@10: 19.24% Recall@10: 64.31% MRR@10: 0.6848 NDCG@10: 0.6218 Model Query Sparsity: Active Dimensions: 72.7, Sparsity Ratio: 0.9976 Model Corpus Sparsity: Active Dimensions: 165.9, Sparsity Ratio: 0.9946 """ # Print the results print(f"Primary metric: {evaluator.primary_metric}") # => Primary metric: NanoBEIR_mean_dot_ndcg@10 print(f"Primary metric value: {results[evaluator.primary_metric]:.4f}") # => Primary metric value: 0.6218
import logging from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseNanoBEIREvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") evaluator = SparseNanoBEIREvaluator( dataset_names=None, # None means evaluate on all datasets show_progress_bar=True, batch_size=16, ) # Run evaluation results = evaluator(model) """ Average Queries: 49.92307692307692 Average Corpus: 4334.7692307692305 Aggregated for Score Function: dot Accuracy@1: 58.72% Accuracy@3: 75.37% Accuracy@5: 80.76% Accuracy@10: 87.07% Precision@1: 58.72% Recall@1: 35.61% Precision@3: 36.31% Recall@3: 50.84% Precision@5: 27.72% Recall@5: 56.55% Precision@10: 19.18% Recall@10: 64.21% MRR@10: 0.6822 NDCG@10: 0.6204 Model Query Sparsity: Active Dimensions: 74.9, Sparsity Ratio: 0.9975 Model Corpus Sparsity: Active Dimensions: 174.8, Sparsity Ratio: 0.9943 """ # Print the results print(f"Primary metric: {evaluator.primary_metric}") # => Primary metric: NanoBEIR_mean_dot_ndcg@10 print(f"Primary metric value: {results[evaluator.primary_metric]:.4f}") # => Primary metric value: 0.6204
from . import InputExample import gzip class PairedFilesReader(object): """Reads in the a Pair Dataset, split in two files""" def __init__(self, filepaths): self.filepaths = filepaths def get_examples(self, max_examples=0): fIns = [] for filepath in self.filepaths: fIn = ( gzip.open(filepath, "rt", encoding="utf-8") if filepath.endswith(".gz") else open(filepath, encoding="utf-8") ) fIns.append(fIn) examples = [] eof = False while not eof: texts = [] for fIn in fIns: text = fIn.readline() if text == "": eof = True break texts.append(text) if eof: break examples.append(InputExample(guid=str(len(examples)), texts=texts, label=1)) if max_examples > 0 and len(examples) >= max_examples: break return examples
from . import InputExample import gzip class PairedFilesReader(object): """ Reads in the a Pair Dataset, split in two files """ def __init__(self, filepaths): self.filepaths = filepaths def get_examples(self, max_examples=0): """ """ fIns = [] for filepath in self.filepaths: fIn = ( gzip.open(filepath, "rt", encoding="utf-8") if filepath.endswith(".gz") else open(filepath, encoding="utf-8") ) fIns.append(fIn) examples = [] eof = False while not eof: texts = [] for fIn in fIns: text = fIn.readline() if text == "": eof = True break texts.append(text) if eof: break examples.append(InputExample(guid=str(len(examples)), texts=texts, label=1)) if max_examples > 0 and len(examples) >= max_examples: break return examples
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"]
_base_ = [ '../_base_/models/mask-rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] norm_cfg = dict(type='BN', requires_grad=True) image_size = (640, 640) batch_augments = [dict(type='BatchFixedSizePad', size=image_size)] model = dict( data_preprocessor=dict(pad_size_divisor=64, batch_augments=batch_augments), backbone=dict(norm_cfg=norm_cfg, norm_eval=False), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, norm_cfg=norm_cfg, num_outs=5), roi_head=dict( bbox_head=dict(norm_cfg=norm_cfg), mask_head=dict(norm_cfg=norm_cfg))) dataset_type = 'CocoDataset' data_root = 'data/coco/' train_pipeline = [ dict(type='LoadImageFromFile', backend_args={{_base_.backend_args}}), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='RandomResize', scale=image_size, ratio_range=(0.8, 1.2), keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, allow_negative_crop=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', backend_args={{_base_.backend_args}}), dict(type='Resize', scale=image_size, keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=8, num_workers=4, dataset=dict(pipeline=train_pipeline)) val_dataloader = dict(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader # learning policy max_epochs = 50 train_cfg = dict(max_epochs=max_epochs, val_interval=2) param_scheduler = [ dict(type='LinearLR', start_factor=0.1, by_epoch=False, begin=0, end=1000), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[30, 40], gamma=0.1) ] # optimizer optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001), paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True), clip_grad=None) # 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/mask-rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] norm_cfg = dict(type='BN', requires_grad=True) image_size = (640, 640) batch_augments = [dict(type='BatchFixedSizePad', size=image_size)] model = dict( data_preprocessor=dict(pad_size_divisor=64, batch_augments=batch_augments), backbone=dict(norm_cfg=norm_cfg, norm_eval=False), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, norm_cfg=norm_cfg, num_outs=5), roi_head=dict( bbox_head=dict(norm_cfg=norm_cfg), mask_head=dict(norm_cfg=norm_cfg))) dataset_type = 'CocoDataset' data_root = 'data/coco/' train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='RandomResize', scale=image_size, ratio_range=(0.8, 1.2), keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, allow_negative_crop=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='Resize', scale=image_size, keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=8, num_workers=4, dataset=dict(pipeline=train_pipeline)) val_dataloader = dict(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader # learning policy max_epochs = 50 train_cfg = dict(max_epochs=max_epochs, val_interval=2) param_scheduler = [ dict(type='LinearLR', start_factor=0.1, by_epoch=False, begin=0, end=1000), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[30, 40], gamma=0.1) ] # optimizer optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001), paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True), clip_grad=None) # 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)
import torch import torchaudio.prototype.functional as F from parameterized import parameterized from torch.autograd import gradcheck from torchaudio_unittest.common_utils import TestBaseMixin class AutogradTestImpl(TestBaseMixin): @parameterized.expand( [ (8000, (2, 3, 5, 7)), (8000, (8000, 1)), ] ) def test_oscillator_bank(self, sample_rate, shape): # can be replaced with math.prod when we drop 3.7 support def prod(iterable): ret = 1 for item in iterable: ret *= item return ret numel = prod(shape) # use 1.9 instead of 2 so as to include values above nyquist frequency fmax = sample_rate / 1.9 freq = torch.linspace(-fmax, fmax, numel, dtype=self.dtype, device=self.device, requires_grad=True).reshape( shape ) amps = torch.linspace(-5, 5, numel, dtype=self.dtype, device=self.device, requires_grad=True).reshape(shape) assert gradcheck(F.oscillator_bank, (freq, amps, sample_rate)) def test_extend_pitch(self): num_frames, num_pitches = 5, 7 input = torch.ones((num_frames, 1), device=self.device, dtype=self.dtype, requires_grad=True) pattern = torch.linspace(1, num_pitches, num_pitches, device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.extend_pitch, (input, num_pitches)) assert gradcheck(F.extend_pitch, (input, pattern)) def test_sinc_ir(self): cutoff = torch.tensor([0, 0.5, 1.0], device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.sinc_impulse_response, (cutoff, 513, False)) assert gradcheck(F.sinc_impulse_response, (cutoff, 513, True)) def test_freq_ir(self): mags = torch.tensor([0, 0.5, 1.0], device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.frequency_impulse_response, (mags,)) def test_filter_waveform(self): waveform = torch.rand(3, 1, 2, 10, device=self.device, dtype=self.dtype, requires_grad=True) filters = torch.rand(3, 2, device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.filter_waveform, (waveform, filters))
import torch import torchaudio.prototype.functional as F from parameterized import parameterized from torch.autograd import gradcheck, gradgradcheck from torchaudio_unittest.common_utils import nested_params, TestBaseMixin class AutogradTestImpl(TestBaseMixin): @nested_params( [F.convolve, F.fftconvolve], ["full", "valid", "same"], ) def test_convolve(self, fn, mode): leading_dims = (4, 3, 2) L_x, L_y = 23, 40 x = torch.rand(*leading_dims, L_x, dtype=self.dtype, device=self.device, requires_grad=True) y = torch.rand(*leading_dims, L_y, dtype=self.dtype, device=self.device, requires_grad=True) self.assertTrue(gradcheck(fn, (x, y, mode))) self.assertTrue(gradgradcheck(fn, (x, y, mode))) def test_add_noise(self): leading_dims = (5, 2, 3) L = 51 waveform = torch.rand(*leading_dims, L, dtype=self.dtype, device=self.device, requires_grad=True) noise = torch.rand(*leading_dims, L, dtype=self.dtype, device=self.device, requires_grad=True) lengths = torch.rand(*leading_dims, dtype=self.dtype, device=self.device, requires_grad=True) snr = torch.rand(*leading_dims, dtype=self.dtype, device=self.device, requires_grad=True) * 10 self.assertTrue(gradcheck(F.add_noise, (waveform, noise, snr, lengths))) self.assertTrue(gradgradcheck(F.add_noise, (waveform, noise, snr, lengths))) @parameterized.expand( [ (8000, (2, 3, 5, 7)), (8000, (8000, 1)), ] ) def test_oscillator_bank(self, sample_rate, shape): # can be replaced with math.prod when we drop 3.7 support def prod(iterable): ret = 1 for item in iterable: ret *= item return ret numel = prod(shape) # use 1.9 instead of 2 so as to include values above nyquist frequency fmax = sample_rate / 1.9 freq = torch.linspace(-fmax, fmax, numel, dtype=self.dtype, device=self.device, requires_grad=True).reshape( shape ) amps = torch.linspace(-5, 5, numel, dtype=self.dtype, device=self.device, requires_grad=True).reshape(shape) assert gradcheck(F.oscillator_bank, (freq, amps, sample_rate)) def test_extend_pitch(self): num_frames, num_pitches = 5, 7 input = torch.ones((num_frames, 1), device=self.device, dtype=self.dtype, requires_grad=True) pattern = torch.linspace(1, num_pitches, num_pitches, device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.extend_pitch, (input, num_pitches)) assert gradcheck(F.extend_pitch, (input, pattern)) def test_sinc_ir(self): cutoff = torch.tensor([0, 0.5, 1.0], device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.sinc_impulse_response, (cutoff, 513, False)) assert gradcheck(F.sinc_impulse_response, (cutoff, 513, True)) def test_speed(self): leading_dims = (3, 2) T = 200 waveform = torch.rand(*leading_dims, T, dtype=self.dtype, device=self.device, requires_grad=True) lengths = torch.randint(1, T, leading_dims, dtype=self.dtype, device=self.device) self.assertTrue(gradcheck(F.speed, (waveform, lengths, 1000, 1.1))) self.assertTrue(gradgradcheck(F.speed, (waveform, lengths, 1000, 1.1))) def test_preemphasis(self): waveform = torch.rand(3, 2, 100, device=self.device, dtype=self.dtype, requires_grad=True) coeff = 0.9 self.assertTrue(gradcheck(F.preemphasis, (waveform, coeff))) self.assertTrue(gradgradcheck(F.preemphasis, (waveform, coeff))) def test_deemphasis(self): waveform = torch.rand(3, 2, 100, device=self.device, dtype=self.dtype, requires_grad=True) coeff = 0.9 self.assertTrue(gradcheck(F.deemphasis, (waveform, coeff))) self.assertTrue(gradgradcheck(F.deemphasis, (waveform, coeff))) def test_freq_ir(self): mags = torch.tensor([0, 0.5, 1.0], device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.frequency_impulse_response, (mags,)) def test_filter_waveform(self): waveform = torch.rand(3, 1, 2, 10, device=self.device, dtype=self.dtype, requires_grad=True) filters = torch.rand(3, 2, device=self.device, dtype=self.dtype, requires_grad=True) assert gradcheck(F.filter_waveform, (waveform, filters))
# Copyright (c) OpenMMLab. All rights reserved. import os from importlib.util import find_spec as find_module import numpy import numpy.compat import numpy.linalg as linalg from mmengine.config import Config from mmengine.fileio import LocalBackend as local from mmengine.fileio import PetrelBackend from ._base_.default_runtime import default_scope as scope from ._base_.scheduler import val_cfg from rich.progress import Progress start = Progress.start
# Copyright (c) OpenMMLab. All rights reserved. import os from importlib.util import find_spec as find_module import numpy import numpy.compat import numpy.linalg as linalg from mmengine.config import Config from mmengine.fileio import LocalBackend as local from mmengine.fileio import PetrelBackend from ._base_.default_runtime import default_scope as scope from ._base_.scheduler import val_cfg
from __future__ import annotations from typing import Literal from sentence_transformers.losses.GISTEmbedLoss import GISTEmbedLoss from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class SparseGISTEmbedLoss(GISTEmbedLoss): def __init__( self, model: SparseEncoder, guide: SparseEncoder, temperature: float = 0.1, margin_strategy: Literal["absolute", "relative"] = "absolute", margin: float = 0.0, ) -> None: """ This loss is used to train a SparseEncoder model using the GISTEmbed algorithm. It takes a model and a guide model as input, and uses the guide model to guide the in-batch negative sample selection. The cosine similarity is used to compute the loss and the temperature parameter is used to scale the cosine similarities. You can apply different false-negative filtering strategies to discard hard negatives that are too similar to the positive. Two strategies are supported: - "absolute": Discards negatives whose similarity score is greater than or equal to ``positive_score - margin``. - "relative": Discards negatives whose similarity score is greater than or equal to ``positive_score * (1 - margin)``. Args: model: SparseEncoder model based on a `transformers` model. guide: SparseEncoder model to guide the in-batch negative sample selection. temperature: Temperature parameter to scale the cosine similarities. Defaults to 0.1, adapted for Sparse embeddings. Experimentation is recommended. margin_strategy: Strategy used for false negative filtering. One of {"absolute", "relative"}. margin: The margin value for filtering negatives. Defaults to 0.0, together with the "absolute" strategy, this only removes negatives that are more similar to the query than the positive is to the query. References: - For further details, see: https://arxiv.org/abs/2402.16829 Requirements: 1. (anchor, positive, negative) triplets 2. (anchor, positive) pairs Inputs: +---------------------------------------+--------+ | Texts | Labels | +=======================================+========+ | (anchor, positive, negative) triplets | none | +---------------------------------------+--------+ | (anchor, positive) pairs | none | +---------------------------------------+--------+ Recommendations: - Use ``BatchSamplers.NO_DUPLICATES`` (:class:`docs <sentence_transformers.training_args.BatchSamplers>`) to ensure that no in-batch negatives are duplicates of the anchor or positive samples. Relations: - :class:`SparseMultipleNegativesRankingLoss` is similar to this loss, but it does not use a guide model to guide the in-batch negative sample selection. :class:`SparseGISTEmbedLoss` yields a stronger training signal at the cost of some training overhead. Example: :: from datasets import Dataset from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses # Initialize the SPLADE model model = SparseEncoder("distilbert/distilbert-base-uncased") guide = SparseEncoder("naver/splade-cocondenser-ensembledistil") train_dataset = Dataset.from_dict( { "anchor": ["It's nice weather outside today.", "He drove to work."], "positive": ["It's so sunny.", "He took the car to the office."], } ) loss = losses.SparseGISTEmbedLoss(model, guide=guide) trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss) trainer.train() """ return super().__init__( model, guide=guide, temperature=temperature, margin_strategy=margin_strategy, margin=margin )
from __future__ import annotations from typing import Literal from sentence_transformers.losses.GISTEmbedLoss import GISTEmbedLoss from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class SparseGISTEmbedLoss(GISTEmbedLoss): def __init__( self, model: SparseEncoder, guide: SparseEncoder, temperature: float = 0.1, margin_strategy: Literal["absolute", "relative"] = "absolute", margin: float = 0.0, ) -> None: """ This loss is used to train a SparseEncoder model using the GISTEmbed algorithm. It takes a model and a guide model as input, and uses the guide model to guide the in-batch negative sample selection. The cosine similarity is used to compute the loss and the temperature parameter is used to scale the cosine similarities. You can apply different false-negative filtering strategies to discard hard negatives that are too similar to the positive. Two strategies are supported: - "absolute": Discards negatives whose similarity score is greater than or equal to ``positive_score - margin``. - "relative": Discards negatives whose similarity score is greater than or equal to ``positive_score * (1 - margin)``. Args: model: SparseEncoder model based on a `transformers` model. guide: SparseEncoder model to guide the in-batch negative sample selection. temperature: Temperature parameter to scale the cosine similarities, default is 0.1 here adapted for Sparse embeddings, might need some adaptations. margin_strategy: Strategy used for false negative filtering. One of {"absolute", "relative"}. margin: The margin value for filtering negatives. Defaults to 0.0, together with the "absolute" strategy, this only removes negatives that are more similar to the query than the positive is to the query. References: - For further details, see: https://arxiv.org/abs/2402.16829 Requirements: 1. (anchor, positive, negative) triplets 2. (anchor, positive) pairs Inputs: +---------------------------------------+--------+ | Texts | Labels | +=======================================+========+ | (anchor, positive, negative) triplets | none | +---------------------------------------+--------+ | (anchor, positive) pairs | none | +---------------------------------------+--------+ Recommendations: - Use ``BatchSamplers.NO_DUPLICATES`` (:class:`docs <sentence_transformers.training_args.BatchSamplers>`) to ensure that no in-batch negatives are duplicates of the anchor or positive samples. Relations: - :class:`SparseMultipleNegativesRankingLoss` is similar to this loss, but it does not use a guide model to guide the in-batch negative sample selection. `SparseGISTEmbedLoss` yields a stronger training signal at the cost of some training overhead. Example: :: from datasets import Dataset from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses # Initialize the SPLADE model model = SparseEncoder("distilbert/distilbert-base-uncased") guide = SparseEncoder("naver/splade-cocondenser-ensembledistil") train_dataset = Dataset.from_dict( { "anchor": ["It's nice weather outside today.", "He drove to work."], "positive": ["It's so sunny.", "He took the car to the office."], } ) loss = losses.SparseGISTEmbedLoss(model, guide=guide) trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss) trainer.train() """ return super().__init__( model, guide=guide, temperature=temperature, margin_strategy=margin_strategy, margin=margin )
from torch import Tensor from torch import nn from typing import Dict import torch.nn.functional as F class Normalize(nn.Module): """This layer normalizes embeddings to unit length""" def __init__(self): super(Normalize, self).__init__() def forward(self, features: Dict[str, Tensor]): features.update({"sentence_embedding": F.normalize(features["sentence_embedding"], p=2, dim=1)}) return features def save(self, output_path): pass @staticmethod def load(input_path): return Normalize()
from torch import Tensor from torch import nn from typing import Dict import torch.nn.functional as F class Normalize(nn.Module): """ This layer normalizes embeddings to unit length """ def __init__(self): super(Normalize, self).__init__() def forward(self, features: Dict[str, Tensor]): features.update({"sentence_embedding": F.normalize(features["sentence_embedding"], p=2, dim=1)}) return features def save(self, output_path): pass @staticmethod def load(input_path): return Normalize()
from typing import Dict, List, Optional, Tuple from torch import Tensor AVAILABLE_METRICS = ["mae", "rmse", "epe", "bad1", "bad2", "epe", "1px", "3px", "5px", "fl-all", "relepe"] def compute_metrics( flow_pred: Tensor, flow_gt: Tensor, valid_flow_mask: Optional[Tensor], metrics: List[str] ) -> Tuple[Dict[str, float], int]: for m in metrics: if m not in AVAILABLE_METRICS: raise ValueError(f"Invalid metric: {m}. Valid metrics are: {AVAILABLE_METRICS}") metrics_dict = {} pixels_diffs = (flow_pred - flow_gt).abs() # there is no Y flow in Stereo Matching, therefore flow.abs() = flow.pow(2).sum(dim=1).sqrt() flow_norm = flow_gt.abs() if valid_flow_mask is not None: valid_flow_mask = valid_flow_mask.unsqueeze(1) pixels_diffs = pixels_diffs[valid_flow_mask] flow_norm = flow_norm[valid_flow_mask] num_pixels = pixels_diffs.numel() if "bad1" in metrics: metrics_dict["bad1"] = (pixels_diffs > 1).float().mean().item() if "bad2" in metrics: metrics_dict["bad2"] = (pixels_diffs > 2).float().mean().item() if "mae" in metrics: metrics_dict["mae"] = pixels_diffs.mean().item() if "rmse" in metrics: metrics_dict["rmse"] = pixels_diffs.pow(2).mean().sqrt().item() if "epe" in metrics: metrics_dict["epe"] = pixels_diffs.mean().item() if "1px" in metrics: metrics_dict["1px"] = (pixels_diffs < 1).float().mean().item() if "3px" in metrics: metrics_dict["3px"] = (pixels_diffs < 3).float().mean().item() if "5px" in metrics: metrics_dict["5px"] = (pixels_diffs < 5).float().mean().item() if "fl-all" in metrics: metrics_dict["fl-all"] = ((pixels_diffs < 3) & ((pixels_diffs / flow_norm) < 0.05)).float().mean().item() * 100 if "relepe" in metrics: metrics_dict["relepe"] = (pixels_diffs / flow_norm).mean().item() return metrics_dict, num_pixels
from typing import Dict, List, Optional, Tuple from torch import Tensor AVAILABLE_METRICS = ["mae", "rmse", "epe", "bad1", "bad2", "epe", "1px", "3px", "5px", "fl-all", "relepe"] def compute_metrics( flow_pred: Tensor, flow_gt: Tensor, valid_flow_mask: Optional[Tensor], metrics: List[str] ) -> Tuple[Dict[str, float], int]: for m in metrics: if m not in AVAILABLE_METRICS: raise ValueError(f"Invalid metric: {m}. Valid metrics are: {AVAILABLE_METRICS}") metrics_dict = {} pixels_diffs = (flow_pred - flow_gt).abs() # there is no Y flow in Stereo Matching, therefor flow.abs() = flow.pow(2).sum(dim=1).sqrt() flow_norm = flow_gt.abs() if valid_flow_mask is not None: valid_flow_mask = valid_flow_mask.unsqueeze(1) pixels_diffs = pixels_diffs[valid_flow_mask] flow_norm = flow_norm[valid_flow_mask] num_pixels = pixels_diffs.numel() if "bad1" in metrics: metrics_dict["bad1"] = (pixels_diffs > 1).float().mean().item() if "bad2" in metrics: metrics_dict["bad2"] = (pixels_diffs > 2).float().mean().item() if "mae" in metrics: metrics_dict["mae"] = pixels_diffs.mean().item() if "rmse" in metrics: metrics_dict["rmse"] = pixels_diffs.pow(2).mean().sqrt().item() if "epe" in metrics: metrics_dict["epe"] = pixels_diffs.mean().item() if "1px" in metrics: metrics_dict["1px"] = (pixels_diffs < 1).float().mean().item() if "3px" in metrics: metrics_dict["3px"] = (pixels_diffs < 3).float().mean().item() if "5px" in metrics: metrics_dict["5px"] = (pixels_diffs < 5).float().mean().item() if "fl-all" in metrics: metrics_dict["fl-all"] = ((pixels_diffs < 3) & ((pixels_diffs / flow_norm) < 0.05)).float().mean().item() * 100 if "relepe" in metrics: metrics_dict["relepe"] = (pixels_diffs / flow_norm).mean().item() return metrics_dict, num_pixels
# Configuration file for the Sphinx documentation builder. # # This file only contains a selection of the most common options. For a full # list see the documentation: # https://www.sphinx-doc.org/en/master/usage/configuration.html # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # import os import sys import pytorch_sphinx_theme sys.path.insert(0, os.path.abspath('../..')) # -- Project information ----------------------------------------------------- project = 'mmengine' copyright = '2022, mmengine contributors' author = 'mmengine contributors' version_file = '../../mmengine/version.py' with open(version_file) as f: exec(compile(f.read(), version_file, 'exec')) __version__ = locals()['__version__'] # The short X.Y version version = __version__ # The full version, including alpha/beta/rc tags release = __version__ # -- General configuration --------------------------------------------------- # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinx.ext.intersphinx', 'sphinx.ext.napoleon', 'sphinx.ext.viewcode', 'myst_parser', 'sphinx_copybutton', 'sphinx.ext.autodoc.typehints', ] # yapf: disable autodoc_typehints = 'description' myst_heading_anchors = 4 myst_enable_extensions = ['colon_fence'] # Configuration for intersphinx intersphinx_mapping = { 'python': ('https://docs.python.org/3', None), 'numpy': ('https://numpy.org/doc/stable', None), 'torch': ('https://pytorch.org/docs/stable/', None), 'mmcv': ('https://mmcv.readthedocs.io/en/2.x/', None), } # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # html_theme = 'pytorch_sphinx_theme' html_theme_path = [pytorch_sphinx_theme.get_html_theme_path()] html_theme_options = { 'menu': [ { 'name': 'GitHub', 'url': 'https://github.com/open-mmlab/mmengine' }, ], # Specify the language of shared menu 'menu_lang': 'en', } # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] html_css_files = ['css/readthedocs.css'] # -- Extension configuration ------------------------------------------------- # Ignore >>> when copying code copybutton_prompt_text = r'>>> |\.\.\. ' copybutton_prompt_is_regexp = True
# Configuration file for the Sphinx documentation builder. # # This file only contains a selection of the most common options. For a full # list see the documentation: # https://www.sphinx-doc.org/en/master/usage/configuration.html # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # import os import sys import pytorch_sphinx_theme sys.path.insert(0, os.path.abspath('../..')) # -- Project information ----------------------------------------------------- project = 'mmengine' copyright = '2022, mmengine contributors' author = 'mmengine contributors' version_file = '../../mmengine/version.py' with open(version_file) as f: exec(compile(f.read(), version_file, 'exec')) __version__ = locals()['__version__'] # The short X.Y version version = __version__ # The full version, including alpha/beta/rc tags release = __version__ # -- General configuration --------------------------------------------------- # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', 'sphinx.ext.intersphinx', 'sphinx.ext.napoleon', 'sphinx.ext.viewcode', 'sphinx.ext.autosectionlabel', 'myst_parser', 'sphinx_copybutton', 'sphinx.ext.autodoc.typehints', ] # yapf: disable autodoc_typehints = 'description' myst_heading_anchors = 4 myst_enable_extensions = ['colon_fence'] # Configuration for intersphinx intersphinx_mapping = { 'python': ('https://docs.python.org/3', None), 'numpy': ('https://numpy.org/doc/stable', None), 'torch': ('https://pytorch.org/docs/stable/', None), 'mmcv': ('https://mmcv.readthedocs.io/en/2.x/', None), } # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # html_theme = 'pytorch_sphinx_theme' html_theme_path = [pytorch_sphinx_theme.get_html_theme_path()] html_theme_options = { 'menu': [ { 'name': 'GitHub', 'url': 'https://github.com/open-mmlab/mmengine' }, ], # Specify the language of shared menu 'menu_lang': 'en', } # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] html_css_files = ['css/readthedocs.css'] # -- Extension configuration ------------------------------------------------- # Ignore >>> when copying code copybutton_prompt_text = r'>>> |\.\.\. ' copybutton_prompt_is_regexp = True
import os import pkg_resources from setuptools import setup, find_packages setup( name="whisper", py_modules=["whisper"], version="1.0", description="Robust Speech Recognition via Large-Scale Weak Supervision", readme="README.md", python_requires=">=3.7", author="OpenAI", url="https://github.com/openai/whisper", license="MIT", packages=find_packages(exclude=["tests*"]), install_requires=[ str(r) for r in pkg_resources.parse_requirements( open(os.path.join(os.path.dirname(__file__), "requirements.txt")) ) ], entry_points = { 'console_scripts': ['whisper=whisper.transcribe:cli'], }, include_package_data=True, extras_require={'dev': ['pytest']}, )
import os import pkg_resources from setuptools import setup, find_packages setup( name="whisper", py_modules=["whisper"], version="1.0", description="", author="OpenAI", packages=find_packages(exclude=["tests*"]), install_requires=[ str(r) for r in pkg_resources.parse_requirements( open(os.path.join(os.path.dirname(__file__), "requirements.txt")) ) ], entry_points = { 'console_scripts': ['whisper=whisper.transcribe:cli'], }, include_package_data=True, extras_require={'dev': ['pytest']}, )
import contextlib import logging import typing import fastapi import fastapi.responses import starlette.middleware.cors import uvicorn from autogpt_libs.feature_flag.client import ( initialize_launchdarkly, shutdown_launchdarkly, ) import backend.data.block import backend.data.db import backend.data.graph import backend.data.user import backend.server.routers.v1 import backend.util.service import backend.util.settings settings = backend.util.settings.Settings() logger = logging.getLogger(__name__) logging.getLogger("autogpt_libs").setLevel(logging.INFO) @contextlib.contextmanager def launch_darkly_context(): if settings.config.app_env != backend.util.settings.AppEnvironment.LOCAL: initialize_launchdarkly() try: yield finally: shutdown_launchdarkly() else: yield @contextlib.asynccontextmanager async def lifespan_context(app: fastapi.FastAPI): await backend.data.db.connect() await backend.data.block.initialize_blocks() await backend.data.user.migrate_and_encrypt_user_integrations() await backend.data.graph.fix_llm_provider_credentials() with launch_darkly_context(): yield await backend.data.db.disconnect() docs_url = ( "/docs" if settings.config.app_env == backend.util.settings.AppEnvironment.LOCAL else None ) app = fastapi.FastAPI( title="AutoGPT Agent Server", description=( "This server is used to execute agents that are created by the " "AutoGPT system." ), summary="AutoGPT Agent Server", version="0.1", lifespan=lifespan_context, docs_url=docs_url, ) def handle_internal_http_error(status_code: int = 500, log_error: bool = True): def handler(request: fastapi.Request, exc: Exception): if log_error: logger.exception(f"{request.method} {request.url.path} failed: {exc}") return fastapi.responses.JSONResponse( content={ "message": f"{request.method} {request.url.path} failed", "detail": str(exc), }, status_code=status_code, ) return handler app.add_exception_handler(ValueError, handle_internal_http_error(400)) app.add_exception_handler(Exception, handle_internal_http_error(500)) app.include_router(backend.server.routers.v1.v1_router, tags=["v1"]) @app.get(path="/health", tags=["health"], dependencies=[]) async def health(): return {"status": "healthy"} class AgentServer(backend.util.service.AppProcess): def run(self): server_app = starlette.middleware.cors.CORSMiddleware( app=app, allow_origins=settings.config.backend_cors_allow_origins, allow_credentials=True, allow_methods=["*"], # Allows all methods allow_headers=["*"], # Allows all headers ) uvicorn.run( server_app, host=backend.util.settings.Config().agent_api_host, port=backend.util.settings.Config().agent_api_port, ) @staticmethod async def test_execute_graph( graph_id: str, node_input: dict[typing.Any, typing.Any], user_id: str ): return backend.server.routers.v1.execute_graph(graph_id, node_input, user_id) @staticmethod async def test_create_graph( create_graph: backend.server.routers.v1.CreateGraph, user_id: str, is_template=False, ): return await backend.server.routers.v1.create_new_graph(create_graph, user_id) @staticmethod async def test_get_graph_run_status( graph_id: str, graph_exec_id: str, user_id: str ): return await backend.server.routers.v1.get_graph_run_status( graph_id, graph_exec_id, user_id ) @staticmethod async def test_get_graph_run_node_execution_results( graph_id: str, graph_exec_id: str, user_id: str ): return await backend.server.routers.v1.get_graph_run_node_execution_results( graph_id, graph_exec_id, user_id ) @staticmethod async def test_delete_graph(graph_id: str, user_id: str): return await backend.server.routers.v1.delete_graph(graph_id, user_id) def set_test_dependency_overrides(self, overrides: dict): app.dependency_overrides.update(overrides)
import contextlib import logging import typing import fastapi import fastapi.responses import starlette.middleware.cors import uvicorn from autogpt_libs.feature_flag.client import ( initialize_launchdarkly, shutdown_launchdarkly, ) import backend.data.block import backend.data.db import backend.data.graph import backend.data.user import backend.server.routers.v1 import backend.util.service import backend.util.settings settings = backend.util.settings.Settings() logger = logging.getLogger(__name__) logging.getLogger("autogpt_libs").setLevel(logging.INFO) @contextlib.asynccontextmanager async def lifespan_context(app: fastapi.FastAPI): await backend.data.db.connect() await backend.data.block.initialize_blocks() await backend.data.user.migrate_and_encrypt_user_integrations() await backend.data.graph.fix_llm_provider_credentials() initialize_launchdarkly() yield shutdown_launchdarkly() await backend.data.db.disconnect() docs_url = ( "/docs" if settings.config.app_env == backend.util.settings.AppEnvironment.LOCAL else None ) app = fastapi.FastAPI( title="AutoGPT Agent Server", description=( "This server is used to execute agents that are created by the " "AutoGPT system." ), summary="AutoGPT Agent Server", version="0.1", lifespan=lifespan_context, docs_url=docs_url, ) def handle_internal_http_error(status_code: int = 500, log_error: bool = True): def handler(request: fastapi.Request, exc: Exception): if log_error: logger.exception(f"{request.method} {request.url.path} failed: {exc}") return fastapi.responses.JSONResponse( content={ "message": f"{request.method} {request.url.path} failed", "detail": str(exc), }, status_code=status_code, ) return handler app.add_exception_handler(ValueError, handle_internal_http_error(400)) app.add_exception_handler(Exception, handle_internal_http_error(500)) app.include_router(backend.server.routers.v1.v1_router, tags=["v1"]) @app.get(path="/health", tags=["health"], dependencies=[]) async def health(): return {"status": "healthy"} class AgentServer(backend.util.service.AppProcess): def run(self): server_app = starlette.middleware.cors.CORSMiddleware( app=app, allow_origins=settings.config.backend_cors_allow_origins, allow_credentials=True, allow_methods=["*"], # Allows all methods allow_headers=["*"], # Allows all headers ) uvicorn.run( server_app, host=backend.util.settings.Config().agent_api_host, port=backend.util.settings.Config().agent_api_port, ) @staticmethod async def test_execute_graph( graph_id: str, node_input: dict[typing.Any, typing.Any], user_id: str ): return backend.server.routers.v1.execute_graph(graph_id, node_input, user_id) @staticmethod async def test_create_graph( create_graph: backend.server.routers.v1.CreateGraph, user_id: str, is_template=False, ): return await backend.server.routers.v1.create_new_graph(create_graph, user_id) @staticmethod async def test_get_graph_run_status( graph_id: str, graph_exec_id: str, user_id: str ): return await backend.server.routers.v1.get_graph_run_status( graph_id, graph_exec_id, user_id ) @staticmethod async def test_get_graph_run_node_execution_results( graph_id: str, graph_exec_id: str, user_id: str ): return await backend.server.routers.v1.get_graph_run_node_execution_results( graph_id, graph_exec_id, user_id ) @staticmethod async def test_delete_graph(graph_id: str, user_id: str): return await backend.server.routers.v1.delete_graph(graph_id, user_id) def set_test_dependency_overrides(self, overrides: dict): app.dependency_overrides.update(overrides)
import os import re from pathlib import Path from typing import Optional, Tuple, Union import torch from torch.utils.data import Dataset from torchaudio._internal import download_url_to_file from torchaudio.datasets.utils import _extract_tar, _load_waveform URL = "https://speech.fit.vutbr.cz/files/quesst14Database.tgz" SAMPLE_RATE = 8000 _CHECKSUM = "4f869e06bc066bbe9c5dde31dbd3909a0870d70291110ebbb38878dcbc2fc5e4" _LANGUAGES = [ "albanian", "basque", "czech", "nnenglish", "romanian", "slovak", ] class QUESST14(Dataset): """*QUESST14* :cite:`Mir2015QUESST2014EQ` dataset. Args: root (str or Path): Root directory where the dataset's top level directory is found subset (str): Subset of the dataset to use. Options: [``"docs"``, ``"dev"``, ``"eval"``]. language (str or None, optional): Language to get dataset for. Options: [``None``, ``albanian``, ``basque``, ``czech``, ``nnenglish``, ``romanian``, ``slovak``]. If ``None``, dataset consists of all languages. (default: ``"nnenglish"``) download (bool, optional): Whether to download the dataset if it is not found at root path. (default: ``False``) """ def __init__( self, root: Union[str, Path], subset: str, language: Optional[str] = "nnenglish", download: bool = False, ) -> None: if subset not in ["docs", "dev", "eval"]: raise ValueError("`subset` must be one of ['docs', 'dev', 'eval']") if language is not None and language not in _LANGUAGES: raise ValueError(f"`language` must be None or one of {str(_LANGUAGES)}") # Get string representation of 'root' root = os.fspath(root) basename = os.path.basename(URL) archive = os.path.join(root, basename) basename = basename.rsplit(".", 2)[0] self._path = os.path.join(root, basename) if not os.path.isdir(self._path): if not os.path.isfile(archive): if not download: raise RuntimeError("Dataset not found. Please use `download=True` to download") download_url_to_file(URL, archive, hash_prefix=_CHECKSUM) _extract_tar(archive, root) if subset == "docs": self.data = filter_audio_paths(self._path, language, "language_key_utterances.lst") elif subset == "dev": self.data = filter_audio_paths(self._path, language, "language_key_dev.lst") elif subset == "eval": self.data = filter_audio_paths(self._path, language, "language_key_eval.lst") def get_metadata(self, n: int) -> Tuple[str, int, str]: """Get metadata for the n-th sample from the dataset. Returns filepath instead of waveform, but otherwise returns the same fields as :py:func:`__getitem__`. Args: n (int): The index of the sample to be loaded Returns: Tuple of the following items; str: Path to audio int: Sample rate str: File name """ audio_path = self.data[n] relpath = os.path.relpath(audio_path, self._path) return relpath, SAMPLE_RATE, audio_path.with_suffix("").name def __getitem__(self, n: int) -> Tuple[torch.Tensor, int, str]: """Load the n-th sample from the dataset. Args: n (int): The index of the sample to be loaded Returns: Tuple of the following items; Tensor: Waveform int: Sample rate str: File name """ metadata = self.get_metadata(n) waveform = _load_waveform(self._path, metadata[0], metadata[1]) return (waveform,) + metadata[1:] def __len__(self) -> int: return len(self.data) def filter_audio_paths( path: str, language: str, lst_name: str, ): """Extract audio paths for the given language.""" audio_paths = [] path = Path(path) with open(path / "scoring" / lst_name) as f: for line in f: audio_path, lang = line.strip().split() if language is not None and lang != language: continue audio_path = re.sub(r"^.*?\/", "", audio_path) audio_paths.append(path / audio_path) return audio_paths
import os import re from pathlib import Path from typing import Optional, Tuple, Union import torch from torch.hub import download_url_to_file from torch.utils.data import Dataset from torchaudio.datasets.utils import _extract_tar, _load_waveform URL = "https://speech.fit.vutbr.cz/files/quesst14Database.tgz" SAMPLE_RATE = 8000 _CHECKSUM = "4f869e06bc066bbe9c5dde31dbd3909a0870d70291110ebbb38878dcbc2fc5e4" _LANGUAGES = [ "albanian", "basque", "czech", "nnenglish", "romanian", "slovak", ] class QUESST14(Dataset): """*QUESST14* :cite:`Mir2015QUESST2014EQ` dataset. Args: root (str or Path): Root directory where the dataset's top level directory is found subset (str): Subset of the dataset to use. Options: [``"docs"``, ``"dev"``, ``"eval"``]. language (str or None, optional): Language to get dataset for. Options: [``None``, ``albanian``, ``basque``, ``czech``, ``nnenglish``, ``romanian``, ``slovak``]. If ``None``, dataset consists of all languages. (default: ``"nnenglish"``) download (bool, optional): Whether to download the dataset if it is not found at root path. (default: ``False``) """ def __init__( self, root: Union[str, Path], subset: str, language: Optional[str] = "nnenglish", download: bool = False, ) -> None: if subset not in ["docs", "dev", "eval"]: raise ValueError("`subset` must be one of ['docs', 'dev', 'eval']") if language is not None and language not in _LANGUAGES: raise ValueError(f"`language` must be None or one of {str(_LANGUAGES)}") # Get string representation of 'root' root = os.fspath(root) basename = os.path.basename(URL) archive = os.path.join(root, basename) basename = basename.rsplit(".", 2)[0] self._path = os.path.join(root, basename) if not os.path.isdir(self._path): if not os.path.isfile(archive): if not download: raise RuntimeError("Dataset not found. Please use `download=True` to download") download_url_to_file(URL, archive, hash_prefix=_CHECKSUM) _extract_tar(archive, root) if subset == "docs": self.data = filter_audio_paths(self._path, language, "language_key_utterances.lst") elif subset == "dev": self.data = filter_audio_paths(self._path, language, "language_key_dev.lst") elif subset == "eval": self.data = filter_audio_paths(self._path, language, "language_key_eval.lst") def get_metadata(self, n: int) -> Tuple[str, int, str]: """Get metadata for the n-th sample from the dataset. Returns filepath instead of waveform, but otherwise returns the same fields as :py:func:`__getitem__`. Args: n (int): The index of the sample to be loaded Returns: Tuple of the following items; str: Path to audio int: Sample rate str: File name """ audio_path = self.data[n] relpath = os.path.relpath(audio_path, self._path) return relpath, SAMPLE_RATE, audio_path.with_suffix("").name def __getitem__(self, n: int) -> Tuple[torch.Tensor, int, str]: """Load the n-th sample from the dataset. Args: n (int): The index of the sample to be loaded Returns: Tuple of the following items; Tensor: Waveform int: Sample rate str: File name """ metadata = self.get_metadata(n) waveform = _load_waveform(self._path, metadata[0], metadata[1]) return (waveform,) + metadata[1:] def __len__(self) -> int: return len(self.data) def filter_audio_paths( path: str, language: str, lst_name: str, ): """Extract audio paths for the given language.""" audio_paths = [] path = Path(path) with open(path / "scoring" / lst_name) as f: for line in f: audio_path, lang = line.strip().split() if language is not None and lang != language: continue audio_path = re.sub(r"^.*?\/", "", audio_path) audio_paths.append(path / audio_path) return audio_paths
import asyncio import os import random import string import tempfile import time import pytest from jina import helper @pytest.fixture(scope='function') def random_workspace_name(): """Generate a random workspace name with digits and letters.""" rand = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6)) return f'JINA_TEST_WORKSPACE_{rand}' @pytest.fixture(scope='function') def test_metas(tmpdir, random_workspace_name): from jina.serve.executors.metas import get_default_metas os.environ[random_workspace_name] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ[random_workspace_name] yield metas del os.environ[random_workspace_name] @pytest.fixture() def docker_compose(request): os.system( f"docker-compose -f {request.param} --project-directory . up --build -d --remove-orphans" ) time.sleep(10) yield os.system( f"docker-compose -f {request.param} --project-directory . down --remove-orphans" ) @pytest.fixture(scope='function') def port_generator(): generated_ports = set() def random_port(): port = helper.random_port() while port in generated_ports: port = helper.random_port() generated_ports.add(port) return port return random_port @pytest.fixture(autouse=True) def test_log_level(monkeypatch): monkeypatch.setenv('JINA_LOG_LEVEL', 'DEBUG') @pytest.fixture(autouse=True) def test_grpc_fork_support_false(monkeypatch): monkeypatch.setenv('GRPC_ENABLE_FORK_SUPPORT', 'true') @pytest.fixture(autouse=True) def test_timeout_ctrl_time(monkeypatch): monkeypatch.setenv('JINA_DEFAULT_TIMEOUT_CTRL', '500') @pytest.fixture(autouse=True) def test_disable_telemetry(monkeypatch): monkeypatch.setenv('JINA_OPTOUT_TELEMETRY', 'True') @pytest.fixture(autouse=True) def tmpfile(tmpdir): tmpfile = f'jina_test_{next(tempfile._get_candidate_names())}.db' return tmpdir / tmpfile @pytest.fixture(scope='session') def event_loop(request): """ Valid only for `pytest.mark.asyncio` tests """ loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close() @pytest.fixture(autouse=True) def set_test_pip_version() -> None: os.environ['JINA_GATEWAY_IMAGE'] = 'jinaai/jina:test-pip' yield if 'JINA_GATEWAY_IMAGE' in os.environ: # maybe another fixture has already removed del os.environ['JINA_GATEWAY_IMAGE']
import asyncio import os import random import string import tempfile import time import pytest from jina import helper @pytest.fixture(scope='function') def random_workspace_name(): """Generate a random workspace name with digits and letters.""" rand = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6)) return f'JINA_TEST_WORKSPACE_{rand}' @pytest.fixture(scope='function') def test_metas(tmpdir, random_workspace_name): from jina.serve.executors.metas import get_default_metas os.environ[random_workspace_name] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ[random_workspace_name] yield metas del os.environ[random_workspace_name] @pytest.fixture() def docker_compose(request): os.system( f"docker-compose -f {request.param} --project-directory . up --build -d --remove-orphans" ) time.sleep(10) yield os.system( f"docker-compose -f {request.param} --project-directory . down --remove-orphans" ) @pytest.fixture(scope='function') def port_generator(): generated_ports = set() def random_port(): port = helper.random_port() while port in generated_ports: port = helper.random_port() generated_ports.add(port) return port return random_port @pytest.fixture(autouse=True) def test_log_level(monkeypatch): monkeypatch.setenv('JINA_LOG_LEVEL', 'DEBUG') @pytest.fixture(autouse=True) def test_grpc_fork_support_false(monkeypatch): monkeypatch.setenv('GRPC_ENABLE_FORK_SUPPORT', 'true') @pytest.fixture(autouse=True) def test_timeout_ctrl_time(monkeypatch): monkeypatch.setenv('JINA_DEFAULT_TIMEOUT_CTRL', '500') @pytest.fixture(autouse=True) def test_disable_telemetry(monkeypatch): monkeypatch.setenv('JINA_OPTOUT_TELEMETRY', 'True') @pytest.fixture(autouse=True) def tmpfile(tmpdir): tmpfile = f'jina_test_{next(tempfile._get_candidate_names())}.db' return tmpdir / tmpfile @pytest.fixture(scope='session') def event_loop(request): """ Valid only for `pytest.mark.asyncio` tests """ loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close()
_base_ = [ '../_base_/models/mask_rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] norm_cfg = dict(type='BN', requires_grad=True) model = dict( backbone=dict(norm_cfg=norm_cfg, norm_eval=False), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, norm_cfg=norm_cfg, num_outs=5), roi_head=dict( bbox_head=dict(norm_cfg=norm_cfg), mask_head=dict(norm_cfg=norm_cfg))) dataset_type = 'CocoDataset' data_root = 'data/coco/' 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, with_mask=True), dict( type='Resize', img_scale=(640, 640), ratio_range=(0.8, 1.2), keep_ratio=True), dict(type='RandomCrop', crop_size=(640, 640)), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size=(640, 640)), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(640, 640), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=64), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=8, workers_per_gpu=4, train=dict(pipeline=train_pipeline), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # learning policy optimizer = dict( type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001, paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True)) optimizer_config = dict(grad_clip=None) # learning policy lr_config = dict( policy='step', warmup='linear', warmup_iters=1000, warmup_ratio=0.1, step=[30, 40]) # runtime settings runner = dict(max_epochs=50) evaluation = dict(interval=2) # 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/mask_rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] norm_cfg = dict(type='BN', requires_grad=True) model = dict( backbone=dict(norm_cfg=norm_cfg, norm_eval=False), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, norm_cfg=norm_cfg, num_outs=5), roi_head=dict( bbox_head=dict(norm_cfg=norm_cfg), mask_head=dict(norm_cfg=norm_cfg))) dataset_type = 'CocoDataset' data_root = 'data/coco/' 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, with_mask=True), dict( type='Resize', img_scale=(640, 640), ratio_range=(0.8, 1.2), keep_ratio=True), dict(type='RandomCrop', crop_size=(640, 640)), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size=(640, 640)), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(640, 640), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=64), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=8, workers_per_gpu=4, train=dict(pipeline=train_pipeline), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # learning policy optimizer = dict( type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001, paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True)) optimizer_config = dict(grad_clip=None) # learning policy lr_config = dict( policy='step', warmup='linear', warmup_iters=1000, warmup_ratio=0.1, step=[30, 40]) # runtime settings runner = dict(max_epochs=50) evaluation = dict(interval=2)
from docarray import BaseDocument, DocumentArray from docarray.document import AnyDocument def test_generic_init(): class Text(BaseDocument): text: str da = DocumentArray[Text]([]) da.document_type == Text assert isinstance(da, DocumentArray) def test_normal_access_init(): da = DocumentArray([]) da.document_type == AnyDocument assert isinstance(da, DocumentArray)
from docarray import DocumentArray, Document from docarray.document import AnyDocument def test_generic_init(): class Text(Document): text: str da = DocumentArray[Text]([]) da.document_type == Text assert isinstance(da, DocumentArray) def test_normal_access_init(): da = DocumentArray([]) da.document_type == AnyDocument assert isinstance(da, DocumentArray)
""" 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, ) def _ignore_google_warnings(): import warnings warnings.filterwarnings( 'ignore', category=DeprecationWarning, message='Deprecated call to `pkg_resources.declare_namespace(\'google\')`.', append=True ) _warnings.formatwarning = _warning_on_one_line _warnings.simplefilter('always', DeprecationWarning, append=True) _ignore_google_warnings() # 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 try: _set_start_method('fork') _warnings.warn(f'multiprocessing start method is set to `fork`') except Exception as e: _warnings.warn(f'failed to set multiprocessing start_method to `fork`: {e!r}') # 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.23.0' # do not change this line manually # this is managed by proto/build-proto.sh and updated on every execution __proto_version__ = '0.1.27' 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.runtimes.gateway.gateway import 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, ) def _ignore_google_warnings(): import warnings warnings.filterwarnings( 'ignore', category=DeprecationWarning, message='Deprecated call to `pkg_resources.declare_namespace(\'google\')`.', append=True ) _warnings.formatwarning = _warning_on_one_line _warnings.simplefilter('always', DeprecationWarning, append=True) _ignore_google_warnings() # 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 try: _set_start_method('fork') _warnings.warn(f'multiprocessing start method is set to `fork`') except Exception as e: _warnings.warn(f'failed to set multiprocessing start_method to `fork`: {e!r}') # 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.22.5' # do not change this line manually # this is managed by proto/build-proto.sh and updated on every execution __proto_version__ = '0.1.27' 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.runtimes.gateway.gateway import Gateway
import logging import random from datasets import load_dataset from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseInformationRetrievalEvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load the NFcorpus IR dataset (https://huggingface.co/datasets/BeIR/nfcorpus, https://huggingface.co/datasets/BeIR/nfcorpus-qrels) corpus = load_dataset("BeIR/nfcorpus", "corpus", split="corpus") queries = load_dataset("BeIR/nfcorpus", "queries", split="queries") relevant_docs_data = load_dataset("BeIR/nfcorpus-qrels", split="test") # For this dataset, we want to concatenate the title and texts for the corpus corpus = corpus.map(lambda x: {"text": x["title"] + " " + x["text"]}, remove_columns=["title"]) # Shrink the corpus size heavily to only the relevant documents + 1,000 random documents required_corpus_ids = set(map(str, relevant_docs_data["corpus-id"])) required_corpus_ids |= set(random.sample(corpus["_id"], k=1000)) corpus = corpus.filter(lambda x: x["_id"] in required_corpus_ids) # Convert the datasets to dictionaries corpus = dict(zip(corpus["_id"], corpus["text"])) # Our corpus (cid => document) queries = dict(zip(queries["_id"], queries["text"])) # Our queries (qid => question) relevant_docs = {} # Query ID to relevant documents (qid => set([relevant_cids]) for qid, corpus_ids in zip(relevant_docs_data["query-id"], relevant_docs_data["corpus-id"]): qid = str(qid) corpus_ids = str(corpus_ids) if qid not in relevant_docs: relevant_docs[qid] = set() relevant_docs[qid].add(corpus_ids) # Given queries, a corpus and a mapping with relevant documents, the SparseInformationRetrievalEvaluator computes different IR metrics. ir_evaluator = SparseInformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="BeIR-nfcorpus-subset-test", show_progress_bar=True, batch_size=16, ) # Run evaluation results = ir_evaluator(model) """ Query info: num_rows: 323, num_cols: 30522, row_non_zero_mean: 42.891639709472656, row_sparsity_mean: 0.9985947012901306 Corpus info: num_rows: 3270, num_cols: 30522, row_non_zero_mean: 206.98899841308594, row_sparsity_mean: 0.9932184219360352 Score-Function: dot Accuracy@1: 50.46% Accuracy@3: 64.09% Accuracy@5: 67.49% Accuracy@10: 72.14% Precision@1: 50.46% Precision@3: 40.76% Precision@5: 34.06% Precision@10: 25.98% Recall@1: 6.09% Recall@3: 11.73% Recall@5: 13.64% Recall@10: 17.21% MRR@10: 0.5796 NDCG@10: 0.3613 MAP@100: 0.1827 Primary metric value: 0.3613 """ # Print the results print(f"Primary metric: {ir_evaluator.primary_metric}") # => Primary metric: BeIR-nfcorpus-subset-test_dot_ndcg@10 print(f"Primary metric value: {results[ir_evaluator.primary_metric]:.4f}") # => Primary metric value: 0.3613
import logging import random from datasets import load_dataset from sentence_transformers.sparse_encoder import ( SparseEncoder, SparseInformationRetrievalEvaluator, ) logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load the NFcorpus IR dataset (https://huggingface.co/datasets/BeIR/nfcorpus, https://huggingface.co/datasets/BeIR/nfcorpus-qrels) corpus = load_dataset("BeIR/nfcorpus", "corpus", split="corpus") queries = load_dataset("BeIR/nfcorpus", "queries", split="queries") relevant_docs_data = load_dataset("BeIR/nfcorpus-qrels", split="test") # For this dataset, we want to concatenate the title and texts for the corpus corpus = corpus.map(lambda x: {"text": x["title"] + " " + x["text"]}, remove_columns=["title"]) # Shrink the corpus size heavily to only the relevant documents + 1,000 random documents required_corpus_ids = set(map(str, relevant_docs_data["corpus-id"])) required_corpus_ids |= set(random.sample(corpus["_id"], k=1000)) corpus = corpus.filter(lambda x: x["_id"] in required_corpus_ids) # Convert the datasets to dictionaries corpus = dict(zip(corpus["_id"], corpus["text"])) # Our corpus (cid => document) queries = dict(zip(queries["_id"], queries["text"])) # Our queries (qid => question) relevant_docs = {} # Query ID to relevant documents (qid => set([relevant_cids]) for qid, corpus_ids in zip(relevant_docs_data["query-id"], relevant_docs_data["corpus-id"]): qid = str(qid) corpus_ids = str(corpus_ids) if qid not in relevant_docs: relevant_docs[qid] = set() relevant_docs[qid].add(corpus_ids) # Given queries, a corpus and a mapping with relevant documents, the SparseInformationRetrievalEvaluator computes different IR metrics. ir_evaluator = SparseInformationRetrievalEvaluator( queries=queries, corpus=corpus, relevant_docs=relevant_docs, name="BeIR-nfcorpus-subset-test", show_progress_bar=True, batch_size=16, ) # Run evaluation results = ir_evaluator(model) """ Query info: num_rows: 323, num_cols: 30522, row_non_zero_mean: 42.891639709472656, row_sparsity_mean: 0.9985947012901306 Corpus info: num_rows: 3270, num_cols: 30522, row_non_zero_mean: 206.98899841308594, row_sparsity_mean: 0.9932184219360352 Score-Function: dot Accuracy@1: 50.46% Accuracy@3: 64.09% Accuracy@5: 67.49% Accuracy@10: 72.14% Precision@1: 50.46% Precision@3: 40.76% Precision@5: 34.06% Precision@10: 25.98% Recall@1: 6.09% Recall@3: 11.73% Recall@5: 13.64% Recall@10: 17.21% MRR@10: 0.5796 NDCG@10: 0.3613 MAP@100: 0.1827 Primary metric value: 0.3613 """ # Print the results print(f"Primary metric: {ir_evaluator.primary_metric}") # => Primary metric: BeIR-nfcorpus-subset-test_dot_ndcg@10 print(f"Primary metric value: {results[ir_evaluator.primary_metric]:.4f}") # => Primary metric value: 0.3613
import datetime import autogpt_libs.auth as autogpt_auth_lib import fastapi.testclient import pytest import pytest_mock import backend.server.model as server_model import backend.server.v2.library.model as library_model from backend.server.v2.library.routes import router as library_router app = fastapi.FastAPI() app.include_router(library_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_auth_lib.auth_middleware] = override_auth_middleware app.dependency_overrides[autogpt_auth_lib.depends.get_user_id] = override_get_user_id @pytest.mark.asyncio async def test_get_library_agents_success(mocker: pytest_mock.MockFixture): mocked_value = library_model.LibraryAgentResponse( agents=[ library_model.LibraryAgent( id="test-agent-1", graph_id="test-agent-1", graph_version=1, name="Test Agent 1", description="Test Description 1", image_url=None, creator_name="Test Creator", creator_image_url="", input_schema={"type": "object", "properties": {}}, status=library_model.LibraryAgentStatus.COMPLETED, new_output=False, can_access_graph=True, is_latest_version=True, updated_at=datetime.datetime(2023, 1, 1, 0, 0, 0), ), library_model.LibraryAgent( id="test-agent-2", graph_id="test-agent-2", graph_version=1, name="Test Agent 2", description="Test Description 2", image_url=None, creator_name="Test Creator", creator_image_url="", input_schema={"type": "object", "properties": {}}, status=library_model.LibraryAgentStatus.COMPLETED, new_output=False, can_access_graph=False, is_latest_version=True, updated_at=datetime.datetime(2023, 1, 1, 0, 0, 0), ), ], pagination=server_model.Pagination( total_items=2, total_pages=1, current_page=1, page_size=50 ), ) mock_db_call = mocker.patch("backend.server.v2.library.db.list_library_agents") mock_db_call.return_value = mocked_value response = client.get("/agents?search_term=test") assert response.status_code == 200 data = library_model.LibraryAgentResponse.model_validate(response.json()) assert len(data.agents) == 2 assert data.agents[0].graph_id == "test-agent-1" assert data.agents[0].can_access_graph is True assert data.agents[1].graph_id == "test-agent-2" assert data.agents[1].can_access_graph is False mock_db_call.assert_called_once_with( user_id="test-user-id", search_term="test", sort_by=library_model.LibraryAgentSort.UPDATED_AT, page=1, page_size=15, ) def test_get_library_agents_error(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.list_library_agents") mock_db_call.side_effect = Exception("Test error") response = client.get("/agents?search_term=test") assert response.status_code == 500 mock_db_call.assert_called_once_with( user_id="test-user-id", search_term="test", sort_by=library_model.LibraryAgentSort.UPDATED_AT, page=1, page_size=15, ) @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 datetime import autogpt_libs.auth as autogpt_auth_lib import fastapi.testclient import pytest import pytest_mock import backend.server.model as server_model import backend.server.v2.library.model as library_model from backend.server.v2.library.routes import router as library_router app = fastapi.FastAPI() app.include_router(library_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_auth_lib.auth_middleware] = override_auth_middleware app.dependency_overrides[autogpt_auth_lib.depends.get_user_id] = override_get_user_id @pytest.mark.asyncio async def test_get_library_agents_success(mocker: pytest_mock.MockFixture): mocked_value = library_model.LibraryAgentResponse( agents=[ library_model.LibraryAgent( id="test-agent-1", agent_id="test-agent-1", agent_version=1, name="Test Agent 1", description="Test Description 1", image_url=None, creator_name="Test Creator", creator_image_url="", input_schema={"type": "object", "properties": {}}, status=library_model.LibraryAgentStatus.COMPLETED, new_output=False, can_access_graph=True, is_latest_version=True, updated_at=datetime.datetime(2023, 1, 1, 0, 0, 0), ), library_model.LibraryAgent( id="test-agent-2", agent_id="test-agent-2", agent_version=1, name="Test Agent 2", description="Test Description 2", image_url=None, creator_name="Test Creator", creator_image_url="", input_schema={"type": "object", "properties": {}}, status=library_model.LibraryAgentStatus.COMPLETED, new_output=False, can_access_graph=False, is_latest_version=True, updated_at=datetime.datetime(2023, 1, 1, 0, 0, 0), ), ], pagination=server_model.Pagination( total_items=2, total_pages=1, current_page=1, page_size=50 ), ) mock_db_call = mocker.patch("backend.server.v2.library.db.list_library_agents") mock_db_call.return_value = mocked_value response = client.get("/agents?search_term=test") assert response.status_code == 200 data = library_model.LibraryAgentResponse.model_validate(response.json()) assert len(data.agents) == 2 assert data.agents[0].agent_id == "test-agent-1" assert data.agents[0].can_access_graph is True assert data.agents[1].agent_id == "test-agent-2" assert data.agents[1].can_access_graph is False mock_db_call.assert_called_once_with( user_id="test-user-id", search_term="test", sort_by=library_model.LibraryAgentSort.UPDATED_AT, page=1, page_size=15, ) def test_get_library_agents_error(mocker: pytest_mock.MockFixture): mock_db_call = mocker.patch("backend.server.v2.library.db.list_library_agents") mock_db_call.side_effect = Exception("Test error") response = client.get("/agents?search_term=test") assert response.status_code == 500 mock_db_call.assert_called_once_with( user_id="test-user-id", search_term="test", sort_by=library_model.LibraryAgentSort.UPDATED_AT, page=1, page_size=15, ) @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" )
"""Use a single chain to route an input to one of multiple retrieval qa chains.""" from __future__ import annotations from collections.abc import Mapping from typing import Any, Optional from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import PromptTemplate from langchain_core.retrievers import BaseRetriever from langchain.chains import ConversationChain from langchain.chains.base import Chain from langchain.chains.conversation.prompt import DEFAULT_TEMPLATE from langchain.chains.retrieval_qa.base import BaseRetrievalQA, RetrievalQA from langchain.chains.router.base import MultiRouteChain from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser from langchain.chains.router.multi_retrieval_prompt import ( MULTI_RETRIEVAL_ROUTER_TEMPLATE, ) class MultiRetrievalQAChain(MultiRouteChain): # type: ignore[override] """A multi-route chain that uses an LLM router chain to choose amongst retrieval qa chains.""" router_chain: LLMRouterChain """Chain for deciding a destination chain and the input to it.""" destination_chains: Mapping[str, BaseRetrievalQA] """Map of name to candidate chains that inputs can be routed to.""" default_chain: Chain """Default chain to use when router doesn't map input to one of the destinations.""" @property def output_keys(self) -> list[str]: return ["result"] @classmethod def from_retrievers( cls, llm: BaseLanguageModel, retriever_infos: list[dict[str, Any]], default_retriever: Optional[BaseRetriever] = None, default_prompt: Optional[PromptTemplate] = None, default_chain: Optional[Chain] = None, *, default_chain_llm: Optional[BaseLanguageModel] = None, **kwargs: Any, ) -> MultiRetrievalQAChain: if default_prompt and not default_retriever: raise ValueError( "`default_retriever` must be specified if `default_prompt` is " "provided. Received only `default_prompt`." ) destinations = [f"{r['name']}: {r['description']}" for r in retriever_infos] destinations_str = "\n".join(destinations) router_template = MULTI_RETRIEVAL_ROUTER_TEMPLATE.format( destinations=destinations_str ) router_prompt = PromptTemplate( template=router_template, input_variables=["input"], output_parser=RouterOutputParser(next_inputs_inner_key="query"), ) router_chain = LLMRouterChain.from_llm(llm, router_prompt) destination_chains = {} for r_info in retriever_infos: prompt = r_info.get("prompt") retriever = r_info["retriever"] chain = RetrievalQA.from_llm(llm, prompt=prompt, retriever=retriever) name = r_info["name"] destination_chains[name] = chain if default_chain: _default_chain = default_chain elif default_retriever: _default_chain = RetrievalQA.from_llm( llm, prompt=default_prompt, retriever=default_retriever ) else: prompt_template = DEFAULT_TEMPLATE.replace("input", "query") prompt = PromptTemplate( template=prompt_template, input_variables=["history", "query"] ) if default_chain_llm is None: raise NotImplementedError( "conversation_llm must be provided if default_chain is not " "specified. This API has been changed to avoid instantiating " "default LLMs on behalf of users." "You can provide a conversation LLM like so:\n" "from langchain_openai import ChatOpenAI\n" "llm = ChatOpenAI()" ) _default_chain = ConversationChain( llm=default_chain_llm, prompt=prompt, input_key="query", output_key="result", ) return cls( router_chain=router_chain, destination_chains=destination_chains, default_chain=_default_chain, **kwargs, )
"""Use a single chain to route an input to one of multiple retrieval qa chains.""" from __future__ import annotations from typing import Any, Dict, List, Mapping, Optional from langchain_core.language_models import BaseLanguageModel from langchain_core.prompts import PromptTemplate from langchain_core.retrievers import BaseRetriever from langchain.chains import ConversationChain from langchain.chains.base import Chain from langchain.chains.conversation.prompt import DEFAULT_TEMPLATE from langchain.chains.retrieval_qa.base import BaseRetrievalQA, RetrievalQA from langchain.chains.router.base import MultiRouteChain from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser from langchain.chains.router.multi_retrieval_prompt import ( MULTI_RETRIEVAL_ROUTER_TEMPLATE, ) class MultiRetrievalQAChain(MultiRouteChain): # type: ignore[override] """A multi-route chain that uses an LLM router chain to choose amongst retrieval qa chains.""" router_chain: LLMRouterChain """Chain for deciding a destination chain and the input to it.""" destination_chains: Mapping[str, BaseRetrievalQA] """Map of name to candidate chains that inputs can be routed to.""" default_chain: Chain """Default chain to use when router doesn't map input to one of the destinations.""" @property def output_keys(self) -> List[str]: return ["result"] @classmethod def from_retrievers( cls, llm: BaseLanguageModel, retriever_infos: List[Dict[str, Any]], default_retriever: Optional[BaseRetriever] = None, default_prompt: Optional[PromptTemplate] = None, default_chain: Optional[Chain] = None, *, default_chain_llm: Optional[BaseLanguageModel] = None, **kwargs: Any, ) -> MultiRetrievalQAChain: if default_prompt and not default_retriever: raise ValueError( "`default_retriever` must be specified if `default_prompt` is " "provided. Received only `default_prompt`." ) destinations = [f"{r['name']}: {r['description']}" for r in retriever_infos] destinations_str = "\n".join(destinations) router_template = MULTI_RETRIEVAL_ROUTER_TEMPLATE.format( destinations=destinations_str ) router_prompt = PromptTemplate( template=router_template, input_variables=["input"], output_parser=RouterOutputParser(next_inputs_inner_key="query"), ) router_chain = LLMRouterChain.from_llm(llm, router_prompt) destination_chains = {} for r_info in retriever_infos: prompt = r_info.get("prompt") retriever = r_info["retriever"] chain = RetrievalQA.from_llm(llm, prompt=prompt, retriever=retriever) name = r_info["name"] destination_chains[name] = chain if default_chain: _default_chain = default_chain elif default_retriever: _default_chain = RetrievalQA.from_llm( llm, prompt=default_prompt, retriever=default_retriever ) else: prompt_template = DEFAULT_TEMPLATE.replace("input", "query") prompt = PromptTemplate( template=prompt_template, input_variables=["history", "query"] ) if default_chain_llm is None: raise NotImplementedError( "conversation_llm must be provided if default_chain is not " "specified. This API has been changed to avoid instantiating " "default LLMs on behalf of users." "You can provide a conversation LLM like so:\n" "from langchain_openai import ChatOpenAI\n" "llm = ChatOpenAI()" ) _default_chain = ConversationChain( llm=default_chain_llm, prompt=prompt, input_key="query", output_key="result", ) return cls( router_chain=router_chain, destination_chains=destination_chains, default_chain=_default_chain, **kwargs, )
_base_ = [ '../_base_/models/cascade-mask-rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict( type='CascadeRCNN', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), rpn_head=dict( anchor_generator=dict(type='LegacyAnchorGenerator', center_offset=0.5), bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0])), roi_head=dict( bbox_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict( type='RoIAlign', output_size=7, sampling_ratio=2, aligned=False)), bbox_head=[ dict( type='Shared2FCBBoxHead', reg_class_agnostic=True, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.1, 0.1, 0.2, 0.2])), dict( type='Shared2FCBBoxHead', reg_class_agnostic=True, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.05, 0.05, 0.1, 0.1])), dict( type='Shared2FCBBoxHead', reg_class_agnostic=True, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.033, 0.033, 0.067, 0.067])), ], mask_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict( type='RoIAlign', output_size=14, sampling_ratio=2, aligned=False)))) dist_params = dict(backend='nccl', port=29515)
_base_ = [ '../_base_/models/cascade_mask_rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict( type='CascadeRCNN', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), rpn_head=dict( anchor_generator=dict(type='LegacyAnchorGenerator', center_offset=0.5), bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0])), roi_head=dict( bbox_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict( type='RoIAlign', output_size=7, sampling_ratio=2, aligned=False)), bbox_head=[ dict( type='Shared2FCBBoxHead', reg_class_agnostic=True, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.1, 0.1, 0.2, 0.2])), dict( type='Shared2FCBBoxHead', reg_class_agnostic=True, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.05, 0.05, 0.1, 0.1])), dict( type='Shared2FCBBoxHead', reg_class_agnostic=True, in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='LegacyDeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.033, 0.033, 0.067, 0.067])), ], mask_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict( type='RoIAlign', output_size=14, sampling_ratio=2, aligned=False)))) dist_params = dict(backend='nccl', port=29515)
from __future__ import annotations from typing import Any from langchain_core._api import deprecated from langchain_core.caches import BaseCache as BaseCache # For model_rebuild from langchain_core.callbacks import Callbacks as Callbacks # For model_rebuild from langchain_core.chat_history import BaseChatMessageHistory from langchain_core.language_models import BaseLanguageModel from langchain_core.messages import BaseMessage, SystemMessage, get_buffer_string from langchain_core.prompts import BasePromptTemplate from langchain_core.utils import pre_init from pydantic import BaseModel from langchain.chains.llm import LLMChain from langchain.memory.chat_memory import BaseChatMemory from langchain.memory.prompt import SUMMARY_PROMPT @deprecated( since="0.2.12", removal="1.0", message=( "Refer here for how to incorporate summaries of conversation history: " "https://langchain-ai.github.io/langgraph/how-tos/memory/add-summary-conversation-history/" # noqa: E501 ), ) class SummarizerMixin(BaseModel): """Mixin for summarizer.""" human_prefix: str = "Human" ai_prefix: str = "AI" llm: BaseLanguageModel prompt: BasePromptTemplate = SUMMARY_PROMPT summary_message_cls: type[BaseMessage] = SystemMessage def predict_new_summary( self, messages: list[BaseMessage], existing_summary: str ) -> str: new_lines = get_buffer_string( messages, human_prefix=self.human_prefix, ai_prefix=self.ai_prefix, ) chain = LLMChain(llm=self.llm, prompt=self.prompt) return chain.predict(summary=existing_summary, new_lines=new_lines) async def apredict_new_summary( self, messages: list[BaseMessage], existing_summary: str ) -> str: new_lines = get_buffer_string( messages, human_prefix=self.human_prefix, ai_prefix=self.ai_prefix, ) chain = LLMChain(llm=self.llm, prompt=self.prompt) return await chain.apredict(summary=existing_summary, new_lines=new_lines) @deprecated( since="0.3.1", removal="1.0.0", message=( "Please see the migration guide at: " "https://python.langchain.com/docs/versions/migrating_memory/" ), ) class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin): """Continually summarizes the conversation history. The summary is updated after each conversation turn. The implementations returns a summary of the conversation history which can be used to provide context to the model. """ buffer: str = "" memory_key: str = "history" #: :meta private: @classmethod def from_messages( cls, llm: BaseLanguageModel, chat_memory: BaseChatMessageHistory, *, summarize_step: int = 2, **kwargs: Any, ) -> ConversationSummaryMemory: obj = cls(llm=llm, chat_memory=chat_memory, **kwargs) for i in range(0, len(obj.chat_memory.messages), summarize_step): obj.buffer = obj.predict_new_summary( obj.chat_memory.messages[i : i + summarize_step], obj.buffer ) return obj @property def memory_variables(self) -> list[str]: """Will always return list of memory variables. :meta private: """ return [self.memory_key] def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]: """Return history buffer.""" if self.return_messages: buffer: Any = [self.summary_message_cls(content=self.buffer)] else: buffer = self.buffer return {self.memory_key: buffer} @pre_init def validate_prompt_input_variables(cls, values: dict) -> dict: """Validate that prompt input variables are consistent.""" prompt_variables = values["prompt"].input_variables expected_keys = {"summary", "new_lines"} if expected_keys != set(prompt_variables): msg = ( "Got unexpected prompt input variables. The prompt expects " f"{prompt_variables}, but it should have {expected_keys}." ) raise ValueError(msg) return values def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None: """Save context from this conversation to buffer.""" super().save_context(inputs, outputs) self.buffer = self.predict_new_summary( self.chat_memory.messages[-2:], self.buffer ) def clear(self) -> None: """Clear memory contents.""" super().clear() self.buffer = "" ConversationSummaryMemory.model_rebuild()
from __future__ import annotations from typing import Any from langchain_core._api import deprecated from langchain_core.caches import BaseCache as BaseCache # For model_rebuild from langchain_core.callbacks import Callbacks as Callbacks # For model_rebuild from langchain_core.chat_history import BaseChatMessageHistory from langchain_core.language_models import BaseLanguageModel from langchain_core.messages import BaseMessage, SystemMessage, get_buffer_string from langchain_core.prompts import BasePromptTemplate from langchain_core.utils import pre_init from pydantic import BaseModel from langchain.chains.llm import LLMChain from langchain.memory.chat_memory import BaseChatMemory from langchain.memory.prompt import SUMMARY_PROMPT @deprecated( since="0.2.12", removal="1.0", message=( "Refer here for how to incorporate summaries of conversation history: " "https://langchain-ai.github.io/langgraph/how-tos/memory/add-summary-conversation-history/" # noqa: E501 ), ) class SummarizerMixin(BaseModel): """Mixin for summarizer.""" human_prefix: str = "Human" ai_prefix: str = "AI" llm: BaseLanguageModel prompt: BasePromptTemplate = SUMMARY_PROMPT summary_message_cls: type[BaseMessage] = SystemMessage def predict_new_summary( self, messages: list[BaseMessage], existing_summary: str ) -> str: new_lines = get_buffer_string( messages, human_prefix=self.human_prefix, ai_prefix=self.ai_prefix, ) chain = LLMChain(llm=self.llm, prompt=self.prompt) return chain.predict(summary=existing_summary, new_lines=new_lines) async def apredict_new_summary( self, messages: list[BaseMessage], existing_summary: str ) -> str: new_lines = get_buffer_string( messages, human_prefix=self.human_prefix, ai_prefix=self.ai_prefix, ) chain = LLMChain(llm=self.llm, prompt=self.prompt) return await chain.apredict(summary=existing_summary, new_lines=new_lines) @deprecated( since="0.3.1", removal="1.0.0", message=( "Please see the migration guide at: " "https://python.langchain.com/docs/versions/migrating_memory/" ), ) class ConversationSummaryMemory(BaseChatMemory, SummarizerMixin): """Continually summarizes the conversation history. The summary is updated after each conversation turn. The implementations returns a summary of the conversation history which can be used to provide context to the model. """ buffer: str = "" memory_key: str = "history" #: :meta private: @classmethod def from_messages( cls, llm: BaseLanguageModel, chat_memory: BaseChatMessageHistory, *, summarize_step: int = 2, **kwargs: Any, ) -> ConversationSummaryMemory: obj = cls(llm=llm, chat_memory=chat_memory, **kwargs) for i in range(0, len(obj.chat_memory.messages), summarize_step): obj.buffer = obj.predict_new_summary( obj.chat_memory.messages[i : i + summarize_step], obj.buffer ) return obj @property def memory_variables(self) -> list[str]: """Will always return list of memory variables. :meta private: """ return [self.memory_key] def load_memory_variables(self, inputs: dict[str, Any]) -> dict[str, Any]: """Return history buffer.""" if self.return_messages: buffer: Any = [self.summary_message_cls(content=self.buffer)] else: buffer = self.buffer return {self.memory_key: buffer} @pre_init def validate_prompt_input_variables(cls, values: dict) -> dict: """Validate that prompt input variables are consistent.""" prompt_variables = values["prompt"].input_variables expected_keys = {"summary", "new_lines"} if expected_keys != set(prompt_variables): raise ValueError( "Got unexpected prompt input variables. The prompt expects " f"{prompt_variables}, but it should have {expected_keys}." ) return values def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None: """Save context from this conversation to buffer.""" super().save_context(inputs, outputs) self.buffer = self.predict_new_summary( self.chat_memory.messages[-2:], self.buffer ) def clear(self) -> None: """Clear memory contents.""" super().clear() self.buffer = "" ConversationSummaryMemory.model_rebuild()
""" 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, ) def _ignore_google_warnings(): import warnings warnings.filterwarnings( 'ignore', category=DeprecationWarning, message='Deprecated call to `pkg_resources.declare_namespace(\'google\')`.', append=True ) _warnings.formatwarning = _warning_on_one_line _warnings.simplefilter('always', DeprecationWarning, append=True) _ignore_google_warnings() # 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 try: _set_start_method('fork') _warnings.warn(f'multiprocessing start method is set to `fork`') except Exception as e: _warnings.warn(f'failed to set multiprocessing start_method to `fork`: {e!r}') # 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.20.4' # do not change this line manually # this is managed by proto/build-proto.sh and updated on every execution __proto_version__ = '0.1.27' 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.runtimes.gateway.gateway import 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, ) def _ignore_google_warnings(): import warnings warnings.filterwarnings( 'ignore', category=DeprecationWarning, message='Deprecated call to `pkg_resources.declare_namespace(\'google\')`.', append=True ) _warnings.formatwarning = _warning_on_one_line _warnings.simplefilter('always', DeprecationWarning, append=True) _ignore_google_warnings() # 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 try: _set_start_method('fork') _warnings.warn(f'multiprocessing start method is set to `fork`') except Exception as e: _warnings.warn(f'failed to set multiprocessing start_method to `fork`: {e!r}') # 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.20.3' # do not change this line manually # this is managed by proto/build-proto.sh and updated on every execution __proto_version__ = '0.1.27' 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.runtimes.gateway.gateway import Gateway
""" This examples trains BERT (or any other transformer model like RoBERTa, DistilBERT etc.) for the STSbenchmark from scratch. It uses MatryoshkaLoss with the powerful CoSENTLoss to train models that perform well at output dimensions [768, 512, 256, 128, 64]. It generates sentence embeddings that can be compared using cosine-similarity to measure the similarity. Usage: python 2d_matryoshka_sts.py OR python 2d_matryoshka_sts.py pretrained_transformer_model_name """ import logging import sys import traceback from datetime import datetime from datasets import load_dataset from sentence_transformers import ( SentenceTransformer, SentenceTransformerTrainer, SentenceTransformerTrainingArguments, losses, ) from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator, SimilarityFunction # Set the log level to INFO to get more information logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO) model_name = sys.argv[1] if len(sys.argv) > 1 else "distilbert-base-uncased" batch_size = 16 num_train_epochs = 4 # Save path of the model output_dir = f"output/2d_matryoshka_sts_{model_name.replace('/', '-')}-{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}" # 1. Here we define our SentenceTransformer model. If not already a Sentence Transformer model, it will automatically # create one with "mean" pooling. model = SentenceTransformer(model_name) # If we want, we can limit the maximum sequence length for the model # model.max_seq_length = 75 logging.info(model) # 2. Load the STSB dataset: https://huggingface.co/datasets/sentence-transformers/stsb train_dataset = load_dataset("sentence-transformers/stsb", split="train") eval_dataset = load_dataset("sentence-transformers/stsb", split="validation") test_dataset = load_dataset("sentence-transformers/stsb", split="test") logging.info(train_dataset) # 3. Define our training loss # CoSENTLoss (https://sbert.net/docs/package_reference/sentence_transformer/losses.html#cosentloss) needs two text columns and one # similarity score column (between 0 and 1) inner_train_loss = losses.CoSENTLoss(model=model) train_loss = losses.Matryoshka2dLoss(model, inner_train_loss, [768, 512, 256, 128, 64]) # 4. Define an evaluator for use during training. This is useful to keep track of alongside the evaluation loss. dev_evaluator = EmbeddingSimilarityEvaluator( sentences1=eval_dataset["sentence1"], sentences2=eval_dataset["sentence2"], scores=eval_dataset["score"], main_similarity=SimilarityFunction.COSINE, name="sts-dev", ) # 5. Define the training arguments args = SentenceTransformerTrainingArguments( # Required parameter: output_dir=output_dir, # Optional training parameters: num_train_epochs=num_train_epochs, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, warmup_ratio=0.1, fp16=True, # Set to False if you get an error that your GPU can't run on FP16 bf16=False, # Set to True if you have a GPU that supports BF16 # Optional tracking/debugging parameters: eval_strategy="steps", eval_steps=100, save_strategy="steps", save_steps=100, save_total_limit=2, logging_steps=100, run_name="2d-matryoshka-sts", # Will be used in W&B if `wandb` is installed ) # 6. Create the trainer & start training trainer = SentenceTransformerTrainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=eval_dataset, loss=train_loss, evaluator=dev_evaluator, ) trainer.train() # 7. Evaluate the model performance on the STS Benchmark test dataset test_evaluator = EmbeddingSimilarityEvaluator( sentences1=test_dataset["sentence1"], sentences2=test_dataset["sentence2"], scores=test_dataset["score"], main_similarity=SimilarityFunction.COSINE, name="sts-test", ) test_evaluator(model) # 8. Save the trained & evaluated model locally final_output_dir = f"{output_dir}/final" model.save(final_output_dir) # 9. (Optional) save the model to the Hugging Face Hub! # It is recommended to run `huggingface-cli login` to log into your Hugging Face account first model_name = model_name if "/" not in model_name else model_name.split("/")[-1] try: model.push_to_hub(f"{model_name}-sts-2d-matryoshka") except Exception: logging.error( f"Error uploading model to the Hugging Face Hub:\n{traceback.format_exc()}To upload it manually, you can run " f"`huggingface-cli login`, followed by loading the model using `model = SentenceTransformer({final_output_dir!r})` " f"and saving it using `model.push_to_hub('{model_name}-sts-2d-matryoshka')`." )
""" This examples trains BERT (or any other transformer model like RoBERTa, DistilBERT etc.) for the STSbenchmark from scratch. It uses MatryoshkaLoss with the powerful CoSENTLoss to train models that perform well at output dimensions [768, 512, 256, 128, 64]. It generates sentence embeddings that can be compared using cosine-similarity to measure the similarity. Usage: python 2d_matryoshka_sts.py OR python 2d_matryoshka_sts.py pretrained_transformer_model_name """ import logging import sys import traceback from datetime import datetime from datasets import load_dataset from sentence_transformers import ( SentenceTransformer, SentenceTransformerTrainer, SentenceTransformerTrainingArguments, losses, ) from sentence_transformers.evaluation import EmbeddingSimilarityEvaluator, SimilarityFunction # Set the log level to INFO to get more information logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO) model_name = sys.argv[1] if len(sys.argv) > 1 else "distilbert-base-uncased" batch_size = 16 num_train_epochs = 4 # Save path of the model output_dir = f"output/2d_matryoshka_sts_{model_name.replace('/', '-')}-{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}" # 1. Here we define our SentenceTransformer model. If not already a Sentence Transformer model, it will automatically # create one with "mean" pooling. model = SentenceTransformer(model_name) # If we want, we can limit the maximum sequence length for the model # model.max_seq_length = 75 logging.info(model) # 2. Load the STSB dataset: https://huggingface.co/datasets/sentence-transformers/stsb train_dataset = load_dataset("sentence-transformers/stsb", split="train") eval_dataset = load_dataset("sentence-transformers/stsb", split="validation") test_dataset = load_dataset("sentence-transformers/stsb", split="test") logging.info(train_dataset) # 3. Define our training loss # CoSENTLoss (https://sbert.net/docs/package_reference/losses.html#cosentloss) needs two text columns and one # similarity score column (between 0 and 1) inner_train_loss = losses.CoSENTLoss(model=model) train_loss = losses.Matryoshka2dLoss(model, inner_train_loss, [768, 512, 256, 128, 64]) # 4. Define an evaluator for use during training. This is useful to keep track of alongside the evaluation loss. dev_evaluator = EmbeddingSimilarityEvaluator( sentences1=eval_dataset["sentence1"], sentences2=eval_dataset["sentence2"], scores=eval_dataset["score"], main_similarity=SimilarityFunction.COSINE, name="sts-dev", ) # 5. Define the training arguments args = SentenceTransformerTrainingArguments( # Required parameter: output_dir=output_dir, # Optional training parameters: num_train_epochs=num_train_epochs, per_device_train_batch_size=batch_size, per_device_eval_batch_size=batch_size, warmup_ratio=0.1, fp16=True, # Set to False if you get an error that your GPU can't run on FP16 bf16=False, # Set to True if you have a GPU that supports BF16 # Optional tracking/debugging parameters: eval_strategy="steps", eval_steps=100, save_strategy="steps", save_steps=100, save_total_limit=2, logging_steps=100, run_name="2d-matryoshka-sts", # Will be used in W&B if `wandb` is installed ) # 6. Create the trainer & start training trainer = SentenceTransformerTrainer( model=model, args=args, train_dataset=train_dataset, eval_dataset=eval_dataset, loss=train_loss, evaluator=dev_evaluator, ) trainer.train() # 7. Evaluate the model performance on the STS Benchmark test dataset test_evaluator = EmbeddingSimilarityEvaluator( sentences1=test_dataset["sentence1"], sentences2=test_dataset["sentence2"], scores=test_dataset["score"], main_similarity=SimilarityFunction.COSINE, name="sts-test", ) test_evaluator(model) # 8. Save the trained & evaluated model locally final_output_dir = f"{output_dir}/final" model.save(final_output_dir) # 9. (Optional) save the model to the Hugging Face Hub! # It is recommended to run `huggingface-cli login` to log into your Hugging Face account first model_name = model_name if "/" not in model_name else model_name.split("/")[-1] try: model.push_to_hub(f"{model_name}-sts-2d-matryoshka") except Exception: logging.error( f"Error uploading model to the Hugging Face Hub:\n{traceback.format_exc()}To upload it manually, you can run " f"`huggingface-cli login`, followed by loading the model using `model = SentenceTransformer({final_output_dir!r})` " f"and saving it using `model.push_to_hub('{model_name}-sts-2d-matryoshka')`." )
from typing import Iterable, Dict from docarray.array.storage.base.getsetdel import BaseGetSetDelMixin from docarray.array.storage.base.helper import Offset2ID from docarray import Document class GetSetDelMixin(BaseGetSetDelMixin): """Provide concrete implementation for ``__getitem__``, ``__setitem__``, and ``__delitem__`` for ``DocumentArrayElastic``""" def _document_to_elastic(self, doc: 'Document') -> Dict: extra_columns = {col: doc.tags.get(col) for col, _ in self._config.columns} request = { '_op_type': 'index', '_id': doc.id, '_index': self._config.index_name, 'embedding': self._map_embedding(doc.embedding), 'blob': doc.to_base64(), **extra_columns, } if self._config.tag_indices: for index in self._config.tag_indices: request[index] = doc.tags.get(index) if doc.text: request['text'] = doc.text return request def _getitem(self, doc_id: str) -> 'Document': """Helper method for getting item with elastic as storage :param doc_id: id of the document :raises KeyError: raise error when elastic id does not exist in storage :return: Document """ try: result = self._client.get(index=self._config.index_name, id=doc_id) doc = Document.from_base64(result['_source']['blob']) return doc except Exception as ex: raise KeyError(doc_id) from ex def _get_doc_by_id(self, _id: str) -> 'Document': """Concrete implementation of base class' ``_get_doc_by_id`` :param _id: the id of the document :return: the retrieved document from elastic """ return self._getitem(_id) def _set_doc_by_id(self, _id: str, value: 'Document'): """Concrete implementation of base class' ``_set_doc_by_id`` :param _id: the id of doc to update :param value: the document to update to """ if _id != value.id: self._del_doc_by_id(_id) request = [self._document_to_elastic(value)] self._send_requests(request) self._refresh(self._config.index_name) def _set_docs_by_ids(self, ids, docs: Iterable['Document'], mismatch_ids: Dict): """Overridden implementation of _set_docs_by_ids in order to add docs in batches and flush at the end :param ids: the ids used for indexing """ for _id, doc in zip(ids, docs): self._set_doc_by_id(_id, doc) self._refresh(self._config.index_name) def _del_doc_by_id(self, _id: str): """Concrete implementation of base class' ``_del_doc_by_id`` :param _id: the id of the document to delete """ if self._doc_id_exists(_id): self._client.delete(index=self._config.index_name, id=_id) self._refresh(self._config.index_name) def _clear_storage(self): """Concrete implementation of base class' ``_clear_storage``""" self._client.indices.delete(index=self._config.index_name) def _load_offset2ids(self): ids = self._get_offset2ids_meta() self._offset2ids = Offset2ID(ids) def _save_offset2ids(self): self._update_offset2ids_meta()
from typing import Iterable, Dict from ..base.getsetdel import BaseGetSetDelMixin from ..base.helper import Offset2ID from .... import Document class GetSetDelMixin(BaseGetSetDelMixin): """Provide concrete implementation for ``__getitem__``, ``__setitem__``, and ``__delitem__`` for ``DocumentArrayElastic``""" def _document_to_elastic(self, doc: 'Document') -> Dict: extra_columns = {col: doc.tags.get(col) for col, _ in self._config.columns} request = { '_op_type': 'index', '_id': doc.id, '_index': self._config.index_name, 'embedding': self._map_embedding(doc.embedding), 'blob': doc.to_base64(), **extra_columns, } if self._config.tag_indices: for index in self._config.tag_indices: request[index] = doc.tags.get(index) if doc.text: request['text'] = doc.text return request def _getitem(self, doc_id: str) -> 'Document': """Helper method for getting item with elastic as storage :param doc_id: id of the document :raises KeyError: raise error when elastic id does not exist in storage :return: Document """ try: result = self._client.get(index=self._config.index_name, id=doc_id) doc = Document.from_base64(result['_source']['blob']) return doc except Exception as ex: raise KeyError(doc_id) from ex def _get_doc_by_id(self, _id: str) -> 'Document': """Concrete implementation of base class' ``_get_doc_by_id`` :param _id: the id of the document :return: the retrieved document from elastic """ return self._getitem(_id) def _set_doc_by_id(self, _id: str, value: 'Document'): """Concrete implementation of base class' ``_set_doc_by_id`` :param _id: the id of doc to update :param value: the document to update to """ if _id != value.id: self._del_doc_by_id(_id) request = [self._document_to_elastic(value)] self._send_requests(request) self._refresh(self._config.index_name) def _set_docs_by_ids(self, ids, docs: Iterable['Document'], mismatch_ids: Dict): """Overridden implementation of _set_docs_by_ids in order to add docs in batches and flush at the end :param ids: the ids used for indexing """ for _id, doc in zip(ids, docs): self._set_doc_by_id(_id, doc) self._refresh(self._config.index_name) def _del_doc_by_id(self, _id: str): """Concrete implementation of base class' ``_del_doc_by_id`` :param _id: the id of the document to delete """ if self._doc_id_exists(_id): self._client.delete(index=self._config.index_name, id=_id) self._refresh(self._config.index_name) def _clear_storage(self): """Concrete implementation of base class' ``_clear_storage``""" self._client.indices.delete(index=self._config.index_name) def _load_offset2ids(self): ids = self._get_offset2ids_meta() self._offset2ids = Offset2ID(ids) def _save_offset2ids(self): self._update_offset2ids_meta()
from __future__ import annotations from sentence_transformers.sparse_encoder.evaluation.SparseBinaryClassificationEvaluator import ( SparseBinaryClassificationEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseEmbeddingSimilarityEvaluator import ( SparseEmbeddingSimilarityEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseInformationRetrievalEvaluator import ( SparseInformationRetrievalEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseMSEEvaluator import ( SparseMSEEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseMSEEvaluatorDataFrame import ( SparseMSEEvaluatorDataFrame, ) from sentence_transformers.sparse_encoder.evaluation.SparseNanoBEIREvaluator import ( SparseNanoBEIREvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseRerankingEvaluator import ( SparseRerankingEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseTranslationEvaluator import ( SparseTranslationEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseTripletEvaluator import ( SparseTripletEvaluator, ) __all__ = [ "SparseEmbeddingSimilarityEvaluator", "SparseInformationRetrievalEvaluator", "SparseBinaryClassificationEvaluator", "SparseMSEEvaluator", "SparseNanoBEIREvaluator", "SparseTripletEvaluator", "SparseTranslationEvaluator", "SparseRerankingEvaluator", "SparseMSEEvaluatorDataFrame", ] # TODO: SparseMSEEvaluatorDataFrame : As for now handle sparse embed with numpy because of : trg_embeddings = np.asarray(self.embed_inputs(model, trg_sentences)) in MSEEvaluatorFromDataFrame check if needed and adapt to it # TODO: Adapt ParaphraseMiningEvaluator for handling Sparse override (but lot of fct to check esp in utils) # TODO: Check label accuracy (not understand how to adapt yet) if possible to have Sparse version
from __future__ import annotations from sentence_transformers.sparse_encoder.evaluation.SparseBinaryClassificationEvaluator import ( SparseBinaryClassificationEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseEmbeddingSimilarityEvaluator import ( SparseEmbeddingSimilarityEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseInformationRetrievalEvaluator import ( SparseInformationRetrievalEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseMSEEvaluator import ( SparseMSEEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseMSEEvaluatorDataFrame import ( SparseMSEEvaluatorDataFrame, ) from sentence_transformers.sparse_encoder.evaluation.SparseNanoBEIREvaluator import ( SparseNanoBEIREvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseReranking import ( SparseReranking, ) from sentence_transformers.sparse_encoder.evaluation.SparseTranslationEvaluator import ( SparseTranslationEvaluator, ) from sentence_transformers.sparse_encoder.evaluation.SparseTripletEvaluator import ( SparseTripletEvaluator, ) __all__ = [ "SparseEmbeddingSimilarityEvaluator", "SparseInformationRetrievalEvaluator", "SparseBinaryClassificationEvaluator", "SparseMSEEvaluator", "SparseNanoBEIREvaluator", "SparseTripletEvaluator", "SparseTranslationEvaluator", "SparseReranking", "SparseMSEEvaluatorDataFrame", ] # TODO: SparseMSEEvaluatorDataFrame : As for now handle sparse embed with numpy because of : trg_embeddings = np.asarray(self.embed_inputs(model, trg_sentences)) in MSEEvaluatorFromDataFrame check if needed and adapt to it # TODO: Adapt ParaphraseMiningEvaluator for handling Sparse override (but lot of fct to check esp in utils) # TODO: Check label accuracy (not understand how to adapt yet) if possible to have Sparse version
# THIS FILE HAS BEEN AUTOGENERATED. To update: # 1. modify the `_deps` dict in setup.py # 2. run `make deps_table_update` deps = { "Pillow": "Pillow", "accelerate": "accelerate>=0.31.0", "compel": "compel==0.1.8", "datasets": "datasets", "filelock": "filelock", "flax": "flax>=0.4.1", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.27.0", "requests-mock": "requests-mock==1.10.0", "importlib_metadata": "importlib_metadata", "invisible-watermark": "invisible-watermark>=0.2.0", "isort": "isort>=5.5.4", "jax": "jax>=0.4.1", "jaxlib": "jaxlib>=0.4.1", "Jinja2": "Jinja2", "k-diffusion": "k-diffusion>=0.0.12", "torchsde": "torchsde", "note_seq": "note_seq", "librosa": "librosa", "numpy": "numpy", "parameterized": "parameterized", "peft": "peft>=0.6.0", "protobuf": "protobuf>=3.20.3,<4", "pytest": "pytest", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "python": "python>=3.8.0", "ruff": "ruff==0.9.10", "safetensors": "safetensors>=0.3.1", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "GitPython": "GitPython<3.1.19", "scipy": "scipy", "onnx": "onnx", "optimum_quanto": "optimum_quanto>=0.2.6", "gguf": "gguf>=0.10.0", "torchao": "torchao>=0.7.0", "bitsandbytes": "bitsandbytes>=0.43.3", "regex": "regex!=2019.12.17", "requests": "requests", "tensorboard": "tensorboard", "tiktoken": "tiktoken>=0.7.0", "torch": "torch>=1.4", "torchvision": "torchvision", "transformers": "transformers>=4.41.2", "urllib3": "urllib3<=2.0.0", "black": "black", "phonemizer": "phonemizer", "opencv-python": "opencv-python", }
# THIS FILE HAS BEEN AUTOGENERATED. To update: # 1. modify the `_deps` dict in setup.py # 2. run `make deps_table_update` deps = { "Pillow": "Pillow", "accelerate": "accelerate>=0.31.0", "compel": "compel==0.1.8", "datasets": "datasets", "filelock": "filelock", "flax": "flax>=0.4.1", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.27.0", "requests-mock": "requests-mock==1.10.0", "importlib_metadata": "importlib_metadata", "invisible-watermark": "invisible-watermark>=0.2.0", "isort": "isort>=5.5.4", "jax": "jax>=0.4.1", "jaxlib": "jaxlib>=0.4.1", "Jinja2": "Jinja2", "k-diffusion": "k-diffusion>=0.0.12", "torchsde": "torchsde", "note_seq": "note_seq", "librosa": "librosa", "numpy": "numpy", "parameterized": "parameterized", "peft": "peft>=0.6.0", "protobuf": "protobuf>=3.20.3,<4", "pytest": "pytest", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "python": "python>=3.8.0", "ruff": "ruff==0.9.10", "safetensors": "safetensors>=0.3.1", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "GitPython": "GitPython<3.1.19", "scipy": "scipy", "onnx": "onnx", "optimum_quanto": "optimum_quanto>=0.2.6", "gguf": "gguf>=0.10.0", "torchao": "torchao>=0.7.0", "bitsandbytes": "bitsandbytes>=0.43.3", "regex": "regex!=2019.12.17", "requests": "requests", "tensorboard": "tensorboard", "tiktoken": "tiktoken>=0.7.0", "torch": "torch>=1.4", "torchvision": "torchvision", "transformers": "transformers>=4.41.2", "urllib3": "urllib3<=2.0.0", "black": "black", "phonemizer": "phonemizer", }
_base_ = './cascade-mask-rcnn_convnext-t-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py' # noqa # TODO: delete custom_imports after mmcls supports auto import # please install mmcls>=1.0 # import mmcls.models to trigger register_module in mmcls custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-small_3rdparty_32xb128-noema_in1k_20220301-303e75e3.pth' # noqa model = dict( backbone=dict( _delete_=True, type='mmcls.ConvNeXt', arch='small', out_indices=[0, 1, 2, 3], drop_path_rate=0.6, layer_scale_init_value=1.0, gap_before_final_norm=False, init_cfg=dict( type='Pretrained', checkpoint=checkpoint_file, prefix='backbone.'))) optim_wrapper = dict(paramwise_cfg={ 'decay_rate': 0.7, 'decay_type': 'layer_wise', 'num_layers': 12 })
_base_ = './cascade-mask-rcnn_convnext-t-p4-w7_fpn_4conv1fc-giou_amp-ms-crop-3x_coco.py' # noqa # please install mmcls>=1.0 # import mmcls.models to trigger register_module in mmcls custom_imports = dict(imports=['mmcls.models'], allow_failed_imports=False) checkpoint_file = 'https://download.openmmlab.com/mmclassification/v0/convnext/downstream/convnext-small_3rdparty_32xb128-noema_in1k_20220301-303e75e3.pth' # noqa model = dict( backbone=dict( _delete_=True, type='mmcls.ConvNeXt', arch='small', out_indices=[0, 1, 2, 3], drop_path_rate=0.6, layer_scale_init_value=1.0, gap_before_final_norm=False, init_cfg=dict( type='Pretrained', checkpoint=checkpoint_file, prefix='backbone.'))) optim_wrapper = dict(paramwise_cfg={ 'decay_rate': 0.7, 'decay_type': 'layer_wise', 'num_layers': 12 })
_base_ = '../retinanet/retinanet_r50_fpn_1x_coco.py' model = dict( bbox_head=dict( _delete_=True, type='GARetinaHead', 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]), anchor_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0]), loc_filter_thr=0.01, loss_loc=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_shape=dict(type='BoundedIoULoss', beta=0.2, loss_weight=1.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='SmoothL1Loss', beta=0.04, loss_weight=1.0)), # training and testing settings train_cfg=dict( ga_assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.4, ignore_iof_thr=-1), ga_sampler=dict( type='RandomSampler', num=256, pos_fraction=0.5, neg_pos_ub=-1, add_gt_as_proposals=False), assigner=dict(neg_iou_thr=0.5, min_pos_iou=0.0), center_ratio=0.2, ignore_ratio=0.5)) optim_wrapper = dict(clip_grad=dict(max_norm=35, norm_type=2))
_base_ = '../retinanet/retinanet_r50_fpn_1x_coco.py' model = dict( bbox_head=dict( _delete_=True, type='GARetinaHead', 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]), anchor_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[1.0, 1.0, 1.0, 1.0]), loc_filter_thr=0.01, loss_loc=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_shape=dict(type='BoundedIoULoss', beta=0.2, loss_weight=1.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='SmoothL1Loss', beta=0.04, loss_weight=1.0)), # training and testing settings train_cfg=dict( ga_assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.4, ignore_iof_thr=-1), ga_sampler=dict( type='RandomSampler', num=256, pos_fraction=0.5, neg_pos_ub=-1, add_gt_as_proposals=False), assigner=dict(neg_iou_thr=0.5, min_pos_iou=0.0), center_ratio=0.2, ignore_ratio=0.5)) optimizer_config = dict( _delete_=True, grad_clip=dict(max_norm=35, norm_type=2))
from keras.src.api_export import keras_export from keras.src.layers.pooling.base_pooling import BasePooling @keras_export(["keras.layers.MaxPooling2D", "keras.layers.MaxPool2D"]) class MaxPooling2D(BasePooling): """Max pooling operation for 2D spatial data. Downsamples the input along its spatial dimensions (height and width) by taking the maximum value over an input window (of size defined by `pool_size`) for each channel of the input. The window is shifted by `strides` along each dimension. The resulting output when using the `"valid"` padding option has a spatial shape (number of rows or columns) of: `output_shape = math.floor((input_shape - pool_size) / strides) + 1` (when `input_shape >= pool_size`) The resulting output shape when using the `"same"` padding option is: `output_shape = math.floor((input_shape - 1) / strides) + 1` Args: pool_size: int or tuple of 2 integers, factors by which to downscale (dim1, dim2). If only one integer is specified, the same window length will be used for all dimensions. strides: int or tuple of 2 integers, or None. Strides values. If None, it will default to `pool_size`. If only one int is specified, the same stride size will be used for all dimensions. padding: string, either `"valid"` or `"same"` (case-insensitive). `"valid"` means no padding. `"same"` results in padding evenly to the left/right or up/down of the input such that output has the same height/width dimension as the input. data_format: string, either `"channels_last"` or `"channels_first"`. The ordering of the dimensions in the inputs. `"channels_last"` corresponds to inputs with shape `(batch, height, width, channels)` while `"channels_first"` corresponds to inputs with shape `(batch, channels, height, width)`. It defaults to the `image_data_format` value found in your Keras config file at `~/.keras/keras.json`. If you never set it, then it will be `"channels_last"`. Input shape: - If `data_format="channels_last"`: 4D tensor with shape `(batch_size, height, width, channels)`. - If `data_format="channels_first"`: 4D tensor with shape `(batch_size, channels, height, width)`. Output shape: - If `data_format="channels_last"`: 4D tensor with shape `(batch_size, pooled_height, pooled_width, channels)`. - If `data_format="channels_first"`: 4D tensor with shape `(batch_size, channels, pooled_height, pooled_width)`. Examples: `strides=(1, 1)` and `padding="valid"`: >>> x = np.array([[1., 2., 3.], ... [4., 5., 6.], ... [7., 8., 9.]]) >>> x = np.reshape(x, [1, 3, 3, 1]) >>> max_pool_2d = keras.layers.MaxPooling2D(pool_size=(2, 2), ... strides=(1, 1), padding="valid") >>> max_pool_2d(x) `strides=(2, 2)` and `padding="valid"`: >>> x = np.array([[1., 2., 3., 4.], ... [5., 6., 7., 8.], ... [9., 10., 11., 12.]]) >>> x = np.reshape(x, [1, 3, 4, 1]) >>> max_pool_2d = keras.layers.MaxPooling2D(pool_size=(2, 2), ... strides=(2, 2), padding="valid") >>> max_pool_2d(x) `stride=(1, 1)` and `padding="same"`: >>> x = np.array([[1., 2., 3.], ... [4., 5., 6.], ... [7., 8., 9.]]) >>> x = np.reshape(x, [1, 3, 3, 1]) >>> max_pool_2d = keras.layers.MaxPooling2D(pool_size=(2, 2), ... strides=(1, 1), padding="same") >>> max_pool_2d(x) """ def __init__( self, pool_size=(2, 2), strides=None, padding="valid", data_format=None, name=None, **kwargs, ): super().__init__( pool_size, strides, pool_dimensions=2, pool_mode="max", padding=padding, data_format=data_format, name=name, **kwargs, )
from keras.src.api_export import keras_export from keras.src.layers.pooling.base_pooling import BasePooling @keras_export(["keras.layers.MaxPooling2D", "keras.layers.MaxPool2D"]) class MaxPooling2D(BasePooling): """Max pooling operation for 2D spatial data. Downsamples the input along its spatial dimensions (height and width) by taking the maximum value over an input window (of size defined by `pool_size`) for each channel of the input. The window is shifted by `strides` along each dimension. The resulting output when using the `"valid"` padding option has a spatial shape (number of rows or columns) of: `output_shape = math.floor((input_shape - pool_size) / strides) + 1` (when `input_shape >= pool_size`) The resulting output shape when using the `"same"` padding option is: `output_shape = math.floor((input_shape - 1) / strides) + 1` Args: pool_size: int or tuple of 2 integers, factors by which to downscale (dim1, dim2). If only one integer is specified, the same window length will be used for all dimensions. strides: int or tuple of 2 integers, or None. Strides values. If None, it will default to `pool_size`. If only one int is specified, the same stride size will be used for all dimensions. padding: string, either `"valid"` or `"same"` (case-insensitive). `"valid"` means no padding. `"same"` results in padding evenly to the left/right or up/down of the input such that output has the same height/width dimension as the input. data_format: string, either `"channels_last"` or `"channels_first"`. The ordering of the dimensions in the inputs. `"channels_last"` corresponds to inputs with shape `(batch, height, width, channels)` while `"channels_first"` corresponds to inputs with shape `(batch, channels, height, width)`. It defaults to the `image_data_format` value found in your Keras config file at `~/.keras/keras.json`. If you never set it, then it will be `"channels_last"`. Input shape: - If `data_format="channels_last"`: 4D tensor with shape `(batch_size, height, width, channels)`. - If `data_format="channels_first"`: 4D tensor with shape `(batch_size, channels, height, width)`. Output shape: - If `data_format="channels_last"`: 4D tensor with shape `(batch_size, pooled_height, pooled_width, channels)`. - If `data_format="channels_first"`: 4D tensor with shape `(batch_size, channels, pooled_height, pooled_width)`. Examples: `strides=(1, 1)` and `padding="valid"`: >>> x = np.array([[1., 2., 3.], ... [4., 5., 6.], ... [7., 8., 9.]]) >>> x = np.reshape(x, [1, 3, 3, 1]) >>> max_pool_2d = keras.layers.MaxPooling2D(pool_size=(2, 2), ... strides=(1, 1), padding="valid") >>> max_pool_2d(x) `strides=(2, 2)` and `padding="valid"`: >>> x = np.array([[1., 2., 3., 4.], ... [5., 6., 7., 8.], ... [9., 10., 11., 12.]]) >>> x = np.reshape(x, [1, 3, 4, 1]) >>> max_pool_2d = keras.layers.MaxPooling2D(pool_size=(2, 2), ... strides=(2, 2), padding="valid") >>> max_pool_2d(x) `stride=(1, 1)` and `padding="same"`: >>> x = np.array([[1., 2., 3.], ... [4., 5., 6.], ... [7., 8., 9.]]) >>> x = np.reshape(x, [1, 3, 3, 1]) >>> max_pool_2d = keras.layers.MaxPooling2D(pool_size=(2, 2), ... strides=(1, 1), padding="same") >>> max_pool_2d(x) """ def __init__( self, pool_size=(2, 2), strides=None, padding="valid", data_format=None, name=None, **kwargs ): super().__init__( pool_size, strides, pool_dimensions=2, pool_mode="max", padding=padding, data_format=data_format, name=name, **kwargs, )
import importlib.util import warnings from functools import wraps from typing import Optional def is_module_available(*modules: str) -> bool: r"""Returns if a top-level module with :attr:`name` exists *without** importing it. This is generally safer than try-catch block around a `import X`. It avoids third party libraries breaking assumptions of some of our tests, e.g., setting multiprocessing start method when imported (see librosa/#747, torchvision/#544). """ return all(importlib.util.find_spec(m) is not None for m in modules) def requires_module(*modules: str): """Decorate function to give error message if invoked without required optional modules. This decorator is to give better error message to users rather than raising ``NameError: name 'module' is not defined`` at random places. """ missing = [m for m in modules if not is_module_available(m)] if not missing: # fall through. If all the modules are available, no need to decorate def decorator(func): return func else: req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}") return wrapped return decorator def deprecated(direction: str, version: Optional[str] = None): """Decorator to add deprecation message Args: direction (str): Migration steps to be given to users. version (str or int): The version when the object will be removed """ def decorator(func): @wraps(func) def wrapped(*args, **kwargs): message = ( f"{func.__module__}.{func.__name__} has been deprecated " f'and will be removed from {"future" if version is None else version} release. ' f"{direction}" ) warnings.warn(message, stacklevel=2) return func(*args, **kwargs) return wrapped return decorator def fail_with_message(message): """Generate decorator to give users message about missing TorchAudio extension.""" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} {message}") return wrapped return decorator def no_op(func): """Op-op decorator. Used in place of fail_with_message when a functionality that requires extension works fine.""" return func
import importlib.util import warnings from functools import wraps from typing import Optional def is_module_available(*modules: str) -> bool: r"""Returns if a top-level module with :attr:`name` exists *without** importing it. This is generally safer than try-catch block around a `import X`. It avoids third party libraries breaking assumptions of some of our tests, e.g., setting multiprocessing start method when imported (see librosa/#747, torchvision/#544). """ return all(importlib.util.find_spec(m) is not None for m in modules) def requires_module(*modules: str): """Decorate function to give error message if invoked without required optional modules. This decorator is to give better error message to users rather than raising ``NameError: name 'module' is not defined`` at random places. """ missing = [m for m in modules if not is_module_available(m)] if not missing: # fall through. If all the modules are available, no need to decorate def decorator(func): return func else: req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}") return wrapped return decorator def deprecated(direction: str, version: Optional[str] = None): """Decorator to add deprecation message Args: direction (str): Migration steps to be given to users. version (str or int): The version when the object will be removed """ def decorator(func): @wraps(func) def wrapped(*args, **kwargs): message = ( f"{func.__module__}.{func.__name__} has been deprecated " f'and will be removed from {"future" if version is None else version} release. ' f"{direction}" ) warnings.warn(message, stacklevel=2) return func(*args, **kwargs) return wrapped return decorator def is_kaldi_available(): try: import torchaudio.lib._torchaudio return torchaudio.lib._torchaudio.is_kaldi_available() except Exception: return False def requires_kaldi(): if is_kaldi_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires kaldi") return wrapped return decorator def _check_soundfile_importable(): if not is_module_available("soundfile"): return False try: import soundfile # noqa: F401 return True except Exception: warnings.warn("Failed to import soundfile. 'soundfile' backend is not available.") return False _is_soundfile_importable = _check_soundfile_importable() def is_soundfile_available(): return _is_soundfile_importable def requires_soundfile(): if is_soundfile_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires soundfile") return wrapped return decorator def is_sox_available(): return is_module_available("torchaudio.lib._torchaudio_sox") def requires_sox(): if is_sox_available(): def decorator(func): return func else: def decorator(func): @wraps(func) def wrapped(*args, **kwargs): raise RuntimeError(f"{func.__module__}.{func.__name__} requires sox") return wrapped return decorator
# model settings preprocess_cfg = dict( mean=[103.530, 116.280, 123.675], std=[1.0, 1.0, 1.0], to_rgb=False, pad_size_divisor=32) model = dict( type='FastRCNN', preprocess_cfg=preprocess_cfg, backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), roi_head=dict( type='StandardRoIHead', bbox_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0), out_channels=256, featmap_strides=[4, 8, 16, 32]), bbox_head=dict( type='Shared2FCBBoxHead', in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.1, 0.1, 0.2, 0.2]), reg_class_agnostic=False, loss_cls=dict( type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0))), # model training and testing settings train_cfg=dict( rcnn=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0.5, match_low_quality=False, ignore_iof_thr=-1), sampler=dict( type='RandomSampler', num=512, pos_fraction=0.25, neg_pos_ub=-1, add_gt_as_proposals=True), pos_weight=-1, debug=False)), test_cfg=dict( rcnn=dict( score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100)))
# model settings model = dict( type='FastRCNN', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), roi_head=dict( type='StandardRoIHead', bbox_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0), out_channels=256, featmap_strides=[4, 8, 16, 32]), bbox_head=dict( type='Shared2FCBBoxHead', in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[0., 0., 0., 0.], target_stds=[0.1, 0.1, 0.2, 0.2]), reg_class_agnostic=False, loss_cls=dict( type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0))), # model training and testing settings train_cfg=dict( rcnn=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0.5, match_low_quality=False, ignore_iof_thr=-1), sampler=dict( type='RandomSampler', num=512, pos_fraction=0.25, neg_pos_ub=-1, add_gt_as_proposals=True), pos_weight=-1, debug=False)), test_cfg=dict( rcnn=dict( score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100)))
import asyncio import random import pytest from docarray import Document, DocumentArray from jina.helper import Namespace, random_identity from jina.serve.stream import RequestStreamer from jina.types.request.data import DataRequest class RequestStreamerWrapper: def __init__(self, num_requests, prefetch, iterate_sync_in_thread): self.num_requests = num_requests self.requests_handled = [] self.results_handled = [] self.request_ids = [random_identity() for _ in range(num_requests)] self.response_ids = [] args = Namespace() args.prefetch = prefetch self.streamer = RequestStreamer( request_handler=self.request_handler_fn, result_handler=self.result_handle_fn, end_of_iter_handler=self.end_of_iter_fn, prefetch=getattr(args, 'prefetch', 0), iterate_sync_in_thread=iterate_sync_in_thread, ) def request_handler_fn(self, request, **kwargs): self.requests_handled.append(request) async def task(): rand_sleep = random.uniform(0.1, 0.6) await asyncio.sleep(rand_sleep) docs = request.docs docs[0].tags['request_handled'] = True request.data.docs = docs return request future = asyncio.ensure_future(task()) return future, None def result_handle_fn(self, result): self.results_handled.append(result) assert isinstance(result, DataRequest) docs = result.docs docs[0].tags['result_handled'] = True result.data.docs = docs return result def end_of_iter_fn(self): # with a sync generator, iteration assert len(self.requests_handled) == self.num_requests assert len(self.results_handled) <= self.num_requests def _yield_data_request(self, i): req = DataRequest() req.header.request_id = self.request_ids[i] da = DocumentArray() da.append(Document()) req.data.docs = da return req def _get_sync_requests_iterator(self): for i in range(self.num_requests): yield self._yield_data_request(i) async def _get_async_requests_iterator(self): for i in range(self.num_requests): yield self._yield_data_request(i) await asyncio.sleep(0.1) @pytest.mark.asyncio @pytest.mark.parametrize('prefetch', [0, 5]) @pytest.mark.parametrize('num_requests', [1, 5, 13]) @pytest.mark.parametrize('async_iterator', [False, True]) @pytest.mark.parametrize('results_in_order', [False, True]) @pytest.mark.parametrize('iterate_sync_in_thread', [False, True]) async def test_request_streamer( prefetch, num_requests, async_iterator, results_in_order, iterate_sync_in_thread ): test_streamer = RequestStreamerWrapper( num_requests, prefetch, iterate_sync_in_thread ) streamer = test_streamer.streamer it = ( test_streamer._get_async_requests_iterator() if async_iterator else test_streamer._get_sync_requests_iterator() ) response = streamer.stream(request_iterator=it, results_in_order=results_in_order) num_responses = 0 async for r in response: test_streamer.response_ids.append(r.header.request_id) num_responses += 1 assert r.docs[0].tags['request_handled'] assert r.docs[0].tags['result_handled'] assert num_responses == num_requests assert len(test_streamer.request_ids) == len(test_streamer.response_ids) if results_in_order: for req_id, resp_id in zip( test_streamer.request_ids, test_streamer.response_ids ): assert req_id == resp_id @pytest.mark.asyncio @pytest.mark.parametrize('num_requests', [1, 5, 13]) @pytest.mark.parametrize('iterate_sync_in_thread', [False, True]) async def test_request_streamer_process_single_data( monkeypatch, num_requests, iterate_sync_in_thread ): test_streamer = RequestStreamerWrapper(num_requests, 0, iterate_sync_in_thread) streamer = test_streamer.streamer def end_of_iter_fn(): # bypass some assertions in RequestStreamerWrapper.end_of_iter_fn pass monkeypatch.setattr(streamer, '_end_of_iter_handler', end_of_iter_fn) it = test_streamer._get_sync_requests_iterator() num_responses = 0 for req in it: r = await streamer.process_single_data(request=req) test_streamer.response_ids.append(r.header.request_id) num_responses += 1 assert r.docs[0].tags['request_handled'] assert r.docs[0].tags['result_handled'] assert num_responses == num_requests assert len(test_streamer.request_ids) == len(test_streamer.response_ids)
import asyncio import random import pytest from docarray import Document, DocumentArray from jina.helper import Namespace, random_identity from jina.serve.stream import RequestStreamer from jina.types.request.data import DataRequest class RequestStreamerWrapper: def __init__(self, num_requests, prefetch, iterate_sync_in_thread): self.num_requests = num_requests self.requests_handled = [] self.results_handled = [] self.request_ids = [random_identity() for _ in range(num_requests)] self.response_ids = [] args = Namespace() args.prefetch = prefetch self.streamer = RequestStreamer( request_handler=self.request_handler_fn, result_handler=self.result_handle_fn, end_of_iter_handler=self.end_of_iter_fn, prefetch=getattr(args, 'prefetch', 0), iterate_sync_in_thread=iterate_sync_in_thread ) def request_handler_fn(self, request): self.requests_handled.append(request) async def task(): rand_sleep = random.uniform(0.1, 0.6) await asyncio.sleep(rand_sleep) docs = request.docs docs[0].tags['request_handled'] = True request.data.docs = docs return request future = asyncio.ensure_future(task()) return future, None def result_handle_fn(self, result): self.results_handled.append(result) assert isinstance(result, DataRequest) docs = result.docs docs[0].tags['result_handled'] = True result.data.docs = docs return result def end_of_iter_fn(self): # with a sync generator, iteration assert len(self.requests_handled) == self.num_requests assert len(self.results_handled) <= self.num_requests def _yield_data_request(self, i): req = DataRequest() req.header.request_id = self.request_ids[i] da = DocumentArray() da.append(Document()) req.data.docs = da return req def _get_sync_requests_iterator(self): for i in range(self.num_requests): yield self._yield_data_request(i) async def _get_async_requests_iterator(self): for i in range(self.num_requests): yield self._yield_data_request(i) await asyncio.sleep(0.1) @pytest.mark.asyncio @pytest.mark.parametrize('prefetch', [0, 5]) @pytest.mark.parametrize('num_requests', [1, 5, 13]) @pytest.mark.parametrize('async_iterator', [False, True]) @pytest.mark.parametrize('results_in_order', [False, True]) @pytest.mark.parametrize('iterate_sync_in_thread', [False, True]) async def test_request_streamer( prefetch, num_requests, async_iterator, results_in_order, iterate_sync_in_thread ): test_streamer = RequestStreamerWrapper(num_requests, prefetch, iterate_sync_in_thread) streamer = test_streamer.streamer it = ( test_streamer._get_async_requests_iterator() if async_iterator else test_streamer._get_sync_requests_iterator() ) response = streamer.stream(request_iterator=it, results_in_order=results_in_order) num_responses = 0 async for r in response: test_streamer.response_ids.append(r.header.request_id) num_responses += 1 assert r.docs[0].tags['request_handled'] assert r.docs[0].tags['result_handled'] assert num_responses == num_requests assert len(test_streamer.request_ids) == len(test_streamer.response_ids) if results_in_order: for req_id, resp_id in zip( test_streamer.request_ids, test_streamer.response_ids ): assert req_id == resp_id @pytest.mark.asyncio @pytest.mark.parametrize('num_requests', [1, 5, 13]) @pytest.mark.parametrize('iterate_sync_in_thread', [False, True]) async def test_request_streamer_process_single_data(monkeypatch, num_requests, iterate_sync_in_thread): test_streamer = RequestStreamerWrapper(num_requests, 0, iterate_sync_in_thread) streamer = test_streamer.streamer def end_of_iter_fn(): # bypass some assertions in RequestStreamerWrapper.end_of_iter_fn pass monkeypatch.setattr(streamer, '_end_of_iter_handler', end_of_iter_fn) it = test_streamer._get_sync_requests_iterator() num_responses = 0 for req in it: r = await streamer.process_single_data(request=req) test_streamer.response_ids.append(r.header.request_id) num_responses += 1 assert r.docs[0].tags['request_handled'] assert r.docs[0].tags['result_handled'] assert num_responses == num_requests assert len(test_streamer.request_ids) == len(test_streamer.response_ids)
from dataclasses import dataclass from typing import Callable, Dict from torchaudio._internal.module_utils import dropping_class_support from ._vggish_impl import _SAMPLE_RATE, VGGish as _VGGish, VGGishInputProcessor as _VGGishInputProcessor def _get_state_dict(): path = torchaudio.utils.download_asset("models/vggish.pt") return torch.load(path) @dropping_class_support @dataclass class VGGishBundle: """VGGish :cite:`45611` inference pipeline ported from `torchvggish <https://github.com/harritaylor/torchvggish>`__ and `tensorflow-models <https://github.com/tensorflow/models/tree/master/research/audioset>`__. Example: >>> import torchaudio >>> from torchaudio.prototype.pipelines import VGGISH >>> >>> input_sr = VGGISH.sample_rate >>> input_proc = VGGISH.get_input_processor() >>> model = VGGISH.get_model() >>> >>> waveform, sr = torchaudio.load( >>> "Chopin_Ballade_-1_In_G_Minor,_Op._23.mp3", >>> ) >>> waveform = waveform.squeeze(0) >>> waveform = torchaudio.functional.resample(waveform, sr, input_sr) >>> mono_output = model(input_proc(waveform)) """ class VGGish(_VGGish): __doc__ = _VGGish.__doc__ class VGGishInputProcessor(_VGGishInputProcessor): __doc__ = _VGGishInputProcessor.__doc__ _state_dict_func: Callable[[], Dict] @property def sample_rate(self) -> int: """Sample rate of input waveform expected by input processor and model. :type: int """ return _SAMPLE_RATE def get_model(self) -> VGGish: """Constructs pre-trained VGGish model. Downloads and caches weights as necessary. Returns: VGGish: VGGish model with pre-trained weights loaded. """ model = self.VGGish() state_dict = self._state_dict_func() model.load_state_dict(state_dict) model.eval() return model def get_input_processor(self) -> VGGishInputProcessor: """Constructs input processor for VGGish. Returns: VGGishInputProcessor: input processor for VGGish. """ return self.VGGishInputProcessor() VGGISH = VGGishBundle(_get_state_dict) VGGISH.__doc__ = """Pre-trained VGGish :cite:`45611` inference pipeline ported from `torchvggish <https://github.com/harritaylor/torchvggish>`__ and `tensorflow-models <https://github.com/tensorflow/models/tree/master/research/audioset>`__. Per the `documentation <https://github.com/tensorflow/models/tree/master/research/audioset/vggish>`__ for the original model, the model is "trained on a large YouTube dataset (a preliminary version of what later became YouTube-8M)". """
from dataclasses import dataclass from typing import Callable, Dict import torch import torchaudio from ._vggish_impl import _SAMPLE_RATE, VGGish as _VGGish, VGGishInputProcessor as _VGGishInputProcessor def _get_state_dict(): path = torchaudio.utils.download_asset("models/vggish.pt") return torch.load(path) @dataclass class VGGishBundle: """VGGish :cite:`45611` inference pipeline ported from `torchvggish <https://github.com/harritaylor/torchvggish>`__ and `tensorflow-models <https://github.com/tensorflow/models/tree/master/research/audioset>`__. Example: >>> import torchaudio >>> from torchaudio.prototype.pipelines import VGGISH >>> >>> input_sr = VGGISH.sample_rate >>> input_proc = VGGISH.get_input_processor() >>> model = VGGISH.get_model() >>> >>> waveform, sr = torchaudio.load( >>> "Chopin_Ballade_-1_In_G_Minor,_Op._23.mp3", >>> ) >>> waveform = waveform.squeeze(0) >>> waveform = torchaudio.functional.resample(waveform, sr, input_sr) >>> mono_output = model(input_proc(waveform)) """ class VGGish(_VGGish): __doc__ = _VGGish.__doc__ class VGGishInputProcessor(_VGGishInputProcessor): __doc__ = _VGGishInputProcessor.__doc__ _state_dict_func: Callable[[], Dict] @property def sample_rate(self) -> int: """Sample rate of input waveform expected by input processor and model. :type: int """ return _SAMPLE_RATE def get_model(self) -> VGGish: """Constructs pre-trained VGGish model. Downloads and caches weights as necessary. Returns: VGGish: VGGish model with pre-trained weights loaded. """ model = self.VGGish() state_dict = self._state_dict_func() model.load_state_dict(state_dict) model.eval() return model def get_input_processor(self) -> VGGishInputProcessor: """Constructs input processor for VGGish. Returns: VGGishInputProcessor: input processor for VGGish. """ return self.VGGishInputProcessor() VGGISH = VGGishBundle(_get_state_dict) VGGISH.__doc__ = """Pre-trained VGGish :cite:`45611` inference pipeline ported from `torchvggish <https://github.com/harritaylor/torchvggish>`__ and `tensorflow-models <https://github.com/tensorflow/models/tree/master/research/audioset>`__. Per the `documentation <https://github.com/tensorflow/models/tree/master/research/audioset/vggish>`__ for the original model, the model is "trained on a large YouTube dataset (a preliminary version of what later became YouTube-8M)". """
"""Timescale Vector Auto-retrieval Pack.""" from datetime import timedelta from typing import Any, Dict, List, Optional from llama_index.core.indices.vector_store import VectorStoreIndex from llama_index.core.indices.vector_store.retrievers import ( VectorIndexAutoRetriever, ) from llama_index.core.llama_pack.base import BaseLlamaPack from llama_index.core.query_engine import RetrieverQueryEngine from llama_index.core.schema import TextNode from llama_index.core.storage.storage_context import StorageContext from llama_index.core.vector_stores.types import VectorStoreInfo from llama_index.vector_stores.timescalevector import TimescaleVectorStore class TimescaleVectorAutoretrievalPack(BaseLlamaPack): """Timescale Vector auto-retrieval pack.""" def __init__( self, service_url: str, table_name: str, time_partition_interval: timedelta, vector_store_info: VectorStoreInfo, nodes: Optional[List[TextNode]] = None, **kwargs: Any, ) -> None: """Init params.""" self._vector_store = TimescaleVectorStore.from_params( service_url=service_url, table_name=table_name, time_partition_interval=time_partition_interval, ) if nodes is not None: self._storage_context = StorageContext.from_defaults( vector_store=self._vector_store ) self._index = VectorStoreIndex( nodes, storage_context=self._storage_context, **kwargs ) else: self._index = VectorStoreIndex.from_vector_store( self._vector_store, **kwargs ) self._storage_context = self._index.storage_context self.retriever = VectorIndexAutoRetriever( self._index, vector_store_info=vector_store_info ) self.query_engine = RetrieverQueryEngine(self.retriever) def get_modules(self) -> Dict[str, Any]: """Get modules.""" return { "vector_store": self._vector_store, "storage_context": self._storage_context, "index": self._index, "retriever": self.retriever, "query_engine": self.query_engine, } def retrieve(self, query_str: str) -> Any: """Retrieve.""" return self.retriever.retrieve(query_str) def run(self, *args: Any, **kwargs: Any) -> Any: """Run the pipeline.""" return self.query_engine.query(*args, **kwargs)
"""Timescale Vector Auto-retrieval Pack.""" from datetime import timedelta from typing import Any, Dict, List, Optional from llama_index.core.indices.vector_store import VectorStoreIndex from llama_index.core.indices.vector_store.retrievers import ( VectorIndexAutoRetriever, ) from llama_index.core.llama_pack.base import BaseLlamaPack from llama_index.core.query_engine import RetrieverQueryEngine from llama_index.core.schema import TextNode from llama_index.core.storage.storage_context import StorageContext from llama_index.core.vector_stores.types import VectorStoreInfo from llama_index.vector_stores.timescalevector import TimescaleVectorStore class TimescaleVectorAutoretrievalPack(BaseLlamaPack): """Timescale Vector auto-retrieval pack.""" def __init__( self, service_url: str, table_name: str, time_partition_interval: timedelta, vector_store_info: VectorStoreInfo, nodes: Optional[List[TextNode]] = None, **kwargs: Any, ) -> None: """Init params.""" self._vector_store = TimescaleVectorStore.from_params( service_url=service_url, table_name=table_name, time_partition_interval=time_partition_interval, ) if nodes is not None: self._storage_context = StorageContext.from_defaults( vector_store=self._vector_store ) self._index = VectorStoreIndex( nodes, storage_context=self._storage_context, **kwargs ) else: self._index = VectorStoreIndex.from_vector_store( self._vector_store, **kwargs ) self._storage_context = self._index.storage_context self.retriever = VectorIndexAutoRetriever( self._index, vector_store_info=vector_store_info ) self.query_engine = RetrieverQueryEngine(self.retriever) def get_modules(self) -> Dict[str, Any]: """Get modules.""" return { "vector_store": self._vector_store, "storage_context": self._storage_context, "index": self._index, "retriever": self.retriever, "query_engine": self.query_engine, } def retrieve(self, query_str: str) -> Any: """Retrieve.""" return self.retriever.retrieve(query_str) def run(self, *args: Any, **kwargs: Any) -> Any: """Run the pipeline.""" return self.query_engine.query(*args, **kwargs)
"""Tool for the DataForSeo SERP API.""" from typing import Optional from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) from langchain_core.tools import BaseTool from pydantic import Field from langchain_community.utilities.dataforseo_api_search import DataForSeoAPIWrapper class DataForSeoAPISearchRun(BaseTool): """Tool that queries the DataForSeo Google search API.""" name: str = "dataforseo_api_search" description: str = ( "A robust Google Search API provided by DataForSeo." "This tool is handy when you need information about trending topics " "or current events." ) api_wrapper: DataForSeoAPIWrapper def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" return str(self.api_wrapper.run(query)) async def _arun( self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" return (await self.api_wrapper.arun(query)).__str__() class DataForSeoAPISearchResults(BaseTool): """Tool that queries the DataForSeo Google Search API and get back json.""" name: str = "dataforseo_results_json" description: str = ( "A comprehensive Google Search API provided by DataForSeo." "This tool is useful for obtaining real-time data on current events " "or popular searches." "The input should be a search query and the output is a JSON object " "of the query results." ) api_wrapper: DataForSeoAPIWrapper = Field(default_factory=DataForSeoAPIWrapper) def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" return str(self.api_wrapper.results(query)) async def _arun( self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" return (await self.api_wrapper.aresults(query)).__str__()
"""Tool for the DataForSeo SERP API.""" from typing import Optional from langchain_core.callbacks import ( AsyncCallbackManagerForToolRun, CallbackManagerForToolRun, ) from langchain_core.tools import BaseTool from pydantic import Field from langchain_community.utilities.dataforseo_api_search import DataForSeoAPIWrapper class DataForSeoAPISearchRun(BaseTool): # type: ignore[override] """Tool that queries the DataForSeo Google search API.""" name: str = "dataforseo_api_search" description: str = ( "A robust Google Search API provided by DataForSeo." "This tool is handy when you need information about trending topics " "or current events." ) api_wrapper: DataForSeoAPIWrapper def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" return str(self.api_wrapper.run(query)) async def _arun( self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" return (await self.api_wrapper.arun(query)).__str__() class DataForSeoAPISearchResults(BaseTool): # type: ignore[override] """Tool that queries the DataForSeo Google Search API and get back json.""" name: str = "dataforseo_results_json" description: str = ( "A comprehensive Google Search API provided by DataForSeo." "This tool is useful for obtaining real-time data on current events " "or popular searches." "The input should be a search query and the output is a JSON object " "of the query results." ) api_wrapper: DataForSeoAPIWrapper = Field(default_factory=DataForSeoAPIWrapper) def _run( self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: """Use the tool.""" return str(self.api_wrapper.results(query)) async def _arun( self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None, ) -> str: """Use the tool asynchronously.""" return (await self.api_wrapper.aresults(query)).__str__()
_base_ = ['./mask2former_r50_lsj_8x2_50e_coco.py'] pretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth' # noqa depths = [2, 2, 6, 2] model = dict( type='Mask2Former', backbone=dict( _delete_=True, type='SwinTransformer', embed_dims=96, depths=depths, num_heads=[3, 6, 12, 24], window_size=7, mlp_ratio=4, qkv_bias=True, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0.3, patch_norm=True, out_indices=(0, 1, 2, 3), with_cp=False, convert_weights=True, frozen_stages=-1, init_cfg=dict(type='Pretrained', checkpoint=pretrained)), panoptic_head=dict( type='Mask2FormerHead', in_channels=[96, 192, 384, 768]), init_cfg=None) # set all layers in backbone to lr_mult=0.1 # set all norm layers, position_embeding, # query_embeding, level_embeding to decay_multi=0.0 backbone_norm_multi = dict(lr_mult=0.1, decay_mult=0.0) backbone_embed_multi = dict(lr_mult=0.1, decay_mult=0.0) embed_multi = dict(lr_mult=1.0, decay_mult=0.0) custom_keys = { 'backbone': dict(lr_mult=0.1, decay_mult=1.0), 'backbone.patch_embed.norm': backbone_norm_multi, 'backbone.norm': backbone_norm_multi, 'absolute_pos_embed': backbone_embed_multi, 'relative_position_bias_table': backbone_embed_multi, 'query_embed': embed_multi, 'query_feat': embed_multi, 'level_embed': embed_multi } custom_keys.update({ f'backbone.stages.{stage_id}.blocks.{block_id}.norm': backbone_norm_multi for stage_id, num_blocks in enumerate(depths) for block_id in range(num_blocks) }) custom_keys.update({ f'backbone.stages.{stage_id}.downsample.norm': backbone_norm_multi for stage_id in range(len(depths) - 1) }) # optimizer optim_wrapper = dict( paramwise_cfg=dict(custom_keys=custom_keys, norm_decay_mult=0.0))
_base_ = ['./mask2former_r50_lsj_8x2_50e_coco.py'] pretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_tiny_patch4_window7_224.pth' # noqa depths = [2, 2, 6, 2] model = dict( type='Mask2Former', backbone=dict( _delete_=True, type='SwinTransformer', embed_dims=96, depths=depths, num_heads=[3, 6, 12, 24], window_size=7, mlp_ratio=4, qkv_bias=True, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0.3, patch_norm=True, out_indices=(0, 1, 2, 3), with_cp=False, convert_weights=True, frozen_stages=-1, init_cfg=dict(type='Pretrained', checkpoint=pretrained)), panoptic_head=dict( type='Mask2FormerHead', in_channels=[96, 192, 384, 768]), init_cfg=None) # set all layers in backbone to lr_mult=0.1 # set all norm layers, position_embeding, # query_embeding, level_embeding to decay_multi=0.0 backbone_norm_multi = dict(lr_mult=0.1, decay_mult=0.0) backbone_embed_multi = dict(lr_mult=0.1, decay_mult=0.0) embed_multi = dict(lr_mult=1.0, decay_mult=0.0) custom_keys = { 'backbone': dict(lr_mult=0.1, decay_mult=1.0), 'backbone.patch_embed.norm': backbone_norm_multi, 'backbone.norm': backbone_norm_multi, 'absolute_pos_embed': backbone_embed_multi, 'relative_position_bias_table': backbone_embed_multi, 'query_embed': embed_multi, 'query_feat': embed_multi, 'level_embed': embed_multi } custom_keys.update({ f'backbone.stages.{stage_id}.blocks.{block_id}.norm': backbone_norm_multi for stage_id, num_blocks in enumerate(depths) for block_id in range(num_blocks) }) custom_keys.update({ f'backbone.stages.{stage_id}.downsample.norm': backbone_norm_multi for stage_id in range(len(depths) - 1) }) # optimizer optimizer = dict( type='AdamW', lr=0.0001, weight_decay=0.05, eps=1e-8, betas=(0.9, 0.999), paramwise_cfg=dict(custom_keys=custom_keys, norm_decay_mult=0.0))
# Copyright (c) OpenMMLab. All rights reserved. from unittest.mock import Mock from mmengine.hooks import DistSamplerSeedHook class TestDistSamplerSeedHook: def test_before_epoch(self): hook = DistSamplerSeedHook() # Test dataset sampler runner = Mock() runner.epoch = 1 runner.train_loop.dataloader = Mock() runner.train_loop.dataloader.sampler = Mock() runner.train_loop.dataloader.sampler.set_epoch = Mock() hook.before_train_epoch(runner) runner.train_loop.dataloader.sampler.set_epoch.assert_called() # Test batch sampler runner = Mock() runner.train_loop.dataloader = Mock() runner.train_loop.dataloader.sampler = Mock(spec_set=True) runner.train_loop.dataloader.batch_sampler = Mock() runner.train_loop.dataloader.batch_sampler.sampler = Mock() runner.train_loop.dataloader.batch_sampler.sampler.set_epoch = Mock() hook.before_train_epoch(runner) runner.train_loop.dataloader.\ batch_sampler.sampler.set_epoch.assert_called()
# Copyright (c) OpenMMLab. All rights reserved. from unittest.mock import Mock from mmengine.hooks import DistSamplerSeedHook class TestDistSamplerSeedHook: def test_before_epoch(self): hook = DistSamplerSeedHook() # Test dataset sampler runner = Mock() runner.epoch = 1 runner.cur_dataloader = Mock() runner.cur_dataloader.sampler = Mock() runner.cur_dataloader.sampler.set_epoch = Mock() hook.before_train_epoch(runner) runner.cur_dataloader.sampler.set_epoch.assert_called() # Test batch sampler runner = Mock() runner.cur_dataloader = Mock() runner.cur_dataloader.sampler = Mock(spec_set=True) runner.cur_dataloader.batch_sampler = Mock() runner.cur_dataloader.batch_sampler.sampler = Mock() runner.cur_dataloader.batch_sampler.sampler.set_epoch = Mock() hook.before_train_epoch(runner) runner.cur_dataloader.batch_sampler.sampler.set_epoch.assert_called()
import multiprocessing import random import time from functools import partial import pytest from jina import Client, Document, DocumentArray, Executor, Flow, requests from jina.types.request.data import Response NUM_REQUESTS = 5 class MyExecutor(Executor): @requests(on='/ping') def ping(self, **kwargs): time.sleep(0.1 * random.random()) @pytest.mark.parametrize('protocol', ['http', 'grpc']) @pytest.mark.parametrize('shards', [10]) @pytest.mark.parametrize('polling', ['ANY', 'ALL']) @pytest.mark.parametrize('prefetch', [1, 10]) @pytest.mark.parametrize('concurrent', [15]) @pytest.mark.parametrize('use_stream', [False, True]) def test_concurrent_clients(concurrent, protocol, shards, polling, prefetch, reraise, use_stream): if not use_stream and protocol != 'grpc': return def pong(peer_hash, queue, resp: Response): for d in resp.docs: queue.put((peer_hash, d.text)) def peer_client(port, protocol, peer_hash, queue): c = Client(protocol=protocol, port=port) for _ in range(NUM_REQUESTS): c.post( '/ping', Document(text=peer_hash), on_done=lambda r: pong(peer_hash, queue, r), return_responses=True, stream=use_stream ) f = Flow(protocol=protocol, prefetch=prefetch).add( uses=MyExecutor, shards=shards, polling=polling ) with f: pqueue = multiprocessing.Queue() port = f.port process_pool = [] for peer_id in range(concurrent): p = multiprocessing.Process( target=partial(peer_client, port, protocol, str(peer_id), pqueue), daemon=True, ) p.start() process_pool.append(p) for p in process_pool: p.join() queue_len = 0 while not pqueue.empty(): peer_hash, text = pqueue.get() assert peer_hash == text queue_len += 1 assert queue_len == concurrent * NUM_REQUESTS
import multiprocessing import random import time from functools import partial import pytest from jina import Client, Document, DocumentArray, Executor, Flow, requests from jina.types.request.data import Response NUM_REQUESTS = 5 class MyExecutor(Executor): @requests(on='/ping') def ping(self, **kwargs): time.sleep(0.1 * random.random()) @pytest.mark.parametrize('protocol', ['http', 'grpc']) @pytest.mark.parametrize('shards', [10]) @pytest.mark.parametrize('polling', ['ANY', 'ALL']) @pytest.mark.parametrize('prefetch', [1, 10]) @pytest.mark.parametrize('concurrent', [15]) def test_concurrent_clients(concurrent, protocol, shards, polling, prefetch, reraise): def pong(peer_hash, queue, resp: Response): for d in resp.docs: queue.put((peer_hash, d.text)) def peer_client(port, protocol, peer_hash, queue): c = Client(protocol=protocol, port=port) for _ in range(NUM_REQUESTS): c.post( '/ping', Document(text=peer_hash), on_done=lambda r: pong(peer_hash, queue, r), return_responses=True, ) f = Flow(protocol=protocol, prefetch=prefetch).add( uses=MyExecutor, shards=shards, polling=polling ) with f: pqueue = multiprocessing.Queue() port = f.port process_pool = [] for peer_id in range(concurrent): p = multiprocessing.Process( target=partial(peer_client, port, protocol, str(peer_id), pqueue), daemon=True, ) p.start() process_pool.append(p) for p in process_pool: p.join() queue_len = 0 while not pqueue.empty(): peer_hash, text = pqueue.get() assert peer_hash == text queue_len += 1 assert queue_len == concurrent * NUM_REQUESTS
# mypy: allow-untyped-defs import torch import torch.utils._pytree as pytree from torch._C import DispatchKey from torch._higher_order_ops.utils import ( autograd_not_implemented, reenter_make_fx, unique_graph_id, ) from torch._ops import HigherOrderOperator from torch._subclasses.fake_tensor import FakeTensorMode from torch.fx.experimental.proxy_tensor import ProxyTorchDispatchMode, track_tensor_tree # used for wrapping a function/op with context hints class HintsWrapper(HigherOrderOperator): def __init__(self): super().__init__("hints_wrapper") def __call__(self, body_fn, args, kwargs, hints): r""" Call implementation of hints_wrapper Args: body_fn (Callable): A callable function that is within the scope that is being traced. args (Tuple of torch.Tensor/int/float/bool): A tuple of inputs to body_fn. kwargs (dict): Keyword argument to the body_fn. hints (dict): A dict of context hints which could be passed to backend compiler. """ if not isinstance(args, tuple): raise RuntimeError(f"args must be a tuple, got {type(args)}") if not all(isinstance(t, (torch.Tensor, int, float, bool)) for t in args): raise RuntimeError( f"args must be a tuple of tensors, ints, floats, or bools, got {args}" ) if not isinstance(kwargs, dict): raise RuntimeError(f"kwargs must be a dict, got {type(kwargs)}") if len(kwargs) > 0: raise RuntimeError( f"kwargs except for hints are not supported, got {kwargs}" ) if not isinstance(hints, dict): raise RuntimeError(f"hints must be a dict, got {type(hints)}") for k, v in hints.items(): if not isinstance(k, str): raise RuntimeError(f"hints key must be a str, got {k}.") if not isinstance(v, (int, float, bool, str)): raise RuntimeError( "hints must be a dict containing int, float, bool or str " f"value, got value {v} for key {k}." ) return super().__call__(body_fn, args, kwargs, hints) hints_wrapper = HintsWrapper() @hints_wrapper.py_impl(DispatchKey.CompositeExplicitAutograd) def hints_wrapper_dense(body_fn, args, kwargs, hints): return body_fn(*args, **kwargs) hints_wrapper.py_autograd_impl( autograd_not_implemented(hints_wrapper, deferred_error=True) ) @hints_wrapper.py_impl(FakeTensorMode) def hints_wrapper_fake_tensor_mode(mode, body_func, args, kwargs, hints): flat_args = pytree.tree_leaves(args) with mode: return body_func(*flat_args, **kwargs) @hints_wrapper.py_functionalize_impl def hints_wrapper_functionalize(ctx, body_fn, args, kwargs, hints): from torch._higher_order_ops.utils import _check_alias_and_mutation unwrapped_args = ctx.unwrap_tensors(args) unwrapped_kwargs = ctx.unwrap_tensors(kwargs) unwrapped_hints = ctx.unwrap_tensors(hints) with ctx.redispatch_to_next(): functional_body_fn = ctx.functionalize(body_fn) pre_dispatch = hasattr(ctx, "mode") and ctx.mode.pre_dispatch _check_alias_and_mutation( body_fn, unwrapped_args, "hints_wrapper", pre_dispatch ) outputs = hints_wrapper( functional_body_fn, unwrapped_args, unwrapped_kwargs, unwrapped_hints, ) return ctx.wrap_tensors(outputs) def trace_hints_wrapper(proxy_mode, hints_wrapper, body_fn, args, kwargs, hints): flat_args = tuple(pytree.tree_leaves(args)) body_graph = reenter_make_fx(body_fn)(*flat_args, **kwargs) _, body_graph_name = unique_graph_id(proxy_mode, prefix="hints_wrapper_body_graph") proxy_mode.tracer.root.register_module(body_graph_name, body_graph) new_args: tuple = (body_graph, flat_args, {}) # merge hints into kwargs new_kwargs = {} new_kwargs["hints"] = hints proxy_args = pytree.tree_map(proxy_mode.tracer.unwrap_proxy, new_args) proxy_kwargs = pytree.tree_map(proxy_mode.tracer.unwrap_proxy, new_kwargs) out_proxy = proxy_mode.tracer.create_proxy( "call_function", hints_wrapper, proxy_args, proxy_kwargs, name="hints_wrapper" ) out = body_fn(*flat_args, **kwargs) return track_tensor_tree(out, out_proxy, constant=None, tracer=proxy_mode.tracer) @hints_wrapper.py_impl(ProxyTorchDispatchMode) def inner(proxy_mode, body_fn, args, kwargs, hints): if proxy_mode.enable_tracing: return trace_hints_wrapper( proxy_mode, hints_wrapper, body_fn, args, kwargs, hints ) else: return hints_wrapper(body_fn, args, kwargs, hints)
# mypy: allow-untyped-defs import torch import torch.utils._pytree as pytree from torch._C import DispatchKey from torch._higher_order_ops.utils import ( autograd_not_implemented, reenter_make_fx, unique_graph_id, ) from torch._ops import HigherOrderOperator from torch._subclasses.fake_tensor import FakeTensorMode from torch.fx.experimental.proxy_tensor import ProxyTorchDispatchMode, track_tensor_tree # used for wrapping a function/op with context hints class HintsWrapper(HigherOrderOperator): def __init__(self): super().__init__("hints_wrapper") def __call__(self, body_fn, args, kwargs, hints): r""" Call implementation of hints_wrapper Args: body_fn (Callable): A callable function that is within the scope that is being traced. args (Tuple of torch.Tensor/int/float/bool): A tuple of inputs to body_fn. kwargs (dict): Keyword argument to the body_fn. hints (dict): A dict of context hints which could be passed to backend compiler. """ if not isinstance(args, tuple): raise RuntimeError(f"args must be a tuple, got {type(args)}") if not all(isinstance(t, (torch.Tensor, int, float, bool)) for t in args): raise RuntimeError( "args must be a tuple of tensors, ints, floats, or bools, got " f"{args}" ) if not isinstance(kwargs, dict): raise RuntimeError(f"kwargs must be a dict, got {type(kwargs)}") if len(kwargs) > 0: raise RuntimeError( f"kwargs except for hints are not supported, got {kwargs}" ) if not isinstance(hints, dict): raise RuntimeError(f"hints must be a dict, got {type(hints)}") for k, v in hints.items(): if not isinstance(k, str): raise RuntimeError(f"hints key must be a str, got {k}.") if not isinstance(v, (int, float, bool, str)): raise RuntimeError( "hints must be a dict containing int, float, bool or str " f"value, got value {v} for key {k}." ) return super().__call__(body_fn, args, kwargs, hints) hints_wrapper = HintsWrapper() @hints_wrapper.py_impl(DispatchKey.CompositeExplicitAutograd) def hints_wrapper_dense(body_fn, args, kwargs, hints): return body_fn(*args, **kwargs) hints_wrapper.py_autograd_impl( autograd_not_implemented(hints_wrapper, deferred_error=True) ) @hints_wrapper.py_impl(FakeTensorMode) def hints_wrapper_fake_tensor_mode(mode, body_func, args, kwargs, hints): flat_args = pytree.tree_leaves(args) with mode: return body_func(*flat_args, **kwargs) @hints_wrapper.py_functionalize_impl def hints_wrapper_functionalize(ctx, body_fn, args, kwargs, hints): from torch._higher_order_ops.utils import _check_alias_and_mutation unwrapped_args = ctx.unwrap_tensors(args) unwrapped_kwargs = ctx.unwrap_tensors(kwargs) unwrapped_hints = ctx.unwrap_tensors(hints) with ctx.redispatch_to_next(): functional_body_fn = ctx.functionalize(body_fn) pre_dispatch = hasattr(ctx, "mode") and ctx.mode.pre_dispatch _check_alias_and_mutation( body_fn, unwrapped_args, "hints_wrapper", pre_dispatch ) outputs = hints_wrapper( functional_body_fn, unwrapped_args, unwrapped_kwargs, unwrapped_hints, ) return ctx.wrap_tensors(outputs) def trace_hints_wrapper(proxy_mode, hints_wrapper, body_fn, args, kwargs, hints): flat_args = tuple(pytree.tree_leaves(args)) body_graph = reenter_make_fx(body_fn)(*flat_args, **kwargs) _, body_graph_name = unique_graph_id(proxy_mode, prefix="hints_wrapper_body_graph") proxy_mode.tracer.root.register_module(body_graph_name, body_graph) new_args: tuple = (body_graph, flat_args, {}) # merge hints into kwargs new_kwargs = {} new_kwargs["hints"] = hints proxy_args = pytree.tree_map(proxy_mode.tracer.unwrap_proxy, new_args) proxy_kwargs = pytree.tree_map(proxy_mode.tracer.unwrap_proxy, new_kwargs) out_proxy = proxy_mode.tracer.create_proxy( "call_function", hints_wrapper, proxy_args, proxy_kwargs, name="hints_wrapper" ) out = body_fn(*flat_args, **kwargs) return track_tensor_tree(out, out_proxy, constant=None, tracer=proxy_mode.tracer) @hints_wrapper.py_impl(ProxyTorchDispatchMode) def inner(proxy_mode, body_fn, args, kwargs, hints): if proxy_mode.enable_tracing: return trace_hints_wrapper( proxy_mode, hints_wrapper, body_fn, args, kwargs, hints ) else: return hints_wrapper(body_fn, args, kwargs, hints)
# Licensed to the LF AI & Data foundation under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings from typing import List, Optional, Tuple, TypeVar from docarray.typing import AudioNdArray from docarray.typing.bytes.audio_bytes import AudioBytes from docarray.typing.proto_register import _register_proto from docarray.typing.url.any_url import AnyUrl from docarray.typing.url.mimetypes import AUDIO_MIMETYPE from docarray.utils._internal.misc import is_notebook T = TypeVar('T', bound='AudioUrl') @_register_proto(proto_type_name='audio_url') class AudioUrl(AnyUrl): """ URL to an audio file. Can be remote (web) URL, or a local file path. """ @classmethod def mime_type(cls) -> str: return AUDIO_MIMETYPE @classmethod def extra_extensions(cls) -> List[str]: """ Returns a list of additional file extensions that are valid for this class but cannot be identified by the mimetypes library. """ return [] def load(self: T) -> Tuple[AudioNdArray, int]: """ Load the data from the url into an [`AudioNdArray`][docarray.typing.AudioNdArray] and the frame rate. --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioNdArray, AudioUrl class MyDoc(BaseDoc): audio_url: AudioUrl audio_tensor: Optional[AudioNdArray] = None doc = MyDoc(audio_url='https://www.kozco.com/tech/piano2.wav') doc.audio_tensor, _ = doc.audio_url.load() assert isinstance(doc.audio_tensor, AudioNdArray) ``` --- :return: tuple of an [`AudioNdArray`][docarray.typing.AudioNdArray] representing the audio file content, and an integer representing the frame rate. """ bytes_ = self.load_bytes() return bytes_.load() def load_bytes(self, timeout: Optional[float] = None) -> AudioBytes: """ Convert url to [`AudioBytes`][docarray.typing.AudioBytes]. This will either load or download the file and save it into an [`AudioBytes`][docarray.typing.AudioBytes] object. :param timeout: timeout for urlopen. Only relevant if url is not local :return: [`AudioBytes`][docarray.typing.AudioBytes] object """ bytes_ = super().load_bytes(timeout=timeout) return AudioBytes(bytes_) def display(self): """ Play the audio sound from url in notebook. """ if is_notebook(): from IPython.display import Audio, display remote_url = True if self.startswith('http') else False if remote_url: display(Audio(data=self)) else: display(Audio(filename=self)) else: warnings.warn('Display of audio is only possible in a notebook.')
import warnings from typing import List, Optional, Tuple, TypeVar from docarray.typing import AudioNdArray from docarray.typing.bytes.audio_bytes import AudioBytes from docarray.typing.proto_register import _register_proto from docarray.typing.url.any_url import AnyUrl from docarray.typing.url.mimetypes import AUDIO_MIMETYPE from docarray.utils._internal.misc import is_notebook T = TypeVar('T', bound='AudioUrl') @_register_proto(proto_type_name='audio_url') class AudioUrl(AnyUrl): """ URL to an audio file. Can be remote (web) URL, or a local file path. """ @classmethod def mime_type(cls) -> str: return AUDIO_MIMETYPE @classmethod def extra_extensions(cls) -> List[str]: """ Returns a list of additional file extensions that are valid for this class but cannot be identified by the mimetypes library. """ return [] def load(self: T) -> Tuple[AudioNdArray, int]: """ Load the data from the url into an [`AudioNdArray`][docarray.typing.AudioNdArray] and the frame rate. --- ```python from typing import Optional from docarray import BaseDoc from docarray.typing import AudioNdArray, AudioUrl class MyDoc(BaseDoc): audio_url: AudioUrl audio_tensor: Optional[AudioNdArray] = None doc = MyDoc(audio_url='https://www.kozco.com/tech/piano2.wav') doc.audio_tensor, _ = doc.audio_url.load() assert isinstance(doc.audio_tensor, AudioNdArray) ``` --- :return: tuple of an [`AudioNdArray`][docarray.typing.AudioNdArray] representing the audio file content, and an integer representing the frame rate. """ bytes_ = self.load_bytes() return bytes_.load() def load_bytes(self, timeout: Optional[float] = None) -> AudioBytes: """ Convert url to [`AudioBytes`][docarray.typing.AudioBytes]. This will either load or download the file and save it into an [`AudioBytes`][docarray.typing.AudioBytes] object. :param timeout: timeout for urlopen. Only relevant if url is not local :return: [`AudioBytes`][docarray.typing.AudioBytes] object """ bytes_ = super().load_bytes(timeout=timeout) return AudioBytes(bytes_) def display(self): """ Play the audio sound from url in notebook. """ if is_notebook(): from IPython.display import Audio, display remote_url = True if self.startswith('http') else False if remote_url: display(Audio(data=self)) else: display(Audio(filename=self)) else: warnings.warn('Display of audio is only possible in a notebook.')
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '0.7.4' def parse_version_info(version_str): """Parse the version information. Args: version_str (str): version string like '0.1.0'. Returns: tuple: version information contains major, minor, micro version. """ version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '0.7.3' def parse_version_info(version_str): """Parse the version information. Args: version_str (str): version string like '0.1.0'. Returns: tuple: version information contains major, minor, micro version. """ version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
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)}
_base_ = '../faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py' # model settings model = dict( neck=[ dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), dict( type='BFP', in_channels=256, num_levels=5, refine_level=2, refine_type='non_local') ], roi_head=dict( bbox_head=dict( loss_bbox=dict( _delete_=True, type='BalancedL1Loss', alpha=0.5, gamma=1.5, beta=1.0, loss_weight=1.0))), # model training and testing settings train_cfg=dict( rpn=dict(sampler=dict(neg_pos_ub=5), allowed_border=-1), rcnn=dict( sampler=dict( _delete_=True, type='CombinedSampler', num=512, pos_fraction=0.25, add_gt_as_proposals=True, pos_sampler=dict(type='InstanceBalancedPosSampler'), neg_sampler=dict( type='IoUBalancedNegSampler', floor_thr=-1, floor_fraction=0, num_bins=3)))))
_base_ = '../faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py' # model settings model = dict( neck=[ dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), dict( type='BFP', in_channels=256, num_levels=5, refine_level=2, refine_type='non_local') ], roi_head=dict( bbox_head=dict( loss_bbox=dict( _delete_=True, type='BalancedL1Loss', alpha=0.5, gamma=1.5, beta=1.0, loss_weight=1.0))), # model training and testing settings train_cfg=dict( rpn=dict(sampler=dict(neg_pos_ub=5), allowed_border=-1), rcnn=dict( sampler=dict( _delete_=True, type='CombinedSampler', num=512, pos_fraction=0.25, add_gt_as_proposals=True, pos_sampler=dict(type='InstanceBalancedPosSampler'), neg_sampler=dict( type='IoUBalancedNegSampler', floor_thr=-1, floor_fraction=0, num_bins=3)))))
from sentence_transformers.models import Router from sentence_transformers.sparse_encoder import SparseEncoder from sentence_transformers.sparse_encoder.models import IDF, MLMTransformer, SpladePooling print("# ------------------------------------------example with v2 distill-----------------------------------------") doc_encoder = MLMTransformer("opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill") router = Router.for_query_document( query_modules=[ IDF.from_json( "opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill", tokenizer=doc_encoder.tokenizer, frozen=True, ), ], document_modules=[ doc_encoder, SpladePooling("max"), ], ) model = SparseEncoder( modules=[router], similarity_fn_name="dot", ) query = "What's the weather in ny now?" document = "Currently New York is rainy." query_embed = model.encode_query(query) document_embed = model.encode_document(document) sim = model.similarity(query_embed, document_embed) print(f"Similarity: {sim}") # Visualize top tokens for each text top_k = 3 print(f"\nTop tokens {top_k} for each text:") decoded_query = model.decode(query_embed, top_k=top_k) decoded_document = model.decode(document_embed) for i in range(top_k): query_token, query_score = decoded_query[i] doc_score = next((score for token, score in decoded_document if token == query_token), 0) if doc_score != 0: print(f"Token: {query_token}, Query score: {query_score:.4f}, Document score: {doc_score:.4f}") """ # ------------------------------------------example with v2 distill----------------------------------------- Similarity: tensor([[17.5307]], device='cuda:0') Top tokens 3 for each text: Token: ny, Query score: 5.7729, Document score: 1.4109 Token: weather, Query score: 4.5684, Document score: 1.4673 Token: now, Query score: 3.5895, Document score: 0.7473 """ print("# -----------------------------------------example with v3 distill-----------------------------------------") doc_encoder = MLMTransformer("opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill") router = Router.for_query_document( query_modules=[ IDF.from_json( "opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill", tokenizer=doc_encoder.tokenizer, frozen=True, ), ], document_modules=[ doc_encoder, SpladePooling(pooling_strategy="max", activation_function="log1p_relu"), ], ) model = SparseEncoder( modules=[router], similarity_fn_name="dot", ) query = "What's the weather in ny now?" document = "Currently New York is rainy." query_embed = model.encode_query(query) document_embed = model.encode_document(document) sim = model.similarity(query_embed, document_embed) print(f"Similarity: {sim}") # Visualize top tokens for each text top_k = 10 print(f"\nTop tokens {top_k} for each text:") decoded_query = model.decode(query_embed, top_k=top_k) decoded_document = model.decode(document_embed) for i in range(min(top_k, len(decoded_query))): query_token, query_score = decoded_query[i] doc_score = next((score for token, score in decoded_document if token == query_token), 0) if doc_score != 0: print(f"Token: {query_token}, Query score: {query_score:.4f}, Document score: {doc_score:.4f}") """ # -----------------------------------------example with v3 distill----------------------------------------- Similarity: tensor([[11.1105]], device='cuda:0') Top tokens 10 for each text: Token: ny, Query score: 5.7729, Document score: 0.8049 Token: weather, Query score: 4.5684, Document score: 0.9710 Token: now, Query score: 3.5895, Document score: 0.4720 Token: ?, Query score: 3.3313, Document score: 0.0286 Token: what, Query score: 2.7699, Document score: 0.0787 Token: in, Query score: 0.4989, Document score: 0.0417 """
from sentence_transformers import models from sentence_transformers.sparse_encoder import SparseEncoder from sentence_transformers.sparse_encoder.models import IDF, MLMTransformer, SpladePooling print("# ------------------------------------------example with v2 distill-----------------------------------------") doc_encoder = MLMTransformer("opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill") asym = models.Router( { "query": [ IDF.from_json( "opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill", tokenizer=doc_encoder.tokenizer, frozen=True, ), ], "document": [ doc_encoder, SpladePooling("max"), ], } ) model = SparseEncoder( modules=[asym], similarity_fn_name="dot", ) query = "What's the weather in ny now?" document = "Currently New York is rainy." query_embed = model.encode([{"query": query}]) document_embed = model.encode([{"document": document}]) sim = model.similarity(query_embed, document_embed) print(f"Similarity: {sim}") # Visualize top tokens for each text top_k = 3 print(f"\nTop tokens {top_k} for each text:") decoded_query = model.decode(query_embed[0], top_k=top_k) decoded_document = model.decode(document_embed[0]) for i in range(top_k): query_token, query_score = decoded_query[i] doc_score = next((score for token, score in decoded_document if token == query_token), 0) if doc_score != 0: print(f"Token: {query_token}, Query score: {query_score:.4f}, Document score: {doc_score:.4f}") """ # ------------------------------------------example with v2 distill----------------------------------------- Similarity: tensor([[17.5307]], device='cuda:0') Top tokens 3 for each text: Token: ny, Query score: 5.7729, Document score: 1.4109 Token: weather, Query score: 4.5684, Document score: 1.4673 Token: now, Query score: 3.5895, Document score: 0.7473 """ print("# -----------------------------------------example with v3 distill-----------------------------------------") doc_encoder = MLMTransformer("opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill") asym = models.Router( { "query": [ IDF.from_json( "opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill", tokenizer=doc_encoder.tokenizer, frozen=True, ), ], "document": [ doc_encoder, SpladePooling(pooling_strategy="max", activation_function="log1p_relu"), ], } ) model = SparseEncoder( modules=[asym], similarity_fn_name="dot", ) query = "What's the weather in ny now?" document = "Currently New York is rainy." query_embed = model.encode([{"query": query}]) document_embed = model.encode([{"document": document}]) sim = model.similarity(query_embed, document_embed) print(f"Similarity: {sim}") # Visualize top tokens for each text top_k = 10 print(f"\nTop tokens {top_k} for each text:") decoded_query = model.decode(query_embed[0], top_k=top_k) decoded_document = model.decode(document_embed[0]) for i in range(min(top_k, len(decoded_query))): query_token, query_score = decoded_query[i] doc_score = next((score for token, score in decoded_document if token == query_token), 0) if doc_score != 0: print(f"Token: {query_token}, Query score: {query_score:.4f}, Document score: {doc_score:.4f}") """ # -----------------------------------------example with v3 distill----------------------------------------- Similarity: tensor([[11.1105]], device='cuda:0') Top tokens 10 for each text: Token: ny, Query score: 5.7729, Document score: 0.8049 Token: weather, Query score: 4.5684, Document score: 0.9710 Token: now, Query score: 3.5895, Document score: 0.4720 Token: ?, Query score: 3.3313, Document score: 0.0286 Token: what, Query score: 2.7699, Document score: 0.0787 Token: in, Query score: 0.4989, Document score: 0.0417 """
from __future__ import annotations from torch import Tensor, nn from sentence_transformers.cross_encoder.CrossEncoder import CrossEncoder class CrossEntropyLoss(nn.Module): def __init__(self, model: CrossEncoder, activation_fct: nn.Module = nn.Identity(), **kwargs) -> None: """ Computes the Cross Entropy Loss for a CrossEncoder model. This loss is used to train a model to predict the correct class label for a given pair of sentences. The number of classes should be equal to the number of model output labels. Args: model (:class:`~sentence_transformers.cross_encoder.CrossEncoder`): A CrossEncoder model to be trained. activation_fct (:class:`~torch.nn.Module`): Activation function applied to the logits before computing the loss. Defaults to :class:`~torch.nn.Identity`. **kwargs: Additional keyword arguments passed to the underlying :class:`torch.nn.CrossEntropyLoss`. References: - :class:`torch.nn.CrossEntropyLoss` Requirements: 1. Your model can be initialized with `num_labels > 1` to predict multiple classes. 2. The number of dataset classes should be equal to the number of model output labels (`model.num_labels`). Inputs: +-------------------------------------------------+--------+-------------------------------+ | Texts | Labels | Number of Model Output Labels | +=================================================+========+===============================+ | (sentence_A, sentence_B) pairs | class | `num_classes` | +-------------------------------------------------+--------+-------------------------------+ Example: :: from sentence_transformers.cross_encoder import CrossEncoder, CrossEncoderTrainer, losses from datasets import Dataset model = CrossEncoder("microsoft/mpnet-base", num_labels=2) train_dataset = Dataset.from_dict({ "sentence1": ["How can I be a good geologist?", "What is the capital of France?"], "sentence2": ["What should I do to be a great geologist?", "What is the capital of Germany?"], "label": [1, 0], # 1: duplicate, 0: not duplicate }) loss = losses.CrossEntropyLoss(model) trainer = CrossEncoderTrainer( model=model, train_dataset=train_dataset, loss=loss, ) trainer.train() """ super().__init__() self.model = model self.activation_fct = activation_fct self.ce_loss = nn.CrossEntropyLoss(**kwargs) if not isinstance(self.model, CrossEncoder): raise ValueError( f"{self.__class__.__name__} expects a model of type CrossEncoder, " f"but got a model of type {type(self.model)}." ) def forward(self, inputs: list[list[str]], labels: Tensor) -> Tensor: if len(inputs) != 2: raise ValueError( f"CrossEntropyLoss expects a dataset with two non-label columns, but got a dataset with {len(inputs)} columns." ) pairs = list(zip(inputs[0], inputs[1])) tokens = self.model.tokenizer( pairs, padding=True, truncation=True, return_tensors="pt", ) tokens.to(self.model.device) logits = self.model(**tokens)[0] logits = self.activation_fct(logits) loss = self.ce_loss(logits, labels) return loss
from __future__ import annotations from torch import Tensor, nn from sentence_transformers.cross_encoder import CrossEncoder # TODO: Consider the naming of this class class CrossEntropyLoss(nn.Module): def __init__(self, model: CrossEncoder, activation_fct: nn.Module = nn.Identity(), **kwargs) -> None: super().__init__() self.model = model self.activation_fct = activation_fct self.ce_loss = nn.CrossEntropyLoss(**kwargs) def forward(self, inputs: list[list[str]], labels: Tensor) -> Tensor: if len(inputs) != 2: raise ValueError( f"CrossEntropyLoss expects a dataset with two non-label columns, but got a dataset with {len(inputs)} columns." ) pairs = list(zip(inputs[0], inputs[1])) tokens = self.model.tokenizer( pairs, padding=True, truncation=True, return_tensors="pt", ) tokens.to(self.model.device) logits = self.model(**tokens)[0] logits = self.activation_fct(logits) loss = self.ce_loss(logits, labels) return loss
# Copyright (c) OpenMMLab. All rights reserved. from unittest.mock import Mock import pytest from mmengine.hooks import ParamSchedulerHook class TestParamSchedulerHook: error_msg = ('runner.param_schedulers should be list of ParamScheduler or ' 'a dict containing list of ParamScheduler') def test_after_iter(self): # runner.param_schedulers should be a list or dict with pytest.raises(TypeError, match=self.error_msg): hook = ParamSchedulerHook() runner = Mock() scheduler = Mock() scheduler.step = Mock() scheduler.by_epoch = False runner.param_schedulers = scheduler hook.after_train_iter(runner, 0) scheduler.step.assert_called() # runner.param_schedulers is a list of schedulers hook = ParamSchedulerHook() runner = Mock() scheduler = Mock() scheduler.step = Mock() scheduler.by_epoch = False runner.param_schedulers = [scheduler] hook.after_train_iter(runner, 0) scheduler.step.assert_called() # runner.param_schedulers is a dict containing list of schedulers scheduler1 = Mock() scheduler1.step = Mock() scheduler1.by_epoch = False scheduler2 = Mock() scheduler2.step = Mock() scheduler2.by_epoch = False runner.param_schedulers = dict(key1=[scheduler1], key2=[scheduler2]) hook.after_train_epoch(runner) hook.after_train_iter(runner, 0) scheduler2.step.assert_called() def test_after_epoch(self): # runner.param_schedulers should be a list or dict with pytest.raises(TypeError, match=self.error_msg): hook = ParamSchedulerHook() runner = Mock() scheduler = Mock() scheduler.step = Mock() scheduler.by_epoch = True runner.param_schedulers = scheduler hook.after_train_iter(runner, 0) scheduler.step.assert_called() # runner.param_schedulers is a list of schedulers hook = ParamSchedulerHook() runner = Mock() scheduler = Mock() scheduler.step = Mock() scheduler.by_epoch = True runner.param_schedulers = [scheduler] hook.after_train_epoch(runner) scheduler.step.assert_called() # runner.param_schedulers is a dict containing list of schedulers scheduler1 = Mock() scheduler1.step = Mock() scheduler1.by_epoch = True scheduler2 = Mock() scheduler2.step = Mock() scheduler2.by_epoch = True runner.param_schedulers = dict(key1=[scheduler1], key2=[scheduler2]) hook.after_train_epoch(runner) scheduler1.step.assert_called() scheduler2.step.assert_called()
# Copyright (c) OpenMMLab. All rights reserved. from unittest.mock import Mock from mmengine.hooks import ParamSchedulerHook class TestParamSchedulerHook: def test_after_iter(self): hook = ParamSchedulerHook() runner = Mock() scheduler = Mock() scheduler.step = Mock() scheduler.by_epoch = False runner.param_schedulers = [scheduler] hook.after_train_iter(runner, 0) scheduler.step.assert_called() def test_after_epoch(self): hook = ParamSchedulerHook() runner = Mock() scheduler = Mock() scheduler.step = Mock() scheduler.by_epoch = True runner.param_schedulers = [scheduler] hook.after_train_epoch(runner) scheduler.step.assert_called()
""" Tatoeba (https://tatoeba.org/) is a collection of sentences and translation, mainly aiming for language learning. It is available for more than 300 languages. This script downloads the Tatoeba corpus and extracts the sentences & translations in the languages you like """ import gzip import os import tarfile import sentence_transformers # Note: Tatoeba uses 3 letter languages codes (ISO-639-2), # while other datasets like OPUS use 2 letter language codes (ISO-639-1) # For training of sentence transformers, which type of language code is used doesn't matter. # For language codes, see: https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes source_languages = set(["eng"]) target_languages = set(["deu", "ara", "tur", "spa", "ita", "fra"]) num_dev_sentences = 1000 # Number of sentences that are used to create a development set tatoeba_folder = "../datasets/tatoeba" output_folder = "parallel-sentences/" sentences_file_bz2 = os.path.join(tatoeba_folder, "sentences.tar.bz2") sentences_file = os.path.join(tatoeba_folder, "sentences.csv") links_file_bz2 = os.path.join(tatoeba_folder, "links.tar.bz2") links_file = os.path.join(tatoeba_folder, "links.csv") download_url = "https://downloads.tatoeba.org/exports/" os.makedirs(tatoeba_folder, exist_ok=True) os.makedirs(output_folder, exist_ok=True) # Download files if needed for filepath in [sentences_file_bz2, links_file_bz2]: if not os.path.exists(filepath): url = download_url + os.path.basename(filepath) print("Download", url) sentence_transformers.util.http_get(url, filepath) # Extract files if needed if not os.path.exists(sentences_file): print("Extract", sentences_file_bz2) tar = tarfile.open(sentences_file_bz2, "r:bz2") tar.extract("sentences.csv", path=tatoeba_folder) tar.close() if not os.path.exists(links_file): print("Extract", links_file_bz2) tar = tarfile.open(links_file_bz2, "r:bz2") tar.extract("links.csv", path=tatoeba_folder) tar.close() # Read sentences sentences = {} all_langs = target_languages.union(source_languages) print("Read sentences.csv file") with open(sentences_file, encoding="utf8") as fIn: for line in fIn: id, lang, sentence = line.strip().split("\t") if lang in all_langs: sentences[id] = (lang, sentence) # Read links that map the translations between different languages print("Read links.csv") translations = {src_lang: {trg_lang: {} for trg_lang in target_languages} for src_lang in source_languages} with open(links_file, encoding="utf8") as fIn: for line in fIn: src_id, target_id = line.strip().split() if src_id in sentences and target_id in sentences: src_lang, src_sent = sentences[src_id] trg_lang, trg_sent = sentences[target_id] if src_lang in source_languages and trg_lang in target_languages: if src_sent not in translations[src_lang][trg_lang]: translations[src_lang][trg_lang][src_sent] = [] translations[src_lang][trg_lang][src_sent].append(trg_sent) # Write everything to the output folder print("Write output files") for src_lang in source_languages: for trg_lang in target_languages: source_sentences = list(translations[src_lang][trg_lang]) train_sentences = source_sentences[num_dev_sentences:] dev_sentences = source_sentences[0:num_dev_sentences] print("{}-{} has {} sentences".format(src_lang, trg_lang, len(source_sentences))) if len(dev_sentences) > 0: with gzip.open( os.path.join(output_folder, "Tatoeba-{}-{}-dev.tsv.gz".format(src_lang, trg_lang)), "wt", encoding="utf8", ) as fOut: for sent in dev_sentences: fOut.write("\t".join([sent] + translations[src_lang][trg_lang][sent])) fOut.write("\n") if len(train_sentences) > 0: with gzip.open( os.path.join(output_folder, "Tatoeba-{}-{}-train.tsv.gz".format(src_lang, trg_lang)), "wt", encoding="utf8", ) as fOut: for sent in train_sentences: fOut.write("\t".join([sent] + translations[src_lang][trg_lang][sent])) fOut.write("\n") print("---DONE---")
""" Tatoeba (https://tatoeba.org/) is a collection of sentences and translation, mainly aiming for language learning. It is available for more than 300 languages. This script downloads the Tatoeba corpus and extracts the sentences & translations in the languages you like """ import os import sentence_transformers import tarfile import gzip # Note: Tatoeba uses 3 letter languages codes (ISO-639-2), # while other datasets like OPUS / TED2020 use 2 letter language codes (ISO-639-1) # For training of sentence transformers, which type of language code is used doesn't matter. # For language codes, see: https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes source_languages = set(['eng']) target_languages = set(['deu', 'ara', 'tur', 'spa', 'ita', 'fra']) num_dev_sentences = 1000 #Number of sentences that are used to create a development set tatoeba_folder = "../datasets/tatoeba" output_folder = "parallel-sentences/" sentences_file_bz2 = os.path.join(tatoeba_folder, 'sentences.tar.bz2') sentences_file = os.path.join(tatoeba_folder, 'sentences.csv') links_file_bz2 = os.path.join(tatoeba_folder, 'links.tar.bz2') links_file = os.path.join(tatoeba_folder, 'links.csv') download_url = "https://downloads.tatoeba.org/exports/" os.makedirs(tatoeba_folder, exist_ok=True) os.makedirs(output_folder, exist_ok=True) #Download files if needed for filepath in [sentences_file_bz2, links_file_bz2]: if not os.path.exists(filepath): url = download_url+os.path.basename(filepath) print("Download", url) sentence_transformers.util.http_get(url, filepath) #Extract files if needed if not os.path.exists(sentences_file): print("Extract", sentences_file_bz2) tar = tarfile.open(sentences_file_bz2, "r:bz2") tar.extract('sentences.csv', path=tatoeba_folder) tar.close() if not os.path.exists(links_file): print("Extract", links_file_bz2) tar = tarfile.open(links_file_bz2, "r:bz2") tar.extract('links.csv', path=tatoeba_folder) tar.close() #Read sentences sentences = {} all_langs = target_languages.union(source_languages) print("Read sentences.csv file") with open(sentences_file, encoding='utf8') as fIn: for line in fIn: id, lang, sentence = line.strip().split('\t') if lang in all_langs: sentences[id] = (lang, sentence) #Read links that map the translations between different languages print("Read links.csv") translations = {src_lang: {trg_lang: {} for trg_lang in target_languages} for src_lang in source_languages} with open(links_file, encoding='utf8') as fIn: for line in fIn: src_id, target_id = line.strip().split() if src_id in sentences and target_id in sentences: src_lang, src_sent = sentences[src_id] trg_lang, trg_sent = sentences[target_id] if src_lang in source_languages and trg_lang in target_languages: if src_sent not in translations[src_lang][trg_lang]: translations[src_lang][trg_lang][src_sent] = [] translations[src_lang][trg_lang][src_sent].append(trg_sent) #Write everything to the output folder print("Write output files") for src_lang in source_languages: for trg_lang in target_languages: source_sentences = list(translations[src_lang][trg_lang]) train_sentences = source_sentences[num_dev_sentences:] dev_sentences = source_sentences[0:num_dev_sentences] print("{}-{} has {} sentences".format(src_lang, trg_lang, len(source_sentences))) if len(dev_sentences) > 0: with gzip.open(os.path.join(output_folder, 'Tatoeba-{}-{}-dev.tsv.gz'.format(src_lang, trg_lang)), 'wt', encoding='utf8') as fOut: for sent in dev_sentences: fOut.write("\t".join([sent]+translations[src_lang][trg_lang][sent])) fOut.write("\n") if len(train_sentences) > 0: with gzip.open(os.path.join(output_folder, 'Tatoeba-{}-{}-train.tsv.gz'.format(src_lang, trg_lang)), 'wt', encoding='utf8') as fOut: for sent in train_sentences: fOut.write("\t".join([sent]+translations[src_lang][trg_lang][sent])) fOut.write("\n") print("---DONE---")
"""Standard LangChain interface tests""" from langchain_core.embeddings import Embeddings from langchain_tests.unit_tests.embeddings import EmbeddingsUnitTests from langchain_fireworks import FireworksEmbeddings class TestFireworksStandard(EmbeddingsUnitTests): @property def embeddings_class(self) -> type[Embeddings]: return FireworksEmbeddings @property def embeddings_params(self) -> dict: return {"api_key": "test_api_key"} @property def init_from_env_params(self) -> tuple[dict, dict, dict]: return ( { "FIREWORKS_API_KEY": "api_key", }, {}, { "fireworks_api_key": "api_key", }, )
"""Standard LangChain interface tests""" from typing import Tuple, Type from langchain_core.embeddings import Embeddings from langchain_tests.unit_tests.embeddings import EmbeddingsUnitTests from langchain_fireworks import FireworksEmbeddings class TestFireworksStandard(EmbeddingsUnitTests): @property def embeddings_class(self) -> Type[Embeddings]: return FireworksEmbeddings @property def embeddings_params(self) -> dict: return {"api_key": "test_api_key"} @property def init_from_env_params(self) -> Tuple[dict, dict, dict]: return ( { "FIREWORKS_API_KEY": "api_key", }, {}, { "fireworks_api_key": "api_key", }, )
import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDoc from docarray.documents import ImageDoc from docarray.utils.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp REMOTE_JPG = ( 'https://upload.wikimedia.org/wikipedia/commons/8/80/' 'Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg' ) @pytest.mark.slow @pytest.mark.internet def test_image(): image = ImageDoc(url=REMOTE_JPG) image.tensor = image.url.load() assert isinstance(image.tensor, np.ndarray) def test_image_str(): image = parse_obj_as(ImageDoc, 'http://myurl.jpg') assert image.url == 'http://myurl.jpg' def test_image_np(): image = parse_obj_as(ImageDoc, np.zeros((10, 10, 3))) assert (image.tensor == np.zeros((10, 10, 3))).all() def test_image_torch(): image = parse_obj_as(ImageDoc, torch.zeros(10, 10, 3)) assert (image.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.tensorflow def test_image_tensorflow(): image = ImageDoc(tensor=tf.zeros((10, 10, 3))) assert tnp.allclose(image.tensor.tensor, tf.zeros((10, 10, 3))) def test_image_shortcut_doc(): class MyDoc(BaseDoc): image: ImageDoc image2: ImageDoc image3: ImageDoc doc = MyDoc( image='http://myurl.jpg', image2=np.zeros((10, 10, 3)), image3=torch.zeros(10, 10, 3), ) assert doc.image.url == 'http://myurl.jpg' assert (doc.image2.tensor == np.zeros((10, 10, 3))).all() assert (doc.image3.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.slow @pytest.mark.internet def test_byte(): img = ImageDoc(url=REMOTE_JPG) img.bytes_ = img.url.load_bytes() @pytest.mark.slow @pytest.mark.internet def test_byte_from_tensor(): img = ImageDoc(url=REMOTE_JPG) img.tensor = img.url.load() img.bytes_ = img.tensor.to_bytes() assert isinstance(img.bytes_, bytes) assert len(img.bytes_) > 0
import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDocument from docarray.documents import ImageDoc from docarray.utils.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp REMOTE_JPG = ( 'https://upload.wikimedia.org/wikipedia/commons/8/80/' 'Dag_Sebastian_Ahlander_at_G%C3%B6teborg_Book_Fair_2012b.jpg' ) @pytest.mark.slow @pytest.mark.internet def test_image(): image = ImageDoc(url=REMOTE_JPG) image.tensor = image.url.load() assert isinstance(image.tensor, np.ndarray) def test_image_str(): image = parse_obj_as(ImageDoc, 'http://myurl.jpg') assert image.url == 'http://myurl.jpg' def test_image_np(): image = parse_obj_as(ImageDoc, np.zeros((10, 10, 3))) assert (image.tensor == np.zeros((10, 10, 3))).all() def test_image_torch(): image = parse_obj_as(ImageDoc, torch.zeros(10, 10, 3)) assert (image.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.tensorflow def test_image_tensorflow(): image = ImageDoc(tensor=tf.zeros((10, 10, 3))) assert tnp.allclose(image.tensor.tensor, tf.zeros((10, 10, 3))) def test_image_shortcut_doc(): class MyDoc(BaseDocument): image: ImageDoc image2: ImageDoc image3: ImageDoc doc = MyDoc( image='http://myurl.jpg', image2=np.zeros((10, 10, 3)), image3=torch.zeros(10, 10, 3), ) assert doc.image.url == 'http://myurl.jpg' assert (doc.image2.tensor == np.zeros((10, 10, 3))).all() assert (doc.image3.tensor == torch.zeros(10, 10, 3)).all() @pytest.mark.slow @pytest.mark.internet def test_byte(): img = ImageDoc(url=REMOTE_JPG) img.bytes_ = img.url.load_bytes() @pytest.mark.slow @pytest.mark.internet def test_byte_from_tensor(): img = ImageDoc(url=REMOTE_JPG) img.tensor = img.url.load() img.bytes_ = img.tensor.to_bytes() assert isinstance(img.bytes_, bytes) assert len(img.bytes_) > 0
from __future__ import annotations import json import os from typing import Any import torch from torch import nn class SpladePooling(nn.Module): """SPLADE pooling layer that aggregates MLM logits using max or sum pooling. This pooling layer takes MLM logits (shape: batch_size, seq_length, vocab_size) and applies SPLADE transformation (ReLU + log) followed by pooling across the sequence length dimension. Args: word_embedding_dimension: Dimension of the word embeddings (vocab size) pooling_strategy: Either 'max' or 'sum' for SPLADE pooling """ SPLADE_POOLING_MODES = ("sum", "max") def __init__(self, pooling_strategy: str = "max") -> None: super().__init__() self.pooling_strategy = pooling_strategy if pooling_strategy not in self.SPLADE_POOLING_MODES: raise ValueError("pooling_strategy must be either 'max' or 'sum'") self.config_keys = ["pooling_strategy"] self.word_embedding_dimension = None # This will be set in the forward method def forward(self, features: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: """Forward pass of the model. Args: features: Dictionary containing input features with 'mlm_logits' key Returns: Dictionary containing SPLADE pooled embeddings """ # Get the MLM head logits (shape: batch_size, seq_length, vocab_size) mlm_logits = features["mlm_logits"] # Apply ReLU and log transformation for SPLADE splade_scores = torch.log1p(torch.relu(mlm_logits)) # Pool across sequence length dimension if self.pooling_strategy == "max": pooled_scores = torch.max(splade_scores, dim=1)[0] # shape: batch_size, vocab_size else: # sum pooled_scores = torch.sum(splade_scores, dim=1) # shape: batch_size, vocab_size # Set the word embedding dimension if self.word_embedding_dimension is None: self.word_embedding_dimension = pooled_scores.shape[1] return {"sentence_embedding": pooled_scores} def get_config_dict(self) -> dict[str, Any]: return {key: self.__dict__[key] for key in self.config_keys} def save(self, output_path) -> None: with open(os.path.join(output_path, "config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) @staticmethod def load(input_path) -> SpladePooling: with open(os.path.join(input_path, "config.json")) as fIn: config = json.load(fIn) return SpladePooling(**config) def __repr__(self) -> str: return f"SpladePooling({self.get_config_dict()})" def get_sentence_embedding_dimension(self) -> int: """Get the dimension of the sentence embedding. Returns: int: Dimension of the sentence embedding """ return self.word_embedding_dimension
from __future__ import annotations import json import os from typing import Any import torch from torch import nn class SpladePooling(nn.Module): """SPLADE pooling layer that aggregates MLM logits using max or sum pooling. This pooling layer takes MLM logits (shape: batch_size, seq_length, vocab_size) and applies SPLADE transformation (ReLU + log) followed by pooling across the sequence length dimension. Args: word_embedding_dimension: Dimension of the word embeddings (vocab size) pooling_strategy: Either 'max' or 'sum' for SPLADE pooling """ SPLADE_POOLING_MODES = ("sum", "max") def __init__(self, pooling_strategy: str = "max") -> None: super().__init__() self.pooling_strategy = pooling_strategy if pooling_strategy not in self.SPLADE_POOLING_MODES: raise ValueError("pooling_strategy must be either 'max' or 'sum'") self.config_keys = ["pooling_strategy"] def forward(self, features: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: """FForward pass of the model. Args: features: Dictionary containing input features with 'mlm_logits' key Returns: Dictionary containing SPLADE pooled embeddings """ # Get the MLM head logits (shape: batch_size, seq_length, vocab_size) mlm_logits = features["mlm_logits"] # Apply ReLU and log transformation for SPLADE splade_scores = torch.log1p(torch.relu(mlm_logits)) # Pool across sequence length dimension if self.pooling_strategy == "max": pooled_scores = torch.max(splade_scores, dim=1)[0] # shape: batch_size, vocab_size else: # sum pooled_scores = torch.sum(splade_scores, dim=1) # shape: batch_size, vocab_size return {"sentence_embedding": pooled_scores} def get_config_dict(self) -> dict[str, Any]: return {key: self.__dict__[key] for key in self.config_keys} def save(self, output_path) -> None: with open(os.path.join(output_path, "config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) @staticmethod def load(input_path) -> SpladePooling: with open(os.path.join(input_path, "config.json")) as fIn: config = json.load(fIn) return SpladePooling(**config) def __repr__(self) -> str: return f"SpladePooling({self.get_config_dict()})"
import base64 from email.message import EmailMessage from typing import List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun from pydantic import BaseModel, Field from langchain_community.tools.gmail.base import GmailBaseTool class CreateDraftSchema(BaseModel): """Input for CreateDraftTool.""" message: str = Field( ..., description="The message to include in the draft.", ) to: List[str] = Field( ..., description="The list of recipients.", ) subject: str = Field( ..., description="The subject of the message.", ) cc: Optional[List[str]] = Field( None, description="The list of CC recipients.", ) bcc: Optional[List[str]] = Field( None, description="The list of BCC recipients.", ) class GmailCreateDraft(GmailBaseTool): """Tool that creates a draft email for Gmail.""" name: str = "create_gmail_draft" description: str = ( "Use this tool to create a draft email with the provided message fields." ) args_schema: Type[CreateDraftSchema] = CreateDraftSchema def _prepare_draft_message( self, message: str, to: List[str], subject: str, cc: Optional[List[str]] = None, bcc: Optional[List[str]] = None, ) -> dict: draft_message = EmailMessage() draft_message.set_content(message) draft_message["To"] = ", ".join(to) draft_message["Subject"] = subject if cc is not None: draft_message["Cc"] = ", ".join(cc) if bcc is not None: draft_message["Bcc"] = ", ".join(bcc) encoded_message = base64.urlsafe_b64encode(draft_message.as_bytes()).decode() return {"message": {"raw": encoded_message}} def _run( self, message: str, to: List[str], subject: str, cc: Optional[List[str]] = None, bcc: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: try: create_message = self._prepare_draft_message(message, to, subject, cc, bcc) draft = ( self.api_resource.users() .drafts() .create(userId="me", body=create_message) .execute() ) output = f"Draft created. Draft Id: {draft['id']}" return output except Exception as e: raise Exception(f"An error occurred: {e}")
import base64 from email.message import EmailMessage from typing import List, Optional, Type from langchain_core.callbacks import CallbackManagerForToolRun from pydantic import BaseModel, Field from langchain_community.tools.gmail.base import GmailBaseTool class CreateDraftSchema(BaseModel): """Input for CreateDraftTool.""" message: str = Field( ..., description="The message to include in the draft.", ) to: List[str] = Field( ..., description="The list of recipients.", ) subject: str = Field( ..., description="The subject of the message.", ) cc: Optional[List[str]] = Field( None, description="The list of CC recipients.", ) bcc: Optional[List[str]] = Field( None, description="The list of BCC recipients.", ) class GmailCreateDraft(GmailBaseTool): # type: ignore[override, override] """Tool that creates a draft email for Gmail.""" name: str = "create_gmail_draft" description: str = ( "Use this tool to create a draft email with the provided message fields." ) args_schema: Type[CreateDraftSchema] = CreateDraftSchema def _prepare_draft_message( self, message: str, to: List[str], subject: str, cc: Optional[List[str]] = None, bcc: Optional[List[str]] = None, ) -> dict: draft_message = EmailMessage() draft_message.set_content(message) draft_message["To"] = ", ".join(to) draft_message["Subject"] = subject if cc is not None: draft_message["Cc"] = ", ".join(cc) if bcc is not None: draft_message["Bcc"] = ", ".join(bcc) encoded_message = base64.urlsafe_b64encode(draft_message.as_bytes()).decode() return {"message": {"raw": encoded_message}} def _run( self, message: str, to: List[str], subject: str, cc: Optional[List[str]] = None, bcc: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForToolRun] = None, ) -> str: try: create_message = self._prepare_draft_message(message, to, subject, cc, bcc) draft = ( self.api_resource.users() .drafts() .create(userId="me", body=create_message) .execute() ) output = f"Draft created. Draft Id: {draft['id']}" return output except Exception as e: raise Exception(f"An error occurred: {e}")
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.embeddings import ( DeterministicFakeEmbedding, FakeEmbeddings, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "FakeEmbeddings": "langchain_community.embeddings", "DeterministicFakeEmbedding": "langchain_community.embeddings", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "DeterministicFakeEmbedding", "FakeEmbeddings", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.embeddings import ( DeterministicFakeEmbedding, FakeEmbeddings, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "FakeEmbeddings": "langchain_community.embeddings", "DeterministicFakeEmbedding": "langchain_community.embeddings", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "FakeEmbeddings", "DeterministicFakeEmbedding", ]
import functools import inspect import typing from typing import Optional, Union import grpc from jina.helper import convert_tuple_to_list if typing.TYPE_CHECKING: from prometheus_client.context_managers import Timer from prometheus_client import Summary from contextlib import nullcontext def _get_summary_time_context_or_null( summary_metric: Optional['Summary'], ) -> Union[nullcontext, 'Timer']: """ helper function to either get a time context or a nullcontext if the summary metric is None :param summary_metric: An optional metric :return: either a Timer context or a nullcontext """ return summary_metric.time() if summary_metric else nullcontext() def wrap_func(cls, func_lst, wrapper, **kwargs): """Wrapping a class method only once, inherited but not overridden method will not be wrapped again :param cls: class :param func_lst: function list to wrap :param wrapper: the wrapper :param kwargs: extra wrapper kwargs """ for f_name in func_lst: if hasattr(cls, f_name) and all( getattr(cls, f_name) != getattr(i, f_name, None) for i in cls.mro()[1:] ): setattr(cls, f_name, wrapper(getattr(cls, f_name), **kwargs)) def store_init_kwargs( func: typing.Callable, taboo: Optional[typing.Set] = None ) -> typing.Callable: """Mark the args and kwargs of :func:`__init__` later to be stored via :func:`save_config` in YAML :param func: the function to decorate :param taboo: class taboo set of parameters :return: the wrapped function """ taboo = taboo or {} @functools.wraps(func) def arg_wrapper(self, *args, **kwargs): if func.__name__ != '__init__': raise TypeError('this decorator should only be used on __init__ method') all_pars = inspect.signature(func).parameters tmp = {k: v.default for k, v in all_pars.items() if k not in taboo} tmp_list = [k for k in all_pars.keys() if k not in taboo] # set args by aligning tmp_list with arg values for k, v in zip(tmp_list, args): tmp[k] = v # set kwargs for k, v in kwargs.items(): if k in tmp: tmp[k] = v if hasattr(self, '_init_kwargs_dict'): self._init_kwargs_dict.update(tmp) else: self._init_kwargs_dict = tmp convert_tuple_to_list(self._init_kwargs_dict) f = func(self, *args, **kwargs) return f return arg_wrapper def extract_trailing_metadata(error: grpc.aio.AioRpcError) -> Optional[str]: '''Return formatted string of the trailing metadata if exists otherwise return None :param error: AioRpcError :return: string of Metadata or None ''' if type(error) == grpc.aio.AioRpcError: trailing_metadata = error.trailing_metadata() if trailing_metadata and len(trailing_metadata): return f'trailing_metadata={trailing_metadata}' return None def format_grpc_error(error: grpc.aio.AioRpcError) -> str: '''Adds grpc context trainling metadata if available :param error: AioRpcError :return: formatted error ''' default_string = str(error) trailing_metadata = extract_trailing_metadata(error) if trailing_metadata: return f'{default_string}\n{trailing_metadata}' return default_string
import functools import inspect import typing from typing import Optional, Union from jina.helper import convert_tuple_to_list if typing.TYPE_CHECKING: from prometheus_client.context_managers import Timer from prometheus_client import Summary from contextlib import nullcontext def _get_summary_time_context_or_null( summary_metric: Optional['Summary'], ) -> Union[nullcontext, 'Timer']: """ helper function to either get a time context or a nullcontext if the summary metric is None :param summary_metric: An optional metric :return: either a Timer context or a nullcontext """ return summary_metric.time() if summary_metric else nullcontext() def wrap_func(cls, func_lst, wrapper, **kwargs): """Wrapping a class method only once, inherited but not overridden method will not be wrapped again :param cls: class :param func_lst: function list to wrap :param wrapper: the wrapper :param kwargs: extra wrapper kwargs """ for f_name in func_lst: if hasattr(cls, f_name) and all( getattr(cls, f_name) != getattr(i, f_name, None) for i in cls.mro()[1:] ): setattr(cls, f_name, wrapper(getattr(cls, f_name), **kwargs)) def store_init_kwargs( func: typing.Callable, taboo: Optional[typing.Set] = None ) -> typing.Callable: """Mark the args and kwargs of :func:`__init__` later to be stored via :func:`save_config` in YAML :param func: the function to decorate :param taboo: class taboo set of parameters :return: the wrapped function """ taboo = taboo or {} @functools.wraps(func) def arg_wrapper(self, *args, **kwargs): if func.__name__ != '__init__': raise TypeError('this decorator should only be used on __init__ method') all_pars = inspect.signature(func).parameters tmp = {k: v.default for k, v in all_pars.items() if k not in taboo} tmp_list = [k for k in all_pars.keys() if k not in taboo] # set args by aligning tmp_list with arg values for k, v in zip(tmp_list, args): tmp[k] = v # set kwargs for k, v in kwargs.items(): if k in tmp: tmp[k] = v if hasattr(self, '_init_kwargs_dict'): self._init_kwargs_dict.update(tmp) else: self._init_kwargs_dict = tmp convert_tuple_to_list(self._init_kwargs_dict) f = func(self, *args, **kwargs) return f return arg_wrapper
import copy from dataclasses import dataclass, field from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=True) class AudioClassification(TaskTemplate): task: str = field(default="audio-classification", metadata={"include_in_asdict_even_if_is_default": True}) input_schema: ClassVar[Features] = Features({"audio": Audio()}) label_schema: ClassVar[Features] = Features({"labels": ClassLabel}) audio_column: str = "audio" label_column: str = "labels" def align_with_features(self, features): if self.label_column not in features: raise ValueError(f"Column {self.label_column} is not present in features.") if not isinstance(features[self.label_column], ClassLabel): raise ValueError(f"Column {self.label_column} is not a ClassLabel.") task_template = copy.deepcopy(self) label_schema = self.label_schema.copy() label_schema["labels"] = features[self.label_column] task_template.__dict__["label_schema"] = label_schema return task_template @property def column_mapping(self) -> Dict[str, str]: return { self.audio_column: "audio", self.label_column: "labels", }
import copy from dataclasses import dataclass from typing import ClassVar, Dict from ..features import Audio, ClassLabel, Features from .base import TaskTemplate @dataclass(frozen=True) class AudioClassification(TaskTemplate): task: str = "audio-classification" input_schema: ClassVar[Features] = Features({"audio": Audio()}) label_schema: ClassVar[Features] = Features({"labels": ClassLabel}) audio_column: str = "audio" label_column: str = "labels" def align_with_features(self, features): if self.label_column not in features: raise ValueError(f"Column {self.label_column} is not present in features.") if not isinstance(features[self.label_column], ClassLabel): raise ValueError(f"Column {self.label_column} is not a ClassLabel.") task_template = copy.deepcopy(self) label_schema = self.label_schema.copy() label_schema["labels"] = features[self.label_column] task_template.__dict__["label_schema"] = label_schema return task_template @property def column_mapping(self) -> Dict[str, str]: return { self.audio_column: "audio", self.label_column: "labels", }
from __future__ import annotations from collections.abc import Iterable import torch from torch import Tensor, nn from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class FlopsLoss(nn.Module): def __init__(self, model: SparseEncoder) -> None: """ FlopsLoss implements a regularization technique to promote sparsity in sparse encoder models. It calculates the squared L2 norm of the mean embedding vector, which helps reduce the number of floating-point operations (FLOPs) required during inference by encouraging more zero values in the embeddings. This loss is used as a regularization component within other losses like :class:`SpladeLoss` rather than being used as a standalone loss function. Args: model: SparseEncoder model to be regularized References: - For further details, see: https://arxiv.org/abs/2004.05665 Relations: - Used as a component within :class:`SpladeLoss` to regularize both query and document embeddings Example: - This loss is typically used within the :class:`SpladeLoss` class, which combines it with other loss components. """ super().__init__() self.model = model def forward(self, sentence_features: Iterable[dict[str, Tensor]], labels: Tensor) -> Tensor: # Compute the embeddings and distribute them to anchor and candidates (positive and optionally negatives) embeddings = [self.model(sentence_feature)["sentence_embedding"] for sentence_feature in sentence_features] return self.compute_loss_from_embeddings(embeddings) def compute_loss_from_embeddings(self, embeddings: list[torch.Tensor], embeddings_type: str) -> torch.Tensor: anchors = embeddings[0] # (batch_size, embedding_dim) candidates = torch.cat(embeddings[1:]) # (batch_size * (1 + num_negatives), embedding_dim) if embeddings_type == "query": return torch.sum(torch.mean(anchors, dim=0) ** 2) else: return torch.sum(torch.mean(candidates, dim=0) ** 2) @property def citation(self) -> str: return """ @article{paria2020minimizing, title={Minimizing flops to learn efficient sparse representations}, author={Paria, Biswajit and Yeh, Chih-Kuan and Yen, Ian EH and Xu, Ning and Ravikumar, Pradeep and P{\'o}czos, Barnab{\'a}s}, journal={arXiv preprint arXiv:2004.05665}, year={2020} } """
from __future__ import annotations from collections.abc import Iterable import torch from torch import Tensor, nn from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class FlopsLoss(nn.Module): def __init__(self, model: SparseEncoder) -> None: """ FlopsLoss implements a regularization technique to promote sparsity in sparse encoder models. It calculates the squared L2 norm of the mean embedding vector, which helps reduce the number of floating-point operations (FLOPs) required during inference by encouraging more zero values in the embeddings. This loss is used as a regularization component within other losses like it's done in SpladeLoss rather than as a standalone loss function. Args: model: SparseEncoder model to be regularized References: - For further details, see: https://arxiv.org/abs/2004.05665 Relations: - Used as a component within :class:`SpladeLoss` to regularize both query and document embeddings Example: This loss is typically used within the :class:`SpladeLoss` class, which combines it with other loss components. """ super().__init__() self.model = model def forward(self, sentence_features: Iterable[dict[str, Tensor]], labels: Tensor) -> Tensor: # Compute the embeddings and distribute them to anchor and candidates (positive and optionally negatives) embeddings = [self.model(sentence_feature)["sentence_embedding"] for sentence_feature in sentence_features] return self.compute_loss_from_embeddings(embeddings) def compute_loss_from_embeddings(self, embeddings: list[torch.Tensor], embeddings_type: str) -> torch.Tensor: anchors = embeddings[0] # (batch_size, embedding_dim) candidates = torch.cat(embeddings[1:]) # (batch_size * (1 + num_negatives), embedding_dim) if embeddings_type == "query": return torch.sum(torch.mean(anchors, dim=0) ** 2) else: return torch.sum(torch.mean(candidates, dim=0) ** 2) @property def citation(self) -> str: return """ @article{paria2020minimizing, title={Minimizing flops to learn efficient sparse representations}, author={Paria, Biswajit and Yeh, Chih-Kuan and Yen, Ian EH and Xu, Ning and Ravikumar, Pradeep and P{\'o}czos, Barnab{\'a}s}, journal={arXiv preprint arXiv:2004.05665}, year={2020} } """
import asyncio import json import os import time import pytest from jina import Client, Document from jina.enums import PodRoleType, PollingType from jina.helper import random_port from jina.orchestrate.pods import Pod from jina.orchestrate.pods.container import ContainerPod from jina.parsers import set_gateway_parser from jina.serve.runtimes.head import HeadRuntime from jina.serve.runtimes.worker import WorkerRuntime from tests.helper import _generate_pod_args cur_dir = os.path.dirname(os.path.abspath(__file__)) @pytest.fixture(scope='module') def head_runtime_docker_image_built(): import docker client = docker.from_env() client.images.build(path=os.path.join(cur_dir, 'head-runtime/'), tag='head-runtime') client.close() yield time.sleep(2) client = docker.from_env() client.containers.prune() @pytest.fixture(scope='module') def worker_runtime_docker_image_built(): import docker client = docker.from_env() client.images.build( path=os.path.join(cur_dir, 'worker-runtime/'), tag='worker-runtime' ) client.close() yield time.sleep(2) client = docker.from_env() client.containers.prune() @pytest.mark.asyncio # test gateway, head and worker pod by creating them manually in the most simple configuration async def test_pods_trivial_topology( head_runtime_docker_image_built, worker_runtime_docker_image_built ): worker_port = random_port() head_port = random_port() port = random_port() graph_description = '{"start-gateway": ["pod0"], "pod0": ["end-gateway"]}' pod_addresses = f'{{"pod0": ["0.0.0.0:{head_port}"]}}' # create a single worker pod worker_pod = _create_worker_pod(worker_port) # this would be done by the Pod, its adding the worker to the head worker_host, worker_port = worker_pod.runtime_ctrl_address.split(':') connection_list_dict = {'0': [f'{worker_host}:{worker_port}']} # create a single head pod head_pod = _create_head_pod(head_port, connection_list_dict) # create a single gateway pod gateway_pod = _create_gateway_pod(graph_description, pod_addresses, port) with gateway_pod, head_pod, worker_pod: await asyncio.sleep(1.0) assert HeadRuntime.wait_for_ready_or_shutdown( timeout=5.0, ctrl_address=head_pod.runtime_ctrl_address, ready_or_shutdown_event=head_pod.ready_or_shutdown.event, ) assert WorkerRuntime.wait_for_ready_or_shutdown( timeout=5.0, ctrl_address=worker_pod.runtime_ctrl_address, ready_or_shutdown_event=worker_pod.ready_or_shutdown.event, ) head_pod.ready_or_shutdown.event.wait(timeout=5.0) worker_pod.ready_or_shutdown.event.wait(timeout=5.0) gateway_pod.ready_or_shutdown.event.wait(timeout=5.0) # send requests to the gateway c = Client(host='localhost', port=port, asyncio=True) responses = c.post( '/', inputs=async_inputs, request_size=1, return_responses=True ) response_list = [] async for response in responses: response_list.append(response) assert len(response_list) == 20 assert len(response_list[0].docs) == 1 def _create_worker_pod(port): args = _generate_pod_args() args.port = port args.name = 'worker' args.uses = 'docker://worker-runtime' return ContainerPod(args) def _create_head_pod(port, connection_list_dict): args = _generate_pod_args() args.port = port args.name = 'head' args.pod_role = PodRoleType.HEAD args.polling = PollingType.ANY args.uses = 'docker://head-runtime' args.connection_list = json.dumps(connection_list_dict) return ContainerPod(args) def _create_gateway_pod(graph_description, pod_addresses, port): return Pod( set_gateway_parser().parse_args( [ '--graph-description', graph_description, '--deployments-addresses', pod_addresses, '--port', str(port), ] ) ) async def async_inputs(): for _ in range(20): yield Document(text='client0-Request')
import asyncio import json import os import time import pytest from jina import Client, Document from jina.enums import PodRoleType, PollingType from jina.helper import random_port from jina.orchestrate.pods import Pod from jina.orchestrate.pods.container import ContainerPod from jina.parsers import set_gateway_parser, set_pod_parser from jina.serve.runtimes.head import HeadRuntime from jina.serve.runtimes.worker import WorkerRuntime cur_dir = os.path.dirname(os.path.abspath(__file__)) @pytest.fixture(scope='module') def head_runtime_docker_image_built(): import docker client = docker.from_env() client.images.build(path=os.path.join(cur_dir, 'head-runtime/'), tag='head-runtime') client.close() yield time.sleep(2) client = docker.from_env() client.containers.prune() @pytest.fixture(scope='module') def worker_runtime_docker_image_built(): import docker client = docker.from_env() client.images.build( path=os.path.join(cur_dir, 'worker-runtime/'), tag='worker-runtime' ) client.close() yield time.sleep(2) client = docker.from_env() client.containers.prune() @pytest.mark.asyncio # test gateway, head and worker pod by creating them manually in the most simple configuration async def test_pods_trivial_topology( head_runtime_docker_image_built, worker_runtime_docker_image_built ): worker_port = random_port() head_port = random_port() port = random_port() graph_description = '{"start-gateway": ["pod0"], "pod0": ["end-gateway"]}' pod_addresses = f'{{"pod0": ["0.0.0.0:{head_port}"]}}' # create a single worker pod worker_pod = _create_worker_pod(worker_port) # this would be done by the Pod, its adding the worker to the head worker_host, worker_port = worker_pod.runtime_ctrl_address.split(':') connection_list_dict = {'0': [f'{worker_host}:{worker_port}']} # create a single head pod head_pod = _create_head_pod(head_port, connection_list_dict) # create a single gateway pod gateway_pod = _create_gateway_pod(graph_description, pod_addresses, port) with gateway_pod, head_pod, worker_pod: await asyncio.sleep(1.0) assert HeadRuntime.wait_for_ready_or_shutdown( timeout=5.0, ctrl_address=head_pod.runtime_ctrl_address, ready_or_shutdown_event=head_pod.ready_or_shutdown.event, ) assert WorkerRuntime.wait_for_ready_or_shutdown( timeout=5.0, ctrl_address=worker_pod.runtime_ctrl_address, ready_or_shutdown_event=worker_pod.ready_or_shutdown.event, ) head_pod.ready_or_shutdown.event.wait(timeout=5.0) worker_pod.ready_or_shutdown.event.wait(timeout=5.0) gateway_pod.ready_or_shutdown.event.wait(timeout=5.0) # send requests to the gateway c = Client(host='localhost', port=port, asyncio=True) responses = c.post( '/', inputs=async_inputs, request_size=1, return_responses=True ) response_list = [] async for response in responses: response_list.append(response) assert len(response_list) == 20 assert len(response_list[0].docs) == 1 def _create_worker_pod(port): args = set_pod_parser().parse_args([]) args.port = port args.name = 'worker' args.uses = 'docker://worker-runtime' return ContainerPod(args) def _create_head_pod(port, connection_list_dict): args = set_pod_parser().parse_args([]) args.port = port args.name = 'head' args.pod_role = PodRoleType.HEAD args.polling = PollingType.ANY args.uses = 'docker://head-runtime' args.connection_list = json.dumps(connection_list_dict) return ContainerPod(args) def _create_gateway_pod(graph_description, pod_addresses, port): return Pod( set_gateway_parser().parse_args( [ '--graph-description', graph_description, '--deployments-addresses', pod_addresses, '--port', str(port), ] ) ) async def async_inputs(): for _ in range(20): yield Document(text='client0-Request')
import pytest import torch from mmdet.models.backbones.swin import SwinBlock, SwinTransformer def test_swin_block(): # test SwinBlock structure and forward block = SwinBlock(embed_dims=64, num_heads=4, feedforward_channels=256) assert block.ffn.embed_dims == 64 assert block.attn.w_msa.num_heads == 4 assert block.ffn.feedforward_channels == 256 x = torch.randn(1, 56 * 56, 64) x_out = block(x, (56, 56)) assert x_out.shape == torch.Size([1, 56 * 56, 64]) # Test BasicBlock with checkpoint forward block = SwinBlock( embed_dims=64, num_heads=4, feedforward_channels=256, with_cp=True) assert block.with_cp x = torch.randn(1, 56 * 56, 64) x_out = block(x, (56, 56)) assert x_out.shape == torch.Size([1, 56 * 56, 64]) def test_swin_transformer(): """Test Swin Transformer backbone.""" with pytest.raises(TypeError): # Pretrained arg must be str or None. SwinTransformer(pretrained=123) with pytest.raises(AssertionError): # Because swin uses non-overlapping patch embed, so the stride of patch # embed must be equal to patch size. SwinTransformer(strides=(2, 2, 2, 2), patch_size=4) # test pretrained image size with pytest.raises(AssertionError): SwinTransformer(pretrain_img_size=(224, 224, 224)) # Test absolute position embedding temp = torch.randn((1, 3, 224, 224)) model = SwinTransformer(pretrain_img_size=224, use_abs_pos_embed=True) model.init_weights() model(temp) # Test patch norm model = SwinTransformer(patch_norm=False) model(temp) # Test normal inference temp = torch.randn((1, 3, 32, 32)) model = SwinTransformer() outs = model(temp) assert outs[0].shape == (1, 96, 8, 8) assert outs[1].shape == (1, 192, 4, 4) assert outs[2].shape == (1, 384, 2, 2) assert outs[3].shape == (1, 768, 1, 1) # Test abnormal inference size temp = torch.randn((1, 3, 31, 31)) model = SwinTransformer() outs = model(temp) assert outs[0].shape == (1, 96, 8, 8) assert outs[1].shape == (1, 192, 4, 4) assert outs[2].shape == (1, 384, 2, 2) assert outs[3].shape == (1, 768, 1, 1) # Test abnormal inference size temp = torch.randn((1, 3, 112, 137)) model = SwinTransformer() outs = model(temp) assert outs[0].shape == (1, 96, 28, 35) assert outs[1].shape == (1, 192, 14, 18) assert outs[2].shape == (1, 384, 7, 9) assert outs[3].shape == (1, 768, 4, 5) model = SwinTransformer(frozen_stages=4) model.train() for p in model.parameters(): assert not p.requires_grad
import pytest import torch from mmdet.models.backbones.swin import SwinBlock, SwinTransformer def test_swin_block(): # test SwinBlock structure and forward block = SwinBlock(embed_dims=64, num_heads=4, feedforward_channels=256) assert block.ffn.embed_dims == 64 assert block.attn.w_msa.num_heads == 4 assert block.ffn.feedforward_channels == 256 x = torch.randn(1, 56 * 56, 64) x_out = block(x, (56, 56)) assert x_out.shape == torch.Size([1, 56 * 56, 64]) # Test BasicBlock with checkpoint forward block = SwinBlock( embed_dims=64, num_heads=4, feedforward_channels=256, with_cp=True) assert block.with_cp x = torch.randn(1, 56 * 56, 64) x_out = block(x, (56, 56)) assert x_out.shape == torch.Size([1, 56 * 56, 64]) def test_swin_transformer(): """Test Swin Transformer backbone.""" with pytest.raises(TypeError): # Pretrained arg must be str or None. SwinTransformer(pretrained=123) with pytest.raises(AssertionError): # Because swin uses non-overlapping patch embed, so the stride of patch # embed must be equal to patch size. SwinTransformer(strides=(2, 2, 2, 2), patch_size=4) # test pretrained image size with pytest.raises(AssertionError): SwinTransformer(pretrain_img_size=(224, 224, 224)) # Test absolute position embedding temp = torch.randn((1, 3, 224, 224)) model = SwinTransformer(pretrain_img_size=224, use_abs_pos_embed=True) model.init_weights() model(temp) # Test patch norm model = SwinTransformer(patch_norm=False) model(temp) # Test normal inference temp = torch.randn((1, 3, 512, 512)) model = SwinTransformer() outs = model(temp) assert outs[0].shape == (1, 96, 128, 128) assert outs[1].shape == (1, 192, 64, 64) assert outs[2].shape == (1, 384, 32, 32) assert outs[3].shape == (1, 768, 16, 16) # Test abnormal inference size temp = torch.randn((1, 3, 511, 511)) model = SwinTransformer() outs = model(temp) assert outs[0].shape == (1, 96, 128, 128) assert outs[1].shape == (1, 192, 64, 64) assert outs[2].shape == (1, 384, 32, 32) assert outs[3].shape == (1, 768, 16, 16) # Test abnormal inference size temp = torch.randn((1, 3, 112, 137)) model = SwinTransformer() outs = model(temp) assert outs[0].shape == (1, 96, 28, 35) assert outs[1].shape == (1, 192, 14, 18) assert outs[2].shape == (1, 384, 7, 9) assert outs[3].shape == (1, 768, 4, 5) model = SwinTransformer(frozen_stages=4) model.train() for p in model.parameters(): assert not p.requires_grad