input
stringlengths
33
5k
output
stringlengths
32
5k
_base_ = [ '../_base_/models/mask_rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] model = dict( backbone=dict( frozen_stages=0, norm_cfg=dict(type='SyncBN', requires_grad=True), norm_eval=False, init_cfg=dict( type='Pretrained', checkpoint='./swav_800ep_pretrain.pth.tar'))) 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=[(1333, 640), (1333, 800)], keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline))
_base_ = [ '../_base_/models/mask_rcnn_r50_fpn.py', '../_base_/datasets/coco_instance.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] model = dict( backbone=dict( frozen_stages=0, norm_cfg=dict(type='SyncBN', requires_grad=True), norm_eval=False, init_cfg=dict( type='Pretrained', checkpoint='./swav_800ep_pretrain.pth.tar'))) 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=[(1333, 640), (1333, 800)], multiscale_mode='range', keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']) ] data = dict(train=dict(pipeline=train_pipeline))
_base_ = 'ssd300_voc0712.py' input_size = 512 model = dict( neck=dict( out_channels=(512, 1024, 512, 256, 256, 256, 256), level_strides=(2, 2, 2, 2, 1), level_paddings=(1, 1, 1, 1, 1), last_kernel_size=4), bbox_head=dict( in_channels=(512, 1024, 512, 256, 256, 256, 256), anchor_generator=dict( input_size=input_size, strides=[8, 16, 32, 64, 128, 256, 512], basesize_ratio_range=(0.15, 0.9), ratios=([2], [2, 3], [2, 3], [2, 3], [2, 3], [2], [2])))) # dataset settings dataset_type = 'VOCDataset' data_root = 'data/VOCdevkit/' train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='Expand', mean={{_base_.model.data_preprocessor.mean}}, to_rgb={{_base_.model.data_preprocessor.bgr_to_rgb}}, ratio_range=(1, 4)), dict( type='MinIoURandomCrop', min_ious=(0.1, 0.3, 0.5, 0.7, 0.9), min_crop_size=0.3), dict(type='Resize', scale=(input_size, input_size), keep_ratio=False), dict(type='RandomFlip', prob=0.5), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile'), dict(type='Resize', scale=(input_size, input_size), keep_ratio=False), # avoid bboxes being resized dict(type='LoadAnnotations', with_bbox=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=8, num_workers=3, dataset=dict( # RepeatDataset # the dataset is repeated 10 times, and the training schedule is 2x, # so the actual epoch = 12 * 10 = 120. times=10, dataset=dict( # ConcatDataset datasets=[ dict( type=dataset_type, data_root=data_root, ann_file='VOC2007/ImageSets/Main/trainval.txt', data_prefix=dict(sub_data_root='VOC2007/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline), dict( type=dataset_type, data_root=data_root, ann_file='VOC2012/ImageSets/Main/trainval.txt', data_prefix=dict(sub_data_root='VOC2012/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline) ]))) val_dataloader = dict(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader
_base_ = 'ssd300_voc0712.py' input_size = 512 model = dict( neck=dict( out_channels=(512, 1024, 512, 256, 256, 256, 256), level_strides=(2, 2, 2, 2, 1), level_paddings=(1, 1, 1, 1, 1), last_kernel_size=4), bbox_head=dict( in_channels=(512, 1024, 512, 256, 256, 256, 256), anchor_generator=dict( input_size=input_size, strides=[8, 16, 32, 64, 128, 256, 512], basesize_ratio_range=(0.15, 0.9), ratios=([2], [2, 3], [2, 3], [2, 3], [2, 3], [2], [2])))) img_norm_cfg = dict(mean=[123.675, 116.28, 103.53], std=[1, 1, 1], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='Expand', mean=img_norm_cfg['mean'], to_rgb=img_norm_cfg['to_rgb'], ratio_range=(1, 4)), dict( type='MinIoURandomCrop', min_ious=(0.1, 0.3, 0.5, 0.7, 0.9), min_crop_size=0.3), dict(type='Resize', img_scale=(512, 512), keep_ratio=False), dict(type='RandomFlip', flip_ratio=0.5), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict(type='Normalize', **img_norm_cfg), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(512, 512), flip=False, transforms=[ dict(type='Resize', keep_ratio=False), dict(type='Normalize', **img_norm_cfg), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( train=dict(dataset=dict(pipeline=train_pipeline)), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline))
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.chat_loaders.utils import ( map_ai_messages, map_ai_messages_in_session, merge_chat_runs, merge_chat_runs_in_session, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "merge_chat_runs_in_session": "langchain_community.chat_loaders.utils", "merge_chat_runs": "langchain_community.chat_loaders.utils", "map_ai_messages_in_session": "langchain_community.chat_loaders.utils", "map_ai_messages": "langchain_community.chat_loaders.utils", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "map_ai_messages", "map_ai_messages_in_session", "merge_chat_runs", "merge_chat_runs_in_session", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.chat_loaders.utils import ( map_ai_messages, map_ai_messages_in_session, merge_chat_runs, merge_chat_runs_in_session, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "merge_chat_runs_in_session": "langchain_community.chat_loaders.utils", "merge_chat_runs": "langchain_community.chat_loaders.utils", "map_ai_messages_in_session": "langchain_community.chat_loaders.utils", "map_ai_messages": "langchain_community.chat_loaders.utils", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "merge_chat_runs_in_session", "merge_chat_runs", "map_ai_messages_in_session", "map_ai_messages", ]
from typing import Dict, List, Tuple import pytest from opentelemetry.metrics import Meter from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics.export import ( HistogramDataPoint, InMemoryMetricReader, Metric, ) from jina.serve.networking.instrumentation import _NetworkingHistograms @pytest.fixture def metrics_setup() -> Tuple[InMemoryMetricReader, MeterProvider]: metric_reader = InMemoryMetricReader() meter_provider = MeterProvider(metric_readers=[metric_reader]) meter = meter_provider.get_meter('test') yield metric_reader, meter if hasattr(meter_provider, 'force_flush'): metric_reader.force_flush() if hasattr(meter_provider, 'shutdown'): meter_provider.shutdown() def test_get_labels(): a: _NetworkingHistograms = _NetworkingHistograms() assert a._get_labels() == None HIST_LABELS = { 'a': 1, 'b': 2, } a.histogram_metric_labels = HIST_LABELS assert a._get_labels() == HIST_LABELS ADD_LABELS = { 'b': 3, 'c': 4, } assert a._get_labels(ADD_LABELS) == {**HIST_LABELS, **ADD_LABELS} def test_recording_methods(metrics_setup: Tuple[InMemoryMetricReader, Meter]): metric_reader, meter = metrics_setup a: _NetworkingHistograms = _NetworkingHistograms( sending_requests_time_metrics=meter.create_histogram("request_time"), send_requests_bytes_metrics=meter.create_histogram("request_bytes"), received_response_bytes=meter.create_histogram("response_bytes"), histogram_metric_labels=None, ) a.record_sending_requests_time_metrics(10) a.record_send_requests_bytes_metrics(20) a.record_received_response_bytes(30) histogram_metrics: List[Metric] = ( metric_reader.get_metrics_data().resource_metrics[0].scope_metrics[0].metrics ) data_points_sums: Dict[str, HistogramDataPoint] = { hist.name: next(iter(hist.data.data_points)).sum for hist in histogram_metrics } assert data_points_sums == { 'request_time': 10, 'request_bytes': 20, 'response_bytes': 30, }
from typing import Dict, List, Tuple import pytest from opentelemetry.metrics import Meter from opentelemetry.sdk.metrics import MeterProvider from opentelemetry.sdk.metrics.export import ( HistogramDataPoint, InMemoryMetricReader, Metric, ) from jina.serve.networking import _NetworkingHistograms @pytest.fixture def metrics_setup() -> Tuple[InMemoryMetricReader, MeterProvider]: metric_reader = InMemoryMetricReader() meter_provider = MeterProvider(metric_readers=[metric_reader]) meter = meter_provider.get_meter('test') yield metric_reader, meter if hasattr(meter_provider, 'force_flush'): metric_reader.force_flush() if hasattr(meter_provider, 'shutdown'): meter_provider.shutdown() def test_get_labels(): a: _NetworkingHistograms = _NetworkingHistograms() assert a._get_labels() == None HIST_LABELS = { 'a': 1, 'b': 2, } a.histogram_metric_labels = HIST_LABELS assert a._get_labels() == HIST_LABELS ADD_LABELS = { 'b': 3, 'c': 4, } assert a._get_labels(ADD_LABELS) == {**HIST_LABELS, **ADD_LABELS} def test_recording_methods(metrics_setup: Tuple[InMemoryMetricReader, Meter]): metric_reader, meter = metrics_setup a: _NetworkingHistograms = _NetworkingHistograms( sending_requests_time_metrics=meter.create_histogram("request_time"), send_requests_bytes_metrics=meter.create_histogram("request_bytes"), received_response_bytes=meter.create_histogram("response_bytes"), histogram_metric_labels=None, ) a.record_sending_requests_time_metrics(10) a.record_send_requests_bytes_metrics(20) a.record_received_response_bytes(30) histogram_metrics: List[Metric] = ( metric_reader.get_metrics_data().resource_metrics[0].scope_metrics[0].metrics ) data_points_sums: Dict[str, HistogramDataPoint] = { hist.name: next(iter(hist.data.data_points)).sum for hist in histogram_metrics } assert data_points_sums == { 'request_time': 10, 'request_bytes': 20, 'response_bytes': 30, }
""" Computes embeddings """ import numpy as np from sentence_transformers import SentenceTransformer def test_encode_token_embeddings(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: """ Test that encode(output_value='token_embeddings') works :return: """ model = paraphrase_distilroberta_base_v1_model sent = [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", "Sentences", "Sentence five five five five five five five", ] emb = model.encode(sent, output_value="token_embeddings", batch_size=2) assert len(emb) == len(sent) for s, e in zip(sent, emb): assert len(model.tokenize([s])["input_ids"][0]) == e.shape[0] def test_encode_single_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Single sentence emb = model.encode("Hello Word, a test sentence") assert emb.shape == (768,) assert abs(np.sum(emb) - 7.9811716) < 0.001 # Single sentence as list emb = model.encode(["Hello Word, a test sentence"]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 7.9811716) < 0.001 # Sentence list emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 22.968266) < 0.001 def test_encode_normalize(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ], normalize_embeddings=True, ) assert emb.shape == (3, 768) for norm in np.linalg.norm(emb, axis=1): assert abs(norm - 1) < 0.001 def test_encode_tuple_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Input a sentence tuple emb = model.encode([("Hello Word, a test sentence", "Second input for model")]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 9.503508) < 0.001 # List of sentence tuples emb = model.encode( [ ("Hello Word, a test sentence", "Second input for model"), ("My second tuple", "With two inputs"), ("Final tuple", "final test"), ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 32.14627) < 0.001
""" Computes embeddings """ import numpy as np from sentence_transformers import SentenceTransformer def test_encode_token_embeddings(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: """ Test that encode(output_value='token_embeddings') works :return: """ model = paraphrase_distilroberta_base_v1_model sent = [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", "Sentences", "Sentence five five five five five five five", ] emb = model.encode(sent, output_value="token_embeddings", batch_size=2) assert len(emb) == len(sent) for s, e in zip(sent, emb): assert len(model.tokenize([s])["input_ids"][0]) == e.shape[0] def test_encode_single_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Single sentence emb = model.encode("Hello Word, a test sentence") assert emb.shape == (768,) assert abs(np.sum(emb) - 7.9811716) < 0.001 # Single sentence as list emb = model.encode(["Hello Word, a test sentence"]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 7.9811716) < 0.001 # Sentence list emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 22.968266) < 0.001 def test_encode_normalize(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model emb = model.encode( [ "Hello Word, a test sentence", "Here comes another sentence", "My final sentence", ], normalize_embeddings=True, ) assert emb.shape == (3, 768) for norm in np.linalg.norm(emb, axis=1): assert abs(norm - 1) < 0.001 def test_encode_tuple_sentences(paraphrase_distilroberta_base_v1_model: SentenceTransformer) -> None: model = paraphrase_distilroberta_base_v1_model # Input a sentence tuple emb = model.encode([("Hello Word, a test sentence", "Second input for model")]) assert emb.shape == (1, 768) assert abs(np.sum(emb) - 9.503508) < 0.001 # List of sentence tuples emb = model.encode( [ ("Hello Word, a test sentence", "Second input for model"), ("My second tuple", "With two inputs"), ("Final tuple", "final test"), ] ) assert emb.shape == (3, 768) assert abs(np.sum(emb) - 32.14627) < 0.001
"""Download llama-pack as template.""" import logging import os import subprocess import sys from importlib import util from pathlib import Path from typing import Any, Optional, Union import requests from llama_index.core.download.utils import ( ChangeDirectory, get_file_content, initialize_directory, get_source_files_recursive, ) LLAMA_PACKS_CONTENTS_URL = ( "https://raw.githubusercontent.com/run-llama/llama_index/main/llama-index-packs" ) LLAMA_PACKS_SOURCE_FILES_GITHUB_TREE_URL = ( "https://github.com/run-llama/llama_index/tree/main" ) PY_NAMESPACE = "llama_index/packs" PATH_TYPE = Union[str, Path] LLAMAHUB_ANALYTICS_PROXY_SERVER = "https://llamahub.ai/api/analytics/downloads" logger = logging.getLogger(__name__) def download_module_and_reqs( local_dir_path: PATH_TYPE, remote_dir_path: PATH_TYPE, remote_source_dir_path: PATH_TYPE, package: str, sub_module: str, refresh_cache: bool = False, ) -> None: """Load module.""" if isinstance(local_dir_path, str): local_dir_path = Path(local_dir_path) module_path = f"{local_dir_path}/{PY_NAMESPACE}/{sub_module}" if refresh_cache or not os.path.exists(module_path): os.makedirs(module_path, exist_ok=True) # download all source files source_files = get_source_files_recursive( str(remote_source_dir_path), f"/llama-index-packs/{package}/{PY_NAMESPACE}/{sub_module}", ) for source_file in source_files: source_file_raw_content, _ = get_file_content( str(remote_dir_path), f"{source_file}", ) local_source_file_path = ( f"{local_dir_path}/{'/'.join(source_file.split('/')[2:])}" ) # ensure parent dir of file exists Path(local_source_file_path).parent.absolute().mkdir( parents=True, exist_ok=True ) with open(local_source_file_path, "w") as f: f.write(source_file_raw_content) # pyproject.toml and README pyproject_toml_path = f"{local_dir_path}/pyproject.toml" readme_path = ( f"{local_dir_path}/README.md" # needed to install deps from pyproject.toml ) if not os.path.exists(pyproject_toml_path): # NOTE: need to check the status code response_txt, status_code = get_file_content( str(remote_dir_path), f"/{package}/pyproject.toml" ) if status_code == 200: with open(pyproject_toml_path, "w") as f: f.write(response_txt) if not os.path.exists(readme_path): with open(readme_path, "w") as f: f.write( "DO NOT DELETE\nThis readme file is needed to install from pyproject.toml." ) # Install dependencies if os.path.exists(pyproject_toml_path): with ChangeDirectory(str(local_dir_path)): subprocess.check_call([sys.executable, "-m", "pip", "install", "."]) def download_llama_pack_template( new_install_parent: str, llama_pack_class: str, llama_pack_url: str = LLAMA_PACKS_CONTENTS_URL, llama_pack_source_files_dir_path: str = LLAMA_PACKS_SOURCE_FILES_GITHUB_TREE_URL, refresh_cache: bool = False, custom_dir: Optional[str] = None, custom_path: Optional[str] = None, base_file_name: str = "__init__.py", ) -> Any: # create directory / get path dirpath = initialize_directory(custom_path=custom_path, custom_dir=custom_dir) sub_module = new_install_parent.replace("llama-index-packs-", "").replace("-", "_") # download the module, install requirements download_module_and_reqs( local_dir_path=dirpath, remote_dir_path=llama_pack_url, remote_source_dir_path=llama_pack_source_files_dir_path, package=new_install_parent, sub_module=sub_module, refresh_cache=refresh_cache, ) # loads the module into memory path = f"{dirpath}/{PY_NAMESPACE}/{sub_module}/{base_file_name}" spec = util.spec_from_file_location("llama_index.packs._custom", location=path) if spec is None: raise ValueError(f"Could not find file: {path}.") module = util.module_from_spec(spec) spec.loader.exec_module(module) # type: ignore return getattr(module, llama_pack_class) def track_download(module_class: str, module_type: str) -> None: """ Tracks number of downloads via Llamahub proxy. Args: module_class: The name of the llama module being downloaded, e.g.,`GmailOpenAIAgentPack`. module_type: Can be "loader", "tool", "llamapack", or "datasets" """ try: requests.post( LLAMAHUB_ANALYTICS_PROXY_SERVER, json={"type": module_type, "plugin": module_class}, ) except Exception as e: logger.info(f"Error tracking downloads for {module_class} : {e}")
"""Download llama-pack as template.""" import logging import os import subprocess import sys from importlib import util from pathlib import Path from typing import Any, Optional, Union import requests from llama_index.core.download.utils import ( ChangeDirectory, get_file_content, initialize_directory, get_source_files_recursive, ) LLAMA_PACKS_CONTENTS_URL = ( "https://raw.githubusercontent.com/run-llama/llama_index/main/llama-index-packs" ) LLAMA_PACKS_SOURCE_FILES_GITHUB_TREE_URL = ( "https://github.com/run-llama/llama_index/tree/main" ) PY_NAMESPACE = "llama_index/packs" PATH_TYPE = Union[str, Path] LLAMAHUB_ANALYTICS_PROXY_SERVER = "https://llamahub.ai/api/analytics/downloads" logger = logging.getLogger(__name__) def download_module_and_reqs( local_dir_path: PATH_TYPE, remote_dir_path: PATH_TYPE, remote_source_dir_path: PATH_TYPE, package: str, sub_module: str, refresh_cache: bool = False, ) -> None: """Load module.""" if isinstance(local_dir_path, str): local_dir_path = Path(local_dir_path) module_path = f"{local_dir_path}/{PY_NAMESPACE}/{sub_module}" if refresh_cache or not os.path.exists(module_path): os.makedirs(module_path, exist_ok=True) # download all source files source_files = get_source_files_recursive( str(remote_source_dir_path), f"/llama-index-packs/{package}/{PY_NAMESPACE}/{sub_module}", ) for source_file in source_files: source_file_raw_content, _ = get_file_content( str(remote_dir_path), f"{source_file}", ) local_source_file_path = ( f"{local_dir_path}/{'/'.join(source_file.split('/')[2:])}" ) # ensure parent dir of file exists Path(local_source_file_path).parent.absolute().mkdir( parents=True, exist_ok=True ) with open(local_source_file_path, "w") as f: f.write(source_file_raw_content) # pyproject.toml and README pyproject_toml_path = f"{local_dir_path}/pyproject.toml" readme_path = ( f"{local_dir_path}/README.md" # needed to install deps from pyproject.toml ) if not os.path.exists(pyproject_toml_path): # NOTE: need to check the status code response_txt, status_code = get_file_content( str(remote_dir_path), f"/{package}/pyproject.toml" ) if status_code == 200: with open(pyproject_toml_path, "w") as f: f.write(response_txt) if not os.path.exists(readme_path): with open(readme_path, "w") as f: f.write( "DO NOT DELETE\nThis readme file is needed to install from pyproject.toml." ) # Install dependencies if os.path.exists(pyproject_toml_path): with ChangeDirectory(str(local_dir_path)): subprocess.check_call([sys.executable, "-m", "pip", "install", "."]) def download_llama_pack_template( new_install_parent: str, llama_pack_class: str, llama_pack_url: str = LLAMA_PACKS_CONTENTS_URL, llama_pack_source_files_dir_path: str = LLAMA_PACKS_SOURCE_FILES_GITHUB_TREE_URL, refresh_cache: bool = False, custom_dir: Optional[str] = None, custom_path: Optional[str] = None, base_file_name: str = "__init__.py", ) -> Any: # create directory / get path dirpath = initialize_directory(custom_path=custom_path, custom_dir=custom_dir) sub_module = new_install_parent.replace("llama-index-packs-", "").replace("-", "_") # download the module, install requirements download_module_and_reqs( local_dir_path=dirpath, remote_dir_path=llama_pack_url, remote_source_dir_path=llama_pack_source_files_dir_path, package=new_install_parent, sub_module=sub_module, refresh_cache=refresh_cache, ) # loads the module into memory path = f"{dirpath}/{PY_NAMESPACE}/{sub_module}/{base_file_name}" spec = util.spec_from_file_location("llama_index.packs._custom", location=path) if spec is None: raise ValueError(f"Could not find file: {path}.") module = util.module_from_spec(spec) spec.loader.exec_module(module) # type: ignore return getattr(module, llama_pack_class) def track_download(module_class: str, module_type: str) -> None: """Tracks number of downloads via Llamahub proxy. Args: module_class: The name of the llama module being downloaded, e.g.,`GmailOpenAIAgentPack`. module_type: Can be "loader", "tool", "llamapack", or "datasets" """ try: requests.post( LLAMAHUB_ANALYTICS_PROXY_SERVER, json={"type": module_type, "plugin": module_class}, ) except Exception as e: logger.info(f"Error tracking downloads for {module_class} : {e}")
# Licensed to the LF AI & Data foundation under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING, Any, Type, TypeVar, Union from uuid import UUID from pydantic import parse_obj_as from docarray.typing.proto_register import _register_proto from docarray.utils._internal.pydantic import is_pydantic_v2 if TYPE_CHECKING: from docarray.proto import NodeProto from docarray.typing.abstract_type import AbstractType if is_pydantic_v2: from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler from pydantic.json_schema import JsonSchemaValue from pydantic_core import core_schema T = TypeVar('T', bound='ID') @_register_proto(proto_type_name='id') class ID(str, AbstractType): """ Represent an unique ID """ @classmethod def _docarray_validate( cls: Type[T], value: Union[str, int, UUID], ) -> T: try: id: str = str(value) return cls(id) except Exception: raise ValueError(f'Expected a str, int or UUID, got {type(value)}') def _to_node_protobuf(self) -> 'NodeProto': """Convert an ID into a NodeProto message. This function should be called when the self is nested into another Document that need to be converted into a protobuf :return: the nested item protobuf message """ from docarray.proto import NodeProto return NodeProto(text=self, type=self._proto_type_name) @classmethod def from_protobuf(cls: Type[T], pb_msg: 'str') -> T: """ read ndarray from a proto msg :param pb_msg: :return: a string """ return parse_obj_as(cls, pb_msg) if is_pydantic_v2: @classmethod def __get_pydantic_core_schema__( cls, source: Type[Any], handler: 'GetCoreSchemaHandler' ) -> core_schema.CoreSchema: return core_schema.with_info_plain_validator_function( cls.validate, ) @classmethod def __get_pydantic_json_schema__( cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler ) -> JsonSchemaValue: field_schema: dict[str, Any] = {} field_schema.update(type='string') return field_schema
# Licensed to the LF AI & Data foundation under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import TYPE_CHECKING, Any, Type, TypeVar, Union from uuid import UUID from pydantic import parse_obj_as from docarray.typing.proto_register import _register_proto from docarray.utils._internal.pydantic import is_pydantic_v2 if TYPE_CHECKING: from docarray.proto import NodeProto from docarray.typing.abstract_type import AbstractType if is_pydantic_v2: from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler from pydantic.json_schema import JsonSchemaValue from pydantic_core import core_schema T = TypeVar('T', bound='ID') @_register_proto(proto_type_name='id') class ID(str, AbstractType): """ Represent an unique ID """ @classmethod def _docarray_validate( cls: Type[T], value: Union[str, int, UUID], ) -> T: try: id: str = str(value) return cls(id) except Exception: raise ValueError(f'Expected a str, int or UUID, got {type(value)}') def _to_node_protobuf(self) -> 'NodeProto': """Convert an ID into a NodeProto message. This function should be called when the self is nested into another Document that need to be converted into a protobuf :return: the nested item protobuf message """ from docarray.proto import NodeProto return NodeProto(text=self, type=self._proto_type_name) @classmethod def from_protobuf(cls: Type[T], pb_msg: 'str') -> T: """ read ndarray from a proto msg :param pb_msg: :return: a string """ return parse_obj_as(cls, pb_msg) if is_pydantic_v2: @classmethod def __get_pydantic_core_schema__( cls, source: Type[Any], handler: 'GetCoreSchemaHandler' ) -> core_schema.CoreSchema: return core_schema.general_plain_validator_function( cls.validate, ) @classmethod def __get_pydantic_json_schema__( cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler ) -> JsonSchemaValue: field_schema: dict[str, Any] = {} field_schema.update(type='string') return field_schema
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] # model settings norm_cfg = dict(type='GN', num_groups=32, requires_grad=True) model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( _delete_=True, type='SABLRetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, approx_anchor_generator=dict( type='AnchorGenerator', octave_base_scale=4, scales_per_octave=3, ratios=[0.5, 1.0, 2.0], strides=[8, 16, 32, 64, 128]), square_anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], scales=[4], strides=[8, 16, 32, 64, 128]), norm_cfg=norm_cfg, bbox_coder=dict( type='BucketingBBoxCoder', num_buckets=14, scale_factor=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.5), loss_bbox_reg=dict( type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.5)), # training and testing settings train_cfg=dict( assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False)) # optimizer optim_wrapper = dict( optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001))
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] # model settings norm_cfg = dict(type='GN', num_groups=32, requires_grad=True) model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( _delete_=True, type='SABLRetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, approx_anchor_generator=dict( type='AnchorGenerator', octave_base_scale=4, scales_per_octave=3, ratios=[0.5, 1.0, 2.0], strides=[8, 16, 32, 64, 128]), square_anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], scales=[4], strides=[8, 16, 32, 64, 128]), norm_cfg=norm_cfg, bbox_coder=dict( type='BucketingBBoxCoder', num_buckets=14, scale_factor=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.5), loss_bbox_reg=dict( type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.5)), # training and testing settings train_cfg=dict( assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False)) # optimizer optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)
import logging from typing import Any from backend.data.block import ( Block, BlockCategory, BlockInput, BlockOutput, BlockSchema, BlockType, get_block, ) from backend.data.execution import ExecutionStatus from backend.data.model import SchemaField from backend.util import json logger = logging.getLogger(__name__) class AgentExecutorBlock(Block): class Input(BlockSchema): user_id: str = SchemaField(description="User ID") graph_id: str = SchemaField(description="Graph ID") graph_version: int = SchemaField(description="Graph Version") data: BlockInput = SchemaField(description="Input data for the graph") input_schema: dict = SchemaField(description="Input schema for the graph") output_schema: dict = SchemaField(description="Output schema for the graph") @classmethod def get_input_schema(cls, data: BlockInput) -> dict[str, Any]: return data.get("input_schema", {}) @classmethod def get_input_defaults(cls, data: BlockInput) -> BlockInput: return data.get("data", {}) @classmethod def get_missing_input(cls, data: BlockInput) -> set[str]: required_fields = cls.get_input_schema(data).get("required", []) return set(required_fields) - set(data) @classmethod def get_mismatch_error(cls, data: BlockInput) -> str | None: return json.validate_with_jsonschema(cls.get_input_schema(data), data) class Output(BlockSchema): pass def __init__(self): super().__init__( id="e189baac-8c20-45a1-94a7-55177ea42565", description="Executes an existing agent inside your agent", input_schema=AgentExecutorBlock.Input, output_schema=AgentExecutorBlock.Output, block_type=BlockType.AGENT, categories={BlockCategory.AGENT}, ) def run(self, input_data: Input, **kwargs) -> BlockOutput: from backend.data.execution import ExecutionEventType from backend.executor import utils as execution_utils event_bus = execution_utils.get_execution_event_bus() graph_exec = execution_utils.add_graph_execution( graph_id=input_data.graph_id, graph_version=input_data.graph_version, user_id=input_data.user_id, data=input_data.data, ) log_id = f"Graph #{input_data.graph_id}-V{input_data.graph_version}, exec-id: {graph_exec.graph_exec_id}" logger.info(f"Starting execution of {log_id}") for event in event_bus.listen( user_id=graph_exec.user_id, graph_id=graph_exec.graph_id, graph_exec_id=graph_exec.graph_exec_id, ): if event.event_type == ExecutionEventType.GRAPH_EXEC_UPDATE: if event.status in [ ExecutionStatus.COMPLETED, ExecutionStatus.TERMINATED, ExecutionStatus.FAILED, ]: logger.info(f"Execution {log_id} ended with status {event.status}") break else: continue logger.debug( f"Execution {log_id} produced input {event.input_data} output {event.output_data}" ) if not event.block_id: logger.warning(f"{log_id} received event without block_id {event}") continue block = get_block(event.block_id) if not block or block.block_type != BlockType.OUTPUT: continue output_name = event.input_data.get("name") if not output_name: logger.warning(f"{log_id} produced an output with no name {event}") continue for output_data in event.output_data.get("output", []): logger.debug( f"Execution {log_id} produced {output_name}: {output_data}" ) yield output_name, output_data
import logging from typing import Any from backend.data.block import ( Block, BlockCategory, BlockInput, BlockOutput, BlockSchema, BlockType, get_block, ) from backend.data.execution import ExecutionStatus from backend.data.model import SchemaField from backend.util import json logger = logging.getLogger(__name__) class AgentExecutorBlock(Block): class Input(BlockSchema): user_id: str = SchemaField(description="User ID") graph_id: str = SchemaField(description="Graph ID") graph_version: int = SchemaField(description="Graph Version") data: BlockInput = SchemaField(description="Input data for the graph") input_schema: dict = SchemaField(description="Input schema for the graph") output_schema: dict = SchemaField(description="Output schema for the graph") @classmethod def get_input_schema(cls, data: BlockInput) -> dict[str, Any]: return data.get("input_schema", {}) @classmethod def get_input_defaults(cls, data: BlockInput) -> BlockInput: return data.get("data", {}) @classmethod def get_missing_input(cls, data: BlockInput) -> set[str]: required_fields = cls.get_input_schema(data).get("required", []) return set(required_fields) - set(data) @classmethod def get_mismatch_error(cls, data: BlockInput) -> str | None: return json.validate_with_jsonschema(cls.get_input_schema(data), data) class Output(BlockSchema): pass def __init__(self): super().__init__( id="e189baac-8c20-45a1-94a7-55177ea42565", description="Executes an existing agent inside your agent", input_schema=AgentExecutorBlock.Input, output_schema=AgentExecutorBlock.Output, block_type=BlockType.AGENT, categories={BlockCategory.AGENT}, ) def run(self, input_data: Input, **kwargs) -> BlockOutput: from backend.data.execution import ExecutionEventType from backend.executor import utils as execution_utils event_bus = execution_utils.get_execution_event_bus() graph_exec = execution_utils.add_graph_execution( graph_id=input_data.graph_id, graph_version=input_data.graph_version, user_id=input_data.user_id, data=input_data.data, ) log_id = f"Graph #{input_data.graph_id}-V{input_data.graph_version}, exec-id: {graph_exec.graph_exec_id}" logger.info(f"Starting execution of {log_id}") for event in event_bus.listen( user_id=graph_exec.user_id, graph_id=graph_exec.graph_id, graph_exec_id=graph_exec.graph_exec_id, ): if event.event_type == ExecutionEventType.GRAPH_EXEC_UPDATE: if event.status in [ ExecutionStatus.COMPLETED, ExecutionStatus.TERMINATED, ExecutionStatus.FAILED, ]: logger.info(f"Execution {log_id} ended with status {event.status}") break else: continue logger.info( f"Execution {log_id} produced input {event.input_data} output {event.output_data}" ) if not event.block_id: logger.warning(f"{log_id} received event without block_id {event}") continue block = get_block(event.block_id) if not block or block.block_type != BlockType.OUTPUT: continue output_name = event.input_data.get("name") if not output_name: logger.warning(f"{log_id} produced an output with no name {event}") continue for output_data in event.output_data.get("output", []): logger.info(f"Execution {log_id} produced {output_name}: {output_data}") yield output_name, output_data
import pathlib from typing import Any, Callable, Optional, Union from .folder import default_loader from .utils import verify_str_arg from .vision import VisionDataset class StanfordCars(VisionDataset): """Stanford Cars Dataset The Cars dataset contains 16,185 images of 196 classes of cars. The data is split into 8,144 training images and 8,041 testing images, where each class has been split roughly in a 50-50 split The original URL is https://ai.stanford.edu/~jkrause/cars/car_dataset.html, the dataset isn't available online anymore. .. note:: This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format. Args: root (str or ``pathlib.Path``): Root directory of dataset split (string, optional): The dataset split, supports ``"train"`` (default) or ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader, and returns a transformed version. E.g, ``transforms.RandomCrop`` target_transform (callable, optional): A function/transform that takes in the target and transforms it. download (bool, optional): This parameter exists for backward compatibility but it does not download the dataset, since the original URL is not available anymore. loader (callable, optional): A function to load an image given its path. By default, it uses PIL as its image loader, but users could also pass in ``torchvision.io.decode_image`` for decoding image data into tensors directly. """ def __init__( self, root: Union[str, pathlib.Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False, loader: Callable[[str], Any] = default_loader, ) -> None: try: import scipy.io as sio except ImportError: raise RuntimeError("Scipy is not found. This dataset needs to have scipy installed: pip install scipy") super().__init__(root, transform=transform, target_transform=target_transform) self._split = verify_str_arg(split, "split", ("train", "test")) self._base_folder = pathlib.Path(root) / "stanford_cars" devkit = self._base_folder / "devkit" if self._split == "train": self._annotations_mat_path = devkit / "cars_train_annos.mat" self._images_base_path = self._base_folder / "cars_train" else: self._annotations_mat_path = self._base_folder / "cars_test_annos_withlabels.mat" self._images_base_path = self._base_folder / "cars_test" if download: self.download() if not self._check_exists(): raise RuntimeError("Dataset not found.") self._samples = [ ( str(self._images_base_path / annotation["fname"]), annotation["class"] - 1, # Original target mapping starts from 1, hence -1 ) for annotation in sio.loadmat(self._annotations_mat_path, squeeze_me=True)["annotations"] ] self.classes = sio.loadmat(str(devkit / "cars_meta.mat"), squeeze_me=True)["class_names"].tolist() self.class_to_idx = {cls: i for i, cls in enumerate(self.classes)} self.loader = loader def __len__(self) -> int: return len(self._samples) def __getitem__(self, idx: int) -> tuple[Any, Any]: """Returns pil_image and class_id for given index""" image_path, target = self._samples[idx] image = self.loader(image_path) if self.transform is not None: image = self.transform(image) if self.target_transform is not None: target = self.target_transform(target) return image, target def _check_exists(self) -> bool: if not (self._base_folder / "devkit").is_dir(): return False return self._annotations_mat_path.exists() and self._images_base_path.is_dir() def download(self): raise ValueError("The original URL is broken so the StanfordCars dataset cannot be downloaded anymore.")
import pathlib from typing import Any, Callable, Optional, Tuple, Union from .folder import default_loader from .utils import verify_str_arg from .vision import VisionDataset class StanfordCars(VisionDataset): """Stanford Cars Dataset The Cars dataset contains 16,185 images of 196 classes of cars. The data is split into 8,144 training images and 8,041 testing images, where each class has been split roughly in a 50-50 split The original URL is https://ai.stanford.edu/~jkrause/cars/car_dataset.html, the dataset isn't available online anymore. .. note:: This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format. Args: root (str or ``pathlib.Path``): Root directory of dataset split (string, optional): The dataset split, supports ``"train"`` (default) or ``"test"``. transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader, and returns a transformed version. E.g, ``transforms.RandomCrop`` target_transform (callable, optional): A function/transform that takes in the target and transforms it. download (bool, optional): This parameter exists for backward compatibility but it does not download the dataset, since the original URL is not available anymore. loader (callable, optional): A function to load an image given its path. By default, it uses PIL as its image loader, but users could also pass in ``torchvision.io.decode_image`` for decoding image data into tensors directly. """ def __init__( self, root: Union[str, pathlib.Path], split: str = "train", transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False, loader: Callable[[str], Any] = default_loader, ) -> None: try: import scipy.io as sio except ImportError: raise RuntimeError("Scipy is not found. This dataset needs to have scipy installed: pip install scipy") super().__init__(root, transform=transform, target_transform=target_transform) self._split = verify_str_arg(split, "split", ("train", "test")) self._base_folder = pathlib.Path(root) / "stanford_cars" devkit = self._base_folder / "devkit" if self._split == "train": self._annotations_mat_path = devkit / "cars_train_annos.mat" self._images_base_path = self._base_folder / "cars_train" else: self._annotations_mat_path = self._base_folder / "cars_test_annos_withlabels.mat" self._images_base_path = self._base_folder / "cars_test" if download: self.download() if not self._check_exists(): raise RuntimeError("Dataset not found.") self._samples = [ ( str(self._images_base_path / annotation["fname"]), annotation["class"] - 1, # Original target mapping starts from 1, hence -1 ) for annotation in sio.loadmat(self._annotations_mat_path, squeeze_me=True)["annotations"] ] self.classes = sio.loadmat(str(devkit / "cars_meta.mat"), squeeze_me=True)["class_names"].tolist() self.class_to_idx = {cls: i for i, cls in enumerate(self.classes)} self.loader = loader def __len__(self) -> int: return len(self._samples) def __getitem__(self, idx: int) -> Tuple[Any, Any]: """Returns pil_image and class_id for given index""" image_path, target = self._samples[idx] image = self.loader(image_path) if self.transform is not None: image = self.transform(image) if self.target_transform is not None: target = self.target_transform(target) return image, target def _check_exists(self) -> bool: if not (self._base_folder / "devkit").is_dir(): return False return self._annotations_mat_path.exists() and self._images_base_path.is_dir() def download(self): raise ValueError("The original URL is broken so the StanfordCars dataset cannot be downloaded anymore.")
from typing import Any, Optional from typing_extensions import override from langchain_core.caches import RETURN_VAL_TYPE, BaseCache from langchain_core.globals import set_llm_cache from langchain_core.language_models import FakeListLLM class InMemoryCache(BaseCache): """In-memory cache used for testing purposes.""" def __init__(self) -> None: """Initialize with empty cache.""" self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]: """Look up based on prompt and llm_string.""" return self._cache.get((prompt, llm_string), None) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: """Update cache based on prompt and llm_string.""" self._cache[(prompt, llm_string)] = return_val @override def clear(self, **kwargs: Any) -> None: """Clear cache.""" self._cache = {} async def test_local_cache_generate_async() -> None: global_cache = InMemoryCache() local_cache = InMemoryCache() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=local_cache, responses=["foo", "bar"]) output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "foo" output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "foo" assert global_cache._cache == {} assert len(local_cache._cache) == 1 finally: set_llm_cache(None) def test_local_cache_generate_sync() -> None: global_cache = InMemoryCache() local_cache = InMemoryCache() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=local_cache, responses=["foo", "bar"]) output = llm.generate(["foo"]) assert output.generations[0][0].text == "foo" output = llm.generate(["foo"]) assert output.generations[0][0].text == "foo" assert global_cache._cache == {} assert len(local_cache._cache) == 1 finally: set_llm_cache(None) class InMemoryCacheBad(BaseCache): """In-memory cache used for testing purposes.""" def __init__(self) -> None: """Initialize with empty cache.""" self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]: """Look up based on prompt and llm_string.""" msg = "This code should not be triggered" raise NotImplementedError(msg) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: """Update cache based on prompt and llm_string.""" msg = "This code should not be triggered" raise NotImplementedError(msg) @override def clear(self, **kwargs: Any) -> None: """Clear cache.""" self._cache = {} def test_no_cache_generate_sync() -> None: global_cache = InMemoryCacheBad() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=False, responses=["foo", "bar"]) output = llm.generate(["foo"]) assert output.generations[0][0].text == "foo" output = llm.generate(["foo"]) assert output.generations[0][0].text == "bar" assert global_cache._cache == {} finally: set_llm_cache(None) async def test_no_cache_generate_async() -> None: global_cache = InMemoryCacheBad() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=False, responses=["foo", "bar"]) output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "foo" output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "bar" assert global_cache._cache == {} finally: set_llm_cache(None)
from typing import Any, Optional from langchain_core.caches import RETURN_VAL_TYPE, BaseCache from langchain_core.globals import set_llm_cache from langchain_core.language_models import FakeListLLM class InMemoryCache(BaseCache): """In-memory cache used for testing purposes.""" def __init__(self) -> None: """Initialize with empty cache.""" self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]: """Look up based on prompt and llm_string.""" return self._cache.get((prompt, llm_string), None) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: """Update cache based on prompt and llm_string.""" self._cache[(prompt, llm_string)] = return_val def clear(self, **kwargs: Any) -> None: """Clear cache.""" self._cache = {} async def test_local_cache_generate_async() -> None: global_cache = InMemoryCache() local_cache = InMemoryCache() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=local_cache, responses=["foo", "bar"]) output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "foo" output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "foo" assert global_cache._cache == {} assert len(local_cache._cache) == 1 finally: set_llm_cache(None) def test_local_cache_generate_sync() -> None: global_cache = InMemoryCache() local_cache = InMemoryCache() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=local_cache, responses=["foo", "bar"]) output = llm.generate(["foo"]) assert output.generations[0][0].text == "foo" output = llm.generate(["foo"]) assert output.generations[0][0].text == "foo" assert global_cache._cache == {} assert len(local_cache._cache) == 1 finally: set_llm_cache(None) class InMemoryCacheBad(BaseCache): """In-memory cache used for testing purposes.""" def __init__(self) -> None: """Initialize with empty cache.""" self._cache: dict[tuple[str, str], RETURN_VAL_TYPE] = {} def lookup(self, prompt: str, llm_string: str) -> Optional[RETURN_VAL_TYPE]: """Look up based on prompt and llm_string.""" msg = "This code should not be triggered" raise NotImplementedError(msg) def update(self, prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None: """Update cache based on prompt and llm_string.""" msg = "This code should not be triggered" raise NotImplementedError(msg) def clear(self, **kwargs: Any) -> None: """Clear cache.""" self._cache = {} def test_no_cache_generate_sync() -> None: global_cache = InMemoryCacheBad() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=False, responses=["foo", "bar"]) output = llm.generate(["foo"]) assert output.generations[0][0].text == "foo" output = llm.generate(["foo"]) assert output.generations[0][0].text == "bar" assert global_cache._cache == {} finally: set_llm_cache(None) async def test_no_cache_generate_async() -> None: global_cache = InMemoryCacheBad() try: set_llm_cache(global_cache) llm = FakeListLLM(cache=False, responses=["foo", "bar"]) output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "foo" output = await llm.agenerate(["foo"]) assert output.generations[0][0].text == "bar" assert global_cache._cache == {} finally: set_llm_cache(None)
import numpy as np import pytest from pydantic import parse_obj_as from docarray.base_document.document import BaseDocument from docarray.documents import Mesh3D from tests import TOYDATA_DIR LOCAL_OBJ_FILE = str(TOYDATA_DIR / 'tetrahedron.obj') REMOTE_OBJ_FILE = 'https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj' @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize('file_url', [LOCAL_OBJ_FILE, REMOTE_OBJ_FILE]) def test_mesh(file_url): mesh = Mesh3D(url=file_url) mesh.tensors = mesh.url.load() assert isinstance(mesh.tensors.vertices, np.ndarray) assert isinstance(mesh.tensors.faces, np.ndarray) def test_str_init(): t = parse_obj_as(Mesh3D, 'http://hello.ply') assert t.url == 'http://hello.ply' def test_doc(): class MyDoc(BaseDocument): mesh1: Mesh3D mesh2: Mesh3D doc = MyDoc(mesh1='http://hello.ply', mesh2=Mesh3D(url='http://hello.ply')) assert doc.mesh1.url == 'http://hello.ply' assert doc.mesh2.url == 'http://hello.ply'
import numpy as np import pytest from pydantic import parse_obj_as from docarray import BaseDocument from docarray.documents import Mesh3D from tests import TOYDATA_DIR LOCAL_OBJ_FILE = str(TOYDATA_DIR / 'tetrahedron.obj') REMOTE_OBJ_FILE = 'https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj' @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize('file_url', [LOCAL_OBJ_FILE, REMOTE_OBJ_FILE]) def test_mesh(file_url): mesh = Mesh3D(url=file_url) mesh.vertices, mesh.faces = mesh.url.load() assert isinstance(mesh.vertices, np.ndarray) assert isinstance(mesh.faces, np.ndarray) def test_str_init(): t = parse_obj_as(Mesh3D, 'http://hello.ply') assert t.url == 'http://hello.ply' def test_doc(): class MyDoc(BaseDocument): mesh1: Mesh3D mesh2: Mesh3D doc = MyDoc(mesh1='http://hello.ply', mesh2=Mesh3D(url='http://hello.ply')) assert doc.mesh1.url == 'http://hello.ply' assert doc.mesh2.url == 'http://hello.ply'
from __future__ import annotations from typing import Union, Sequence, Literal import torch import torch.fft from torch.fft import * # noqa: F403 from ._typing import Array # Several torch fft functions do not map axes to dim def fftn( x: Array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> Array: return torch.fft.fftn(x, s=s, dim=axes, norm=norm, **kwargs) def ifftn( x: Array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> Array: return torch.fft.ifftn(x, s=s, dim=axes, norm=norm, **kwargs) def rfftn( x: Array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> Array: return torch.fft.rfftn(x, s=s, dim=axes, norm=norm, **kwargs) def irfftn( x: Array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> Array: return torch.fft.irfftn(x, s=s, dim=axes, norm=norm, **kwargs) def fftshift( x: Array, /, *, axes: Union[int, Sequence[int]] = None, **kwargs, ) -> Array: return torch.fft.fftshift(x, dim=axes, **kwargs) def ifftshift( x: Array, /, *, axes: Union[int, Sequence[int]] = None, **kwargs, ) -> Array: return torch.fft.ifftshift(x, dim=axes, **kwargs) __all__ = torch.fft.__all__ + [ "fftn", "ifftn", "rfftn", "irfftn", "fftshift", "ifftshift", ] _all_ignore = ['torch']
from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: import torch array = torch.Tensor from typing import Union, Sequence, Literal from torch.fft import * # noqa: F403 import torch.fft # Several torch fft functions do not map axes to dim def fftn( x: array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> array: return torch.fft.fftn(x, s=s, dim=axes, norm=norm, **kwargs) def ifftn( x: array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> array: return torch.fft.ifftn(x, s=s, dim=axes, norm=norm, **kwargs) def rfftn( x: array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> array: return torch.fft.rfftn(x, s=s, dim=axes, norm=norm, **kwargs) def irfftn( x: array, /, *, s: Sequence[int] = None, axes: Sequence[int] = None, norm: Literal["backward", "ortho", "forward"] = "backward", **kwargs, ) -> array: return torch.fft.irfftn(x, s=s, dim=axes, norm=norm, **kwargs) def fftshift( x: array, /, *, axes: Union[int, Sequence[int]] = None, **kwargs, ) -> array: return torch.fft.fftshift(x, dim=axes, **kwargs) def ifftshift( x: array, /, *, axes: Union[int, Sequence[int]] = None, **kwargs, ) -> array: return torch.fft.ifftshift(x, dim=axes, **kwargs) __all__ = torch.fft.__all__ + [ "fftn", "ifftn", "rfftn", "irfftn", "fftshift", "ifftshift", ] _all_ignore = ['torch']
import functools import os import os.path import pathlib from typing import Any, BinaryIO, Collection, Dict, List, Optional, Tuple, Union from torchdata.datapipes.iter import FileLister, FileOpener, Filter, IterDataPipe, Mapper from torchvision.prototype.datapoints import Label from torchvision.prototype.datasets.utils import EncodedData, EncodedImage from torchvision.prototype.datasets.utils._internal import hint_sharding, hint_shuffling __all__ = ["from_data_folder", "from_image_folder"] def _is_not_top_level_file(path: str, *, root: pathlib.Path) -> bool: rel_path = pathlib.Path(path).relative_to(root) return rel_path.is_dir() or rel_path.parent != pathlib.Path(".") def _prepare_sample( data: Tuple[str, BinaryIO], *, root: pathlib.Path, categories: List[str], ) -> Dict[str, Any]: path, buffer = data category = pathlib.Path(path).relative_to(root).parts[0] return dict( path=path, data=EncodedData.from_file(buffer), label=Label.from_category(category, categories=categories), ) def from_data_folder( root: Union[str, pathlib.Path], *, valid_extensions: Optional[Collection[str]] = None, recursive: bool = True, ) -> Tuple[IterDataPipe, List[str]]: root = pathlib.Path(root).expanduser().resolve() categories = sorted(entry.name for entry in os.scandir(root) if entry.is_dir()) masks: Union[List[str], str] = [f"*.{ext}" for ext in valid_extensions] if valid_extensions is not None else "" dp = FileLister(str(root), recursive=recursive, masks=masks) dp: IterDataPipe = Filter(dp, functools.partial(_is_not_top_level_file, root=root)) dp = hint_sharding(dp) dp = hint_shuffling(dp) dp = FileOpener(dp, mode="rb") return Mapper(dp, functools.partial(_prepare_sample, root=root, categories=categories)), categories def _data_to_image_key(sample: Dict[str, Any]) -> Dict[str, Any]: sample["image"] = EncodedImage(sample.pop("data").data) return sample def from_image_folder( root: Union[str, pathlib.Path], *, valid_extensions: Collection[str] = ("jpg", "jpeg", "png", "ppm", "bmp", "pgm", "tif", "tiff", "webp"), **kwargs: Any, ) -> Tuple[IterDataPipe, List[str]]: valid_extensions = [valid_extension for ext in valid_extensions for valid_extension in (ext.lower(), ext.upper())] dp, categories = from_data_folder(root, valid_extensions=valid_extensions, **kwargs) return Mapper(dp, _data_to_image_key), categories
import functools import os import os.path import pathlib from typing import Any, BinaryIO, Collection, Dict, List, Optional, Tuple, Union from torchdata.datapipes.iter import FileLister, FileOpener, Filter, IterDataPipe, Mapper from torchvision.prototype.datasets.utils import EncodedData, EncodedImage from torchvision.prototype.datasets.utils._internal import hint_sharding, hint_shuffling from torchvision.prototype.features import Label __all__ = ["from_data_folder", "from_image_folder"] def _is_not_top_level_file(path: str, *, root: pathlib.Path) -> bool: rel_path = pathlib.Path(path).relative_to(root) return rel_path.is_dir() or rel_path.parent != pathlib.Path(".") def _prepare_sample( data: Tuple[str, BinaryIO], *, root: pathlib.Path, categories: List[str], ) -> Dict[str, Any]: path, buffer = data category = pathlib.Path(path).relative_to(root).parts[0] return dict( path=path, data=EncodedData.from_file(buffer), label=Label.from_category(category, categories=categories), ) def from_data_folder( root: Union[str, pathlib.Path], *, valid_extensions: Optional[Collection[str]] = None, recursive: bool = True, ) -> Tuple[IterDataPipe, List[str]]: root = pathlib.Path(root).expanduser().resolve() categories = sorted(entry.name for entry in os.scandir(root) if entry.is_dir()) masks: Union[List[str], str] = [f"*.{ext}" for ext in valid_extensions] if valid_extensions is not None else "" dp = FileLister(str(root), recursive=recursive, masks=masks) dp: IterDataPipe = Filter(dp, functools.partial(_is_not_top_level_file, root=root)) dp = hint_sharding(dp) dp = hint_shuffling(dp) dp = FileOpener(dp, mode="rb") return Mapper(dp, functools.partial(_prepare_sample, root=root, categories=categories)), categories def _data_to_image_key(sample: Dict[str, Any]) -> Dict[str, Any]: sample["image"] = EncodedImage(sample.pop("data").data) return sample def from_image_folder( root: Union[str, pathlib.Path], *, valid_extensions: Collection[str] = ("jpg", "jpeg", "png", "ppm", "bmp", "pgm", "tif", "tiff", "webp"), **kwargs: Any, ) -> Tuple[IterDataPipe, List[str]]: valid_extensions = [valid_extension for ext in valid_extensions for valid_extension in (ext.lower(), ext.upper())] dp, categories = from_data_folder(root, valid_extensions=valid_extensions, **kwargs) return Mapper(dp, _data_to_image_key), categories
#!/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/' files = sorted(glob.glob('../configs/*/README.md')) stats = [] titles = [] num_ckpts = 0 for f in files: url = osp.dirname(f.replace('../', 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/' files = sorted(glob.glob('../configs/*/README.md')) stats = [] titles = [] num_ckpts = 0 for f in files: url = osp.dirname(f.replace('../', 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)
_base_ = './sparse-rcnn_r50_fpn_ms-480-800-3x_coco.py' num_proposals = 300 model = dict( rpn_head=dict(num_proposals=num_proposals), test_cfg=dict( _delete_=True, rpn=None, rcnn=dict(max_per_img=num_proposals))) # augmentation strategy originates from DETR. train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict(type='RandomFlip', prob=0.5), dict( type='RandomChoice', transforms=[[ dict( type='RandomChoiceResize', scales=[(480, 1333), (512, 1333), (544, 1333), (576, 1333), (608, 1333), (640, 1333), (672, 1333), (704, 1333), (736, 1333), (768, 1333), (800, 1333)], keep_ratio=True) ], [ dict( type='RandomChoiceResize', scales=[(400, 1333), (500, 1333), (600, 1333)], keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=(384, 600), allow_negative_crop=True), dict( type='RandomChoiceResize', scales=[(480, 1333), (512, 1333), (544, 1333), (576, 1333), (608, 1333), (640, 1333), (672, 1333), (704, 1333), (736, 1333), (768, 1333), (800, 1333)], keep_ratio=True) ]]), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline))
_base_ = './sparse_rcnn_r50_fpn_mstrain_480-800_3x_coco.py' num_proposals = 300 model = dict( rpn_head=dict(num_proposals=num_proposals), test_cfg=dict( _delete_=True, rpn=None, rcnn=dict(max_per_img=num_proposals))) # augmentation strategy originates from DETR. train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict(type='RandomFlip', prob=0.5), dict( type='RandomChoice', transforms=[[ dict( type='RandomChoiceResize', scales=[(480, 1333), (512, 1333), (544, 1333), (576, 1333), (608, 1333), (640, 1333), (672, 1333), (704, 1333), (736, 1333), (768, 1333), (800, 1333)], keep_ratio=True) ], [ dict( type='RandomChoiceResize', scales=[(400, 1333), (500, 1333), (600, 1333)], keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=(384, 600), allow_negative_crop=True), dict( type='RandomChoiceResize', scales=[(480, 1333), (512, 1333), (544, 1333), (576, 1333), (608, 1333), (640, 1333), (672, 1333), (704, 1333), (736, 1333), (768, 1333), (800, 1333)], keep_ratio=True) ]]), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline))
from __future__ import annotations import re from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from sentence_transformers.SentenceTransformer import SentenceTransformer class SentenceEvaluator: """ Base class for all evaluators Extend this class and implement __call__ for custom evaluators. """ def __init__(self): """ Base class for all evaluators. Notably, this class introduces the ``greater_is_better`` and ``primary_metric`` attributes. The former is a boolean indicating whether a higher evaluation score is better, which is used for choosing the best checkpoint if ``load_best_model_at_end`` is set to ``True`` in the training arguments. The latter is a string indicating the primary metric for the evaluator. This has to be defined whenever the evaluator returns a dictionary of metrics, and the primary metric is the key pointing to the primary metric, i.e. the one that is used for model selection and/or logging. """ self.greater_is_better = True self.primary_metric = None def __call__( self, model: SentenceTransformer, output_path: str = None, epoch: int = -1, steps: int = -1 ) -> float | dict[str, float]: """ This is called during training to evaluate the model. It returns a score for the evaluation with a higher score indicating a better result. Args: model: the model to evaluate output_path: path where predictions and metrics are written to epoch: the epoch where the evaluation takes place. This is used for the file prefixes. If this is -1, then we assume evaluation on test data. steps: the steps in the current epoch at time of the evaluation. This is used for the file prefixes. If this is -1, then we assume evaluation at the end of the epoch. Returns: Either a score for the evaluation with a higher score indicating a better result, or a dictionary with scores. If the latter is chosen, then `evaluator.primary_metric` must be defined """ pass def prefix_name_to_metrics(self, metrics: dict[str, float], name: str) -> dict[str, float]: if not name: return metrics metrics = {name + "_" + key: float(value) for key, value in metrics.items()} if hasattr(self, "primary_metric") and not self.primary_metric.startswith(name + "_"): self.primary_metric = name + "_" + self.primary_metric return metrics def store_metrics_in_model_card_data( self, model: SentenceTransformer, metrics: dict[str, Any], epoch: int = 0, step: int = 0 ) -> None: model.model_card_data.set_evaluation_metrics(self, metrics, epoch, step) @property def description(self) -> str: """ Returns a human-readable description of the evaluator: BinaryClassificationEvaluator -> Binary Classification 1. Remove "Evaluator" from the class name 2. Add a space before every capital letter """ class_name = self.__class__.__name__ try: index = class_name.index("Evaluator") class_name = class_name[:index] except IndexError: pass return re.sub(r"([a-z])([A-Z])", r"\g<1> \g<2>", class_name)
from __future__ import annotations import re from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from sentence_transformers.SentenceTransformer import SentenceTransformer class SentenceEvaluator: """ Base class for all evaluators Extend this class and implement __call__ for custom evaluators. """ def __init__(self): """ Base class for all evaluators. Notably, this class introduces the ``greater_is_better`` and ``primary_metric`` attributes. The former is a boolean indicating whether a higher evaluation score is better, which is used for choosing the best checkpoint if ``load_best_model_at_end`` is set to ``True`` in the training arguments. The latter is a string indicating the primary metric for the evaluator. This has to be defined whenever the evaluator returns a dictionary of metrics, and the primary metric is the key pointing to the primary metric, i.e. the one that is used for model selection and/or logging. """ self.greater_is_better = True self.primary_metric = None def __call__( self, model: SentenceTransformer, output_path: str = None, epoch: int = -1, steps: int = -1 ) -> float | dict[str, float]: """ This is called during training to evaluate the model. It returns a score for the evaluation with a higher score indicating a better result. Args: model: the model to evaluate output_path: path where predictions and metrics are written to epoch: the epoch where the evaluation takes place. This is used for the file prefixes. If this is -1, then we assume evaluation on test data. steps: the steps in the current epoch at time of the evaluation. This is used for the file prefixes. If this is -1, then we assume evaluation at the end of the epoch. Returns: Either a score for the evaluation with a higher score indicating a better result, or a dictionary with scores. If the latter is chosen, then `evaluator.primary_metric` must be defined """ pass def prefix_name_to_metrics(self, metrics: dict[str, float], name: str) -> dict[str, float]: if not name: return metrics metrics = {name + "_" + key: float(value) for key, value in metrics.items()} if hasattr(self, "primary_metric") and not self.primary_metric.startswith(name + "_"): self.primary_metric = name + "_" + self.primary_metric return metrics def store_metrics_in_model_card_data(self, model: SentenceTransformer, metrics: dict[str, Any]) -> None: model.model_card_data.set_evaluation_metrics(self, metrics) @property def description(self) -> str: """ Returns a human-readable description of the evaluator: BinaryClassificationEvaluator -> Binary Classification 1. Remove "Evaluator" from the class name 2. Add a space before every capital letter """ class_name = self.__class__.__name__ try: index = class_name.index("Evaluator") class_name = class_name[:index] except IndexError: pass return re.sub(r"([a-z])([A-Z])", r"\g<1> \g<2>", class_name)
import pytest from backend.util.request import validate_url @pytest.mark.parametrize( "url, trusted_origins, expected_value, should_raise", [ # Rejected IP ranges ("localhost", [], None, True), ("192.168.1.1", [], None, True), ("127.0.0.1", [], None, True), ("0.0.0.0", [], None, True), # Normal URLs (should default to http:// if no scheme provided) ("google.com/a?b=c", [], "http://google.com/a?b=c", False), ("github.com?key=!@!@", [], "http://github.com?key=!@!@", False), # Scheme Enforcement ("ftp://example.com", [], None, True), ("file://example.com", [], None, True), # International domain converting to punycode (allowed if public) ("http://xn--exmple-cua.com", [], "http://xn--exmple-cua.com", False), # Invalid domain (IDNA failure) ("http://exa◌mple.com", [], None, True), # IPv6 addresses (loopback/blocked) ("::1", [], None, True), ("http://[::1]", [], None, True), # Suspicious Characters in Hostname ("http://example_underscore.com", [], None, True), ("http://exa mple.com", [], None, True), # Malformed URLs ("http://", [], None, True), # No hostname ("://missing-scheme", [], None, True), # Missing proper scheme # Trusted Origins ( "internal-api.company.com", ["internal-api.company.com", "10.0.0.5"], "http://internal-api.company.com", False, ), ("10.0.0.5", ["10.0.0.5"], "http://10.0.0.5", False), # Special Characters in Path ( "example.com/path%20with%20spaces", [], "http://example.com/path%20with%20spaces", False, ), # Backslashes should be replaced with forward slashes ("http://example.com\\backslash", [], "http://example.com/backslash", False), # Check default-scheme behavior for valid domains ("example.com", [], "http://example.com", False), ("https://secure.com", [], "https://secure.com", False), # Non-ASCII Characters in Query/Fragment ("example.com?param=äöü", [], "http://example.com?param=äöü", False), ], ) def test_validate_url_no_dns_rebinding( url, trusted_origins, expected_value, should_raise ): if should_raise: with pytest.raises(ValueError): validate_url(url, trusted_origins, enable_dns_rebinding=False) else: url, host = validate_url(url, trusted_origins, enable_dns_rebinding=False) assert url == expected_value @pytest.mark.parametrize( "hostname, resolved_ips, expect_error, expected_ip", [ # Multiple public IPs, none blocked ("public-example.com", ["8.8.8.8", "9.9.9.9"], False, "8.8.8.8"), # Includes a blocked IP (e.g. link-local 169.254.x.x) => should raise ("rebinding.com", ["1.2.3.4", "169.254.169.254"], True, None), # Single public IP ("single-public.com", ["8.8.8.8"], False, "8.8.8.8"), # Single blocked IP ("blocked.com", ["127.0.0.1"], True, None), ], ) def test_dns_rebinding_fix( monkeypatch, hostname, resolved_ips, expect_error, expected_ip ): """ Tests that validate_url pins the first valid public IP address, and rejects the domain if any of the resolved IPs are blocked (i.e., DNS Rebinding scenario). """ def mock_getaddrinfo(host, port, *args, **kwargs): # Simulate multiple IPs returned for the given hostname return [(None, None, None, None, (ip, port)) for ip in resolved_ips] # Patch socket.getaddrinfo so we control the DNS resolution in the test monkeypatch.setattr("socket.getaddrinfo", mock_getaddrinfo) if expect_error: # If any IP is blocked, we expect a ValueError with pytest.raises(ValueError): validate_url(hostname, []) else: pinned_url, ascii_hostname = validate_url(hostname, []) # The pinned_url should contain the first valid IP assert pinned_url.startswith("http://") or pinned_url.startswith("https://") assert expected_ip in pinned_url # The ascii_hostname should match our original hostname after IDNA encoding assert ascii_hostname == hostname
import pytest from backend.util.request import validate_url def test_validate_url(): # Rejected IP ranges with pytest.raises(ValueError): validate_url("localhost", []) with pytest.raises(ValueError): validate_url("192.168.1.1", []) with pytest.raises(ValueError): validate_url("127.0.0.1", []) with pytest.raises(ValueError): validate_url("0.0.0.0", []) # Normal URLs assert validate_url("google.com/a?b=c", []) == "http://google.com/a?b=c" assert validate_url("github.com?key=!@!@", []) == "http://github.com?key=!@!@" # Scheme Enforcement with pytest.raises(ValueError): validate_url("ftp://example.com", []) with pytest.raises(ValueError): validate_url("file://example.com", []) # International domain that converts to punycode - should be allowed if public assert validate_url("http://xn--exmple-cua.com", []) == "http://xn--exmple-cua.com" # If the domain fails IDNA encoding or is invalid, it should raise an error with pytest.raises(ValueError): validate_url("http://exa◌mple.com", []) # IPv6 Addresses with pytest.raises(ValueError): validate_url("::1", []) # IPv6 loopback should be blocked with pytest.raises(ValueError): validate_url("http://[::1]", []) # IPv6 loopback in URL form # Suspicious Characters in Hostname with pytest.raises(ValueError): validate_url("http://example_underscore.com", []) with pytest.raises(ValueError): validate_url("http://exa mple.com", []) # Space in hostname # Malformed URLs with pytest.raises(ValueError): validate_url("http://", []) # No hostname with pytest.raises(ValueError): validate_url("://missing-scheme", []) # Missing proper scheme # Trusted Origins trusted = ["internal-api.company.com", "10.0.0.5"] assert ( validate_url("internal-api.company.com", trusted) == "http://internal-api.company.com" ) assert validate_url("10.0.0.5", ["10.0.0.5"]) == "http://10.0.0.5" # Special Characters in Path or Query assert ( validate_url("example.com/path%20with%20spaces", []) == "http://example.com/path%20with%20spaces" ) # Backslashes should be replaced with forward slashes assert ( validate_url("http://example.com\\backslash", []) == "http://example.com/backslash" ) # Check defaulting scheme behavior for valid domains assert validate_url("example.com", []) == "http://example.com" assert validate_url("https://secure.com", []) == "https://secure.com" # Non-ASCII Characters in Query/Fragment assert validate_url("example.com?param=äöü", []) == "http://example.com?param=äöü"
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Tuple, Dict, List import numpy as np from jina import Executor, requests, DocumentArray, Document from jina_commons import get_logger from jina_commons.indexers.dump import import_vectors class NumpySearcher(Executor): def __init__( self, dump_path: str = None, default_top_k: int = 5, default_traversal_paths: List[str] = ['r'], metric: str = 'cosine', is_distance: bool = False, **kwargs, ): super().__init__(**kwargs) self.default_traversal_paths = default_traversal_paths self.is_distance = is_distance self.metric = metric self.dump_path = dump_path or kwargs.get('runtime_args').get('dump_path') self.logger = get_logger(self) self.default_top_k = default_top_k if self.dump_path is not None: self.logger.info(f'Importing data from {self.dump_path}') ids, vecs = import_vectors(self.dump_path, str(self.runtime_args.pea_id)) self._ids = np.array(list(ids)) self._vecs = np.array(list(vecs)) self._ids_to_idx = {} self.logger.info(f'Imported {len(self._ids)} documents.') else: self.logger.warning( f'No dump_path provided for {self.__class__.__name__}. Use flow.rolling_update()...' ) @requests(on='/search') def search(self, docs: 'DocumentArray', parameters: Dict = None, **kwargs): if not hasattr(self, '_vecs') or not self._vecs.size: self.logger.warning('Searching an empty index') return top_k = int(parameters.get('top_k', self.default_top_k)) traversal_paths = parameters.get( 'traversal_paths', self.default_traversal_paths ) doc_embeddings = docs.traverse_flat(traversal_paths).get_attributes('embedding') if not docs: self.logger.info('No documents to search for') return if not doc_embeddings: self.logger.info('None of the docs have any embeddings') return doc_embeddings = np.stack(doc_embeddings) q_emb = _ext_A(_norm(doc_embeddings)) d_emb = _ext_B(_norm(self._vecs)) if self.metric == 'cosine': dists = _cosine(q_emb, d_emb) elif self.metric == 'euclidean': dists = _euclidean(q_emb, d_emb) else: self.logger.error(f'Metric {self.metric} not supported.') positions, dist = self._get_sorted_top_k(dists, top_k) for _q, _positions, _dists in zip(docs, positions, dist): for position, dist in zip(_positions, _dists): d = Document(id=self._ids[position], embedding=self._vecs[position]) if self.is_distance: d.scores[self.metric] = dist else: if self.metric == 'cosine': d.scores[self.metric] = 1 - dist elif self.metric == 'euclidean': d.scores[self.metric] = 1 / (1 + dist) _q.matches.append(d) @staticmethod def _get_sorted_top_k( dist: 'np.array', top_k: int ) -> Tuple['np.ndarray', 'np.ndarray']: if top_k >= dist.shape[1]: idx = dist.argsort(axis=1)[:, :top_k] dist = np.take_along_axis(dist, idx, axis=1) else: idx_ps = dist.argpartition(kth=top_k, axis=1)[:, :top_k] dist = np.take_along_axis(dist, idx_ps, axis=1) idx_fs = dist.argsort(axis=1) idx = np.take_along_axis(idx_ps, idx_fs, axis=1) dist = np.take_along_axis(dist, idx_fs, axis=1) return idx, dist def _get_ones(x, y): return np.ones((x, y)) def _ext_A(A): nA, dim = A.shape A_ext = _get_ones(nA, dim * 3) A_ext[:, dim: 2 * dim] = A A_ext[:, 2 * dim:] = A ** 2 return A_ext def _ext_B(B): nB, dim = B.shape B_ext = _get_ones(dim * 3, nB) B_ext[:dim] = (B ** 2).T B_ext[dim: 2 * dim] = -2.0 * B.T del B return B_ext def _euclidean(A_ext, B_ext): sqdist = A_ext.dot(B_ext).clip(min=0) return np.sqrt(sqdist) def _norm(A): return A / np.linalg.norm(A, ord=2, axis=1, keepdims=True) def _cosine(A_norm_ext, B_norm_ext): return A_norm_ext.dot(B_norm_ext).clip(min=0) / 2
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from typing import Tuple, Dict, List import numpy as np from jina import Executor, requests, DocumentArray, Document from jina_commons import get_logger from jina_commons.indexers.dump import import_vectors class NumpySearcher(Executor): def __init__( self, dump_path: str = None, default_top_k: int = 5, default_traversal_paths: List[str] = ['r'], **kwargs, ): super().__init__(**kwargs) self.default_traversal_paths = default_traversal_paths self.dump_path = dump_path or kwargs.get('runtime_args').get('dump_path') self.logger = get_logger(self) self.default_top_k = default_top_k if self.dump_path is not None: self.logger.info(f'Importing data from {self.dump_path}') ids, vecs = import_vectors(self.dump_path, str(self.runtime_args.pea_id)) self._ids = np.array(list(ids)) self._vecs = np.array(list(vecs)) self._ids_to_idx = {} self.logger.info(f'Imported {len(self._ids)} documents.') else: self.logger.warning( f'No dump_path provided for {self.__class__.__name__}. Use flow.rolling_update()...' ) @requests(on='/search') def search(self, docs: 'DocumentArray', parameters: Dict = None, **kwargs): if not hasattr(self, '_vecs') or not self._vecs.size: self.logger.warning('Searching an empty index') return top_k = int(parameters.get('top_k', self.default_top_k)) traversal_paths = parameters.get( 'traversal_paths', self.default_traversal_paths ) doc_embeddings = np.stack( docs.traverse_flat(traversal_paths).get_attributes('embedding') ) q_emb = _ext_A(_norm(doc_embeddings)) d_emb = _ext_B(_norm(self._vecs)) dists = _cosine(q_emb, d_emb) positions, dist = self._get_sorted_top_k(dists, top_k) for _q, _positions, _dists in zip(docs, positions, dist): for position, _dist in zip(_positions, _dists): d = Document(id=self._ids[position], embedding=self._vecs[position]) d.scores['similarity'] = 1 - _dist _q.matches.append(d) @staticmethod def _get_sorted_top_k( dist: 'np.array', top_k: int ) -> Tuple['np.ndarray', 'np.ndarray']: if top_k >= dist.shape[1]: idx = dist.argsort(axis=1)[:, :top_k] dist = np.take_along_axis(dist, idx, axis=1) else: idx_ps = dist.argpartition(kth=top_k, axis=1)[:, :top_k] dist = np.take_along_axis(dist, idx_ps, axis=1) idx_fs = dist.argsort(axis=1) idx = np.take_along_axis(idx_ps, idx_fs, axis=1) dist = np.take_along_axis(dist, idx_fs, axis=1) return idx, dist def _get_ones(x, y): return np.ones((x, y)) def _ext_A(A): nA, dim = A.shape A_ext = _get_ones(nA, dim * 3) A_ext[:, dim : 2 * dim] = A A_ext[:, 2 * dim :] = A ** 2 return A_ext def _ext_B(B): nB, dim = B.shape B_ext = _get_ones(dim * 3, nB) B_ext[:dim] = (B ** 2).T B_ext[dim : 2 * dim] = -2.0 * B.T del B return B_ext def _euclidean(A_ext, B_ext): sqdist = A_ext.dot(B_ext).clip(min=0) return np.sqrt(sqdist) def _norm(A): return A / np.linalg.norm(A, ord=2, axis=1, keepdims=True) def _cosine(A_norm_ext, B_norm_ext): return A_norm_ext.dot(B_norm_ext).clip(min=0) / 2
from __future__ import annotations import logging from dataclasses import dataclass from sentence_transformers.data_collator import SentenceTransformerDataCollator logger = logging.getLogger(__name__) @dataclass class SparseEncoderDataCollator(SentenceTransformerDataCollator): """Collator for a SparseEncoder model. Overridden from SentenceTransformerDataCollator with nothing added. This encodes the text columns to {column}_input_ids and {column}_attention_mask columns. This works with the two text dataset that is used as the example in the training overview: https://www.sbert.net/docs/sentence_transformer/training_overview.html It is important that the columns are in the expected order. For example, if your dataset has columns "answer", "question" in that order, then the MultipleNegativesRankingLoss will consider "answer" as the anchor and "question" as the positive, and it will (unexpectedly) optimize for "given the answer, what is the question?". """
from __future__ import annotations import logging from dataclasses import dataclass from sentence_transformers.data_collator import SentenceTransformerDataCollator logger = logging.getLogger(__name__) @dataclass class SparseEncoderDataCollator(SentenceTransformerDataCollator): """Collator for a SparseEncoder model. Overrided from SentenceTransformerDataCollator nothing added. This encodes the text columns to {column}_input_ids and {column}_attention_mask columns. This works with the two text dataset that is used as the example in the training overview: https://www.sbert.net/docs/sentence_transformer/training_overview.html It is important that the columns are in the expected order. For example, if your dataset has columns "answer", "question" in that order, then the MultipleNegativesRankingLoss will consider "answer" as the anchor and "question" as the positive, and it will (unexpectedly) optimize for "given the answer, what is the question?". """
# Copyright (c) OpenMMLab. All rights reserved. from ..builder import DETECTORS from .two_stage import TwoStageDetector @DETECTORS.register_module() class MaskScoringRCNN(TwoStageDetector): """Mask Scoring RCNN. https://arxiv.org/abs/1903.00241 """ def __init__(self, backbone, rpn_head, roi_head, train_cfg, test_cfg, neck=None, pretrained=None, init_cfg=None): super(MaskScoringRCNN, self).__init__( backbone=backbone, neck=neck, rpn_head=rpn_head, roi_head=roi_head, train_cfg=train_cfg, test_cfg=test_cfg, pretrained=pretrained, init_cfg=init_cfg)
from ..builder import DETECTORS from .two_stage import TwoStageDetector @DETECTORS.register_module() class MaskScoringRCNN(TwoStageDetector): """Mask Scoring RCNN. https://arxiv.org/abs/1903.00241 """ def __init__(self, backbone, rpn_head, roi_head, train_cfg, test_cfg, neck=None, pretrained=None, init_cfg=None): super(MaskScoringRCNN, self).__init__( backbone=backbone, neck=neck, rpn_head=rpn_head, roi_head=roi_head, train_cfg=train_cfg, test_cfg=test_cfg, pretrained=pretrained, init_cfg=init_cfg)
""" LexRank implementation Source: https://github.com/crabcamp/lexrank/tree/dev """ import logging import numpy as np from scipy.sparse.csgraph import connected_components from scipy.special import softmax logger = logging.getLogger(__name__) def degree_centrality_scores( similarity_matrix, threshold=None, increase_power=True, ): if not (threshold is None or isinstance(threshold, float) and 0 <= threshold < 1): raise ValueError( "'threshold' should be a floating-point number " "from the interval [0, 1) or None", ) if threshold is None: markov_matrix = create_markov_matrix(similarity_matrix) else: markov_matrix = create_markov_matrix_discrete( similarity_matrix, threshold, ) scores = stationary_distribution( markov_matrix, increase_power=increase_power, normalized=False, ) return scores def _power_method(transition_matrix, increase_power=True, max_iter=10000): eigenvector = np.ones(len(transition_matrix)) if len(eigenvector) == 1: return eigenvector transition = transition_matrix.transpose() for _ in range(max_iter): eigenvector_next = np.dot(transition, eigenvector) if np.allclose(eigenvector_next, eigenvector): return eigenvector_next eigenvector = eigenvector_next if increase_power: transition = np.dot(transition, transition) logger.warning("Maximum number of iterations for power method exceeded without convergence!") return eigenvector_next def connected_nodes(matrix): _, labels = connected_components(matrix) groups = [] for tag in np.unique(labels): group = np.where(labels == tag)[0] groups.append(group) return groups def create_markov_matrix(weights_matrix): n_1, n_2 = weights_matrix.shape if n_1 != n_2: raise ValueError("'weights_matrix' should be square") row_sum = weights_matrix.sum(axis=1, keepdims=True) # normalize probability distribution differently if we have negative transition values if np.min(weights_matrix) <= 0: return softmax(weights_matrix, axis=1) return weights_matrix / row_sum def create_markov_matrix_discrete(weights_matrix, threshold): discrete_weights_matrix = np.zeros(weights_matrix.shape) ixs = np.where(weights_matrix >= threshold) discrete_weights_matrix[ixs] = 1 return create_markov_matrix(discrete_weights_matrix) def stationary_distribution( transition_matrix, increase_power=True, normalized=True, ): n_1, n_2 = transition_matrix.shape if n_1 != n_2: raise ValueError("'transition_matrix' should be square") distribution = np.zeros(n_1) grouped_indices = connected_nodes(transition_matrix) for group in grouped_indices: t_matrix = transition_matrix[np.ix_(group, group)] eigenvector = _power_method(t_matrix, increase_power=increase_power) distribution[group] = eigenvector if normalized: distribution /= n_1 return distribution
""" LexRank implementation Source: https://github.com/crabcamp/lexrank/tree/dev """ import numpy as np from scipy.sparse.csgraph import connected_components from scipy.special import softmax import logging logger = logging.getLogger(__name__) def degree_centrality_scores( similarity_matrix, threshold=None, increase_power=True, ): if not (threshold is None or isinstance(threshold, float) and 0 <= threshold < 1): raise ValueError( "'threshold' should be a floating-point number " "from the interval [0, 1) or None", ) if threshold is None: markov_matrix = create_markov_matrix(similarity_matrix) else: markov_matrix = create_markov_matrix_discrete( similarity_matrix, threshold, ) scores = stationary_distribution( markov_matrix, increase_power=increase_power, normalized=False, ) return scores def _power_method(transition_matrix, increase_power=True, max_iter=10000): eigenvector = np.ones(len(transition_matrix)) if len(eigenvector) == 1: return eigenvector transition = transition_matrix.transpose() for _ in range(max_iter): eigenvector_next = np.dot(transition, eigenvector) if np.allclose(eigenvector_next, eigenvector): return eigenvector_next eigenvector = eigenvector_next if increase_power: transition = np.dot(transition, transition) logger.warning("Maximum number of iterations for power method exceeded without convergence!") return eigenvector_next def connected_nodes(matrix): _, labels = connected_components(matrix) groups = [] for tag in np.unique(labels): group = np.where(labels == tag)[0] groups.append(group) return groups def create_markov_matrix(weights_matrix): n_1, n_2 = weights_matrix.shape if n_1 != n_2: raise ValueError("'weights_matrix' should be square") row_sum = weights_matrix.sum(axis=1, keepdims=True) # normalize probability distribution differently if we have negative transition values if np.min(weights_matrix) <= 0: return softmax(weights_matrix, axis=1) return weights_matrix / row_sum def create_markov_matrix_discrete(weights_matrix, threshold): discrete_weights_matrix = np.zeros(weights_matrix.shape) ixs = np.where(weights_matrix >= threshold) discrete_weights_matrix[ixs] = 1 return create_markov_matrix(discrete_weights_matrix) def stationary_distribution( transition_matrix, increase_power=True, normalized=True, ): n_1, n_2 = transition_matrix.shape if n_1 != n_2: raise ValueError("'transition_matrix' should be square") distribution = np.zeros(n_1) grouped_indices = connected_nodes(transition_matrix) for group in grouped_indices: t_matrix = transition_matrix[np.ix_(group, group)] eigenvector = _power_method(t_matrix, increase_power=increase_power) distribution[group] = eigenvector if normalized: distribution /= n_1 return distribution
# dataset settings dataset_type = 'DeepFashionDataset' data_root = 'data/DeepFashion/In-shop/' # Example to use different file client # Method 1: simply set the data root and let the file I/O module # automatically infer from prefix (not support LMDB and Memcache yet) # data_root = 's3://openmmlab/datasets/detection/coco/' # Method 2: Use `backend_args`, `file_client_args` in versions before 3.0.0rc6 # backend_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) backend_args = None train_pipeline = [ dict(type='LoadImageFromFile', backend_args=backend_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict(type='Resize', scale=(750, 1101), keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', backend_args=backend_args), dict(type='Resize', scale=(750, 1101), keep_ratio=True), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), batch_sampler=dict(type='AspectRatioBatchSampler'), dataset=dict( type='RepeatDataset', times=2, dataset=dict( type=dataset_type, data_root=data_root, ann_file='Anno/segmentation/DeepFashion_segmentation_train.json', data_prefix=dict(img='Img/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline, backend_args=backend_args))) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='Anno/segmentation/DeepFashion_segmentation_query.json', data_prefix=dict(img='Img/'), test_mode=True, pipeline=test_pipeline, backend_args=backend_args)) test_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='Anno/segmentation/DeepFashion_segmentation_gallery.json', data_prefix=dict(img='Img/'), test_mode=True, pipeline=test_pipeline, backend_args=backend_args)) val_evaluator = dict( type='CocoMetric', ann_file=data_root + 'Anno/segmentation/DeepFashion_segmentation_query.json', metric=['bbox', 'segm'], format_only=False, backend_args=backend_args) test_evaluator = dict( type='CocoMetric', ann_file=data_root + 'Anno/segmentation/DeepFashion_segmentation_gallery.json', metric=['bbox', 'segm'], format_only=False, backend_args=backend_args)
# dataset settings dataset_type = 'DeepFashionDataset' data_root = 'data/DeepFashion/In-shop/' # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) file_client_args = dict(backend='disk') train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict(type='Resize', scale=(750, 1101), keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(750, 1101), keep_ratio=True), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), batch_sampler=dict(type='AspectRatioBatchSampler'), dataset=dict( type='RepeatDataset', times=2, dataset=dict( type=dataset_type, data_root=data_root, ann_file='Anno/segmentation/DeepFashion_segmentation_train.json', data_prefix=dict(img='Img/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline))) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='Anno/segmentation/DeepFashion_segmentation_query.json', data_prefix=dict(img='Img/'), test_mode=True, pipeline=test_pipeline)) test_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='Anno/segmentation/DeepFashion_segmentation_gallery.json', data_prefix=dict(img='Img/'), test_mode=True, pipeline=test_pipeline)) val_evaluator = dict( type='CocoMetric', ann_file=data_root + 'Anno/segmentation/DeepFashion_segmentation_query.json', metric=['bbox', 'segm'], format_only=False) test_evaluator = dict( type='CocoMetric', ann_file=data_root + 'Anno/segmentation/DeepFashion_segmentation_gallery.json', metric=['bbox', 'segm'], format_only=False)
# Copyright (c) OpenMMLab. All rights reserved. import asyncio import contextlib import logging import os import time from typing import List import torch logger = logging.getLogger(__name__) DEBUG_COMPLETED_TIME = bool(os.environ.get('DEBUG_COMPLETED_TIME', False)) @contextlib.asynccontextmanager async def completed(trace_name='', name='', sleep_interval=0.05, streams: List[torch.cuda.Stream] = None): """Async context manager that waits for work to complete on given CUDA streams.""" if not torch.cuda.is_available(): yield return stream_before_context_switch = torch.cuda.current_stream() if not streams: streams = [stream_before_context_switch] else: streams = [s if s else stream_before_context_switch for s in streams] end_events = [ torch.cuda.Event(enable_timing=DEBUG_COMPLETED_TIME) for _ in streams ] if DEBUG_COMPLETED_TIME: start = torch.cuda.Event(enable_timing=True) stream_before_context_switch.record_event(start) cpu_start = time.monotonic() logger.debug('%s %s starting, streams: %s', trace_name, name, streams) grad_enabled_before = torch.is_grad_enabled() try: yield finally: current_stream = torch.cuda.current_stream() assert current_stream == stream_before_context_switch if DEBUG_COMPLETED_TIME: cpu_end = time.monotonic() for i, stream in enumerate(streams): event = end_events[i] stream.record_event(event) grad_enabled_after = torch.is_grad_enabled() # observed change of torch.is_grad_enabled() during concurrent run of # async_test_bboxes code assert (grad_enabled_before == grad_enabled_after ), 'Unexpected is_grad_enabled() value change' are_done = [e.query() for e in end_events] logger.debug('%s %s completed: %s streams: %s', trace_name, name, are_done, streams) with torch.cuda.stream(stream_before_context_switch): while not all(are_done): await asyncio.sleep(sleep_interval) are_done = [e.query() for e in end_events] logger.debug( '%s %s completed: %s streams: %s', trace_name, name, are_done, streams, ) current_stream = torch.cuda.current_stream() assert current_stream == stream_before_context_switch if DEBUG_COMPLETED_TIME: cpu_time = (cpu_end - cpu_start) * 1000 stream_times_ms = '' for i, stream in enumerate(streams): elapsed_time = start.elapsed_time(end_events[i]) stream_times_ms += f' {stream} {elapsed_time:.2f} ms' logger.info('%s %s %.2f ms %s', trace_name, name, cpu_time, stream_times_ms) @contextlib.asynccontextmanager async def concurrent(streamqueue: asyncio.Queue, trace_name='concurrent', name='stream'): """Run code concurrently in different streams. :param streamqueue: asyncio.Queue instance. Queue tasks define the pool of streams used for concurrent execution. """ if not torch.cuda.is_available(): yield return initial_stream = torch.cuda.current_stream() with torch.cuda.stream(initial_stream): stream = await streamqueue.get() assert isinstance(stream, torch.cuda.Stream) try: with torch.cuda.stream(stream): logger.debug('%s %s is starting, stream: %s', trace_name, name, stream) yield current = torch.cuda.current_stream() assert current == stream logger.debug('%s %s has finished, stream: %s', trace_name, name, stream) finally: streamqueue.task_done() streamqueue.put_nowait(stream)
import asyncio import contextlib import logging import os import time from typing import List import torch logger = logging.getLogger(__name__) DEBUG_COMPLETED_TIME = bool(os.environ.get('DEBUG_COMPLETED_TIME', False)) @contextlib.asynccontextmanager async def completed(trace_name='', name='', sleep_interval=0.05, streams: List[torch.cuda.Stream] = None): """Async context manager that waits for work to complete on given CUDA streams.""" if not torch.cuda.is_available(): yield return stream_before_context_switch = torch.cuda.current_stream() if not streams: streams = [stream_before_context_switch] else: streams = [s if s else stream_before_context_switch for s in streams] end_events = [ torch.cuda.Event(enable_timing=DEBUG_COMPLETED_TIME) for _ in streams ] if DEBUG_COMPLETED_TIME: start = torch.cuda.Event(enable_timing=True) stream_before_context_switch.record_event(start) cpu_start = time.monotonic() logger.debug('%s %s starting, streams: %s', trace_name, name, streams) grad_enabled_before = torch.is_grad_enabled() try: yield finally: current_stream = torch.cuda.current_stream() assert current_stream == stream_before_context_switch if DEBUG_COMPLETED_TIME: cpu_end = time.monotonic() for i, stream in enumerate(streams): event = end_events[i] stream.record_event(event) grad_enabled_after = torch.is_grad_enabled() # observed change of torch.is_grad_enabled() during concurrent run of # async_test_bboxes code assert (grad_enabled_before == grad_enabled_after ), 'Unexpected is_grad_enabled() value change' are_done = [e.query() for e in end_events] logger.debug('%s %s completed: %s streams: %s', trace_name, name, are_done, streams) with torch.cuda.stream(stream_before_context_switch): while not all(are_done): await asyncio.sleep(sleep_interval) are_done = [e.query() for e in end_events] logger.debug( '%s %s completed: %s streams: %s', trace_name, name, are_done, streams, ) current_stream = torch.cuda.current_stream() assert current_stream == stream_before_context_switch if DEBUG_COMPLETED_TIME: cpu_time = (cpu_end - cpu_start) * 1000 stream_times_ms = '' for i, stream in enumerate(streams): elapsed_time = start.elapsed_time(end_events[i]) stream_times_ms += f' {stream} {elapsed_time:.2f} ms' logger.info('%s %s %.2f ms %s', trace_name, name, cpu_time, stream_times_ms) @contextlib.asynccontextmanager async def concurrent(streamqueue: asyncio.Queue, trace_name='concurrent', name='stream'): """Run code concurrently in different streams. :param streamqueue: asyncio.Queue instance. Queue tasks define the pool of streams used for concurrent execution. """ if not torch.cuda.is_available(): yield return initial_stream = torch.cuda.current_stream() with torch.cuda.stream(initial_stream): stream = await streamqueue.get() assert isinstance(stream, torch.cuda.Stream) try: with torch.cuda.stream(stream): logger.debug('%s %s is starting, stream: %s', trace_name, name, stream) yield current = torch.cuda.current_stream() assert current == stream logger.debug('%s %s has finished, stream: %s', trace_name, name, stream) finally: streamqueue.task_done() streamqueue.put_nowait(stream)
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] # model settings norm_cfg = dict(type='GN', num_groups=32, requires_grad=True) model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( _delete_=True, type='SABLRetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, approx_anchor_generator=dict( type='AnchorGenerator', octave_base_scale=4, scales_per_octave=3, ratios=[0.5, 1.0, 2.0], strides=[8, 16, 32, 64, 128]), square_anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], scales=[4], strides=[8, 16, 32, 64, 128]), norm_cfg=norm_cfg, bbox_coder=dict( type='BucketingBBoxCoder', num_buckets=14, scale_factor=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.5), loss_bbox_reg=dict( type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.5)), # training and testing settings train_cfg=dict( assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False)) # dataset settings train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=[(1333, 480), (1333, 960)], keep_ratio=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline)) # optimizer optim_wrapper = dict( optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001))
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] # model settings norm_cfg = dict(type='GN', num_groups=32, requires_grad=True) model = dict( backbone=dict( depth=101, init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet101')), bbox_head=dict( _delete_=True, type='SABLRetinaHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, approx_anchor_generator=dict( type='AnchorGenerator', octave_base_scale=4, scales_per_octave=3, ratios=[0.5, 1.0, 2.0], strides=[8, 16, 32, 64, 128]), square_anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], scales=[4], strides=[8, 16, 32, 64, 128]), norm_cfg=norm_cfg, bbox_coder=dict( type='BucketingBBoxCoder', num_buckets=14, scale_factor=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.5), loss_bbox_reg=dict( type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.5)), # training and testing settings train_cfg=dict( assigner=dict( type='ApproxMaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.4, min_pos_iou=0.0, ignore_iof_thr=-1), allowed_border=-1, pos_weight=-1, debug=False)) # dataset settings train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=[(1333, 480), (1333, 960)], resize_cfg=dict(type='Resize', keep_ratio=True)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline)) # optimizer optim_wrapper = dict( optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001))
# Licensed to the LF AI & Data foundation under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations from typing import Any, Dict, Optional from docarray import BaseDoc, DocList from docarray.typing import AnyEmbedding, AnyTensor class LegacyDocument(BaseDoc): """ This Document is the LegacyDocument. It follows the same schema as in DocArray <=0.21. It can be useful to start migrating a codebase from v1 to v2. Nevertheless, the API is not totally compatible with DocArray <=0.21 `Document`. Indeed, none of the method associated with `Document` are present. Only the schema of the data is similar. ```python from docarray import DocList from docarray.documents.legacy import LegacyDocument import numpy as np doc = LegacyDocument(text='hello') doc.url = 'http://myimg.png' doc.tensor = np.zeros((3, 224, 224)) doc.embedding = np.zeros((100, 1)) doc.tags['price'] = 10 doc.chunks = DocList[Document]([Document() for _ in range(10)]) doc.chunks = DocList[Document]([Document() for _ in range(10)]) ``` """ tensor: Optional[AnyTensor] = None chunks: Optional[DocList[LegacyDocument]] = None matches: Optional[DocList[LegacyDocument]] = None blob: Optional[bytes] = None text: Optional[str] = None url: Optional[str] = None embedding: Optional[AnyEmbedding] = None tags: Dict[str, Any] = dict() scores: Optional[Dict[str, Any]] = None
from __future__ import annotations from typing import Any, Dict, Optional from docarray import BaseDoc, DocList from docarray.typing import AnyEmbedding, AnyTensor class LegacyDocument(BaseDoc): """ This Document is the LegacyDocument. It follows the same schema as in DocArray <=0.21. It can be useful to start migrating a codebase from v1 to v2. Nevertheless, the API is not totally compatible with DocArray <=0.21 `Document`. Indeed, none of the method associated with `Document` are present. Only the schema of the data is similar. ```python from docarray import DocList from docarray.documents.legacy import LegacyDocument import numpy as np doc = LegacyDocument(text='hello') doc.url = 'http://myimg.png' doc.tensor = np.zeros((3, 224, 224)) doc.embedding = np.zeros((100, 1)) doc.tags['price'] = 10 doc.chunks = DocList[Document]([Document() for _ in range(10)]) doc.chunks = DocList[Document]([Document() for _ in range(10)]) ``` """ tensor: Optional[AnyTensor] = None chunks: Optional[DocList[LegacyDocument]] = None matches: Optional[DocList[LegacyDocument]] = None blob: Optional[bytes] = None text: Optional[str] = None url: Optional[str] = None embedding: Optional[AnyEmbedding] = None tags: Dict[str, Any] = dict() scores: Optional[Dict[str, Any]] = None
from docarray.array.document import DocumentArray from docarray.array.storage.annlite import StorageMixins, AnnliteConfig __all__ = ['AnnliteConfig', 'DocumentArrayAnnlite'] class DocumentArrayAnnlite(StorageMixins, DocumentArray): """ DocumentArray that stores Documents in `ANNLite <https://github.com/jina-ai/annlite>`_. .. note:: This DocumentArray requires `annlite`. You can install it via `pip install "docarray[annlite]"`. With this implementation, :meth:`match` and :meth:`find` perform fast (approximate) vector search. Additionally, search with filters on a :class:`~docarray.document.Document` s :attr:`~docarray.document.Document.tags` attribute is supported. Example usage: .. code-block:: python from docarray import Document, DocumentArray import numpy as np da = DocumentArray(storage='annlite', config={'data_path': './data', 'n_dim': 10}) n_dim = 3 da = DocumentArray( storage='annlite', config={ 'n_dim': n_dim, 'columns': [('price', 'float')], }, ) with da: da.extend([Document(id=f'r{i}', tags={'price': i}) for i in range(10)]) max_price = 3 n_limit = 4 filter = {'price': {'$lte': max_price}} results = da.find(filter=filter) .. seealso:: For further details, see our :ref:`user guide <annlite>`. """ def __new__(cls, *args, **kwargs): return super().__new__(cls)
from .document import DocumentArray from .storage.annlite import StorageMixins, AnnliteConfig __all__ = ['AnnliteConfig', 'DocumentArrayAnnlite'] class DocumentArrayAnnlite(StorageMixins, DocumentArray): """ DocumentArray that stores Documents in `ANNLite <https://github.com/jina-ai/annlite>`_. .. note:: This DocumentArray requires `annlite`. You can install it via `pip install "docarray[annlite]"`. With this implementation, :meth:`match` and :meth:`find` perform fast (approximate) vector search. Additionally, search with filters on a :class:`~docarray.document.Document` s :attr:`~docarray.document.Document.tags` attribute is supported. Example usage: .. code-block:: python from docarray import Document, DocumentArray import numpy as np da = DocumentArray(storage='annlite', config={'data_path': './data', 'n_dim': 10}) n_dim = 3 da = DocumentArray( storage='annlite', config={ 'n_dim': n_dim, 'columns': [('price', 'float')], }, ) with da: da.extend([Document(id=f'r{i}', tags={'price': i}) for i in range(10)]) max_price = 3 n_limit = 4 filter = {'price': {'$lte': max_price}} results = da.find(filter=filter) .. seealso:: For further details, see our :ref:`user guide <annlite>`. """ def __new__(cls, *args, **kwargs): return super().__new__(cls)
_base_ = '../mask_rcnn/mask-rcnn_r50_fpn_1x_coco.py' model = dict( backbone=dict( dcn=dict(type='DCNv2', deform_groups=1, fallback_on_stride=False), stage_with_dcn=(False, True, True, True))) # MMEngine support the following two ways, users can choose # according to convenience # optim_wrapper = dict(type='AmpOptimWrapper') _base_.optim_wrapper.type = 'AmpOptimWrapper'
_base_ = '../mask_rcnn/mask-rcnn_r50_fpn_1x_coco.py' model = dict( backbone=dict( dcn=dict(type='DCNv2', deform_groups=1, fallback_on_stride=False), stage_with_dcn=(False, True, True, True))) fp16 = dict(loss_scale=512.)
from typing import Optional import numpy as np import pytest import torch from pydantic.tools import parse_obj_as, schema_json_of from docarray import BaseDocument from docarray.base_document.io.json import orjson_dumps from docarray.typing import AudioTorchTensor, AudioUrl from docarray.utils.misc import is_tf_available from tests import TOYDATA_DIR tf_available = is_tf_available() if tf_available: import tensorflow as tf from docarray.typing.tensor.audio import AudioTensorFlowTensor AUDIO_FILES = [ str(TOYDATA_DIR / 'hello.wav'), str(TOYDATA_DIR / 'olleh.wav'), ] REMOTE_AUDIO_FILE = 'https://github.com/docarray/docarray/blob/feat-rewrite-v2/tests/toydata/olleh.wav?raw=true' # noqa: E501 @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_audio_url(file_url): uri = parse_obj_as(AudioUrl, file_url) tensor = uri.load() assert isinstance(tensor, np.ndarray) @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_load_audio_url_to_audio_torch_tensor_field(file_url): class MyAudioDoc(BaseDocument): audio_url: AudioUrl tensor: Optional[AudioTorchTensor] doc = MyAudioDoc(audio_url=file_url) doc.tensor = doc.audio_url.load() assert isinstance(doc.tensor, torch.Tensor) assert isinstance(doc.tensor, AudioTorchTensor) @pytest.mark.tensorflow @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_load_audio_url_to_audio_tensorflow_tensor_field(file_url): class MyAudioDoc(BaseDocument): audio_url: AudioUrl tensor: Optional[AudioTensorFlowTensor] doc = MyAudioDoc(audio_url=file_url) doc.tensor = doc.audio_url.load() assert isinstance(doc.tensor, AudioTensorFlowTensor) assert isinstance(doc.tensor.tensor, tf.Tensor) @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_load(file_url): url = parse_obj_as(AudioUrl, file_url) tensor = url.load() assert isinstance(tensor, np.ndarray) def test_json_schema(): schema_json_of(AudioUrl) def test_dump_json(): url = parse_obj_as(AudioUrl, REMOTE_AUDIO_FILE) orjson_dumps(url) @pytest.mark.parametrize( 'path_to_file', [ *[file for file in AUDIO_FILES], REMOTE_AUDIO_FILE, ], ) def test_validation(path_to_file): url = parse_obj_as(AudioUrl, path_to_file) assert isinstance(url, AudioUrl) assert isinstance(url, str) @pytest.mark.parametrize( 'path_to_file', [ 'illegal', 'https://www.google.com', 'my/local/text/file.txt', 'my/local/text/file.png', ], ) def test_illegal_validation(path_to_file): with pytest.raises(ValueError, match='AudioUrl'): parse_obj_as(AudioUrl, path_to_file) @pytest.mark.proto @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_proto_audio_url(file_url): uri = parse_obj_as(AudioUrl, file_url) proto = uri._to_node_protobuf() assert 'audio_url' in str(proto)
from typing import Optional import numpy as np import pytest import torch from pydantic.tools import parse_obj_as, schema_json_of from docarray import BaseDocument from docarray.base_document.io.json import orjson_dumps from docarray.typing import AudioTorchTensor, AudioUrl from tests import TOYDATA_DIR AUDIO_FILES = [ str(TOYDATA_DIR / 'hello.wav'), str(TOYDATA_DIR / 'olleh.wav'), ] REMOTE_AUDIO_FILE = 'https://github.com/docarray/docarray/blob/feat-rewrite-v2/tests/toydata/olleh.wav?raw=true' # noqa: E501 @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_audio_url(file_url): uri = parse_obj_as(AudioUrl, file_url) tensor = uri.load() assert isinstance(tensor, np.ndarray) @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_load_audio_url_to_audio_torch_tensor_field(file_url): class MyAudioDoc(BaseDocument): audio_url: AudioUrl tensor: Optional[AudioTorchTensor] doc = MyAudioDoc(audio_url=file_url) doc.tensor = doc.audio_url.load() assert isinstance(doc.tensor, torch.Tensor) assert isinstance(doc.tensor, AudioTorchTensor) @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_load(file_url): url = parse_obj_as(AudioUrl, file_url) tensor = url.load() assert isinstance(tensor, np.ndarray) def test_json_schema(): schema_json_of(AudioUrl) def test_dump_json(): url = parse_obj_as(AudioUrl, REMOTE_AUDIO_FILE) orjson_dumps(url) @pytest.mark.parametrize( 'path_to_file', [ *[file for file in AUDIO_FILES], REMOTE_AUDIO_FILE, ], ) def test_validation(path_to_file): url = parse_obj_as(AudioUrl, path_to_file) assert isinstance(url, AudioUrl) assert isinstance(url, str) @pytest.mark.parametrize( 'path_to_file', [ 'illegal', 'https://www.google.com', 'my/local/text/file.txt', 'my/local/text/file.png', ], ) def test_illegal_validation(path_to_file): with pytest.raises(ValueError, match='AudioUrl'): parse_obj_as(AudioUrl, path_to_file) @pytest.mark.proto @pytest.mark.slow @pytest.mark.internet @pytest.mark.parametrize( 'file_url', [*AUDIO_FILES, REMOTE_AUDIO_FILE], ) def test_proto_audio_url(file_url): uri = parse_obj_as(AudioUrl, file_url) proto = uri._to_node_protobuf() assert 'audio_url' in str(proto)
# Copyright (c) OpenMMLab. All rights reserved. import asyncio from argparse import ArgumentParser import mmcv from mmdet.apis import (async_inference_detector, inference_detector, init_detector) from mmdet.registry import VISUALIZERS def parse_args(): parser = ArgumentParser() parser.add_argument('img', help='Image file') parser.add_argument('config', help='Config file') parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument('--out-file', default=None, help='Path to output file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--palette', default='none', choices=['coco', 'voc', 'citys', 'random', 'none'], help='Color palette used for visualization') parser.add_argument( '--score-thr', type=float, default=0.3, help='bbox score threshold') parser.add_argument( '--async-test', action='store_true', help='whether to set async options for async inference.') args = parser.parse_args() return args def main(args): # TODO: Support inference of image directory. # build the model from a config file and a checkpoint file model = init_detector( args.config, args.checkpoint, palette=args.palette, device=args.device) # init visualizer visualizer = VISUALIZERS.build(model.cfg.visualizer) # the dataset_meta is loaded from the checkpoint and # then pass to the model in init_detector visualizer.dataset_meta = model.dataset_meta # test a single image result = inference_detector(model, args.img) # show the results img = mmcv.imread(args.img) img = mmcv.imconvert(img, 'bgr', 'rgb') visualizer.add_datasample( 'result', img, data_sample=result, draw_gt=False, show=args.out_file is None, wait_time=0, out_file=args.out_file, pred_score_thr=args.score_thr) async def async_main(args): # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # init visualizer visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta # test a single image tasks = asyncio.create_task(async_inference_detector(model, args.img)) result = await asyncio.gather(tasks) # show the results img = mmcv.imread(args.img) img = mmcv.imconvert(img, 'bgr', 'rgb') visualizer.add_datasample( 'result', img, pred_sample=result[0], show=args.out_file is None, wait_time=0, out_file=args.out_file, pred_score_thr=args.score_thr) if __name__ == '__main__': args = parse_args() assert not args.async_test, 'async inference is not supported yet.' if args.async_test: asyncio.run(async_main(args)) else: main(args)
# Copyright (c) OpenMMLab. All rights reserved. import asyncio from argparse import ArgumentParser import mmcv from mmdet.apis import (async_inference_detector, inference_detector, init_detector) from mmdet.registry import VISUALIZERS from mmdet.utils import register_all_modules def parse_args(): parser = ArgumentParser() parser.add_argument('img', help='Image file') parser.add_argument('config', help='Config file') parser.add_argument('checkpoint', help='Checkpoint file') parser.add_argument('--out-file', default=None, help='Path to output file') parser.add_argument( '--device', default='cuda:0', help='Device used for inference') parser.add_argument( '--palette', default='none', choices=['coco', 'voc', 'citys', 'random', 'none'], help='Color palette used for visualization') parser.add_argument( '--score-thr', type=float, default=0.3, help='bbox score threshold') parser.add_argument( '--async-test', action='store_true', help='whether to set async options for async inference.') args = parser.parse_args() return args def main(args): # register all modules in mmdet into the registries register_all_modules() # TODO: Support inference of image directory. # build the model from a config file and a checkpoint file model = init_detector( args.config, args.checkpoint, palette=args.palette, device=args.device) # init visualizer visualizer = VISUALIZERS.build(model.cfg.visualizer) # the dataset_meta is loaded from the checkpoint and # then pass to the model in init_detector visualizer.dataset_meta = model.dataset_meta # test a single image result = inference_detector(model, args.img) # show the results img = mmcv.imread(args.img) img = mmcv.imconvert(img, 'bgr', 'rgb') visualizer.add_datasample( 'result', img, data_sample=result, draw_gt=False, show=args.out_file is None, wait_time=0, out_file=args.out_file, pred_score_thr=args.score_thr) async def async_main(args): # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # init visualizer visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta # test a single image tasks = asyncio.create_task(async_inference_detector(model, args.img)) result = await asyncio.gather(tasks) # show the results img = mmcv.imread(args.img) img = mmcv.imconvert(img, 'bgr', 'rgb') visualizer.add_datasample( 'result', img, pred_sample=result[0], show=args.out_file is None, wait_time=0, out_file=args.out_file, pred_score_thr=args.score_thr) if __name__ == '__main__': args = parse_args() assert not args.async_test, 'async inference is not supported yet.' if args.async_test: asyncio.run(async_main(args)) else: main(args)
from langchain_core.prompts.chat import ( ChatPromptTemplate, ) from langchain_core.prompts.prompt import PromptTemplate from langchain.chains.prompt_selector import ConditionalPromptSelector, is_chat_model DEFAULT_REFINE_PROMPT_TMPL = ( "The original question is as follows: {question}\n" "We have provided an existing answer: {existing_answer}\n" "We have the opportunity to refine the existing answer " "(only if needed) with some more context below.\n" "------------\n" "{context_str}\n" "------------\n" "Given the new context, refine the original answer to better " "answer the question. " "If the context isn't useful, return the original answer." ) DEFAULT_REFINE_PROMPT = PromptTemplate.from_template(DEFAULT_REFINE_PROMPT_TMPL) refine_template = ( "We have the opportunity to refine the existing answer " "(only if needed) with some more context below.\n" "------------\n" "{context_str}\n" "------------\n" "Given the new context, refine the original answer to better " "answer the question. " "If the context isn't useful, return the original answer." ) CHAT_REFINE_PROMPT = ChatPromptTemplate.from_messages( [ ("human", "{question}"), ("ai", "{existing_answer}"), ("human", refine_template), ] ) REFINE_PROMPT_SELECTOR = ConditionalPromptSelector( default_prompt=DEFAULT_REFINE_PROMPT, conditionals=[(is_chat_model, CHAT_REFINE_PROMPT)], ) DEFAULT_TEXT_QA_PROMPT_TMPL = ( "Context information is below. \n" "------------\n" "{context_str}\n" "------------\n" "Given the context information and not prior knowledge, " "answer the question: {question}\n" ) DEFAULT_TEXT_QA_PROMPT = PromptTemplate.from_template(DEFAULT_TEXT_QA_PROMPT_TMPL) chat_qa_prompt_template = ( "Context information is below.\n" "------------\n" "{context_str}\n" "------------\n" "Given the context information and not prior knowledge, " "answer any questions" ) CHAT_QUESTION_PROMPT = ChatPromptTemplate.from_messages( [ ("system", chat_qa_prompt_template), ("human", "{question}"), ] ) QUESTION_PROMPT_SELECTOR = ConditionalPromptSelector( default_prompt=DEFAULT_TEXT_QA_PROMPT, conditionals=[(is_chat_model, CHAT_QUESTION_PROMPT)], )
# flake8: noqa from langchain.chains.prompt_selector import ConditionalPromptSelector, is_chat_model from langchain_core.prompts.chat import ( AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, ) from langchain_core.prompts.prompt import PromptTemplate DEFAULT_REFINE_PROMPT_TMPL = ( "The original question is as follows: {question}\n" "We have provided an existing answer: {existing_answer}\n" "We have the opportunity to refine the existing answer " "(only if needed) with some more context below.\n" "------------\n" "{context_str}\n" "------------\n" "Given the new context, refine the original answer to better " "answer the question. " "If the context isn't useful, return the original answer." ) DEFAULT_REFINE_PROMPT = PromptTemplate.from_template(DEFAULT_REFINE_PROMPT_TMPL) refine_template = ( "We have the opportunity to refine the existing answer " "(only if needed) with some more context below.\n" "------------\n" "{context_str}\n" "------------\n" "Given the new context, refine the original answer to better " "answer the question. " "If the context isn't useful, return the original answer." ) CHAT_REFINE_PROMPT = ChatPromptTemplate.from_messages( [ ("human", "{question}"), ("ai", "{existing_answer}"), ("human", refine_template), ] ) REFINE_PROMPT_SELECTOR = ConditionalPromptSelector( default_prompt=DEFAULT_REFINE_PROMPT, conditionals=[(is_chat_model, CHAT_REFINE_PROMPT)], ) DEFAULT_TEXT_QA_PROMPT_TMPL = ( "Context information is below. \n" "------------\n" "{context_str}\n" "------------\n" "Given the context information and not prior knowledge, " "answer the question: {question}\n" ) DEFAULT_TEXT_QA_PROMPT = PromptTemplate.from_template(DEFAULT_TEXT_QA_PROMPT_TMPL) chat_qa_prompt_template = ( "Context information is below.\n" "------------\n" "{context_str}\n" "------------\n" "Given the context information and not prior knowledge, " "answer any questions" ) CHAT_QUESTION_PROMPT = ChatPromptTemplate.from_messages( [ ("system", chat_qa_prompt_template), ("human", "{question}"), ] ) QUESTION_PROMPT_SELECTOR = ConditionalPromptSelector( default_prompt=DEFAULT_TEXT_QA_PROMPT, conditionals=[(is_chat_model, CHAT_QUESTION_PROMPT)], )
"""Query Understanding agent pack.""" from typing import Any, Dict, List, Optional from llama_index.core.agent import AgentRunner from llama_index.core.callbacks import CallbackManager from llama_index.core.llama_pack.base import BaseLlamaPack from llama_index.core.llms.llm import LLM from llama_index.core.tools.types import BaseTool from llama_index.llms.openai import OpenAI from .step import QueryUnderstandingAgentWorker class QueryUnderstandingAgentPack(BaseLlamaPack): """ LLMCompilerAgent pack. Args: tools (List[BaseTool]): List of tools to use. llm (Optional[LLM]): LLM to use. """ def __init__( self, tools: List[BaseTool], llm: Optional[LLM] = None, callback_manager: Optional[CallbackManager] = None, agent_worker_kwargs: Optional[Dict[str, Any]] = None, agent_runner_kwargs: Optional[Dict[str, Any]] = None, ) -> None: """Init params.""" self.llm = llm or OpenAI(model="gpt-4") self.callback_manager = callback_manager or self.llm.callback_manager self.agent_worker = QueryUnderstandingAgentWorker.from_tools( tools, llm=llm, verbose=True, callback_manager=self.callback_manager, **(agent_worker_kwargs or {}) ) self.agent = AgentRunner( self.agent_worker, callback_manager=self.callback_manager, **(agent_runner_kwargs or {}) ) def get_modules(self) -> Dict[str, Any]: """Get modules.""" return { "llm": self.llm, "callback_manager": self.callback_manager, "agent_worker": self.agent_worker, "agent": self.agent, } def run(self, *args: Any, **kwargs: Any) -> Any: """Run the pipeline.""" return self.agent.chat(*args, **kwargs)
"""Query Understanding agent pack.""" from typing import Any, Dict, List, Optional from llama_index.core.agent import AgentRunner from llama_index.core.callbacks import CallbackManager from llama_index.core.llama_pack.base import BaseLlamaPack from llama_index.core.llms.llm import LLM from llama_index.core.tools.types import BaseTool from llama_index.llms.openai import OpenAI from .step import QueryUnderstandingAgentWorker class QueryUnderstandingAgentPack(BaseLlamaPack): """LLMCompilerAgent pack. Args: tools (List[BaseTool]): List of tools to use. llm (Optional[LLM]): LLM to use. """ def __init__( self, tools: List[BaseTool], llm: Optional[LLM] = None, callback_manager: Optional[CallbackManager] = None, agent_worker_kwargs: Optional[Dict[str, Any]] = None, agent_runner_kwargs: Optional[Dict[str, Any]] = None, ) -> None: """Init params.""" self.llm = llm or OpenAI(model="gpt-4") self.callback_manager = callback_manager or self.llm.callback_manager self.agent_worker = QueryUnderstandingAgentWorker.from_tools( tools, llm=llm, verbose=True, callback_manager=self.callback_manager, **(agent_worker_kwargs or {}) ) self.agent = AgentRunner( self.agent_worker, callback_manager=self.callback_manager, **(agent_runner_kwargs or {}) ) def get_modules(self) -> Dict[str, Any]: """Get modules.""" return { "llm": self.llm, "callback_manager": self.callback_manager, "agent_worker": self.agent_worker, "agent": self.agent, } def run(self, *args: Any, **kwargs: Any) -> Any: """Run the pipeline.""" return self.agent.chat(*args, **kwargs)
import pytest from langchain.evaluation import ExactMatchStringEvaluator @pytest.fixture def exact_match_string_evaluator() -> ExactMatchStringEvaluator: """Create an ExactMatchStringEvaluator with default configuration.""" return ExactMatchStringEvaluator() @pytest.fixture def exact_match_string_evaluator_ignore_case() -> ExactMatchStringEvaluator: """Create an ExactMatchStringEvaluator with ignore_case set to True.""" return ExactMatchStringEvaluator(ignore_case=True) def test_default_exact_matching( exact_match_string_evaluator: ExactMatchStringEvaluator, ) -> None: prediction = "Mindy is the CTO" reference = "Mindy is the CTO" result = exact_match_string_evaluator.evaluate_strings( prediction=prediction, reference=reference, ) assert result["score"] == 1.0 reference = "Mindy is the CEO" result = exact_match_string_evaluator.evaluate_strings( prediction=prediction, reference=reference, ) assert result["score"] == 0.0 def test_exact_matching_with_ignore_case( exact_match_string_evaluator_ignore_case: ExactMatchStringEvaluator, ) -> None: prediction = "Mindy is the CTO" reference = "mindy is the cto" result = exact_match_string_evaluator_ignore_case.evaluate_strings( prediction=prediction, reference=reference, ) assert result["score"] == 1.0 reference = "mindy is the CEO" result = exact_match_string_evaluator_ignore_case.evaluate_strings( prediction=prediction, reference=reference, ) assert result["score"] == 0.0
import pytest from langchain.evaluation import ExactMatchStringEvaluator @pytest.fixture def exact_match_string_evaluator() -> ExactMatchStringEvaluator: """Create an ExactMatchStringEvaluator with default configuration.""" return ExactMatchStringEvaluator() @pytest.fixture def exact_match_string_evaluator_ignore_case() -> ExactMatchStringEvaluator: """Create an ExactMatchStringEvaluator with ignore_case set to True.""" return ExactMatchStringEvaluator(ignore_case=True) def test_default_exact_matching( exact_match_string_evaluator: ExactMatchStringEvaluator, ) -> None: prediction = "Mindy is the CTO" reference = "Mindy is the CTO" result = exact_match_string_evaluator.evaluate_strings( prediction=prediction, reference=reference ) assert result["score"] == 1.0 reference = "Mindy is the CEO" result = exact_match_string_evaluator.evaluate_strings( prediction=prediction, reference=reference ) assert result["score"] == 0.0 def test_exact_matching_with_ignore_case( exact_match_string_evaluator_ignore_case: ExactMatchStringEvaluator, ) -> None: prediction = "Mindy is the CTO" reference = "mindy is the cto" result = exact_match_string_evaluator_ignore_case.evaluate_strings( prediction=prediction, reference=reference ) assert result["score"] == 1.0 reference = "mindy is the CEO" result = exact_match_string_evaluator_ignore_case.evaluate_strings( prediction=prediction, reference=reference ) assert result["score"] == 0.0
import grpc import pytest from jina import Flow from jina.clients import Client from jina.serve.helper import get_server_side_grpc_options from jina.serve.runtimes.gateway.grpc import GRPCGateway from tests import random_docs @pytest.fixture(scope='function') def flow_with_grpc(): class AuthInterceptor(grpc.aio.ServerInterceptor): def __init__(self, key): self._valid_metadata = ('rpc-auth-header', key) def deny(_, context): context.abort(grpc.StatusCode.UNAUTHENTICATED, 'Invalid key') self._deny = grpc.unary_unary_rpc_method_handler(deny) async def intercept_service(self, continuation, handler_call_details): meta = handler_call_details.invocation_metadata metas_dicts = {m.key: m.value for m in meta} assert 'rpc-auth-header' in metas_dicts assert ( metas_dicts['rpc-auth-header'] == 'access_key' ), f'Invalid access key detected, got {metas_dicts["rpc-auth-header"]}' for m in meta: if m == self._valid_metadata: return await continuation(handler_call_details) return self._deny class AlternativeGRPCGateway(GRPCGateway): def __init__(self, *args, **kwargs): super(AlternativeGRPCGateway, self).__init__(*args, **kwargs) self.server = grpc.aio.server( interceptors=(AuthInterceptor('access_key'),), options=get_server_side_grpc_options(self.grpc_server_options), ) return Flow(protocol='grpc', uses=AlternativeGRPCGateway).add() def test_client_grpc_kwargs(flow_with_grpc): with flow_with_grpc: client = Client( port=flow_with_grpc.port, host='localhost', protocol='grpc', ) meta_data = (('rpc-auth-header', 'invalid_access_key'),) try: client.post('', random_docs(1), request_size=1, metadata=meta_data) except Exception as exc: assert 'Invalid access key detected, got invalid_access_key' in repr(exc)
import grpc import pytest from jina import Flow from jina.clients import Client from jina.serve.helper import get_server_side_grpc_options from jina.serve.runtimes.gateway.grpc import GRPCGateway from tests import random_docs @pytest.fixture(scope='function') def flow_with_grpc(monkeypatch): class AuthInterceptor(grpc.aio.ServerInterceptor): def __init__(self, key): self._valid_metadata = ('rpc-auth-header', key) def deny(_, context): context.abort(grpc.StatusCode.UNAUTHENTICATED, 'Invalid key') self._deny = grpc.unary_unary_rpc_method_handler(deny) async def intercept_service(self, continuation, handler_call_details): meta = handler_call_details.invocation_metadata metas_dicts = {m.key: m.value for m in meta} assert 'rpc-auth-header' in metas_dicts assert ( metas_dicts['rpc-auth-header'] == 'access_key' ), f'Invalid access key detected, got {metas_dicts["rpc-auth-header"]}' for m in meta: if m == self._valid_metadata: return await continuation(handler_call_details) return self._deny class AlternativeGRPCGateway(GRPCGateway): def __init__(self, *args, **kwargs): super(AlternativeGRPCGateway, self).__init__(*args, **kwargs) self.server = grpc.aio.server( interceptors=(AuthInterceptor('access_key'),), options=get_server_side_grpc_options(self.grpc_server_options), ) return Flow(protocol='grpc', uses=AlternativeGRPCGateway).add() def test_client_grpc_kwargs(flow_with_grpc): with flow_with_grpc: client = Client( port=flow_with_grpc.port, host='localhost', protocol='grpc', ) meta_data = (('rpc-auth-header', 'invalid_access_key'),) try: client.post('', random_docs(1), request_size=1, metadata=meta_data) except Exception as exc: assert 'Invalid access key detected, got invalid_access_key' in repr(exc)
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.document_loaders import ( GoogleApiClient, GoogleApiYoutubeLoader, YoutubeLoader, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "YoutubeLoader": "langchain_community.document_loaders", "GoogleApiYoutubeLoader": "langchain_community.document_loaders", "GoogleApiClient": "langchain_community.document_loaders", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "GoogleApiClient", "GoogleApiYoutubeLoader", "YoutubeLoader", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.document_loaders import ( GoogleApiClient, GoogleApiYoutubeLoader, YoutubeLoader, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "YoutubeLoader": "langchain_community.document_loaders", "GoogleApiYoutubeLoader": "langchain_community.document_loaders", "GoogleApiClient": "langchain_community.document_loaders", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "YoutubeLoader", "GoogleApiYoutubeLoader", "GoogleApiClient", ]
from typing import Optional import numpy as np import torch from docarray import DocumentArray from docarray.base_document import BaseDocument from docarray.typing import NdArray, TorchTensor def test_proto_simple(): class CustomDoc(BaseDocument): text: str doc = CustomDoc(text='hello') CustomDoc.from_protobuf(doc.to_protobuf()) def test_proto_ndarray(): class CustomDoc(BaseDocument): tensor: NdArray tensor = np.zeros((3, 224, 224)) doc = CustomDoc(tensor=tensor) new_doc = CustomDoc.from_protobuf(doc.to_protobuf()) assert (new_doc.tensor == tensor).all() def test_proto_with_nested_doc(): class CustomInnerDoc(BaseDocument): tensor: NdArray class CustomDoc(BaseDocument): text: str inner: CustomInnerDoc doc = CustomDoc(text='hello', inner=CustomInnerDoc(tensor=np.zeros((3, 224, 224)))) CustomDoc.from_protobuf(doc.to_protobuf()) def test_proto_with_chunks_doc(): class CustomInnerDoc(BaseDocument): tensor: NdArray class CustomDoc(BaseDocument): text: str chunks: DocumentArray[CustomInnerDoc] doc = CustomDoc( text='hello', chunks=DocumentArray[CustomInnerDoc]( [CustomInnerDoc(tensor=np.zeros((3, 224, 224))) for _ in range(5)], ), ) new_doc = CustomDoc.from_protobuf(doc.to_protobuf()) for chunk1, chunk2 in zip(doc.chunks, new_doc.chunks): assert (chunk1.tensor == chunk2.tensor).all() def test_proto_with_nested_doc_pytorch(): class CustomInnerDoc(BaseDocument): tensor: TorchTensor class CustomDoc(BaseDocument): text: str inner: CustomInnerDoc doc = CustomDoc( text='hello', inner=CustomInnerDoc(tensor=torch.zeros((3, 224, 224))) ) CustomDoc.from_protobuf(doc.to_protobuf()) def test_proto_with_chunks_doc_pytorch(): class CustomInnerDoc(BaseDocument): tensor: TorchTensor class CustomDoc(BaseDocument): text: str chunks: DocumentArray[CustomInnerDoc] doc = CustomDoc( text='hello', chunks=DocumentArray[CustomInnerDoc]( [CustomInnerDoc(tensor=torch.zeros((3, 224, 224))) for _ in range(5)], ), ) new_doc = CustomDoc.from_protobuf(doc.to_protobuf()) for chunk1, chunk2 in zip(doc.chunks, new_doc.chunks): assert (chunk1.tensor == chunk2.tensor).all() def test_optional_field_in_doc(): class CustomDoc(BaseDocument): text: Optional[str] CustomDoc.from_protobuf(CustomDoc().to_protobuf()) def test_optional_field_nested_in_doc(): class InnerDoc(BaseDocument): title: str class CustomDoc(BaseDocument): text: Optional[InnerDoc] CustomDoc.from_protobuf(CustomDoc().to_protobuf())
from typing import Optional import numpy as np import torch from docarray import DocumentArray from docarray.document import BaseDocument from docarray.typing import NdArray, TorchTensor def test_proto_simple(): class CustomDoc(BaseDocument): text: str doc = CustomDoc(text='hello') CustomDoc.from_protobuf(doc.to_protobuf()) def test_proto_ndarray(): class CustomDoc(BaseDocument): tensor: NdArray tensor = np.zeros((3, 224, 224)) doc = CustomDoc(tensor=tensor) new_doc = CustomDoc.from_protobuf(doc.to_protobuf()) assert (new_doc.tensor == tensor).all() def test_proto_with_nested_doc(): class CustomInnerDoc(BaseDocument): tensor: NdArray class CustomDoc(BaseDocument): text: str inner: CustomInnerDoc doc = CustomDoc(text='hello', inner=CustomInnerDoc(tensor=np.zeros((3, 224, 224)))) CustomDoc.from_protobuf(doc.to_protobuf()) def test_proto_with_chunks_doc(): class CustomInnerDoc(BaseDocument): tensor: NdArray class CustomDoc(BaseDocument): text: str chunks: DocumentArray[CustomInnerDoc] doc = CustomDoc( text='hello', chunks=DocumentArray[CustomInnerDoc]( [CustomInnerDoc(tensor=np.zeros((3, 224, 224))) for _ in range(5)], ), ) new_doc = CustomDoc.from_protobuf(doc.to_protobuf()) for chunk1, chunk2 in zip(doc.chunks, new_doc.chunks): assert (chunk1.tensor == chunk2.tensor).all() def test_proto_with_nested_doc_pytorch(): class CustomInnerDoc(BaseDocument): tensor: TorchTensor class CustomDoc(BaseDocument): text: str inner: CustomInnerDoc doc = CustomDoc( text='hello', inner=CustomInnerDoc(tensor=torch.zeros((3, 224, 224))) ) CustomDoc.from_protobuf(doc.to_protobuf()) def test_proto_with_chunks_doc_pytorch(): class CustomInnerDoc(BaseDocument): tensor: TorchTensor class CustomDoc(BaseDocument): text: str chunks: DocumentArray[CustomInnerDoc] doc = CustomDoc( text='hello', chunks=DocumentArray[CustomInnerDoc]( [CustomInnerDoc(tensor=torch.zeros((3, 224, 224))) for _ in range(5)], ), ) new_doc = CustomDoc.from_protobuf(doc.to_protobuf()) for chunk1, chunk2 in zip(doc.chunks, new_doc.chunks): assert (chunk1.tensor == chunk2.tensor).all() def test_optional_field_in_doc(): class CustomDoc(BaseDocument): text: Optional[str] CustomDoc.from_protobuf(CustomDoc().to_protobuf()) def test_optional_field_nested_in_doc(): class InnerDoc(BaseDocument): title: str class CustomDoc(BaseDocument): text: Optional[InnerDoc] CustomDoc.from_protobuf(CustomDoc().to_protobuf())
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.document_loaders.pyspark_dataframe import ( PySparkDataFrameLoader, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "PySparkDataFrameLoader": "langchain_community.document_loaders.pyspark_dataframe", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = ["PySparkDataFrameLoader"]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.document_loaders.pyspark_dataframe import ( PySparkDataFrameLoader, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "PySparkDataFrameLoader": "langchain_community.document_loaders.pyspark_dataframe" } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = ["PySparkDataFrameLoader"]
"""Checks the bundled license is installed with the wheel.""" import platform import site from itertools import chain from pathlib import Path site_packages = site.getsitepackages() site_packages_path = (Path(p) for p in site_packages) try: distinfo_path = next( chain( s for site_package in site_packages_path for s in site_package.glob("scikit_learn-*.dist-info") ) ) except StopIteration as e: raise RuntimeError("Unable to find scikit-learn's dist-info") from e license_text = (distinfo_path / "licenses" / "COPYING").read_text() assert "Copyright (c)" in license_text assert ( "This binary distribution of scikit-learn also bundles the following software" in license_text ), f"Unable to find bundled license for {platform.system()}"
"""Checks the bundled license is installed with the wheel.""" import platform import site from itertools import chain from pathlib import Path site_packages = site.getsitepackages() site_packages_path = (Path(p) for p in site_packages) try: distinfo_path = next( chain( s for site_package in site_packages_path for s in site_package.glob("scikit_learn-*.dist-info") ) ) except StopIteration as e: raise RuntimeError("Unable to find scikit-learn's dist-info") from e license_text = (distinfo_path / "COPYING").read_text() assert "Copyright (c)" in license_text assert ( "This binary distribution of scikit-learn also bundles the following software" in license_text ), f"Unable to find bundled license for {platform.system()}"
from typing import Any, Literal, Optional, Union from exa_py import Exa # type: ignore[untyped-import] from exa_py.api import ( HighlightsContentsOptions, # type: ignore[untyped-import] TextContentsOptions, # type: ignore[untyped-import] ) from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document from langchain_core.retrievers import BaseRetriever from pydantic import Field, SecretStr, model_validator from langchain_exa._utilities import initialize_client def _get_metadata(result: Any) -> dict[str, Any]: """Get the metadata from a result object.""" metadata = { "title": result.title, "url": result.url, "id": result.id, "score": result.score, "published_date": result.published_date, "author": result.author, } if getattr(result, "highlights"): metadata["highlights"] = result.highlights if getattr(result, "highlight_scores"): metadata["highlight_scores"] = result.highlight_scores return metadata class ExaSearchRetriever(BaseRetriever): """Exa Search retriever.""" k: int = 10 # num_results """The number of search results to return.""" include_domains: Optional[list[str]] = None """A list of domains to include in the search.""" exclude_domains: Optional[list[str]] = None """A list of domains to exclude from the search.""" start_crawl_date: Optional[str] = None """The start date for the crawl (in YYYY-MM-DD format).""" end_crawl_date: Optional[str] = None """The end date for the crawl (in YYYY-MM-DD format).""" start_published_date: Optional[str] = None """The start date for when the document was published (in YYYY-MM-DD format).""" end_published_date: Optional[str] = None """The end date for when the document was published (in YYYY-MM-DD format).""" use_autoprompt: Optional[bool] = None """Whether to use autoprompt for the search.""" type: str = "neural" """The type of search, 'keyword' or 'neural'. Default: neural""" highlights: Optional[Union[HighlightsContentsOptions, bool]] = None """Whether to set the page content to the highlights of the results.""" text_contents_options: Union[TextContentsOptions, Literal[True]] = True """How to set the page content of the results""" client: Exa = Field(default=None) exa_api_key: SecretStr = Field(default=None) exa_base_url: Optional[str] = None @model_validator(mode="before") @classmethod def validate_environment(cls, values: dict) -> Any: """Validate the environment.""" values = initialize_client(values) return values def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun ) -> list[Document]: response = self.client.search_and_contents( # type: ignore[misc] query, num_results=self.k, text=self.text_contents_options, highlights=self.highlights, # type: ignore include_domains=self.include_domains, exclude_domains=self.exclude_domains, start_crawl_date=self.start_crawl_date, end_crawl_date=self.end_crawl_date, start_published_date=self.start_published_date, end_published_date=self.end_published_date, use_autoprompt=self.use_autoprompt, ) results = response.results return [ Document( page_content=(result.text), metadata=_get_metadata(result), ) for result in results ]
from typing import Any, Dict, List, Literal, Optional, Union from exa_py import Exa # type: ignore[untyped-import] from exa_py.api import ( HighlightsContentsOptions, # type: ignore[untyped-import] TextContentsOptions, # type: ignore[untyped-import] ) from langchain_core.callbacks import CallbackManagerForRetrieverRun from langchain_core.documents import Document from langchain_core.retrievers import BaseRetriever from pydantic import Field, SecretStr, model_validator from langchain_exa._utilities import initialize_client def _get_metadata(result: Any) -> Dict[str, Any]: """Get the metadata from a result object.""" metadata = { "title": result.title, "url": result.url, "id": result.id, "score": result.score, "published_date": result.published_date, "author": result.author, } if getattr(result, "highlights"): metadata["highlights"] = result.highlights if getattr(result, "highlight_scores"): metadata["highlight_scores"] = result.highlight_scores return metadata class ExaSearchRetriever(BaseRetriever): """Exa Search retriever.""" k: int = 10 # num_results """The number of search results to return.""" include_domains: Optional[List[str]] = None """A list of domains to include in the search.""" exclude_domains: Optional[List[str]] = None """A list of domains to exclude from the search.""" start_crawl_date: Optional[str] = None """The start date for the crawl (in YYYY-MM-DD format).""" end_crawl_date: Optional[str] = None """The end date for the crawl (in YYYY-MM-DD format).""" start_published_date: Optional[str] = None """The start date for when the document was published (in YYYY-MM-DD format).""" end_published_date: Optional[str] = None """The end date for when the document was published (in YYYY-MM-DD format).""" use_autoprompt: Optional[bool] = None """Whether to use autoprompt for the search.""" type: str = "neural" """The type of search, 'keyword' or 'neural'. Default: neural""" highlights: Optional[Union[HighlightsContentsOptions, bool]] = None """Whether to set the page content to the highlights of the results.""" text_contents_options: Union[TextContentsOptions, Literal[True]] = True """How to set the page content of the results""" client: Exa = Field(default=None) exa_api_key: SecretStr = Field(default=None) exa_base_url: Optional[str] = None @model_validator(mode="before") @classmethod def validate_environment(cls, values: Dict) -> Any: """Validate the environment.""" values = initialize_client(values) return values def _get_relevant_documents( self, query: str, *, run_manager: CallbackManagerForRetrieverRun ) -> List[Document]: response = self.client.search_and_contents( # type: ignore[misc] query, num_results=self.k, text=self.text_contents_options, highlights=self.highlights, # type: ignore include_domains=self.include_domains, exclude_domains=self.exclude_domains, start_crawl_date=self.start_crawl_date, end_crawl_date=self.end_crawl_date, start_published_date=self.start_published_date, end_published_date=self.end_published_date, use_autoprompt=self.use_autoprompt, ) results = response.results return [ Document( page_content=(result.text), metadata=_get_metadata(result), ) for result in results ]
_base_ = './decoupled_solo_r50_fpn_3x_coco.py' # model settings model = dict( mask_head=dict( type='DecoupledSOLOLightHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, strides=[8, 8, 16, 32, 32], scale_ranges=((1, 64), (32, 128), (64, 256), (128, 512), (256, 2048)), pos_scale=0.2, num_grids=[40, 36, 24, 16, 12], cls_down_index=0, loss_mask=dict( type='DiceLoss', use_sigmoid=True, activate=False, loss_weight=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), norm_cfg=dict(type='GN', num_groups=32, requires_grad=True))) train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( # TODO: Update after mmcv.RandomChoiceResize finish refactor type='RandomChoiceResize', scales=[(852, 512), (852, 480), (852, 448), (852, 416), (852, 384), (852, 352)], resize_cfg=dict(type='Resize', keep_ratio=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=(852, 512), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict(dataset=dict(pipeline=train_pipeline)) val_dataloader = dict(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader
_base_ = './decoupled_solo_r50_fpn_3x_coco.py' # model settings model = dict( mask_head=dict( type='DecoupledSOLOLightHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, strides=[8, 8, 16, 32, 32], scale_ranges=((1, 64), (32, 128), (64, 256), (128, 512), (256, 2048)), pos_scale=0.2, num_grids=[40, 36, 24, 16, 12], cls_down_index=0, loss_mask=dict( type='DiceLoss', use_sigmoid=True, activate=False, loss_weight=3.0), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), norm_cfg=dict(type='GN', num_groups=32, requires_grad=True))) 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=[(852, 512), (852, 480), (852, 448), (852, 416), (852, 384), (852, 352)], multiscale_mode='value', keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(852, 512), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( train=dict(pipeline=train_pipeline), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline))
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.ops.nn import average_pool from keras.src.ops.nn import batch_normalization from keras.src.ops.nn import binary_crossentropy from keras.src.ops.nn import categorical_crossentropy from keras.src.ops.nn import celu from keras.src.ops.nn import conv from keras.src.ops.nn import conv_transpose from keras.src.ops.nn import ctc_decode from keras.src.ops.nn import ctc_loss from keras.src.ops.nn import depthwise_conv from keras.src.ops.nn import dot_product_attention from keras.src.ops.nn import elu from keras.src.ops.nn import gelu from keras.src.ops.nn import glu from keras.src.ops.nn import hard_sigmoid from keras.src.ops.nn import hard_silu from keras.src.ops.nn import hard_silu as hard_swish from keras.src.ops.nn import hard_tanh from keras.src.ops.nn import leaky_relu from keras.src.ops.nn import log_sigmoid from keras.src.ops.nn import log_softmax from keras.src.ops.nn import max_pool from keras.src.ops.nn import moments from keras.src.ops.nn import multi_hot from keras.src.ops.nn import normalize from keras.src.ops.nn import one_hot from keras.src.ops.nn import psnr from keras.src.ops.nn import relu from keras.src.ops.nn import relu6 from keras.src.ops.nn import selu from keras.src.ops.nn import separable_conv from keras.src.ops.nn import sigmoid from keras.src.ops.nn import silu from keras.src.ops.nn import silu as swish from keras.src.ops.nn import softmax from keras.src.ops.nn import softplus from keras.src.ops.nn import softsign from keras.src.ops.nn import sparse_categorical_crossentropy
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.ops.nn import average_pool from keras.src.ops.nn import batch_normalization from keras.src.ops.nn import binary_crossentropy from keras.src.ops.nn import categorical_crossentropy from keras.src.ops.nn import celu from keras.src.ops.nn import conv from keras.src.ops.nn import conv_transpose from keras.src.ops.nn import ctc_decode from keras.src.ops.nn import ctc_loss from keras.src.ops.nn import depthwise_conv from keras.src.ops.nn import dot_product_attention from keras.src.ops.nn import elu from keras.src.ops.nn import gelu from keras.src.ops.nn import glu from keras.src.ops.nn import hard_sigmoid from keras.src.ops.nn import hard_silu from keras.src.ops.nn import hard_silu as hard_swish from keras.src.ops.nn import leaky_relu from keras.src.ops.nn import log_sigmoid from keras.src.ops.nn import log_softmax from keras.src.ops.nn import max_pool from keras.src.ops.nn import moments from keras.src.ops.nn import multi_hot from keras.src.ops.nn import normalize from keras.src.ops.nn import one_hot from keras.src.ops.nn import psnr from keras.src.ops.nn import relu from keras.src.ops.nn import relu6 from keras.src.ops.nn import selu from keras.src.ops.nn import separable_conv from keras.src.ops.nn import sigmoid from keras.src.ops.nn import silu from keras.src.ops.nn import silu as swish from keras.src.ops.nn import softmax from keras.src.ops.nn import softplus from keras.src.ops.nn import softsign from keras.src.ops.nn import sparse_categorical_crossentropy
# flake8: noqa # Copyright 2020 The HuggingFace Datasets Authors and the TensorFlow Datasets Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position __version__ = "2.14.1" from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip _arrow_dataset.concatenate_datasets = concatenate_datasets _utils.DownloadConfig = DownloadConfig _utils.DownloadManager = DownloadManager _utils.DownloadMode = DownloadMode _deprecated_download_manager.DownloadConfig = DownloadConfig _deprecated_download_manager.DownloadMode = DownloadMode _deprecated_download_manager.DownloadManager = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
# flake8: noqa # Copyright 2020 The HuggingFace Datasets Authors and the TensorFlow Datasets Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Lint as: python3 # pylint: enable=line-too-long # pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position __version__ = "2.14.1.dev0" from .arrow_dataset import Dataset from .arrow_reader import ReadInstruction from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder from .combine import concatenate_datasets, interleave_datasets from .dataset_dict import DatasetDict, IterableDatasetDict from .download import * from .features import * from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled from .info import DatasetInfo, MetricInfo from .inspect import ( get_dataset_config_info, get_dataset_config_names, get_dataset_infos, get_dataset_split_names, inspect_dataset, inspect_metric, list_datasets, list_metrics, ) from .iterable_dataset import IterableDataset from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric from .metric import Metric from .splits import ( NamedSplit, NamedSplitAll, Split, SplitBase, SplitDict, SplitGenerator, SplitInfo, SubSplitInfo, percent, ) from .tasks import * from .utils import * from .utils import logging # deprecated modules from datasets import arrow_dataset as _arrow_dataset # isort:skip from datasets import utils as _utils # isort:skip from datasets.utils import download_manager as _deprecated_download_manager # isort:skip _arrow_dataset.concatenate_datasets = concatenate_datasets _utils.DownloadConfig = DownloadConfig _utils.DownloadManager = DownloadManager _utils.DownloadMode = DownloadMode _deprecated_download_manager.DownloadConfig = DownloadConfig _deprecated_download_manager.DownloadMode = DownloadMode _deprecated_download_manager.DownloadManager = DownloadManager del _arrow_dataset, _utils, _deprecated_download_manager
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from mmengine.data import InstanceData from parameterized import parameterized from mmdet.models.roi_heads import StandardRoIHead # noqa from mmdet.registry import MODELS from mmdet.testing import demo_mm_inputs, get_detector_cfg def _fake_roi_head(cfg_file): """Set a fake roi head config.""" model = get_detector_cfg(cfg_file) roi_head = model.roi_head rcnn_train_cfg = model.train_cfg.rcnn if model.train_cfg is not None \ else None roi_head.update(train_cfg=rcnn_train_cfg) return roi_head def _fake_proposals(img_metas, proposal_len): """Create a fake proposal list.""" results = [] for i in range(len(img_metas)): result = InstanceData(metainfo=img_metas[i]) proposal = torch.randn(proposal_len, 4).to(device='cuda') result.bboxes = proposal results.append(result) return results class TestCascadeRoIHead(TestCase): @parameterized.expand( ['cascade_rcnn/cascade_mask_rcnn_r50_fpn_1x_coco.py']) def test_init(self, cfg_file): """Test init standard RoI head.""" # Normal Cascade Mask R-CNN RoI head roi_head_cfg = _fake_roi_head(cfg_file) roi_head = MODELS.build(roi_head_cfg) assert roi_head.with_bbox assert roi_head.with_mask @parameterized.expand( ['cascade_rcnn/cascade_mask_rcnn_r50_fpn_1x_coco.py']) def test_cascade_roi_head_loss(self, cfg_file): """Tests standard roi head loss when truth is empty and non-empty.""" if not torch.cuda.is_available(): # RoI pooling only support in GPU return unittest.skip('test requires GPU and torch+cuda') s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'scale_factor': 1, }] roi_head_cfg = _fake_roi_head(cfg_file) roi_head = MODELS.build(roi_head_cfg) roi_head = roi_head.cuda() feats = [] for i in range(len(roi_head_cfg.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 1, s // (2**(i + 2)), s // (2**(i + 2))).to(device='cuda')) feats = tuple(feats) # When truth is non-empty then both cls, box, and mask loss # should be nonzero for random inputs proposal_list = _fake_proposals(img_metas, 100) packed_inputs = demo_mm_inputs( batch_size=1, image_shapes=[(s, s, 3)], num_items=[1], num_classes=4, with_mask=True) batch_data_samples = [] for i in range(len(packed_inputs)): batch_data_samples.append( packed_inputs[i]['data_sample'].to(device='cuda')) out = roi_head.loss(feats, proposal_list, batch_data_samples) for name, value in out.items(): if 'loss' in name: self.assertGreaterEqual( value.sum(), 0, msg='loss should be non-zero') # When there is no truth, the cls loss should be nonzero but # there should be no box and mask loss. proposal_list = _fake_proposals(img_metas, 100) packed_inputs = demo_mm_inputs( batch_size=1, image_shapes=[(s, s, 3)], num_items=[0], num_classes=4, with_mask=True) batch_data_samples = [] for i in range(len(packed_inputs)): batch_data_samples.append( packed_inputs[i]['data_sample'].to(device='cuda')) out = roi_head.loss(feats, proposal_list, batch_data_samples) for name, value in out.items(): if 'loss_cls' in name: self.assertGreaterEqual( value.sum(), 0, msg='loss should be non-zero') elif 'loss_bbox' in name or 'loss_mask' in name: self.assertEqual(value.sum(), 0)
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from mmengine.data import InstanceData from parameterized import parameterized from mmdet.models.roi_heads import StandardRoIHead # noqa from mmdet.registry import MODELS from mmdet.testing import demo_mm_inputs, get_detector_cfg def _fake_roi_head(cfg_file): """Set a fake roi head config.""" model = get_detector_cfg(cfg_file) roi_head = model.roi_head rcnn_train_cfg = model.train_cfg.rcnn if model.train_cfg is not None \ else None roi_head.update(train_cfg=rcnn_train_cfg) return roi_head def _fake_proposals(img_metas, proposal_len): """Create a fake proposal list.""" results = [] for i in range(len(img_metas)): result = InstanceData(metainfo=img_metas[i]) proposal = torch.randn(proposal_len, 4).to(device='cuda') result.bboxes = proposal results.append(result) return results class TestStandardRoIHead(TestCase): @parameterized.expand( ['cascade_rcnn/cascade_mask_rcnn_r50_fpn_1x_coco.py']) def test_init(self, cfg_file): """Test init standard RoI head.""" # Normal Cascade Mask R-CNN RoI head roi_head_cfg = _fake_roi_head(cfg_file) roi_head = MODELS.build(roi_head_cfg) assert roi_head.with_bbox assert roi_head.with_mask @parameterized.expand( ['cascade_rcnn/cascade_mask_rcnn_r50_fpn_1x_coco.py']) def test_cascade_roi_head_loss(self, cfg_file): """Tests standard roi head loss when truth is empty and non-empty.""" if not torch.cuda.is_available(): # RoI pooling only support in GPU return unittest.skip('test requires GPU and torch+cuda') s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'scale_factor': 1, }] roi_head_cfg = _fake_roi_head(cfg_file) roi_head = MODELS.build(roi_head_cfg) roi_head = roi_head.cuda() feats = [] for i in range(len(roi_head_cfg.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 1, s // (2**(i + 2)), s // (2**(i + 2))).to(device='cuda')) feats = tuple(feats) # When truth is non-empty then both cls, box, and mask loss # should be nonzero for random inputs proposal_list = _fake_proposals(img_metas, 100) packed_inputs = demo_mm_inputs( batch_size=1, image_shapes=[(s, s, 3)], num_items=[1], num_classes=4, with_mask=True) batch_data_samples = [] for i in range(len(packed_inputs)): batch_data_samples.append( packed_inputs[i]['data_sample'].to(device='cuda')) out = roi_head.forward_train(feats, proposal_list, batch_data_samples) for name, value in out.items(): if 'loss' in name: self.assertGreaterEqual( value.sum(), 0, msg='loss should be non-zero') # When there is no truth, the cls loss should be nonzero but # there should be no box and mask loss. proposal_list = _fake_proposals(img_metas, 100) packed_inputs = demo_mm_inputs( batch_size=1, image_shapes=[(s, s, 3)], num_items=[0], num_classes=4, with_mask=True) batch_data_samples = [] for i in range(len(packed_inputs)): batch_data_samples.append( packed_inputs[i]['data_sample'].to(device='cuda')) out = roi_head.forward_train(feats, proposal_list, batch_data_samples) for name, value in out.items(): if 'loss_cls' in name: self.assertGreaterEqual( value.sum(), 0, msg='loss should be non-zero') elif 'loss_bbox' in name or 'loss_mask' in name: self.assertEqual(value.sum(), 0)
#!/usr/bin/env python3 """Generate feature statistics for training set. Example: python global_stats.py --model-type librispeech --dataset-path /home/librispeech """ import json import logging import pathlib from argparse import ArgumentParser, RawTextHelpFormatter import torch import torchaudio from common import ( MODEL_TYPE_LIBRISPEECH, MODEL_TYPE_MUSTC, MODEL_TYPE_TEDLIUM3, piecewise_linear_log, spectrogram_transform, ) from must.dataset import MUSTC logger = logging.getLogger() def parse_args(): parser = ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter) parser.add_argument( "--model-type", type=str, choices=[MODEL_TYPE_LIBRISPEECH, MODEL_TYPE_TEDLIUM3, MODEL_TYPE_MUSTC], required=True ) parser.add_argument( "--dataset-path", required=True, type=pathlib.Path, help="Path to dataset. " "For LibriSpeech, all of 'train-clean-360', 'train-clean-100', and 'train-other-500' must exist.", ) parser.add_argument( "--output-path", default=pathlib.Path("global_stats.json"), type=pathlib.Path, help="File to save feature statistics to. (Default: './global_stats.json')", ) return parser.parse_args() def generate_statistics(samples): E_x = 0 E_x_2 = 0 N = 0 for idx, sample in enumerate(samples): mel_spec = spectrogram_transform(sample[0].squeeze()).transpose(1, 0) scaled_mel_spec = piecewise_linear_log(mel_spec) sum = scaled_mel_spec.sum(0) sq_sum = scaled_mel_spec.pow(2).sum(0) M = scaled_mel_spec.size(0) E_x = E_x * (N / (N + M)) + sum / (N + M) E_x_2 = E_x_2 * (N / (N + M)) + sq_sum / (N + M) N += M if idx % 100 == 0: logger.info(f"Processed {idx}") return E_x, (E_x_2 - E_x**2) ** 0.5 def get_dataset(args): if args.model_type == MODEL_TYPE_LIBRISPEECH: return torch.utils.data.ConcatDataset( [ torchaudio.datasets.LIBRISPEECH(args.dataset_path, url="train-clean-360"), torchaudio.datasets.LIBRISPEECH(args.dataset_path, url="train-clean-100"), torchaudio.datasets.LIBRISPEECH(args.dataset_path, url="train-other-500"), ] ) elif args.model_type == MODEL_TYPE_TEDLIUM3: return torchaudio.datasets.TEDLIUM(args.dataset_path, release="release3", subset="train") elif args.model_type == MODEL_TYPE_MUSTC: return MUSTC(args.dataset_path, subset="train") else: raise ValueError(f"Encountered unsupported model type {args.model_type}.") def cli_main(): args = parse_args() dataset = get_dataset(args) dataloader = torch.utils.data.DataLoader(dataset, num_workers=4) mean, stddev = generate_statistics(iter(dataloader)) json_str = json.dumps({"mean": mean.tolist(), "invstddev": (1 / stddev).tolist()}, indent=2) with open(args.output_path, "w") as f: f.write(json_str) if __name__ == "__main__": cli_main()
#!/usr/bin/env python3 """Generate feature statistics for training set. Example: python global_stats.py --model-type librispeech --dataset-path /home/librispeech """ import json import logging import pathlib from argparse import ArgumentParser, RawTextHelpFormatter import torch import torchaudio from common import ( MODEL_TYPE_LIBRISPEECH, MODEL_TYPE_TEDLIUM3, MODEL_TYPE_MUSTC, piecewise_linear_log, spectrogram_transform, ) from must.dataset import MUSTC logger = logging.getLogger() def parse_args(): parser = ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter) parser.add_argument( "--model-type", type=str, choices=[MODEL_TYPE_LIBRISPEECH, MODEL_TYPE_TEDLIUM3, MODEL_TYPE_MUSTC], required=True ) parser.add_argument( "--dataset-path", required=True, type=pathlib.Path, help="Path to dataset. " "For LibriSpeech, all of 'train-clean-360', 'train-clean-100', and 'train-other-500' must exist.", ) parser.add_argument( "--output-path", default=pathlib.Path("global_stats.json"), type=pathlib.Path, help="File to save feature statistics to. (Default: './global_stats.json')", ) return parser.parse_args() def generate_statistics(samples): E_x = 0 E_x_2 = 0 N = 0 for idx, sample in enumerate(samples): mel_spec = spectrogram_transform(sample[0].squeeze()).transpose(1, 0) scaled_mel_spec = piecewise_linear_log(mel_spec) sum = scaled_mel_spec.sum(0) sq_sum = scaled_mel_spec.pow(2).sum(0) M = scaled_mel_spec.size(0) E_x = E_x * (N / (N + M)) + sum / (N + M) E_x_2 = E_x_2 * (N / (N + M)) + sq_sum / (N + M) N += M if idx % 100 == 0: logger.info(f"Processed {idx}") return E_x, (E_x_2 - E_x**2) ** 0.5 def get_dataset(args): if args.model_type == MODEL_TYPE_LIBRISPEECH: return torch.utils.data.ConcatDataset( [ torchaudio.datasets.LIBRISPEECH(args.dataset_path, url="train-clean-360"), torchaudio.datasets.LIBRISPEECH(args.dataset_path, url="train-clean-100"), torchaudio.datasets.LIBRISPEECH(args.dataset_path, url="train-other-500"), ] ) elif args.model_type == MODEL_TYPE_TEDLIUM3: return torchaudio.datasets.TEDLIUM(args.dataset_path, release="release3", subset="train") elif args.model_type == MODEL_TYPE_MUSTC: return MUSTC(args.dataset_path, subset="train") else: raise ValueError(f"Encountered unsupported model type {args.model_type}.") def cli_main(): args = parse_args() dataset = get_dataset(args) dataloader = torch.utils.data.DataLoader(dataset, num_workers=4) mean, stddev = generate_statistics(iter(dataloader)) json_str = json.dumps({"mean": mean.tolist(), "invstddev": (1 / stddev).tolist()}, indent=2) with open(args.output_path, "w") as f: f.write(json_str) if __name__ == "__main__": cli_main()
# mypy: allow-untyped-defs r"""Autograd anomaly mode.""" import warnings import torch __all__ = ["detect_anomaly", "set_detect_anomaly"] class detect_anomaly: r"""Context-manager that enable anomaly detection for the autograd engine. This does two things: - Running the forward pass with detection enabled will allow the backward pass to print the traceback of the forward operation that created the failing backward function. - If ``check_nan`` is ``True``, any backward computation that generate "nan" value will raise an error. Default ``True``. .. warning:: This mode should be enabled only for debugging as the different tests will slow down your program execution. Example: >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ANOMALY) >>> import torch >>> from torch import autograd >>> class MyFunc(autograd.Function): ... @staticmethod ... def forward(ctx, inp): ... return inp.clone() ... ... @staticmethod ... def backward(ctx, gO): ... # Error during the backward pass ... raise RuntimeError("Some error in backward") ... return gO.clone() >>> def run_fn(a): ... out = MyFunc.apply(a) ... return out.sum() >>> inp = torch.rand(10, 10, requires_grad=True) >>> out = run_fn(inp) >>> out.backward() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/your/pytorch/install/torch/_tensor.py", line 93, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph) File "/your/pytorch/install/torch/autograd/__init__.py", line 90, in backward allow_unreachable=True) # allow_unreachable flag File "/your/pytorch/install/torch/autograd/function.py", line 76, in apply return self._forward_cls.backward(self, *args) File "<stdin>", line 8, in backward RuntimeError: Some error in backward >>> with autograd.detect_anomaly(): ... inp = torch.rand(10, 10, requires_grad=True) ... out = run_fn(inp) ... out.backward() Traceback of forward call that caused the error: File "tmp.py", line 53, in <module> out = run_fn(inp) File "tmp.py", line 44, in run_fn out = MyFunc.apply(a) Traceback (most recent call last): File "<stdin>", line 4, in <module> File "/your/pytorch/install/torch/_tensor.py", line 93, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph) File "/your/pytorch/install/torch/autograd/__init__.py", line 90, in backward allow_unreachable=True) # allow_unreachable flag File "/your/pytorch/install/torch/autograd/function.py", line 76, in apply return self._forward_cls.backward(self, *args) File "<stdin>", line 8, in backward RuntimeError: Some error in backward """ def __init__(self, check_nan=True) -> None: # noqa: D107 self.prev = torch.is_anomaly_enabled() self.check_nan = check_nan self.prev_check_nan = torch.is_anomaly_check_nan_enabled() warnings.warn( "Anomaly Detection has been enabled. " "This mode will increase the runtime " "and should only be enabled for debugging.", stacklevel=2, ) def __enter__(self) -> None: # noqa: D105 torch.set_anomaly_enabled(True, self.check_nan) def __exit__(self, *args: object) -> None: # noqa: D105 torch.set_anomaly_enabled(self.prev, self.prev_check_nan) class set_detect_anomaly: r"""Context-manager that sets the anomaly detection for the autograd engine on or off. ``set_detect_anomaly`` will enable or disable the autograd anomaly detection based on its argument :attr:`mode`. It can be used as a context-manager or as a function. See ``detect_anomaly`` above for details of the anomaly detection behaviour. Args: mode (bool): Flag whether to enable anomaly detection (``True``), or disable (``False``). check_nan (bool): Flag whether to raise an error when the backward generate "nan" """ def __init__(self, mode: bool, check_nan: bool = True) -> None: # noqa: D107 self.prev = torch.is_anomaly_enabled() self.prev_check_nan = torch.is_anomaly_check_nan_enabled() torch.set_anomaly_enabled(mode, check_nan) def __enter__(self) -> None: # noqa: D105 pass def __exit__(self, *args: object) -> None: # noqa: D105 torch.set_anomaly_enabled(self.prev, self.prev_check_nan)
# mypy: allow-untyped-defs r"""Autograd anomaly mode.""" import warnings import torch __all__ = ["detect_anomaly", "set_detect_anomaly"] class detect_anomaly: r"""Context-manager that enable anomaly detection for the autograd engine. This does two things: - Running the forward pass with detection enabled will allow the backward pass to print the traceback of the forward operation that created the failing backward function. - If ``check_nan`` is ``True``, any backward computation that generate "nan" value will raise an error. Default ``True``. .. warning:: This mode should be enabled only for debugging as the different tests will slow down your program execution. Example: >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ANOMALY) >>> import torch >>> from torch import autograd >>> class MyFunc(autograd.Function): ... @staticmethod ... def forward(ctx, inp): ... return inp.clone() ... @staticmethod ... def backward(ctx, gO): ... # Error during the backward pass ... raise RuntimeError("Some error in backward") ... return gO.clone() >>> def run_fn(a): ... out = MyFunc.apply(a) ... return out.sum() >>> inp = torch.rand(10, 10, requires_grad=True) >>> out = run_fn(inp) >>> out.backward() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/your/pytorch/install/torch/_tensor.py", line 93, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph) File "/your/pytorch/install/torch/autograd/__init__.py", line 90, in backward allow_unreachable=True) # allow_unreachable flag File "/your/pytorch/install/torch/autograd/function.py", line 76, in apply return self._forward_cls.backward(self, *args) File "<stdin>", line 8, in backward RuntimeError: Some error in backward >>> with autograd.detect_anomaly(): ... inp = torch.rand(10, 10, requires_grad=True) ... out = run_fn(inp) ... out.backward() Traceback of forward call that caused the error: File "tmp.py", line 53, in <module> out = run_fn(inp) File "tmp.py", line 44, in run_fn out = MyFunc.apply(a) Traceback (most recent call last): File "<stdin>", line 4, in <module> File "/your/pytorch/install/torch/_tensor.py", line 93, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph) File "/your/pytorch/install/torch/autograd/__init__.py", line 90, in backward allow_unreachable=True) # allow_unreachable flag File "/your/pytorch/install/torch/autograd/function.py", line 76, in apply return self._forward_cls.backward(self, *args) File "<stdin>", line 8, in backward RuntimeError: Some error in backward """ def __init__(self, check_nan=True) -> None: # noqa: D107 self.prev = torch.is_anomaly_enabled() self.check_nan = check_nan self.prev_check_nan = torch.is_anomaly_check_nan_enabled() warnings.warn( "Anomaly Detection has been enabled. " "This mode will increase the runtime " "and should only be enabled for debugging.", stacklevel=2, ) def __enter__(self) -> None: # noqa: D105 torch.set_anomaly_enabled(True, self.check_nan) def __exit__(self, *args: object) -> None: # noqa: D105 torch.set_anomaly_enabled(self.prev, self.prev_check_nan) class set_detect_anomaly: r"""Context-manager that sets the anomaly detection for the autograd engine on or off. ``set_detect_anomaly`` will enable or disable the autograd anomaly detection based on its argument :attr:`mode`. It can be used as a context-manager or as a function. See ``detect_anomaly`` above for details of the anomaly detection behaviour. Args: mode (bool): Flag whether to enable anomaly detection (``True``), or disable (``False``). check_nan (bool): Flag whether to raise an error when the backward generate "nan" """ def __init__(self, mode: bool, check_nan: bool = True) -> None: # noqa: D107 self.prev = torch.is_anomaly_enabled() self.prev_check_nan = torch.is_anomaly_check_nan_enabled() torch.set_anomaly_enabled(mode, check_nan) def __enter__(self) -> None: # noqa: D105 pass def __exit__(self, *args: object) -> None: # noqa: D105 torch.set_anomaly_enabled(self.prev, self.prev_check_nan)
import numpy as np import pytest from tensorflow import data as tf_data from keras.src import backend from keras.src import layers from keras.src import testing class RescalingTest(testing.TestCase): @pytest.mark.requires_trainable_backend def test_rescaling_basics(self): self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 1.0 / 255, "offset": 0.5}, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) @pytest.mark.requires_trainable_backend def test_rescaling_dtypes(self): # int scale self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 2, "offset": 0.5}, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) # int offset self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 1.0, "offset": 2}, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) # int inputs self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 1.0 / 255, "offset": 0.5}, input_shape=(2, 3), input_dtype="int16", expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) def test_rescaling_correctness(self): layer = layers.Rescaling(scale=1.0 / 255, offset=0.5) x = np.random.random((3, 10, 10, 3)) * 255 out = layer(x) self.assertAllClose(out, x / 255 + 0.5) def test_tf_data_compatibility(self): layer = layers.Rescaling(scale=1.0 / 255, offset=0.5) x = np.random.random((3, 10, 10, 3)) * 255 ds = tf_data.Dataset.from_tensor_slices(x).batch(3).map(layer) next(iter(ds)).numpy() def test_rescaling_with_channels_first_and_vector_scale(self): config = backend.image_data_format() backend.set_image_data_format("channels_first") layer = layers.Rescaling( scale=[1.0 / 255, 1.5 / 255, 2.0 / 255], offset=0.5 ) x = np.random.random((2, 3, 10, 10)) * 255 layer(x) backend.set_image_data_format(config) @pytest.mark.requires_trainable_backend def test_numpy_args(self): # https://github.com/keras-team/keras/issues/20072 self.run_layer_test( layers.Rescaling, init_kwargs={ "scale": np.array(1.0 / 255.0), "offset": np.array(0.5), }, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, )
import numpy as np import pytest from tensorflow import data as tf_data from keras.src import backend from keras.src import layers from keras.src import testing class RescalingTest(testing.TestCase): @pytest.mark.requires_trainable_backend def test_rescaling_basics(self): self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 1.0 / 255, "offset": 0.5}, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) @pytest.mark.requires_trainable_backend def test_rescaling_dtypes(self): # int scale self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 2, "offset": 0.5}, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) # int offset self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 1.0, "offset": 2}, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) # int inputs self.run_layer_test( layers.Rescaling, init_kwargs={"scale": 1.0 / 255, "offset": 0.5}, input_shape=(2, 3), input_dtype="int16", expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, ) def test_rescaling_correctness(self): layer = layers.Rescaling(scale=1.0 / 255, offset=0.5) x = np.random.random((3, 10, 10, 3)) * 255 out = layer(x) self.assertAllClose(out, x / 255 + 0.5) def test_tf_data_compatibility(self): layer = layers.Rescaling(scale=1.0 / 255, offset=0.5) x = np.random.random((3, 10, 10, 3)) * 255 ds = tf_data.Dataset.from_tensor_slices(x).batch(3).map(layer) for output in ds.take(1): output.numpy() def test_rescaling_with_channels_first_and_vector_scale(self): config = backend.image_data_format() backend.set_image_data_format("channels_first") layer = layers.Rescaling( scale=[1.0 / 255, 1.5 / 255, 2.0 / 255], offset=0.5 ) x = np.random.random((2, 3, 10, 10)) * 255 layer(x) backend.set_image_data_format(config) @pytest.mark.requires_trainable_backend def test_numpy_args(self): # https://github.com/keras-team/keras/issues/20072 self.run_layer_test( layers.Rescaling, init_kwargs={ "scale": np.array(1.0 / 255.0), "offset": np.array(0.5), }, input_shape=(2, 3), expected_output_shape=(2, 3), expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, supports_masking=True, )
# Copyright 2021 AlQuraishi Laboratory # Copyright 2021 DeepMind Technologies Limited # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Optional import torch def _calculate_bin_centers(boundaries: torch.Tensor) -> torch.Tensor: step = boundaries[1] - boundaries[0] bin_centers = boundaries + step / 2 bin_centers = torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1)], dim=0) return bin_centers def _calculate_expected_aligned_error( alignment_confidence_breaks: torch.Tensor, aligned_distance_error_probs: torch.Tensor, ) -> tuple[torch.Tensor, torch.Tensor]: bin_centers = _calculate_bin_centers(alignment_confidence_breaks) return ( torch.sum(aligned_distance_error_probs * bin_centers, dim=-1), bin_centers[-1], ) def compute_predicted_aligned_error( logits: torch.Tensor, max_bin: int = 31, no_bins: int = 64, **kwargs, ) -> dict[str, torch.Tensor]: """Computes aligned confidence metrics from logits. Args: logits: [*, num_res, num_res, num_bins] the logits output from PredictedAlignedErrorHead. max_bin: Maximum bin value no_bins: Number of bins Returns: aligned_confidence_probs: [*, num_res, num_res, num_bins] the predicted aligned error probabilities over bins for each residue pair. predicted_aligned_error: [*, num_res, num_res] the expected aligned distance error for each pair of residues. max_predicted_aligned_error: [*] the maximum predicted error possible. """ boundaries = torch.linspace(0, max_bin, steps=(no_bins - 1), device=logits.device) aligned_confidence_probs = torch.nn.functional.softmax(logits, dim=-1) predicted_aligned_error, max_predicted_aligned_error = _calculate_expected_aligned_error( alignment_confidence_breaks=boundaries, aligned_distance_error_probs=aligned_confidence_probs, ) return { "aligned_confidence_probs": aligned_confidence_probs, "predicted_aligned_error": predicted_aligned_error, "max_predicted_aligned_error": max_predicted_aligned_error, } def compute_tm( logits: torch.Tensor, residue_weights: Optional[torch.Tensor] = None, max_bin: int = 31, no_bins: int = 64, eps: float = 1e-8, **kwargs, ) -> torch.Tensor: if residue_weights is None: residue_weights = logits.new_ones(logits.shape[-2]) boundaries = torch.linspace(0, max_bin, steps=(no_bins - 1), device=logits.device) bin_centers = _calculate_bin_centers(boundaries) torch.sum(residue_weights) n = logits.shape[-2] clipped_n = max(n, 19) d0 = 1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8 probs = torch.nn.functional.softmax(logits, dim=-1) tm_per_bin = 1.0 / (1 + (bin_centers**2) / (d0**2)) predicted_tm_term = torch.sum(probs * tm_per_bin, dim=-1) normed_residue_mask = residue_weights / (eps + residue_weights.sum()) per_alignment = torch.sum(predicted_tm_term * normed_residue_mask, dim=-1) weighted = per_alignment * residue_weights argmax = (weighted == torch.max(weighted)).nonzero()[0] return per_alignment[tuple(argmax)]
# Copyright 2021 AlQuraishi Laboratory # Copyright 2021 DeepMind Technologies Limited # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from typing import Dict, Optional, Tuple import torch def _calculate_bin_centers(boundaries: torch.Tensor) -> torch.Tensor: step = boundaries[1] - boundaries[0] bin_centers = boundaries + step / 2 bin_centers = torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1)], dim=0) return bin_centers def _calculate_expected_aligned_error( alignment_confidence_breaks: torch.Tensor, aligned_distance_error_probs: torch.Tensor, ) -> Tuple[torch.Tensor, torch.Tensor]: bin_centers = _calculate_bin_centers(alignment_confidence_breaks) return ( torch.sum(aligned_distance_error_probs * bin_centers, dim=-1), bin_centers[-1], ) def compute_predicted_aligned_error( logits: torch.Tensor, max_bin: int = 31, no_bins: int = 64, **kwargs, ) -> Dict[str, torch.Tensor]: """Computes aligned confidence metrics from logits. Args: logits: [*, num_res, num_res, num_bins] the logits output from PredictedAlignedErrorHead. max_bin: Maximum bin value no_bins: Number of bins Returns: aligned_confidence_probs: [*, num_res, num_res, num_bins] the predicted aligned error probabilities over bins for each residue pair. predicted_aligned_error: [*, num_res, num_res] the expected aligned distance error for each pair of residues. max_predicted_aligned_error: [*] the maximum predicted error possible. """ boundaries = torch.linspace(0, max_bin, steps=(no_bins - 1), device=logits.device) aligned_confidence_probs = torch.nn.functional.softmax(logits, dim=-1) predicted_aligned_error, max_predicted_aligned_error = _calculate_expected_aligned_error( alignment_confidence_breaks=boundaries, aligned_distance_error_probs=aligned_confidence_probs, ) return { "aligned_confidence_probs": aligned_confidence_probs, "predicted_aligned_error": predicted_aligned_error, "max_predicted_aligned_error": max_predicted_aligned_error, } def compute_tm( logits: torch.Tensor, residue_weights: Optional[torch.Tensor] = None, max_bin: int = 31, no_bins: int = 64, eps: float = 1e-8, **kwargs, ) -> torch.Tensor: if residue_weights is None: residue_weights = logits.new_ones(logits.shape[-2]) boundaries = torch.linspace(0, max_bin, steps=(no_bins - 1), device=logits.device) bin_centers = _calculate_bin_centers(boundaries) torch.sum(residue_weights) n = logits.shape[-2] clipped_n = max(n, 19) d0 = 1.24 * (clipped_n - 15) ** (1.0 / 3) - 1.8 probs = torch.nn.functional.softmax(logits, dim=-1) tm_per_bin = 1.0 / (1 + (bin_centers**2) / (d0**2)) predicted_tm_term = torch.sum(probs * tm_per_bin, dim=-1) normed_residue_mask = residue_weights / (eps + residue_weights.sum()) per_alignment = torch.sum(predicted_tm_term * normed_residue_mask, dim=-1) weighted = per_alignment * residue_weights argmax = (weighted == torch.max(weighted)).nonzero()[0] return per_alignment[tuple(argmax)]
from __future__ import annotations import json from json import JSONDecodeError from typing import Annotated, Any, Optional, TypeVar, Union import jsonpatch # type: ignore[import] import pydantic from pydantic import SkipValidation from langchain_core.exceptions import OutputParserException from langchain_core.output_parsers.format_instructions import JSON_FORMAT_INSTRUCTIONS from langchain_core.output_parsers.transform import BaseCumulativeTransformOutputParser from langchain_core.outputs import Generation from langchain_core.utils.json import ( parse_and_check_json_markdown, parse_json_markdown, parse_partial_json, ) from langchain_core.utils.pydantic import PYDANTIC_MAJOR_VERSION if PYDANTIC_MAJOR_VERSION < 2: PydanticBaseModel = pydantic.BaseModel else: from pydantic.v1 import BaseModel # Union type needs to be last assignment to PydanticBaseModel to make mypy happy. PydanticBaseModel = Union[BaseModel, pydantic.BaseModel] # type: ignore TBaseModel = TypeVar("TBaseModel", bound=PydanticBaseModel) class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]): """Parse the output of an LLM call to a JSON object. When used in streaming mode, it will yield partial JSON objects containing all the keys that have been returned so far. In streaming, if `diff` is set to `True`, yields JSONPatch operations describing the difference between the previous and the current object. """ pydantic_object: Annotated[Optional[type[TBaseModel]], SkipValidation()] = None # type: ignore """The Pydantic object to use for validation. If None, no validation is performed.""" def _diff(self, prev: Optional[Any], next: Any) -> Any: return jsonpatch.make_patch(prev, next).patch def _get_schema(self, pydantic_object: type[TBaseModel]) -> dict[str, Any]: if issubclass(pydantic_object, pydantic.BaseModel): return pydantic_object.model_json_schema() elif issubclass(pydantic_object, pydantic.v1.BaseModel): return pydantic_object.schema() def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any: """Parse the result of an LLM call to a JSON object. Args: result: The result of the LLM call. partial: Whether to parse partial JSON objects. If True, the output will be a JSON object containing all the keys that have been returned so far. If False, the output will be the full JSON object. Default is False. Returns: The parsed JSON object. Raises: OutputParserException: If the output is not valid JSON. """ text = result[0].text text = text.strip() if partial: try: return parse_json_markdown(text) except JSONDecodeError: return None else: try: return parse_json_markdown(text) except JSONDecodeError as e: msg = f"Invalid json output: {text}" raise OutputParserException(msg, llm_output=text) from e def parse(self, text: str) -> Any: """Parse the output of an LLM call to a JSON object. Args: text: The output of the LLM call. Returns: The parsed JSON object. """ return self.parse_result([Generation(text=text)]) def get_format_instructions(self) -> str: """Return the format instructions for the JSON output. Returns: The format instructions for the JSON output. """ if self.pydantic_object is None: return "Return a JSON object." else: # Copy schema to avoid altering original Pydantic schema. schema = dict(self._get_schema(self.pydantic_object).items()) # Remove extraneous fields. reduced_schema = schema if "title" in reduced_schema: del reduced_schema["title"] if "type" in reduced_schema: del reduced_schema["type"] # Ensure json in context is well-formed with double quotes. schema_str = json.dumps(reduced_schema, ensure_ascii=False) return JSON_FORMAT_INSTRUCTIONS.format(schema=schema_str) @property def _type(self) -> str: return "simple_json_output_parser" # For backwards compatibility SimpleJsonOutputParser = JsonOutputParser __all__ = [ "JsonOutputParser", "SimpleJsonOutputParser", # For backwards compatibility "parse_partial_json", # For backwards compatibility "parse_and_check_json_markdown", # For backwards compatibility ]
from __future__ import annotations import json from json import JSONDecodeError from typing import Annotated, Any, Optional, TypeVar, Union import jsonpatch # type: ignore[import] import pydantic from pydantic import SkipValidation from langchain_core.exceptions import OutputParserException from langchain_core.output_parsers.format_instructions import JSON_FORMAT_INSTRUCTIONS from langchain_core.output_parsers.transform import BaseCumulativeTransformOutputParser from langchain_core.outputs import Generation from langchain_core.utils.json import ( parse_and_check_json_markdown, parse_json_markdown, parse_partial_json, ) from langchain_core.utils.pydantic import PYDANTIC_MAJOR_VERSION if PYDANTIC_MAJOR_VERSION < 2: PydanticBaseModel = pydantic.BaseModel else: from pydantic.v1 import BaseModel # Union type needs to be last assignment to PydanticBaseModel to make mypy happy. PydanticBaseModel = Union[BaseModel, pydantic.BaseModel] # type: ignore TBaseModel = TypeVar("TBaseModel", bound=PydanticBaseModel) class JsonOutputParser(BaseCumulativeTransformOutputParser[Any]): """Parse the output of an LLM call to a JSON object. When used in streaming mode, it will yield partial JSON objects containing all the keys that have been returned so far. In streaming, if `diff` is set to `True`, yields JSONPatch operations describing the difference between the previous and the current object. """ pydantic_object: Annotated[Optional[type[TBaseModel]], SkipValidation()] = None # type: ignore """The Pydantic object to use for validation. If None, no validation is performed.""" def _diff(self, prev: Optional[Any], next: Any) -> Any: return jsonpatch.make_patch(prev, next).patch def _get_schema(self, pydantic_object: type[TBaseModel]) -> dict[str, Any]: if issubclass(pydantic_object, pydantic.BaseModel): return pydantic_object.model_json_schema() elif issubclass(pydantic_object, pydantic.v1.BaseModel): return pydantic_object.schema() def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any: """Parse the result of an LLM call to a JSON object. Args: result: The result of the LLM call. partial: Whether to parse partial JSON objects. If True, the output will be a JSON object containing all the keys that have been returned so far. If False, the output will be the full JSON object. Default is False. Returns: The parsed JSON object. Raises: OutputParserException: If the output is not valid JSON. """ text = result[0].text text = text.strip() if partial: try: return parse_json_markdown(text) except JSONDecodeError: return None else: try: return parse_json_markdown(text) except JSONDecodeError as e: msg = f"Invalid json output: {text}" raise OutputParserException(msg, llm_output=text) from e def parse(self, text: str) -> Any: """Parse the output of an LLM call to a JSON object. Args: text: The output of the LLM call. Returns: The parsed JSON object. """ return self.parse_result([Generation(text=text)]) def get_format_instructions(self) -> str: """Return the format instructions for the JSON output. Returns: The format instructions for the JSON output. """ if self.pydantic_object is None: return "Return a JSON object." else: # Copy schema to avoid altering original Pydantic schema. schema = dict(self._get_schema(self.pydantic_object).items()) # Remove extraneous fields. reduced_schema = schema if "title" in reduced_schema: del reduced_schema["title"] if "type" in reduced_schema: del reduced_schema["type"] # Ensure json in context is well-formed with double quotes. schema_str = json.dumps(reduced_schema, ensure_ascii=False) return JSON_FORMAT_INSTRUCTIONS.format(schema=schema_str) @property def _type(self) -> str: return "simple_json_output_parser" # For backwards compatibility SimpleJsonOutputParser = JsonOutputParser parse_partial_json = parse_partial_json parse_and_check_json_markdown = parse_and_check_json_markdown
from jina import Client from docarray import DocList from docarray.documents import TextDoc if __name__ == '__main__': c = Client(host='grpc://0.0.0.0:54321') da = c.post( '/', DocList[TextDoc]([TextDoc(), TextDoc()]), return_type=DocList[TextDoc] ) print(da.text)
from jina import Client from docarray import DocList from docarray.documents import TextDoc if __name__ == '__main__': c = Client(host='grpc://0.0.0.0:54321') da = c.post('/', DocList[TextDoc]([TextDoc(), TextDoc()]), return_type=DocList[TextDoc]) print(da.text)
import json import logging from enum import Enum from typing import Any from requests.exceptions import HTTPError, RequestException from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField from backend.util.request import requests logger = logging.getLogger(name=__name__) class HttpMethod(Enum): GET = "GET" POST = "POST" PUT = "PUT" DELETE = "DELETE" PATCH = "PATCH" OPTIONS = "OPTIONS" HEAD = "HEAD" class SendWebRequestBlock(Block): class Input(BlockSchema): url: str = SchemaField( description="The URL to send the request to", placeholder="https://api.example.com", ) method: HttpMethod = SchemaField( description="The HTTP method to use for the request", default=HttpMethod.POST, ) headers: dict[str, str] = SchemaField( description="The headers to include in the request", default_factory=dict, ) json_format: bool = SchemaField( title="JSON format", description="Whether to send and receive body as JSON", default=True, ) body: Any = SchemaField( description="The body of the request", default=None, ) class Output(BlockSchema): response: object = SchemaField(description="The response from the server") client_error: object = SchemaField(description="Errors on 4xx status codes") server_error: object = SchemaField(description="Errors on 5xx status codes") error: str = SchemaField(description="Errors for all other exceptions") def __init__(self): super().__init__( id="6595ae1f-b924-42cb-9a41-551a0611c4b4", description="This block makes an HTTP request to the given URL.", categories={BlockCategory.OUTPUT}, input_schema=SendWebRequestBlock.Input, output_schema=SendWebRequestBlock.Output, ) def run(self, input_data: Input, **kwargs) -> BlockOutput: body = input_data.body if input_data.json_format: if isinstance(body, str): try: # Try to parse as JSON first body = json.loads(body) except json.JSONDecodeError: # If it's not valid JSON and just plain text, # we should send it as plain text instead input_data.json_format = False try: response = requests.request( input_data.method.value, input_data.url, headers=input_data.headers, json=body if input_data.json_format else None, data=body if not input_data.json_format else None, ) if input_data.json_format: if response.status_code == 204 or not response.content.strip(): result = None else: result = response.json() else: result = response.text yield "response", result except HTTPError as e: # Handle error responses try: result = e.response.json() if input_data.json_format else str(e) except json.JSONDecodeError: result = str(e) if 400 <= e.response.status_code < 500: yield "client_error", result elif 500 <= e.response.status_code < 600: yield "server_error", result else: error_msg = ( "Unexpected status code " f"{e.response.status_code} '{e.response.reason}'" ) logger.warning(error_msg) yield "error", error_msg except RequestException as e: # Handle other request-related exceptions yield "error", str(e) except Exception as e: # Catch any other unexpected exceptions yield "error", str(e)
import json import logging from enum import Enum from typing import Any from requests.exceptions import HTTPError, RequestException from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField from backend.util.request import requests logger = logging.getLogger(name=__name__) class HttpMethod(Enum): GET = "GET" POST = "POST" PUT = "PUT" DELETE = "DELETE" PATCH = "PATCH" OPTIONS = "OPTIONS" HEAD = "HEAD" class SendWebRequestBlock(Block): class Input(BlockSchema): url: str = SchemaField( description="The URL to send the request to", placeholder="https://api.example.com", ) method: HttpMethod = SchemaField( description="The HTTP method to use for the request", default=HttpMethod.POST, ) headers: dict[str, str] = SchemaField( description="The headers to include in the request", default_factory=dict, ) json_format: bool = SchemaField( title="JSON format", description="Whether to send and receive body as JSON", default=True, ) body: Any = SchemaField( description="The body of the request", default=None, ) class Output(BlockSchema): response: object = SchemaField(description="The response from the server") client_error: object = SchemaField(description="Errors on 4xx status codes") server_error: object = SchemaField(description="Errors on 5xx status codes") error: str = SchemaField(description="Errors for all other exceptions") def __init__(self): super().__init__( id="6595ae1f-b924-42cb-9a41-551a0611c4b4", description="This block makes an HTTP request to the given URL.", categories={BlockCategory.OUTPUT}, input_schema=SendWebRequestBlock.Input, output_schema=SendWebRequestBlock.Output, ) def run(self, input_data: Input, **kwargs) -> BlockOutput: body = input_data.body if input_data.json_format: if isinstance(body, str): try: # Try to parse as JSON first body = json.loads(body) except json.JSONDecodeError: # If it's not valid JSON and just plain text, # we should send it as plain text instead input_data.json_format = False try: response = requests.request( input_data.method.value, input_data.url, headers=input_data.headers, json=body if input_data.json_format else None, data=body if not input_data.json_format else None, ) result = response.json() if input_data.json_format else response.text yield "response", result except HTTPError as e: # Handle error responses try: result = e.response.json() if input_data.json_format else str(e) except json.JSONDecodeError: result = str(e) if 400 <= e.response.status_code < 500: yield "client_error", result elif 500 <= e.response.status_code < 600: yield "server_error", result else: error_msg = ( "Unexpected status code " f"{e.response.status_code} '{e.response.reason}'" ) logger.warning(error_msg) yield "error", error_msg except RequestException as e: # Handle other request-related exceptions yield "error", str(e) except Exception as e: # Catch any other unexpected exceptions yield "error", str(e)
# Copyright (c) OpenMMLab. All rights reserved. import math import torch.nn as nn from mmcv.cnn import ConvModule from mmcv.runner import BaseModule, auto_fp16 from mmdet.models.builder import NECKS @NECKS.register_module() class CTResNetNeck(BaseModule): """The neck used in `CenterNet <https://arxiv.org/abs/1904.07850>`_ for object classification and box regression. Args: in_channel (int): Number of input channels. num_deconv_filters (tuple[int]): Number of filters per stage. num_deconv_kernels (tuple[int]): Number of kernels per stage. use_dcn (bool): If True, use DCNv2. Default: True. init_cfg (dict or list[dict], optional): Initialization config dict. """ def __init__(self, in_channel, num_deconv_filters, num_deconv_kernels, use_dcn=True, init_cfg=None): super(CTResNetNeck, self).__init__(init_cfg) assert len(num_deconv_filters) == len(num_deconv_kernels) self.fp16_enabled = False self.use_dcn = use_dcn self.in_channel = in_channel self.deconv_layers = self._make_deconv_layer(num_deconv_filters, num_deconv_kernels) def _make_deconv_layer(self, num_deconv_filters, num_deconv_kernels): """use deconv layers to upsample backbone's output.""" layers = [] for i in range(len(num_deconv_filters)): feat_channel = num_deconv_filters[i] conv_module = ConvModule( self.in_channel, feat_channel, 3, padding=1, conv_cfg=dict(type='DCNv2') if self.use_dcn else None, norm_cfg=dict(type='BN')) layers.append(conv_module) upsample_module = ConvModule( feat_channel, feat_channel, num_deconv_kernels[i], stride=2, padding=1, conv_cfg=dict(type='deconv'), norm_cfg=dict(type='BN')) layers.append(upsample_module) self.in_channel = feat_channel return nn.Sequential(*layers) def init_weights(self): for m in self.modules(): if isinstance(m, nn.ConvTranspose2d): # In order to be consistent with the source code, # reset the ConvTranspose2d initialization parameters m.reset_parameters() # Simulated bilinear upsampling kernel w = m.weight.data f = math.ceil(w.size(2) / 2) c = (2 * f - 1 - f % 2) / (2. * f) for i in range(w.size(2)): for j in range(w.size(3)): w[0, 0, i, j] = \ (1 - math.fabs(i / f - c)) * ( 1 - math.fabs(j / f - c)) for c in range(1, w.size(0)): w[c, 0, :, :] = w[0, 0, :, :] elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) # self.use_dcn is False elif not self.use_dcn and isinstance(m, nn.Conv2d): # In order to be consistent with the source code, # reset the Conv2d initialization parameters m.reset_parameters() @auto_fp16() def forward(self, inputs): assert isinstance(inputs, (list, tuple)) outs = self.deconv_layers(inputs[-1]) return outs,
import math import torch.nn as nn from mmcv.cnn import ConvModule from mmcv.runner import BaseModule, auto_fp16 from mmdet.models.builder import NECKS @NECKS.register_module() class CTResNetNeck(BaseModule): """The neck used in `CenterNet <https://arxiv.org/abs/1904.07850>`_ for object classification and box regression. Args: in_channel (int): Number of input channels. num_deconv_filters (tuple[int]): Number of filters per stage. num_deconv_kernels (tuple[int]): Number of kernels per stage. use_dcn (bool): If True, use DCNv2. Default: True. init_cfg (dict or list[dict], optional): Initialization config dict. """ def __init__(self, in_channel, num_deconv_filters, num_deconv_kernels, use_dcn=True, init_cfg=None): super(CTResNetNeck, self).__init__(init_cfg) assert len(num_deconv_filters) == len(num_deconv_kernels) self.fp16_enabled = False self.use_dcn = use_dcn self.in_channel = in_channel self.deconv_layers = self._make_deconv_layer(num_deconv_filters, num_deconv_kernels) def _make_deconv_layer(self, num_deconv_filters, num_deconv_kernels): """use deconv layers to upsample backbone's output.""" layers = [] for i in range(len(num_deconv_filters)): feat_channel = num_deconv_filters[i] conv_module = ConvModule( self.in_channel, feat_channel, 3, padding=1, conv_cfg=dict(type='DCNv2') if self.use_dcn else None, norm_cfg=dict(type='BN')) layers.append(conv_module) upsample_module = ConvModule( feat_channel, feat_channel, num_deconv_kernels[i], stride=2, padding=1, conv_cfg=dict(type='deconv'), norm_cfg=dict(type='BN')) layers.append(upsample_module) self.in_channel = feat_channel return nn.Sequential(*layers) def init_weights(self): for m in self.modules(): if isinstance(m, nn.ConvTranspose2d): # In order to be consistent with the source code, # reset the ConvTranspose2d initialization parameters m.reset_parameters() # Simulated bilinear upsampling kernel w = m.weight.data f = math.ceil(w.size(2) / 2) c = (2 * f - 1 - f % 2) / (2. * f) for i in range(w.size(2)): for j in range(w.size(3)): w[0, 0, i, j] = \ (1 - math.fabs(i / f - c)) * ( 1 - math.fabs(j / f - c)) for c in range(1, w.size(0)): w[c, 0, :, :] = w[0, 0, :, :] elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) # self.use_dcn is False elif not self.use_dcn and isinstance(m, nn.Conv2d): # In order to be consistent with the source code, # reset the Conv2d initialization parameters m.reset_parameters() @auto_fp16() def forward(self, inputs): assert isinstance(inputs, (list, tuple)) outs = self.deconv_layers(inputs[-1]) return outs,
"""This module contains the core type definitions and protocols used throughout Dynamo. The types defined here fall into several categories: - Guard related types (GuardFn, GuardFail, GuardedCode): Used for tracking and managing guards that protect compiled code - Frame and cache types (FrameState, CacheEntry): Used for managing interpreter frame state and caching - Callback protocols (DynamoCallbackFn): Define the interface for frame evaluation callbacks - Hook protocols (DynamoGuardHook, ProfilerStartHook, ProfilerEndHook, BytecodeHook): Define various hook points for instrumentation and customization These types provide the foundational interfaces that enable Dynamo's dynamic compilation and optimization system, ensuring type safety and clear contracts between different components of the system. """ import dataclasses import types from typing import Any, Callable, NamedTuple, Optional, Protocol, Union # CacheEntry has a `guard_manager` field for the guard, and a `code` field for the code object. from torch._C._dynamo.eval_frame import ( _CacheEntry as CacheEntry, _ExtraState as ExtraState, _FrameAction as FrameAction, _FrameExecStrategy as FrameExecStrategy, _PyInterpreterFrame as DynamoFrameType, ) from torch._guards import CompileId, Guard # We use a dict to store additional data per frame. FrameState = dict[Any, Any] class GuardFail(NamedTuple): # A string repr of the piece of failed guard code we eval-ed reason: str # A code object where we failed a guard orig_code: types.CodeType @dataclasses.dataclass(frozen=True) class GuardFilterEntry: name: str has_value: bool value: object guard_type: str derived_guard_types: tuple[str, ...] is_global: bool orig_guard: Guard class GuardFn(Protocol): closure_vars: dict[str, object] args: list[str] code_parts: list[str] verbose_code_parts: list[str] global_scope: dict[str, object] guard_fail_fn: Optional[Callable[[GuardFail], None]] cache_entry: Optional[CacheEntry] extra_state: Optional[ExtraState] # maps locals of user function to bool def __call__(self, f_locals: dict[str, object]) -> bool: ... @dataclasses.dataclass class GuardedCode: code: types.CodeType guard_manager: GuardFn compile_id: CompileId trace_annotation: str = "Unknown" @dataclasses.dataclass class ConvertFrameReturn: # default return is no compiled code (i.e. `return None`): # strategy is to skip non-recursively, for all future intercepted frames too # eval frame execution strategy for this frame frame_exec_strategy: FrameExecStrategy = dataclasses.field( default_factory=lambda: FrameExecStrategy(FrameAction.SKIP, FrameAction.DEFAULT) ) # also apply frame_exec strategy to future frames with same code apply_to_code: bool = True guarded_code: Optional[GuardedCode] = None def wrap_guarded_code(guarded_code: GuardedCode) -> ConvertFrameReturn: return ConvertFrameReturn( frame_exec_strategy=FrameExecStrategy(FrameAction.DEFAULT, FrameAction.DEFAULT), guarded_code=guarded_code, ) class DynamoCallbackFn(Protocol): def __call__( self, frame: DynamoFrameType, cache_entry: Optional[CacheEntry], frame_state: FrameState, ) -> ConvertFrameReturn: ... DynamoCallback = Union[DynamoCallbackFn, None, bool] class DynamoGuardHook(Protocol): def __call__( self, guard_manager: GuardFn, code: types.CodeType, f_locals: dict[str, object], index: int, last: bool, ) -> None: ... class ProfilerStartHook(Protocol): def __call__( self, name: str, # TODO(whc) how do I annotate a _RecordFunction here? ) -> Any: ... class ProfilerEndHook(Protocol): def __call__(self, record: Any) -> None: ... class BytecodeHook(Protocol): def __call__( self, code: types.CodeType, new_code: types.CodeType ) -> Optional[types.CodeType]: ...
"""This module contains the core type definitions and protocols used throughout Dynamo. The types defined here fall into several categories: - Guard related types (GuardFn, GuardFail, GuardedCode): Used for tracking and managing guards that protect compiled code - Frame and cache types (FrameState, CacheEntry): Used for managing interpreter frame state and caching - Callback protocols (DynamoCallbackFn): Define the interface for frame evaluation callbacks - Hook protocols (DynamoGuardHook, ProfilerStartHook, ProfilerEndHook, BytecodeHook): Define various hook points for instrumentation and customization These types provide the foundational interfaces that enable Dynamo's dynamic compilation and optimization system, ensuring type safety and clear contracts between different components of the system. """ import dataclasses import types from typing import Any, Callable, NamedTuple, Optional, Protocol, Union # CacheEntry has a `guard_manager` field for the guard, and a `code` field for the code object. from torch._C._dynamo.eval_frame import ( _CacheEntry as CacheEntry, _ExtraState as ExtraState, _FrameAction as FrameAction, _FrameExecStrategy as FrameExecStrategy, _PyInterpreterFrame as DynamoFrameType, ) from torch._guards import CompileId, Guard # We use a dict to store additional data per frame. FrameState = dict[Any, Any] class GuardFail(NamedTuple): # A string repr of the piece of failed guard code we eval-ed reason: str # A code object where we failed a guard orig_code: types.CodeType @dataclasses.dataclass(frozen=True) class GuardFilterEntry: name: str has_value: bool value: object guard_type: str derived_guard_types: tuple[str, ...] is_global: bool orig_guard: Guard class GuardFn(Protocol): closure_vars: dict[str, object] args: list[str] code_parts: list[str] verbose_code_parts: list[str] global_scope: dict[str, object] guard_fail_fn: Optional[Callable[[GuardFail], None]] cache_entry: Optional[CacheEntry] extra_state: Optional[ExtraState] # maps locals of user function to bool def __call__(self, f_locals: dict[str, object]) -> bool: ... @dataclasses.dataclass class GuardedCode: code: types.CodeType guard_manager: GuardFn compile_id: CompileId trace_annotation: str = "Unknown" @dataclasses.dataclass class ConvertFrameReturn: # default return is no compiled code (i.e. `return None`): # strategy is to skip non-recursively, for all future intercepted frames too # eval fram execution strategy for this frame frame_exec_strategy: FrameExecStrategy = dataclasses.field( default_factory=lambda: FrameExecStrategy(FrameAction.SKIP, FrameAction.DEFAULT) ) # also apply frame_exec strategy to future frames with same code apply_to_code: bool = True guarded_code: Optional[GuardedCode] = None def wrap_guarded_code(guarded_code: GuardedCode) -> ConvertFrameReturn: return ConvertFrameReturn( frame_exec_strategy=FrameExecStrategy(FrameAction.DEFAULT, FrameAction.DEFAULT), guarded_code=guarded_code, ) class DynamoCallbackFn(Protocol): def __call__( self, frame: DynamoFrameType, cache_entry: Optional[CacheEntry], frame_state: FrameState, ) -> ConvertFrameReturn: ... DynamoCallback = Union[DynamoCallbackFn, None, bool] class DynamoGuardHook(Protocol): def __call__( self, guard_manager: GuardFn, code: types.CodeType, f_locals: dict[str, object], index: int, last: bool, ) -> None: ... class ProfilerStartHook(Protocol): def __call__( self, name: str, # TODO(whc) how do I annotate a _RecordFunction here? ) -> Any: ... class ProfilerEndHook(Protocol): def __call__(self, record: Any) -> None: ... class BytecodeHook(Protocol): def __call__( self, code: types.CodeType, new_code: types.CodeType ) -> Optional[types.CodeType]: ...
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import subprocess import numpy as np import pytest from jina import Document, DocumentArray, Flow cur_dir = os.path.dirname(os.path.abspath(__file__)) def test_video_torch_encoder(): model_state_dict_path = os.path.join(cur_dir, '../model/model_state_dict.pth') input_dim = 224 test_img = np.random.rand(3, input_dim, input_dim) docs = DocumentArray([Document(blob=test_img), Document(blob=test_img)]) f = Flow().add( uses={ 'jtype': 'CustomImageTorchEncoder', 'with': { 'model_state_dict_path': model_state_dict_path, 'layer_name': 'conv1', 'model_definition_file': os.path.join( cur_dir, '../model/external_model.py' ), 'model_class_name': 'ExternalModel', }, } ) with f: resp = f.post(on='/test', inputs=docs, return_results=True) assert resp[0].docs[0].embedding.shape == (10,) assert resp[0].docs[1].embedding.shape == (10,) @pytest.mark.docker def test_docker_runtime(build_docker_image: str): with pytest.raises(subprocess.TimeoutExpired): subprocess.run( ['jina', 'executor', f'--uses=docker://{build_docker_image}'], timeout=30, check=True, ) @pytest.mark.gpu @pytest.mark.docker def test_docker_runtime_gpu(build_docker_image_gpu: str): with pytest.raises(subprocess.TimeoutExpired): subprocess.run( [ 'jina', 'pea', f'--uses=docker://{build_docker_image_gpu}', '--gpus', 'all', '--uses-with', 'device:cuda', ], timeout=30, check=True, )
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import numpy as np from jina import Document, Flow, DocumentArray from ...custom_image_torch_encoder import CustomImageTorchEncoder cur_dir = os.path.dirname(os.path.abspath(__file__)) def test_video_torch_encoder(): model_state_dict_path = os.path.join(cur_dir, '../model/model_state_dict.pth') input_dim = 224 test_img = np.random.rand(3, input_dim, input_dim) docs = DocumentArray([Document(blob=test_img), Document(blob=test_img)]) f = Flow().add(uses={'jtype': 'CustomImageTorchEncoder', 'with': {'model_state_dict_path': model_state_dict_path, 'layer_name': 'conv1', 'model_definition_file': os.path.join(cur_dir, '../model/external_model.py'), 'model_class_name': 'ExternalModel'}}) with f: resp = f.post(on='/test', inputs=docs, return_results=True) assert resp[0].docs[0].embedding.shape == (10,) assert resp[0].docs[1].embedding.shape == (10,)
import logging from datasets import load_dataset from sentence_transformers import SparseEncoder from sentence_transformers.sparse_encoder.evaluation import SparseTranslationEvaluator logging.basicConfig(format="%(message)s", level=logging.INFO) # Load a model, not mutilingual but hope to see some on the hub soon model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Load a parallel sentences dataset dataset = load_dataset("sentence-transformers/parallel-sentences-news-commentary", "en-nl", split="train[:1000]") # Initialize the TranslationEvaluator using the same texts from two languages translation_evaluator = SparseTranslationEvaluator( source_sentences=dataset["english"], target_sentences=dataset["non_english"], name="news-commentary-en-nl", ) results = translation_evaluator(model) """ Evaluating translation matching Accuracy of the model on the news-commentary-en-nl dataset: Accuracy src2trg: 41.40 Accuracy trg2src: 47.70 Model Sparsity Stats: Row Non-Zero Mean: 113.6150016784668, Row Sparsity Mean: 0.9962776005268097 """ # Print the results print(f"Primary metric: {translation_evaluator.primary_metric}") # => Primary metric: news-commentary-en-nl_mean_accuracy print(f"Primary metric value: {results[translation_evaluator.primary_metric]:.4f}") # => Primary metric value: 0.4455
import logging from datasets import load_dataset from sentence_transformers.sparse_encoder import ( MLMTransformer, SparseEncoder, SparseTranslationEvaluator, SpladePooling, ) logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO) # Initialize the SPLADE model model_name = "naver/splade-cocondenser-ensembledistil" model = SparseEncoder( modules=[ MLMTransformer(model_name), SpladePooling(pooling_strategy="max"), # You can also use 'sum' ], device="cuda:0", ) # Load a parallel sentences dataset dataset = load_dataset("sentence-transformers/parallel-sentences-news-commentary", "en-nl", split="train[:1000]") # Initialize the TranslationEvaluator using the same texts from two languages translation_evaluator = SparseTranslationEvaluator( source_sentences=dataset["english"], target_sentences=dataset["non_english"], name="news-commentary-en-nl", ) results = translation_evaluator(model) # Print the results print(f"Primary metric: {translation_evaluator.primary_metric}") print(f"Primary metric value: {results[translation_evaluator.primary_metric]:.4f}")
# Copyright (c) OpenMMLab. All rights reserved. from .builder import build_match_cost from .match_cost import BBoxL1Cost, ClassificationCost, FocalLossCost, IoUCost __all__ = [ 'build_match_cost', 'ClassificationCost', 'BBoxL1Cost', 'IoUCost', 'FocalLossCost' ]
from .builder import build_match_cost from .match_cost import BBoxL1Cost, ClassificationCost, FocalLossCost, IoUCost __all__ = [ 'build_match_cost', 'ClassificationCost', 'BBoxL1Cost', 'IoUCost', 'FocalLossCost' ]
_base_ = './mask-rcnn_hrnetv2p-w18-1x_coco.py' # learning policy max_epochs = 24 train_cfg = dict(max_epochs=max_epochs) param_scheduler = [ dict( type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[16, 22], gamma=0.1) ]
_base_ = './mask_rcnn_hrnetv2p_w18_1x_coco.py' # learning policy max_epochs = 24 train_cfg = dict(max_epochs=max_epochs) param_scheduler = [ dict( type='LinearLR', start_factor=0.001, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[16, 22], gamma=0.1) ]
import functools import importlib import os import re from pathlib import Path from typing import TYPE_CHECKING, TypeVar if TYPE_CHECKING: from backend.data.block import Block T = TypeVar("T") @functools.cache def load_all_blocks() -> dict[str, type["Block"]]: from backend.data.block import Block # Dynamically load all modules under backend.blocks current_dir = Path(__file__).parent modules = [ str(f.relative_to(current_dir))[:-3].replace(os.path.sep, ".") for f in current_dir.rglob("*.py") if f.is_file() and f.name != "__init__.py" and not f.name.startswith("test_") ] for module in modules: if not re.match("^[a-z0-9_.]+$", module): raise ValueError( f"Block module {module} error: module name must be lowercase, " "and contain only alphanumeric characters and underscores." ) importlib.import_module(f".{module}", package=__name__) # Load all Block instances from the available modules available_blocks: dict[str, type["Block"]] = {} for block_cls in all_subclasses(Block): class_name = block_cls.__name__ if class_name.endswith("Base"): continue if not class_name.endswith("Block"): raise ValueError( f"Block class {class_name} does not end with 'Block'. " "If you are creating an abstract class, " "please name the class with 'Base' at the end" ) block = block_cls.create() if not isinstance(block.id, str) or len(block.id) != 36: raise ValueError( f"Block ID {block.name} error: {block.id} is not a valid UUID" ) if block.id in available_blocks: raise ValueError( f"Block ID {block.name} error: {block.id} is already in use" ) input_schema = block.input_schema.model_fields output_schema = block.output_schema.model_fields # Make sure `error` field is a string in the output schema if "error" in output_schema and output_schema["error"].annotation is not str: raise ValueError( f"{block.name} `error` field in output_schema must be a string" ) # Ensure all fields in input_schema and output_schema are annotated SchemaFields for field_name, field in [*input_schema.items(), *output_schema.items()]: if field.annotation is None: raise ValueError( f"{block.name} has a field {field_name} that is not annotated" ) if field.json_schema_extra is None: raise ValueError( f"{block.name} has a field {field_name} not defined as SchemaField" ) for field in block.input_schema.model_fields.values(): if field.annotation is bool and field.default not in (True, False): raise ValueError( f"{block.name} has a boolean field with no default value" ) available_blocks[block.id] = block_cls return available_blocks __all__ = ["load_all_blocks"] def all_subclasses(cls: type[T]) -> list[type[T]]: subclasses = cls.__subclasses__() for subclass in subclasses: subclasses += all_subclasses(subclass) return subclasses
import functools import importlib import os import re from pathlib import Path from typing import TYPE_CHECKING, TypeVar if TYPE_CHECKING: from backend.data.block import Block T = TypeVar("T") @functools.cache def load_all_blocks() -> dict[str, type["Block"]]: from backend.data.block import Block # Dynamically load all modules under backend.blocks current_dir = Path(__file__).parent modules = [ str(f.relative_to(current_dir))[:-3].replace(os.path.sep, ".") for f in current_dir.rglob("*.py") if f.is_file() and f.name != "__init__.py" ] for module in modules: if not re.match("^[a-z0-9_.]+$", module): raise ValueError( f"Block module {module} error: module name must be lowercase, " "and contain only alphanumeric characters and underscores." ) importlib.import_module(f".{module}", package=__name__) # Load all Block instances from the available modules available_blocks: dict[str, type["Block"]] = {} for block_cls in all_subclasses(Block): class_name = block_cls.__name__ if class_name.endswith("Base"): continue if not class_name.endswith("Block"): raise ValueError( f"Block class {class_name} does not end with 'Block'. " "If you are creating an abstract class, " "please name the class with 'Base' at the end" ) block = block_cls.create() if not isinstance(block.id, str) or len(block.id) != 36: raise ValueError( f"Block ID {block.name} error: {block.id} is not a valid UUID" ) if block.id in available_blocks: raise ValueError( f"Block ID {block.name} error: {block.id} is already in use" ) input_schema = block.input_schema.model_fields output_schema = block.output_schema.model_fields # Make sure `error` field is a string in the output schema if "error" in output_schema and output_schema["error"].annotation is not str: raise ValueError( f"{block.name} `error` field in output_schema must be a string" ) # Ensure all fields in input_schema and output_schema are annotated SchemaFields for field_name, field in [*input_schema.items(), *output_schema.items()]: if field.annotation is None: raise ValueError( f"{block.name} has a field {field_name} that is not annotated" ) if field.json_schema_extra is None: raise ValueError( f"{block.name} has a field {field_name} not defined as SchemaField" ) for field in block.input_schema.model_fields.values(): if field.annotation is bool and field.default not in (True, False): raise ValueError( f"{block.name} has a boolean field with no default value" ) available_blocks[block.id] = block_cls return available_blocks __all__ = ["load_all_blocks"] def all_subclasses(cls: type[T]) -> list[type[T]]: subclasses = cls.__subclasses__() for subclass in subclasses: subclasses += all_subclasses(subclass) return subclasses
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_true(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_true(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']
from typing import Iterable, Type from docarray.array.abstract_array import AbstractDocumentArray from docarray.array.mixins import GetAttributeArrayMixin, ProtoArrayMixin from docarray.document import AnyDocument, BaseDocument, BaseNode class DocumentArray( list, ProtoArrayMixin, GetAttributeArrayMixin, AbstractDocumentArray, BaseNode, ): """ a DocumentArray is a list-like container of Document of the same schema :param docs: iterable of Document """ document_type: Type[BaseDocument] = AnyDocument def __init__(self, docs: Iterable[BaseDocument]): super().__init__(doc_ for doc_ in docs) def __class_getitem__(cls, item: Type[BaseDocument]): if not issubclass(item, BaseDocument): raise ValueError( f'DocumentArray[item] item should be a Document not a {item} ' ) class _DocumenArrayTyped(DocumentArray): document_type: Type[BaseDocument] = item for field in _DocumenArrayTyped.document_type.__fields__.keys(): def _proprety_generator(val: str): return property(lambda self: self._get_documents_attribute(val)) setattr(_DocumenArrayTyped, field, _proprety_generator(field)) # this generates property on the fly based on the schema of the item _DocumenArrayTyped.__name__ = f'DocumentArray{item.__name__}' return _DocumenArrayTyped
from typing import Iterable, Type from docarray.array.abstract_array import AbstractDocumentArray from docarray.array.mixins import GetAttributeArrayMixin, ProtoArrayMixin from docarray.document import AnyDocument, BaseDocument, BaseNode from docarray.document.abstract_document import AbstractDocument class DocumentArray( list, ProtoArrayMixin, GetAttributeArrayMixin, AbstractDocumentArray, BaseNode, ): """ a DocumentArray is a list-like container of Document of the same schema :param docs: iterable of Document """ document_type: Type[BaseDocument] = AnyDocument def __init__(self, docs: Iterable[AbstractDocument]): super().__init__(doc_ for doc_ in docs) def __class_getitem__(cls, item: Type[BaseDocument]): if not issubclass(item, BaseDocument): raise ValueError( f'DocumentArray[item] item should be a Document not a {item} ' ) class _DocumenArrayTyped(DocumentArray): document_type = item for field in _DocumenArrayTyped.document_type.__fields__.keys(): def _proprety_generator(val: str): return property(lambda self: self._get_documents_attribute(val)) setattr(_DocumenArrayTyped, field, _proprety_generator(field)) # this generates property on the fly based on the schema of the item _DocumenArrayTyped.__name__ = f'DocumentArray{item.__name__}' return _DocumenArrayTyped
from __future__ import annotations import os import pytest from sentence_transformers import SentenceTransformer from sentence_transformers.models import Pooling, Transformer from sentence_transformers.util import is_datasets_available from tests.utils import SafeTemporaryDirectory if is_datasets_available(): from datasets import DatasetDict, load_dataset @pytest.fixture() def stsb_bert_tiny_model() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") @pytest.fixture(scope="session") def stsb_bert_tiny_model_reused() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") @pytest.fixture() def stsb_bert_tiny_model_onnx() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-onnx") @pytest.fixture() def stsb_bert_tiny_model_openvino() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-openvino") @pytest.fixture() def paraphrase_distilroberta_base_v1_model() -> SentenceTransformer: return SentenceTransformer("paraphrase-distilroberta-base-v1") @pytest.fixture() def clip_vit_b_32_model() -> SentenceTransformer: return SentenceTransformer("clip-ViT-B-32") @pytest.fixture() def distilbert_base_uncased_model() -> SentenceTransformer: word_embedding_model = Transformer("distilbert-base-uncased") pooling_model = Pooling(word_embedding_model.get_word_embedding_dimension()) model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) return model @pytest.fixture(scope="session") def stsb_dataset_dict() -> DatasetDict: return load_dataset("sentence-transformers/stsb") @pytest.fixture() def cache_dir(): """ In the CI environment, we use a temporary directory as `cache_dir` to avoid keeping the downloaded models on disk after the test. """ if os.environ.get("CI", None): # Note: `ignore_cleanup_errors=True` is used to avoid NotADirectoryError in Windows on GitHub Actions. # See https://github.com/python/cpython/issues/107408, https://www.scivision.dev/python-tempfile-permission-error-windows/ with SafeTemporaryDirectory() as tmp_dir: yield tmp_dir else: yield None
from __future__ import annotations import os import pytest from sentence_transformers import CrossEncoder, SentenceTransformer from sentence_transformers.models import Pooling, Transformer from sentence_transformers.util import is_datasets_available from tests.utils import SafeTemporaryDirectory if is_datasets_available(): from datasets import DatasetDict, load_dataset @pytest.fixture() def stsb_bert_tiny_model() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") @pytest.fixture(scope="session") def stsb_bert_tiny_model_reused() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-safetensors") @pytest.fixture() def stsb_bert_tiny_model_onnx() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-onnx") @pytest.fixture() def stsb_bert_tiny_model_openvino() -> SentenceTransformer: return SentenceTransformer("sentence-transformers-testing/stsb-bert-tiny-openvino") @pytest.fixture() def paraphrase_distilroberta_base_v1_model() -> SentenceTransformer: return SentenceTransformer("paraphrase-distilroberta-base-v1") @pytest.fixture() def distilroberta_base_ce_model() -> CrossEncoder: return CrossEncoder("distilroberta-base", num_labels=1) @pytest.fixture() def clip_vit_b_32_model() -> SentenceTransformer: return SentenceTransformer("clip-ViT-B-32") @pytest.fixture() def distilbert_base_uncased_model() -> SentenceTransformer: word_embedding_model = Transformer("distilbert-base-uncased") pooling_model = Pooling(word_embedding_model.get_word_embedding_dimension()) model = SentenceTransformer(modules=[word_embedding_model, pooling_model]) return model @pytest.fixture(scope="session") def stsb_dataset_dict() -> DatasetDict: return load_dataset("mteb/stsbenchmark-sts") @pytest.fixture() def cache_dir(): """ In the CI environment, we use a temporary directory as `cache_dir` to avoid keeping the downloaded models on disk after the test. """ if os.environ.get("CI", None): # Note: `ignore_cleanup_errors=True` is used to avoid NotADirectoryError in Windows on GitHub Actions. # See https://github.com/python/cpython/issues/107408, https://www.scivision.dev/python-tempfile-permission-error-windows/ with SafeTemporaryDirectory() as tmp_dir: yield tmp_dir else: yield None
from typing import TYPE_CHECKING from ..utils import DIFFUSERS_SLOW_IMPORT, _LazyModule, deprecate from ..utils.import_utils import is_peft_available, is_torch_available, is_transformers_available def text_encoder_lora_state_dict(text_encoder): deprecate( "text_encoder_load_state_dict in `models`", "0.27.0", "`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.", ) state_dict = {} for name, module in text_encoder_attn_modules(text_encoder): for k, v in module.q_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.q_proj.lora_linear_layer.{k}"] = v for k, v in module.k_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.k_proj.lora_linear_layer.{k}"] = v for k, v in module.v_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.v_proj.lora_linear_layer.{k}"] = v for k, v in module.out_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.out_proj.lora_linear_layer.{k}"] = v return state_dict if is_transformers_available(): def text_encoder_attn_modules(text_encoder): deprecate( "text_encoder_attn_modules in `models`", "0.27.0", "`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.", ) from transformers import CLIPTextModel, CLIPTextModelWithProjection attn_modules = [] if isinstance(text_encoder, (CLIPTextModel, CLIPTextModelWithProjection)): for i, layer in enumerate(text_encoder.text_model.encoder.layers): name = f"text_model.encoder.layers.{i}.self_attn" mod = layer.self_attn attn_modules.append((name, mod)) else: raise ValueError(f"do not know how to get attention modules for: {text_encoder.__class__.__name__}") return attn_modules _import_structure = {} if is_torch_available(): _import_structure["single_file_model"] = ["FromOriginalModelMixin"] _import_structure["unet"] = ["UNet2DConditionLoadersMixin"] _import_structure["utils"] = ["AttnProcsLayers"] if is_transformers_available(): _import_structure["single_file"] = ["FromSingleFileMixin"] _import_structure["lora_pipeline"] = [ "AmusedLoraLoaderMixin", "StableDiffusionLoraLoaderMixin", "SD3LoraLoaderMixin", "StableDiffusionXLLoraLoaderMixin", "LoraLoaderMixin", "FluxLoraLoaderMixin", "CogVideoXLoraLoaderMixin", "Mochi1LoraLoaderMixin", ] _import_structure["textual_inversion"] = ["TextualInversionLoaderMixin"] _import_structure["ip_adapter"] = ["IPAdapterMixin"] _import_structure["peft"] = ["PeftAdapterMixin"] if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT: if is_torch_available(): from .single_file_model import FromOriginalModelMixin from .unet import UNet2DConditionLoadersMixin from .utils import AttnProcsLayers if is_transformers_available(): from .ip_adapter import IPAdapterMixin from .lora_pipeline import ( AmusedLoraLoaderMixin, CogVideoXLoraLoaderMixin, FluxLoraLoaderMixin, LoraLoaderMixin, Mochi1LoraLoaderMixin, SD3LoraLoaderMixin, StableDiffusionLoraLoaderMixin, StableDiffusionXLLoraLoaderMixin, ) from .single_file import FromSingleFileMixin from .textual_inversion import TextualInversionLoaderMixin from .peft import PeftAdapterMixin else: import sys sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
from typing import TYPE_CHECKING from ..utils import DIFFUSERS_SLOW_IMPORT, _LazyModule, deprecate from ..utils.import_utils import is_peft_available, is_torch_available, is_transformers_available def text_encoder_lora_state_dict(text_encoder): deprecate( "text_encoder_load_state_dict in `models`", "0.27.0", "`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.", ) state_dict = {} for name, module in text_encoder_attn_modules(text_encoder): for k, v in module.q_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.q_proj.lora_linear_layer.{k}"] = v for k, v in module.k_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.k_proj.lora_linear_layer.{k}"] = v for k, v in module.v_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.v_proj.lora_linear_layer.{k}"] = v for k, v in module.out_proj.lora_linear_layer.state_dict().items(): state_dict[f"{name}.out_proj.lora_linear_layer.{k}"] = v return state_dict if is_transformers_available(): def text_encoder_attn_modules(text_encoder): deprecate( "text_encoder_attn_modules in `models`", "0.27.0", "`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.", ) from transformers import CLIPTextModel, CLIPTextModelWithProjection attn_modules = [] if isinstance(text_encoder, (CLIPTextModel, CLIPTextModelWithProjection)): for i, layer in enumerate(text_encoder.text_model.encoder.layers): name = f"text_model.encoder.layers.{i}.self_attn" mod = layer.self_attn attn_modules.append((name, mod)) else: raise ValueError(f"do not know how to get attention modules for: {text_encoder.__class__.__name__}") return attn_modules _import_structure = {} if is_torch_available(): _import_structure["single_file_model"] = ["FromOriginalModelMixin"] _import_structure["unet"] = ["UNet2DConditionLoadersMixin"] _import_structure["utils"] = ["AttnProcsLayers"] if is_transformers_available(): _import_structure["single_file"] = ["FromSingleFileMixin"] _import_structure["lora_pipeline"] = [ "AmusedLoraLoaderMixin", "StableDiffusionLoraLoaderMixin", "SD3LoraLoaderMixin", "StableDiffusionXLLoraLoaderMixin", "LoraLoaderMixin", "FluxLoraLoaderMixin", "CogVideoXLoraLoaderMixin", ] _import_structure["textual_inversion"] = ["TextualInversionLoaderMixin"] _import_structure["ip_adapter"] = ["IPAdapterMixin"] _import_structure["peft"] = ["PeftAdapterMixin"] if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT: if is_torch_available(): from .single_file_model import FromOriginalModelMixin from .unet import UNet2DConditionLoadersMixin from .utils import AttnProcsLayers if is_transformers_available(): from .ip_adapter import IPAdapterMixin from .lora_pipeline import ( AmusedLoraLoaderMixin, CogVideoXLoraLoaderMixin, FluxLoraLoaderMixin, LoraLoaderMixin, SD3LoraLoaderMixin, StableDiffusionLoraLoaderMixin, StableDiffusionXLLoraLoaderMixin, ) from .single_file import FromSingleFileMixin from .textual_inversion import TextualInversionLoaderMixin from .peft import PeftAdapterMixin else: import sys sys.modules[__name__] = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
_base_ = 'mask-rcnn_r50-caffe_fpn_rpn-2conv_4conv1fc_syncbn-all_lsj-100e_coco.py' # noqa # Enable automatic-mixed-precision training with AmpOptimWrapper. optim_wrapper = dict(type='AmpOptimWrapper')
_base_ = 'mask_rcnn_r50_caffe_fpn_syncbn-all_rpn-2conv_lsj_100e_coco.py' # Enable automatic-mixed-precision training with AmpOptimWrapper. optim_wrapper = dict(type='AmpOptimWrapper')
# pants requires this import to recognize the dep import pytest_asyncio # noqa: F401 import pytest import os from llama_index.embeddings.nvidia import NVIDIAEmbedding as Interface from llama_index.embeddings.nvidia.base import DEFAULT_MODEL from typing import Generator # this fixture is used to mask the NVIDIA_API_KEY environment variable and restore it # after the test. it also returns the value of the NVIDIA_API_KEY environment variable # before it was masked so that it can be used in the test. @pytest.fixture() def masked_env_var() -> Generator[str, None, None]: var = "NVIDIA_API_KEY" try: if val := os.environ.get(var, None): del os.environ[var] yield val finally: if val: os.environ[var] = val @pytest.fixture(params=[Interface]) def public_class(request: pytest.FixtureRequest) -> type: return request.param def pytest_collection_modifyitems(config, items): if "NVIDIA_API_KEY" not in os.environ: skip_marker = pytest.mark.skip( reason="requires NVIDIA_API_KEY environment variable or --nim-endpoint option" ) for item in items: if "integration" in item.keywords and not config.getoption( "--nim-endpoint" ): item.add_marker(skip_marker) def pytest_addoption(parser: pytest.Parser) -> None: parser.addoption( "--all-models", action="store_true", help="Run tests across all models", ) parser.addoption( "--model-id", action="store", help="Run tests for a specific chat model", ) parser.addoption( "--nim-endpoint", type=str, help="Run tests using NIM mode", ) def get_mode(config: pytest.Config) -> dict: nim_endpoint = config.getoption("--nim-endpoint") if nim_endpoint: return {"base_url": nim_endpoint} return {} def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: mode = get_mode(metafunc.config) if "model" in metafunc.fixturenames: models = [DEFAULT_MODEL] if model := metafunc.config.getoption("--model-id"): models = [model] elif metafunc.config.getoption("--all-models"): models = [model.id for model in Interface(**mode).available_models] metafunc.parametrize("model", models, ids=models) @pytest.fixture() def mode(request: pytest.FixtureRequest) -> dict: return get_mode(request.config)
import pytest import os from llama_index.embeddings.nvidia import NVIDIAEmbedding as Interface from llama_index.embeddings.nvidia.base import DEFAULT_MODEL from typing import Generator # this fixture is used to mask the NVIDIA_API_KEY environment variable and restore it # after the test. it also returns the value of the NVIDIA_API_KEY environment variable # before it was masked so that it can be used in the test. @pytest.fixture() def masked_env_var() -> Generator[str, None, None]: var = "NVIDIA_API_KEY" try: if val := os.environ.get(var, None): del os.environ[var] yield val finally: if val: os.environ[var] = val @pytest.fixture(params=[Interface]) def public_class(request: pytest.FixtureRequest) -> type: return request.param def pytest_collection_modifyitems(config, items): if "NVIDIA_API_KEY" not in os.environ: skip_marker = pytest.mark.skip( reason="requires NVIDIA_API_KEY environment variable or --nim-endpoint option" ) for item in items: if "integration" in item.keywords and not config.getoption( "--nim-endpoint" ): item.add_marker(skip_marker) def pytest_addoption(parser: pytest.Parser) -> None: parser.addoption( "--all-models", action="store_true", help="Run tests across all models", ) parser.addoption( "--model-id", action="store", help="Run tests for a specific chat model", ) parser.addoption( "--nim-endpoint", type=str, help="Run tests using NIM mode", ) def get_mode(config: pytest.Config) -> dict: nim_endpoint = config.getoption("--nim-endpoint") if nim_endpoint: return {"base_url": nim_endpoint} return {} def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: mode = get_mode(metafunc.config) if "model" in metafunc.fixturenames: models = [DEFAULT_MODEL] if model := metafunc.config.getoption("--model-id"): models = [model] elif metafunc.config.getoption("--all-models"): models = [model.id for model in Interface(**mode).available_models] metafunc.parametrize("model", models, ids=models) @pytest.fixture() def mode(request: pytest.FixtureRequest) -> dict: return get_mode(request.config)
# coding=utf-8 # Copyright 2024 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image from diffusers.utils.testing_utils import require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class FlaxStableDiffusionInpaintPipelineIntegrationTests(unittest.TestCase): def tearDown(self): # clean up the VRAM after each test super().tearDown() gc.collect() def test_stable_diffusion_inpaint_pipeline(self): init_image = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-inpaint/init_image.png" ) mask_image = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png" ) model_id = "xvjiarui/stable-diffusion-2-inpainting" pipeline, params = FlaxStableDiffusionInpaintPipeline.from_pretrained(model_id, safety_checker=None) prompt = "Face of a yellow cat, high resolution, sitting on a park bench" prng_seed = jax.random.PRNGKey(0) num_inference_steps = 50 num_samples = jax.device_count() prompt = num_samples * [prompt] init_image = num_samples * [init_image] mask_image = num_samples * [mask_image] prompt_ids, processed_masked_images, processed_masks = pipeline.prepare_inputs(prompt, init_image, mask_image) # shard inputs and rng params = replicate(params) prng_seed = jax.random.split(prng_seed, jax.device_count()) prompt_ids = shard(prompt_ids) processed_masked_images = shard(processed_masked_images) processed_masks = shard(processed_masks) output = pipeline( prompt_ids, processed_masks, processed_masked_images, params, prng_seed, num_inference_steps, jit=True ) images = output.images.reshape(num_samples, 512, 512, 3) image_slice = images[0, 253:256, 253:256, -1] output_slice = jnp.asarray(jax.device_get(image_slice.flatten())) expected_slice = jnp.array( [0.3611307, 0.37649736, 0.3757408, 0.38213953, 0.39295167, 0.3841631, 0.41554978, 0.4137475, 0.4217084] ) assert jnp.abs(output_slice - expected_slice).max() < 1e-2
# coding=utf-8 # Copyright 2024 HuggingFace Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import gc import unittest from diffusers import FlaxStableDiffusionInpaintPipeline from diffusers.utils import is_flax_available, load_image from diffusers.utils.testing_utils import require_flax, slow if is_flax_available(): import jax import jax.numpy as jnp from flax.jax_utils import replicate from flax.training.common_utils import shard @slow @require_flax class FlaxStableDiffusionInpaintPipelineIntegrationTests(unittest.TestCase): def tearDown(self): # clean up the VRAM after each test super().tearDown() gc.collect() def test_stable_diffusion_inpaint_pipeline(self): init_image = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/sd2-inpaint/init_image.png" ) mask_image = load_image( "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png" ) model_id = "xvjiarui/stable-diffusion-2-inpainting" pipeline, params = FlaxStableDiffusionInpaintPipeline.from_pretrained(model_id, safety_checker=None) prompt = "Face of a yellow cat, high resolution, sitting on a park bench" prng_seed = jax.random.PRNGKey(0) num_inference_steps = 50 num_samples = jax.device_count() prompt = num_samples * [prompt] init_image = num_samples * [init_image] mask_image = num_samples * [mask_image] prompt_ids, processed_masked_images, processed_masks = pipeline.prepare_inputs(prompt, init_image, mask_image) # shard inputs and rng params = replicate(params) prng_seed = jax.random.split(prng_seed, jax.device_count()) prompt_ids = shard(prompt_ids) processed_masked_images = shard(processed_masked_images) processed_masks = shard(processed_masks) output = pipeline( prompt_ids, processed_masks, processed_masked_images, params, prng_seed, num_inference_steps, jit=True ) images = output.images.reshape(num_samples, 512, 512, 3) image_slice = images[0, 253:256, 253:256, -1] output_slice = jnp.asarray(jax.device_get(image_slice.flatten())) expected_slice = jnp.array( [0.3611307, 0.37649736, 0.3757408, 0.38213953, 0.39295167, 0.3841631, 0.41554978, 0.4137475, 0.4217084] ) print(f"output_slice: {output_slice}") assert jnp.abs(output_slice - expected_slice).max() < 1e-2
import collections.abc import dataclasses from typing import Optional, Sequence import pytest import torch from torch.nn.functional import one_hot from torchvision.prototype import datapoints from transforms_v2_legacy_utils import combinations_grid, DEFAULT_EXTRA_DIMS, from_loader, from_loaders, TensorLoader @dataclasses.dataclass class LabelLoader(TensorLoader): categories: Optional[Sequence[str]] def _parse_categories(categories): if categories is None: num_categories = int(torch.randint(1, 11, ())) elif isinstance(categories, int): num_categories = categories categories = [f"category{idx}" for idx in range(num_categories)] elif isinstance(categories, collections.abc.Sequence) and all(isinstance(category, str) for category in categories): categories = list(categories) num_categories = len(categories) else: raise pytest.UsageError( f"`categories` can either be `None` (default), an integer, or a sequence of strings, " f"but got '{categories}' instead." ) return categories, num_categories def make_label_loader(*, extra_dims=(), categories=None, dtype=torch.int64): categories, num_categories = _parse_categories(categories) def fn(shape, dtype, device): # The idiom `make_tensor(..., dtype=torch.int64).to(dtype)` is intentional to only get integer values, # regardless of the requested dtype, e.g. 0 or 0.0 rather than 0 or 0.123 data = torch.testing.make_tensor(shape, low=0, high=num_categories, dtype=torch.int64, device=device).to(dtype) return datapoints.Label(data, categories=categories) return LabelLoader(fn, shape=extra_dims, dtype=dtype, categories=categories) make_label = from_loader(make_label_loader) @dataclasses.dataclass class OneHotLabelLoader(TensorLoader): categories: Optional[Sequence[str]] def make_one_hot_label_loader(*, categories=None, extra_dims=(), dtype=torch.int64): categories, num_categories = _parse_categories(categories) def fn(shape, dtype, device): if num_categories == 0: data = torch.empty(shape, dtype=dtype, device=device) else: # The idiom `make_label_loader(..., dtype=torch.int64); ...; one_hot(...).to(dtype)` is intentional # since `one_hot` only supports int64 label = make_label_loader(extra_dims=extra_dims, categories=num_categories, dtype=torch.int64).load(device) data = one_hot(label, num_classes=num_categories).to(dtype) return datapoints.OneHotLabel(data, categories=categories) return OneHotLabelLoader(fn, shape=(*extra_dims, num_categories), dtype=dtype, categories=categories) def make_one_hot_label_loaders( *, categories=(1, 0, None), extra_dims=DEFAULT_EXTRA_DIMS, dtypes=(torch.int64, torch.float32), ): for params in combinations_grid(categories=categories, extra_dims=extra_dims, dtype=dtypes): yield make_one_hot_label_loader(**params) make_one_hot_labels = from_loaders(make_one_hot_label_loaders)
import collections.abc import dataclasses from typing import Optional, Sequence import pytest import torch from common_utils import combinations_grid, DEFAULT_EXTRA_DIMS, from_loader, from_loaders, TensorLoader from torch.nn.functional import one_hot from torchvision.prototype import datapoints @dataclasses.dataclass class LabelLoader(TensorLoader): categories: Optional[Sequence[str]] def _parse_categories(categories): if categories is None: num_categories = int(torch.randint(1, 11, ())) elif isinstance(categories, int): num_categories = categories categories = [f"category{idx}" for idx in range(num_categories)] elif isinstance(categories, collections.abc.Sequence) and all(isinstance(category, str) for category in categories): categories = list(categories) num_categories = len(categories) else: raise pytest.UsageError( f"`categories` can either be `None` (default), an integer, or a sequence of strings, " f"but got '{categories}' instead." ) return categories, num_categories def make_label_loader(*, extra_dims=(), categories=None, dtype=torch.int64): categories, num_categories = _parse_categories(categories) def fn(shape, dtype, device): # The idiom `make_tensor(..., dtype=torch.int64).to(dtype)` is intentional to only get integer values, # regardless of the requested dtype, e.g. 0 or 0.0 rather than 0 or 0.123 data = torch.testing.make_tensor(shape, low=0, high=num_categories, dtype=torch.int64, device=device).to(dtype) return datapoints.Label(data, categories=categories) return LabelLoader(fn, shape=extra_dims, dtype=dtype, categories=categories) make_label = from_loader(make_label_loader) @dataclasses.dataclass class OneHotLabelLoader(TensorLoader): categories: Optional[Sequence[str]] def make_one_hot_label_loader(*, categories=None, extra_dims=(), dtype=torch.int64): categories, num_categories = _parse_categories(categories) def fn(shape, dtype, device): if num_categories == 0: data = torch.empty(shape, dtype=dtype, device=device) else: # The idiom `make_label_loader(..., dtype=torch.int64); ...; one_hot(...).to(dtype)` is intentional # since `one_hot` only supports int64 label = make_label_loader(extra_dims=extra_dims, categories=num_categories, dtype=torch.int64).load(device) data = one_hot(label, num_classes=num_categories).to(dtype) return datapoints.OneHotLabel(data, categories=categories) return OneHotLabelLoader(fn, shape=(*extra_dims, num_categories), dtype=dtype, categories=categories) def make_one_hot_label_loaders( *, categories=(1, 0, None), extra_dims=DEFAULT_EXTRA_DIMS, dtypes=(torch.int64, torch.float32), ): for params in combinations_grid(categories=categories, extra_dims=extra_dims, dtype=dtypes): yield make_one_hot_label_loader(**params) make_one_hot_labels = from_loaders(make_one_hot_label_loaders)
import types from typing_extensions import TYPE_CHECKING from docarray.typing.tensor.image.image_ndarray import ImageNdArray from docarray.typing.tensor.image.image_tensor import ImageTensor from docarray.utils._internal.misc import ( _get_path_from_docarray_root_level, import_library, ) if TYPE_CHECKING: from docarray.typing.tensor.image.image_jax_array import ImageJaxArray # noqa from docarray.typing.tensor.image.image_tensorflow_tensor import ( # noqa ImageTensorFlowTensor, ) from docarray.typing.tensor.image.image_torch_tensor import ImageTorchTensor # noqa __all__ = ['ImageNdArray', 'ImageTensor'] def __getattr__(name: str): lib: types.ModuleType if name == 'ImageTorchTensor': import_library('torch', raise_error=True) import docarray.typing.tensor.image.image_torch_tensor as lib elif name == 'ImageTensorFlowTensor': import_library('tensorflow', raise_error=True) import docarray.typing.tensor.image.image_tensorflow_tensor as lib elif name == 'ImageJaxArray': import_library('jax', raise_error=True) import docarray.typing.tensor.image.image_jax_array as lib else: raise ImportError( f'cannot import name \'{name}\' from \'{_get_path_from_docarray_root_level(__file__)}\'' ) tensor_cls = getattr(lib, name) if name not in __all__: __all__.append(name) return tensor_cls
import types from typing_extensions import TYPE_CHECKING from docarray.typing.tensor.image.image_ndarray import ImageNdArray from docarray.typing.tensor.image.image_tensor import ImageTensor from docarray.utils._internal.misc import ( _get_path_from_docarray_root_level, import_library, ) if TYPE_CHECKING: from docarray.typing.tensor.image.image_tensorflow_tensor import ( # noqa ImageTensorFlowTensor, ) from docarray.typing.tensor.image.image_torch_tensor import ImageTorchTensor # noqa __all__ = ['ImageNdArray', 'ImageTensor'] def __getattr__(name: str): lib: types.ModuleType if name == 'ImageTorchTensor': import_library('torch', raise_error=True) import docarray.typing.tensor.image.image_torch_tensor as lib elif name == 'ImageTensorFlowTensor': import_library('tensorflow', raise_error=True) import docarray.typing.tensor.image.image_tensorflow_tensor as lib else: raise ImportError( f'cannot import name \'{name}\' from \'{_get_path_from_docarray_root_level(__file__)}\'' ) tensor_cls = getattr(lib, name) if name not in __all__: __all__.append(name) return tensor_cls
from __future__ import annotations try: from typing import Self except ImportError: from typing_extensions import Self import torch import transformers from PIL import Image from sentence_transformers.models.Asym import InputModule class CLIPModel(InputModule): save_in_root: bool = True def __init__(self, model_name: str = "openai/clip-vit-base-patch32", processor_name=None) -> None: super().__init__() if processor_name is None: processor_name = model_name self.model = transformers.CLIPModel.from_pretrained(model_name) self.processor = transformers.CLIPProcessor.from_pretrained(processor_name) def __repr__(self) -> str: return "CLIPModel()" @property def max_seq_length(self) -> int: return self.processor.tokenizer.model_max_length @max_seq_length.setter def max_seq_length(self, value: int) -> None: self.processor.tokenizer.model_max_length = value def forward(self, features: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: image_embeds = [] text_embeds = [] if "pixel_values" in features: vision_outputs = self.model.vision_model(pixel_values=features["pixel_values"]) image_embeds = self.model.visual_projection(vision_outputs[1]) if "input_ids" in features: text_outputs = self.model.text_model( input_ids=features.get("input_ids"), attention_mask=features.get("attention_mask", None), position_ids=features.get("position_ids", None), output_attentions=features.get("output_attentions", None), output_hidden_states=features.get("output_hidden_states", None), ) text_embeds = self.model.text_projection(text_outputs[1]) sentence_embedding = [] image_features = iter(image_embeds) text_features = iter(text_embeds) for idx, input_type in enumerate(features["image_text_info"]): if input_type == 0: sentence_embedding.append(next(image_features)) else: sentence_embedding.append(next(text_features)) features["sentence_embedding"] = torch.stack(sentence_embedding).float() return features def tokenize(self, texts, padding: str | bool = True) -> dict[str, torch.Tensor]: images = [] texts_values = [] image_text_info = [] for idx, data in enumerate(texts): if isinstance(data, Image.Image): # An Image images.append(data) image_text_info.append(0) else: # A text texts_values.append(data) image_text_info.append(1) encoding = {} if len(texts_values): encoding = self.processor.tokenizer(texts_values, padding=padding, truncation=True, return_tensors="pt") if len(images): image_features = self.processor.image_processor(images, return_tensors="pt") encoding["pixel_values"] = image_features.pixel_values encoding["image_text_info"] = image_text_info return dict(encoding) @property def tokenizer(self) -> transformers.CLIPProcessor: return self.processor def save(self, output_path: str, *args, safe_serialization: bool = True, **kwargs) -> None: self.model.save_pretrained(output_path, safe_serialization=safe_serialization) self.processor.save_pretrained(output_path) @classmethod def load( cls, model_name_or_path: str, subfolder: str = "", token: bool | str | None = None, cache_folder: str | None = None, revision: str | None = None, local_files_only: bool = False, **kwargs, ) -> Self: local_path = cls.load_dir_path( model_name_or_path=model_name_or_path, subfolder=subfolder, token=token, cache_folder=cache_folder, revision=revision, local_files_only=local_files_only, ) return cls(local_path)
from __future__ import annotations import torch import transformers from PIL import Image from torch import nn class CLIPModel(nn.Module): save_in_root: bool = True def __init__(self, model_name: str = "openai/clip-vit-base-patch32", processor_name=None) -> None: super().__init__() if processor_name is None: processor_name = model_name self.model = transformers.CLIPModel.from_pretrained(model_name) self.processor = transformers.CLIPProcessor.from_pretrained(processor_name) def __repr__(self) -> str: return "CLIPModel()" @property def max_seq_length(self) -> int: return self.processor.tokenizer.model_max_length @max_seq_length.setter def max_seq_length(self, value: int) -> None: self.processor.tokenizer.model_max_length = value def forward(self, features: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: image_embeds = [] text_embeds = [] if "pixel_values" in features: vision_outputs = self.model.vision_model(pixel_values=features["pixel_values"]) image_embeds = self.model.visual_projection(vision_outputs[1]) if "input_ids" in features: text_outputs = self.model.text_model( input_ids=features.get("input_ids"), attention_mask=features.get("attention_mask", None), position_ids=features.get("position_ids", None), output_attentions=features.get("output_attentions", None), output_hidden_states=features.get("output_hidden_states", None), ) text_embeds = self.model.text_projection(text_outputs[1]) sentence_embedding = [] image_features = iter(image_embeds) text_features = iter(text_embeds) for idx, input_type in enumerate(features["image_text_info"]): if input_type == 0: sentence_embedding.append(next(image_features)) else: sentence_embedding.append(next(text_features)) features["sentence_embedding"] = torch.stack(sentence_embedding).float() return features def tokenize(self, texts, padding: str | bool = True) -> dict[str, torch.Tensor]: images = [] texts_values = [] image_text_info = [] for idx, data in enumerate(texts): if isinstance(data, Image.Image): # An Image images.append(data) image_text_info.append(0) else: # A text texts_values.append(data) image_text_info.append(1) encoding = {} if len(texts_values): encoding = self.processor.tokenizer(texts_values, padding=padding, truncation=True, return_tensors="pt") if len(images): image_features = self.processor.image_processor(images, return_tensors="pt") encoding["pixel_values"] = image_features.pixel_values encoding["image_text_info"] = image_text_info return dict(encoding) @property def tokenizer(self) -> transformers.CLIPProcessor: return self.processor def save(self, output_path: str) -> None: self.model.save_pretrained(output_path) self.processor.save_pretrained(output_path) @staticmethod def load(input_path: str) -> CLIPModel: return CLIPModel(model_name=input_path)
from __future__ import annotations from collections.abc import Iterable from enum import Enum from typing import Any import torch.nn.functional as F from torch import Tensor, nn from sentence_transformers.SentenceTransformer import SentenceTransformer from sentence_transformers.util import pairwise_cos_sim, pairwise_euclidean_sim, pairwise_manhattan_sim class TripletDistanceMetric(Enum): """The metric for the triplet loss""" COSINE = lambda x, y: 1 - pairwise_cos_sim(x, y) EUCLIDEAN = lambda x, y: pairwise_euclidean_sim(x, y) MANHATTAN = lambda x, y: pairwise_manhattan_sim(x, y) class TripletLoss(nn.Module): def __init__( self, model: SentenceTransformer, distance_metric=TripletDistanceMetric.EUCLIDEAN, triplet_margin: float = 5 ) -> None: """ This class implements triplet loss. Given a triplet of (anchor, positive, negative), the loss minimizes the distance between anchor and positive while it maximizes the distance between anchor and negative. It compute the following loss function: ``loss = max(||anchor - positive|| - ||anchor - negative|| + margin, 0)``. Margin is an important hyperparameter and needs to be tuned respectively. Args: model: SentenceTransformerModel distance_metric: Function to compute distance between two embeddings. The class TripletDistanceMetric contains common distance metrices that can be used. triplet_margin: The negative should be at least this much further away from the anchor than the positive. References: - For further details, see: https://en.wikipedia.org/wiki/Triplet_loss Requirements: 1. (anchor, positive, negative) triplets Inputs: +---------------------------------------+--------+ | Texts | Labels | +=======================================+========+ | (anchor, positive, negative) triplets | none | +---------------------------------------+--------+ Example: :: from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses from datasets import Dataset model = SentenceTransformer("microsoft/mpnet-base") 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."], "negative": ["It's quite rainy, sadly.", "She walked to the store."], }) loss = losses.TripletLoss(model=model) trainer = SentenceTransformerTrainer( model=model, train_dataset=train_dataset, loss=loss, ) trainer.train() """ super().__init__() self.model = model self.distance_metric = distance_metric self.triplet_margin = triplet_margin 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 CoSENT loss from embeddings. Args: embeddings: List of embeddings Returns: Loss value """ rep_anchor, rep_pos, rep_neg = embeddings distance_pos = self.distance_metric(rep_anchor, rep_pos) distance_neg = self.distance_metric(rep_anchor, rep_neg) losses = F.relu(distance_pos - distance_neg + self.triplet_margin) return losses.mean() def get_config_dict(self) -> dict[str, Any]: distance_metric_name = self.distance_metric.__name__ for name, value in vars(TripletDistanceMetric).items(): if value == self.distance_metric: distance_metric_name = f"TripletDistanceMetric.{name}" break return {"distance_metric": distance_metric_name, "triplet_margin": self.triplet_margin} @property def citation(self) -> str: return """ @misc{hermans2017defense, title={In Defense of the Triplet Loss for Person Re-Identification}, author={Alexander Hermans and Lucas Beyer and Bastian Leibe}, year={2017}, eprint={1703.07737}, archivePrefix={arXiv}, primaryClass={cs.CV} } """
from __future__ import annotations from collections.abc import Iterable from enum import Enum from typing import Any import torch.nn.functional as F from torch import Tensor, nn from sentence_transformers.SentenceTransformer import SentenceTransformer from sentence_transformers.util import pairwise_cos_sim, pairwise_euclidean_sim, pairwise_manhattan_sim class TripletDistanceMetric(Enum): """The metric for the triplet loss""" COSINE = lambda x, y: 1 - pairwise_cos_sim(x, y) EUCLIDEAN = lambda x, y: pairwise_euclidean_sim(x, y) MANHATTAN = lambda x, y: pairwise_manhattan_sim(x, y) class TripletLoss(nn.Module): def __init__( self, model: SentenceTransformer, distance_metric=TripletDistanceMetric.EUCLIDEAN, triplet_margin: float = 5 ) -> None: """ This class implements triplet loss. Given a triplet of (anchor, positive, negative), the loss minimizes the distance between anchor and positive while it maximizes the distance between anchor and negative. It compute the following loss function: ``loss = max(||anchor - positive|| - ||anchor - negative|| + margin, 0)``. Margin is an important hyperparameter and needs to be tuned respectively. Args: model: SentenceTransformerModel distance_metric: Function to compute distance between two embeddings. The class TripletDistanceMetric contains common distance metrices that can be used. triplet_margin: The negative should be at least this much further away from the anchor than the positive. References: - For further details, see: https://en.wikipedia.org/wiki/Triplet_loss Requirements: 1. (anchor, positive, negative) triplets Inputs: +---------------------------------------+--------+ | Texts | Labels | +=======================================+========+ | (anchor, positive, negative) triplets | none | +---------------------------------------+--------+ Example: :: from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses from datasets import Dataset model = SentenceTransformer("microsoft/mpnet-base") 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."], "negative": ["It's quite rainy, sadly.", "She walked to the store."], }) loss = losses.TripletLoss(model=model) trainer = SentenceTransformerTrainer( model=model, train_dataset=train_dataset, loss=loss, ) trainer.train() """ super().__init__() self.model = model self.distance_metric = distance_metric self.triplet_margin = triplet_margin def forward(self, sentence_features: Iterable[dict[str, Tensor]], labels: Tensor) -> Tensor: reps = [self.model(sentence_feature)["sentence_embedding"] for sentence_feature in sentence_features] rep_anchor, rep_pos, rep_neg = reps distance_pos = self.distance_metric(rep_anchor, rep_pos) distance_neg = self.distance_metric(rep_anchor, rep_neg) losses = F.relu(distance_pos - distance_neg + self.triplet_margin) return losses.mean() def get_config_dict(self) -> dict[str, Any]: distance_metric_name = self.distance_metric.__name__ for name, value in vars(TripletDistanceMetric).items(): if value == self.distance_metric: distance_metric_name = f"TripletDistanceMetric.{name}" break return {"distance_metric": distance_metric_name, "triplet_margin": self.triplet_margin} @property def citation(self) -> str: return """ @misc{hermans2017defense, title={In Defense of the Triplet Loss for Person Re-Identification}, author={Alexander Hermans and Lucas Beyer and Bastian Leibe}, year={2017}, eprint={1703.07737}, archivePrefix={arXiv}, primaryClass={cs.CV} } """
"""LLMResult class.""" from __future__ import annotations from copy import deepcopy from typing import Literal, Optional, Union from pydantic import BaseModel from langchain_core.outputs.chat_generation import ChatGeneration, ChatGenerationChunk from langchain_core.outputs.generation import Generation, GenerationChunk from langchain_core.outputs.run_info import RunInfo class LLMResult(BaseModel): """A container for results of an LLM call. Both chat models and LLMs generate an LLMResult object. This object contains the generated outputs and any additional information that the model provider wants to return. """ generations: list[ list[Union[Generation, ChatGeneration, GenerationChunk, ChatGenerationChunk]] ] """Generated outputs. The first dimension of the list represents completions for different input prompts. The second dimension of the list represents different candidate generations for a given prompt. When returned from an LLM the type is list[list[Generation]]. When returned from a chat model the type is list[list[ChatGeneration]]. ChatGeneration is a subclass of Generation that has a field for a structured chat message. """ llm_output: Optional[dict] = None """For arbitrary LLM provider specific output. This dictionary is a free-form dictionary that can contain any information that the provider wants to return. It is not standardized and is provider-specific. Users should generally avoid relying on this field and instead rely on accessing relevant information from standardized fields present in AIMessage. """ run: Optional[list[RunInfo]] = None """List of metadata info for model call for each input.""" type: Literal["LLMResult"] = "LLMResult" """Type is used exclusively for serialization purposes.""" def flatten(self) -> list[LLMResult]: """Flatten generations into a single list. Unpack list[list[Generation]] -> list[LLMResult] where each returned LLMResult contains only a single Generation. If token usage information is available, it is kept only for the LLMResult corresponding to the top-choice Generation, to avoid over-counting of token usage downstream. Returns: List of LLMResults where each returned LLMResult contains a single Generation. """ llm_results = [] for i, gen_list in enumerate(self.generations): # Avoid double counting tokens in OpenAICallback if i == 0: llm_results.append( LLMResult( generations=[gen_list], llm_output=self.llm_output, ) ) else: if self.llm_output is not None: llm_output = deepcopy(self.llm_output) llm_output["token_usage"] = {} else: llm_output = None llm_results.append( LLMResult( generations=[gen_list], llm_output=llm_output, ) ) return llm_results def __eq__(self, other: object) -> bool: """Check for LLMResult equality by ignoring any metadata related to runs.""" if not isinstance(other, LLMResult): return NotImplemented return ( self.generations == other.generations and self.llm_output == other.llm_output ) __hash__ = None # type: ignore[assignment]
"""LLMResult class.""" from __future__ import annotations from copy import deepcopy from typing import Literal, Optional, Union from pydantic import BaseModel from langchain_core.outputs.chat_generation import ChatGeneration, ChatGenerationChunk from langchain_core.outputs.generation import Generation, GenerationChunk from langchain_core.outputs.run_info import RunInfo class LLMResult(BaseModel): """A container for results of an LLM call. Both chat models and LLMs generate an LLMResult object. This object contains the generated outputs and any additional information that the model provider wants to return. """ generations: list[ list[Union[Generation, ChatGeneration, GenerationChunk, ChatGenerationChunk]] ] """Generated outputs. The first dimension of the list represents completions for different input prompts. The second dimension of the list represents different candidate generations for a given prompt. When returned from an LLM the type is list[list[Generation]]. When returned from a chat model the type is list[list[ChatGeneration]]. ChatGeneration is a subclass of Generation that has a field for a structured chat message. """ llm_output: Optional[dict] = None """For arbitrary LLM provider specific output. This dictionary is a free-form dictionary that can contain any information that the provider wants to return. It is not standardized and is provider-specific. Users should generally avoid relying on this field and instead rely on accessing relevant information from standardized fields present in AIMessage. """ run: Optional[list[RunInfo]] = None """List of metadata info for model call for each input.""" type: Literal["LLMResult"] = "LLMResult" """Type is used exclusively for serialization purposes.""" def flatten(self) -> list[LLMResult]: """Flatten generations into a single list. Unpack list[list[Generation]] -> list[LLMResult] where each returned LLMResult contains only a single Generation. If token usage information is available, it is kept only for the LLMResult corresponding to the top-choice Generation, to avoid over-counting of token usage downstream. Returns: List of LLMResults where each returned LLMResult contains a single Generation. """ llm_results = [] for i, gen_list in enumerate(self.generations): # Avoid double counting tokens in OpenAICallback if i == 0: llm_results.append( LLMResult( generations=[gen_list], llm_output=self.llm_output, ) ) else: if self.llm_output is not None: llm_output = deepcopy(self.llm_output) llm_output["token_usage"] = {} else: llm_output = None llm_results.append( LLMResult( generations=[gen_list], llm_output=llm_output, ) ) return llm_results def __eq__(self, other: object) -> bool: """Check for LLMResult equality by ignoring any metadata related to runs.""" if not isinstance(other, LLMResult): return NotImplemented return ( self.generations == other.generations and self.llm_output == other.llm_output )
# Copyright (c) OpenMMLab. All rights reserved. from .visualizer import Visualizer from .writer import (BaseWriter, ComposedWriter, LocalWriter, TensorboardWriter, WandbWriter) __all__ = [ 'Visualizer', 'BaseWriter', 'LocalWriter', 'WandbWriter', 'TensorboardWriter', 'ComposedWriter' ]
# Copyright (c) OpenMMLab. All rights reserved. from .visualizer import Visualizer __all__ = ['Visualizer']
""" This examples measures the inference speed of a certain model Usage: python evaluation_inference_speed.py OR python evaluation_inference_speed.py model_name """ import sys import time import torch from datasets import load_dataset from sentence_transformers import SentenceTransformer # Limit torch to 4 threads torch.set_num_threads(4) model_name = sys.argv[1] if len(sys.argv) > 1 else "bert-base-nli-mean-tokens" # Load a sentence transformer model model = SentenceTransformer(model_name) max_sentences = 100_000 all_nli_dataset = load_dataset("sentence-transformers/all-nli", "pair", split="train") sentences = list(set(all_nli_dataset["anchor"]))[:max_sentences] print("Model Name:", model_name) print("Number of sentences:", len(sentences)) for i in range(3): print("Run", i) start_time = time.time() emb = model.encode(sentences, batch_size=32) end_time = time.time() diff_time = end_time - start_time print("Done after {:.2f} seconds".format(diff_time)) print("Speed: {:.2f} sentences / second".format(len(sentences) / diff_time)) print("=====")
""" This examples measures the inference speed of a certain model Usage: python evaluation_inference_speed.py OR python evaluation_inference_speed.py model_name """ from sentence_transformers import SentenceTransformer import sys import time import torch from datasets import load_dataset # Limit torch to 4 threads torch.set_num_threads(4) model_name = sys.argv[1] if len(sys.argv) > 1 else "bert-base-nli-mean-tokens" # Load a sentence transformer model model = SentenceTransformer(model_name) max_sentences = 100_000 all_nli_dataset = load_dataset("sentence-transformers/all-nli", "pair", split="train") sentences = list(set(all_nli_dataset["anchor"]))[:max_sentences] print("Model Name:", model_name) print("Number of sentences:", len(sentences)) for i in range(3): print("Run", i) start_time = time.time() emb = model.encode(sentences, batch_size=32) end_time = time.time() diff_time = end_time - start_time print("Done after {:.2f} seconds".format(diff_time)) print("Speed: {:.2f} sentences / second".format(len(sentences) / diff_time)) print("=====")
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: request = { '_op_type': 'index', '_id': doc.id, '_index': self._config.index_name, 'embedding': self._map_embedding(doc.embedding), 'blob': doc.to_base64(), } 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 _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 = [ { "_op_type": "index", '_id': value.id, '_index': self._config.index_name, 'embedding': self._map_embedding(value.embedding), 'blob': value.to_base64(), } ] 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 docarray.array.any_array import AnyDocArray from docarray.array.doc_list.doc_list import DocList from docarray.array.doc_vec.doc_vec import DocVec __all__ = ['DocList', 'DocVec', 'AnyDocArray']
from docarray.array.array.array import DocArray from docarray.array.stacked.array_stacked import DocArrayStacked __all__ = ['DocArray', 'DocArrayStacked']
import logging import time from datetime import datetime from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.cron import CronTrigger from autogpt_libs.utils.cache import thread_cached from backend.data.block import BlockInput from backend.data.schedule import ( ExecutionSchedule, add_schedule, get_active_schedules, get_schedules, update_schedule, ) from backend.executor.manager import ExecutionManager from backend.util.service import AppService, expose, get_service_client from backend.util.settings import Config logger = logging.getLogger(__name__) def log(msg, **kwargs): logger.warning("[ExecutionScheduler] " + msg, **kwargs) class ExecutionScheduler(AppService): def __init__(self, refresh_interval=10): super().__init__() self.use_db = True self.last_check = datetime.min self.refresh_interval = refresh_interval @classmethod def get_port(cls) -> int: return Config().execution_scheduler_port @property @thread_cached def execution_client(self) -> ExecutionManager: return get_service_client(ExecutionManager) def run_service(self): scheduler = BackgroundScheduler() scheduler.start() while True: self.__refresh_jobs_from_db(scheduler) time.sleep(self.refresh_interval) def __refresh_jobs_from_db(self, scheduler: BackgroundScheduler): schedules = self.run_and_wait(get_active_schedules(self.last_check)) for schedule in schedules: if schedule.last_updated: self.last_check = max(self.last_check, schedule.last_updated) if not schedule.is_enabled: log(f"Removing recurring job {schedule.id}: {schedule.schedule}") scheduler.remove_job(schedule.id) continue log(f"Adding recurring job {schedule.id}: {schedule.schedule}") scheduler.add_job( self.__execute_graph, CronTrigger.from_crontab(schedule.schedule), id=schedule.id, args=[schedule.graph_id, schedule.input_data, schedule.user_id], replace_existing=True, ) def __execute_graph(self, graph_id: str, input_data: dict, user_id: str): try: log(f"Executing recurring job for graph #{graph_id}") self.execution_client.add_execution(graph_id, input_data, user_id) except Exception as e: logger.exception(f"Error executing graph {graph_id}: {e}") @expose def update_schedule(self, schedule_id: str, is_enabled: bool, user_id: str) -> str: self.run_and_wait(update_schedule(schedule_id, is_enabled, user_id)) return schedule_id @expose def add_execution_schedule( self, graph_id: str, graph_version: int, cron: str, input_data: BlockInput, user_id: str, ) -> str: schedule = ExecutionSchedule( graph_id=graph_id, user_id=user_id, graph_version=graph_version, schedule=cron, input_data=input_data, ) return self.run_and_wait(add_schedule(schedule)).id @expose def get_execution_schedules(self, graph_id: str, user_id: str) -> dict[str, str]: schedules = self.run_and_wait(get_schedules(graph_id, user_id=user_id)) return {v.id: v.schedule for v in schedules}
import logging import time from datetime import datetime from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.cron import CronTrigger from autogpt_libs.utils.cache import thread_cached_property from backend.data.block import BlockInput from backend.data.schedule import ( ExecutionSchedule, add_schedule, get_active_schedules, get_schedules, update_schedule, ) from backend.executor.manager import ExecutionManager from backend.util.service import AppService, expose, get_service_client from backend.util.settings import Config logger = logging.getLogger(__name__) def log(msg, **kwargs): logger.warning("[ExecutionScheduler] " + msg, **kwargs) class ExecutionScheduler(AppService): def __init__(self, refresh_interval=10): super().__init__() self.use_db = True self.last_check = datetime.min self.refresh_interval = refresh_interval @classmethod def get_port(cls) -> int: return Config().execution_scheduler_port @thread_cached_property def execution_client(self) -> ExecutionManager: return get_service_client(ExecutionManager) def run_service(self): scheduler = BackgroundScheduler() scheduler.start() while True: self.__refresh_jobs_from_db(scheduler) time.sleep(self.refresh_interval) def __refresh_jobs_from_db(self, scheduler: BackgroundScheduler): schedules = self.run_and_wait(get_active_schedules(self.last_check)) for schedule in schedules: if schedule.last_updated: self.last_check = max(self.last_check, schedule.last_updated) if not schedule.is_enabled: log(f"Removing recurring job {schedule.id}: {schedule.schedule}") scheduler.remove_job(schedule.id) continue log(f"Adding recurring job {schedule.id}: {schedule.schedule}") scheduler.add_job( self.__execute_graph, CronTrigger.from_crontab(schedule.schedule), id=schedule.id, args=[schedule.graph_id, schedule.input_data, schedule.user_id], replace_existing=True, ) def __execute_graph(self, graph_id: str, input_data: dict, user_id: str): try: log(f"Executing recurring job for graph #{graph_id}") self.execution_client.add_execution(graph_id, input_data, user_id) except Exception as e: logger.exception(f"Error executing graph {graph_id}: {e}") @expose def update_schedule(self, schedule_id: str, is_enabled: bool, user_id: str) -> str: self.run_and_wait(update_schedule(schedule_id, is_enabled, user_id)) return schedule_id @expose def add_execution_schedule( self, graph_id: str, graph_version: int, cron: str, input_data: BlockInput, user_id: str, ) -> str: schedule = ExecutionSchedule( graph_id=graph_id, user_id=user_id, graph_version=graph_version, schedule=cron, input_data=input_data, ) return self.run_and_wait(add_schedule(schedule)).id @expose def get_execution_schedules(self, graph_id: str, user_id: str) -> dict[str, str]: schedules = self.run_and_wait(get_schedules(graph_id, user_id=user_id)) return {v.id: v.schedule for v in schedules}
# Copyright (c) OpenMMLab. All rights reserved. import os import platform import warnings import cv2 import torch.multiprocessing as mp from mmengine import DefaultScope def setup_multi_processes(cfg): """Setup multi-processing environment variables.""" # set multi-process start method as `fork` to speed up the training if platform.system() != 'Windows': mp_start_method = cfg.get('mp_start_method', 'fork') current_method = mp.get_start_method(allow_none=True) if current_method is not None and current_method != mp_start_method: warnings.warn( f'Multi-processing start method `{mp_start_method}` is ' f'different from the previous setting `{current_method}`.' f'It will be force set to `{mp_start_method}`. You can change ' f'this behavior by changing `mp_start_method` in your config.') mp.set_start_method(mp_start_method, force=True) # disable opencv multithreading to avoid system being overloaded opencv_num_threads = cfg.get('opencv_num_threads', 0) cv2.setNumThreads(opencv_num_threads) # setup OMP threads # This code is referred from https://github.com/pytorch/pytorch/blob/master/torch/distributed/run.py # noqa workers_per_gpu = cfg.data.get('workers_per_gpu', 1) if 'train_dataloader' in cfg.data: workers_per_gpu = \ max(cfg.data.train_dataloader.get('workers_per_gpu', 1), workers_per_gpu) if 'OMP_NUM_THREADS' not in os.environ and workers_per_gpu > 1: omp_num_threads = 1 warnings.warn( f'Setting OMP_NUM_THREADS environment variable for each process ' f'to be {omp_num_threads} in default, to avoid your system being ' f'overloaded, please further tune the variable for optimal ' f'performance in your application as needed.') os.environ['OMP_NUM_THREADS'] = str(omp_num_threads) # setup MKL threads if 'MKL_NUM_THREADS' not in os.environ and workers_per_gpu > 1: mkl_num_threads = 1 warnings.warn( f'Setting MKL_NUM_THREADS environment variable for each process ' f'to be {mkl_num_threads} in default, to avoid your system being ' f'overloaded, please further tune the variable for optimal ' f'performance in your application as needed.') os.environ['MKL_NUM_THREADS'] = str(mkl_num_threads) def register_all_modules(init_default_scope: bool = True) -> None: """Register all modules in mmdet into the registries. Args: init_default_scope (bool): Whether initialize the mmdet default scope. When `init_default_scope=True`, the global default scope will be set to `mmdet`, and all registries will build modules from mmdet's registry node. To understand more about the registry, please refer to https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/registry.md Defaults to True. """ # noqa import mmdet.core # noqa: F401,F403 import mmdet.datasets # noqa: F401,F403 import mmdet.metrics # noqa: F401,F403 import mmdet.models # noqa: F401,F403 if init_default_scope: DefaultScope.get_instance('mmdet', scope_name='mmdet')
# Copyright (c) OpenMMLab. All rights reserved. import os import platform import warnings import cv2 import torch.multiprocessing as mp def setup_multi_processes(cfg): """Setup multi-processing environment variables.""" # set multi-process start method as `fork` to speed up the training if platform.system() != 'Windows': mp_start_method = cfg.get('mp_start_method', 'fork') current_method = mp.get_start_method(allow_none=True) if current_method is not None and current_method != mp_start_method: warnings.warn( f'Multi-processing start method `{mp_start_method}` is ' f'different from the previous setting `{current_method}`.' f'It will be force set to `{mp_start_method}`. You can change ' f'this behavior by changing `mp_start_method` in your config.') mp.set_start_method(mp_start_method, force=True) # disable opencv multithreading to avoid system being overloaded opencv_num_threads = cfg.get('opencv_num_threads', 0) cv2.setNumThreads(opencv_num_threads) # setup OMP threads # This code is referred from https://github.com/pytorch/pytorch/blob/master/torch/distributed/run.py # noqa workers_per_gpu = cfg.data.get('workers_per_gpu', 1) if 'train_dataloader' in cfg.data: workers_per_gpu = \ max(cfg.data.train_dataloader.get('workers_per_gpu', 1), workers_per_gpu) if 'OMP_NUM_THREADS' not in os.environ and workers_per_gpu > 1: omp_num_threads = 1 warnings.warn( f'Setting OMP_NUM_THREADS environment variable for each process ' f'to be {omp_num_threads} in default, to avoid your system being ' f'overloaded, please further tune the variable for optimal ' f'performance in your application as needed.') os.environ['OMP_NUM_THREADS'] = str(omp_num_threads) # setup MKL threads if 'MKL_NUM_THREADS' not in os.environ and workers_per_gpu > 1: mkl_num_threads = 1 warnings.warn( f'Setting MKL_NUM_THREADS environment variable for each process ' f'to be {mkl_num_threads} in default, to avoid your system being ' f'overloaded, please further tune the variable for optimal ' f'performance in your application as needed.') os.environ['MKL_NUM_THREADS'] = str(mkl_num_threads)
from torchvision.transforms import AutoAugmentPolicy, InterpolationMode # usort: skip from . import functional, utils # usort: skip from ._transform import Transform # usort: skip from ._augment import Cutmix, Mixup, RandomErasing from ._auto_augment import AugMix, AutoAugment, RandAugment, TrivialAugmentWide from ._color import ( ColorJitter, Grayscale, RandomAdjustSharpness, RandomAutocontrast, RandomEqualize, RandomGrayscale, RandomInvert, RandomPhotometricDistort, RandomPosterize, RandomSolarize, ) from ._container import Compose, RandomApply, RandomChoice, RandomOrder from ._geometry import ( CenterCrop, ElasticTransform, FiveCrop, Pad, RandomAffine, RandomCrop, RandomHorizontalFlip, RandomIoUCrop, RandomPerspective, RandomResize, RandomResizedCrop, RandomRotation, RandomShortestSize, RandomVerticalFlip, RandomZoomOut, Resize, ScaleJitter, TenCrop, ) from ._meta import ClampBoundingBox, ConvertBoundingBoxFormat from ._misc import ( ConvertImageDtype, GaussianBlur, Identity, Lambda, LinearTransformation, Normalize, SanitizeBoundingBox, ToDtype, ) from ._temporal import UniformTemporalSubsample from ._type_conversion import PILToTensor, ToImagePIL, ToImageTensor, ToPILImage from ._deprecated import ToTensor # usort: skip from torchvision import _BETA_TRANSFORMS_WARNING, _WARN_ABOUT_BETA_TRANSFORMS if _WARN_ABOUT_BETA_TRANSFORMS: import warnings warnings.warn(_BETA_TRANSFORMS_WARNING)
from torchvision.transforms import AutoAugmentPolicy, InterpolationMode # usort: skip from . import functional, utils # usort: skip from ._transform import Transform # usort: skip from ._augment import Cutmix, Mixup, RandomErasing from ._auto_augment import AugMix, AutoAugment, RandAugment, TrivialAugmentWide from ._color import ( ColorJitter, Grayscale, RandomAdjustSharpness, RandomAutocontrast, RandomEqualize, RandomGrayscale, RandomInvert, RandomPhotometricDistort, RandomPosterize, RandomSolarize, ) from ._container import Compose, RandomApply, RandomChoice, RandomOrder from ._geometry import ( CenterCrop, ElasticTransform, FiveCrop, Pad, RandomAffine, RandomCrop, RandomHorizontalFlip, RandomIoUCrop, RandomPerspective, RandomResize, RandomResizedCrop, RandomRotation, RandomShortestSize, RandomVerticalFlip, RandomZoomOut, Resize, ScaleJitter, TenCrop, ) from ._meta import ClampBoundingBox, ConvertBoundingBoxFormat, ConvertImageDtype from ._misc import GaussianBlur, Identity, Lambda, LinearTransformation, Normalize, SanitizeBoundingBox, ToDtype from ._temporal import UniformTemporalSubsample from ._type_conversion import PILToTensor, ToImagePIL, ToImageTensor, ToPILImage from ._deprecated import ToTensor # usort: skip from torchvision import _BETA_TRANSFORMS_WARNING, _WARN_ABOUT_BETA_TRANSFORMS if _WARN_ABOUT_BETA_TRANSFORMS: import warnings warnings.warn(_BETA_TRANSFORMS_WARNING)
import numpy as np from docarray import BaseDocument from docarray.typing import NdArray def test_set_tensor(): class MyDocument(BaseDocument): tensor: NdArray 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()
import numpy as np from docarray import Document from docarray.typing import NdArray def test_set_tensor(): class MyDocument(Document): tensor: NdArray 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()
import os from pathlib import Path from typing import List, Optional, Tuple, Union import torch import torchaudio from torch.hub import download_url_to_file from torch.utils.data import Dataset from torchaudio.datasets.utils import extract_archive _URL = "https://zenodo.org/record/3338373/files/musdb18hq.zip" _CHECKSUM = "baac80d0483c61d74b2e5f3be75fa557eec52898339e6aa45c1fa48833c5d21d" _EXT = ".wav" _SAMPLE_RATE = 44100 _VALIDATION_SET = [ "Actions - One Minute Smile", "Clara Berry And Wooldog - Waltz For My Victims", "Johnny Lokke - Promises & Lies", "Patrick Talbot - A Reason To Leave", "Triviul - Angelsaint", "Alexander Ross - Goodbye Bolero", "Fergessen - Nos Palpitants", "Leaf - Summerghost", "Skelpolu - Human Mistakes", "Young Griffo - Pennies", "ANiMAL - Rockshow", "James May - On The Line", "Meaxic - Take A Step", "Traffic Experiment - Sirens", ] class MUSDB_HQ(Dataset): """Create *MUSDB_HQ* [:footcite:`MUSDB18HQ`] 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: [``"train"``, ``"test"``]. sources (List[str] or None, optional): Sources extract data from. List can contain the following options: [``"bass"``, ``"drums"``, ``"other"``, ``"mixture"``, ``"vocals"``]. If ``None``, dataset consists of tracks except mixture. (default: ``None``) split (str or None, optional): Whether to split training set into train and validation set. If ``None``, no splitting occurs. If ``train`` or ``validation``, returns respective set. (default: ``None``) 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, sources: Optional[List[str]] = None, split: Optional[str] = None, download: bool = False, ) -> None: self.sources = ["bass", "drums", "other", "vocals"] if not sources else sources self.split = split basename = os.path.basename(_URL) archive = os.path.join(root, basename) basename = basename.rsplit(".", 2)[0] if subset not in ["test", "train"]: raise ValueError("`subset` must be one of ['test', 'train']") if self.split is not None and self.split not in ["train", "validation"]: raise ValueError("`split` must be one of ['train', 'validation']") base_path = os.path.join(root, basename) self._path = os.path.join(base_path, subset) 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) os.makedirs(base_path, exist_ok=True) extract_archive(archive, base_path) self.names = self._collect_songs() def _get_track(self, name, source): return Path(self._path) / name / f"{source}{_EXT}" def _load_sample(self, n: int) -> Tuple[torch.Tensor, int, int, str]: name = self.names[n] wavs = [] num_frames = None for source in self.sources: track = self._get_track(name, source) wav, sr = torchaudio.load(str(track)) if sr != _SAMPLE_RATE: raise ValueError(f"expected sample rate {_SAMPLE_RATE}, but got {sr}") if num_frames is None: num_frames = wav.shape[-1] else: if wav.shape[-1] != num_frames: raise ValueError("num_frames do not match across sources") wavs.append(wav) stacked = torch.stack(wavs) return stacked, _SAMPLE_RATE, num_frames, name def _collect_songs(self): if self.split == "validation": return _VALIDATION_SET path = Path(self._path) names = [] for root, folders, _ in os.walk(path, followlinks=True): root = Path(root) if root.name.startswith(".") or folders or root == path: continue name = str(root.relative_to(path)) if self.split and name in _VALIDATION_SET: continue names.append(name) return sorted(names) def __getitem__(self, n: int) -> Tuple[torch.Tensor, int, int, str]: """Load the n-th sample from the dataset. Args: n (int): The index of the sample to be loaded Returns: (Tensor, int, int, str): ``(waveforms, sample_rate, num_frames, track_name)`` """ return self._load_sample(n) def __len__(self) -> int: return len(self.names)
import os from pathlib import Path from typing import List, Optional, Tuple, Union import torch import torchaudio from torch.hub import download_url_to_file from torch.utils.data import Dataset from torchaudio.datasets.utils import extract_archive _URL = "https://zenodo.org/record/3338373/files/musdb18hq.zip" _CHECKSUM = "baac80d0483c61d74b2e5f3be75fa557eec52898339e6aa45c1fa48833c5d21d" _EXT = ".wav" _SAMPLE_RATE = 44100 _VALIDATION_SET = [ "Actions - One Minute Smile", "Clara Berry And Wooldog - Waltz For My Victims", "Johnny Lokke - Promises & Lies", "Patrick Talbot - A Reason To Leave", "Triviul - Angelsaint", "Alexander Ross - Goodbye Bolero", "Fergessen - Nos Palpitants", "Leaf - Summerghost", "Skelpolu - Human Mistakes", "Young Griffo - Pennies", "ANiMAL - Rockshow", "James May - On The Line", "Meaxic - Take A Step", "Traffic Experiment - Sirens", ] class MUSDB_HQ(Dataset): """Create *MUSDB_HQ* [:footcite:`MUSDB18HQ`] 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: [``"train"``, ``"test"``]. sources (List[str] or None, optional): Sources extract data from. List can contain the following options: [``"bass"``, ``"drums"``, ``"other"``, ``"mixture"``, ``"vocals"``]. If ``None``, dataset consists of tracks except mixture. (default: ``None``) split (str or None, optional): Whether to split training set into train and validation set. If ``None``, no splitting occurs. If ``train`` or ``validation``, returns respective set. (default: ``None``) 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, sources: Optional[List[str]] = None, split: Optional[str] = None, download: bool = False, ) -> None: self.sources = ["bass", "drums", "other", "vocals"] if not sources else sources self.split = split basename = os.path.basename(_URL) archive = os.path.join(root, basename) basename = basename.rsplit(".", 2)[0] assert subset in ["test", "train"], "`subset` must be one of ['test', 'train']" assert self.split is None or self.split in [ "train", "validation", ], "`split` must be one of ['train', 'validation']" base_path = os.path.join(root, basename) self._path = os.path.join(base_path, subset) 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) os.makedirs(base_path, exist_ok=True) extract_archive(archive, base_path) self.names = self._collect_songs() def _get_track(self, name, source): return Path(self._path) / name / f"{source}{_EXT}" def _load_sample(self, n: int) -> Tuple[torch.Tensor, int, int, str]: name = self.names[n] wavs = [] num_frames = None for source in self.sources: track = self._get_track(name, source) wav, sr = torchaudio.load(str(track)) assert sr == _SAMPLE_RATE, f"expected sample rate {_SAMPLE_RATE}, but got {sr}" if num_frames is None: num_frames = wav.shape[-1] else: assert wav.shape[-1] == num_frames, "num_frames do not match across sources" wavs.append(wav) stacked = torch.stack(wavs) return stacked, _SAMPLE_RATE, num_frames, name def _collect_songs(self): if self.split == "validation": return _VALIDATION_SET path = Path(self._path) names = [] for root, folders, _ in os.walk(path, followlinks=True): root = Path(root) if root.name.startswith(".") or folders or root == path: continue name = str(root.relative_to(path)) if self.split and name in _VALIDATION_SET: continue names.append(name) return sorted(names) def __getitem__(self, n: int) -> Tuple[torch.Tensor, int, int, str]: """Load the n-th sample from the dataset. Args: n (int): The index of the sample to be loaded Returns: (Tensor, int, int, str): ``(waveforms, sample_rate, num_frames, track_name)`` """ return self._load_sample(n) def __len__(self) -> int: return len(self.names)
# Copyright (c) OpenMMLab. All rights reserved. import torch from mmdet.registry import TASK_UTILS from mmdet.structures.bbox import bbox_overlaps, get_box_tensor def cast_tensor_type(x, scale=1., dtype=None): if dtype == 'fp16': # scale is for preventing overflows x = (x / scale).half() return x @TASK_UTILS.register_module() class BboxOverlaps2D: """2D Overlaps (e.g. IoUs, GIoUs) Calculator.""" def __init__(self, scale=1., dtype=None): self.scale = scale self.dtype = dtype def __call__(self, bboxes1, bboxes2, mode='iou', is_aligned=False): """Calculate IoU between 2D bboxes. Args: bboxes1 (Tensor or :obj:`BaseBoxes`): bboxes have shape (m, 4) in <x1, y1, x2, y2> format, or shape (m, 5) in <x1, y1, x2, y2, score> format. bboxes2 (Tensor or :obj:`BaseBoxes`): bboxes have shape (m, 4) in <x1, y1, x2, y2> format, shape (m, 5) in <x1, y1, x2, y2, score> format, or be empty. If ``is_aligned `` is ``True``, then m and n must be equal. mode (str): "iou" (intersection over union), "iof" (intersection over foreground), or "giou" (generalized intersection over union). is_aligned (bool, optional): If True, then m and n must be equal. Default False. Returns: Tensor: shape (m, n) if ``is_aligned `` is False else shape (m,) """ bboxes1 = get_box_tensor(bboxes1) bboxes2 = get_box_tensor(bboxes2) assert bboxes1.size(-1) in [0, 4, 5] assert bboxes2.size(-1) in [0, 4, 5] if bboxes2.size(-1) == 5: bboxes2 = bboxes2[..., :4] if bboxes1.size(-1) == 5: bboxes1 = bboxes1[..., :4] if self.dtype == 'fp16': # change tensor type to save cpu and cuda memory and keep speed bboxes1 = cast_tensor_type(bboxes1, self.scale, self.dtype) bboxes2 = cast_tensor_type(bboxes2, self.scale, self.dtype) overlaps = bbox_overlaps(bboxes1, bboxes2, mode, is_aligned) if not overlaps.is_cuda and overlaps.dtype == torch.float16: # resume cpu float32 overlaps = overlaps.float() return overlaps return bbox_overlaps(bboxes1, bboxes2, mode, is_aligned) def __repr__(self): """str: a string describing the module""" repr_str = self.__class__.__name__ + f'(' \ f'scale={self.scale}, dtype={self.dtype})' return repr_str @TASK_UTILS.register_module() class BboxOverlaps2D_GLIP(BboxOverlaps2D): def __call__(self, bboxes1, bboxes2, mode='iou', is_aligned=False): TO_REMOVE = 1 area1 = (bboxes1[:, 2] - bboxes1[:, 0] + TO_REMOVE) * ( bboxes1[:, 3] - bboxes1[:, 1] + TO_REMOVE) area2 = (bboxes2[:, 2] - bboxes2[:, 0] + TO_REMOVE) * ( bboxes2[:, 3] - bboxes2[:, 1] + TO_REMOVE) lt = torch.max(bboxes1[:, None, :2], bboxes2[:, :2]) # [N,M,2] rb = torch.min(bboxes1[:, None, 2:], bboxes2[:, 2:]) # [N,M,2] wh = (rb - lt + TO_REMOVE).clamp(min=0) # [N,M,2] inter = wh[:, :, 0] * wh[:, :, 1] # [N,M] iou = inter / (area1[:, None] + area2 - inter) return iou
# Copyright (c) OpenMMLab. All rights reserved. import torch from mmdet.registry import TASK_UTILS from mmdet.structures.bbox import bbox_overlaps, get_box_tensor def cast_tensor_type(x, scale=1., dtype=None): if dtype == 'fp16': # scale is for preventing overflows x = (x / scale).half() return x @TASK_UTILS.register_module() class BboxOverlaps2D: """2D Overlaps (e.g. IoUs, GIoUs) Calculator.""" def __init__(self, scale=1., dtype=None): self.scale = scale self.dtype = dtype def __call__(self, bboxes1, bboxes2, mode='iou', is_aligned=False): """Calculate IoU between 2D bboxes. Args: bboxes1 (Tensor or :obj:`BaseBoxes`): bboxes have shape (m, 4) in <x1, y1, x2, y2> format, or shape (m, 5) in <x1, y1, x2, y2, score> format. bboxes2 (Tensor or :obj:`BaseBoxes`): bboxes have shape (m, 4) in <x1, y1, x2, y2> format, shape (m, 5) in <x1, y1, x2, y2, score> format, or be empty. If ``is_aligned `` is ``True``, then m and n must be equal. mode (str): "iou" (intersection over union), "iof" (intersection over foreground), or "giou" (generalized intersection over union). is_aligned (bool, optional): If True, then m and n must be equal. Default False. Returns: Tensor: shape (m, n) if ``is_aligned `` is False else shape (m,) """ bboxes1 = get_box_tensor(bboxes1) bboxes2 = get_box_tensor(bboxes2) assert bboxes1.size(-1) in [0, 4, 5] assert bboxes2.size(-1) in [0, 4, 5] if bboxes2.size(-1) == 5: bboxes2 = bboxes2[..., :4] if bboxes1.size(-1) == 5: bboxes1 = bboxes1[..., :4] if self.dtype == 'fp16': # change tensor type to save cpu and cuda memory and keep speed bboxes1 = cast_tensor_type(bboxes1, self.scale, self.dtype) bboxes2 = cast_tensor_type(bboxes2, self.scale, self.dtype) overlaps = bbox_overlaps(bboxes1, bboxes2, mode, is_aligned) if not overlaps.is_cuda and overlaps.dtype == torch.float16: # resume cpu float32 overlaps = overlaps.float() return overlaps return bbox_overlaps(bboxes1, bboxes2, mode, is_aligned) def __repr__(self): """str: a string describing the module""" repr_str = self.__class__.__name__ + f'(' \ f'scale={self.scale}, dtype={self.dtype})' return repr_str
# Copyright (c) OpenMMLab. All rights reserved. import argparse import torch.nn.functional as F import torchvision import torchvision.transforms as transforms from torch.optim import SGD from mmengine.evaluator import BaseMetric from mmengine.model import BaseModel from mmengine.runner import Runner class MMResNet50(BaseModel): def __init__(self): super().__init__() self.resnet = torchvision.models.resnet50() def forward(self, imgs, labels, mode): x = self.resnet(imgs) if mode == 'loss': return {'loss': F.cross_entropy(x, labels)} elif mode == 'predict': return x, labels class Accuracy(BaseMetric): def process(self, data_batch, data_samples): score, gt = data_samples self.results.append({ 'batch_size': len(gt), 'correct': (score.argmax(dim=1) == gt).sum().cpu(), }) def compute_metrics(self, results): total_correct = sum(item['correct'] for item in results) total_size = sum(item['batch_size'] for item in results) return dict(accuracy=100 * total_correct / total_size) def parse_args(): parser = argparse.ArgumentParser(description='Distributed Training') parser.add_argument( '--launcher', choices=['none', 'pytorch', 'slurm', 'mpi'], default='none', help='job launcher') parser.add_argument('--local_rank', type=int, default=0) args = parser.parse_args() return args def main(): args = parse_args() norm_cfg = dict(mean=[0.491, 0.482, 0.447], std=[0.202, 0.199, 0.201]) train_set = torchvision.datasets.CIFAR10( 'data/cifar10', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(**norm_cfg) ])) valid_set = torchvision.datasets.CIFAR10( 'data/cifar10', train=False, download=True, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize(**norm_cfg)])) train_dataloader = dict( batch_size=32, dataset=train_set, sampler=dict(type='DefaultSampler', shuffle=True), collate_fn=dict(type='default_collate')) val_dataloader = dict( batch_size=32, dataset=valid_set, sampler=dict(type='DefaultSampler', shuffle=False), collate_fn=dict(type='default_collate')) runner = Runner( model=MMResNet50(), work_dir='./work_dir', train_dataloader=train_dataloader, optim_wrapper=dict(optimizer=dict(type=SGD, lr=0.001, momentum=0.9)), train_cfg=dict(by_epoch=True, max_epochs=2, val_interval=1), val_dataloader=val_dataloader, val_cfg=dict(), val_evaluator=dict(type=Accuracy), launcher=args.launcher, ) runner.train() if __name__ == '__main__': main()
# Copyright (c) OpenMMLab. All rights reserved. import argparse import torch.nn.functional as F import torchvision import torchvision.transforms as transforms from torch.optim import SGD from torch.utils.data import DataLoader from mmengine.evaluator import BaseMetric from mmengine.model import BaseModel from mmengine.runner import Runner class MMResNet50(BaseModel): def __init__(self): super().__init__() self.resnet = torchvision.models.resnet50() def forward(self, imgs, labels, mode): x = self.resnet(imgs) if mode == 'loss': return {'loss': F.cross_entropy(x, labels)} elif mode == 'predict': return x, labels class Accuracy(BaseMetric): def process(self, data_batch, data_samples): score, gt = data_samples self.results.append({ 'batch_size': len(gt), 'correct': (score.argmax(dim=1) == gt).sum().cpu(), }) def compute_metrics(self, results): total_correct = sum(item['correct'] for item in results) total_size = sum(item['batch_size'] for item in results) return dict(accuracy=100 * total_correct / total_size) def parse_args(): parser = argparse.ArgumentParser(description='Distributed Training') parser.add_argument( '--launcher', choices=['none', 'pytorch', 'slurm', 'mpi'], default='none', help='job launcher') parser.add_argument('--local_rank', type=int, default=0) args = parser.parse_args() return args def main(): args = parse_args() norm_cfg = dict(mean=[0.491, 0.482, 0.447], std=[0.202, 0.199, 0.201]) train_dataloader = DataLoader( batch_size=32, shuffle=True, dataset=torchvision.datasets.CIFAR10( 'data/cifar10', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(**norm_cfg) ]))) val_dataloader = DataLoader( batch_size=32, shuffle=False, dataset=torchvision.datasets.CIFAR10( 'data/cifar10', train=False, download=True, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize(**norm_cfg)]))) runner = Runner( model=MMResNet50(), work_dir='./work_dir', train_dataloader=train_dataloader, optim_wrapper=dict(optimizer=dict(type=SGD, lr=0.001, momentum=0.9)), train_cfg=dict(by_epoch=True, max_epochs=2, val_interval=1), val_dataloader=val_dataloader, val_cfg=dict(), val_evaluator=dict(type=Accuracy), launcher=args.launcher, ) runner.train() if __name__ == '__main__': main()
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict( type='PAA', 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_output', num_outs=5), bbox_head=dict( type='PAAHead', reg_decoded_bbox=True, score_voting=True, topk=9, num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], octave_base_scale=8, scales_per_octave=1, strides=[8, 16, 32, 64, 128]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[0.1, 0.1, 0.2, 0.2]), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='GIoULoss', loss_weight=1.3), loss_centerness=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=0.5)), # training and testing settings train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.1, neg_iou_thr=0.1, 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.6), max_per_img=100)) # optimizer optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001))
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict( type='PAA', 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_output', num_outs=5), bbox_head=dict( type='PAAHead', reg_decoded_bbox=True, score_voting=True, topk=9, num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, anchor_generator=dict( type='AnchorGenerator', ratios=[1.0], octave_base_scale=8, scales_per_octave=1, strides=[8, 16, 32, 64, 128]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[0.1, 0.1, 0.2, 0.2]), loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='GIoULoss', loss_weight=1.3), loss_centerness=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=0.5)), # training and testing settings train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.1, neg_iou_thr=0.1, 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.6), max_per_img=100)) # optimizer optimizer = dict(type='SGD', lr=0.01, momentum=0.9, weight_decay=0.0001)
__version__ = '0.16.2' import os from docarray.document import Document from docarray.array import DocumentArray from docarray.dataclasses import dataclass, field if 'DA_RICH_HANDLER' in os.environ: from rich.traceback import install install()
__version__ = '0.16.1' import os from docarray.document import Document from docarray.array import DocumentArray from docarray.dataclasses import dataclass, field if 'DA_RICH_HANDLER' in os.environ: from rich.traceback import install install()
from __future__ import annotations from typing import Any, Optional, Union import PIL.Image import torch from ._tv_tensor import TVTensor class Image(TVTensor): """:class:`torch.Tensor` subclass for images with shape ``[..., C, H, W]``. .. note:: In the :ref:`transforms <transforms>`, ``Image`` instances are largely interchangeable with pure :class:`torch.Tensor`. See :ref:`this note <passthrough_heuristic>` for more details. Args: data (tensor-like, PIL.Image.Image): Any data that can be turned into a tensor with :func:`torch.as_tensor` as well as PIL images. dtype (torch.dtype, optional): Desired data type. If omitted, will be inferred from ``data``. device (torch.device, optional): Desired device. If omitted and ``data`` is a :class:`torch.Tensor`, the device is taken from it. Otherwise, the image is constructed on the CPU. requires_grad (bool, optional): Whether autograd should record operations. If omitted and ``data`` is a :class:`torch.Tensor`, the value is taken from it. Otherwise, defaults to ``False``. """ def __new__( cls, data: Any, *, dtype: Optional[torch.dtype] = None, device: Optional[Union[torch.device, str, int]] = None, requires_grad: Optional[bool] = None, ) -> Image: if isinstance(data, PIL.Image.Image): from torchvision.transforms.v2 import functional as F data = F.pil_to_tensor(data) tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad) if tensor.ndim < 2: raise ValueError elif tensor.ndim == 2: tensor = tensor.unsqueeze(0) return tensor.as_subclass(cls) def __repr__(self, *, tensor_contents: Any = None) -> str: # type: ignore[override] return self._make_repr()
from __future__ import annotations from typing import Any, Optional, Union import PIL.Image import torch from ._tv_tensor import TVTensor class Image(TVTensor): """:class:`torch.Tensor` subclass for images. .. note:: In the :ref:`transforms <transforms>`, ``Image`` instances are largely interchangeable with pure :class:`torch.Tensor`. See :ref:`this note <passthrough_heuristic>` for more details. Args: data (tensor-like, PIL.Image.Image): Any data that can be turned into a tensor with :func:`torch.as_tensor` as well as PIL images. dtype (torch.dtype, optional): Desired data type. If omitted, will be inferred from ``data``. device (torch.device, optional): Desired device. If omitted and ``data`` is a :class:`torch.Tensor`, the device is taken from it. Otherwise, the image is constructed on the CPU. requires_grad (bool, optional): Whether autograd should record operations. If omitted and ``data`` is a :class:`torch.Tensor`, the value is taken from it. Otherwise, defaults to ``False``. """ def __new__( cls, data: Any, *, dtype: Optional[torch.dtype] = None, device: Optional[Union[torch.device, str, int]] = None, requires_grad: Optional[bool] = None, ) -> Image: if isinstance(data, PIL.Image.Image): from torchvision.transforms.v2 import functional as F data = F.pil_to_tensor(data) tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad) if tensor.ndim < 2: raise ValueError elif tensor.ndim == 2: tensor = tensor.unsqueeze(0) return tensor.as_subclass(cls) def __repr__(self, *, tensor_contents: Any = None) -> str: # type: ignore[override] return self._make_repr()
_base_ = '../_base_/default_runtime.py' # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' image_size = (1024, 1024) file_client_args = dict(backend='disk') # comment out the code below to use different file client # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='RandomResize', scale=image_size, ratio_range=(0.1, 2.0), keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, recompute_bbox=True, allow_negative_crop=True), dict(type='FilterAnnotations', min_gt_bbox_wh=(1e-2, 1e-2)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] # Use RepeatDataset to speed up training train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), dataset=dict( type='RepeatDataset', times=4, # simply change this from 2 to 16 for 50e - 400e training. dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_train2017.json', data_prefix=dict(img='train2017/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline))) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_val2017.json', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=test_pipeline)) test_dataloader = val_dataloader val_evaluator = dict( type='CocoMetric', ann_file=data_root + 'annotations/instances_val2017.json', metric=['bbox', 'segm'], format_only=False) test_evaluator = val_evaluator max_epochs = 25 train_cfg = dict( type='EpochBasedTrainLoop', max_epochs=max_epochs, val_interval=5) val_cfg = dict(type='ValLoop') test_cfg = dict(type='TestLoop') # optimizer assumes bs=64 optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.1, momentum=0.9, weight_decay=0.00004)) # learning rate param_scheduler = [ dict( type='LinearLR', start_factor=0.067, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[22, 24], gamma=0.1) ] # only keep latest 2 checkpoints default_hooks = dict(checkpoint=dict(max_keep_ckpts=2)) # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (32 GPUs) x (2 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = '../_base_/default_runtime.py' # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' image_size = (1024, 1024) file_client_args = dict(backend='disk') # comment out the code below to use different file client # file_client_args = dict( # backend='petrel', # path_mapping=dict({ # './data/': 's3://openmmlab/datasets/detection/', # 'data/': 's3://openmmlab/datasets/detection/' # })) train_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='RandomResize', scale=image_size, ratio_range=(0.1, 2.0), keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, recompute_bbox=True, allow_negative_crop=True), dict(type='FilterAnnotations', min_gt_bbox_wh=(1e-2, 1e-2)), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict(type='LoadImageFromFile', file_client_args=file_client_args), dict(type='Resize', scale=(1333, 800), keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] # Use RepeatDataset to speed up training train_dataloader = dict( batch_size=2, num_workers=2, persistent_workers=True, sampler=dict(type='DefaultSampler', shuffle=True), dataset=dict( type='RepeatDataset', times=4, # simply change this from 2 to 16 for 50e - 400e training. dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_train2017.json', data_prefix=dict(img='train2017/'), filter_cfg=dict(filter_empty_gt=True, min_size=32), pipeline=train_pipeline))) val_dataloader = dict( batch_size=1, num_workers=2, persistent_workers=True, drop_last=False, sampler=dict(type='DefaultSampler', shuffle=False), dataset=dict( type=dataset_type, data_root=data_root, ann_file='annotations/instances_val2017.json', data_prefix=dict(img='val2017/'), test_mode=True, pipeline=test_pipeline)) test_dataloader = val_dataloader val_evaluator = dict( type='CocoMetric', ann_file=data_root + 'annotations/instances_val2017.json', metric=['bbox', 'segm'], format_only=False) test_evaluator = val_evaluator max_epochs = 25 train_cfg = dict( type='EpochBasedTrainLoop', max_epochs=max_epochs, val_interval=5) val_cfg = dict(type='ValLoop') test_cfg = dict(type='TestLoop') # optimizer assumes bs=64 optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.1, momentum=0.9, weight_decay=0.00004)) # learning rate param_scheduler = [ dict( type='LinearLR', start_factor=0.067, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[22, 24], gamma=0.1) ] # only keep latest 2 checkpoints default_hooks = dict(checkpoint=dict(max_keep_ckpts=2)) # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (32 GPUs) x (2 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/default_runtime.py' ] model = dict( type='SingleStageDetector', backbone=dict( type='MobileNetV2', out_indices=(4, 7), norm_cfg=dict(type='BN', eps=0.001, momentum=0.03), init_cfg=dict(type='TruncNormal', layer='Conv2d', std=0.03)), neck=dict( type='SSDNeck', in_channels=(96, 1280), out_channels=(96, 1280, 512, 256, 256, 128), level_strides=(2, 2, 2, 2), level_paddings=(1, 1, 1, 1), l2_norm_scale=None, use_depthwise=True, norm_cfg=dict(type='BN', eps=0.001, momentum=0.03), act_cfg=dict(type='ReLU6'), init_cfg=dict(type='TruncNormal', layer='Conv2d', std=0.03)), bbox_head=dict( type='SSDHead', in_channels=(96, 1280, 512, 256, 256, 128), num_classes=80, use_depthwise=True, norm_cfg=dict(type='BN', eps=0.001, momentum=0.03), act_cfg=dict(type='ReLU6'), init_cfg=dict(type='Normal', layer='Conv2d', std=0.001), # set anchor size manually instead of using the predefined # SSD300 setting. anchor_generator=dict( type='SSDAnchorGenerator', scale_major=False, strides=[16, 32, 64, 107, 160, 320], ratios=[[2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3]], min_sizes=[48, 100, 150, 202, 253, 304], max_sizes=[100, 150, 202, 253, 304, 320]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[0.1, 0.1, 0.2, 0.2])), # model training and testing settings train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0., ignore_iof_thr=-1, gt_max_assign_all=False), smoothl1_beta=1., allowed_border=-1, pos_weight=-1, neg_pos_ratio=3, debug=False), test_cfg=dict( nms_pre=1000, nms=dict(type='nms', iou_threshold=0.45), min_bbox_size=0, score_thr=0.02, max_per_img=200)) cudnn_benchmark = True # dataset settings 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), dict( type='Expand', mean=img_norm_cfg['mean'], to_rgb=img_norm_cfg['to_rgb'], ratio_range=(1, 4)), dict( type='MinIoURandomCrop', min_ious=(0.1, 0.3, 0.5, 0.7, 0.9), min_crop_size=0.3), dict(type='Resize', img_scale=(320, 320), keep_ratio=False), dict(type='RandomFlip', flip_ratio=0.5), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=320), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(320, 320), flip=False, transforms=[ dict(type='Resize', keep_ratio=False), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=320), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=24, workers_per_gpu=4, train=dict( _delete_=True, type='RepeatDataset', # use RepeatDataset to speed up training times=5, dataset=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_train2017.json', img_prefix=data_root + 'train2017/', pipeline=train_pipeline)), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # optimizer optimizer = dict(type='SGD', lr=0.015, momentum=0.9, weight_decay=4.0e-5) optimizer_config = dict(grad_clip=None) # learning policy lr_config = dict( policy='CosineAnnealing', warmup='linear', warmup_iters=500, warmup_ratio=0.001, min_lr=0) runner = dict(type='EpochBasedRunner', max_epochs=120) # Avoid evaluation and saving weights too frequently evaluation = dict(interval=5, metric='bbox') checkpoint_config = dict(interval=5) custom_hooks = [ dict(type='NumClassCheckHook'), dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW') ]
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/default_runtime.py' ] model = dict( type='SingleStageDetector', backbone=dict( type='MobileNetV2', out_indices=(4, 7), norm_cfg=dict(type='BN', eps=0.001, momentum=0.03), init_cfg=dict(type='TruncNormal', layer='Conv2d', std=0.03)), neck=dict( type='SSDNeck', in_channels=(96, 1280), out_channels=(96, 1280, 512, 256, 256, 128), level_strides=(2, 2, 2, 2), level_paddings=(1, 1, 1, 1), l2_norm_scale=None, use_depthwise=True, norm_cfg=dict(type='BN', eps=0.001, momentum=0.03), act_cfg=dict(type='ReLU6'), init_cfg=dict(type='TruncNormal', layer='Conv2d', std=0.03)), bbox_head=dict( type='SSDHead', in_channels=(96, 1280, 512, 256, 256, 128), num_classes=80, use_depthwise=True, norm_cfg=dict(type='BN', eps=0.001, momentum=0.03), act_cfg=dict(type='ReLU6'), init_cfg=dict(type='Normal', layer='Conv2d', std=0.001), # set anchor size manually instead of using the predefined # SSD300 setting. anchor_generator=dict( type='SSDAnchorGenerator', scale_major=False, strides=[16, 32, 64, 107, 160, 320], ratios=[[2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3]], min_sizes=[48, 100, 150, 202, 253, 304], max_sizes=[100, 150, 202, 253, 304, 320]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[0.1, 0.1, 0.2, 0.2])), # model training and testing settings train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0., ignore_iof_thr=-1, gt_max_assign_all=False), smoothl1_beta=1., allowed_border=-1, pos_weight=-1, neg_pos_ratio=3, debug=False), test_cfg=dict( nms_pre=1000, nms=dict(type='nms', iou_threshold=0.45), min_bbox_size=0, score_thr=0.02, max_per_img=200)) cudnn_benchmark = True # dataset settings 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', to_float32=True), dict(type='LoadAnnotations', with_bbox=True), dict( type='PhotoMetricDistortion', brightness_delta=32, contrast_range=(0.5, 1.5), saturation_range=(0.5, 1.5), hue_delta=18), dict( type='Expand', mean=img_norm_cfg['mean'], to_rgb=img_norm_cfg['to_rgb'], ratio_range=(1, 4)), dict( type='MinIoURandomCrop', min_ious=(0.1, 0.3, 0.5, 0.7, 0.9), min_crop_size=0.3), dict(type='Resize', img_scale=(320, 320), keep_ratio=False), dict(type='Normalize', **img_norm_cfg), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Pad', size_divisor=320), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(320, 320), flip=False, transforms=[ dict(type='Resize', keep_ratio=False), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=320), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=24, workers_per_gpu=4, train=dict( _delete_=True, type='RepeatDataset', # use RepeatDataset to speed up training times=5, dataset=dict( type=dataset_type, ann_file=data_root + 'annotations/instances_train2017.json', img_prefix=data_root + 'train2017/', pipeline=train_pipeline)), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # optimizer optimizer = dict(type='SGD', lr=0.015, momentum=0.9, weight_decay=4.0e-5) optimizer_config = dict(grad_clip=None) # learning policy lr_config = dict( policy='CosineAnnealing', warmup='linear', warmup_iters=500, warmup_ratio=0.001, min_lr=0) runner = dict(type='EpochBasedRunner', max_epochs=120) # Avoid evaluation and saving weights too frequently evaluation = dict(interval=5, metric='bbox') checkpoint_config = dict(interval=5) custom_hooks = [ dict(type='NumClassCheckHook'), dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW') ]
"""**Text Splitters** are classes for splitting text. **Class hierarchy:** .. code-block:: BaseDocumentTransformer --> TextSplitter --> <name>TextSplitter # Example: CharacterTextSplitter RecursiveCharacterTextSplitter --> <name>TextSplitter Note: **MarkdownHeaderTextSplitter** and **HTMLHeaderTextSplitter do not derive from TextSplitter. **Main helpers:** .. code-block:: Document, Tokenizer, Language, LineType, HeaderType """ # noqa: E501 from langchain_text_splitters.base import ( Language, TextSplitter, Tokenizer, TokenTextSplitter, split_text_on_tokens, ) from langchain_text_splitters.character import ( CharacterTextSplitter, RecursiveCharacterTextSplitter, ) from langchain_text_splitters.html import ( ElementType, HTMLHeaderTextSplitter, HTMLSectionSplitter, HTMLSemanticPreservingSplitter, ) from langchain_text_splitters.json import RecursiveJsonSplitter from langchain_text_splitters.jsx import JSFrameworkTextSplitter from langchain_text_splitters.konlpy import KonlpyTextSplitter from langchain_text_splitters.latex import LatexTextSplitter from langchain_text_splitters.markdown import ( ExperimentalMarkdownSyntaxTextSplitter, HeaderType, LineType, MarkdownHeaderTextSplitter, MarkdownTextSplitter, ) from langchain_text_splitters.nltk import NLTKTextSplitter from langchain_text_splitters.python import PythonCodeTextSplitter from langchain_text_splitters.sentence_transformers import ( SentenceTransformersTokenTextSplitter, ) from langchain_text_splitters.spacy import SpacyTextSplitter __all__ = [ "CharacterTextSplitter", "ElementType", "ExperimentalMarkdownSyntaxTextSplitter", "HTMLHeaderTextSplitter", "HTMLSectionSplitter", "HTMLSemanticPreservingSplitter", "HeaderType", "JSFrameworkTextSplitter", "KonlpyTextSplitter", "Language", "LatexTextSplitter", "LineType", "MarkdownHeaderTextSplitter", "MarkdownTextSplitter", "NLTKTextSplitter", "PythonCodeTextSplitter", "RecursiveCharacterTextSplitter", "RecursiveJsonSplitter", "SentenceTransformersTokenTextSplitter", "SpacyTextSplitter", "TextSplitter", "TokenTextSplitter", "Tokenizer", "split_text_on_tokens", ]
"""**Text Splitters** are classes for splitting text. **Class hierarchy:** .. code-block:: BaseDocumentTransformer --> TextSplitter --> <name>TextSplitter # Example: CharacterTextSplitter RecursiveCharacterTextSplitter --> <name>TextSplitter Note: **MarkdownHeaderTextSplitter** and **HTMLHeaderTextSplitter do not derive from TextSplitter. **Main helpers:** .. code-block:: Document, Tokenizer, Language, LineType, HeaderType """ # noqa: E501 from langchain_text_splitters.base import ( Language, TextSplitter, Tokenizer, TokenTextSplitter, split_text_on_tokens, ) from langchain_text_splitters.character import ( CharacterTextSplitter, RecursiveCharacterTextSplitter, ) from langchain_text_splitters.html import ( ElementType, HTMLHeaderTextSplitter, HTMLSectionSplitter, HTMLSemanticPreservingSplitter, ) from langchain_text_splitters.json import RecursiveJsonSplitter from langchain_text_splitters.jsx import JSFrameworkTextSplitter from langchain_text_splitters.konlpy import KonlpyTextSplitter from langchain_text_splitters.latex import LatexTextSplitter from langchain_text_splitters.markdown import ( ExperimentalMarkdownSyntaxTextSplitter, HeaderType, LineType, MarkdownHeaderTextSplitter, MarkdownTextSplitter, ) from langchain_text_splitters.nltk import NLTKTextSplitter from langchain_text_splitters.python import PythonCodeTextSplitter from langchain_text_splitters.sentence_transformers import ( SentenceTransformersTokenTextSplitter, ) from langchain_text_splitters.spacy import SpacyTextSplitter __all__ = [ "TokenTextSplitter", "TextSplitter", "Tokenizer", "Language", "RecursiveCharacterTextSplitter", "RecursiveJsonSplitter", "LatexTextSplitter", "JSFrameworkTextSplitter", "PythonCodeTextSplitter", "KonlpyTextSplitter", "SpacyTextSplitter", "NLTKTextSplitter", "split_text_on_tokens", "SentenceTransformersTokenTextSplitter", "ElementType", "HeaderType", "LineType", "HTMLHeaderTextSplitter", "HTMLSectionSplitter", "HTMLSemanticPreservingSplitter", "MarkdownHeaderTextSplitter", "MarkdownTextSplitter", "CharacterTextSplitter", "ExperimentalMarkdownSyntaxTextSplitter", ]
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.regularizers import deserialize as deserialize from keras.src.regularizers import get as get from keras.src.regularizers import serialize as serialize from keras.src.regularizers.regularizers import L1 as L1 from keras.src.regularizers.regularizers import L1 as l1 from keras.src.regularizers.regularizers import L1L2 as L1L2 from keras.src.regularizers.regularizers import L1L2 as l1_l2 from keras.src.regularizers.regularizers import L2 as L2 from keras.src.regularizers.regularizers import L2 as l2 from keras.src.regularizers.regularizers import ( OrthogonalRegularizer as OrthogonalRegularizer, ) from keras.src.regularizers.regularizers import ( OrthogonalRegularizer as orthogonal_regularizer, ) from keras.src.regularizers.regularizers import Regularizer as Regularizer
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.regularizers import deserialize from keras.src.regularizers import get from keras.src.regularizers import serialize from keras.src.regularizers.regularizers import L1 from keras.src.regularizers.regularizers import L1 as l1 from keras.src.regularizers.regularizers import L1L2 from keras.src.regularizers.regularizers import L1L2 as l1_l2 from keras.src.regularizers.regularizers import L2 from keras.src.regularizers.regularizers import L2 as l2 from keras.src.regularizers.regularizers import OrthogonalRegularizer from keras.src.regularizers.regularizers import ( OrthogonalRegularizer as orthogonal_regularizer, ) from keras.src.regularizers.regularizers import Regularizer
from PIL import Image from sentence_transformers import SentenceTransformer, models, util ########### image = Image.open("two_dogs_in_snow.jpg") from transformers import CLIPModel, CLIPProcessor model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") inputs = processor(text=["a cat", "a dog"], images=[image], return_tensors="pt", padding=True) output = model(**inputs) # vision_outputs = model.vision_model(pixel_values=inputs['pixel_values']) # image_embeds = model.visual_projection(vision_outputs[1]) # print(image_embeds.shape) # exit() # Load CLIP model clip = models.CLIPModel() model = SentenceTransformer(modules=[clip]) model.save("tmp-clip-model") model = SentenceTransformer("tmp-clip-model") # Encode an image: img_emb = model.encode(Image.open("two_dogs_in_snow.jpg")) # Encode text descriptions text_emb = model.encode(["Two dogs in the snow", "A cat on a table", "A picture of London at night"]) # Compute cosine similarities cos_scores = util.cos_sim(img_emb, text_emb) print(cos_scores)
from PIL import Image from sentence_transformers import SentenceTransformer, models, util ########### image = Image.open("two_dogs_in_snow.jpg") from transformers import CLIPModel, CLIPProcessor model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32") processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32") inputs = processor(texts=["a cat", "a dog"], images=[image], return_tensors="pt", padding=True) output = model(**inputs) # vision_outputs = model.vision_model(pixel_values=inputs['pixel_values']) # image_embeds = model.visual_projection(vision_outputs[1]) # print(image_embeds.shape) # exit() # Load CLIP model clip = models.CLIPModel() model = SentenceTransformer(modules=[clip]) model.save("tmp-clip-model") model = SentenceTransformer("tmp-clip-model") # Encode an image: img_emb = model.encode(Image.open("two_dogs_in_snow.jpg")) # Encode text descriptions text_emb = model.encode(["Two dogs in the snow", "A cat on a table", "A picture of London at night"]) # Compute cosine similarities cos_scores = util.cos_sim(img_emb, text_emb) print(cos_scores)
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.datasets.california_housing import load_data as load_data
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.datasets.california_housing import load_data
from __future__ import annotations from collections.abc import Iterable from torch import Tensor from sentence_transformers.losses.TripletLoss import TripletDistanceMetric, TripletLoss from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class SparseTripletLoss(TripletLoss): def __init__( self, model: SparseEncoder, distance_metric=TripletDistanceMetric.EUCLIDEAN, triplet_margin: float = 5 ) -> None: """ This class implements triplet loss. Given a triplet of (anchor, positive, negative), the loss minimizes the distance between anchor and positive while it maximizes the distance between anchor and negative. It compute the following loss function: ``loss = max(||anchor - positive|| - ||anchor - negative|| + margin, 0)``. Margin is an important hyperparameter and needs to be tuned respectively. Args: model: SparseEncoder distance_metric: Function to compute distance between two embeddings. The class TripletDistanceMetric contains common distance metrices that can be used. triplet_margin: The negative should be at least this much further away from the anchor than the positive. References: - For further details, see: https://en.wikipedia.org/wiki/Triplet_loss Requirements: 1. Need to be used in SpladeLoss or CSRLoss as a loss function. 2. (anchor, positive, negative) triplets Inputs: +---------------------------------------+--------+ | Texts | Labels | +=======================================+========+ | (anchor, positive, negative) triplets | none | +---------------------------------------+--------+ Example: :: from datasets import Dataset from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses model = SparseEncoder("distilbert/distilbert-base-uncased") train_dataset = Dataset.from_dict( { "anchor": ["It's nice weather outside today.", "He drove to work."], "positive": ["It's so sunny.", "He took the car to the office."], "negative": ["It's quite rainy, sadly.", "She walked to the store."], } ) loss = losses.SpladeLoss(model=model, loss=losses.SparseTripletLoss(model), lambda_corpus=3e-5, lambda_query=5e-5) trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss) trainer.train() """ super().__init__(model, distance_metric=distance_metric, triplet_margin=triplet_margin) def forward(self, sentence_features: Iterable[dict[str, Tensor]], labels: Tensor) -> Tensor: raise AttributeError("SparseTripletLoss shold not be used alone. Use it with SpladeLoss or CSRLoss.")
from __future__ import annotations from sentence_transformers.losses.TripletLoss import TripletDistanceMetric, TripletLoss from sentence_transformers.sparse_encoder.SparseEncoder import SparseEncoder class SparseTripletLoss(TripletLoss): def __init__( self, model: SparseEncoder, distance_metric=TripletDistanceMetric.EUCLIDEAN, triplet_margin: float = 5 ) -> None: """ This class implements triplet loss. Given a triplet of (anchor, positive, negative), the loss minimizes the distance between anchor and positive while it maximizes the distance between anchor and negative. It compute the following loss function: ``loss = max(||anchor - positive|| - ||anchor - negative|| + margin, 0)``. Margin is an important hyperparameter and needs to be tuned respectively. Args: model: SparseEncoder distance_metric: Function to compute distance between two embeddings. The class TripletDistanceMetric contains common distance metrices that can be used. triplet_margin: The negative should be at least this much further away from the anchor than the positive. References: - For further details, see: https://en.wikipedia.org/wiki/Triplet_loss Requirements: 1. (anchor, positive, negative) triplets Inputs: +---------------------------------------+--------+ | Texts | Labels | +=======================================+========+ | (anchor, positive, negative) triplets | none | +---------------------------------------+--------+ Example: :: from datasets import Dataset from sentence_transformers.sparse_encoder import SparseEncoder, SparseEncoderTrainer, losses model = SparseEncoder("distilbert/distilbert-base-uncased") train_dataset = Dataset.from_dict( { "anchor": ["It's nice weather outside today.", "He drove to work."], "positive": ["It's so sunny.", "He took the car to the office."], "negative": ["It's quite rainy, sadly.", "She walked to the store."], } ) loss = losses.SpladeLoss(model=model, loss=losses.SparseTripletLoss(model), lambda_corpus=3e-5, lambda_query=5e-5) trainer = SparseEncoderTrainer(model=model, train_dataset=train_dataset, loss=loss) trainer.train() """ super().__init__(model, distance_metric=distance_metric, triplet_margin=triplet_margin)
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from parameterized import parameterized from mmdet.registry import MODELS from mmdet.testing import demo_mm_inputs, demo_mm_proposals, get_roi_head_cfg from mmdet.utils import register_all_modules class TestGridRoIHead(TestCase): def setUp(self): register_all_modules() self.roi_head_cfg = get_roi_head_cfg( 'grid_rcnn/grid-rcnn_r50_fpn_gn-head_2x_coco.py') def test_init(self): roi_head = MODELS.build(self.roi_head_cfg) self.assertTrue(roi_head.with_bbox) @parameterized.expand(['cpu', 'cuda']) def test_grid_roi_head_loss(self, device): """Tests trident roi head predict.""" if device == 'cuda': if not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[1], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) loss_cls = out['loss_cls'] loss_grid = out['loss_grid'] self.assertGreater(loss_cls.sum(), 0, 'cls loss should be non-zero') self.assertGreater(loss_grid.sum(), 0, 'grid loss should be non-zero') batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[0], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) empty_cls_loss = out['loss_cls'] self.assertGreater(empty_cls_loss.sum(), 0, 'cls loss should be non-zero') self.assertNotIn( 'loss_grid', out, 'grid loss should be passed when there are no true boxes') @parameterized.expand(['cpu', 'cuda']) def test_grid_roi_head_predict(self, device): """Tests trident roi head predict.""" if device == 'cuda': if not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[0], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) roi_head.predict(feats, proposals_list, batch_data_samples) @parameterized.expand(['cpu', 'cuda']) def test_grid_roi_head_forward(self, device): """Tests trident roi head forward.""" if device == 'cuda': if not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) roi_head.forward(feats, proposals_list)
# Copyright (c) OpenMMLab. All rights reserved. import unittest from unittest import TestCase import torch from parameterized import parameterized from mmdet.registry import MODELS from mmdet.testing import demo_mm_inputs, demo_mm_proposals, get_roi_head_cfg from mmdet.utils import register_all_modules class TestGridRoIHead(TestCase): def setUp(self): register_all_modules() self.roi_head_cfg = get_roi_head_cfg( 'grid_rcnn/grid_rcnn_r50_fpn_gn-head_2x_coco.py') def test_init(self): roi_head = MODELS.build(self.roi_head_cfg) self.assertTrue(roi_head.with_bbox) @parameterized.expand(['cpu', 'cuda']) def test_grid_roi_head_loss(self, device): """Tests trident roi head predict.""" if device == 'cuda': if not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[1], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) loss_cls = out['loss_cls'] loss_grid = out['loss_grid'] self.assertGreater(loss_cls.sum(), 0, 'cls loss should be non-zero') self.assertGreater(loss_grid.sum(), 0, 'grid loss should be non-zero') batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[0], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) out = roi_head.loss(feats, proposals_list, batch_data_samples) empty_cls_loss = out['loss_cls'] self.assertGreater(empty_cls_loss.sum(), 0, 'cls loss should be non-zero') self.assertNotIn( 'loss_grid', out, 'grid loss should be passed when there are no true boxes') @parameterized.expand(['cpu', 'cuda']) def test_grid_roi_head_predict(self, device): """Tests trident roi head predict.""" if device == 'cuda': if not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] batch_data_samples = demo_mm_inputs( batch_size=1, image_shapes=image_shapes, num_items=[0], num_classes=4, with_mask=True, device=device)['data_samples'] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) roi_head.predict(feats, proposals_list, batch_data_samples) @parameterized.expand(['cpu', 'cuda']) def test_grid_roi_head_forward(self, device): """Tests trident roi head forward.""" if device == 'cuda': if not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') roi_head = MODELS.build(self.roi_head_cfg) roi_head = roi_head.to(device=device) s = 256 feats = [] for i in range(len(roi_head.bbox_roi_extractor.featmap_strides)): feats.append( torch.rand(1, 256, s // (2**(i + 2)), s // (2**(i + 2))).to(device=device)) image_shapes = [(3, s, s)] proposals_list = demo_mm_proposals( image_shapes=image_shapes, num_proposals=100, device=device) roi_head.forward(feats, proposals_list)
from docarray.typing.bytes import ImageBytes from docarray.typing.id import ID from docarray.typing.tensor import ImageNdArray, ImageTensor from docarray.typing.tensor.audio import AudioNdArray from docarray.typing.tensor.embedding.embedding import AnyEmbedding, NdArrayEmbedding from docarray.typing.tensor.ndarray import NdArray from docarray.typing.tensor.tensor import AnyTensor from docarray.typing.tensor.video import VideoNdArray from docarray.typing.url import ( AnyUrl, AudioUrl, ImageUrl, Mesh3DUrl, PointCloud3DUrl, TextUrl, VideoUrl, ) __all__ = [ 'NdArray', 'NdArrayEmbedding', 'AudioNdArray', 'VideoNdArray', 'AnyEmbedding', 'ImageUrl', 'AudioUrl', 'TextUrl', 'Mesh3DUrl', 'PointCloud3DUrl', 'VideoUrl', 'AnyUrl', 'ID', 'AnyTensor', 'NdArrayEmbedding', 'ImageBytes', 'ImageTensor', 'ImageNdArray', ] from docarray.utils._internal.misc import is_tf_available, is_torch_available torch_available = is_torch_available() if torch_available: from docarray.typing.tensor import TorchEmbedding, TorchTensor # noqa: F401 from docarray.typing.tensor.audio.audio_torch_tensor import AudioTorchTensor # noqa from docarray.typing.tensor.image import ImageTorchTensor # noqa: F401 from docarray.typing.tensor.video.video_torch_tensor import VideoTorchTensor # noqa __all__.extend( [ 'AudioTorchTensor', 'TorchEmbedding', 'TorchTensor', 'VideoTorchTensor', 'ImageTorchTensor', ] ) tf_available = is_tf_available() if tf_available: from docarray.typing.tensor import TensorFlowTensor # noqa: F401 from docarray.typing.tensor.audio import AudioTensorFlowTensor # noqa: F401 from docarray.typing.tensor.embedding import TensorFlowEmbedding # noqa: F401 from docarray.typing.tensor.image import ImageTensorFlowTensor # noqa: F401 from docarray.typing.tensor.video import VideoTensorFlowTensor # noqa __all__.extend( [ 'TensorFlowTensor', 'TensorFlowEmbedding', 'AudioTensorFlowTensor', 'ImageTensorFlowTensor', 'VideoTensorFlowTensor', ] )
from docarray.typing.bytes import ImageBytes from docarray.typing.id import ID from docarray.typing.tensor import ImageNdArray, ImageTensor from docarray.typing.tensor.audio import AudioNdArray from docarray.typing.tensor.embedding.embedding import AnyEmbedding, NdArrayEmbedding from docarray.typing.tensor.ndarray import NdArray from docarray.typing.tensor.tensor import AnyTensor from docarray.typing.tensor.video import VideoNdArray from docarray.typing.url import ( AnyUrl, AudioUrl, ImageUrl, Mesh3DUrl, PointCloud3DUrl, TextUrl, VideoUrl, ) __all__ = [ 'NdArray', 'NdArrayEmbedding', 'AudioNdArray', 'VideoNdArray', 'AnyEmbedding', 'ImageUrl', 'AudioUrl', 'TextUrl', 'Mesh3DUrl', 'PointCloud3DUrl', 'VideoUrl', 'AnyUrl', 'ID', 'AnyTensor', 'TensorFlowTensor', 'NdArrayEmbedding', 'ImageBytes', 'ImageTensor', 'ImageNdArray', ] from docarray.utils.misc import is_tf_available, is_torch_available torch_available = is_torch_available() if torch_available: from docarray.typing.tensor import TorchEmbedding, TorchTensor # noqa: F401 from docarray.typing.tensor.audio.audio_torch_tensor import AudioTorchTensor # noqa from docarray.typing.tensor.image import ImageTorchTensor # noqa: F401 from docarray.typing.tensor.video.video_torch_tensor import VideoTorchTensor # noqa __all__.extend( [ 'AudioTorchTensor', 'TorchEmbedding', 'TorchTensor', 'VideoTorchTensor', 'ImageTorchTensor', ] ) tf_available = is_tf_available() if tf_available: from docarray.typing.tensor import TensorFlowTensor from docarray.typing.tensor.audio import AudioTensorFlowTensor # noqa: F401 from docarray.typing.tensor.embedding import TensorFlowEmbedding # noqa: F401 from docarray.typing.tensor.image import ImageTensorFlowTensor # noqa: F401 from docarray.typing.tensor.video import VideoTensorFlowTensor # noqa __all__.extend( [ 'TensorFlowTensor', 'TensorFlowEmbedding', 'AudioTensorFlowTensor', 'ImageTensorFlowTensor', 'VideoTensorFlowTensor', ] )
from langchain_core.output_parsers.json import ( SimpleJsonOutputParser, ) from langchain_core.utils.json import ( parse_and_check_json_markdown, parse_json_markdown, parse_partial_json, ) __all__ = [ "SimpleJsonOutputParser", "parse_and_check_json_markdown", "parse_json_markdown", "parse_partial_json", ]
from langchain_core.output_parsers.json import ( SimpleJsonOutputParser, ) from langchain_core.utils.json import ( parse_and_check_json_markdown, parse_json_markdown, parse_partial_json, ) __all__ = [ "SimpleJsonOutputParser", "parse_partial_json", "parse_json_markdown", "parse_and_check_json_markdown", ]
from typing import TYPE_CHECKING from ...utils import ( DIFFUSERS_SLOW_IMPORT, OptionalDependencyNotAvailable, _LazyModule, get_objects_from_module, is_torch_available, is_transformers_available, ) _dummy_objects = {} _additional_imports = {} _import_structure = {"pipeline_output": ["ChromaPipelineOutput"]} try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils import dummy_torch_and_transformers_objects # noqa F403 _dummy_objects.update(get_objects_from_module(dummy_torch_and_transformers_objects)) else: _import_structure["pipeline_chroma"] = ["ChromaPipeline"] _import_structure["pipeline_chroma_img2img"] = ["ChromaImg2ImgPipeline"] if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT: try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_chroma import ChromaPipeline from .pipeline_chroma_img2img import ChromaImg2ImgPipeline else: import sys sys.modules[__name__] = _LazyModule( __name__, globals()["__file__"], _import_structure, module_spec=__spec__, ) for name, value in _dummy_objects.items(): setattr(sys.modules[__name__], name, value) for name, value in _additional_imports.items(): setattr(sys.modules[__name__], name, value)
from typing import TYPE_CHECKING from ...utils import ( DIFFUSERS_SLOW_IMPORT, OptionalDependencyNotAvailable, _LazyModule, get_objects_from_module, is_torch_available, is_transformers_available, ) _dummy_objects = {} _additional_imports = {} _import_structure = {"pipeline_output": ["ChromaPipelineOutput"]} try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils import dummy_torch_and_transformers_objects # noqa F403 _dummy_objects.update(get_objects_from_module(dummy_torch_and_transformers_objects)) else: _import_structure["pipeline_chroma"] = ["ChromaPipeline"] if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT: try: if not (is_transformers_available() and is_torch_available()): raise OptionalDependencyNotAvailable() except OptionalDependencyNotAvailable: from ...utils.dummy_torch_and_transformers_objects import * # noqa F403 else: from .pipeline_chroma import ChromaPipeline else: import sys sys.modules[__name__] = _LazyModule( __name__, globals()["__file__"], _import_structure, module_spec=__spec__, ) for name, value in _dummy_objects.items(): setattr(sys.modules[__name__], name, value) for name, value in _additional_imports.items(): setattr(sys.modules[__name__], name, value)
""" Mbox parser. Contains simple parser for mbox files. """ import logging from pathlib import Path from typing import Any, Dict, List, Optional from fsspec import AbstractFileSystem from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document logger = logging.getLogger(__name__) class MboxReader(BaseReader): """ Mbox parser. Extract messages from mailbox files. Returns string including date, subject, sender, receiver and content for each message. """ DEFAULT_MESSAGE_FORMAT: str = ( "Date: {_date}\n" "From: {_from}\n" "To: {_to}\n" "Subject: {_subject}\n" "Content: {_content}" ) def __init__( self, *args: Any, max_count: int = 0, message_format: str = DEFAULT_MESSAGE_FORMAT, **kwargs: Any, ) -> None: """Init params.""" try: from bs4 import BeautifulSoup # noqa except ImportError: raise ImportError( "`beautifulsoup4` package not found: `pip install beautifulsoup4`" ) super().__init__(*args, **kwargs) self.max_count = max_count self.message_format = message_format def load_data( self, file: Path, extra_info: Optional[Dict] = None, fs: Optional[AbstractFileSystem] = None, ) -> List[Document]: """Parse file into string.""" # Import required libraries import mailbox from email.parser import BytesParser from email.policy import default from bs4 import BeautifulSoup if fs: logger.warning( "fs was specified but MboxReader doesn't support loading " "from fsspec filesystems. Will load from local filesystem instead." ) i = 0 results: List[str] = [] # Load file using mailbox bytes_parser = BytesParser(policy=default).parse mbox = mailbox.mbox(file, factory=bytes_parser) # type: ignore # Iterate through all messages for _, _msg in enumerate(mbox): try: msg: mailbox.mboxMessage = _msg # Parse multipart messages if msg.is_multipart(): for part in msg.walk(): ctype = part.get_content_type() cdispo = str(part.get("Content-Disposition")) if ctype == "text/plain" and "attachment" not in cdispo: content = part.get_payload(decode=True) # decode break # Get plain message payload for non-multipart messages else: content = msg.get_payload(decode=True) # Parse message HTML content and remove unneeded whitespace soup = BeautifulSoup(content) stripped_content = " ".join(soup.get_text().split()) # Format message to include date, sender, receiver and subject msg_string = self.message_format.format( _date=msg["date"], _from=msg["from"], _to=msg["to"], _subject=msg["subject"], _content=stripped_content, ) # Add message string to results results.append(msg_string) except Exception as e: logger.warning(f"Failed to parse message:\n{_msg}\n with exception {e}") # Increment counter and return if max count is met i += 1 if self.max_count > 0 and i >= self.max_count: break return [Document(text=result, metadata=extra_info or {}) for result in results]
"""Mbox parser. Contains simple parser for mbox files. """ import logging from pathlib import Path from typing import Any, Dict, List, Optional from fsspec import AbstractFileSystem from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document logger = logging.getLogger(__name__) class MboxReader(BaseReader): """Mbox parser. Extract messages from mailbox files. Returns string including date, subject, sender, receiver and content for each message. """ DEFAULT_MESSAGE_FORMAT: str = ( "Date: {_date}\n" "From: {_from}\n" "To: {_to}\n" "Subject: {_subject}\n" "Content: {_content}" ) def __init__( self, *args: Any, max_count: int = 0, message_format: str = DEFAULT_MESSAGE_FORMAT, **kwargs: Any, ) -> None: """Init params.""" try: from bs4 import BeautifulSoup # noqa except ImportError: raise ImportError( "`beautifulsoup4` package not found: `pip install beautifulsoup4`" ) super().__init__(*args, **kwargs) self.max_count = max_count self.message_format = message_format def load_data( self, file: Path, extra_info: Optional[Dict] = None, fs: Optional[AbstractFileSystem] = None, ) -> List[Document]: """Parse file into string.""" # Import required libraries import mailbox from email.parser import BytesParser from email.policy import default from bs4 import BeautifulSoup if fs: logger.warning( "fs was specified but MboxReader doesn't support loading " "from fsspec filesystems. Will load from local filesystem instead." ) i = 0 results: List[str] = [] # Load file using mailbox bytes_parser = BytesParser(policy=default).parse mbox = mailbox.mbox(file, factory=bytes_parser) # type: ignore # Iterate through all messages for _, _msg in enumerate(mbox): try: msg: mailbox.mboxMessage = _msg # Parse multipart messages if msg.is_multipart(): for part in msg.walk(): ctype = part.get_content_type() cdispo = str(part.get("Content-Disposition")) if ctype == "text/plain" and "attachment" not in cdispo: content = part.get_payload(decode=True) # decode break # Get plain message payload for non-multipart messages else: content = msg.get_payload(decode=True) # Parse message HTML content and remove unneeded whitespace soup = BeautifulSoup(content) stripped_content = " ".join(soup.get_text().split()) # Format message to include date, sender, receiver and subject msg_string = self.message_format.format( _date=msg["date"], _from=msg["from"], _to=msg["to"], _subject=msg["subject"], _content=stripped_content, ) # Add message string to results results.append(msg_string) except Exception as e: logger.warning(f"Failed to parse message:\n{_msg}\n with exception {e}") # Increment counter and return if max count is met i += 1 if self.max_count > 0 and i >= self.max_count: break return [Document(text=result, metadata=extra_info or {}) for result in results]
import torch from torch import nn from typing import List import os import json class CNN(nn.Module): """CNN-layer with multiple kernel-sizes over the word embeddings""" def __init__( self, in_word_embedding_dimension: int, out_channels: int = 256, kernel_sizes: List[int] = [1, 3, 5], stride_sizes: List[int] = None, ): nn.Module.__init__(self) self.config_keys = ["in_word_embedding_dimension", "out_channels", "kernel_sizes"] self.in_word_embedding_dimension = in_word_embedding_dimension self.out_channels = out_channels self.kernel_sizes = kernel_sizes self.embeddings_dimension = out_channels * len(kernel_sizes) self.convs = nn.ModuleList() in_channels = in_word_embedding_dimension if stride_sizes is None: stride_sizes = [1] * len(kernel_sizes) for kernel_size, stride in zip(kernel_sizes, stride_sizes): padding_size = int((kernel_size - 1) / 2) conv = nn.Conv1d( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding_size, ) self.convs.append(conv) def forward(self, features): token_embeddings = features["token_embeddings"] token_embeddings = token_embeddings.transpose(1, -1) vectors = [conv(token_embeddings) for conv in self.convs] out = torch.cat(vectors, 1).transpose(1, -1) features.update({"token_embeddings": out}) return features def get_word_embedding_dimension(self) -> int: return self.embeddings_dimension def tokenize(self, text: str, **kwargs) -> List[int]: raise NotImplementedError() def save(self, output_path: str): with open(os.path.join(output_path, "cnn_config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) torch.save(self.state_dict(), os.path.join(output_path, "pytorch_model.bin")) def get_config_dict(self): return {key: self.__dict__[key] for key in self.config_keys} @staticmethod def load(input_path: str): with open(os.path.join(input_path, "cnn_config.json"), "r") as fIn: config = json.load(fIn) weights = torch.load(os.path.join(input_path, "pytorch_model.bin"), map_location=torch.device("cpu")) model = CNN(**config) model.load_state_dict(weights) return model
import torch from torch import nn from typing import List import os import json class CNN(nn.Module): """CNN-layer with multiple kernel-sizes over the word embeddings""" def __init__( self, in_word_embedding_dimension: int, out_channels: int = 256, kernel_sizes: List[int] = [1, 3, 5], stride_sizes: List[int] = None, ): nn.Module.__init__(self) self.config_keys = ["in_word_embedding_dimension", "out_channels", "kernel_sizes"] self.in_word_embedding_dimension = in_word_embedding_dimension self.out_channels = out_channels self.kernel_sizes = kernel_sizes self.embeddings_dimension = out_channels * len(kernel_sizes) self.convs = nn.ModuleList() in_channels = in_word_embedding_dimension if stride_sizes is None: stride_sizes = [1] * len(kernel_sizes) for kernel_size, stride in zip(kernel_sizes, stride_sizes): padding_size = int((kernel_size - 1) / 2) conv = nn.Conv1d( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding_size, ) self.convs.append(conv) def forward(self, features): token_embeddings = features["token_embeddings"] token_embeddings = token_embeddings.transpose(1, -1) vectors = [conv(token_embeddings) for conv in self.convs] out = torch.cat(vectors, 1).transpose(1, -1) features.update({"token_embeddings": out}) return features def get_word_embedding_dimension(self) -> int: return self.embeddings_dimension def tokenize(self, text: str) -> List[int]: raise NotImplementedError() def save(self, output_path: str): with open(os.path.join(output_path, "cnn_config.json"), "w") as fOut: json.dump(self.get_config_dict(), fOut, indent=2) torch.save(self.state_dict(), os.path.join(output_path, "pytorch_model.bin")) def get_config_dict(self): return {key: self.__dict__[key] for key in self.config_keys} @staticmethod def load(input_path: str): with open(os.path.join(input_path, "cnn_config.json"), "r") as fIn: config = json.load(fIn) weights = torch.load(os.path.join(input_path, "pytorch_model.bin"), map_location=torch.device("cpu")) model = CNN(**config) model.load_state_dict(weights) return model
from typing import Any, Optional from typing_extensions import get_origin from typing_inspect import get_args, is_typevar, is_union_type from docarray.typing.id import ID from docarray.typing.tensor.abstract_tensor import AbstractTensor def is_type_tensor(type_: Any) -> bool: """Return True if type is a type Tensor or an Optional Tensor type.""" return isinstance(type_, type) and issubclass(type_, AbstractTensor) def is_tensor_union(type_: Any) -> bool: """Return True if type is a Union of type Tensors.""" is_union = is_union_type(type_) if is_union is None: return False else: return is_union and all( (is_type_tensor(t) or issubclass(t, type(None))) for t in get_args(type_) ) def change_cls_name(cls: type, new_name: str, scope: Optional[dict] = None) -> None: """Change the name of a class. :param cls: the class to change the name of :param new_name: the new name :param scope: the scope in which the class is defined """ if scope: scope[new_name] = cls cls.__qualname__ = cls.__qualname__[: -len(cls.__name__)] + new_name cls.__name__ = new_name def safe_issubclass(x: type, a_tuple: type) -> bool: """ This is a modified version of the built-in 'issubclass' function to support non-class input. Traditional 'issubclass' calls can result in a crash if the input is non-class type (e.g. list/tuple). :param x: A class 'x' :param a_tuple: A class, or a tuple of classes. :return: A boolean value - 'True' if 'x' is a subclass of 'A_tuple', 'False' otherwise. Note that if the origin of 'x' is a list or tuple, the function immediately returns 'False'. """ if (get_origin(x) in (list, tuple, dict, set)) or is_typevar(x) or x == ID: return False return issubclass(x, a_tuple)
from typing import Any, Optional from typing_extensions import get_origin from typing_inspect import get_args, is_typevar, is_union_type from docarray.typing.tensor.abstract_tensor import AbstractTensor def is_type_tensor(type_: Any) -> bool: """Return True if type is a type Tensor or an Optional Tensor type.""" return isinstance(type_, type) and issubclass(type_, AbstractTensor) def is_tensor_union(type_: Any) -> bool: """Return True if type is a Union of type Tensors.""" is_union = is_union_type(type_) if is_union is None: return False else: return is_union and all( (is_type_tensor(t) or issubclass(t, type(None))) for t in get_args(type_) ) def change_cls_name(cls: type, new_name: str, scope: Optional[dict] = None) -> None: """Change the name of a class. :param cls: the class to change the name of :param new_name: the new name :param scope: the scope in which the class is defined """ if scope: scope[new_name] = cls cls.__qualname__ = cls.__qualname__[: -len(cls.__name__)] + new_name cls.__name__ = new_name def safe_issubclass(x: type, a_tuple: type) -> bool: """ This is a modified version of the built-in 'issubclass' function to support non-class input. Traditional 'issubclass' calls can result in a crash if the input is non-class type (e.g. list/tuple). :param x: A class 'x' :param a_tuple: A class, or a tuple of classes. :return: A boolean value - 'True' if 'x' is a subclass of 'A_tuple', 'False' otherwise. Note that if the origin of 'x' is a list or tuple, the function immediately returns 'False'. """ if (get_origin(x) in (list, tuple, dict, set)) or is_typevar(x): return False return issubclass(x, a_tuple)
import pytest import torch from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_document.io.json import orjson_dumps from docarray.typing import TorchEmbedding, TorchTensor def test_proto_tensor(): tensor = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224)) tensor._to_node_protobuf() def test_json_schema(): schema_json_of(TorchTensor) def test_dump_json(): tensor = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224)) orjson_dumps(tensor) def test_unwrap(): tensor = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224)) ndarray = tensor.unwrap() assert not isinstance(ndarray, TorchTensor) assert isinstance(tensor, TorchTensor) assert isinstance(ndarray, torch.Tensor) assert tensor.data_ptr() == ndarray.data_ptr() assert (ndarray == torch.zeros(3, 224, 224)).all() def test_parametrized(): # correct shape, single axis tensor = parse_obj_as(TorchTensor[128], torch.zeros(128)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (128,) # correct shape, multiple axis tensor = parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(3, 224, 224)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (3, 224, 224) # wrong but reshapable shape tensor = parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(224, 3, 224)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (3, 224, 224) # wrong and not reshapable shape with pytest.raises(ValueError): parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(224, 224)) # test independent variable dimensions tensor = parse_obj_as(TorchTensor[3, 'x', 'y'], torch.zeros(3, 224, 224)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (3, 224, 224) tensor = parse_obj_as(TorchTensor[3, 'x', 'y'], torch.zeros(3, 60, 128)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (3, 60, 128) with pytest.raises(ValueError): parse_obj_as(TorchTensor[3, 'x', 'y'], torch.zeros(4, 224, 224)) with pytest.raises(ValueError): parse_obj_as(TorchTensor[3, 'x', 'y'], torch.zeros(100, 1)) # test dependent variable dimensions tensor = parse_obj_as(TorchTensor[3, 'x', 'x'], torch.zeros(3, 224, 224)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (3, 224, 224) with pytest.raises(ValueError): tensor = parse_obj_as(TorchTensor[3, 'x', 'x'], torch.zeros(3, 60, 128)) with pytest.raises(ValueError): tensor = parse_obj_as(TorchTensor[3, 'x', 'x'], torch.zeros(3, 60)) @pytest.mark.parametrize('shape', [(3, 224, 224), (224, 224, 3)]) def test_parameterized_tensor_class_name(shape): tensor = parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(shape)) assert tensor.__class__.__name__ == 'TorchTensor[3, 224, 224]' assert tensor.__class__.__qualname__ == 'TorchTensor[3, 224, 224]' assert f'{tensor[0][0][0]}' == 'TorchTensor[3, 224, 224](0.)' def test_torch_embedding(): # correct shape tensor = parse_obj_as(TorchEmbedding[128], torch.zeros(128)) assert isinstance(tensor, TorchEmbedding) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (128,) # wrong shape at data setting time with pytest.raises(ValueError): parse_obj_as(TorchEmbedding[128], torch.zeros(256)) # illegal shape at class creation time with pytest.raises(ValueError): parse_obj_as(TorchEmbedding[128, 128], torch.zeros(128, 128)) def test_parametrized_subclass(): c1 = TorchTensor[128] c2 = TorchTensor[128] assert issubclass(c1, c2) assert issubclass(c1, TorchTensor) assert issubclass(c1, torch.Tensor) assert not issubclass(c1, TorchTensor[256]) def test_parametrized_instance(): t = parse_obj_as(TorchTensor[128], torch.zeros(128)) assert isinstance(t, TorchTensor[128]) assert isinstance(t, TorchTensor) assert isinstance(t, torch.Tensor) assert not isinstance(t, TorchTensor[256]) def test_parametrized_equality(): t1 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t2 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t3 = parse_obj_as(TorchTensor[256], torch.zeros(256)) assert (t1 == t2).all() assert not t1 == t3 def test_parametrized_operations(): t1 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t2 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t_result = t1 + t2 assert isinstance(t_result, torch.Tensor) assert isinstance(t_result, TorchTensor) assert isinstance(t_result, TorchTensor[128])
import pytest import torch from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_document.io.json import orjson_dumps from docarray.typing import TorchEmbedding, TorchTensor def test_proto_tensor(): tensor = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224)) tensor._to_node_protobuf() def test_json_schema(): schema_json_of(TorchTensor) def test_dump_json(): tensor = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224)) orjson_dumps(tensor) def test_unwrap(): tensor = parse_obj_as(TorchTensor, torch.zeros(3, 224, 224)) ndarray = tensor.unwrap() assert not isinstance(ndarray, TorchTensor) assert isinstance(tensor, TorchTensor) assert isinstance(ndarray, torch.Tensor) assert tensor.data_ptr() == ndarray.data_ptr() assert (ndarray == torch.zeros(3, 224, 224)).all() def test_parametrized(): # correct shape, single axis tensor = parse_obj_as(TorchTensor[128], torch.zeros(128)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (128,) # correct shape, multiple axis tensor = parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(3, 224, 224)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (3, 224, 224) # wrong but reshapable shape tensor = parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(224, 3, 224)) assert isinstance(tensor, TorchTensor) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (3, 224, 224) # wrong and not reshapable shape with pytest.raises(ValueError): parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(224, 224)) @pytest.mark.parametrize('shape', [(3, 224, 224), (224, 224, 3)]) def test_parameterized_tensor_class_name(shape): tensor = parse_obj_as(TorchTensor[3, 224, 224], torch.zeros(shape)) assert tensor.__class__.__name__ == 'TorchTensor[3, 224, 224]' assert tensor.__class__.__qualname__ == 'TorchTensor[3, 224, 224]' assert f'{tensor[0][0][0]}' == 'TorchTensor[3, 224, 224](0.)' def test_torch_embedding(): # correct shape tensor = parse_obj_as(TorchEmbedding[128], torch.zeros(128)) assert isinstance(tensor, TorchEmbedding) assert isinstance(tensor, torch.Tensor) assert tensor.shape == (128,) # wrong shape at data setting time with pytest.raises(ValueError): parse_obj_as(TorchEmbedding[128], torch.zeros(256)) # illegal shape at class creation time with pytest.raises(ValueError): parse_obj_as(TorchEmbedding[128, 128], torch.zeros(128, 128)) def test_parametrized_subclass(): c1 = TorchTensor[128] c2 = TorchTensor[128] assert issubclass(c1, c2) assert issubclass(c1, TorchTensor) assert issubclass(c1, torch.Tensor) assert not issubclass(c1, TorchTensor[256]) def test_parametrized_instance(): t = parse_obj_as(TorchTensor[128], torch.zeros(128)) assert isinstance(t, TorchTensor[128]) assert isinstance(t, TorchTensor) assert isinstance(t, torch.Tensor) assert not isinstance(t, TorchTensor[256]) def test_parametrized_equality(): t1 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t2 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t3 = parse_obj_as(TorchTensor[256], torch.zeros(256)) assert (t1 == t2).all() assert not t1 == t3 def test_parametrized_operations(): t1 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t2 = parse_obj_as(TorchTensor[128], torch.zeros(128)) t_result = t1 + t2 assert isinstance(t_result, torch.Tensor) assert isinstance(t_result, TorchTensor) assert isinstance(t_result, TorchTensor[128])
import asyncio import numpy as np from typing import Any, List, Literal, Optional from llama_index.core.base.embeddings.base import BaseEmbedding from llama_index.core.bridge.pydantic import Field, PrivateAttr, ConfigDict from fastembed import TextEmbedding class FastEmbedEmbedding(BaseEmbedding): """ Qdrant FastEmbedding models. FastEmbed is a lightweight, fast, Python library built for embedding generation. See more documentation at: * https://github.com/qdrant/fastembed/ * https://qdrant.github.io/fastembed/. To use this class, you must install the `fastembed` Python package. `pip install fastembed` Example: from llama_index.embeddings.fastembed import FastEmbedEmbedding fastembed = FastEmbedEmbedding() """ model_config = ConfigDict( protected_namespaces=("pydantic_model_",), arbitrary_types_allowed=True, use_attribute_docstrings=True, ) model_name: str = Field( default="BAAI/bge-small-en-v1.5", description=( "Name of the FastEmbedding model to use. " "Find the list of supported models at " "https://qdrant.github.io/fastembed/examples/Supported_Models/" ), ) cache_dir: Optional[str] = Field( default=None, description="The path to the cache directory. Defaults to fastembed_cache in the system's temp directory.", ) threads: Optional[int] = Field( default=None, description="The number of threads single onnxruntime session can use. Defaults to None.", ) doc_embed_type: Literal["default", "passage"] = Field( default="default", description="Type of embedding method to use for documents. Available options are 'default' and 'passage'.", ) providers: Optional[List[str]] = Field( default=None, description="The ONNX providers to use for the embedding model." ) _model: TextEmbedding = PrivateAttr() def __init__( self, model_name: str = "BAAI/bge-small-en-v1.5", cache_dir: Optional[str] = None, threads: Optional[int] = None, doc_embed_type: Literal["default", "passage"] = "default", providers: Optional[List[str]] = None, **kwargs: Any, ): super().__init__( model_name=model_name, threads=threads, doc_embed_type=doc_embed_type, providers=providers, cache_dir=cache_dir, **kwargs, ) self._model = TextEmbedding( model_name=model_name, cache_dir=cache_dir, threads=threads, providers=providers, **kwargs, ) @classmethod def class_name(cls) -> str: return "FastEmbedEmbedding" def _get_text_embedding(self, text: str) -> List[float]: return self._get_text_embeddings([text])[0] async def _aget_text_embedding(self, text: str) -> List[float]: return await asyncio.to_thread(self._get_text_embedding, text) def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]: embeddings: List[np.ndarray] if self.doc_embed_type == "passage": embeddings = list(self._model.passage_embed(texts)) else: embeddings = list(self._model.embed(texts)) return [embedding.tolist() for embedding in embeddings] async def _aget_text_embeddings(self, texts: List[str]) -> List[List[float]]: return await asyncio.to_thread(self._get_text_embeddings, texts) def _get_query_embedding(self, query: str) -> List[float]: query_embeddings: list[np.ndarray] = list(self._model.query_embed(query)) return query_embeddings[0].tolist() async def _aget_query_embedding(self, query: str) -> List[float]: return await asyncio.to_thread(self._get_query_embedding, query)
from typing import Any, List, Literal, Optional import numpy as np from llama_index.core.base.embeddings.base import BaseEmbedding from llama_index.core.bridge.pydantic import Field, PrivateAttr from fastembed import TextEmbedding class FastEmbedEmbedding(BaseEmbedding): """ Qdrant FastEmbedding models. FastEmbed is a lightweight, fast, Python library built for embedding generation. See more documentation at: * https://github.com/qdrant/fastembed/ * https://qdrant.github.io/fastembed/. To use this class, you must install the `fastembed` Python package. `pip install fastembed` Example: from llama_index.embeddings.fastembed import FastEmbedEmbedding fastembed = FastEmbedEmbedding() """ model_name: str = Field( "BAAI/bge-small-en-v1.5", description="Name of the FastEmbedding model to use.\n" "Defaults to 'BAAI/bge-small-en-v1.5'.\n" "Find the list of supported models at " "https://qdrant.github.io/fastembed/examples/Supported_Models/", ) max_length: int = Field( 512, description="The maximum number of tokens. Defaults to 512.\n" "Unknown behavior for values > 512.", ) cache_dir: Optional[str] = Field( None, description="The path to the cache directory.\n" "Defaults to `local_cache` in the parent directory", ) threads: Optional[int] = Field( None, description="The number of threads single onnxruntime session can use.\n" "Defaults to None", ) doc_embed_type: Literal["default", "passage"] = Field( "default", description="Type of embedding method to use for documents.\n" "Available options are 'default' and 'passage'.", ) providers: Optional[List[str]] = Field( default=None, description="The ONNX providers to use for the embedding model.", ) _model: Any = PrivateAttr() @classmethod def class_name(self) -> str: return "FastEmbedEmbedding" def __init__( self, model_name: Optional[str] = "BAAI/bge-small-en-v1.5", max_length: Optional[int] = 512, cache_dir: Optional[str] = None, threads: Optional[int] = None, doc_embed_type: Literal["default", "passage"] = "default", providers: Optional[List[str]] = None, **kwargs: Any, ): super().__init__( model_name=model_name, max_length=max_length, threads=threads, doc_embed_type=doc_embed_type, providers=providers, **kwargs, ) self._model = TextEmbedding( model_name=model_name, max_length=max_length, cache_dir=cache_dir, threads=threads, providers=providers, **kwargs, ) def _get_text_embedding(self, text: str) -> List[float]: embeddings: List[np.ndarray] if self.doc_embed_type == "passage": embeddings = list(self._model.passage_embed(text)) else: embeddings = list(self._model.embed(text)) return embeddings[0].tolist() def _get_query_embedding(self, query: str) -> List[float]: query_embeddings: np.ndarray = next(self._model.query_embed(query)) return query_embeddings.tolist() async def _aget_query_embedding(self, query: str) -> List[float]: return self._get_query_embedding(query)
import io import logging from enum import Enum import replicate import replicate.exceptions import requests from replicate.helpers import FileOutput from backend.data.graph import Graph from backend.util.settings import Settings logger = logging.getLogger(__name__) class ImageSize(str, Enum): LANDSCAPE = "1024x768" class ImageStyle(str, Enum): DIGITAL_ART = "digital art" async def generate_agent_image(agent: Graph) -> io.BytesIO: """ Generate an image for an agent using Flux model via Replicate API. Args: agent (Graph): The agent to generate an image for Returns: io.BytesIO: The generated image as bytes """ try: settings = Settings() if not settings.secrets.replicate_api_key: raise ValueError("Missing Replicate API key in settings") # Construct prompt from agent details prompt = f"Create a visually engaging app store thumbnail for the AI agent that highlights what it does in a clear and captivating way:\n- **Name**: {agent.name}\n- **Description**: {agent.description}\nFocus on showcasing its core functionality with an appealing design." # Set up Replicate client client = replicate.Client(api_token=settings.secrets.replicate_api_key) # Model parameters input_data = { "prompt": prompt, "width": 1024, "height": 768, "aspect_ratio": "4:3", "output_format": "jpg", "output_quality": 90, "num_inference_steps": 30, "guidance": 3.5, "negative_prompt": "blurry, low quality, distorted, deformed", "disable_safety_checker": True, } try: # Run model output = client.run("black-forest-labs/flux-1.1-pro", input=input_data) # Depending on the model output, extract the image URL or bytes # If the output is a list of FileOutput or URLs if isinstance(output, list) and output: if isinstance(output[0], FileOutput): image_bytes = output[0].read() else: # If it's a URL string, fetch the image bytes result_url = output[0] response = requests.get(result_url) response.raise_for_status() image_bytes = response.content elif isinstance(output, FileOutput): image_bytes = output.read() elif isinstance(output, str): # Output is a URL response = requests.get(output) response.raise_for_status() image_bytes = response.content else: raise RuntimeError("Unexpected output format from the model.") return io.BytesIO(image_bytes) except replicate.exceptions.ReplicateError as e: if e.status == 401: raise RuntimeError("Invalid Replicate API token") from e raise RuntimeError(f"Replicate API error: {str(e)}") from e except Exception as e: logger.exception("Failed to generate agent image") raise RuntimeError(f"Image generation failed: {str(e)}")
import io import logging from enum import Enum import replicate import replicate.exceptions import requests from replicate.helpers import FileOutput from backend.data.graph import Graph from backend.util.settings import Settings logger = logging.getLogger(__name__) class ImageSize(str, Enum): LANDSCAPE = "1024x768" class ImageStyle(str, Enum): DIGITAL_ART = "digital art" async def generate_agent_image(agent: Graph) -> io.BytesIO: """ Generate an image for an agent using Flux model via Replicate API. Args: agent (Graph): The agent to generate an image for Returns: io.BytesIO: The generated image as bytes """ try: settings = Settings() if not settings.secrets.replicate_api_key: raise ValueError("Missing Replicate API key in settings") # Construct prompt from agent details prompt = f"App store image for AI agent that gives a cool visual representation of what the agent does: - {agent.name} - {agent.description}" # Set up Replicate client client = replicate.Client(api_token=settings.secrets.replicate_api_key) # Model parameters input_data = { "prompt": prompt, "width": 1024, "height": 768, "aspect_ratio": "4:3", "output_format": "jpg", "output_quality": 90, "num_inference_steps": 30, "guidance": 3.5, "negative_prompt": "blurry, low quality, distorted, deformed", "disable_safety_checker": True, } try: # Run model output = client.run("black-forest-labs/flux-pro", input=input_data) # Depending on the model output, extract the image URL or bytes # If the output is a list of FileOutput or URLs if isinstance(output, list) and output: if isinstance(output[0], FileOutput): image_bytes = output[0].read() else: # If it's a URL string, fetch the image bytes result_url = output[0] response = requests.get(result_url) response.raise_for_status() image_bytes = response.content elif isinstance(output, FileOutput): image_bytes = output.read() elif isinstance(output, str): # Output is a URL response = requests.get(output) response.raise_for_status() image_bytes = response.content else: raise RuntimeError("Unexpected output format from the model.") return io.BytesIO(image_bytes) except replicate.exceptions.ReplicateError as e: if e.status == 401: raise RuntimeError("Invalid Replicate API token") from e raise RuntimeError(f"Replicate API error: {str(e)}") from e except Exception as e: logger.exception("Failed to generate agent image") raise RuntimeError(f"Image generation failed: {str(e)}")