input
stringlengths
33
5k
output
stringlengths
32
5k
# Copyright (c) OpenMMLab. All rights reserved. from mmengine.hooks import Hook from mmengine.model.wrappers import is_model_wrapper from mmdet.registry import HOOKS @HOOKS.register_module() class SetEpochInfoHook(Hook): """Set runner's epoch information to the model.""" def before_train_epoch(self, runner): epoch = runner.epoch model = runner.model if is_model_wrapper(model): model = model.module model.set_epoch(epoch)
# Copyright (c) OpenMMLab. All rights reserved. from mmcv.parallel import is_module_wrapper from mmcv.runner import Hook from mmdet.registry import HOOKS @HOOKS.register_module() class SetEpochInfoHook(Hook): """Set runner's epoch information to the model.""" def before_train_epoch(self, runner): epoch = runner.epoch model = runner.model if is_module_wrapper(model): model = model.module model.set_epoch(epoch)
# Copyright (c) OpenMMLab. All rights reserved. import torch.nn as nn from mmcv.cnn import ConvModule from mmcv.runner import BaseModule from mmdet.registry import MODELS @MODELS.register_module() class ChannelMapper(BaseModule): r"""Channel Mapper to reduce/increase channels of backbone features. This is used to reduce/increase channels of backbone features. Args: in_channels (List[int]): Number of input channels per scale. out_channels (int): Number of output channels (used at each scale). kernel_size (int, optional): kernel_size for reducing channels (used at each scale). Default: 3. conv_cfg (dict, optional): Config dict for convolution layer. Default: None. norm_cfg (dict, optional): Config dict for normalization layer. Default: None. act_cfg (dict, optional): Config dict for activation layer in ConvModule. Default: dict(type='ReLU'). num_outs (int, optional): Number of output feature maps. There would be extra_convs when num_outs larger than the length of in_channels. init_cfg (dict or list[dict], optional): Initialization config dict. Example: >>> import torch >>> in_channels = [2, 3, 5, 7] >>> scales = [340, 170, 84, 43] >>> inputs = [torch.rand(1, c, s, s) ... for c, s in zip(in_channels, scales)] >>> self = ChannelMapper(in_channels, 11, 3).eval() >>> outputs = self.forward(inputs) >>> for i in range(len(outputs)): ... print(f'outputs[{i}].shape = {outputs[i].shape}') outputs[0].shape = torch.Size([1, 11, 340, 340]) outputs[1].shape = torch.Size([1, 11, 170, 170]) outputs[2].shape = torch.Size([1, 11, 84, 84]) outputs[3].shape = torch.Size([1, 11, 43, 43]) """ def __init__(self, in_channels, out_channels, kernel_size=3, conv_cfg=None, norm_cfg=None, act_cfg=dict(type='ReLU'), num_outs=None, init_cfg=dict( type='Xavier', layer='Conv2d', distribution='uniform')): super(ChannelMapper, self).__init__(init_cfg) assert isinstance(in_channels, list) self.extra_convs = None if num_outs is None: num_outs = len(in_channels) self.convs = nn.ModuleList() for in_channel in in_channels: self.convs.append( ConvModule( in_channel, out_channels, kernel_size, padding=(kernel_size - 1) // 2, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) if num_outs > len(in_channels): self.extra_convs = nn.ModuleList() for i in range(len(in_channels), num_outs): if i == len(in_channels): in_channel = in_channels[-1] else: in_channel = out_channels self.extra_convs.append( ConvModule( in_channel, out_channels, 3, stride=2, padding=1, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) def forward(self, inputs): """Forward function.""" assert len(inputs) == len(self.convs) outs = [self.convs[i](inputs[i]) for i in range(len(inputs))] if self.extra_convs: for i in range(len(self.extra_convs)): if i == 0: outs.append(self.extra_convs[0](inputs[-1])) else: outs.append(self.extra_convs[i](outs[-1])) return tuple(outs)
# Copyright (c) OpenMMLab. All rights reserved. import torch.nn as nn from mmcv.cnn import ConvModule from mmcv.runner import BaseModule from ..builder import NECKS @NECKS.register_module() class ChannelMapper(BaseModule): r"""Channel Mapper to reduce/increase channels of backbone features. This is used to reduce/increase channels of backbone features. Args: in_channels (List[int]): Number of input channels per scale. out_channels (int): Number of output channels (used at each scale). kernel_size (int, optional): kernel_size for reducing channels (used at each scale). Default: 3. conv_cfg (dict, optional): Config dict for convolution layer. Default: None. norm_cfg (dict, optional): Config dict for normalization layer. Default: None. act_cfg (dict, optional): Config dict for activation layer in ConvModule. Default: dict(type='ReLU'). num_outs (int, optional): Number of output feature maps. There would be extra_convs when num_outs larger than the length of in_channels. init_cfg (dict or list[dict], optional): Initialization config dict. Example: >>> import torch >>> in_channels = [2, 3, 5, 7] >>> scales = [340, 170, 84, 43] >>> inputs = [torch.rand(1, c, s, s) ... for c, s in zip(in_channels, scales)] >>> self = ChannelMapper(in_channels, 11, 3).eval() >>> outputs = self.forward(inputs) >>> for i in range(len(outputs)): ... print(f'outputs[{i}].shape = {outputs[i].shape}') outputs[0].shape = torch.Size([1, 11, 340, 340]) outputs[1].shape = torch.Size([1, 11, 170, 170]) outputs[2].shape = torch.Size([1, 11, 84, 84]) outputs[3].shape = torch.Size([1, 11, 43, 43]) """ def __init__(self, in_channels, out_channels, kernel_size=3, conv_cfg=None, norm_cfg=None, act_cfg=dict(type='ReLU'), num_outs=None, init_cfg=dict( type='Xavier', layer='Conv2d', distribution='uniform')): super(ChannelMapper, self).__init__(init_cfg) assert isinstance(in_channels, list) self.extra_convs = None if num_outs is None: num_outs = len(in_channels) self.convs = nn.ModuleList() for in_channel in in_channels: self.convs.append( ConvModule( in_channel, out_channels, kernel_size, padding=(kernel_size - 1) // 2, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) if num_outs > len(in_channels): self.extra_convs = nn.ModuleList() for i in range(len(in_channels), num_outs): if i == len(in_channels): in_channel = in_channels[-1] else: in_channel = out_channels self.extra_convs.append( ConvModule( in_channel, out_channels, 3, stride=2, padding=1, conv_cfg=conv_cfg, norm_cfg=norm_cfg, act_cfg=act_cfg)) def forward(self, inputs): """Forward function.""" assert len(inputs) == len(self.convs) outs = [self.convs[i](inputs[i]) for i in range(len(inputs))] if self.extra_convs: for i in range(len(self.extra_convs)): if i == 0: outs.append(self.extra_convs[0](inputs[-1])) else: outs.append(self.extra_convs[i](outs[-1])) return tuple(outs)
import asyncio import json from typing import Optional, Union from unittest import mock import pytest from llama_index.core.workflow.decorators import step from llama_index.core.workflow.errors import WorkflowRuntimeError from llama_index.core.workflow.events import Event, StartEvent, StopEvent from llama_index.core.workflow.workflow import ( Context, Workflow, ) from .conftest import AnotherTestEvent, OneTestEvent @pytest.mark.asyncio() async def test_collect_events(): ev1 = OneTestEvent() ev2 = AnotherTestEvent() class TestWorkflow(Workflow): @step async def step1(self, _: StartEvent) -> OneTestEvent: return ev1 @step async def step2(self, _: StartEvent) -> AnotherTestEvent: return ev2 @step async def step3( self, ctx: Context, ev: Union[OneTestEvent, AnotherTestEvent] ) -> Optional[StopEvent]: events = ctx.collect_events(ev, [OneTestEvent, AnotherTestEvent]) if events is None: return None return StopEvent(result=events) workflow = TestWorkflow() result = await workflow.run() assert result == [ev1, ev2] @pytest.mark.asyncio() async def test_get_default(workflow): c1 = Context(workflow) assert await c1.get(key="test_key", default=42) == 42 @pytest.mark.asyncio() async def test_get(ctx): await ctx.set("foo", 42) assert await ctx.get("foo") == 42 @pytest.mark.asyncio() async def test_get_not_found(ctx): with pytest.raises(ValueError): await ctx.get("foo") @pytest.mark.asyncio() async def test_legacy_data(workflow): c1 = Context(workflow) await c1.set(key="test_key", value=42) assert c1.data["test_key"] == 42 def test_send_event_step_is_none(ctx): ctx._queues = {"step1": mock.MagicMock(), "step2": mock.MagicMock()} ev = Event(foo="bar") ctx.send_event(ev) for q in ctx._queues.values(): q.put_nowait.assert_called_with(ev) assert ctx._broker_log == [ev] def test_send_event_to_non_existent_step(ctx): with pytest.raises( WorkflowRuntimeError, match="Step does_not_exist does not exist" ): ctx.send_event(Event(), "does_not_exist") def test_send_event_to_wrong_step(ctx): ctx._workflow._get_steps = mock.MagicMock(return_value={"step": mock.MagicMock()}) with pytest.raises( WorkflowRuntimeError, match="Step step does not accept event of type <class 'llama_index.core.workflow.events.Event'>", ): ctx.send_event(Event(), "step") def test_send_event_to_step(ctx): step2 = mock.MagicMock() step2.__step_config.accepted_events = [Event] ctx._workflow._get_steps = mock.MagicMock( return_value={"step1": mock.MagicMock(), "step2": step2} ) ctx._queues = {"step1": mock.MagicMock(), "step2": mock.MagicMock()} ev = Event(foo="bar") ctx.send_event(ev, "step2") ctx._queues["step1"].put_nowait.assert_not_called() ctx._queues["step2"].put_nowait.assert_called_with(ev) def test_get_result(ctx): ctx._retval = 42 assert ctx.get_result() == 42 def test_to_dict_with_events_buffer(ctx): ctx.collect_events(OneTestEvent(), [OneTestEvent, AnotherTestEvent]) assert json.dumps(ctx.to_dict()) @pytest.mark.asyncio() async def test_deprecated_params(ctx): with pytest.warns( DeprecationWarning, match="`make_private` is deprecated and will be ignored" ): await ctx.set("foo", 42, make_private=True) @pytest.mark.asyncio() async def test_empty_inprogress_when_workflow_done(workflow): h = workflow.run() _ = await h # there shouldn't be any in progress events for inprogress_list in h.ctx._in_progress.values(): assert len(inprogress_list) == 0 @pytest.mark.asyncio() async def test_wait_for_event(ctx): wait_job = asyncio.create_task(ctx.wait_for_event(Event)) await asyncio.sleep(0.01) ctx.send_event(Event(msg="foo")) ev = await wait_job assert ev.msg == "foo" @pytest.mark.asyncio() async def test_wait_for_event_with_requirements(ctx): wait_job = asyncio.create_task(ctx.wait_for_event(Event, {"msg": "foo"})) await asyncio.sleep(0.01) ctx.send_event(Event(msg="bar")) ctx.send_event(Event(msg="foo")) ev = await wait_job assert ev.msg == "foo" @pytest.mark.asyncio() async def test_wait_for_event_in_workflow(): class TestWorkflow(Workflow): @step async def step1(self, ctx: Context, ev: StartEvent) -> StopEvent: ctx.write_event_to_stream(Event(msg="foo")) result = await ctx.wait_for_event(Event) return StopEvent(result=result.msg) workflow = TestWorkflow() handler = workflow.run() async for ev in handler.stream_events(): if isinstance(ev, Event) and ev.msg == "foo": handler.ctx.send_event(Event(msg="bar")) break result = await handler assert result == "bar"
import asyncio from unittest import mock from typing import Union, Optional import pytest from llama_index.core.workflow.workflow import ( Workflow, Context, ) from llama_index.core.workflow.decorators import step from llama_index.core.workflow.errors import WorkflowRuntimeError from llama_index.core.workflow.events import StartEvent, StopEvent, Event from llama_index.core.workflow.workflow import Workflow from .conftest import OneTestEvent, AnotherTestEvent @pytest.mark.asyncio() async def test_collect_events(): ev1 = OneTestEvent() ev2 = AnotherTestEvent() class TestWorkflow(Workflow): @step async def step1(self, _: StartEvent) -> OneTestEvent: return ev1 @step async def step2(self, _: StartEvent) -> AnotherTestEvent: return ev2 @step async def step3( self, ctx: Context, ev: Union[OneTestEvent, AnotherTestEvent] ) -> Optional[StopEvent]: events = ctx.collect_events(ev, [OneTestEvent, AnotherTestEvent]) if events is None: return None return StopEvent(result=events) workflow = TestWorkflow() result = await workflow.run() assert result == [ev1, ev2] @pytest.mark.asyncio() async def test_get_default(workflow): c1 = Context(workflow) assert await c1.get(key="test_key", default=42) == 42 @pytest.mark.asyncio() async def test_get(ctx): await ctx.set("foo", 42) assert await ctx.get("foo") == 42 @pytest.mark.asyncio() async def test_get_not_found(ctx): with pytest.raises(ValueError): await ctx.get("foo") @pytest.mark.asyncio() async def test_legacy_data(workflow): c1 = Context(workflow) await c1.set(key="test_key", value=42) assert c1.data["test_key"] == 42 def test_send_event_step_is_none(ctx): ctx._queues = {"step1": mock.MagicMock(), "step2": mock.MagicMock()} ev = Event(foo="bar") ctx.send_event(ev) for q in ctx._queues.values(): q.put_nowait.assert_called_with(ev) assert ctx._broker_log == [ev] def test_send_event_to_non_existent_step(ctx): with pytest.raises( WorkflowRuntimeError, match="Step does_not_exist does not exist" ): ctx.send_event(Event(), "does_not_exist") def test_send_event_to_wrong_step(ctx): ctx._workflow._get_steps = mock.MagicMock(return_value={"step": mock.MagicMock()}) with pytest.raises( WorkflowRuntimeError, match="Step step does not accept event of type <class 'llama_index.core.workflow.events.Event'>", ): ctx.send_event(Event(), "step") def test_send_event_to_step(ctx): step2 = mock.MagicMock() step2.__step_config.accepted_events = [Event] ctx._workflow._get_steps = mock.MagicMock( return_value={"step1": mock.MagicMock(), "step2": step2} ) ctx._queues = {"step1": mock.MagicMock(), "step2": mock.MagicMock()} ev = Event(foo="bar") ctx.send_event(ev, "step2") ctx._queues["step1"].put_nowait.assert_not_called() ctx._queues["step2"].put_nowait.assert_called_with(ev) def test_get_result(ctx): ctx._retval = 42 assert ctx.get_result() == 42 @pytest.mark.asyncio() async def test_deprecated_params(ctx): with pytest.warns( DeprecationWarning, match="`make_private` is deprecated and will be ignored" ): await ctx.set("foo", 42, make_private=True) @pytest.mark.asyncio() async def test_empty_inprogress_when_workflow_done(workflow): h = workflow.run() _ = await h # there shouldn't be any in progress events for inprogress_list in h.ctx._in_progress.values(): assert len(inprogress_list) == 0 @pytest.mark.asyncio() async def test_wait_for_event(ctx): wait_job = asyncio.create_task(ctx.wait_for_event(Event)) await asyncio.sleep(0.01) ctx.send_event(Event(msg="foo")) ev = await wait_job assert ev.msg == "foo" @pytest.mark.asyncio() async def test_wait_for_event_with_requirements(ctx): wait_job = asyncio.create_task(ctx.wait_for_event(Event, {"msg": "foo"})) await asyncio.sleep(0.01) ctx.send_event(Event(msg="bar")) ctx.send_event(Event(msg="foo")) ev = await wait_job assert ev.msg == "foo" @pytest.mark.asyncio() async def test_wait_for_event_in_workflow(): class TestWorkflow(Workflow): @step async def step1(self, ctx: Context, ev: StartEvent) -> StopEvent: ctx.write_event_to_stream(Event(msg="foo")) result = await ctx.wait_for_event(Event) return StopEvent(result=result.msg) workflow = TestWorkflow() handler = workflow.run() async for ev in handler.stream_events(): if isinstance(ev, Event) and ev.msg == "foo": handler.ctx.send_event(Event(msg="bar")) break result = await handler assert result == "bar"
import numpy as np import pytest import torch from docarray import BaseDoc from docarray.typing import AnyTensor, NdArray, TorchTensor from docarray.utils._internal.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp # type: ignore from docarray.typing import TensorFlowTensor else: TensorFlowTensor = None def test_set_tensor(): class MyDocument(BaseDoc): tensor: AnyTensor d = MyDocument(tensor=np.zeros((3, 224, 224))) assert isinstance(d.tensor, NdArray) assert isinstance(d.tensor, np.ndarray) assert (d.tensor == np.zeros((3, 224, 224))).all() d = MyDocument(tensor=torch.zeros((3, 224, 224))) assert isinstance(d.tensor, TorchTensor) assert isinstance(d.tensor, torch.Tensor) assert (d.tensor == torch.zeros((3, 224, 224))).all() @pytest.mark.tensorflow def test_set_tensor_tensorflow(): class MyDocument(BaseDoc): tensor: AnyTensor d = MyDocument(tensor=tf.zeros((3, 224, 224))) assert isinstance(d.tensor, TensorFlowTensor) assert isinstance(d.tensor.tensor, tf.Tensor) assert tnp.allclose(d.tensor.tensor, tf.zeros((3, 224, 224)))
import numpy as np import pytest import torch from docarray import BaseDoc from docarray.typing import AnyTensor, NdArray, TorchTensor from docarray.utils.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf import tensorflow._api.v2.experimental.numpy as tnp # type: ignore from docarray.typing import TensorFlowTensor else: TensorFlowTensor = None def test_set_tensor(): class MyDocument(BaseDoc): tensor: AnyTensor d = MyDocument(tensor=np.zeros((3, 224, 224))) assert isinstance(d.tensor, NdArray) assert isinstance(d.tensor, np.ndarray) assert (d.tensor == np.zeros((3, 224, 224))).all() d = MyDocument(tensor=torch.zeros((3, 224, 224))) assert isinstance(d.tensor, TorchTensor) assert isinstance(d.tensor, torch.Tensor) assert (d.tensor == torch.zeros((3, 224, 224))).all() @pytest.mark.tensorflow def test_set_tensor_tensorflow(): class MyDocument(BaseDoc): tensor: AnyTensor d = MyDocument(tensor=tf.zeros((3, 224, 224))) assert isinstance(d.tensor, TensorFlowTensor) assert isinstance(d.tensor.tensor, tf.Tensor) assert tnp.allclose(d.tensor.tensor, tf.zeros((3, 224, 224)))
""" This script contains an example how to perform semantic search with OpenSearch. You need OpenSearch up and running locally: https://docs.opensearch.org/docs/latest/getting-started/quickstart/ Further, you need the Python OpenSearch Client installed: https://docs.opensearch.org/docs/latest/clients/python-low-level/, e.g.: ``` pip install opensearch-py ``` This script was created for `opensearch` v2.15.0+. """ import time from datasets import load_dataset from sentence_transformers import SparseEncoder, models from sentence_transformers.sparse_encoder.models import IDF, MLMTransformer, SpladePooling from sentence_transformers.sparse_encoder.search_engines import semantic_search_opensearch # 1. Load the natural-questions dataset with 100K answers dataset = load_dataset("sentence-transformers/natural-questions", split="train") num_docs = 10_000 corpus = dataset["answer"][:num_docs] print(f"Finish loading data. Corpus size: {len(corpus)}") # 2. Come up with some queries queries = dataset["query"][:2] # 3. Load the model model_id = "opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill" doc_encoder = MLMTransformer(model_id) asym = models.Router( { "query": [ IDF.from_json( model_id, tokenizer=doc_encoder.tokenizer, frozen=True, ), ], "doc": [ doc_encoder, SpladePooling("max", activation_function="log1p_relu"), ], } ) sparse_model = SparseEncoder( modules=[asym], similarity_fn_name="dot", ) print("Start encoding corpus...") start_time = time.time() # 4. Encode the corpus corpus_embeddings = sparse_model.encode( [{"doc": doc} for doc in corpus], convert_to_sparse_tensor=True, batch_size=32, show_progress_bar=True ) corpus_embeddings_decoded = sparse_model.decode(corpus_embeddings) print(f"Corpus encoding time: {time.time() - start_time:.6f} seconds") corpus_index = None while True: # 5. Encode the queries using inference-free mode start_time = time.time() query_embeddings = sparse_model.encode([{"query": query} for query in queries], convert_to_sparse_tensor=True) query_embeddings_decoded = sparse_model.decode(query_embeddings) print(f"Query encoding time: {time.time() - start_time:.6f} seconds") # 6. Perform semantic search using OpenSearch results, search_time, corpus_index = semantic_search_opensearch( query_embeddings_decoded, corpus_embeddings_decoded=corpus_embeddings_decoded if corpus_index is None else None, corpus_index=corpus_index, top_k=5, output_index=True, ) # 7. Output the results print(f"Search time: {search_time:.6f} seconds") for query, result in zip(queries, results): print(f"Query: {query}") for entry in result: print(f"(Score: {entry['score']:.4f}) {corpus[entry['corpus_id']]}, corpus_id: {entry['corpus_id']}") print("") # 8. Prompt for more queries queries = [input("Please enter a question: ")]
""" This script contains an example how to perform semantic search with OpenSearch. You need OpenSearch up and running locally: https://docs.opensearch.org/docs/latest/getting-started/quickstart/ Further, you need the Python OpenSearch Client installed: https://docs.opensearch.org/docs/latest/clients/python-low-level/, e.g.: ``` pip install opensearch-py ``` This script was created for `opensearch` v2.15.0+. """ import time from datasets import load_dataset from sentence_transformers import SparseEncoder, models from sentence_transformers.sparse_encoder.models import IDF, MLMTransformer, SpladePooling from sentence_transformers.sparse_encoder.search_engines import semantic_search_opensearch # 1. Load the natural-questions dataset with 100K answers dataset = load_dataset("sentence-transformers/natural-questions", split="train") num_docs = 10_000 corpus = dataset["answer"][:num_docs] print(f"Finish loading data. Corpus size: {len(corpus)}") # 2. Come up with some queries queries = dataset["query"][:2] # 3. Load the model model_id = "opensearch-project/opensearch-neural-sparse-encoding-doc-v3-distill" doc_encoder = MLMTransformer(model_id) asym = models.Asym( { "query": [ IDF.from_json( model_id, tokenizer=doc_encoder.tokenizer, frozen=True, ), ], "doc": [ doc_encoder, SpladePooling("max", activation_function="log1p_relu"), ], } ) sparse_model = SparseEncoder( modules=[asym], similarity_fn_name="dot", ) print("Start encoding corpus...") start_time = time.time() # 4. Encode the corpus corpus_embeddings = sparse_model.encode( [{"doc": doc} for doc in corpus], convert_to_sparse_tensor=True, batch_size=32, show_progress_bar=True ) corpus_embeddings_decoded = sparse_model.decode(corpus_embeddings) print(f"Corpus encoding time: {time.time() - start_time:.6f} seconds") corpus_index = None while True: # 5. Encode the queries using inference-free mode start_time = time.time() query_embeddings = sparse_model.encode([{"query": query} for query in queries], convert_to_sparse_tensor=True) query_embeddings_decoded = sparse_model.decode(query_embeddings) print(f"Query encoding time: {time.time() - start_time:.6f} seconds") # 6. Perform semantic search using OpenSearch results, search_time, corpus_index = semantic_search_opensearch( query_embeddings_decoded, corpus_embeddings_decoded=corpus_embeddings_decoded if corpus_index is None else None, corpus_index=corpus_index, top_k=5, output_index=True, ) # 7. Output the results print(f"Search time: {search_time:.6f} seconds") for query, result in zip(queries, results): print(f"Query: {query}") for entry in result: print(f"(Score: {entry['score']:.4f}) {corpus[entry['corpus_id']]}, corpus_id: {entry['corpus_id']}") print("") # 8. Prompt for more queries queries = [input("Please enter a question: ")]
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.initializers import deserialize as deserialize from keras.src.initializers import get as get from keras.src.initializers import serialize as serialize from keras.src.initializers.constant_initializers import STFT as STFT from keras.src.initializers.constant_initializers import STFT as STFTInitializer from keras.src.initializers.constant_initializers import STFT as stft from keras.src.initializers.constant_initializers import Constant as Constant from keras.src.initializers.constant_initializers import Constant as constant from keras.src.initializers.constant_initializers import Identity as Identity from keras.src.initializers.constant_initializers import ( Identity as IdentityInitializer, ) from keras.src.initializers.constant_initializers import Identity as identity from keras.src.initializers.constant_initializers import Ones as Ones from keras.src.initializers.constant_initializers import Ones as ones from keras.src.initializers.constant_initializers import Zeros as Zeros from keras.src.initializers.constant_initializers import Zeros as zeros from keras.src.initializers.initializer import Initializer as Initializer from keras.src.initializers.random_initializers import ( GlorotNormal as GlorotNormal, ) from keras.src.initializers.random_initializers import ( GlorotNormal as glorot_normal, ) from keras.src.initializers.random_initializers import ( GlorotUniform as GlorotUniform, ) from keras.src.initializers.random_initializers import ( GlorotUniform as glorot_uniform, ) from keras.src.initializers.random_initializers import HeNormal as HeNormal from keras.src.initializers.random_initializers import HeNormal as he_normal from keras.src.initializers.random_initializers import HeUniform as HeUniform from keras.src.initializers.random_initializers import HeUniform as he_uniform from keras.src.initializers.random_initializers import ( LecunNormal as LecunNormal, ) from keras.src.initializers.random_initializers import ( LecunNormal as lecun_normal, ) from keras.src.initializers.random_initializers import ( LecunUniform as LecunUniform, ) from keras.src.initializers.random_initializers import ( LecunUniform as lecun_uniform, ) from keras.src.initializers.random_initializers import Orthogonal as Orthogonal from keras.src.initializers.random_initializers import ( Orthogonal as OrthogonalInitializer, ) from keras.src.initializers.random_initializers import Orthogonal as orthogonal from keras.src.initializers.random_initializers import ( RandomNormal as RandomNormal, ) from keras.src.initializers.random_initializers import ( RandomNormal as random_normal, ) from keras.src.initializers.random_initializers import ( RandomUniform as RandomUniform, ) from keras.src.initializers.random_initializers import ( RandomUniform as random_uniform, ) from keras.src.initializers.random_initializers import ( TruncatedNormal as TruncatedNormal, ) from keras.src.initializers.random_initializers import ( TruncatedNormal as truncated_normal, ) from keras.src.initializers.random_initializers import ( VarianceScaling as VarianceScaling, ) from keras.src.initializers.random_initializers import ( VarianceScaling as variance_scaling, )
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.initializers import deserialize from keras.src.initializers import get from keras.src.initializers import serialize from keras.src.initializers.constant_initializers import STFT from keras.src.initializers.constant_initializers import STFT as STFTInitializer from keras.src.initializers.constant_initializers import STFT as stft from keras.src.initializers.constant_initializers import Constant from keras.src.initializers.constant_initializers import Constant as constant from keras.src.initializers.constant_initializers import Identity from keras.src.initializers.constant_initializers import ( Identity as IdentityInitializer, ) from keras.src.initializers.constant_initializers import Identity as identity from keras.src.initializers.constant_initializers import Ones from keras.src.initializers.constant_initializers import Ones as ones from keras.src.initializers.constant_initializers import Zeros from keras.src.initializers.constant_initializers import Zeros as zeros from keras.src.initializers.initializer import Initializer from keras.src.initializers.random_initializers import GlorotNormal from keras.src.initializers.random_initializers import ( GlorotNormal as glorot_normal, ) from keras.src.initializers.random_initializers import GlorotUniform from keras.src.initializers.random_initializers import ( GlorotUniform as glorot_uniform, ) from keras.src.initializers.random_initializers import HeNormal from keras.src.initializers.random_initializers import HeNormal as he_normal from keras.src.initializers.random_initializers import HeUniform from keras.src.initializers.random_initializers import HeUniform as he_uniform from keras.src.initializers.random_initializers import LecunNormal from keras.src.initializers.random_initializers import ( LecunNormal as lecun_normal, ) from keras.src.initializers.random_initializers import LecunUniform from keras.src.initializers.random_initializers import ( LecunUniform as lecun_uniform, ) from keras.src.initializers.random_initializers import Orthogonal from keras.src.initializers.random_initializers import ( Orthogonal as OrthogonalInitializer, ) from keras.src.initializers.random_initializers import Orthogonal as orthogonal from keras.src.initializers.random_initializers import RandomNormal from keras.src.initializers.random_initializers import ( RandomNormal as random_normal, ) from keras.src.initializers.random_initializers import RandomUniform from keras.src.initializers.random_initializers import ( RandomUniform as random_uniform, ) from keras.src.initializers.random_initializers import TruncatedNormal from keras.src.initializers.random_initializers import ( TruncatedNormal as truncated_normal, ) from keras.src.initializers.random_initializers import VarianceScaling from keras.src.initializers.random_initializers import ( VarianceScaling as variance_scaling, )
"""**Tracers** are classes for tracing runs. **Class hierarchy:** .. code-block:: BaseCallbackHandler --> BaseTracer --> <name>Tracer # Examples: LangChainTracer, RootListenersTracer --> <name> # Examples: LogStreamCallbackHandler """ # noqa: E501 from typing import TYPE_CHECKING from langchain_core._import_utils import import_attr if TYPE_CHECKING: from langchain_core.tracers.base import BaseTracer from langchain_core.tracers.evaluation import EvaluatorCallbackHandler from langchain_core.tracers.langchain import LangChainTracer from langchain_core.tracers.log_stream import ( LogStreamCallbackHandler, RunLog, RunLogPatch, ) from langchain_core.tracers.schemas import Run from langchain_core.tracers.stdout import ConsoleCallbackHandler __all__ = ( "BaseTracer", "EvaluatorCallbackHandler", "LangChainTracer", "ConsoleCallbackHandler", "Run", "RunLog", "RunLogPatch", "LogStreamCallbackHandler", ) _dynamic_imports = { "BaseTracer": "base", "EvaluatorCallbackHandler": "evaluation", "LangChainTracer": "langchain", "LogStreamCallbackHandler": "log_stream", "RunLog": "log_stream", "RunLogPatch": "log_stream", "Run": "schemas", "ConsoleCallbackHandler": "stdout", } def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) result = import_attr(attr_name, module_name, __spec__.parent) globals()[attr_name] = result return result def __dir__() -> list[str]: return list(__all__)
"""**Tracers** are classes for tracing runs. **Class hierarchy:** .. code-block:: BaseCallbackHandler --> BaseTracer --> <name>Tracer # Examples: LangChainTracer, RootListenersTracer --> <name> # Examples: LogStreamCallbackHandler """ # noqa: E501 from importlib import import_module from typing import TYPE_CHECKING if TYPE_CHECKING: from langchain_core.tracers.base import BaseTracer from langchain_core.tracers.evaluation import EvaluatorCallbackHandler from langchain_core.tracers.langchain import LangChainTracer from langchain_core.tracers.log_stream import ( LogStreamCallbackHandler, RunLog, RunLogPatch, ) from langchain_core.tracers.schemas import Run from langchain_core.tracers.stdout import ConsoleCallbackHandler __all__ = [ "BaseTracer", "EvaluatorCallbackHandler", "LangChainTracer", "ConsoleCallbackHandler", "Run", "RunLog", "RunLogPatch", "LogStreamCallbackHandler", ] _dynamic_imports = { "BaseTracer": "base", "EvaluatorCallbackHandler": "evaluation", "LangChainTracer": "langchain", "LogStreamCallbackHandler": "log_stream", "RunLog": "log_stream", "RunLogPatch": "log_stream", "Run": "schemas", "ConsoleCallbackHandler": "stdout", } def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) package = __spec__.parent if module_name == "__module__" or module_name is None: result = import_module(f".{attr_name}", package=package) else: module = import_module(f".{module_name}", package=package) result = getattr(module, attr_name) globals()[attr_name] = result return result def __dir__() -> list[str]: return list(__all__)
from autogpt_libs.auth.depends import requires_user from autogpt_libs.auth.models import User from fastapi import Depends from backend.util.settings import Settings settings = Settings() def get_user_id(user: User = Depends(requires_user)) -> str: return user.user_id
from autogpt_libs.auth.middleware import auth_middleware from fastapi import Depends, HTTPException from backend.data.user import DEFAULT_USER_ID from backend.util.settings import Settings settings = Settings() def get_user_id(payload: dict = Depends(auth_middleware)) -> str: if not payload: # This handles the case when authentication is disabled return DEFAULT_USER_ID user_id = payload.get("sub") if not user_id: raise HTTPException(status_code=401, detail="User ID not found in token") return user_id
import time from functools import partial from huggingface_hub import HfApi, hf_hub_url from packaging import version from requests import ConnectionError, HTTPError from .. import config from . import logging logger = logging.get_logger(__name__) # Retry `preupload_lfs_files` in `huggingface_hub<0.20.0` on the "500 (Internal Server Error)" and "503 (Service Unavailable)" HTTP errors if config.HF_HUB_VERSION < version.parse("0.20.0"): def preupload_lfs_files(hf_api: HfApi, **kwargs): max_retries = 5 base_wait_time = 1 max_wait_time = 8 retry = 0 while True: try: hf_api.preupload_lfs_files(**kwargs) except (RuntimeError, HTTPError, ConnectionError) as err: if isinstance(err, RuntimeError): if isinstance(err.__cause__, (HTTPError, ConnectionError)): err = err.__cause__ else: raise err if retry >= max_retries or err.response and err.response.status_code not in [500, 503]: raise err else: sleep_time = min(max_wait_time, base_wait_time * 2**retry) # Exponential backoff logger.info( f"{hf_api.preupload_lfs_files} timed out, retrying in {sleep_time}s... [{retry/max_retries}]" ) time.sleep(sleep_time) retry += 1 else: break else: def preupload_lfs_files(hf_api: HfApi, **kwargs): hf_api.preupload_lfs_files(**kwargs) # bakckward compatibility hf_hub_url = partial(hf_hub_url, repo_type="dataset")
import time from functools import partial from huggingface_hub import HfApi, hf_hub_url from packaging import version from requests import HTTPError from .. import config from . import logging logger = logging.get_logger(__name__) # Retry `preupload_lfs_files` in `huggingface_hub<0.20.0` on the "500 (Internal Server Error)" and "503 (Service Unavailable)" HTTP errors if config.HF_HUB_VERSION < version.parse("0.20.0"): def preupload_lfs_files(hf_api: HfApi, **kwargs): max_retries = 5 base_wait_time = 1 max_wait_time = 8 status_codes = [500, 503] retry = 0 while True: try: hf_api.preupload_lfs_files(**kwargs) except (RuntimeError, HTTPError) as err: if isinstance(err, RuntimeError) and isinstance(err.__cause__, HTTPError): err = err.__cause__ if retry >= max_retries or err.response.status_code not in status_codes: raise err else: sleep_time = min(max_wait_time, base_wait_time * 2**retry) # Exponential backoff logger.info( f"{hf_api.preupload_lfs_files} timed out, retrying in {sleep_time}s... [{retry/max_retries}]" ) time.sleep(sleep_time) retry += 1 else: break else: def preupload_lfs_files(hf_api: HfApi, **kwargs): hf_api.preupload_lfs_files(**kwargs) # bakckward compatibility hf_hub_url = partial(hf_hub_url, repo_type="dataset")
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.callbacks.sagemaker_callback import ( SageMakerCallbackHandler, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "SageMakerCallbackHandler": "langchain_community.callbacks.sagemaker_callback", } _import_attribute = create_importer(__file__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "SageMakerCallbackHandler", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.callbacks.sagemaker_callback import ( SageMakerCallbackHandler, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "SageMakerCallbackHandler": "langchain_community.callbacks.sagemaker_callback" } _import_attribute = create_importer(__file__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "SageMakerCallbackHandler", ]
# Copyright (c) OpenMMLab. All rights reserved. import torch from mmdet.core import bbox2result from ..builder import DETECTORS, build_head from .single_stage import SingleStageDetector @DETECTORS.register_module() class YOLACT(SingleStageDetector): """Implementation of `YOLACT <https://arxiv.org/abs/1904.02689>`_""" def __init__(self, backbone, neck, bbox_head, segm_head, mask_head, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(YOLACT, self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg) self.segm_head = build_head(segm_head) self.mask_head = build_head(mask_head) def forward_dummy(self, img): """Used for computing network flops. See `mmdetection/tools/analysis_tools/get_flops.py` """ raise NotImplementedError def forward_train(self, img, img_metas, gt_bboxes, gt_labels, gt_bboxes_ignore=None, gt_masks=None): """ Args: img (Tensor): of shape (N, C, H, W) encoding input images. Typically these should be mean centered and std scaled. img_metas (list[dict]): list of image info dict where each dict has: 'img_shape', 'scale_factor', 'flip', and may also contain 'filename', 'ori_shape', 'pad_shape', and 'img_norm_cfg'. For details on the values of these keys see `mmdet/datasets/pipelines/formatting.py:Collect`. gt_bboxes (list[Tensor]): Ground truth bboxes for each image with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format. gt_labels (list[Tensor]): class indices corresponding to each box gt_bboxes_ignore (None | list[Tensor]): specify which bounding boxes can be ignored when computing the loss. gt_masks (None | Tensor) : true segmentation masks for each box used if the architecture supports a segmentation task. Returns: dict[str, Tensor]: a dictionary of loss components """ # convert Bitmap mask or Polygon Mask to Tensor here gt_masks = [ gt_mask.to_tensor(dtype=torch.uint8, device=img.device) for gt_mask in gt_masks ] x = self.extract_feat(img) cls_score, bbox_pred, coeff_pred = self.bbox_head(x) bbox_head_loss_inputs = (cls_score, bbox_pred) + (gt_bboxes, gt_labels, img_metas) losses, sampling_results = self.bbox_head.loss( *bbox_head_loss_inputs, gt_bboxes_ignore=gt_bboxes_ignore) segm_head_outs = self.segm_head(x[0]) loss_segm = self.segm_head.loss(segm_head_outs, gt_masks, gt_labels) losses.update(loss_segm) mask_pred = self.mask_head(x[0], coeff_pred, gt_bboxes, img_metas, sampling_results) loss_mask = self.mask_head.loss(mask_pred, gt_masks, gt_bboxes, img_metas, sampling_results) losses.update(loss_mask) # check NaN and Inf for loss_name in losses.keys(): assert torch.isfinite(torch.stack(losses[loss_name]))\ .all().item(), '{} becomes infinite or NaN!'\ .format(loss_name) return losses def simple_test(self, img, img_metas, rescale=False): """Test function without test-time augmentation.""" feat = self.extract_feat(img) det_bboxes, det_labels, det_coeffs = self.bbox_head.simple_test( feat, img_metas, rescale=rescale) bbox_results = [ bbox2result(det_bbox, det_label, self.bbox_head.num_classes) for det_bbox, det_label in zip(det_bboxes, det_labels) ] segm_results = self.mask_head.simple_test( feat, det_bboxes, det_labels, det_coeffs, img_metas, rescale=rescale) return list(zip(bbox_results, segm_results)) def aug_test(self, imgs, img_metas, rescale=False): """Test with augmentations.""" raise NotImplementedError( 'YOLACT does not support test-time augmentation')
import torch from mmdet.core import bbox2result from ..builder import DETECTORS, build_head from .single_stage import SingleStageDetector @DETECTORS.register_module() class YOLACT(SingleStageDetector): """Implementation of `YOLACT <https://arxiv.org/abs/1904.02689>`_""" def __init__(self, backbone, neck, bbox_head, segm_head, mask_head, train_cfg=None, test_cfg=None, pretrained=None, init_cfg=None): super(YOLACT, self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, pretrained, init_cfg) self.segm_head = build_head(segm_head) self.mask_head = build_head(mask_head) def forward_dummy(self, img): """Used for computing network flops. See `mmdetection/tools/analysis_tools/get_flops.py` """ raise NotImplementedError def forward_train(self, img, img_metas, gt_bboxes, gt_labels, gt_bboxes_ignore=None, gt_masks=None): """ Args: img (Tensor): of shape (N, C, H, W) encoding input images. Typically these should be mean centered and std scaled. img_metas (list[dict]): list of image info dict where each dict has: 'img_shape', 'scale_factor', 'flip', and may also contain 'filename', 'ori_shape', 'pad_shape', and 'img_norm_cfg'. For details on the values of these keys see `mmdet/datasets/pipelines/formatting.py:Collect`. gt_bboxes (list[Tensor]): Ground truth bboxes for each image with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format. gt_labels (list[Tensor]): class indices corresponding to each box gt_bboxes_ignore (None | list[Tensor]): specify which bounding boxes can be ignored when computing the loss. gt_masks (None | Tensor) : true segmentation masks for each box used if the architecture supports a segmentation task. Returns: dict[str, Tensor]: a dictionary of loss components """ # convert Bitmap mask or Polygon Mask to Tensor here gt_masks = [ gt_mask.to_tensor(dtype=torch.uint8, device=img.device) for gt_mask in gt_masks ] x = self.extract_feat(img) cls_score, bbox_pred, coeff_pred = self.bbox_head(x) bbox_head_loss_inputs = (cls_score, bbox_pred) + (gt_bboxes, gt_labels, img_metas) losses, sampling_results = self.bbox_head.loss( *bbox_head_loss_inputs, gt_bboxes_ignore=gt_bboxes_ignore) segm_head_outs = self.segm_head(x[0]) loss_segm = self.segm_head.loss(segm_head_outs, gt_masks, gt_labels) losses.update(loss_segm) mask_pred = self.mask_head(x[0], coeff_pred, gt_bboxes, img_metas, sampling_results) loss_mask = self.mask_head.loss(mask_pred, gt_masks, gt_bboxes, img_metas, sampling_results) losses.update(loss_mask) # check NaN and Inf for loss_name in losses.keys(): assert torch.isfinite(torch.stack(losses[loss_name]))\ .all().item(), '{} becomes infinite or NaN!'\ .format(loss_name) return losses def simple_test(self, img, img_metas, rescale=False): """Test function without test-time augmentation.""" feat = self.extract_feat(img) det_bboxes, det_labels, det_coeffs = self.bbox_head.simple_test( feat, img_metas, rescale=rescale) bbox_results = [ bbox2result(det_bbox, det_label, self.bbox_head.num_classes) for det_bbox, det_label in zip(det_bboxes, det_labels) ] segm_results = self.mask_head.simple_test( feat, det_bboxes, det_labels, det_coeffs, img_metas, rescale=rescale) return list(zip(bbox_results, segm_results)) def aug_test(self, imgs, img_metas, rescale=False): """Test with augmentations.""" raise NotImplementedError( 'YOLACT does not support test-time augmentation')
#!/usr/bin/env python3 # Owner(s): ["oncall: distributed"] import sys import torch import torch.distributed as dist if not dist.is_available(): print("Distributed not available, skipping tests", file=sys.stderr) sys.exit(0) from torch.testing._internal.common_utils import IS_CI, run_tests from torch.testing._internal.distributed.rpc.faulty_rpc_agent_test_fixture import ( FaultyRpcAgentTestFixture, ) from torch.testing._internal.distributed.rpc_utils import ( FAULTY_AGENT_TESTS, generate_tests, ) # On CircleCI these tests are already run on CPU jobs, thus to save resources do # not run them on GPU jobs, since they wouldn't provide additional test signal. if not (IS_CI and torch.cuda.is_available()): globals().update( generate_tests( "Faulty", FaultyRpcAgentTestFixture, FAULTY_AGENT_TESTS, __name__, ) ) if __name__ == "__main__": run_tests()
#!/usr/bin/env python3 # Owner(s): ["oncall: distributed"] import sys import torch import torch.distributed as dist if not dist.is_available(): print("Distributed not available, skipping tests", file=sys.stderr) sys.exit(0) from torch.testing._internal.common_utils import IS_CI, run_tests from torch.testing._internal.distributed.rpc.faulty_rpc_agent_test_fixture import ( FaultyRpcAgentTestFixture, ) from torch.testing._internal.distributed.rpc_utils import ( FAULTY_AGENT_TESTS, generate_tests, ) # On CircleCI these tests are already run on CPU jobs, thus to save resources do # not run them on GPU jobs, since thet wouldn't provide additional test signal. if not (IS_CI and torch.cuda.is_available()): globals().update( generate_tests( "Faulty", FaultyRpcAgentTestFixture, FAULTY_AGENT_TESTS, __name__, ) ) if __name__ == "__main__": run_tests()
"""Setup script.""" import os import pathlib from setuptools import find_packages from setuptools import setup def read(rel_path): here = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(here, rel_path)) as fp: return fp.read() def get_version(rel_path): for line in read(rel_path).splitlines(): if line.startswith("__version__"): delim = '"' if '"' in line else "'" return line.split(delim)[1] raise RuntimeError("Unable to find version string.") HERE = pathlib.Path(__file__).parent README = (HERE / "README.md").read_text() VERSION = get_version("keras/src/version.py") setup( name="keras", description="Multi-backend Keras.", long_description_content_type="text/markdown", long_description=README, version=VERSION, url="https://github.com/keras-team/keras", author="Keras team", author_email="[email protected]", license="Apache License 2.0", install_requires=[ "absl-py", "numpy", "rich", "namex", "h5py", "optree", "ml-dtypes", "packaging", ], # Supported Python versions python_requires=">=3.9", classifiers=[ "Development Status :: 4 - Beta", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3 :: Only", "Operating System :: Unix", "Operating System :: MacOS", "Intended Audience :: Science/Research", "Topic :: Scientific/Engineering", "Topic :: Software Development", ], packages=find_packages( include=("keras", "keras.*"), exclude=("*_test.py", "benchmarks"), ), )
"""Setup script.""" import os import pathlib from setuptools import find_packages from setuptools import setup def read(rel_path): here = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(here, rel_path)) as fp: return fp.read() def get_version(rel_path): for line in read(rel_path).splitlines(): if line.startswith("__version__"): delim = '"' if '"' in line else "'" return line.split(delim)[1] raise RuntimeError("Unable to find version string.") HERE = pathlib.Path(__file__).parent README = (HERE / "README.md").read_text() VERSION = get_version("keras/src/version.py") setup( name="keras", description="Multi-backend Keras.", long_description_content_type="text/markdown", long_description=README, version=VERSION, url="https://github.com/keras-team/keras", author="Keras team", author_email="[email protected]", license="Apache License 2.0", install_requires=[ "absl-py", "numpy", "rich", "namex", "h5py", "optree", "ml-dtypes", "packaging", ], # Supported Python versions python_requires=">=3.9", classifiers=[ "Development Status :: 4 - Beta", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3 :: Only", "Operating System :: Unix", "Operating System :: MacOS", "Intended Audience :: Science/Research", "Topic :: Scientific/Engineering", "Topic :: Software Development", ], packages=find_packages(exclude=("*_test.py",)), )
import base64 import hashlib from datetime import datetime, timedelta, timezone import os import os import jwt from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, load_pem_private_key, ) SPCS_TOKEN_PATH = "/snowflake/session/token" def get_default_spcs_token() -> str: """ Returns the value of the SnowPark default JWT Oauth Session Token. In a Snowpark Container Services environment, there is a 'default' oauth session token. This retrieves it for you (as a string). """ with open(SPCS_TOKEN_PATH) as fp: return fp.read() def is_spcs_environment() -> bool: """ Determines if we're currently in an SPCS (Snowpark Container Services) environment. Does this by checking for the default session token. Returns a boolean: whether or not we're in an SPCS environment. """ return ( os.path.exists(SPCS_TOKEN_PATH) and os.environ.get("SNOWFLAKE_HOST") is not None ) def get_spcs_base_url() -> str: """ Returns a correctly formatted URL for making Snowflake API calls from within an SPCS environment. Raises a ValueError if not in an SPCS environment. Returns a string, https://{some-url} that you can affix an API endpoint such as Cortex to. """ if not is_spcs_environment(): raise ValueError("Cannot call get_spcs_base_url unless in an spcs environment.") return os.getenv("SNOWFLAKE_HOST") def generate_sf_jwt(sf_account: str, sf_user: str, sf_private_key_filepath: str) -> str: """ Generate a JSON Web Token for a Snowflake user. Args: sf_account: Fully qualified snowflake account name (ORG_ID-ACCOUNT_ID). sf_user: User to generate token for. sf_private_key_filepath: Path to user's private key. Returns: str: JSON Web Token """ with open(sf_private_key_filepath, "rb") as pem_in: pemlines = pem_in.read() # TODO: Add support for encrypted private keys private_key = load_pem_private_key(pemlines, None, default_backend()) # Get the raw bytes of the public key. public_key_raw = private_key.public_key().public_bytes( Encoding.DER, PublicFormat.SubjectPublicKeyInfo ) # Get the sha256 hash of the raw bytes. sha256hash = hashlib.sha256() sha256hash.update(public_key_raw) # Base64-encode the value and prepend the prefix 'SHA256:'. public_key_fp = "SHA256:" + base64.b64encode(sha256hash.digest()).decode("utf-8") # Use uppercase for the account identifier and user name. account = sf_account.upper() user = sf_user.upper() qualified_username = account + "." + user # Get the current time in order to specify the time when the JWT was issued and the expiration time of the JWT. now = datetime.now(timezone.utc) # Specify the length of time during which the JWT will be valid. You can specify at most 1 hour. lifetime = timedelta(minutes=59) # Create the payload for the token. payload = { # Set the issuer to the fully qualified username concatenated with the public key fingerprint (calculated in the previous step). "iss": qualified_username + "." + public_key_fp, # Set the subject to the fully qualified username. "sub": qualified_username, # Set the issue time to now. "iat": now, # Set the expiration time, based on the lifetime specified for this object. "exp": now + lifetime, } # Generate the JWT. private_key is the private key that you read from the private key file in the previous step when you generated the public key fingerprint. encoding_algorithm = "RS256" return jwt.encode(payload, key=private_key, algorithm=encoding_algorithm)
import base64 import hashlib from datetime import datetime, timedelta, timezone import os import jwt from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, load_pem_private_key, ) SPCS_TOKEN_PATH = "/snowflake/session/token" def get_default_spcs_token() -> str: """ Returns the value of the SnowPark default JWT Oauth Session Token. In a Snowpark Container Services environment, there is a 'default' oauth session token. This retrieves it for you (as a string). """ with open(SPCS_TOKEN_PATH) as fp: return fp.read() def is_spcs_environment() -> bool: """ Determines if we're currently in an SPCS (Snowpark Container Services) environment. Does this by checking for the default session token. Returns a boolean: whether or not we're in an SPCS environment. """ return ( os.path.exists(SPCS_TOKEN_PATH) and os.environ.get("SNOWFLAKE_HOST") is not None and os.environ.get("SNOWFLAKE_ACCOUNT") is not None ) def get_spcs_base_url() -> str: """ Returns a correctly formatted URL for making Snowflake API calls from within an SPCS environment. Raises a ValueError if not in an SPCS environment. Returns a string, https://{some-url} that you can affix an API endpoint such as Cortex to. """ if not is_spcs_environment(): raise ValueError("Cannot call get_spcs_base_url unless in an spcs environment.") return "https://" + os.environ.get("SNOWFLAKE_HOST").replace( "snowflake", os.environ.get("SNOWFLAKE_ACCOUNT").lower().replace("_", "-"), 1, ) def generate_sf_jwt(sf_account: str, sf_user: str, sf_private_key_filepath: str) -> str: """ Generate a JSON Web Token for a Snowflake user. Args: sf_account: Fully qualified snowflake account name (ORG_ID-ACCOUNT_ID). sf_user: User to generate token for. sf_private_key_filepath: Path to user's private key. Returns: str: JSON Web Token """ with open(sf_private_key_filepath, "rb") as pem_in: pemlines = pem_in.read() # TODO: Add support for encrypted private keys private_key = load_pem_private_key(pemlines, None, default_backend()) # Get the raw bytes of the public key. public_key_raw = private_key.public_key().public_bytes( Encoding.DER, PublicFormat.SubjectPublicKeyInfo ) # Get the sha256 hash of the raw bytes. sha256hash = hashlib.sha256() sha256hash.update(public_key_raw) # Base64-encode the value and prepend the prefix 'SHA256:'. public_key_fp = "SHA256:" + base64.b64encode(sha256hash.digest()).decode("utf-8") # Use uppercase for the account identifier and user name. account = sf_account.upper() user = sf_user.upper() qualified_username = account + "." + user # Get the current time in order to specify the time when the JWT was issued and the expiration time of the JWT. now = datetime.now(timezone.utc) # Specify the length of time during which the JWT will be valid. You can specify at most 1 hour. lifetime = timedelta(minutes=59) # Create the payload for the token. payload = { # Set the issuer to the fully qualified username concatenated with the public key fingerprint (calculated in the previous step). "iss": qualified_username + "." + public_key_fp, # Set the subject to the fully qualified username. "sub": qualified_username, # Set the issue time to now. "iat": now, # Set the expiration time, based on the lifetime specified for this object. "exp": now + lifetime, } # Generate the JWT. private_key is the private key that you read from the private key file in the previous step when you generated the public key fingerprint. encoding_algorithm = "RS256" return jwt.encode(payload, key=private_key, algorithm=encoding_algorithm)
# Copyright (c) OpenMMLab. All rights reserved. import argparse import subprocess import torch from mmengine.logging import print_log from mmengine.utils import digit_version def parse_args(): parser = argparse.ArgumentParser( description='Process a checkpoint to be published') parser.add_argument('in_file', help='input checkpoint filename') parser.add_argument('out_file', help='output checkpoint filename') parser.add_argument( '--save-keys', nargs='+', type=str, default=['meta', 'state_dict'], help='keys to save in the published checkpoint') args = parser.parse_args() return args def process_checkpoint(in_file, out_file, save_keys=['meta', 'state_dict']): checkpoint = torch.load(in_file, map_location='cpu') # only keep `meta` and `state_dict` for smaller file size ckpt_keys = list(checkpoint.keys()) for k in ckpt_keys: if k not in save_keys: print_log( f'Key `{k}` will be removed because it is not in ' f'save_keys. If you want to keep it, ' f'please set --save-keys.', logger='current') checkpoint.pop(k, None) # if it is necessary to remove some sensitive data in checkpoint['meta'], # add the code here. if digit_version(torch.__version__) >= digit_version('1.6'): torch.save(checkpoint, out_file, _use_new_zipfile_serialization=False) else: torch.save(checkpoint, out_file) sha = subprocess.check_output(['sha256sum', out_file]).decode() if out_file.endswith('.pth'): out_file_name = out_file[:-4] else: out_file_name = out_file final_file = out_file_name + f'-{sha[:8]}.pth' subprocess.Popen(['mv', out_file, final_file]) print_log( f'The published model is saved at {final_file}.', logger='current') def main(): args = parse_args() process_checkpoint(args.in_file, args.out_file, args.save_keys) if __name__ == '__main__': main()
# Copyright (c) OpenMMLab. All rights reserved. import argparse import subprocess import torch from mmengine.logging import print_log def parse_args(): parser = argparse.ArgumentParser( description='Process a checkpoint to be published') parser.add_argument('in_file', help='input checkpoint filename') parser.add_argument('out_file', help='output checkpoint filename') parser.add_argument( '--save-keys', nargs='+', type=str, default=['meta', 'state_dict'], help='keys to save in the published checkpoint') args = parser.parse_args() return args def process_checkpoint(in_file, out_file, save_keys=['meta', 'state_dict']): checkpoint = torch.load(in_file, map_location='cpu') # only keep `meta` and `state_dict` for smaller file size ckpt_keys = list(checkpoint.keys()) for k in ckpt_keys: if k not in save_keys: print_log( f'Key `{k}` will be removed because it is not in ' f'save_keys. If you want to keep it, ' f'please set --save-keys.', logger='current') checkpoint.pop(k, None) # if it is necessary to remove some sensitive data in checkpoint['meta'], # add the code here. if torch.__version__ >= '1.6': torch.save(checkpoint, out_file, _use_new_zipfile_serialization=False) else: torch.save(checkpoint, out_file) sha = subprocess.check_output(['sha256sum', out_file]).decode() if out_file.endswith('.pth'): out_file_name = out_file[:-4] else: out_file_name = out_file final_file = out_file_name + f'-{sha[:8]}.pth' subprocess.Popen(['mv', out_file, final_file]) print_log( f'The published model is saved at {final_file}.', logger='current') def main(): args = parse_args() process_checkpoint(args.in_file, args.out_file, args.save_keys) if __name__ == '__main__': main()
# THIS FILE HAS BEEN AUTOGENERATED. To update: # 1. modify the `_deps` dict in setup.py # 2. run `make deps_table_update` deps = { "Pillow": "Pillow", "accelerate": "accelerate>=0.31.0", "compel": "compel==0.1.8", "datasets": "datasets", "filelock": "filelock", "flax": "flax>=0.4.1", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.27.0", "requests-mock": "requests-mock==1.10.0", "importlib_metadata": "importlib_metadata", "invisible-watermark": "invisible-watermark>=0.2.0", "isort": "isort>=5.5.4", "jax": "jax>=0.4.1", "jaxlib": "jaxlib>=0.4.1", "Jinja2": "Jinja2", "k-diffusion": "k-diffusion>=0.0.12", "torchsde": "torchsde", "note_seq": "note_seq", "librosa": "librosa", "numpy": "numpy", "parameterized": "parameterized", "peft": "peft>=0.6.0", "protobuf": "protobuf>=3.20.3,<4", "pytest": "pytest", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "python": "python>=3.8.0", "ruff": "ruff==0.1.5", "safetensors": "safetensors>=0.3.1", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "GitPython": "GitPython<3.1.19", "scipy": "scipy", "onnx": "onnx", "regex": "regex!=2019.12.17", "requests": "requests", "tensorboard": "tensorboard", "torch": "torch>=1.4", "torchvision": "torchvision", "transformers": "transformers>=4.41.2", "urllib3": "urllib3<=2.0.0", "black": "black", "phonemizer": "phonemizer", }
# THIS FILE HAS BEEN AUTOGENERATED. To update: # 1. modify the `_deps` dict in setup.py # 2. run `make deps_table_update` deps = { "Pillow": "Pillow", "accelerate": "accelerate>=0.31.0", "compel": "compel==0.1.8", "datasets": "datasets", "filelock": "filelock", "flax": "flax>=0.4.1", "hf-doc-builder": "hf-doc-builder>=0.3.0", "huggingface-hub": "huggingface-hub>=0.23.2", "requests-mock": "requests-mock==1.10.0", "importlib_metadata": "importlib_metadata", "invisible-watermark": "invisible-watermark>=0.2.0", "isort": "isort>=5.5.4", "jax": "jax>=0.4.1", "jaxlib": "jaxlib>=0.4.1", "Jinja2": "Jinja2", "k-diffusion": "k-diffusion>=0.0.12", "torchsde": "torchsde", "note_seq": "note_seq", "librosa": "librosa", "numpy": "numpy", "parameterized": "parameterized", "peft": "peft>=0.6.0", "protobuf": "protobuf>=3.20.3,<4", "pytest": "pytest", "pytest-timeout": "pytest-timeout", "pytest-xdist": "pytest-xdist", "python": "python>=3.8.0", "ruff": "ruff==0.1.5", "safetensors": "safetensors>=0.3.1", "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92", "GitPython": "GitPython<3.1.19", "scipy": "scipy", "onnx": "onnx", "regex": "regex!=2019.12.17", "requests": "requests", "tensorboard": "tensorboard", "torch": "torch>=1.4", "torchvision": "torchvision", "transformers": "transformers>=4.41.2", "urllib3": "urllib3<=2.0.0", "black": "black", "phonemizer": "phonemizer", }
from ._transforms import BarkScale, BarkSpectrogram, Convolve, FFTConvolve, InverseBarkScale, Speed, SpeedPerturbation __all__ = [ "BarkScale", "BarkSpectrogram", "Convolve", "FFTConvolve", "InverseBarkScale", "SpeedPerturbation", "Speed", ]
from ._transforms import BarkScale, BarkSpectrogram, Convolve, FFTConvolve, InverseBarkScale __all__ = [ "BarkScale", "BarkSpectrogram", "Convolve", "FFTConvolve", "InverseBarkScale", ]
from __future__ import annotations import os import tempfile def is_ci() -> bool: """ Check if the code is running in a Continuous Integration (CI) environment. This is determined by checking for the presence of certain environment variables. """ return "GITHUB_ACTIONS" in os.environ class SafeTemporaryDirectory(tempfile.TemporaryDirectory): """ The GitHub Actions CI on Windows sometimes raises a NotADirectoryError when cleaning up the temporary directory. This class is a workaround to avoid the error. Unlike tempfile.TemporaryDirectory(ignore_cleanup_errors=True), this also works on Python 3.9. """ def __init__(self, *args, **kwargs) -> None: kwargs["ignore_cleanup_errors"] = True try: super().__init__(*args, **kwargs) except TypeError: del kwargs["ignore_cleanup_errors"] super().__init__(*args, **kwargs) def __exit__(self, *args, **kwargs): try: super().__exit__(*args, **kwargs) except NotADirectoryError: pass
from __future__ import annotations import tempfile class SafeTemporaryDirectory(tempfile.TemporaryDirectory): """ The GitHub Actions CI on Windows sometimes raises a NotADirectoryError when cleaning up the temporary directory. This class is a workaround to avoid the error. Unlike tempfile.TemporaryDirectory(ignore_cleanup_errors=True), this also works on Python 3.9. """ def __init__(self, *args, **kwargs) -> None: kwargs["ignore_cleanup_errors"] = True try: super().__init__(*args, **kwargs) except TypeError: del kwargs["ignore_cleanup_errors"] super().__init__(*args, **kwargs) def __exit__(self, *args, **kwargs): try: super().__exit__(*args, **kwargs) except NotADirectoryError: pass
from __future__ import annotations import argparse import os import re import subprocess from pathlib import Path from setuptools import distutils # type: ignore[import,attr-defined] UNKNOWN = "Unknown" RELEASE_PATTERN = re.compile(r"/v[0-9]+(\.[0-9]+)*(-rc[0-9]+)?/") def get_sha(pytorch_root: str | Path) -> str: try: rev = None if os.path.exists(os.path.join(pytorch_root, ".git")): rev = subprocess.check_output( ["git", "rev-parse", "HEAD"], cwd=pytorch_root ) elif os.path.exists(os.path.join(pytorch_root, ".hg")): rev = subprocess.check_output( ["hg", "identify", "-r", "."], cwd=pytorch_root ) if rev: return rev.decode("ascii").strip() except Exception: pass return UNKNOWN def get_tag(pytorch_root: str | Path) -> str: try: tag = subprocess.run( ["git", "describe", "--tags", "--exact"], cwd=pytorch_root, encoding="ascii", capture_output=True, ).stdout.strip() if RELEASE_PATTERN.match(tag): return tag else: return UNKNOWN except Exception: return UNKNOWN def get_torch_version(sha: str | None = None) -> str: pytorch_root = Path(__file__).absolute().parent.parent version = open(pytorch_root / "version.txt").read().strip() if os.getenv("PYTORCH_BUILD_VERSION"): assert os.getenv("PYTORCH_BUILD_NUMBER") is not None build_number = int(os.getenv("PYTORCH_BUILD_NUMBER", "")) version = os.getenv("PYTORCH_BUILD_VERSION", "") if build_number > 1: version += ".post" + str(build_number) elif sha != UNKNOWN: if sha is None: sha = get_sha(pytorch_root) version += "+git" + sha[:7] return version if __name__ == "__main__": parser = argparse.ArgumentParser( description="Generate torch/version.py from build and environment metadata." ) parser.add_argument( "--is-debug", "--is_debug", type=distutils.util.strtobool, help="Whether this build is debug mode or not.", ) parser.add_argument("--cuda-version", "--cuda_version", type=str) parser.add_argument("--hip-version", "--hip_version", type=str) parser.add_argument("--xpu-version", "--xpu_version", type=str) args = parser.parse_args() assert args.is_debug is not None args.cuda_version = None if args.cuda_version == "" else args.cuda_version args.hip_version = None if args.hip_version == "" else args.hip_version args.xpu_version = None if args.xpu_version == "" else args.xpu_version pytorch_root = Path(__file__).parent.parent version_path = pytorch_root / "torch" / "version.py" # Attempt to get tag first, fall back to sha if a tag was not found tagged_version = get_tag(pytorch_root) sha = get_sha(pytorch_root) if tagged_version == UNKNOWN: version = get_torch_version(sha) else: version = tagged_version with open(version_path, "w") as f: f.write("from typing import Optional\n\n") f.write( "__all__ = ['__version__', 'debug', 'cuda', 'git_version', 'hip', 'xpu']\n" ) f.write(f"__version__ = '{version}'\n") # NB: This is not 100% accurate, because you could have built the # library code with DEBUG, but csrc without DEBUG (in which case # this would claim to be a release build when it's not.) f.write(f"debug = {repr(bool(args.is_debug))}\n") f.write(f"cuda: Optional[str] = {repr(args.cuda_version)}\n") f.write(f"git_version = {repr(sha)}\n") f.write(f"hip: Optional[str] = {repr(args.hip_version)}\n") f.write(f"xpu: Optional[str] = {repr(args.xpu_version)}\n")
from __future__ import annotations import argparse import os import re import subprocess from pathlib import Path from setuptools import distutils # type: ignore[import] UNKNOWN = "Unknown" RELEASE_PATTERN = re.compile(r"/v[0-9]+(\.[0-9]+)*(-rc[0-9]+)?/") def get_sha(pytorch_root: str | Path) -> str: try: rev = None if os.path.exists(os.path.join(pytorch_root, ".git")): rev = subprocess.check_output( ["git", "rev-parse", "HEAD"], cwd=pytorch_root ) elif os.path.exists(os.path.join(pytorch_root, ".hg")): rev = subprocess.check_output( ["hg", "identify", "-r", "."], cwd=pytorch_root ) if rev: return rev.decode("ascii").strip() except Exception: pass return UNKNOWN def get_tag(pytorch_root: str | Path) -> str: try: tag = subprocess.run( ["git", "describe", "--tags", "--exact"], cwd=pytorch_root, encoding="ascii", capture_output=True, ).stdout.strip() if RELEASE_PATTERN.match(tag): return tag else: return UNKNOWN except Exception: return UNKNOWN def get_torch_version(sha: str | None = None) -> str: pytorch_root = Path(__file__).absolute().parent.parent version = open(pytorch_root / "version.txt").read().strip() if os.getenv("PYTORCH_BUILD_VERSION"): assert os.getenv("PYTORCH_BUILD_NUMBER") is not None build_number = int(os.getenv("PYTORCH_BUILD_NUMBER", "")) version = os.getenv("PYTORCH_BUILD_VERSION", "") if build_number > 1: version += ".post" + str(build_number) elif sha != UNKNOWN: if sha is None: sha = get_sha(pytorch_root) version += "+git" + sha[:7] return version if __name__ == "__main__": parser = argparse.ArgumentParser( description="Generate torch/version.py from build and environment metadata." ) parser.add_argument( "--is-debug", "--is_debug", type=distutils.util.strtobool, help="Whether this build is debug mode or not.", ) parser.add_argument("--cuda-version", "--cuda_version", type=str) parser.add_argument("--hip-version", "--hip_version", type=str) parser.add_argument("--xpu-version", "--xpu_version", type=str) args = parser.parse_args() assert args.is_debug is not None args.cuda_version = None if args.cuda_version == "" else args.cuda_version args.hip_version = None if args.hip_version == "" else args.hip_version args.xpu_version = None if args.xpu_version == "" else args.xpu_version pytorch_root = Path(__file__).parent.parent version_path = pytorch_root / "torch" / "version.py" # Attempt to get tag first, fall back to sha if a tag was not found tagged_version = get_tag(pytorch_root) sha = get_sha(pytorch_root) if tagged_version == UNKNOWN: version = get_torch_version(sha) else: version = tagged_version with open(version_path, "w") as f: f.write("from typing import Optional\n\n") f.write( "__all__ = ['__version__', 'debug', 'cuda', 'git_version', 'hip', 'xpu']\n" ) f.write(f"__version__ = '{version}'\n") # NB: This is not 100% accurate, because you could have built the # library code with DEBUG, but csrc without DEBUG (in which case # this would claim to be a release build when it's not.) f.write(f"debug = {repr(bool(args.is_debug))}\n") f.write(f"cuda: Optional[str] = {repr(args.cuda_version)}\n") f.write(f"git_version = {repr(sha)}\n") f.write(f"hip: Optional[str] = {repr(args.hip_version)}\n") f.write(f"xpu: Optional[str] = {repr(args.xpu_version)}\n")
_base_ = [ '../_base_/models/faster-rcnn_r50_fpn.py', '../_base_/datasets/openimages_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict(roi_head=dict(bbox_head=dict(num_classes=601))) # Using 32 GPUS while training optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001), clip_grad=dict(max_norm=35, norm_type=2)) # learning rate param_scheduler = [ dict( type='LinearLR', start_factor=1.0 / 64, by_epoch=False, begin=0, end=26000), dict( type='MultiStepLR', begin=0, end=12, by_epoch=True, milestones=[8, 11], gamma=0.1) ] # 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_/models/faster_rcnn_r50_fpn.py', '../_base_/datasets/openimages_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] model = dict(roi_head=dict(bbox_head=dict(num_classes=601))) # Using 32 GPUS while training optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001), clip_grad=dict(max_norm=35, norm_type=2)) # learning rate param_scheduler = [ dict( type='LinearLR', start_factor=1.0 / 64, by_epoch=False, begin=0, end=26000), dict( type='MultiStepLR', begin=0, end=12, by_epoch=True, milestones=[8, 11], gamma=0.1) ] # 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)
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os from pytest_mock import MockerFixture from torch import hub from ...torch_encoder import ImageTorchEncoder def test_load_from_url(tmpdir: str, mocker: MockerFixture) -> None: os.environ['TORCH_HOME'] = str(tmpdir) spy = mocker.spy(hub, 'urlopen') _ = ImageTorchEncoder(model_name='mobilenet_v2') assert os.path.isfile( os.path.join(tmpdir, 'hub', 'checkpoints', 'mobilenet_v2-b0353104.pth') ) assert spy.call_count == 1
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os from torch import hub from pytest_mock import MockerFixture from ...torch_encoder import ImageTorchEncoder def test_load_from_url(tmpdir: str, mocker: MockerFixture) -> None: os.environ['TORCH_HOME'] = str(tmpdir) spy = mocker.spy(hub, 'urlopen') _ = ImageTorchEncoder(model_name='mobilenet_v2') assert os.path.isfile(os.path.join(tmpdir, 'hub', 'checkpoints', 'mobilenet_v2-b0353104.pth')) assert spy.call_count == 1
from typing import Any, Dict, Union import torch from torchvision import datapoints, transforms as _transforms from torchvision.transforms.v2 import functional as F, Transform from .utils import is_simple_tensor class ConvertBoundingBoxFormat(Transform): """[BETA] Convert bounding box coordinates to the given ``format``, eg from "CXCYWH" to "XYXY". .. betastatus:: ConvertBoundingBoxFormat transform Args: format (str or datapoints.BoundingBoxFormat): output bounding box format. Possible values are defined by :class:`~torchvision.datapoints.BoundingBoxFormat` and string values match the enums, e.g. "XYXY" or "XYWH" etc. """ _transformed_types = (datapoints.BoundingBox,) def __init__(self, format: Union[str, datapoints.BoundingBoxFormat]) -> None: super().__init__() if isinstance(format, str): format = datapoints.BoundingBoxFormat[format] self.format = format def _transform(self, inpt: datapoints.BoundingBox, params: Dict[str, Any]) -> datapoints.BoundingBox: return F.convert_format_bounding_box(inpt, new_format=self.format) # type: ignore[return-value] class ConvertDtype(Transform): """[BETA] Convert input image or video to the given ``dtype`` and scale the values accordingly. .. betastatus:: ConvertDtype transform This function does not support PIL Image. Args: dtype (torch.dtype): Desired data type of the output .. note:: When converting from a smaller to a larger integer ``dtype`` the maximum values are **not** mapped exactly. If converted back and forth, this mismatch has no effect. Raises: RuntimeError: When trying to cast :class:`torch.float32` to :class:`torch.int32` or :class:`torch.int64` as well as for trying to cast :class:`torch.float64` to :class:`torch.int64`. These conversions might lead to overflow errors since the floating point ``dtype`` cannot store consecutive integers over the whole range of the integer ``dtype``. """ _v1_transform_cls = _transforms.ConvertImageDtype _transformed_types = (is_simple_tensor, datapoints.Image, datapoints.Video) def __init__(self, dtype: torch.dtype = torch.float32) -> None: super().__init__() self.dtype = dtype def _transform( self, inpt: Union[datapoints._TensorImageType, datapoints._TensorVideoType], params: Dict[str, Any] ) -> Union[datapoints._TensorImageType, datapoints._TensorVideoType]: return F.convert_dtype(inpt, self.dtype) # We changed the name to align it with the new naming scheme. Still, `ConvertImageDtype` is # prevalent and well understood. Thus, we just alias it without deprecating the old name. ConvertImageDtype = ConvertDtype class ClampBoundingBox(Transform): """[BETA] Clamp bounding boxes to their corresponding image dimensions. The clamping is done according to the bounding boxes' ``spatial_size`` meta-data. .. betastatus:: ClampBoundingBox transform """ _transformed_types = (datapoints.BoundingBox,) def _transform(self, inpt: datapoints.BoundingBox, params: Dict[str, Any]) -> datapoints.BoundingBox: return F.clamp_bounding_box(inpt) # type: ignore[return-value]
from typing import Any, Dict, Union import torch from torchvision import datapoints, transforms as _transforms from torchvision.transforms.v2 import functional as F, Transform from .utils import is_simple_tensor class ConvertBoundingBoxFormat(Transform): """[BETA] Convert bounding box coordinates to the given ``format``, e.g. from "CXCYWH" to "XYXY". .. betastatus:: ConvertBoundingBoxFormat transform Args: format (str or datapoints.BoundingBoxFormat): output bounding box format. Possible values are defined by :class:`~torchvision.datapoints.BoundingBoxFormat` and string values match the enums, e.g. "XYXY" or "XYWH" etc. """ _transformed_types = (datapoints.BoundingBox,) def __init__(self, format: Union[str, datapoints.BoundingBoxFormat]) -> None: super().__init__() if isinstance(format, str): format = datapoints.BoundingBoxFormat[format] self.format = format def _transform(self, inpt: datapoints.BoundingBox, params: Dict[str, Any]) -> datapoints.BoundingBox: return F.convert_format_bounding_box(inpt, new_format=self.format) # type: ignore[return-value] class ConvertDtype(Transform): """[BETA] Convert input image or video to the given ``dtype`` and scale the values accordingly. .. betastatus:: ConvertDtype transform This function does not support PIL Image. Args: dtype (torch.dtype): Desired data type of the output .. note:: When converting from a smaller to a larger integer ``dtype`` the maximum values are **not** mapped exactly. If converted back and forth, this mismatch has no effect. Raises: RuntimeError: When trying to cast :class:`torch.float32` to :class:`torch.int32` or :class:`torch.int64` as well as for trying to cast :class:`torch.float64` to :class:`torch.int64`. These conversions might lead to overflow errors since the floating point ``dtype`` cannot store consecutive integers over the whole range of the integer ``dtype``. """ _v1_transform_cls = _transforms.ConvertImageDtype _transformed_types = (is_simple_tensor, datapoints.Image, datapoints.Video) def __init__(self, dtype: torch.dtype = torch.float32) -> None: super().__init__() self.dtype = dtype def _transform( self, inpt: Union[datapoints._TensorImageType, datapoints._TensorVideoType], params: Dict[str, Any] ) -> Union[datapoints._TensorImageType, datapoints._TensorVideoType]: return F.convert_dtype(inpt, self.dtype) # We changed the name to align it with the new naming scheme. Still, `ConvertImageDtype` is # prevalent and well understood. Thus, we just alias it without deprecating the old name. ConvertImageDtype = ConvertDtype class ClampBoundingBox(Transform): """[BETA] Clamp bounding boxes to their corresponding image dimensions. The clamping is done according to the bounding boxes' ``spatial_size`` meta-data. .. betastatus:: ClampBoundingBox transform """ _transformed_types = (datapoints.BoundingBox,) def _transform(self, inpt: datapoints.BoundingBox, params: Dict[str, Any]) -> datapoints.BoundingBox: return F.clamp_bounding_box(inpt) # type: ignore[return-value]
from enum import Enum from typing import Any from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField class ComparisonOperator(Enum): EQUAL = "==" NOT_EQUAL = "!=" GREATER_THAN = ">" LESS_THAN = "<" GREATER_THAN_OR_EQUAL = ">=" LESS_THAN_OR_EQUAL = "<=" class ConditionBlock(Block): class Input(BlockSchema): value1: Any = SchemaField( description="Enter the first value for comparison", placeholder="For example: 10 or 'hello' or True", ) operator: ComparisonOperator = SchemaField( description="Choose the comparison operator", placeholder="Select an operator", ) value2: Any = SchemaField( description="Enter the second value for comparison", placeholder="For example: 20 or 'world' or False", ) yes_value: Any = SchemaField( description="(Optional) Value to output if the condition is true. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) no_value: Any = SchemaField( description="(Optional) Value to output if the condition is false. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) class Output(BlockSchema): result: bool = SchemaField( description="The result of the condition evaluation (True or False)" ) yes_output: Any = SchemaField( description="The output value if the condition is true" ) no_output: Any = SchemaField( description="The output value if the condition is false" ) def __init__(self): super().__init__( id="715696a0-e1da-45c8-b209-c2fa9c3b0be6", input_schema=ConditionBlock.Input, output_schema=ConditionBlock.Output, description="Handles conditional logic based on comparison operators", categories={BlockCategory.LOGIC}, test_input={ "value1": 10, "operator": ComparisonOperator.GREATER_THAN.value, "value2": 5, "yes_value": "Greater", "no_value": "Not greater", }, test_output=[ ("result", True), ("yes_output", "Greater"), ], ) def run(self, input_data: Input, **kwargs) -> BlockOutput: operator = input_data.operator value1 = input_data.value1 if isinstance(value1, str): try: value1 = float(value1.strip()) except ValueError: value1 = value1.strip() value2 = input_data.value2 if isinstance(value2, str): try: value2 = float(value2.strip()) except ValueError: value2 = value2.strip() yes_value = input_data.yes_value if input_data.yes_value is not None else value1 no_value = input_data.no_value if input_data.no_value is not None else value2 comparison_funcs = { ComparisonOperator.EQUAL: lambda a, b: a == b, ComparisonOperator.NOT_EQUAL: lambda a, b: a != b, ComparisonOperator.GREATER_THAN: lambda a, b: a > b, ComparisonOperator.LESS_THAN: lambda a, b: a < b, ComparisonOperator.GREATER_THAN_OR_EQUAL: lambda a, b: a >= b, ComparisonOperator.LESS_THAN_OR_EQUAL: lambda a, b: a <= b, } result = comparison_funcs[operator](value1, value2) yield "result", result if result: yield "yes_output", yes_value else: yield "no_output", no_value
from enum import Enum from typing import Any from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema from backend.data.model import SchemaField class ComparisonOperator(Enum): EQUAL = "==" NOT_EQUAL = "!=" GREATER_THAN = ">" LESS_THAN = "<" GREATER_THAN_OR_EQUAL = ">=" LESS_THAN_OR_EQUAL = "<=" class ConditionBlock(Block): class Input(BlockSchema): value1: Any = SchemaField( description="Enter the first value for comparison", placeholder="For example: 10 or 'hello' or True", ) operator: ComparisonOperator = SchemaField( description="Choose the comparison operator", placeholder="Select an operator", ) value2: Any = SchemaField( description="Enter the second value for comparison", placeholder="For example: 20 or 'world' or False", ) yes_value: Any = SchemaField( description="(Optional) Value to output if the condition is true. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) no_value: Any = SchemaField( description="(Optional) Value to output if the condition is false. If not provided, value1 will be used.", placeholder="Leave empty to use value1, or enter a specific value", default=None, ) class Output(BlockSchema): result: bool = SchemaField( description="The result of the condition evaluation (True or False)" ) yes_output: Any = SchemaField( description="The output value if the condition is true" ) no_output: Any = SchemaField( description="The output value if the condition is false" ) def __init__(self): super().__init__( id="715696a0-e1da-45c8-b209-c2fa9c3b0be6", input_schema=ConditionBlock.Input, output_schema=ConditionBlock.Output, description="Handles conditional logic based on comparison operators", categories={BlockCategory.LOGIC}, test_input={ "value1": 10, "operator": ComparisonOperator.GREATER_THAN.value, "value2": 5, "yes_value": "Greater", "no_value": "Not greater", }, test_output=[ ("result", True), ("yes_output", "Greater"), ], ) def run(self, input_data: Input, **kwargs) -> BlockOutput: operator = input_data.operator value1 = input_data.value1 if isinstance(value1, str): value1 = float(value1.strip()) value2 = input_data.value2 if isinstance(value2, str): value2 = float(value2.strip()) yes_value = input_data.yes_value if input_data.yes_value is not None else value1 no_value = input_data.no_value if input_data.no_value is not None else value2 comparison_funcs = { ComparisonOperator.EQUAL: lambda a, b: a == b, ComparisonOperator.NOT_EQUAL: lambda a, b: a != b, ComparisonOperator.GREATER_THAN: lambda a, b: a > b, ComparisonOperator.LESS_THAN: lambda a, b: a < b, ComparisonOperator.GREATER_THAN_OR_EQUAL: lambda a, b: a >= b, ComparisonOperator.LESS_THAN_OR_EQUAL: lambda a, b: a <= b, } result = comparison_funcs[operator](value1, value2) yield "result", result if result: yield "yes_output", yes_value else: yield "no_output", no_value
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] pretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22k.pth' # noqa model = dict( type='ATSS', data_preprocessor=dict( type='DetDataPreprocessor', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], bgr_to_rgb=True, pad_size_divisor=128), backbone=dict( type='SwinTransformer', pretrain_img_size=384, embed_dims=192, depths=[2, 2, 18, 2], num_heads=[6, 12, 24, 48], window_size=12, mlp_ratio=4, qkv_bias=True, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0.2, patch_norm=True, out_indices=(1, 2, 3), # Please only add indices that would be used # in FPN, otherwise some parameter will not be used with_cp=False, convert_weights=True, init_cfg=dict(type='Pretrained', checkpoint=pretrained)), neck=[ dict( type='FPN', in_channels=[384, 768, 1536], out_channels=256, start_level=0, add_extra_convs='on_output', num_outs=5), dict( type='DyHead', in_channels=256, out_channels=256, num_blocks=6, # disable zero_init_offset to follow official implementation zero_init_offset=False) ], bbox_head=dict( type='ATSSHead', num_classes=80, in_channels=256, pred_kernel_size=1, # follow DyHead official implementation stacked_convs=0, 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], center_offset=0.5), # follow DyHead official implementation 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=2.0), loss_centerness=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)), # training and testing settings train_cfg=dict( assigner=dict(type='ATSSAssigner', topk=9), 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)) # dataset settings train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=[(2000, 480), (2000, 1200)], keep_ratio=True, backend='pillow'), 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=(2000, 1200), keep_ratio=True, backend='pillow'), dict(type='LoadAnnotations', with_bbox=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( dataset=dict( _delete_=True, type='RepeatDataset', times=2, dataset=dict( type={{_base_.dataset_type}}, data_root={{_base_.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(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader # optimizer optim_wrapper = dict( _delete_=True, type='OptimWrapper', optimizer=dict( type='AdamW', lr=0.00005, betas=(0.9, 0.999), weight_decay=0.05), paramwise_cfg=dict( custom_keys={ 'absolute_pos_embed': dict(decay_mult=0.), 'relative_position_bias_table': dict(decay_mult=0.), 'norm': dict(decay_mult=0.) }), clip_grad=None)
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] pretrained = 'https://github.com/SwinTransformer/storage/releases/download/v1.0.0/swin_large_patch4_window12_384_22k.pth' # noqa model = dict( type='ATSS', data_preprocessor=dict( type='DetDataPreprocessor', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], bgr_to_rgb=True, pad_size_divisor=128), backbone=dict( type='SwinTransformer', pretrain_img_size=384, embed_dims=192, depths=[2, 2, 18, 2], num_heads=[6, 12, 24, 48], window_size=12, mlp_ratio=4, qkv_bias=True, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0.2, patch_norm=True, out_indices=(1, 2, 3), # Please only add indices that would be used # in FPN, otherwise some parameter will not be used with_cp=False, convert_weights=True, init_cfg=dict(type='Pretrained', checkpoint=pretrained)), neck=[ dict( type='FPN', in_channels=[384, 768, 1536], out_channels=256, start_level=0, add_extra_convs='on_output', num_outs=5), dict( type='DyHead', in_channels=256, out_channels=256, num_blocks=6, # disable zero_init_offset to follow official implementation zero_init_offset=False) ], bbox_head=dict( type='ATSSHead', num_classes=80, in_channels=256, pred_kernel_size=1, # follow DyHead official implementation stacked_convs=0, 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], center_offset=0.5), # follow DyHead official implementation 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=2.0), loss_centerness=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)), # training and testing settings train_cfg=dict( assigner=dict(type='ATSSAssigner', topk=9), 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)) # dataset settings train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=[(2000, 480), (2000, 1200)], keep_ratio=True, backend='pillow'), 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=(2000, 1200), keep_ratio=True, backend='pillow'), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( dataset=dict( _delete_=True, type='RepeatDataset', times=2, dataset=dict( type={{_base_.dataset_type}}, data_root={{_base_.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(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader # optimizer optim_wrapper = dict( _delete_=True, type='OptimWrapper', optimizer=dict( type='AdamW', lr=0.00005, betas=(0.9, 0.999), weight_decay=0.05), paramwise_cfg=dict( custom_keys={ 'absolute_pos_embed': dict(decay_mult=0.), 'relative_position_bias_table': dict(decay_mult=0.), 'norm': dict(decay_mult=0.) }), clip_grad=None)
"""**Tracers** are classes for tracing runs. **Class hierarchy:** .. code-block:: BaseCallbackHandler --> BaseTracer --> <name>Tracer # Examples: LangChainTracer, RootListenersTracer --> <name> # Examples: LogStreamCallbackHandler """ # noqa: E501 from importlib import import_module from typing import TYPE_CHECKING if TYPE_CHECKING: from langchain_core.tracers.base import BaseTracer from langchain_core.tracers.evaluation import EvaluatorCallbackHandler from langchain_core.tracers.langchain import LangChainTracer from langchain_core.tracers.log_stream import ( LogStreamCallbackHandler, RunLog, RunLogPatch, ) from langchain_core.tracers.schemas import Run from langchain_core.tracers.stdout import ConsoleCallbackHandler __all__ = [ "BaseTracer", "EvaluatorCallbackHandler", "LangChainTracer", "ConsoleCallbackHandler", "Run", "RunLog", "RunLogPatch", "LogStreamCallbackHandler", ] _dynamic_imports = { "BaseTracer": "base", "EvaluatorCallbackHandler": "evaluation", "LangChainTracer": "langchain", "LogStreamCallbackHandler": "log_stream", "RunLog": "log_stream", "RunLogPatch": "log_stream", "Run": "schemas", "ConsoleCallbackHandler": "stdout", } def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) package = __spec__.parent if module_name == "__module__" or module_name is None: result = import_module(f".{attr_name}", package=package) else: module = import_module(f".{module_name}", package=package) result = getattr(module, attr_name) globals()[attr_name] = result return result def __dir__() -> list[str]: return list(__all__)
"""**Tracers** are classes for tracing runs. **Class hierarchy:** .. code-block:: BaseCallbackHandler --> BaseTracer --> <name>Tracer # Examples: LangChainTracer, RootListenersTracer --> <name> # Examples: LogStreamCallbackHandler """ # noqa: E501 from importlib import import_module from typing import TYPE_CHECKING if TYPE_CHECKING: from langchain_core.tracers.base import BaseTracer from langchain_core.tracers.evaluation import EvaluatorCallbackHandler from langchain_core.tracers.langchain import LangChainTracer from langchain_core.tracers.log_stream import ( LogStreamCallbackHandler, RunLog, RunLogPatch, ) from langchain_core.tracers.schemas import Run from langchain_core.tracers.stdout import ConsoleCallbackHandler __all__ = [ "BaseTracer", "EvaluatorCallbackHandler", "LangChainTracer", "ConsoleCallbackHandler", "Run", "RunLog", "RunLogPatch", "LogStreamCallbackHandler", ] _dynamic_imports = { "BaseTracer": "base", "EvaluatorCallbackHandler": "evaluation", "LangChainTracer": "langchain", "LogStreamCallbackHandler": "log_stream", "RunLog": "log_stream", "RunLogPatch": "log_stream", "Run": "schemas", "ConsoleCallbackHandler": "stdout", } def __getattr__(attr_name: str) -> object: module_name = _dynamic_imports.get(attr_name) package = __spec__.parent # type: ignore[name-defined] if module_name == "__module__" or module_name is None: result = import_module(f".{attr_name}", package=package) else: module = import_module(f".{module_name}", package=package) result = getattr(module, attr_name) globals()[attr_name] = result return result def __dir__() -> list[str]: return list(__all__)
"""Parser for JSON output.""" from __future__ import annotations import json from json import JSONDecodeError from typing import Annotated, Any, Optional, TypeVar, Union import jsonpatch # type: ignore[import-untyped] import pydantic from pydantic import SkipValidation from typing_extensions import override 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 IS_PYDANTIC_V1 if IS_PYDANTIC_V1: 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[assignment,misc] 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[valid-type] """The Pydantic object to use for validation. If None, no validation is performed.""" @override 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() if issubclass(pydantic_object, pydantic.v1.BaseModel): return pydantic_object.schema() return None 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." # 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_and_check_json_markdown", # For backwards compatibility "parse_partial_json", # For backwards compatibility ]
"""Parser for JSON output.""" from __future__ import annotations import json from json import JSONDecodeError from typing import Annotated, Any, Optional, TypeVar, Union import jsonpatch # type: ignore[import-untyped] 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 IS_PYDANTIC_V1 if IS_PYDANTIC_V1: 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[assignment,misc] 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[valid-type] """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() if issubclass(pydantic_object, pydantic.v1.BaseModel): return pydantic_object.schema() return None 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." # 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_and_check_json_markdown", # For backwards compatibility "parse_partial_json", # For backwards compatibility ]
"""[DEPRECATED] Pipeline prompt template.""" from typing import Any from pydantic import model_validator from langchain_core._api.deprecation import deprecated from langchain_core.prompt_values import PromptValue from langchain_core.prompts.base import BasePromptTemplate from langchain_core.prompts.chat import BaseChatPromptTemplate def _get_inputs(inputs: dict, input_variables: list[str]) -> dict: return {k: inputs[k] for k in input_variables} @deprecated( since="0.3.22", removal="1.0", message=( "This class is deprecated. Please see the docstring below or at the link" " for a replacement option: " "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.pipeline.PipelinePromptTemplate.html" ), ) class PipelinePromptTemplate(BasePromptTemplate): """[DEPRECATED] Pipeline prompt template. This has been deprecated in favor of chaining individual prompts together in your code. E.g. using a for loop, you could do: .. code-block:: python my_input = {"key": "value"} for name, prompt in pipeline_prompts: my_input[name] = prompt.invoke(my_input).to_string() my_output = final_prompt.invoke(my_input) Prompt template for composing multiple prompt templates together. This can be useful when you want to reuse parts of prompts. A PipelinePrompt consists of two main parts: - final_prompt: This is the final prompt that is returned - pipeline_prompts: This is a list of tuples, consisting of a string (`name`) and a Prompt Template. Each PromptTemplate will be formatted and then passed to future prompt templates as a variable with the same name as `name` """ final_prompt: BasePromptTemplate """The final prompt that is returned.""" pipeline_prompts: list[tuple[str, BasePromptTemplate]] """A list of tuples, consisting of a string (`name`) and a Prompt Template.""" @classmethod def get_lc_namespace(cls) -> list[str]: """Get the namespace of the langchain object.""" return ["langchain", "prompts", "pipeline"] @model_validator(mode="before") @classmethod def get_input_variables(cls, values: dict) -> Any: """Get input variables.""" created_variables = set() all_variables = set() for k, prompt in values["pipeline_prompts"]: created_variables.add(k) all_variables.update(prompt.input_variables) values["input_variables"] = list(all_variables.difference(created_variables)) return values def format_prompt(self, **kwargs: Any) -> PromptValue: """Format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ for k, prompt in self.pipeline_prompts: _inputs = _get_inputs(kwargs, prompt.input_variables) if isinstance(prompt, BaseChatPromptTemplate): kwargs[k] = prompt.format_messages(**_inputs) else: kwargs[k] = prompt.format(**_inputs) _inputs = _get_inputs(kwargs, self.final_prompt.input_variables) return self.final_prompt.format_prompt(**_inputs) async def aformat_prompt(self, **kwargs: Any) -> PromptValue: """Async format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ for k, prompt in self.pipeline_prompts: _inputs = _get_inputs(kwargs, prompt.input_variables) if isinstance(prompt, BaseChatPromptTemplate): kwargs[k] = await prompt.aformat_messages(**_inputs) else: kwargs[k] = await prompt.aformat(**_inputs) _inputs = _get_inputs(kwargs, self.final_prompt.input_variables) return await self.final_prompt.aformat_prompt(**_inputs) def format(self, **kwargs: Any) -> str: """Format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ return self.format_prompt(**kwargs).to_string() async def aformat(self, **kwargs: Any) -> str: """Async format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ return (await self.aformat_prompt(**kwargs)).to_string() @property def _prompt_type(self) -> str: raise ValueError
"""[DEPRECATED] Pipeline prompt template.""" from typing import Any from pydantic import model_validator from langchain_core._api.deprecation import deprecated from langchain_core.prompt_values import PromptValue from langchain_core.prompts.base import BasePromptTemplate from langchain_core.prompts.chat import BaseChatPromptTemplate def _get_inputs(inputs: dict, input_variables: list[str]) -> dict: return {k: inputs[k] for k in input_variables} @deprecated( since="0.3.22", removal="1.0", message=( "This class is deprecated. Please see the docstring below or at the link" " for a replacement option: " "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.pipeline.PipelinePromptTemplate.html" ), ) class PipelinePromptTemplate(BasePromptTemplate): """[DEPRECATED] Pipeline prompt template. This has been deprecated in favor of chaining individual prompts together in your code. E.g. using a for loop, you could do: .. code-block:: python my_input = {"key": "value"} for name, prompt in pipeline_prompts: my_input[name] = prompt.invoke(my_input).to_string() my_output = final_prompt.invoke(my_input) Prompt template for composing multiple prompt templates together. This can be useful when you want to reuse parts of prompts. A PipelinePrompt consists of two main parts: - final_prompt: This is the final prompt that is returned - pipeline_prompts: This is a list of tuples, consisting of a string (`name`) and a Prompt Template. Each PromptTemplate will be formatted and then passed to future prompt templates as a variable with the same name as `name` """ final_prompt: BasePromptTemplate """The final prompt that is returned.""" pipeline_prompts: list[tuple[str, BasePromptTemplate]] """A list of tuples, consisting of a string (`name`) and a Prompt Template.""" @classmethod def get_lc_namespace(cls) -> list[str]: """Get the namespace of the langchain object.""" return ["langchain", "prompts", "pipeline"] @model_validator(mode="before") @classmethod def get_input_variables(cls, values: dict) -> Any: """Get input variables.""" created_variables = set() all_variables = set() for k, prompt in values["pipeline_prompts"]: created_variables.add(k) all_variables.update(prompt.input_variables) values["input_variables"] = list(all_variables.difference(created_variables)) return values def format_prompt(self, **kwargs: Any) -> PromptValue: """Format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ for k, prompt in self.pipeline_prompts: _inputs = _get_inputs(kwargs, prompt.input_variables) if isinstance(prompt, BaseChatPromptTemplate): kwargs[k] = prompt.format_messages(**_inputs) else: kwargs[k] = prompt.format(**_inputs) _inputs = _get_inputs(kwargs, self.final_prompt.input_variables) return self.final_prompt.format_prompt(**_inputs) async def aformat_prompt(self, **kwargs: Any) -> PromptValue: """Async format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ for k, prompt in self.pipeline_prompts: _inputs = _get_inputs(kwargs, prompt.input_variables) if isinstance(prompt, BaseChatPromptTemplate): kwargs[k] = await prompt.aformat_messages(**_inputs) else: kwargs[k] = await prompt.aformat(**_inputs) _inputs = _get_inputs(kwargs, self.final_prompt.input_variables) return await self.final_prompt.aformat_prompt(**_inputs) def format(self, **kwargs: Any) -> str: """Format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ return self.format_prompt(**kwargs).to_string() async def aformat(self, **kwargs: Any) -> str: """Async format the prompt with the inputs. Args: kwargs: Any arguments to be passed to the prompt template. Returns: A formatted string. """ return (await self.aformat_prompt(**kwargs)).to_string() @property def _prompt_type(self) -> str: raise ValueError PipelinePromptTemplate.model_rebuild()
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/default_runtime.py' ] cudnn_benchmark = True # model settings norm_cfg = dict(type='BN', requires_grad=True) model = dict( type='RetinaNet', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=norm_cfg, norm_eval=False, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict(type='NASFPN', stack_times=7, norm_cfg=norm_cfg), bbox_head=dict(type='RetinaSepBNHead', num_ins=5, norm_cfg=norm_cfg), # training and testing settings train_cfg=dict(assigner=dict(neg_iou_thr=0.5))) # dataset settings img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='Resize', img_scale=(640, 640), ratio_range=(0.8, 1.2), keep_ratio=True), dict(type='RandomCrop', crop_size=(640, 640)), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size=(640, 640)), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(640, 640), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=128), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=8, workers_per_gpu=4, train=dict(pipeline=train_pipeline), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # optimizer optimizer = dict( type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001, paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True)) optimizer_config = dict(grad_clip=None) # learning policy lr_config = dict( policy='step', warmup='linear', warmup_iters=1000, warmup_ratio=0.1, step=[30, 40]) # runtime settings runner = dict(type='EpochBasedRunner', max_epochs=50) # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (8 GPUs) x (8 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = [ '../_base_/models/retinanet_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/default_runtime.py' ] cudnn_benchmark = True # model settings norm_cfg = dict(type='BN', requires_grad=True) model = dict( type='RetinaNet', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=norm_cfg, norm_eval=False, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict(type='NASFPN', stack_times=7, norm_cfg=norm_cfg), bbox_head=dict(type='RetinaSepBNHead', num_ins=5, norm_cfg=norm_cfg), # training and testing settings train_cfg=dict(assigner=dict(neg_iou_thr=0.5))) # dataset settings img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict( type='Resize', img_scale=(640, 640), ratio_range=(0.8, 1.2), keep_ratio=True), dict(type='RandomCrop', crop_size=(640, 640)), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size=(640, 640)), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(640, 640), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=128), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=8, workers_per_gpu=4, train=dict(pipeline=train_pipeline), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # optimizer optimizer = dict( type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001, paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True)) optimizer_config = dict(grad_clip=None) # learning policy lr_config = dict( policy='step', warmup='linear', warmup_iters=1000, warmup_ratio=0.1, step=[30, 40]) # runtime settings runner = dict(type='EpochBasedRunner', max_epochs=50)
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.chat_models.baidu_qianfan_endpoint import ( QianfanChatEndpoint, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "QianfanChatEndpoint": "langchain_community.chat_models.baidu_qianfan_endpoint", } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "QianfanChatEndpoint", ]
from typing import TYPE_CHECKING, Any from langchain._api import create_importer if TYPE_CHECKING: from langchain_community.chat_models.baidu_qianfan_endpoint import ( QianfanChatEndpoint, ) # Create a way to dynamically look up deprecated imports. # Used to consolidate logic for raising deprecation warnings and # handling optional imports. DEPRECATED_LOOKUP = { "QianfanChatEndpoint": "langchain_community.chat_models.baidu_qianfan_endpoint" } _import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) def __getattr__(name: str) -> Any: """Look up attributes dynamically.""" return _import_attribute(name) __all__ = [ "QianfanChatEndpoint", ]
import numpy as np import pytest from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_doc.io.json import orjson_dumps from docarray.typing import AnyEmbedding @pytest.mark.proto def test_proto_embedding(): embedding = parse_obj_as(AnyEmbedding, np.zeros((3, 224, 224))) embedding._to_node_protobuf() def test_json_schema(): schema_json_of(AnyEmbedding) def test_dump_json(): tensor = parse_obj_as(AnyEmbedding, np.zeros((3, 224, 224))) orjson_dumps(tensor)
import numpy as np import pytest from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_document.io.json import orjson_dumps from docarray.typing import AnyEmbedding @pytest.mark.proto def test_proto_embedding(): embedding = parse_obj_as(AnyEmbedding, np.zeros((3, 224, 224))) embedding._to_node_protobuf() def test_json_schema(): schema_json_of(AnyEmbedding) def test_dump_json(): tensor = parse_obj_as(AnyEmbedding, np.zeros((3, 224, 224))) orjson_dumps(tensor)
_base_ = './mask-rcnn_x101-32x4d_fpn_2x_coco.py' model = dict( backbone=dict( type='ResNeXt', depth=101, groups=64, base_width=4, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://resnext101_64x4d')))
_base_ = './mask_rcnn_x101_32x4d_fpn_2x_coco.py' model = dict( backbone=dict( type='ResNeXt', depth=101, groups=64, base_width=4, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), style='pytorch', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://resnext101_64x4d')))
# Copyright (c) OpenMMLab. All rights reserved. import os.path as osp from typing import List, Optional from mmengine.dataset import BaseDataset from mmengine.fileio import FileClient, load from mmengine.utils import is_abs from ..registry import DATASETS @DATASETS.register_module() class BaseDetDataset(BaseDataset): """Base dataset for detection. Args: proposal_file (str, optional): Proposals file path. Defaults to None. file_client_args (dict): Arguments to instantiate a FileClient. See :class:`mmengine.fileio.FileClient` for details. Defaults to ``dict(backend='disk')``. """ def __init__(self, *args, seg_map_suffix: str = '.png', proposal_file: Optional[str] = None, file_client_args: dict = dict(backend='disk'), **kwargs) -> None: self.seg_map_suffix = seg_map_suffix self.proposal_file = proposal_file self.file_client_args = file_client_args self.file_client = FileClient(**file_client_args) super().__init__(*args, **kwargs) def full_init(self) -> None: """Load annotation file and set ``BaseDataset._fully_initialized`` to True. If ``lazy_init=False``, ``full_init`` will be called during the instantiation and ``self._fully_initialized`` will be set to True. If ``obj._fully_initialized=False``, the class method decorated by ``force_full_init`` will call ``full_init`` automatically. Several steps to initialize annotation: - load_data_list: Load annotations from annotation file. - load_proposals: Load proposals from proposal file, if `self.proposal_file` is not None. - filter data information: Filter annotations according to filter_cfg. - slice_data: Slice dataset according to ``self._indices`` - serialize_data: Serialize ``self.data_list`` if ``self.serialize_data`` is True. """ if self._fully_initialized: return # load data information self.data_list = self.load_data_list() # get proposals from file if self.proposal_file is not None: self.load_proposals() # filter illegal data, such as data that has no annotations. self.data_list = self.filter_data() # Get subset data according to indices. if self._indices is not None: self.data_list = self._get_unserialized_subset(self._indices) # serialize data_list if self.serialize_data: self.data_bytes, self.data_address = self._serialize_data() self._fully_initialized = True def load_proposals(self) -> None: """Load proposals from proposals file. The `proposals_list` should be a dict[img_path: proposals] with the same length as `data_list`. And the `proposals` should be a `dict` or :obj:`InstanceData` usually contains following keys. - bboxes (np.ndarry): Has a shape (num_instances, 4), the last dimension 4 arrange as (x1, y1, x2, y2). - scores (np.ndarry): Classification scores, has a shape (num_instance, ). """ # TODO: Add Unit Test after fully support Dump-Proposal Metric if not is_abs(self.proposal_file): self.proposal_file = osp.join(self.data_root, self.proposal_file) proposals_list = load( self.proposal_file, file_client_args=self.file_client_args) assert len(self.data_list) == len(proposals_list) for data_info in self.data_list: img_path = data_info['img_path'] # `file_name` is the key to obtain the proposals from the # `proposals_list`. file_name = osp.join( osp.split(osp.split(img_path)[0])[-1], osp.split(img_path)[-1]) proposals = proposals_list[file_name] data_info['proposals'] = proposals def get_cat_ids(self, idx: int) -> List[int]: """Get COCO category ids by index. Args: idx (int): Index of data. Returns: List[int]: All categories in the image of specified index. """ instances = self.get_data_info(idx)['instances'] return [instance['bbox_label'] for instance in instances]
# Copyright (c) OpenMMLab. All rights reserved. import os.path as osp from typing import List, Optional from mmengine.dataset import BaseDataset from mmengine.fileio import FileClient, load from mmengine.utils import is_abs from ..registry import DATASETS @DATASETS.register_module() class BaseDetDataset(BaseDataset): """Base dataset for detection. Args: proposal_file (str, optional): Proposals file path. Defaults to None. file_client_args (dict): Arguments to instantiate a FileClient. See :class:`mmengine.fileio.FileClient` for details. Defaults to ``dict(backend='disk')``. """ def __init__(self, *args, proposal_file: Optional[str] = None, file_client_args: dict = dict(backend='disk'), **kwargs) -> None: self.proposal_file = proposal_file self.file_client_args = file_client_args self.file_client = FileClient(**file_client_args) super().__init__(*args, **kwargs) def full_init(self) -> None: """Load annotation file and set ``BaseDataset._fully_initialized`` to True. If ``lazy_init=False``, ``full_init`` will be called during the instantiation and ``self._fully_initialized`` will be set to True. If ``obj._fully_initialized=False``, the class method decorated by ``force_full_init`` will call ``full_init`` automatically. Several steps to initialize annotation: - load_data_list: Load annotations from annotation file. - load_proposals: Load proposals from proposal file, if `self.proposal_file` is not None. - filter data information: Filter annotations according to filter_cfg. - slice_data: Slice dataset according to ``self._indices`` - serialize_data: Serialize ``self.data_list`` if ``self.serialize_data`` is True. """ if self._fully_initialized: return # load data information self.data_list = self.load_data_list() # get proposals from file if self.proposal_file is not None: self.load_proposals() # filter illegal data, such as data that has no annotations. self.data_list = self.filter_data() # Get subset data according to indices. if self._indices is not None: self.data_list = self._get_unserialized_subset(self._indices) # serialize data_list if self.serialize_data: self.data_bytes, self.data_address = self._serialize_data() self._fully_initialized = True def load_proposals(self) -> None: """Load proposals from proposals file. The `proposals_list` should be a dict[img_path: proposals] with the same length as `data_list`. And the `proposals` should be a `dict` or :obj:`InstanceData` usually contains following keys. - bboxes (np.ndarry): Has a shape (num_instances, 4), the last dimension 4 arrange as (x1, y1, x2, y2). - scores (np.ndarry): Classification scores, has a shape (num_instance, ). """ # TODO: Add Unit Test after fully support Dump-Proposal Metric if not is_abs(self.proposal_file): self.proposal_file = osp.join(self.data_root, self.proposal_file) proposals_list = load( self.proposal_file, file_client_args=self.file_client_args) assert len(self.data_list) == len(proposals_list) for data_info in self.data_list: img_path = data_info['img_path'] # `file_name` is the key to obtain the proposals from the # `proposals_list`. file_name = osp.join( osp.split(osp.split(img_path)[0])[-1], osp.split(img_path)[-1]) proposals = proposals_list[file_name] data_info['proposals'] = proposals def get_cat_ids(self, idx: int) -> List[int]: """Get COCO category ids by index. Args: idx (int): Index of data. Returns: List[int]: All categories in the image of specified index. """ instances = self.get_data_info(idx)['instances'] return [instance['bbox_label'] for instance in instances]
from docarray import DocumentArray from jina import Executor, requests from jina.parsers import set_pod_parser class ProcessExecutor(Executor): @requests(on='/') def process(self, docs: DocumentArray, **kwargs): for doc in docs: doc.text = doc.text + 'world' doc.tags['processed'] = True def _validate_dummy_custom_gateway_response(port, expected): import requests resp = requests.get(f'http://127.0.0.1:{port}/').json() assert resp == expected def _validate_custom_gateway_process(port, text, expected): import requests resp = requests.get(f'http://127.0.0.1:{port}/stream?text={text}').json() assert resp == expected # set_pod_parser returns a parser for worker runtime, which expects list of ports (because external executors # can provide multiple ports and hosts). However this parser is not compatible with ContainerPod, Pod and worker runtime. # Should we add a seperate parser for Pod? def _generate_pod_args(cli_split: list = []): args = set_pod_parser().parse_args(cli_split) args.host = args.host[0] args.port = args.port[0] args.port_monitoring = args.port_monitoring[0] return args
from docarray import DocumentArray from jina import Executor, requests class ProcessExecutor(Executor): @requests(on='/') def process(self, docs: DocumentArray, **kwargs): for doc in docs: doc.text = doc.text + 'world' doc.tags['processed'] = True def _validate_dummy_custom_gateway_response(port, expected): import requests resp = requests.get(f'http://127.0.0.1:{port}/').json() assert resp == expected def _validate_custom_gateway_process(port, text, expected): import requests resp = requests.get(f'http://127.0.0.1:{port}/stream?text={text}').json() assert resp == expected
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] # model settings preprocess_cfg = dict( mean=[102.9801, 115.9465, 122.7717], std=[1.0, 1.0, 1.0], to_rgb=False, pad_size_divisor=32) model = dict( type='FCOS', preprocess_cfg=preprocess_cfg, backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=False), norm_eval=True, style='caffe', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://detectron/resnet50_caffe')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, start_level=1, add_extra_convs='on_output', # use P5 num_outs=5, relu_before_extra_convs=True), bbox_head=dict( type='FCOSHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, strides=[8, 16, 32, 64, 128], loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='IoULoss', loss_weight=1.0), loss_centerness=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)), # testing settings test_cfg=dict( nms_pre=1000, min_bbox_size=0, score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100)) # training schedule for 1x train_cfg = dict(by_epoch=True, max_epochs=12) val_cfg = dict(interval=1) test_cfg = dict() # learning rate param_scheduler = [ dict(type='ConstantLR', factor=1.0 / 3, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=12, by_epoch=True, milestones=[8, 11], gamma=0.1) ] # optimizer optimizer = dict( lr=0.01, paramwise_cfg=dict(bias_lr_mult=2., bias_decay_mult=0.)) default_hooks = dict( optimizer=dict( _delete_=True, type='OptimizerHook', grad_clip=dict(max_norm=35, norm_type=2)))
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] # model settings model = dict( type='FCOS', 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=False), norm_eval=True, style='caffe', init_cfg=dict( type='Pretrained', checkpoint='open-mmlab://detectron/resnet50_caffe')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, start_level=1, add_extra_convs='on_output', # use P5 num_outs=5, relu_before_extra_convs=True), bbox_head=dict( type='FCOSHead', num_classes=80, in_channels=256, stacked_convs=4, feat_channels=256, strides=[8, 16, 32, 64, 128], loss_cls=dict( type='FocalLoss', use_sigmoid=True, gamma=2.0, alpha=0.25, loss_weight=1.0), loss_bbox=dict(type='IoULoss', loss_weight=1.0), loss_centerness=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)), # testing settings test_cfg=dict( nms_pre=1000, min_bbox_size=0, score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100)) img_norm_cfg = dict( mean=[102.9801, 115.9465, 122.7717], std=[1.0, 1.0, 1.0], to_rgb=False) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict(type='Resize', img_scale=(1333, 800), 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']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), 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( samples_per_gpu=2, workers_per_gpu=2, train=dict(pipeline=train_pipeline), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # optimizer optimizer = dict( lr=0.01, paramwise_cfg=dict(bias_lr_mult=2., bias_decay_mult=0.)) optimizer_config = dict( _delete_=True, grad_clip=dict(max_norm=35, norm_type=2)) # learning policy lr_config = dict( policy='step', warmup='constant', warmup_iters=500, warmup_ratio=1.0 / 3, step=[8, 11]) runner = dict(type='EpochBasedRunner', max_epochs=12)
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os from pathlib import Path import pytest import spacy from jina import Document, DocumentArray, Executor from ...spacy_text_encoder import SpacyTextEncoder def test_config(): ex = Executor.load_config(str(Path(__file__).parents[2] / 'config.yml')) assert ex.lang == 'en_core_web_sm' def test_spacy_text_encoder(): # Input docs = DocumentArray( [ Document(text='Han likes eating pizza'), Document(text='Han likes pizza'), Document(text='Jina rocks'), ] ) # Encoder embedding encoder = SpacyTextEncoder() encoder.encode(docs, parameters={}) # Compare with ouptut assert len(docs) == 3 for doc in docs: assert doc.embedding.shape == (96,) def test_spacy_text_encoder_traversal_paths(): # Input docs = DocumentArray( [ Document( chunks=[ Document(text='Han likes eating pizza'), Document(text='Han likes pizza'), ] ), Document(chunks=[Document(text='Jina rocks')]), ] ) # Encoder embedding encoder = SpacyTextEncoder() encoder.encode(docs, parameters={'traversal_paths': ['c']}) # Compare with ouptut assert len(docs) == 2 assert len(docs[0].chunks) == 2 for chunk in docs[0].chunks: assert chunk.embedding.shape == (96,) assert len(docs[1].chunks) == 1 for chunk in docs[1].chunks: assert chunk.embedding.shape == (96,) def test_unsupported_lang(tmpdir): dummy1 = spacy.blank('xx') dummy1_dir_path = os.path.join(tmpdir, 'xx1') dummy1.to_disk(dummy1_dir_path) dummy2 = spacy.blank('xx') dummy2_dir_path = os.path.join(tmpdir, 'xx2') dummy2.to_disk(dummy2_dir_path) # No available language with pytest.raises(IOError): SpacyTextEncoder('abcd') # Language does not have DependencyParser should thrown an error # when try to use default encoder with pytest.raises(ValueError): SpacyTextEncoder(dummy1_dir_path, use_default_encoder=True) # And should be fine when 'parser' pipeline is added dummy1.add_pipe('parser') dummy1.to_disk(dummy1_dir_path) SpacyTextEncoder(dummy1_dir_path, use_default_encoder=True) # Language does not have SentenceRecognizer should thrown an error # when try to use non default encoder with pytest.raises(ValueError): SpacyTextEncoder(dummy2_dir_path, use_default_encoder=False) # And should be fine when 'senter' pipeline is added dummy2.add_pipe('tok2vec') dummy2.to_disk(dummy2_dir_path) SpacyTextEncoder(dummy2_dir_path, use_default_encoder=False)
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import pytest import spacy from jina import Document, DocumentArray try: from spacy_text_encoder import SpacyTextEncoder except: from ...spacy_text_encoder import SpacyTextEncoder cur_dir = os.path.dirname(os.path.abspath(__file__)) def test_spacy_text_encoder(): # Input docs = DocumentArray([Document(text='Han likes eating pizza'), Document(text='Han likes pizza'), Document(text='Jina rocks')]) # Encoder embedding encoder = SpacyTextEncoder() encoder.encode(docs, parameters={}) # Compare with ouptut assert len(docs) == 3 for doc in docs: assert doc.embedding.shape == (96, ) def test_spacy_text_encoder_traversal_paths(): # Input docs = DocumentArray([Document(chunks=[Document(text='Han likes eating pizza'), Document(text='Han likes pizza')]), Document(chunks=[Document(text='Jina rocks')])]) # Encoder embedding encoder = SpacyTextEncoder() encoder.encode(docs, parameters={'traversal_paths': ['c']}) # Compare with ouptut assert len(docs) == 2 assert len(docs[0].chunks) == 2 for chunk in docs[0].chunks: assert chunk.embedding.shape == (96, ) assert len(docs[1].chunks) == 1 for chunk in docs[1].chunks: assert chunk.embedding.shape == (96, ) def test_unsupported_lang(tmpdir): dummy1 = spacy.blank('xx') dummy1_dir_path = os.path.join(tmpdir, 'xx1') dummy1.to_disk(dummy1_dir_path) dummy2 = spacy.blank('xx') dummy2_dir_path = os.path.join(tmpdir, 'xx2') dummy2.to_disk(dummy2_dir_path) # No available language with pytest.raises(IOError): SpacyTextEncoder('abcd') # Language does not have DependencyParser should thrown an error # when try to use default encoder with pytest.raises(ValueError): SpacyTextEncoder(dummy1_dir_path, use_default_encoder=True) # And should be fine when 'parser' pipeline is added dummy1.add_pipe('parser') dummy1.to_disk(dummy1_dir_path) SpacyTextEncoder(dummy1_dir_path, use_default_encoder=True) # Language does not have SentenceRecognizer should thrown an error # when try to use non default encoder with pytest.raises(ValueError): SpacyTextEncoder(dummy2_dir_path, use_default_encoder=False) # And should be fine when 'senter' pipeline is added dummy2.add_pipe('tok2vec') dummy2.to_disk(dummy2_dir_path) SpacyTextEncoder(dummy2_dir_path, use_default_encoder=False)
import os import shutil import subprocess import sys def _get_run_args(print_args: bool = True): from jina.helper import get_rich_console from jina.parsers import get_main_parser console = get_rich_console() silent_print = {'help', 'hub', 'export', 'auth', 'cloud', 'ping'} parser = get_main_parser() if len(sys.argv) > 1: from argparse import _StoreAction, _StoreTrueAction from rich import box from rich.table import Table args, unknown = parser.parse_known_args() if unknown: from jina.helper import warn_unknown_args unknown = list(filter(lambda x: x.startswith('--'), unknown)) warn_unknown_args(unknown) if args.cli not in silent_print and print_args: from jina.constants import __resources_path__ p = parser._actions[-1].choices[sys.argv[1]] default_args = { a.dest: a.default for a in p._actions if isinstance(a, (_StoreAction, _StoreTrueAction)) } with open(os.path.join(__resources_path__, 'jina.logo'), encoding='utf-8') as fp: logo_str = fp.read() param_str = Table( title=' '.join(sys.argv), box=box.ROUNDED, highlight=True, title_justify='left', ) param_str.add_column('Argument', justify='right') param_str.add_column('Value', justify='left') for k, v in sorted(vars(args).items()): param = k.replace('_', '-') value = str(v) if not default_args.get(k, None) == v: value = f'[b]{value}[/]' param_str.add_row(param, value) if 'JINA_LOG_NO_COLOR' not in os.environ: print(f'\n{logo_str}\n') console.print(param_str) return args else: parser.print_help() exit() def _quick_ac_lookup(): from jina_cli.autocomplete import ac_table if len(sys.argv) > 1: if sys.argv[1] == 'commands': for k in ac_table['commands']: print(k) exit() elif sys.argv[1] == 'completions': # search with the longest shared prefix for j in range(len(sys.argv), 2, -1): _input = ' '.join(sys.argv[2:j]).strip() if _input in ac_table['completions']: compl = ac_table['completions'][_input] for k in compl: if k not in sys.argv: print(k) break exit() def _try_plugin_command(): """Tries to call the CLI of an external Jina project. :return: if the plugin has been found (locally or among the known plugins) """ argv = sys.argv if len(argv) < 2: # no command given return False from jina_cli.autocomplete import ac_table if argv[1] in ac_table['commands']: # native command can't be plugin command return False def _cmd_exists(cmd): return shutil.which(cmd) is not None subcommand = argv[1] cmd = 'jina-' + subcommand if _cmd_exists(cmd): subprocess.run([cmd] + argv[2:]) return True from jina_cli.known_plugins import plugin_info if subcommand in plugin_info: from jina.helper import get_rich_console cmd_info = plugin_info[subcommand] project, package = cmd_info['display-name'], cmd_info['pip-package'] console = get_rich_console() console.print( f"It seems like [yellow]{project}[/yellow] is not installed in your environment." f"To use it via the [green]'jina {subcommand}'[/green] command, " f"install it first: [green]'pip install {package}'[/green]." ) return True return False def main(): """The main entrypoint of the CLI""" found_plugin = _try_plugin_command() if not found_plugin: _quick_ac_lookup() from jina_cli import api args = _get_run_args() getattr(api, args.cli.replace('-', '_'))(args)
import os import shutil import subprocess import sys def _get_run_args(print_args: bool = True): from jina.helper import get_rich_console from jina.parsers import get_main_parser console = get_rich_console() silent_print = {'help', 'hub', 'export', 'auth', 'cloud', 'ping'} parser = get_main_parser() if len(sys.argv) > 1: from argparse import _StoreAction, _StoreTrueAction from rich import box from rich.table import Table args, unknown = parser.parse_known_args() if unknown: from jina.helper import warn_unknown_args unknown = list(filter(lambda x: x.startswith('--'), unknown)) warn_unknown_args(unknown) if args.cli not in silent_print and print_args: from jina.constants import __resources_path__ p = parser._actions[-1].choices[sys.argv[1]] default_args = { a.dest: a.default for a in p._actions if isinstance(a, (_StoreAction, _StoreTrueAction)) } with open(os.path.join(__resources_path__, 'jina.logo')) as fp: logo_str = fp.read() param_str = Table( title=' '.join(sys.argv), box=box.ROUNDED, highlight=True, title_justify='left', ) param_str.add_column('Argument', justify='right') param_str.add_column('Value', justify='left') for k, v in sorted(vars(args).items()): param = k.replace('_', '-') value = str(v) if not default_args.get(k, None) == v: value = f'[b]{value}[/]' param_str.add_row(param, value) if 'JINA_LOG_NO_COLOR' not in os.environ: print(f'\n{logo_str}\n') console.print(param_str) return args else: parser.print_help() exit() def _quick_ac_lookup(): from jina_cli.autocomplete import ac_table if len(sys.argv) > 1: if sys.argv[1] == 'commands': for k in ac_table['commands']: print(k) exit() elif sys.argv[1] == 'completions': # search with the longest shared prefix for j in range(len(sys.argv), 2, -1): _input = ' '.join(sys.argv[2:j]).strip() if _input in ac_table['completions']: compl = ac_table['completions'][_input] for k in compl: if k not in sys.argv: print(k) break exit() def _try_plugin_command(): """Tries to call the CLI of an external Jina project. :return: if the plugin has been found (locally or among the known plugins) """ argv = sys.argv if len(argv) < 2: # no command given return False from jina_cli.autocomplete import ac_table if argv[1] in ac_table['commands']: # native command can't be plugin command return False def _cmd_exists(cmd): return shutil.which(cmd) is not None subcommand = argv[1] cmd = 'jina-' + subcommand if _cmd_exists(cmd): subprocess.run([cmd] + argv[2:]) return True from jina_cli.known_plugins import plugin_info if subcommand in plugin_info: from jina.helper import get_rich_console cmd_info = plugin_info[subcommand] project, package = cmd_info['display-name'], cmd_info['pip-package'] console = get_rich_console() console.print( f"It seems like [yellow]{project}[/yellow] is not installed in your environment." f"To use it via the [green]'jina {subcommand}'[/green] command, " f"install it first: [green]'pip install {package}'[/green]." ) return True return False def main(): """The main entrypoint of the CLI""" found_plugin = _try_plugin_command() if not found_plugin: _quick_ac_lookup() from jina_cli import api args = _get_run_args() getattr(api, args.cli.replace('-', '_'))(args)
_base_ = [ '../_base_/models/faster_rcnn_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] norm_cfg = dict(type='BN', requires_grad=True) image_size = (640, 640) batch_augments = [dict(type='BatchFixedSizePad', size=image_size)] model = dict( data_preprocessor=dict(pad_size_divisor=64, batch_augments=batch_augments), backbone=dict(norm_cfg=norm_cfg, norm_eval=False), neck=dict(norm_cfg=norm_cfg), roi_head=dict(bbox_head=dict(norm_cfg=norm_cfg))) dataset_type = 'CocoDataset' data_root = 'data/coco/' train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=image_size, ratio_range=(0.8, 1.2), keep_ratio=True), dict( type='RandomCrop', crop_type='absolute_range', crop_size=image_size, allow_negative_crop=True), dict(type='RandomFlip', prob=0.5), dict(type='PackDetInputs') ] test_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='Resize', scale=image_size, keep_ratio=True), dict( type='PackDetInputs', meta_keys=('img_id', 'img_path', 'ori_shape', 'img_shape', 'scale_factor')) ] train_dataloader = dict( batch_size=8, num_workers=4, dataset=dict(pipeline=train_pipeline)) val_dataloader = dict(dataset=dict(pipeline=test_pipeline)) test_dataloader = val_dataloader # learning policy max_epochs = 50 train_cfg = dict(max_epochs=max_epochs, val_interval=2) param_scheduler = [ dict(type='LinearLR', start_factor=0.1, by_epoch=False, begin=0, end=1000), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[30, 40], gamma=0.1) ] # optimizer optim_wrapper = dict( type='OptimWrapper', optimizer=dict(type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001), paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True), clip_grad=None) # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (8 GPUs) x (8 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = [ '../_base_/models/faster_rcnn_r50_fpn.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] norm_cfg = dict(type='BN', requires_grad=True) model = dict( backbone=dict(norm_cfg=norm_cfg, norm_eval=False), neck=dict(norm_cfg=norm_cfg), roi_head=dict(bbox_head=dict(norm_cfg=norm_cfg))) dataset_type = 'CocoDataset' data_root = 'data/coco/' img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True, with_mask=True), dict( type='Resize', img_scale=(640, 640), ratio_range=(0.8, 1.2), keep_ratio=True), dict(type='RandomCrop', crop_size=(640, 640)), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size=(640, 640)), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']), ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(640, 640), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict(type='Normalize', **img_norm_cfg), dict(type='Pad', size_divisor=64), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']), ]) ] data = dict( samples_per_gpu=8, workers_per_gpu=4, train=dict(pipeline=train_pipeline), val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # learning policy optimizer = dict( type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001, paramwise_cfg=dict(norm_decay_mult=0, bypass_duplicate=True)) optimizer_config = dict(grad_clip=None) # learning policy lr_config = dict( policy='step', warmup='linear', warmup_iters=1000, warmup_ratio=0.1, step=[30, 40]) # runtime settings runner = dict(max_epochs=50) evaluation = dict(interval=2) # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (8 GPUs) x (8 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
"""Utilities for loading configurations from langchain_core-hub.""" import warnings from typing import Any from langchain_core._api.deprecation import deprecated @deprecated( since="0.1.30", removal="1.0", message=( "Using the hwchase17/langchain-hub " "repo for prompts is deprecated. Please use " "<https://smith.langchain.com/hub> instead." ), ) def try_load_from_hub( *args: Any, # noqa: ARG001 **kwargs: Any, # noqa: ARG001 ) -> Any: """[DEPRECATED] Try to load from the old Hub.""" warnings.warn( "Loading from the deprecated github-based Hub is no longer supported. " "Please use the new LangChain Hub at https://smith.langchain.com/hub instead.", DeprecationWarning, stacklevel=2, ) # return None, which indicates that we shouldn't load from old hub # and might just be a filepath for e.g. load_chain return None
"""Utilities for loading configurations from langchain_core-hub.""" import warnings from typing import Any from langchain_core._api.deprecation import deprecated @deprecated( since="0.1.30", removal="1.0", message=( "Using the hwchase17/langchain-hub " "repo for prompts is deprecated. Please use " "<https://smith.langchain.com/hub> instead." ), ) def try_load_from_hub( *args: Any, **kwargs: Any, ) -> Any: """[DEPRECATED] Try to load from the old Hub.""" warnings.warn( "Loading from the deprecated github-based Hub is no longer supported. " "Please use the new LangChain Hub at https://smith.langchain.com/hub instead.", DeprecationWarning, stacklevel=2, ) # return None, which indicates that we shouldn't load from old hub # and might just be a filepath for e.g. load_chain return None
import pytest from absl.testing import parameterized from keras.src import backend from keras.src import layers from keras.src import testing class IdentityTest(testing.TestCase, parameterized.TestCase): @parameterized.named_parameters( [ {"testcase_name": "dense", "sparse": False}, {"testcase_name": "sparse", "sparse": True}, ] ) @pytest.mark.requires_trainable_backend def test_identity_basics(self, sparse): if sparse and not backend.SUPPORTS_SPARSE_TENSORS: pytest.skip("Backend does not support sparse tensors.") self.run_layer_test( layers.Identity, init_kwargs={}, input_shape=(2, 3), input_sparse=sparse, expected_output_shape=(2, 3), expected_output_sparse=sparse, expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, run_training_check=not sparse, supports_masking=True, assert_built_after_instantiation=True, )
import pytest from absl.testing import parameterized from keras.src import backend from keras.src import layers from keras.src import testing class IdentityTest(testing.TestCase, parameterized.TestCase): @parameterized.named_parameters( [ {"testcase_name": "dense", "sparse": False}, {"testcase_name": "sparse", "sparse": True}, ] ) @pytest.mark.requires_trainable_backend def test_identity_basics(self, sparse): if sparse and not backend.SUPPORTS_SPARSE_TENSORS: pytest.skip("Backend does not support sparse tensors.") self.run_layer_test( layers.Identity, init_kwargs={}, input_shape=(2, 3), input_sparse=sparse, expected_output_shape=(2, 3), expected_output_sparse=sparse, expected_num_trainable_weights=0, expected_num_non_trainable_weights=0, expected_num_seed_generators=0, expected_num_losses=0, run_training_check=not sparse, supports_masking=True, )
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] # model settings model = dict( type='VFNet', data_preprocessor=dict( type='DetDataPreprocessor', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], bgr_to_rgb=True, pad_size_divisor=32), 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', # use P5 num_outs=5, relu_before_extra_convs=True), bbox_head=dict( type='VFNetHead', num_classes=80, in_channels=256, stacked_convs=3, feat_channels=256, strides=[8, 16, 32, 64, 128], center_sampling=False, dcn_on_last_conv=False, use_atss=True, use_vfl=True, loss_cls=dict( type='VarifocalLoss', use_sigmoid=True, alpha=0.75, gamma=2.0, iou_weighted=True, loss_weight=1.0), loss_bbox=dict(type='GIoULoss', loss_weight=1.5), loss_bbox_refine=dict(type='GIoULoss', loss_weight=2.0)), # training and testing settings train_cfg=dict( assigner=dict(type='ATSSAssigner', topk=9), 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)) # data setting train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict(type='Resize', scale=(1333, 800), 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=(1333, 800), keep_ratio=True), dict(type='LoadAnnotations', with_bbox=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 # optimizer optim_wrapper = dict( optimizer=dict(lr=0.01), paramwise_cfg=dict(bias_lr_mult=2., bias_decay_mult=0.), clip_grad=None) # learning rate max_epochs = 12 param_scheduler = [ dict(type='LinearLR', start_factor=0.1, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[8, 11], gamma=0.1) ] train_cfg = dict(max_epochs=max_epochs)
_base_ = [ '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py' ] # model settings model = dict( type='VFNet', data_preprocessor=dict( type='DetDataPreprocessor', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], bgr_to_rgb=True, pad_size_divisor=32), 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', # use P5 num_outs=5, relu_before_extra_convs=True), bbox_head=dict( type='VFNetHead', num_classes=80, in_channels=256, stacked_convs=3, feat_channels=256, strides=[8, 16, 32, 64, 128], center_sampling=False, dcn_on_last_conv=False, use_atss=True, use_vfl=True, loss_cls=dict( type='VarifocalLoss', use_sigmoid=True, alpha=0.75, gamma=2.0, iou_weighted=True, loss_weight=1.0), loss_bbox=dict(type='GIoULoss', loss_weight=1.5), loss_bbox_refine=dict(type='GIoULoss', loss_weight=2.0)), # training and testing settings train_cfg=dict( assigner=dict(type='ATSSAssigner', topk=9), 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)) # data setting train_pipeline = [ dict( type='LoadImageFromFile', file_client_args={{_base_.file_client_args}}), dict(type='LoadAnnotations', with_bbox=True), dict(type='Resize', scale=(1333, 800), 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=(1333, 800), 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 # optimizer optim_wrapper = dict( optimizer=dict(lr=0.01), paramwise_cfg=dict(bias_lr_mult=2., bias_decay_mult=0.), clip_grad=None) # learning rate max_epochs = 12 param_scheduler = [ dict(type='LinearLR', start_factor=0.1, by_epoch=False, begin=0, end=500), dict( type='MultiStepLR', begin=0, end=max_epochs, by_epoch=True, milestones=[8, 11], gamma=0.1) ] train_cfg = dict(max_epochs=max_epochs)
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os from pathlib import Path import numpy as np import pytest from jina import Document, DocumentArray, Executor from jina.executors.metas import get_default_metas from jina_commons.indexers.dump import import_vectors from ..annoy_searcher import AnnoySearcher # fix the seed here np.random.seed(500) docs = DocumentArray([Document(embedding=np.random.random(10)) for i in range(10)]) search_doc = DocumentArray([Document(embedding=np.random.random(10))]) DUMP_PATH = 'tests/dump1' TOP_K = 5 @pytest.fixture(scope='function', autouse=True) def metas(tmpdir): os.environ['TEST_WORKSPACE'] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ['TEST_WORKSPACE'] yield metas del os.environ['TEST_WORKSPACE'] def test_config(): ex = Executor.load_config(str(Path(__file__).parents[1] / 'config.yml')) assert ex.metric == 'euclidean' def test_simple_annoy(): from annoy import AnnoyIndex _index = AnnoyIndex(5, 'angular') for j in range(3): _index.add_item(j, np.random.random((5,))) _index.build(4) idx1, _ = _index.get_nns_by_vector( np.random.random((5,)), 3, include_distances=True ) assert len(idx1) == 3 @pytest.mark.parametrize( ['metric', 'is_distance'], [ ('angular', True), ('euclidean', True), ('manhattan', True), ('hamming', True), ('dot', True), ('angular', False), ('euclidean', False), ('manhattan', False), ('hamming', False), ('dot', False), ], ) def test_metric(tmpdir, metric, is_distance): metas = { 'workspace': str(tmpdir), 'name': 'searcher', } runtime_args = {'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher( dump_path=DUMP_PATH, default_top_k=TOP_K, metas=metas, metric=metric, is_distance=is_distance, runtime_args=runtime_args, ) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) assert len(docs[0].matches) == TOP_K for i in range(len(docs[0].matches) - 1): if not is_distance: assert ( docs[0].matches[i].scores[metric].value >= docs[0].matches[i + 1].scores[metric].value ) else: assert ( docs[0].matches[i].scores[metric].value <= docs[0].matches[i + 1].scores[metric].value ) def test_query_vector(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher'} runtime_args = {'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher( dump_path=DUMP_PATH, default_top_k=TOP_K, metas=metas, runtime_args=runtime_args ) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) ids, vecs = import_vectors(DUMP_PATH, str(0)) ids = np.array(list(ids)) vecs = np.array(list(vecs)) assert len(docs) == 1 assert len(docs[0].matches) == TOP_K assert docs[0].matches[0].id in ids assert len(docs[0].matches[0].embedding) == 7 assert docs[0].matches[0].embedding in vecs da = DocumentArray([Document(id=0), Document(id=1), Document(id=2)]) indexer.fill_embedding(da) for i, doc in enumerate(da): assert list(doc.embedding) def test_query_vector_empty(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher'} runtime_args = {'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(default_top_k=TOP_K, metas=metas, runtime_args=runtime_args) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) assert len(docs[0].matches) == 0
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import os import numpy as np import pytest from jina import Document, DocumentArray from jina.executors.metas import get_default_metas from jina_commons.indexers.dump import import_vectors from ..annoy_searcher import AnnoySearcher # fix the seed here np.random.seed(500) docs = DocumentArray([Document(embedding=np.random.random(10)) for i in range(10)]) search_doc = DocumentArray([Document(embedding=np.random.random(10))]) DUMP_PATH = 'tests/dump1' TOP_K = 5 @pytest.fixture(scope='function', autouse=True) def metas(tmpdir): os.environ['TEST_WORKSPACE'] = str(tmpdir) metas = get_default_metas() metas['workspace'] = os.environ['TEST_WORKSPACE'] yield metas del os.environ['TEST_WORKSPACE'] def test_simple_annoy(): from annoy import AnnoyIndex _index = AnnoyIndex(5, 'angular') for j in range(3): _index.add_item(j, np.random.random((5,))) _index.build(4) idx1, _ = _index.get_nns_by_vector( np.random.random((5,)), 3, include_distances=True ) assert len(idx1) == 3 @pytest.mark.parametrize(['metric', 'is_distance'], [('angular', True), ('euclidean', True), ('manhattan', True), ('hamming', True), ('dot', True), ('angular', False), ('euclidean', False), ('manhattan', False), ('hamming', False), ('dot', False)]) def test_metric(tmpdir, metric, is_distance): metas = {'workspace': str(tmpdir), 'name': 'searcher', } runtime_args = {'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(dump_path=DUMP_PATH, default_top_k=TOP_K, metas=metas, metric=metric, is_distance=is_distance, runtime_args=runtime_args) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) assert len(docs[0].matches) == TOP_K for i in range(len(docs[0].matches) - 1): if not is_distance: assert docs[0].matches[i].scores[metric].value >= docs[0].matches[i + 1].scores[metric].value else: assert docs[0].matches[i].scores[metric].value <= docs[0].matches[i + 1].scores[metric].value def test_query_vector(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher'} runtime_args = {'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(dump_path=DUMP_PATH, default_top_k=TOP_K, metas=metas, runtime_args=runtime_args) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) ids, vecs = import_vectors(DUMP_PATH, str(0)) ids = np.array(list(ids)) vecs = np.array(list(vecs)) assert len(docs) == 1 assert len(docs[0].matches) == TOP_K assert docs[0].matches[0].id in ids assert len(docs[0].matches[0].embedding) == 7 assert docs[0].matches[0].embedding in vecs da = DocumentArray([Document(id=0), Document(id=1), Document(id=2)]) indexer.fill_embedding(da) for i, doc in enumerate(da): assert list(doc.embedding) def test_query_vector_empty(tmpdir): metas = {'workspace': str(tmpdir), 'name': 'searcher'} runtime_args = {'pea_id': 0, 'replica_id': 0} indexer = AnnoySearcher(default_top_k=TOP_K, metas=metas, runtime_args=runtime_args) docs = DocumentArray([Document(embedding=np.random.random(7))]) indexer.search(docs, {}) assert len(docs[0].matches) == 0
# Copyright 2025 The HuggingFace Team. All rights reserved. # # 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 .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers pkgs_to_check_at_runtime = "python requests filelock numpy".split() for pkg in pkgs_to_check_at_runtime: if pkg in deps: require_version_core(deps[pkg]) else: raise ValueError(f"can't find {pkg} in {deps.keys()}, check dependency_versions_table.py") def dep_version_check(pkg, hint=None): require_version(deps[pkg], hint)
# Copyright 2024 The HuggingFace Team. All rights reserved. # # 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 .dependency_versions_table import deps from .utils.versions import require_version, require_version_core # define which module versions we always want to check at run time # (usually the ones defined in `install_requires` in setup.py) # # order specific notes: # - tqdm must be checked before tokenizers pkgs_to_check_at_runtime = "python requests filelock numpy".split() for pkg in pkgs_to_check_at_runtime: if pkg in deps: require_version_core(deps[pkg]) else: raise ValueError(f"can't find {pkg} in {deps.keys()}, check dependency_versions_table.py") def dep_version_check(pkg, hint=None): require_version(deps[pkg], hint)
# Copyright (c) OpenMMLab. All rights reserved. from .optimizer import (OPTIMIZER_CONSTRUCTORS, OPTIMIZERS, DefaultOptimizerConstructor, build_optimizer) from .scheduler import (ConstantLR, ConstantMomentum, ConstantParamScheduler, CosineAnnealingLR, CosineAnnealingMomentum, CosineAnnealingParamScheduler, ExponentialLR, ExponentialMomentum, ExponentialParamScheduler, LinearLR, LinearMomentum, LinearParamScheduler, MultiStepLR, MultiStepMomentum, MultiStepParamScheduler, StepLR, StepMomentum, StepParamScheduler, _ParamScheduler) __all__ = [ 'OPTIMIZER_CONSTRUCTORS', 'OPTIMIZERS', 'build_optimizer', 'DefaultOptimizerConstructor', 'ConstantLR', 'CosineAnnealingLR', 'ExponentialLR', 'LinearLR', 'MultiStepLR', 'StepLR', 'ConstantMomentum', 'CosineAnnealingMomentum', 'ExponentialMomentum', 'LinearMomentum', 'MultiStepMomentum', 'StepMomentum', 'ConstantParamScheduler', 'CosineAnnealingParamScheduler', 'ExponentialParamScheduler', 'LinearParamScheduler', 'MultiStepParamScheduler', 'StepParamScheduler', '_ParamScheduler' ]
# Copyright (c) OpenMMLab. All rights reserved. from .optimizer import (OPTIMIZER_CONSTRUCTORS, OPTIMIZERS, DefaultOptimizerConstructor, build_optimizer, build_optimizer_constructor) from .scheduler import (ConstantLR, ConstantMomentum, ConstantParamScheduler, CosineAnnealingLR, CosineAnnealingMomentum, CosineAnnealingParamScheduler, ExponentialLR, ExponentialMomentum, ExponentialParamScheduler, LinearLR, LinearMomentum, LinearParamScheduler, MultiStepLR, MultiStepMomentum, MultiStepParamScheduler, StepLR, StepMomentum, StepParamScheduler, _ParamScheduler) __all__ = [ 'OPTIMIZER_CONSTRUCTORS', 'OPTIMIZERS', 'build_optimizer', 'build_optimizer_constructor', 'DefaultOptimizerConstructor', 'ConstantLR', 'CosineAnnealingLR', 'ExponentialLR', 'LinearLR', 'MultiStepLR', 'StepLR', 'ConstantMomentum', 'CosineAnnealingMomentum', 'ExponentialMomentum', 'LinearMomentum', 'MultiStepMomentum', 'StepMomentum', 'ConstantParamScheduler', 'CosineAnnealingParamScheduler', 'ExponentialParamScheduler', 'LinearParamScheduler', 'MultiStepParamScheduler', 'StepParamScheduler', '_ParamScheduler' ]
# Copyright (c) OpenMMLab. All rights reserved. from mmdet.registry import TASK_UTILS from mmdet.structures.bbox import bbox2distance, distance2bbox from .base_bbox_coder import BaseBBoxCoder @TASK_UTILS.register_module() class DistancePointBBoxCoder(BaseBBoxCoder): """Distance Point BBox coder. This coder encodes gt bboxes (x1, y1, x2, y2) into (top, bottom, left, right) and decode it back to the original. Args: clip_border (bool, optional): Whether clip the objects outside the border of the image. Defaults to True. """ def __init__(self, clip_border=True): super(BaseBBoxCoder, self).__init__() self.clip_border = clip_border def encode(self, points, gt_bboxes, max_dis=None, eps=0.1): """Encode bounding box to distances. Args: points (Tensor): Shape (N, 2), The format is [x, y]. gt_bboxes (Tensor): Shape (N, 4), The format is "xyxy" max_dis (float): Upper bound of the distance. Default None. eps (float): a small value to ensure target < max_dis, instead <=. Default 0.1. Returns: Tensor: Box transformation deltas. The shape is (N, 4). """ assert points.size(0) == gt_bboxes.size(0) assert points.size(-1) == 2 assert gt_bboxes.size(-1) == 4 return bbox2distance(points, gt_bboxes, max_dis, eps) def decode(self, points, pred_bboxes, max_shape=None): """Decode distance prediction to bounding box. Args: points (Tensor): Shape (B, N, 2) or (N, 2). pred_bboxes (Tensor): Distance from the given point to 4 boundaries (left, top, right, bottom). Shape (B, N, 4) or (N, 4) max_shape (Sequence[int] or torch.Tensor or Sequence[ Sequence[int]],optional): Maximum bounds for boxes, specifies (H, W, C) or (H, W). If priors shape is (B, N, 4), then the max_shape should be a Sequence[Sequence[int]], and the length of max_shape should also be B. Default None. Returns: Tensor: Boxes with shape (N, 4) or (B, N, 4) """ assert points.size(0) == pred_bboxes.size(0) assert points.size(-1) == 2 assert pred_bboxes.size(-1) == 4 if self.clip_border is False: max_shape = None return distance2bbox(points, pred_bboxes, max_shape)
# Copyright (c) OpenMMLab. All rights reserved. from mmdet.data_elements.bbox import bbox2distance, distance2bbox from mmdet.registry import TASK_UTILS from .base_bbox_coder import BaseBBoxCoder @TASK_UTILS.register_module() class DistancePointBBoxCoder(BaseBBoxCoder): """Distance Point BBox coder. This coder encodes gt bboxes (x1, y1, x2, y2) into (top, bottom, left, right) and decode it back to the original. Args: clip_border (bool, optional): Whether clip the objects outside the border of the image. Defaults to True. """ def __init__(self, clip_border=True): super(BaseBBoxCoder, self).__init__() self.clip_border = clip_border def encode(self, points, gt_bboxes, max_dis=None, eps=0.1): """Encode bounding box to distances. Args: points (Tensor): Shape (N, 2), The format is [x, y]. gt_bboxes (Tensor): Shape (N, 4), The format is "xyxy" max_dis (float): Upper bound of the distance. Default None. eps (float): a small value to ensure target < max_dis, instead <=. Default 0.1. Returns: Tensor: Box transformation deltas. The shape is (N, 4). """ assert points.size(0) == gt_bboxes.size(0) assert points.size(-1) == 2 assert gt_bboxes.size(-1) == 4 return bbox2distance(points, gt_bboxes, max_dis, eps) def decode(self, points, pred_bboxes, max_shape=None): """Decode distance prediction to bounding box. Args: points (Tensor): Shape (B, N, 2) or (N, 2). pred_bboxes (Tensor): Distance from the given point to 4 boundaries (left, top, right, bottom). Shape (B, N, 4) or (N, 4) max_shape (Sequence[int] or torch.Tensor or Sequence[ Sequence[int]],optional): Maximum bounds for boxes, specifies (H, W, C) or (H, W). If priors shape is (B, N, 4), then the max_shape should be a Sequence[Sequence[int]], and the length of max_shape should also be B. Default None. Returns: Tensor: Boxes with shape (N, 4) or (B, N, 4) """ assert points.size(0) == pred_bboxes.size(0) assert points.size(-1) == 2 assert pred_bboxes.size(-1) == 4 if self.clip_border is False: max_shape = None return distance2bbox(points, pred_bboxes, max_shape)
# coding=utf-8 # Copyright 2022 Meta Platforms, Inc. and The HuggingFace Inc. team. All rights reserved. # # 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. """RegNet model configuration""" from ...configuration_utils import PretrainedConfig from ...utils import logging logger = logging.get_logger(__name__) class RegNetConfig(PretrainedConfig): r""" This is the configuration class to store the configuration of a [`RegNetModel`]. It is used to instantiate a RegNet model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the RegNet [facebook/regnet-y-040](https://huggingface.co/facebook/regnet-y-040) architecture. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: num_channels (`int`, *optional*, defaults to 3): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. layer_type (`str`, *optional*, defaults to `"y"`): The layer to use, it can be either `"x" or `"y"`. An `x` layer is a ResNet's BottleNeck layer with `reduction` fixed to `1`. While a `y` layer is a `x` but with squeeze and excitation. Please refer to the paper for a detailed explanation of how these layers were constructed. hidden_act (`str`, *optional*, defaults to `"relu"`): The non-linear activation function in each block. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. downsample_in_first_stage (`bool`, *optional*, defaults to `False`): If `True`, the first stage will downsample the inputs using a `stride` of 2. Example: ```python >>> from transformers import RegNetConfig, RegNetModel >>> # Initializing a RegNet regnet-y-40 style configuration >>> configuration = RegNetConfig() >>> # Initializing a model from the regnet-y-40 style configuration >>> model = RegNetModel(configuration) >>> # Accessing the model configuration >>> configuration = model.config ``` """ model_type = "regnet" layer_types = ["x", "y"] def __init__( self, num_channels=3, embedding_size=32, hidden_sizes=[128, 192, 512, 1088], depths=[2, 6, 12, 2], groups_width=64, layer_type="y", hidden_act="relu", **kwargs, ): super().__init__(**kwargs) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types)}") self.num_channels = num_channels self.embedding_size = embedding_size self.hidden_sizes = hidden_sizes self.depths = depths self.groups_width = groups_width self.layer_type = layer_type self.hidden_act = hidden_act # always downsample in the first stage self.downsample_in_first_stage = True __all__ = ["RegNetConfig"]
# coding=utf-8 # Copyright 2022 Meta Platforms, Inc. and The HuggingFace Inc. team. All rights reserved. # # 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. """RegNet model configuration""" from ...configuration_utils import PretrainedConfig from ...utils import logging logger = logging.get_logger(__name__) class RegNetConfig(PretrainedConfig): r""" This is the configuration class to store the configuration of a [`RegNetModel`]. It is used to instantiate a RegNet model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the RegNet [facebook/regnet-y-040](https://huggingface.co/facebook/regnet-y-040) architecture. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information. Args: num_channels (`int`, *optional*, defaults to 3): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. hidden_sizes (`List[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. layer_type (`str`, *optional*, defaults to `"y"`): The layer to use, it can be either `"x" or `"y"`. An `x` layer is a ResNet's BottleNeck layer with `reduction` fixed to `1`. While a `y` layer is a `x` but with squeeze and excitation. Please refer to the paper for a detailed explanation of how these layers were constructed. hidden_act (`str`, *optional*, defaults to `"relu"`): The non-linear activation function in each block. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. downsample_in_first_stage (`bool`, *optional*, defaults to `False`): If `True`, the first stage will downsample the inputs using a `stride` of 2. Example: ```python >>> from transformers import RegNetConfig, RegNetModel >>> # Initializing a RegNet regnet-y-40 style configuration >>> configuration = RegNetConfig() >>> # Initializing a model from the regnet-y-40 style configuration >>> model = RegNetModel(configuration) >>> # Accessing the model configuration >>> configuration = model.config ``` """ model_type = "regnet" layer_types = ["x", "y"] def __init__( self, num_channels=3, embedding_size=32, hidden_sizes=[128, 192, 512, 1088], depths=[2, 6, 12, 2], groups_width=64, layer_type="y", hidden_act="relu", **kwargs, ): super().__init__(**kwargs) if layer_type not in self.layer_types: raise ValueError(f"layer_type={layer_type} is not one of {','.join(self.layer_types)}") self.num_channels = num_channels self.embedding_size = embedding_size self.hidden_sizes = hidden_sizes self.depths = depths self.groups_width = groups_width self.layer_type = layer_type self.hidden_act = hidden_act # always downsample in the first stage self.downsample_in_first_stage = True __all__ = ["RegNetConfig"]
"""DO NOT EDIT. This file was autogenerated. Do not edit it by hand, since your modifications would be overwritten. """ from keras.src.datasets.reuters import get_label_names as get_label_names from keras.src.datasets.reuters import get_word_index as get_word_index from keras.src.datasets.reuters 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.reuters import get_label_names from keras.src.datasets.reuters import get_word_index from keras.src.datasets.reuters import load_data
_base_ = [ '../_base_/models/ssd300.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' 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=(300, 300), 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=(300, 300), 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( samples_per_gpu=8, workers_per_gpu=3, train=dict( _delete_=True, type='RepeatDataset', 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=2e-3, momentum=0.9, weight_decay=5e-4) optimizer_config = dict(_delete_=True) custom_hooks = [ dict(type='NumClassCheckHook'), dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW') ] # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (8 GPUs) x (8 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = [ '../_base_/models/ssd300.py', '../_base_/datasets/coco_detection.py', '../_base_/schedules/schedule_2x.py', '../_base_/default_runtime.py' ] # dataset settings dataset_type = 'CocoDataset' data_root = 'data/coco/' 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=(300, 300), 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=(300, 300), 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( samples_per_gpu=8, workers_per_gpu=3, train=dict( _delete_=True, type='RepeatDataset', 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=2e-3, momentum=0.9, weight_decay=5e-4) optimizer_config = dict(_delete_=True) custom_hooks = [ dict(type='NumClassCheckHook'), dict(type='CheckInvalidLossHook', interval=50, priority='VERY_LOW') ]
# Licensed to the LF AI & Data foundation under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDoc from docarray.typing import ImageBytes, ImageNdArray, ImageTensor, ImageTorchTensor from docarray.utils._internal.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf from docarray.computation.tensorflow_backend import tnp from docarray.typing.tensor.image import ImageTensorFlowTensor @pytest.mark.parametrize( 'cls_tensor,tensor', [ (ImageTorchTensor, torch.zeros((224, 224, 3))), (ImageNdArray, np.zeros((224, 224, 3))), ], ) def test_save_image_tensor_to_file(cls_tensor, tensor, tmpdir): tmp_file = str(tmpdir / 'tmp.jpg') image_tensor = parse_obj_as(cls_tensor, tensor) image_tensor.save(tmp_file) assert os.path.isfile(tmp_file) @pytest.mark.tensorflow def test_save_image_tensorflow_tensor_to_file(tmpdir): tmp_file = str(tmpdir / 'tmp.jpg') image_tensor = parse_obj_as(ImageTensorFlowTensor, tf.zeros((224, 224, 3))) image_tensor.save(tmp_file) assert os.path.isfile(tmp_file) @pytest.mark.parametrize( 'image_tensor', [ parse_obj_as(ImageTorchTensor, torch.zeros(224, 224, 3)), parse_obj_as(ImageNdArray, np.zeros((224, 224, 3))), ], ) def test_save_image_tensor_to_bytes(image_tensor): b = image_tensor.to_bytes() isinstance(b, bytes) isinstance(b, ImageBytes) @pytest.mark.parametrize( 'tensor,cls_audio_tensor,cls_tensor', [ (torch.zeros(1000, 2), ImageTorchTensor, torch.Tensor), (np.zeros((1000, 2)), ImageNdArray, np.ndarray), ], ) def test_torch_ndarray_to_image_tensor(tensor, cls_audio_tensor, cls_tensor): class MyImageDoc(BaseDoc): tensor: ImageTensor doc = MyImageDoc(tensor=tensor) assert isinstance(doc.tensor, cls_audio_tensor) assert isinstance(doc.tensor, cls_tensor) assert (doc.tensor == tensor).all() @pytest.mark.tensorflow def test_tensorflow_to_image_tensor(): class MyImageDoc(BaseDoc): tensor: ImageTensor doc = MyImageDoc(tensor=tf.zeros((1000, 2))) assert isinstance(doc.tensor, ImageTensorFlowTensor) assert isinstance(doc.tensor.tensor, tf.Tensor) assert tnp.allclose(doc.tensor.tensor, tf.zeros((1000, 2)))
import os import numpy as np import pytest import torch from pydantic import parse_obj_as from docarray import BaseDoc from docarray.typing import ImageBytes, ImageNdArray, ImageTensor, ImageTorchTensor from docarray.utils._internal.misc import is_tf_available tf_available = is_tf_available() if tf_available: import tensorflow as tf from docarray.computation.tensorflow_backend import tnp from docarray.typing.tensor.image import ImageTensorFlowTensor @pytest.mark.parametrize( 'cls_tensor,tensor', [ (ImageTorchTensor, torch.zeros((224, 224, 3))), (ImageNdArray, np.zeros((224, 224, 3))), ], ) def test_save_image_tensor_to_file(cls_tensor, tensor, tmpdir): tmp_file = str(tmpdir / 'tmp.jpg') image_tensor = parse_obj_as(cls_tensor, tensor) image_tensor.save(tmp_file) assert os.path.isfile(tmp_file) @pytest.mark.tensorflow def test_save_image_tensorflow_tensor_to_file(tmpdir): tmp_file = str(tmpdir / 'tmp.jpg') image_tensor = parse_obj_as(ImageTensorFlowTensor, tf.zeros((224, 224, 3))) image_tensor.save(tmp_file) assert os.path.isfile(tmp_file) @pytest.mark.parametrize( 'image_tensor', [ parse_obj_as(ImageTorchTensor, torch.zeros(224, 224, 3)), parse_obj_as(ImageNdArray, np.zeros((224, 224, 3))), ], ) def test_save_image_tensor_to_bytes(image_tensor): b = image_tensor.to_bytes() isinstance(b, bytes) isinstance(b, ImageBytes) @pytest.mark.parametrize( 'tensor,cls_audio_tensor,cls_tensor', [ (torch.zeros(1000, 2), ImageTorchTensor, torch.Tensor), (np.zeros((1000, 2)), ImageNdArray, np.ndarray), ], ) def test_torch_ndarray_to_image_tensor(tensor, cls_audio_tensor, cls_tensor): class MyImageDoc(BaseDoc): tensor: ImageTensor doc = MyImageDoc(tensor=tensor) assert isinstance(doc.tensor, cls_audio_tensor) assert isinstance(doc.tensor, cls_tensor) assert (doc.tensor == tensor).all() @pytest.mark.tensorflow def test_tensorflow_to_image_tensor(): class MyImageDoc(BaseDoc): tensor: ImageTensor doc = MyImageDoc(tensor=tf.zeros((1000, 2))) assert isinstance(doc.tensor, ImageTensorFlowTensor) assert isinstance(doc.tensor.tensor, tf.Tensor) assert tnp.allclose(doc.tensor.tensor, tf.zeros((1000, 2)))
# Copyright (c) OpenMMLab. All rights reserved. import unittest import torch from parameterized import parameterized from mmdet.data_elements import DetDataSample from mmdet.models import build_detector from mmdet.testing._utils import demo_mm_inputs, get_detector_cfg from mmdet.utils import register_all_modules class TestTwoStagePanopticSegmentor(unittest.TestCase): def setUp(self): register_all_modules() def _create_model_cfg(self): cfg_file = 'panoptic_fpn/panoptic_fpn_r50_fpn_1x_coco.py' model_cfg = get_detector_cfg(cfg_file) model_cfg.backbone.depth = 18 model_cfg.neck.in_channels = [64, 128, 256, 512] model_cfg.backbone.init_cfg = None return model_cfg def test_init(self): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) assert detector.backbone assert detector.neck assert detector.rpn_head assert detector.roi_head assert detector.roi_head.mask_head assert detector.with_semantic_head assert detector.with_panoptic_fusion_head @parameterized.expand([('cpu', ), ('cuda', )]) def test_forward_loss_mode(self, device): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) if device == 'cuda' and not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') detector = detector.to(device) packed_inputs = demo_mm_inputs( 2, image_shapes=[(3, 128, 127), (3, 91, 92)], sem_seg_output_strides=1, with_mask=True, with_semantic=True) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, True) # Test loss mode losses = detector.forward(batch_inputs, data_samples, mode='loss') self.assertIsInstance(losses, dict) @parameterized.expand([('cpu', ), ('cuda', )]) def test_forward_predict_mode(self, device): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) if device == 'cuda' and not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') detector = detector.to(device) packed_inputs = demo_mm_inputs( 2, image_shapes=[(3, 128, 127), (3, 91, 92)], sem_seg_output_strides=1, with_mask=True, with_semantic=True) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) # Test forward test detector.eval() with torch.no_grad(): batch_results = detector.forward( batch_inputs, data_samples, mode='predict') self.assertEqual(len(batch_results), 2) self.assertIsInstance(batch_results[0], DetDataSample) @parameterized.expand([('cpu', ), ('cuda', )]) def test_forward_tensor_mode(self, device): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) if device == 'cuda' and not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') detector = detector.to(device) packed_inputs = demo_mm_inputs( 2, [[3, 128, 128], [3, 125, 130]], sem_seg_output_strides=1, with_mask=True, with_semantic=True) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) out = detector.forward(batch_inputs, data_samples, mode='tensor') self.assertIsInstance(out, tuple)
# Copyright (c) OpenMMLab. All rights reserved. import unittest import torch from parameterized import parameterized from mmdet.core import DetDataSample from mmdet.models import build_detector from mmdet.testing._utils import demo_mm_inputs, get_detector_cfg from mmdet.utils import register_all_modules class TestTwoStagePanopticSegmentor(unittest.TestCase): def setUp(self): register_all_modules() def _create_model_cfg(self): cfg_file = 'panoptic_fpn/panoptic_fpn_r50_fpn_1x_coco.py' model_cfg = get_detector_cfg(cfg_file) model_cfg.backbone.depth = 18 model_cfg.neck.in_channels = [64, 128, 256, 512] model_cfg.backbone.init_cfg = None return model_cfg def test_init(self): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) assert detector.backbone assert detector.neck assert detector.rpn_head assert detector.roi_head assert detector.roi_head.mask_head assert detector.with_semantic_head assert detector.with_panoptic_fusion_head @parameterized.expand([('cpu', ), ('cuda', )]) def test_forward_loss_mode(self, device): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) if device == 'cuda' and not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') detector = detector.to(device) packed_inputs = demo_mm_inputs( 2, image_shapes=[(3, 128, 127), (3, 91, 92)], sem_seg_output_strides=1, with_mask=True, with_semantic=True) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, True) # Test loss mode losses = detector.forward(batch_inputs, data_samples, mode='loss') self.assertIsInstance(losses, dict) @parameterized.expand([('cpu', ), ('cuda', )]) def test_forward_predict_mode(self, device): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) if device == 'cuda' and not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') detector = detector.to(device) packed_inputs = demo_mm_inputs( 2, image_shapes=[(3, 128, 127), (3, 91, 92)], sem_seg_output_strides=1, with_mask=True, with_semantic=True) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) # Test forward test detector.eval() with torch.no_grad(): batch_results = detector.forward( batch_inputs, data_samples, mode='predict') self.assertEqual(len(batch_results), 2) self.assertIsInstance(batch_results[0], DetDataSample) @parameterized.expand([('cpu', ), ('cuda', )]) def test_forward_tensor_mode(self, device): model_cfg = self._create_model_cfg() detector = build_detector(model_cfg) if device == 'cuda' and not torch.cuda.is_available(): return unittest.skip('test requires GPU and torch+cuda') detector = detector.to(device) packed_inputs = demo_mm_inputs( 2, [[3, 128, 128], [3, 125, 130]], sem_seg_output_strides=1, with_mask=True, with_semantic=True) batch_inputs, data_samples = detector.data_preprocessor( packed_inputs, False) out = detector.forward(batch_inputs, data_samples, mode='tensor') self.assertIsInstance(out, tuple)
"""Strategies for updater tests.""" from typing import cast import pytest strategies = pytest.importorskip("hypothesis.strategies") exact_parameter_strategy = strategies.fixed_dictionaries( { "nthread": strategies.integers(1, 4), "max_depth": strategies.integers(1, 11), "min_child_weight": strategies.floats(0.5, 2.0), "alpha": strategies.floats(1e-5, 2.0), "lambda": strategies.floats(1e-5, 2.0), "eta": strategies.floats(0.01, 0.5), "gamma": strategies.floats(1e-5, 2.0), "seed": strategies.integers(0, 10), # We cannot enable subsampling as the training loss can increase # 'subsample': strategies.floats(0.5, 1.0), "colsample_bytree": strategies.floats(0.5, 1.0), "colsample_bylevel": strategies.floats(0.5, 1.0), } ) hist_parameter_strategy = strategies.fixed_dictionaries( { "max_depth": strategies.integers(1, 11), "max_leaves": strategies.integers(0, 1024), "max_bin": strategies.integers(2, 512), "grow_policy": strategies.sampled_from(["lossguide", "depthwise"]), "min_child_weight": strategies.floats(0.5, 2.0), # We cannot enable subsampling as the training loss can increase # 'subsample': strategies.floats(0.5, 1.0), "colsample_bytree": strategies.floats(0.5, 1.0), "colsample_bylevel": strategies.floats(0.5, 1.0), } ).filter( lambda x: (cast(int, x["max_depth"]) > 0 or cast(int, x["max_leaves"]) > 0) and (cast(int, x["max_depth"]) > 0 or x["grow_policy"] == "lossguide") ) hist_cache_strategy = strategies.fixed_dictionaries( {"max_cached_hist_node": strategies.sampled_from([1, 4, 1024, 2**31])} ) hist_multi_parameter_strategy = strategies.fixed_dictionaries( { "max_depth": strategies.integers(1, 11), "max_leaves": strategies.integers(0, 1024), "max_bin": strategies.integers(2, 512), "multi_strategy": strategies.sampled_from( ["multi_output_tree", "one_output_per_tree"] ), "grow_policy": strategies.sampled_from(["lossguide", "depthwise"]), "min_child_weight": strategies.floats(0.5, 2.0), # We cannot enable subsampling as the training loss can increase # 'subsample': strategies.floats(0.5, 1.0), "colsample_bytree": strategies.floats(0.5, 1.0), "colsample_bylevel": strategies.floats(0.5, 1.0), } ).filter( lambda x: (cast(int, x["max_depth"]) > 0 or cast(int, x["max_leaves"]) > 0) and (cast(int, x["max_depth"]) > 0 or x["grow_policy"] == "lossguide") ) cat_parameter_strategy = strategies.fixed_dictionaries( { "max_cat_to_onehot": strategies.integers(1, 128), "max_cat_threshold": strategies.integers(1, 128), } ) lambdarank_parameter_strategy = strategies.fixed_dictionaries( { "lambdarank_unbiased": strategies.sampled_from([True, False]), "lambdarank_pair_method": strategies.sampled_from(["topk", "mean"]), "lambdarank_num_pair_per_sample": strategies.integers(1, 8), "lambdarank_bias_norm": strategies.floats(0.5, 2.0), "objective": strategies.sampled_from( ["rank:ndcg", "rank:map", "rank:pairwise"] ), } ).filter( lambda x: not (x["lambdarank_unbiased"] and x["lambdarank_pair_method"] == "mean") )
"""Strategies for updater tests.""" from typing import cast import pytest strategies = pytest.importorskip("hypothesis.strategies") exact_parameter_strategy = strategies.fixed_dictionaries( { "nthread": strategies.integers(1, 4), "max_depth": strategies.integers(1, 11), "min_child_weight": strategies.floats(0.5, 2.0), "alpha": strategies.floats(1e-5, 2.0), "lambda": strategies.floats(1e-5, 2.0), "eta": strategies.floats(0.01, 0.5), "gamma": strategies.floats(1e-5, 2.0), "seed": strategies.integers(0, 10), # We cannot enable subsampling as the training loss can increase # 'subsample': strategies.floats(0.5, 1.0), "colsample_bytree": strategies.floats(0.5, 1.0), "colsample_bylevel": strategies.floats(0.5, 1.0), } ) hist_parameter_strategy = strategies.fixed_dictionaries( { "max_depth": strategies.integers(1, 11), "max_leaves": strategies.integers(0, 1024), "max_bin": strategies.integers(2, 512), "grow_policy": strategies.sampled_from(["lossguide", "depthwise"]), "min_child_weight": strategies.floats(0.5, 2.0), # We cannot enable subsampling as the training loss can increase # 'subsample': strategies.floats(0.5, 1.0), "colsample_bytree": strategies.floats(0.5, 1.0), "colsample_bylevel": strategies.floats(0.5, 1.0), } ).filter( lambda x: (cast(int, x["max_depth"]) > 0 or cast(int, x["max_leaves"]) > 0) and (cast(int, x["max_depth"]) > 0 or x["grow_policy"] == "lossguide") ) hist_cache_strategy = strategies.fixed_dictionaries( {"internal_max_cached_hist_node": strategies.sampled_from([1, 4, 1024, 2**31])} ) hist_multi_parameter_strategy = strategies.fixed_dictionaries( { "max_depth": strategies.integers(1, 11), "max_leaves": strategies.integers(0, 1024), "max_bin": strategies.integers(2, 512), "multi_strategy": strategies.sampled_from( ["multi_output_tree", "one_output_per_tree"] ), "grow_policy": strategies.sampled_from(["lossguide", "depthwise"]), "min_child_weight": strategies.floats(0.5, 2.0), # We cannot enable subsampling as the training loss can increase # 'subsample': strategies.floats(0.5, 1.0), "colsample_bytree": strategies.floats(0.5, 1.0), "colsample_bylevel": strategies.floats(0.5, 1.0), } ).filter( lambda x: (cast(int, x["max_depth"]) > 0 or cast(int, x["max_leaves"]) > 0) and (cast(int, x["max_depth"]) > 0 or x["grow_policy"] == "lossguide") ) cat_parameter_strategy = strategies.fixed_dictionaries( { "max_cat_to_onehot": strategies.integers(1, 128), "max_cat_threshold": strategies.integers(1, 128), } ) lambdarank_parameter_strategy = strategies.fixed_dictionaries( { "lambdarank_unbiased": strategies.sampled_from([True, False]), "lambdarank_pair_method": strategies.sampled_from(["topk", "mean"]), "lambdarank_num_pair_per_sample": strategies.integers(1, 8), "lambdarank_bias_norm": strategies.floats(0.5, 2.0), "objective": strategies.sampled_from( ["rank:ndcg", "rank:map", "rank:pairwise"] ), } ).filter( lambda x: not (x["lambdarank_unbiased"] and x["lambdarank_pair_method"] == "mean") )
from __future__ import annotations from collections.abc import Sequence from copy import deepcopy from typing import Any, Optional, Union from langchain_core._api.deprecation import deprecated from langchain_core.callbacks import Callbacks from langchain_core.documents import BaseDocumentCompressor, Document from langchain_core.utils import get_from_dict_or_env from pydantic import ConfigDict, model_validator @deprecated( since="0.0.30", removal="1.0", alternative_import="langchain_cohere.CohereRerank", ) class CohereRerank(BaseDocumentCompressor): """Document compressor that uses `Cohere Rerank API`.""" client: Any = None """Cohere client to use for compressing documents.""" top_n: Optional[int] = 3 """Number of documents to return.""" model: str = "rerank-english-v2.0" """Model to use for reranking.""" cohere_api_key: Optional[str] = None """Cohere API key. Must be specified directly or via environment variable COHERE_API_KEY.""" user_agent: str = "langchain" """Identifier for the application making the request.""" model_config = ConfigDict( arbitrary_types_allowed=True, extra="forbid", ) @model_validator(mode="before") @classmethod def validate_environment(cls, values: dict) -> Any: """Validate that api key and python package exists in environment.""" if not values.get("client"): try: import cohere except ImportError as e: msg = ( "Could not import cohere python package. " "Please install it with `pip install cohere`." ) raise ImportError(msg) from e cohere_api_key = get_from_dict_or_env( values, "cohere_api_key", "COHERE_API_KEY", ) client_name = values.get("user_agent", "langchain") values["client"] = cohere.Client(cohere_api_key, client_name=client_name) return values def rerank( self, documents: Sequence[Union[str, Document, dict]], query: str, *, model: Optional[str] = None, top_n: Optional[int] = -1, max_chunks_per_doc: Optional[int] = None, ) -> list[dict[str, Any]]: """Returns an ordered list of documents ordered by their relevance to the provided query. Args: query: The query to use for reranking. documents: A sequence of documents to rerank. model: The model to use for re-ranking. Default to self.model. top_n : The number of results to return. If None returns all results. Defaults to self.top_n. max_chunks_per_doc : The maximum number of chunks derived from a document. """ # noqa: E501 if len(documents) == 0: # to avoid empty api call return [] docs = [ doc.page_content if isinstance(doc, Document) else doc for doc in documents ] model = model or self.model top_n = top_n if (top_n is None or top_n > 0) else self.top_n results = self.client.rerank( query=query, documents=docs, model=model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc, ) if hasattr(results, "results"): results = results.results return [ {"index": res.index, "relevance_score": res.relevance_score} for res in results ] def compress_documents( self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks] = None, ) -> Sequence[Document]: """ Compress documents using Cohere's rerank API. Args: documents: A sequence of documents to compress. query: The query to use for compressing the documents. callbacks: Callbacks to run during the compression process. Returns: A sequence of compressed documents. """ compressed = [] for res in self.rerank(documents, query): doc = documents[res["index"]] doc_copy = Document(doc.page_content, metadata=deepcopy(doc.metadata)) doc_copy.metadata["relevance_score"] = res["relevance_score"] compressed.append(doc_copy) return compressed
from __future__ import annotations from collections.abc import Sequence from copy import deepcopy from typing import Any, Optional, Union from langchain_core._api.deprecation import deprecated from langchain_core.callbacks import Callbacks from langchain_core.documents import BaseDocumentCompressor, Document from langchain_core.utils import get_from_dict_or_env from pydantic import ConfigDict, model_validator @deprecated( since="0.0.30", removal="1.0", alternative_import="langchain_cohere.CohereRerank", ) class CohereRerank(BaseDocumentCompressor): """Document compressor that uses `Cohere Rerank API`.""" client: Any = None """Cohere client to use for compressing documents.""" top_n: Optional[int] = 3 """Number of documents to return.""" model: str = "rerank-english-v2.0" """Model to use for reranking.""" cohere_api_key: Optional[str] = None """Cohere API key. Must be specified directly or via environment variable COHERE_API_KEY.""" user_agent: str = "langchain" """Identifier for the application making the request.""" model_config = ConfigDict( arbitrary_types_allowed=True, extra="forbid", ) @model_validator(mode="before") @classmethod def validate_environment(cls, values: dict) -> Any: """Validate that api key and python package exists in environment.""" if not values.get("client"): try: import cohere except ImportError: msg = ( "Could not import cohere python package. " "Please install it with `pip install cohere`." ) raise ImportError(msg) cohere_api_key = get_from_dict_or_env( values, "cohere_api_key", "COHERE_API_KEY", ) client_name = values.get("user_agent", "langchain") values["client"] = cohere.Client(cohere_api_key, client_name=client_name) return values def rerank( self, documents: Sequence[Union[str, Document, dict]], query: str, *, model: Optional[str] = None, top_n: Optional[int] = -1, max_chunks_per_doc: Optional[int] = None, ) -> list[dict[str, Any]]: """Returns an ordered list of documents ordered by their relevance to the provided query. Args: query: The query to use for reranking. documents: A sequence of documents to rerank. model: The model to use for re-ranking. Default to self.model. top_n : The number of results to return. If None returns all results. Defaults to self.top_n. max_chunks_per_doc : The maximum number of chunks derived from a document. """ # noqa: E501 if len(documents) == 0: # to avoid empty api call return [] docs = [ doc.page_content if isinstance(doc, Document) else doc for doc in documents ] model = model or self.model top_n = top_n if (top_n is None or top_n > 0) else self.top_n results = self.client.rerank( query=query, documents=docs, model=model, top_n=top_n, max_chunks_per_doc=max_chunks_per_doc, ) if hasattr(results, "results"): results = getattr(results, "results") return [ {"index": res.index, "relevance_score": res.relevance_score} for res in results ] def compress_documents( self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks] = None, ) -> Sequence[Document]: """ Compress documents using Cohere's rerank API. Args: documents: A sequence of documents to compress. query: The query to use for compressing the documents. callbacks: Callbacks to run during the compression process. Returns: A sequence of compressed documents. """ compressed = [] for res in self.rerank(documents, query): doc = documents[res["index"]] doc_copy = Document(doc.page_content, metadata=deepcopy(doc.metadata)) doc_copy.metadata["relevance_score"] = res["relevance_score"] compressed.append(doc_copy) return compressed
from backend.data.block import ( Block, BlockCategory, BlockManualWebhookConfig, BlockOutput, BlockSchema, ) from backend.data.model import SchemaField from backend.integrations.providers import ProviderName from backend.integrations.webhooks.generic import GenericWebhookType class GenericWebhookTriggerBlock(Block): class Input(BlockSchema): payload: dict = SchemaField(hidden=True, default_factory=dict) constants: dict = SchemaField( description="The constants to be set when the block is put on the graph", default_factory=dict, ) class Output(BlockSchema): payload: dict = SchemaField( description="The complete webhook payload that was received from the generic webhook." ) constants: dict = SchemaField( description="The constants to be set when the block is put on the graph" ) example_payload = {"message": "Hello, World!"} def __init__(self): super().__init__( id="8fa8c167-2002-47ce-aba8-97572fc5d387", description="This block will output the contents of the generic input for the webhook.", categories={BlockCategory.INPUT}, input_schema=GenericWebhookTriggerBlock.Input, output_schema=GenericWebhookTriggerBlock.Output, webhook_config=BlockManualWebhookConfig( provider=ProviderName.GENERIC_WEBHOOK, webhook_type=GenericWebhookType.PLAIN, ), test_input={"constants": {"key": "value"}, "payload": self.example_payload}, test_output=[ ("constants", {"key": "value"}), ("payload", self.example_payload), ], ) async def run(self, input_data: Input, **kwargs) -> BlockOutput: yield "constants", input_data.constants yield "payload", input_data.payload
from backend.data.block import ( Block, BlockCategory, BlockManualWebhookConfig, BlockOutput, BlockSchema, ) from backend.data.model import SchemaField from backend.integrations.providers import ProviderName from backend.integrations.webhooks.generic import GenericWebhookType class GenericWebhookTriggerBlock(Block): class Input(BlockSchema): payload: dict = SchemaField(hidden=True, default_factory=dict) constants: dict = SchemaField( description="The constants to be set when the block is put on the graph", default_factory=dict, ) class Output(BlockSchema): payload: dict = SchemaField( description="The complete webhook payload that was received from the generic webhook." ) constants: dict = SchemaField( description="The constants to be set when the block is put on the graph" ) example_payload = {"message": "Hello, World!"} def __init__(self): super().__init__( id="8fa8c167-2002-47ce-aba8-97572fc5d387", description="This block will output the contents of the generic input for the webhook.", categories={BlockCategory.INPUT}, input_schema=GenericWebhookTriggerBlock.Input, output_schema=GenericWebhookTriggerBlock.Output, webhook_config=BlockManualWebhookConfig( provider=ProviderName.GENERIC_WEBHOOK, webhook_type=GenericWebhookType.PLAIN, ), test_input={"constants": {"key": "value"}, "payload": self.example_payload}, test_output=[ ("constants", {"key": "value"}), ("payload", self.example_payload), ], ) def run(self, input_data: Input, **kwargs) -> BlockOutput: yield "constants", input_data.constants yield "payload", input_data.payload
# Copyright (c) OpenMMLab. All rights reserved. from .atss import ATSS from .autoassign import AutoAssign from .base import BaseDetector from .cascade_rcnn import CascadeRCNN from .centernet import CenterNet from .cornernet import CornerNet from .ddod import DDOD from .deformable_detr import DeformableDETR from .detr import DETR from .fast_rcnn import FastRCNN from .faster_rcnn import FasterRCNN from .fcos import FCOS from .fovea import FOVEA from .fsaf import FSAF from .gfl import GFL from .grid_rcnn import GridRCNN from .htc import HybridTaskCascade from .kd_one_stage import KnowledgeDistillationSingleStageDetector from .lad import LAD from .mask2former import Mask2Former from .mask_rcnn import MaskRCNN from .mask_scoring_rcnn import MaskScoringRCNN from .maskformer import MaskFormer from .nasfcos import NASFCOS from .paa import PAA from .panoptic_fpn import PanopticFPN from .panoptic_two_stage_segmentor import TwoStagePanopticSegmentor from .point_rend import PointRend from .queryinst import QueryInst from .reppoints_detector import RepPointsDetector from .retinanet import RetinaNet from .rpn import RPN from .scnet import SCNet from .single_stage import SingleStageDetector from .solo import SOLO from .sparse_rcnn import SparseRCNN from .tood import TOOD from .trident_faster_rcnn import TridentFasterRCNN from .two_stage import TwoStageDetector from .vfnet import VFNet from .yolact import YOLACT from .yolo import YOLOV3 from .yolof import YOLOF from .yolox import YOLOX __all__ = [ 'ATSS', 'BaseDetector', 'SingleStageDetector', 'TwoStageDetector', 'RPN', 'KnowledgeDistillationSingleStageDetector', 'FastRCNN', 'FasterRCNN', 'MaskRCNN', 'CascadeRCNN', 'HybridTaskCascade', 'RetinaNet', 'FCOS', 'GridRCNN', 'MaskScoringRCNN', 'RepPointsDetector', 'FOVEA', 'FSAF', 'NASFCOS', 'PointRend', 'GFL', 'CornerNet', 'PAA', 'YOLOV3', 'YOLACT', 'VFNet', 'DETR', 'TridentFasterRCNN', 'SparseRCNN', 'SCNet', 'SOLO', 'DeformableDETR', 'AutoAssign', 'YOLOF', 'CenterNet', 'YOLOX', 'TwoStagePanopticSegmentor', 'PanopticFPN', 'QueryInst', 'LAD', 'TOOD', 'MaskFormer', 'DDOD', 'Mask2Former' ]
# Copyright (c) OpenMMLab. All rights reserved. from .atss import ATSS from .autoassign import AutoAssign from .base import BaseDetector from .cascade_rcnn import CascadeRCNN from .centernet import CenterNet from .cornernet import CornerNet from .deformable_detr import DeformableDETR from .detr import DETR from .fast_rcnn import FastRCNN from .faster_rcnn import FasterRCNN from .fcos import FCOS from .fovea import FOVEA from .fsaf import FSAF from .gfl import GFL from .grid_rcnn import GridRCNN from .htc import HybridTaskCascade from .kd_one_stage import KnowledgeDistillationSingleStageDetector from .lad import LAD from .mask2former import Mask2Former from .mask_rcnn import MaskRCNN from .mask_scoring_rcnn import MaskScoringRCNN from .maskformer import MaskFormer from .nasfcos import NASFCOS from .paa import PAA from .panoptic_fpn import PanopticFPN from .panoptic_two_stage_segmentor import TwoStagePanopticSegmentor from .point_rend import PointRend from .queryinst import QueryInst from .reppoints_detector import RepPointsDetector from .retinanet import RetinaNet from .rpn import RPN from .scnet import SCNet from .single_stage import SingleStageDetector from .solo import SOLO from .sparse_rcnn import SparseRCNN from .tood import TOOD from .trident_faster_rcnn import TridentFasterRCNN from .two_stage import TwoStageDetector from .vfnet import VFNet from .yolact import YOLACT from .yolo import YOLOV3 from .yolof import YOLOF from .yolox import YOLOX __all__ = [ 'ATSS', 'BaseDetector', 'SingleStageDetector', 'TwoStageDetector', 'RPN', 'KnowledgeDistillationSingleStageDetector', 'FastRCNN', 'FasterRCNN', 'MaskRCNN', 'CascadeRCNN', 'HybridTaskCascade', 'RetinaNet', 'FCOS', 'GridRCNN', 'MaskScoringRCNN', 'RepPointsDetector', 'FOVEA', 'FSAF', 'NASFCOS', 'PointRend', 'GFL', 'CornerNet', 'PAA', 'YOLOV3', 'YOLACT', 'VFNet', 'DETR', 'TridentFasterRCNN', 'SparseRCNN', 'SCNet', 'SOLO', 'DeformableDETR', 'AutoAssign', 'YOLOF', 'CenterNet', 'YOLOX', 'TwoStagePanopticSegmentor', 'PanopticFPN', 'QueryInst', 'LAD', 'TOOD', 'MaskFormer', 'Mask2Former' ]
import pathlib from typing import Any, Optional import pytest from langchain.callbacks import FileCallbackHandler from langchain.chains.base import CallbackManagerForChainRun, Chain class FakeChain(Chain): """Fake chain class for testing purposes.""" be_correct: bool = True the_input_keys: list[str] = ["foo"] the_output_keys: list[str] = ["bar"] @property def input_keys(self) -> list[str]: """Input keys.""" return self.the_input_keys @property def output_keys(self) -> list[str]: """Output key of bar.""" return self.the_output_keys def _call( self, inputs: dict[str, str], run_manager: Optional[CallbackManagerForChainRun] = None, ) -> dict[str, str]: return {"bar": "bar"} def test_filecallback(capsys: pytest.CaptureFixture, tmp_path: pathlib.Path) -> Any: """Test the file callback handler.""" p = tmp_path / "output.log" handler = FileCallbackHandler(str(p)) chain_test = FakeChain(callbacks=[handler]) chain_test.invoke({"foo": "bar"}) # Assert the output is as expected assert p.read_text() == ( "\n\n\x1b[1m> Entering new FakeChain " "chain...\x1b[0m\n\n\x1b[1m> Finished chain.\x1b[0m\n" )
import pathlib from typing import Any, Dict, List, Optional import pytest from langchain.callbacks import FileCallbackHandler from langchain.chains.base import CallbackManagerForChainRun, Chain class FakeChain(Chain): """Fake chain class for testing purposes.""" be_correct: bool = True the_input_keys: List[str] = ["foo"] the_output_keys: List[str] = ["bar"] @property def input_keys(self) -> List[str]: """Input keys.""" return self.the_input_keys @property def output_keys(self) -> List[str]: """Output key of bar.""" return self.the_output_keys def _call( self, inputs: Dict[str, str], run_manager: Optional[CallbackManagerForChainRun] = None, ) -> Dict[str, str]: return {"bar": "bar"} def test_filecallback(capsys: pytest.CaptureFixture, tmp_path: pathlib.Path) -> Any: """Test the file callback handler.""" p = tmp_path / "output.log" handler = FileCallbackHandler(str(p)) chain_test = FakeChain(callbacks=[handler]) chain_test.invoke({"foo": "bar"}) # Assert the output is as expected assert p.read_text() == ( "\n\n\x1b[1m> Entering new FakeChain " "chain...\x1b[0m\n\n\x1b[1m> Finished chain.\x1b[0m\n" )
from typing import List import requests from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document class MainContentExtractorReader(BaseReader): """ MainContentExtractor web page reader. Reads pages from the web. Args: text_format (str, optional): The format of the text. Defaults to "markdown". Requires `MainContentExtractor` package. """ def __init__(self, text_format: str = "markdown") -> None: """Initialize with parameters.""" self.text_format = text_format def load_data(self, urls: List[str]) -> List[Document]: """ Load data from the input directory. Args: urls (List[str]): List of URLs to scrape. Returns: List[Document]: List of documents. """ if not isinstance(urls, list): raise ValueError("urls must be a list of strings.") from main_content_extractor import MainContentExtractor documents = [] for url in urls: response = requests.get(url).text response = MainContentExtractor.extract( response, output_format=self.text_format, include_links=False ) documents.append(Document(text=response)) return documents
from typing import List import requests from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document class MainContentExtractorReader(BaseReader): """MainContentExtractor web page reader. Reads pages from the web. Args: text_format (str, optional): The format of the text. Defaults to "markdown". Requires `MainContentExtractor` package. """ def __init__(self, text_format: str = "markdown") -> None: """Initialize with parameters.""" self.text_format = text_format def load_data(self, urls: List[str]) -> List[Document]: """Load data from the input directory. Args: urls (List[str]): List of URLs to scrape. Returns: List[Document]: List of documents. """ if not isinstance(urls, list): raise ValueError("urls must be a list of strings.") from main_content_extractor import MainContentExtractor documents = [] for url in urls: response = requests.get(url).text response = MainContentExtractor.extract( response, output_format=self.text_format, include_links=False ) documents.append(Document(text=response)) return documents
# Copyright (c) OpenMMLab. All rights reserved. from unittest import TestCase import torch from mmengine.structures import InstanceData from mmdet.models.dense_heads import CenterNetUpdateHead class TestCenterNetUpdateHead(TestCase): def test_centernet_update_head_loss(self): """Tests fcos head loss when truth is empty and non-empty.""" s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'pad_shape': (s, s, 3), 'scale_factor': 1, }] centernet_head = CenterNetUpdateHead( num_classes=4, in_channels=1, feat_channels=1, stacked_convs=1, norm_cfg=None) # Fcos head expects a multiple levels of features per image feats = ( torch.rand(1, 1, s // stride[1], s // stride[0]) for stride in centernet_head.prior_generator.strides) cls_scores, bbox_preds = centernet_head.forward(feats) # Test that empty ground truth encourages the network to # predict background gt_instances = InstanceData() gt_instances.bboxes = torch.empty((0, 4)) gt_instances.labels = torch.LongTensor([]) empty_gt_losses = centernet_head.loss_by_feat(cls_scores, bbox_preds, [gt_instances], img_metas) # When there is no truth, the cls loss should be nonzero but # box loss and centerness loss should be zero empty_cls_loss = empty_gt_losses['loss_cls'].item() empty_box_loss = empty_gt_losses['loss_bbox'].item() self.assertGreater(empty_cls_loss, 0, 'cls loss should be non-zero') self.assertEqual( empty_box_loss, 0, 'there should be no box loss when there are no true boxes') # When truth is non-empty then all cls, box loss and centerness loss # should be nonzero for random inputs gt_instances = InstanceData() gt_instances.bboxes = torch.Tensor( [[23.6667, 23.8757, 238.6326, 151.8874]]) gt_instances.labels = torch.LongTensor([2]) one_gt_losses = centernet_head.loss_by_feat(cls_scores, bbox_preds, [gt_instances], img_metas) onegt_cls_loss = one_gt_losses['loss_cls'].item() onegt_box_loss = one_gt_losses['loss_bbox'].item() self.assertGreater(onegt_cls_loss, 0, 'cls loss should be non-zero') self.assertGreater(onegt_box_loss, 0, 'box loss should be non-zero')
# Copyright (c) OpenMMLab. All rights reserved. from unittest import TestCase import torch from mmengine.data import InstanceData from mmdet.models.dense_heads import CenterNetUpdateHead class TestCenterNetUpdateHead(TestCase): def test_centernet_update_head_loss(self): """Tests fcos head loss when truth is empty and non-empty.""" s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'pad_shape': (s, s, 3), 'scale_factor': 1, }] centernet_head = CenterNetUpdateHead( num_classes=4, in_channels=1, feat_channels=1, stacked_convs=1, norm_cfg=None) # Fcos head expects a multiple levels of features per image feats = ( torch.rand(1, 1, s // stride[1], s // stride[0]) for stride in centernet_head.prior_generator.strides) cls_scores, bbox_preds = centernet_head.forward(feats) # Test that empty ground truth encourages the network to # predict background gt_instances = InstanceData() gt_instances.bboxes = torch.empty((0, 4)) gt_instances.labels = torch.LongTensor([]) empty_gt_losses = centernet_head.loss_by_feat(cls_scores, bbox_preds, [gt_instances], img_metas) # When there is no truth, the cls loss should be nonzero but # box loss and centerness loss should be zero empty_cls_loss = empty_gt_losses['loss_cls'].item() empty_box_loss = empty_gt_losses['loss_bbox'].item() self.assertGreater(empty_cls_loss, 0, 'cls loss should be non-zero') self.assertEqual( empty_box_loss, 0, 'there should be no box loss when there are no true boxes') # When truth is non-empty then all cls, box loss and centerness loss # should be nonzero for random inputs gt_instances = InstanceData() gt_instances.bboxes = torch.Tensor( [[23.6667, 23.8757, 238.6326, 151.8874]]) gt_instances.labels = torch.LongTensor([2]) one_gt_losses = centernet_head.loss_by_feat(cls_scores, bbox_preds, [gt_instances], img_metas) onegt_cls_loss = one_gt_losses['loss_cls'].item() onegt_box_loss = one_gt_losses['loss_bbox'].item() self.assertGreater(onegt_cls_loss, 0, 'cls loss should be non-zero') self.assertGreater(onegt_box_loss, 0, 'box loss should be non-zero')
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 = {} _import_structure = {} 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_sana"] = ["SanaPipeline"] _import_structure["pipeline_sana_controlnet"] = ["SanaControlNetPipeline"] _import_structure["pipeline_sana_sprint"] = ["SanaSprintPipeline"] 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 * else: from .pipeline_sana import SanaPipeline from .pipeline_sana_controlnet import SanaControlNetPipeline from .pipeline_sana_sprint import SanaSprintPipeline 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)
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 = {} _import_structure = {} 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_sana"] = ["SanaPipeline"] _import_structure["pipeline_sana_sprint"] = ["SanaSprintPipeline"] 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 * else: from .pipeline_sana import SanaPipeline from .pipeline_sana_sprint import SanaSprintPipeline 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)
from __future__ import annotations from .CSRLoss import CSRLoss, CSRReconstructionLoss from .FlopsLoss import FlopsLoss from .SparseAnglELoss import SparseAnglELoss from .SparseCoSENTLoss import SparseCoSENTLoss from .SparseCosineSimilarityLoss import SparseCosineSimilarityLoss from .SparseDistillKLDivLoss import SparseDistillKLDivLoss from .SparseMarginMSELoss import SparseMarginMSELoss from .SparseMSELoss import SparseMSELoss from .SparseMultipleNegativesRankingLoss import SparseMultipleNegativesRankingLoss from .SparseTripletLoss import SparseTripletLoss from .SpladeLoss import SpladeLoss __all__ = [ "CSRLoss", "CSRReconstructionLoss", "SparseMultipleNegativesRankingLoss", "SparseCoSENTLoss", "SparseTripletLoss", "SparseMarginMSELoss", "SparseCosineSimilarityLoss", "SparseMSELoss", "SparseAnglELoss", "SparseDistillKLDivLoss", "FlopsLoss", "SpladeLoss", ]
from __future__ import annotations from .CSRLoss import CSRLoss, CSRReconstructionLoss from .FlopsLoss import FlopsLoss from .SparseAnglELoss import SparseAnglELoss from .SparseCoSENTLoss import SparseCoSENTLoss from .SparseCosineSimilarityLoss import SparseCosineSimilarityLoss from .SparseDistillKLDivLoss import SparseDistillKLDivLoss from .SparseMarginMSELoss import SparseMarginMSELoss from .SparseMSELoss import SparseMSELoss from .SparseMultipleNegativesRankingLoss import SparseMultipleNegativesRankingLoss from .SparseTripletLoss import SparseTripletLoss from .SpladeLoss import SpladeLoss __all__ = [ "CSRLoss", "CSRReconstructionLoss", "SparseMultipleNegativesRankingLoss", "SparseCoSENTLoss", "SparseTripletLoss", "SparseMarginMSELoss", "SparseCosineSimilarityLoss", "SparseMSELoss", "SparseAnglELoss", "SparseDistillKLDivLoss", "FlopsLoss", "SpladeLoss", ] # TODO: Test cached losses
# Copyright 2025 The HuggingFace Team. All rights reserved. # # 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 List, Optional, Tuple, Union import torch from ....models import UNet2DModel from ....schedulers import ScoreSdeVeScheduler from ....utils.torch_utils import randn_tensor from ...pipeline_utils import DiffusionPipeline, ImagePipelineOutput class ScoreSdeVePipeline(DiffusionPipeline): r""" Pipeline for unconditional image generation. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.). Parameters: unet ([`UNet2DModel`]): A `UNet2DModel` to denoise the encoded image. scheduler ([`ScoreSdeVeScheduler`]): A `ScoreSdeVeScheduler` to be used in combination with `unet` to denoise the encoded image. """ unet: UNet2DModel scheduler: ScoreSdeVeScheduler def __init__(self, unet: UNet2DModel, scheduler: ScoreSdeVeScheduler): super().__init__() self.register_modules(unet=unet, scheduler=scheduler) @torch.no_grad() def __call__( self, batch_size: int = 1, num_inference_steps: int = 2000, generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None, output_type: Optional[str] = "pil", return_dict: bool = True, **kwargs, ) -> Union[ImagePipelineOutput, Tuple]: r""" The call function to the pipeline for generation. Args: batch_size (`int`, *optional*, defaults to 1): The number of images to generate. generator (`torch.Generator`, `optional`): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. output_type (`str`, `optional`, defaults to `"pil"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`ImagePipelineOutput`] instead of a plain tuple. Returns: [`~pipelines.ImagePipelineOutput`] or `tuple`: If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images. """ img_size = self.unet.config.sample_size shape = (batch_size, 3, img_size, img_size) model = self.unet sample = randn_tensor(shape, generator=generator) * self.scheduler.init_noise_sigma sample = sample.to(self.device) self.scheduler.set_timesteps(num_inference_steps) self.scheduler.set_sigmas(num_inference_steps) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps)): sigma_t = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device) # correction step for _ in range(self.scheduler.config.correct_steps): model_output = self.unet(sample, sigma_t).sample sample = self.scheduler.step_correct(model_output, sample, generator=generator).prev_sample # prediction step model_output = model(sample, sigma_t).sample output = self.scheduler.step_pred(model_output, t, sample, generator=generator) sample, sample_mean = output.prev_sample, output.prev_sample_mean sample = sample_mean.clamp(0, 1) sample = sample.cpu().permute(0, 2, 3, 1).numpy() if output_type == "pil": sample = self.numpy_to_pil(sample) if not return_dict: return (sample,) return ImagePipelineOutput(images=sample)
# Copyright 2024 The HuggingFace Team. All rights reserved. # # 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 List, Optional, Tuple, Union import torch from ....models import UNet2DModel from ....schedulers import ScoreSdeVeScheduler from ....utils.torch_utils import randn_tensor from ...pipeline_utils import DiffusionPipeline, ImagePipelineOutput class ScoreSdeVePipeline(DiffusionPipeline): r""" Pipeline for unconditional image generation. This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.). Parameters: unet ([`UNet2DModel`]): A `UNet2DModel` to denoise the encoded image. scheduler ([`ScoreSdeVeScheduler`]): A `ScoreSdeVeScheduler` to be used in combination with `unet` to denoise the encoded image. """ unet: UNet2DModel scheduler: ScoreSdeVeScheduler def __init__(self, unet: UNet2DModel, scheduler: ScoreSdeVeScheduler): super().__init__() self.register_modules(unet=unet, scheduler=scheduler) @torch.no_grad() def __call__( self, batch_size: int = 1, num_inference_steps: int = 2000, generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None, output_type: Optional[str] = "pil", return_dict: bool = True, **kwargs, ) -> Union[ImagePipelineOutput, Tuple]: r""" The call function to the pipeline for generation. Args: batch_size (`int`, *optional*, defaults to 1): The number of images to generate. generator (`torch.Generator`, `optional`): A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation deterministic. output_type (`str`, `optional`, defaults to `"pil"`): The output format of the generated image. Choose between `PIL.Image` or `np.array`. return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`ImagePipelineOutput`] instead of a plain tuple. Returns: [`~pipelines.ImagePipelineOutput`] or `tuple`: If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is returned where the first element is a list with the generated images. """ img_size = self.unet.config.sample_size shape = (batch_size, 3, img_size, img_size) model = self.unet sample = randn_tensor(shape, generator=generator) * self.scheduler.init_noise_sigma sample = sample.to(self.device) self.scheduler.set_timesteps(num_inference_steps) self.scheduler.set_sigmas(num_inference_steps) for i, t in enumerate(self.progress_bar(self.scheduler.timesteps)): sigma_t = self.scheduler.sigmas[i] * torch.ones(shape[0], device=self.device) # correction step for _ in range(self.scheduler.config.correct_steps): model_output = self.unet(sample, sigma_t).sample sample = self.scheduler.step_correct(model_output, sample, generator=generator).prev_sample # prediction step model_output = model(sample, sigma_t).sample output = self.scheduler.step_pred(model_output, t, sample, generator=generator) sample, sample_mean = output.prev_sample, output.prev_sample_mean sample = sample_mean.clamp(0, 1) sample = sample.cpu().permute(0, 2, 3, 1).numpy() if output_type == "pil": sample = self.numpy_to_pil(sample) if not return_dict: return (sample,) return ImagePipelineOutput(images=sample)
from llama_index.core.node_parser.file.markdown import MarkdownNodeParser from llama_index.core.schema import Document def test_header_splits() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Header 1 content # Header 2 Header 2 content """ ) ] ) assert len(splits) == 2 assert splits[0].metadata == {"header_path": "/"} assert splits[1].metadata == {"header_path": "/"} assert splits[0].text == "# Main Header\n\nHeader 1 content" assert splits[1].text == "# Header 2\nHeader 2 content" def test_header_splits_with_forwardslash() -> None: markdown_parser = MarkdownNodeParser( header_path_separator="\u203a" ) # Unicode for "›", infrequently used char splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Header 1 content ## FAQ FAQ content ### 24/7 Support Support content #### Contact info Contact info content """ ) ] ) assert len(splits) == 4 assert splits[0].metadata == {"header_path": "›"} assert splits[1].metadata == {"header_path": "›Main Header›"} assert splits[2].metadata == {"header_path": "›Main Header›FAQ›"} assert splits[3].metadata == {"header_path": "›Main Header›FAQ›24/7 Support›"} assert splits[0].text == "# Main Header\n\nHeader 1 content" assert splits[1].text == "## FAQ\nFAQ content" assert splits[2].text == "### 24/7 Support\nSupport content" assert splits[3].text == "#### Contact info\nContact info content" def test_header_splits_with_indented_code_blocks() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""Some text # Header 1 ## Header 2 ### Header 3 ```txt Non indented block code ``` A list begins here: * Element 1 ```txt # has some indented code, but it's not handled as that. ``` * Element 2 ```txt # also has some code, but unbalanced fences (different number of spaces). Everything after this is considered code block! ``` * Element 3 * Element 4 ### Another Header 3 ```txt # has some wrongly indented fence, and leads to incorrect header detection. ``` ## Another Header 2 """ ) ] ) assert len(splits) == 6 assert splits[0].metadata == {"header_path": "/"} assert splits[0].text == "Some text" assert splits[1].metadata == {"header_path": "/"} assert splits[1].text == "# Header 1" assert splits[2].metadata == {"header_path": "/Header 1/"} assert splits[2].text == "## Header 2" assert splits[3].metadata == {"header_path": "/Header 1/Header 2/"} assert splits[3].text.endswith("* Element 4") assert splits[4].metadata == {"header_path": "/Header 1/Header 2/"} assert splits[4].text.endswith("```") assert splits[5].metadata == {"header_path": "/Header 1/"} assert splits[5].text == "## Another Header 2" def test_non_header_splits() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Header 1 #Not a header Also # not a header # Still not a header """ ) ] ) assert len(splits) == 1 def test_pre_header_content() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text=""" pre-header content # Header 1 Content ## Sub-header """ ) ] ) assert len(splits) == 3 def test_header_metadata() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Content ## Sub-header Content ### Sub-sub header Content # New title """ ) ] ) assert len(splits) == 4 assert splits[0].metadata == {"header_path": "/"} assert splits[1].metadata == {"header_path": "/Main Header/"} assert splits[2].metadata == {"header_path": "/Main Header/Sub-header/"} assert splits[3].metadata == {"header_path": "/"} def test_header_metadata_with_level_jump() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Content ### Sub-header Content ### Sub-sub header Content """ ) ] ) assert len(splits) == 3 assert splits[0].metadata == {"header_path": "/"} assert splits[1].metadata == {"header_path": "/Main Header/"} assert splits[2].metadata == {"header_path": "/Main Header/"}
from llama_index.core.node_parser.file.markdown import MarkdownNodeParser from llama_index.core.schema import Document def test_header_splits() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Header 1 content # Header 2 Header 2 content """ ) ] ) assert len(splits) == 2 assert splits[0].metadata == {"header_path": "/"} assert splits[1].metadata == {"header_path": "/"} assert splits[0].text == "# Main Header\n\nHeader 1 content" assert splits[1].text == "# Header 2\nHeader 2 content" def test_header_splits_with_forwardslash() -> None: markdown_parser = MarkdownNodeParser( header_path_separator="\u203A" ) # Unicode for "›", infrequently used char splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Header 1 content ## FAQ FAQ content ### 24/7 Support Support content #### Contact info Contact info content """ ) ] ) assert len(splits) == 4 assert splits[0].metadata == {"header_path": "›"} assert splits[1].metadata == {"header_path": "›Main Header›"} assert splits[2].metadata == {"header_path": "›Main Header›FAQ›"} assert splits[3].metadata == {"header_path": "›Main Header›FAQ›24/7 Support›"} assert splits[0].text == "# Main Header\n\nHeader 1 content" assert splits[1].text == "## FAQ\nFAQ content" assert splits[2].text == "### 24/7 Support\nSupport content" assert splits[3].text == "#### Contact info\nContact info content" def test_header_splits_with_indented_code_blocks() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""Some text # Header 1 ## Header 2 ### Header 3 ```txt Non indented block code ``` A list begins here: * Element 1 ```txt # has some indented code, but it's not handled as that. ``` * Element 2 ```txt # also has some code, but unbalanced fences (different number of spaces). Everything after this is considered code block! ``` * Element 3 * Element 4 ### Another Header 3 ```txt # has some wrongly indented fence, and leads to incorrect header detection. ``` ## Another Header 2 """ ) ] ) assert len(splits) == 6 assert splits[0].metadata == {"header_path": "/"} assert splits[0].text == "Some text" assert splits[1].metadata == {"header_path": "/"} assert splits[1].text == "# Header 1" assert splits[2].metadata == {"header_path": "/Header 1/"} assert splits[2].text == "## Header 2" assert splits[3].metadata == {"header_path": "/Header 1/Header 2/"} assert splits[3].text.endswith("* Element 4") assert splits[4].metadata == {"header_path": "/Header 1/Header 2/"} assert splits[4].text.endswith("```") assert splits[5].metadata == {"header_path": "/Header 1/"} assert splits[5].text == "## Another Header 2" def test_non_header_splits() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Header 1 #Not a header Also # not a header # Still not a header """ ) ] ) assert len(splits) == 1 def test_pre_header_content() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text=""" pre-header content # Header 1 Content ## Sub-header """ ) ] ) assert len(splits) == 3 def test_header_metadata() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Content ## Sub-header Content ### Sub-sub header Content # New title """ ) ] ) assert len(splits) == 4 assert splits[0].metadata == {"header_path": "/"} assert splits[1].metadata == {"header_path": "/Main Header/"} assert splits[2].metadata == {"header_path": "/Main Header/Sub-header/"} assert splits[3].metadata == {"header_path": "/"} def test_header_metadata_with_level_jump() -> None: markdown_parser = MarkdownNodeParser() splits = markdown_parser.get_nodes_from_documents( [ Document( text="""# Main Header Content ### Sub-header Content ### Sub-sub header Content """ ) ] ) assert len(splits) == 3 assert splits[0].metadata == {"header_path": "/"} assert splits[1].metadata == {"header_path": "/Main Header/"} assert splits[2].metadata == {"header_path": "/Main Header/"}
from datetime import datetime from typing import List from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document from llama_index.readers.earnings_call_transcript.utils import get_earnings_transcript class EarningsCallTranscript(BaseReader): def __init__(self, year: int, ticker: str, quarter: str): """ Get the earning call transcripts for a given company, in a given year and quarter. Args: year (int): Year of the transcript ticker (str): ticker symbol of the stock quarter (str): quarter """ curr_year = datetime.now().year assert year <= curr_year, "The year should be less than current year" assert quarter in [ "Q1", "Q2", "Q3", "Q4", ], 'The quarter should from the list ["Q1","Q2","Q3","Q4"]' self.year = year self.ticker = ticker self.quarter = quarter def load_data(self) -> List[Document]: resp_dict, speakers_list = get_earnings_transcript( self.quarter, self.ticker, self.year ) return Document( text=resp_dict["content"], extra_info={ "ticker": resp_dict["symbol"], "quarter": "Q" + str(resp_dict["quarter"]), "date_time": resp_dict["date"], "speakers_list": speakers_list, }, )
from datetime import datetime from typing import List from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document from llama_index.readers.earnings_call_transcript.utils import get_earnings_transcript class EarningsCallTranscript(BaseReader): def __init__(self, year: int, ticker: str, quarter: str): """Get the earning call transcripts for a given company, in a given year and quarter. Args: year (int): Year of the transcript ticker (str): ticker symbol of the stock quarter (str): quarter """ curr_year = datetime.now().year assert year <= curr_year, "The year should be less than current year" assert quarter in [ "Q1", "Q2", "Q3", "Q4", ], 'The quarter should from the list ["Q1","Q2","Q3","Q4"]' self.year = year self.ticker = ticker self.quarter = quarter def load_data(self) -> List[Document]: resp_dict, speakers_list = get_earnings_transcript( self.quarter, self.ticker, self.year ) return Document( text=resp_dict["content"], extra_info={ "ticker": resp_dict["symbol"], "quarter": "Q" + str(resp_dict["quarter"]), "date_time": resp_dict["date"], "speakers_list": speakers_list, }, )
# Copyright (c) OpenMMLab. All rights reserved. from unittest import TestCase from unittest.mock import MagicMock, patch import pytest import torch import torch.nn as nn from torch.nn.parallel import DataParallel from torch.nn.parallel.distributed import DistributedDataParallel from mmengine.model.wrappers import (MMDataParallel, MMDistributedDataParallel, is_model_wrapper) from mmengine.registry import MODEL_WRAPPERS def mock(*args, **kwargs): pass @patch('torch.distributed._broadcast_coalesced', mock) @patch('torch.distributed.broadcast', mock) @patch('torch.nn.parallel.DistributedDataParallel._ddp_init_helper', mock) def test_is_model_wrapper(): class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(2, 2, 1) def forward(self, x): return self.conv(x) # _verify_model_across_ranks is added in torch1.9.0 so we should check # whether _verify_model_across_ranks is the member of torch.distributed # before mocking if hasattr(torch.distributed, '_verify_model_across_ranks'): torch.distributed._verify_model_across_ranks = mock model = Model() assert not is_model_wrapper(model) mmdp = MMDataParallel(model) assert is_model_wrapper(mmdp) mmddp = MMDistributedDataParallel(model, process_group=MagicMock()) assert is_model_wrapper(mmddp) torch_dp = DataParallel(model) assert is_model_wrapper(torch_dp) torch_ddp = DistributedDataParallel(model, process_group=MagicMock()) assert is_model_wrapper(torch_ddp) # test model wrapper registry @MODEL_WRAPPERS.register_module() class ModelWrapper(object): def __init__(self, module): self.module = module def forward(self, *args, **kwargs): return self.module(*args, **kwargs) model_wrapper = ModelWrapper(model) assert is_model_wrapper(model_wrapper) class TestMMDataParallel(TestCase): def setUp(self): """Setup the demo image in every test method. TestCase calls functions in this order: setUp() -> testMethod() -> tearDown() -> cleanUp() """ class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 2, 1) def forward(self, x): return self.conv(x) def train_step(self, x): return self.forward(x) def val_step(self, x): return self.forward(x) self.model = Model() def test_train_step(self): class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 2, 1) def forward(self, x): return self.conv(x) model = Model() mmdp = MMDataParallel(model) # test without train_step attribute with pytest.raises(AssertionError): mmdp.train_step(torch.zeros([1, 1, 3, 3])) out = self.model.train_step(torch.zeros([1, 1, 3, 3])) assert out.shape == (1, 2, 3, 3) def test_val_step(self): class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 2, 1) def forward(self, x): return self.conv(x) model = Model() mmdp = MMDataParallel(model) # test without val_step attribute with pytest.raises(AssertionError): mmdp.val_step(torch.zeros([1, 1, 3, 3])) out = self.model.val_step(torch.zeros([1, 1, 3, 3])) assert out.shape == (1, 2, 3, 3)
# Copyright (c) OpenMMLab. All rights reserved. from unittest import TestCase from unittest.mock import MagicMock, patch import pytest import torch import torch.nn as nn from mmengine.model.wrappers import (MMDataParallel, MMDistributedDataParallel, is_model_wrapper) from mmengine.registry import MODEL_WRAPPERS def mock(*args, **kwargs): pass @patch('torch.distributed._broadcast_coalesced', mock) @patch('torch.distributed.broadcast', mock) @patch('torch.nn.parallel.DistributedDataParallel._ddp_init_helper', mock) def test_is_model_wrapper(): class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(2, 2, 1) def forward(self, x): return self.conv(x) # _verify_model_across_ranks is added in torch1.9.0 so we should check # whether _verify_model_across_ranks is the member of torch.distributed # before mocking if hasattr(torch.distributed, '_verify_model_across_ranks'): torch.distributed._verify_model_across_ranks = mock model = Model() assert not is_model_wrapper(model) mmdp = MMDataParallel(model) assert is_model_wrapper(mmdp) mmddp = MMDistributedDataParallel(model, process_group=MagicMock()) assert is_model_wrapper(mmddp) # test model wrapper registry @MODEL_WRAPPERS.register_module() class ModelWrapper(object): def __init__(self, module): self.module = module def forward(self, *args, **kwargs): return self.module(*args, **kwargs) model_wrapper = ModelWrapper(model) assert is_model_wrapper(model_wrapper) class TestMMDataParallel(TestCase): def setUp(self): """Setup the demo image in every test method. TestCase calls functions in this order: setUp() -> testMethod() -> tearDown() -> cleanUp() """ class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 2, 1) def forward(self, x): return self.conv(x) def train_step(self, x): return self.forward(x) def val_step(self, x): return self.forward(x) self.model = Model() def test_train_step(self): class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 2, 1) def forward(self, x): return self.conv(x) model = Model() mmdp = MMDataParallel(model) # test without train_step attribute with pytest.raises(AssertionError): mmdp.train_step(torch.zeros([1, 1, 3, 3])) out = self.model.train_step(torch.zeros([1, 1, 3, 3])) assert out.shape == (1, 2, 3, 3) def test_val_step(self): class Model(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(1, 2, 1) def forward(self, x): return self.conv(x) model = Model() mmdp = MMDataParallel(model) # test without val_step attribute with pytest.raises(AssertionError): mmdp.val_step(torch.zeros([1, 1, 3, 3])) out = self.model.val_step(torch.zeros([1, 1, 3, 3])) assert out.shape == (1, 2, 3, 3)
from __future__ import annotations from typing import Any, List, Optional, Tuple, Union import torch from torchvision.transforms import InterpolationMode from ._feature import _Feature, FillTypeJIT class Mask(_Feature): @classmethod def _wrap(cls, tensor: torch.Tensor) -> Mask: return tensor.as_subclass(cls) def __new__( cls, data: Any, *, dtype: Optional[torch.dtype] = None, device: Optional[Union[torch.device, str, int]] = None, requires_grad: bool = False, ) -> Mask: tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad) return cls._wrap(tensor) @classmethod def wrap_like( cls, other: Mask, tensor: torch.Tensor, ) -> Mask: return cls._wrap(tensor) @property def spatial_size(self) -> Tuple[int, int]: return tuple(self.shape[-2:]) # type: ignore[return-value] def horizontal_flip(self) -> Mask: output = self._F.horizontal_flip_mask(self.as_subclass(torch.Tensor)) return Mask.wrap_like(self, output) def vertical_flip(self) -> Mask: output = self._F.vertical_flip_mask(self.as_subclass(torch.Tensor)) return Mask.wrap_like(self, output) def resize( # type: ignore[override] self, size: List[int], interpolation: InterpolationMode = InterpolationMode.NEAREST, max_size: Optional[int] = None, antialias: Optional[bool] = None, ) -> Mask: output = self._F.resize_mask(self.as_subclass(torch.Tensor), size, max_size=max_size) return Mask.wrap_like(self, output) def crop(self, top: int, left: int, height: int, width: int) -> Mask: output = self._F.crop_mask(self.as_subclass(torch.Tensor), top, left, height, width) return Mask.wrap_like(self, output) def center_crop(self, output_size: List[int]) -> Mask: output = self._F.center_crop_mask(self.as_subclass(torch.Tensor), output_size=output_size) return Mask.wrap_like(self, output) def resized_crop( self, top: int, left: int, height: int, width: int, size: List[int], interpolation: InterpolationMode = InterpolationMode.NEAREST, antialias: Optional[bool] = None, ) -> Mask: output = self._F.resized_crop_mask(self.as_subclass(torch.Tensor), top, left, height, width, size=size) return Mask.wrap_like(self, output) def pad( self, padding: Union[int, List[int]], fill: FillTypeJIT = None, padding_mode: str = "constant", ) -> Mask: output = self._F.pad_mask(self.as_subclass(torch.Tensor), padding, padding_mode=padding_mode, fill=fill) return Mask.wrap_like(self, output) def rotate( self, angle: float, interpolation: InterpolationMode = InterpolationMode.NEAREST, expand: bool = False, center: Optional[List[float]] = None, fill: FillTypeJIT = None, ) -> Mask: output = self._F.rotate_mask(self.as_subclass(torch.Tensor), angle, expand=expand, center=center, fill=fill) return Mask.wrap_like(self, output) def affine( self, angle: Union[int, float], translate: List[float], scale: float, shear: List[float], interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: FillTypeJIT = None, center: Optional[List[float]] = None, ) -> Mask: output = self._F.affine_mask( self.as_subclass(torch.Tensor), angle, translate=translate, scale=scale, shear=shear, fill=fill, center=center, ) return Mask.wrap_like(self, output) def perspective( self, startpoints: Optional[List[List[int]]], endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: FillTypeJIT = None, coefficients: Optional[List[float]] = None, ) -> Mask: output = self._F.perspective_mask( self.as_subclass(torch.Tensor), startpoints, endpoints, fill=fill, coefficients=coefficients ) return Mask.wrap_like(self, output) def elastic( self, displacement: torch.Tensor, interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: FillTypeJIT = None, ) -> Mask: output = self._F.elastic_mask(self.as_subclass(torch.Tensor), displacement, fill=fill) return Mask.wrap_like(self, output)
from __future__ import annotations from typing import Any, List, Optional, Tuple, Union import torch from torchvision.transforms import InterpolationMode from ._feature import _Feature, FillTypeJIT class Mask(_Feature): @classmethod def _wrap(cls, tensor: torch.Tensor) -> Mask: return tensor.as_subclass(cls) def __new__( cls, data: Any, *, dtype: Optional[torch.dtype] = None, device: Optional[Union[torch.device, str, int]] = None, requires_grad: bool = False, ) -> Mask: tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad) return cls._wrap(tensor) @classmethod def wrap_like( cls, other: Mask, tensor: torch.Tensor, ) -> Mask: return cls._wrap(tensor) @property def spatial_size(self) -> Tuple[int, int]: return tuple(self.shape[-2:]) # type: ignore[return-value] def horizontal_flip(self) -> Mask: output = self._F.horizontal_flip_mask(self.as_subclass(torch.Tensor)) return Mask.wrap_like(self, output) def vertical_flip(self) -> Mask: output = self._F.vertical_flip_mask(self.as_subclass(torch.Tensor)) return Mask.wrap_like(self, output) def resize( # type: ignore[override] self, size: List[int], interpolation: InterpolationMode = InterpolationMode.NEAREST, max_size: Optional[int] = None, antialias: Optional[bool] = None, ) -> Mask: output = self._F.resize_mask(self.as_subclass(torch.Tensor), size, max_size=max_size) return Mask.wrap_like(self, output) def crop(self, top: int, left: int, height: int, width: int) -> Mask: output = self._F.crop_mask(self.as_subclass(torch.Tensor), top, left, height, width) return Mask.wrap_like(self, output) def center_crop(self, output_size: List[int]) -> Mask: output = self._F.center_crop_mask(self.as_subclass(torch.Tensor), output_size=output_size) return Mask.wrap_like(self, output) def resized_crop( self, top: int, left: int, height: int, width: int, size: List[int], interpolation: InterpolationMode = InterpolationMode.NEAREST, antialias: Optional[bool] = None, ) -> Mask: output = self._F.resized_crop_mask(self.as_subclass(torch.Tensor), top, left, height, width, size=size) return Mask.wrap_like(self, output) def pad( self, padding: Union[int, List[int]], fill: FillTypeJIT = None, padding_mode: str = "constant", ) -> Mask: output = self._F.pad_mask(self.as_subclass(torch.Tensor), padding, padding_mode=padding_mode, fill=fill) return Mask.wrap_like(self, output) def rotate( self, angle: float, interpolation: InterpolationMode = InterpolationMode.NEAREST, expand: bool = False, fill: FillTypeJIT = None, center: Optional[List[float]] = None, ) -> Mask: output = self._F.rotate_mask(self.as_subclass(torch.Tensor), angle, expand=expand, center=center, fill=fill) return Mask.wrap_like(self, output) def affine( self, angle: Union[int, float], translate: List[float], scale: float, shear: List[float], interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: FillTypeJIT = None, center: Optional[List[float]] = None, ) -> Mask: output = self._F.affine_mask( self.as_subclass(torch.Tensor), angle, translate=translate, scale=scale, shear=shear, fill=fill, center=center, ) return Mask.wrap_like(self, output) def perspective( self, startpoints: Optional[List[List[int]]], endpoints: Optional[List[List[int]]], interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: FillTypeJIT = None, coefficients: Optional[List[float]] = None, ) -> Mask: output = self._F.perspective_mask( self.as_subclass(torch.Tensor), startpoints, endpoints, fill=fill, coefficients=coefficients ) return Mask.wrap_like(self, output) def elastic( self, displacement: torch.Tensor, interpolation: InterpolationMode = InterpolationMode.NEAREST, fill: FillTypeJIT = None, ) -> Mask: output = self._F.elastic_mask(self.as_subclass(torch.Tensor), displacement, fill=fill) return Mask.wrap_like(self, output)
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # 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. # ============================================================================== """Code for backpropagation using the tape utilities.""" import collections from tensorflow.python import pywrap_tfe from tensorflow.python.ops.unconnected_gradients import UnconnectedGradients from tensorflow.python.util import compat VSpace = collections.namedtuple("VSpace", [ "aggregate_fn", "num_elements_fn", "zeros_fn", "ones_fn", "zeros_like_fn", "ones_like_fn", "graph_shape_fn" ]) def imperative_grad(tape, target, sources, output_gradients=None, sources_raw=None, unconnected_gradients=UnconnectedGradients.NONE): """Computes gradients from the imperatively defined tape on top of the stack. Works by filtering the tape, computing how many downstream usages are of each tensor and entry, and repeatedly applying backward functions until we have gradients for all sources. Args: tape: the gradient tape which stores the trace. target: either a Tensor or list of Tensors to be differentiated. sources: list of Tensors for which we want gradients output_gradients: if not None, a list of gradient provided for each Target, or None if we are to use the target's computed downstream gradient. sources_raw: if not None, a list of the source python objects from which the sources were generated. Should have the same length as sources. Only needs to be populated if unconnected_gradients is 'zero'. unconnected_gradients: determines the value returned if the target and sources are unconnected. When 'none' the value returned is None whereas when 'zero' a zero tensor in the same shape as the sources is returned. Returns: the gradient wrt each of the sources. Raises: ValueError: if the arguments are invalid. RuntimeError: if something goes wrong. """ try: unconnected_gradients = UnconnectedGradients(unconnected_gradients) except ValueError: raise ValueError( "Unknown value for unconnected_gradients: %r" % unconnected_gradients) return pywrap_tfe.TFE_Py_TapeGradient( tape._tape, # pylint: disable=protected-access target, sources, output_gradients, sources_raw, compat.as_str(unconnected_gradients.value))
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # 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. # ============================================================================== """Code for backpropagation using the tape utilities.""" import collections from tensorflow.python import pywrap_tfe from tensorflow.python.ops.unconnected_gradients import UnconnectedGradients from tensorflow.python.util import compat VSpace = collections.namedtuple("VSpace", [ "aggregate_fn", "num_elements_fn", "zeros_fn", "ones_fn", "zeros_like_fn", "ones_like_fn", "graph_shape_fn" ]) def imperative_grad(tape, target, sources, output_gradients=None, sources_raw=None, unconnected_gradients=UnconnectedGradients.NONE): """Computes gradients from the imperatively defined tape on top of the stack. Works by filtering the tape, computing how many downstream usages are of each tensor and entry, and repeatedly applying backward functions until we have gradients for all sources. Args: tape: the gradient tape which stores the trace. target: either a Tensor or list of Tensors to be differentiated. sources: list of Tensors for which we want gradients output_gradients: if not None, a list of gradient provided for each Target, or None if we are to use the target's computed downstream gradient. sources_raw: if not None, a list of the source python objects from which the sources were generated. Should have the same length as sources. Only needs to be populated if unconnected_gradients is 'zero'. unconnected_gradients: determines the value returned if the target and sources are unconnected. When 'none' the value returned is None wheras when 'zero' a zero tensor in the same shape as the sources is returned. Returns: the gradient wrt each of the sources. Raises: ValueError: if the arguments are invalid. RuntimeError: if something goes wrong. """ try: unconnected_gradients = UnconnectedGradients(unconnected_gradients) except ValueError: raise ValueError( "Unknown value for unconnected_gradients: %r" % unconnected_gradients) return pywrap_tfe.TFE_Py_TapeGradient( tape._tape, # pylint: disable=protected-access target, sources, output_gradients, sources_raw, compat.as_str(unconnected_gradients.value))
from __future__ import annotations from sentence_transformers import SentenceTransformer, losses, util class AnglELoss(losses.CoSENTLoss): def __init__(self, model: SentenceTransformer, scale: float = 20.0) -> None: """ This class implements AnglE (Angle Optimized) loss. This is a modification of :class:`CoSENTLoss`, designed to address the following issue: The cosine function's gradient approaches 0 as the wave approaches the top or bottom of its form. This can hinder the optimization process, so AnglE proposes to instead optimize the angle difference in complex space in order to mitigate this effect. It expects that each of the InputExamples consists of a pair of texts and a float valued label, representing the expected similarity score between the pair. It computes the following loss function: ``loss = logsum(1+exp(s(k,l)-s(i,j))+exp...)``, where ``(i,j)`` and ``(k,l)`` are any of the input pairs in the batch such that the expected similarity of ``(i,j)`` is greater than ``(k,l)``. The summation is over all possible pairs of input pairs in the batch that match this condition. This is the same as CoSENTLoss, with a different similarity function. Args: model: SentenceTransformerModel scale: Output of similarity function is multiplied by scale value. Represents the inverse temperature. References: - For further details, see: https://arxiv.org/abs/2309.12871v1 Requirements: - Sentence pairs with corresponding similarity scores in range of the similarity function. Default is [-1,1]. Relations: - :class:`CoSENTLoss` is AnglELoss with ``pairwise_cos_sim`` as the metric, rather than ``pairwise_angle_sim``. - :class:`CosineSimilarityLoss` seems to produce a weaker training signal than ``CoSENTLoss`` or ``AnglELoss``. Inputs: +--------------------------------+------------------------+ | Texts | Labels | +================================+========================+ | (sentence_A, sentence_B) pairs | float similarity score | +--------------------------------+------------------------+ Example: :: from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses from datasets import Dataset model = SentenceTransformer("microsoft/mpnet-base") train_dataset = Dataset.from_dict({ "sentence1": ["It's nice weather outside today.", "He drove to work."], "sentence2": ["It's so sunny.", "She walked to the store."], "score": [1.0, 0.3], }) loss = losses.AnglELoss(model) trainer = SentenceTransformerTrainer( model=model, train_dataset=train_dataset, loss=loss, ) trainer.train() """ super().__init__(model, scale, similarity_fct=util.pairwise_angle_sim) @property def citation(self) -> str: return """ @misc{li2023angleoptimized, title={AnglE-optimized Text Embeddings}, author={Xianming Li and Jing Li}, year={2023}, eprint={2309.12871}, archivePrefix={arXiv}, primaryClass={cs.CL} } """
from sentence_transformers import SentenceTransformer, losses, util class AnglELoss(losses.CoSENTLoss): def __init__(self, model: SentenceTransformer, scale: float = 20.0) -> None: """ This class implements AnglE (Angle Optimized) loss. This is a modification of :class:`CoSENTLoss`, designed to address the following issue: The cosine function's gradient approaches 0 as the wave approaches the top or bottom of its form. This can hinder the optimization process, so AnglE proposes to instead optimize the angle difference in complex space in order to mitigate this effect. It expects that each of the InputExamples consists of a pair of texts and a float valued label, representing the expected similarity score between the pair. It computes the following loss function: ``loss = logsum(1+exp(s(k,l)-s(i,j))+exp...)``, where ``(i,j)`` and ``(k,l)`` are any of the input pairs in the batch such that the expected similarity of ``(i,j)`` is greater than ``(k,l)``. The summation is over all possible pairs of input pairs in the batch that match this condition. This is the same as CoSENTLoss, with a different similarity function. Args: model: SentenceTransformerModel scale: Output of similarity function is multiplied by scale value. Represents the inverse temperature. References: - For further details, see: https://arxiv.org/abs/2309.12871v1 Requirements: - Sentence pairs with corresponding similarity scores in range of the similarity function. Default is [-1,1]. Relations: - :class:`CoSENTLoss` is AnglELoss with ``pairwise_cos_sim`` as the metric, rather than ``pairwise_angle_sim``. - :class:`CosineSimilarityLoss` seems to produce a weaker training signal than ``CoSENTLoss`` or ``AnglELoss``. Inputs: +--------------------------------+------------------------+ | Texts | Labels | +================================+========================+ | (sentence_A, sentence_B) pairs | float similarity score | +--------------------------------+------------------------+ Example: :: from sentence_transformers import SentenceTransformer, SentenceTransformerTrainer, losses from datasets import Dataset model = SentenceTransformer("microsoft/mpnet-base") train_dataset = Dataset.from_dict({ "sentence1": ["It's nice weather outside today.", "He drove to work."], "sentence2": ["It's so sunny.", "She walked to the store."], "score": [1.0, 0.3], }) loss = losses.AnglELoss(model) trainer = SentenceTransformerTrainer( model=model, train_dataset=train_dataset, loss=loss, ) trainer.train() """ super().__init__(model, scale, similarity_fct=util.pairwise_angle_sim) @property def citation(self) -> str: return """ @misc{li2023angleoptimized, title={AnglE-optimized Text Embeddings}, author={Xianming Li and Jing Li}, year={2023}, eprint={2309.12871}, archivePrefix={arXiv}, primaryClass={cs.CL} } """
from typing import List, Optional, Sequence from llama_index.core.base.base_query_engine import BaseQueryEngine from llama_index.core.base.response.schema import RESPONSE_TYPE from llama_index.core.callbacks.base import CallbackManager from llama_index.core.indices.query.query_transform.base import BaseQueryTransform from llama_index.core.prompts.mixin import PromptMixinType from llama_index.core.schema import NodeWithScore, QueryBundle class TransformQueryEngine(BaseQueryEngine): """ Transform query engine. Applies a query transform to a query bundle before passing it to a query engine. Args: query_engine (BaseQueryEngine): A query engine object. query_transform (BaseQueryTransform): A query transform object. transform_metadata (Optional[dict]): metadata to pass to the query transform. callback_manager (Optional[CallbackManager]): A callback manager. """ def __init__( self, query_engine: BaseQueryEngine, query_transform: BaseQueryTransform, transform_metadata: Optional[dict] = None, callback_manager: Optional[CallbackManager] = None, ) -> None: self._query_engine = query_engine self._query_transform = query_transform self._transform_metadata = transform_metadata super().__init__(callback_manager) def _get_prompt_modules(self) -> PromptMixinType: """Get prompt sub-modules.""" return { "query_transform": self._query_transform, "query_engine": self._query_engine, } def retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]: query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return self._query_engine.retrieve(query_bundle) def synthesize( self, query_bundle: QueryBundle, nodes: List[NodeWithScore], additional_source_nodes: Optional[Sequence[NodeWithScore]] = None, ) -> RESPONSE_TYPE: query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return self._query_engine.synthesize( query_bundle=query_bundle, nodes=nodes, additional_source_nodes=additional_source_nodes, ) async def asynthesize( self, query_bundle: QueryBundle, nodes: List[NodeWithScore], additional_source_nodes: Optional[Sequence[NodeWithScore]] = None, ) -> RESPONSE_TYPE: query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return await self._query_engine.asynthesize( query_bundle=query_bundle, nodes=nodes, additional_source_nodes=additional_source_nodes, ) def _query(self, query_bundle: QueryBundle) -> RESPONSE_TYPE: """Answer a query.""" query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return self._query_engine.query(query_bundle) async def _aquery(self, query_bundle: QueryBundle) -> RESPONSE_TYPE: """Answer a query.""" query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return await self._query_engine.aquery(query_bundle)
from typing import List, Optional, Sequence from llama_index.core.base.base_query_engine import BaseQueryEngine from llama_index.core.base.response.schema import RESPONSE_TYPE from llama_index.core.callbacks.base import CallbackManager from llama_index.core.indices.query.query_transform.base import BaseQueryTransform from llama_index.core.prompts.mixin import PromptMixinType from llama_index.core.schema import NodeWithScore, QueryBundle class TransformQueryEngine(BaseQueryEngine): """Transform query engine. Applies a query transform to a query bundle before passing it to a query engine. Args: query_engine (BaseQueryEngine): A query engine object. query_transform (BaseQueryTransform): A query transform object. transform_metadata (Optional[dict]): metadata to pass to the query transform. callback_manager (Optional[CallbackManager]): A callback manager. """ def __init__( self, query_engine: BaseQueryEngine, query_transform: BaseQueryTransform, transform_metadata: Optional[dict] = None, callback_manager: Optional[CallbackManager] = None, ) -> None: self._query_engine = query_engine self._query_transform = query_transform self._transform_metadata = transform_metadata super().__init__(callback_manager) def _get_prompt_modules(self) -> PromptMixinType: """Get prompt sub-modules.""" return { "query_transform": self._query_transform, "query_engine": self._query_engine, } def retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]: query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return self._query_engine.retrieve(query_bundle) def synthesize( self, query_bundle: QueryBundle, nodes: List[NodeWithScore], additional_source_nodes: Optional[Sequence[NodeWithScore]] = None, ) -> RESPONSE_TYPE: query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return self._query_engine.synthesize( query_bundle=query_bundle, nodes=nodes, additional_source_nodes=additional_source_nodes, ) async def asynthesize( self, query_bundle: QueryBundle, nodes: List[NodeWithScore], additional_source_nodes: Optional[Sequence[NodeWithScore]] = None, ) -> RESPONSE_TYPE: query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return await self._query_engine.asynthesize( query_bundle=query_bundle, nodes=nodes, additional_source_nodes=additional_source_nodes, ) def _query(self, query_bundle: QueryBundle) -> RESPONSE_TYPE: """Answer a query.""" query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return self._query_engine.query(query_bundle) async def _aquery(self, query_bundle: QueryBundle) -> RESPONSE_TYPE: """Answer a query.""" query_bundle = self._query_transform.run( query_bundle, metadata=self._transform_metadata ) return await self._query_engine.aquery(query_bundle)
"""Abstract interface for document loader implementations.""" from __future__ import annotations from abc import ABC, abstractmethod from typing import TYPE_CHECKING, Optional from langchain_core.runnables import run_in_executor if TYPE_CHECKING: from collections.abc import AsyncIterator, Iterator from langchain_text_splitters import TextSplitter from langchain_core.documents import Document from langchain_core.documents.base import Blob class BaseLoader(ABC): # noqa: B024 """Interface for Document Loader. Implementations should implement the lazy-loading method using generators to avoid loading all Documents into memory at once. `load` is provided just for user convenience and should not be overridden. """ # Sub-classes should not implement this method directly. Instead, they # should implement the lazy load method. def load(self) -> list[Document]: """Load data into Document objects.""" return list(self.lazy_load()) async def aload(self) -> list[Document]: """Load data into Document objects.""" return [document async for document in self.alazy_load()] def load_and_split( self, text_splitter: Optional[TextSplitter] = None ) -> list[Document]: """Load Documents and split into chunks. Chunks are returned as Documents. Do not override this method. It should be considered to be deprecated! Args: text_splitter: TextSplitter instance to use for splitting documents. Defaults to RecursiveCharacterTextSplitter. Returns: List of Documents. """ if text_splitter is None: try: from langchain_text_splitters import RecursiveCharacterTextSplitter except ImportError as e: msg = ( "Unable to import from langchain_text_splitters. Please specify " "text_splitter or install langchain_text_splitters with " "`pip install -U langchain-text-splitters`." ) raise ImportError(msg) from e _text_splitter: TextSplitter = RecursiveCharacterTextSplitter() else: _text_splitter = text_splitter docs = self.load() return _text_splitter.split_documents(docs) # Attention: This method will be upgraded into an abstractmethod once it's # implemented in all the existing subclasses. def lazy_load(self) -> Iterator[Document]: """A lazy loader for Documents.""" if type(self).load != BaseLoader.load: return iter(self.load()) msg = f"{self.__class__.__name__} does not implement lazy_load()" raise NotImplementedError(msg) async def alazy_load(self) -> AsyncIterator[Document]: """A lazy loader for Documents.""" iterator = await run_in_executor(None, self.lazy_load) done = object() while True: doc = await run_in_executor(None, next, iterator, done) if doc is done: break yield doc # type: ignore[misc] class BaseBlobParser(ABC): """Abstract interface for blob parsers. A blob parser provides a way to parse raw data stored in a blob into one or more documents. The parser can be composed with blob loaders, making it easy to reuse a parser independent of how the blob was originally loaded. """ @abstractmethod def lazy_parse(self, blob: Blob) -> Iterator[Document]: """Lazy parsing interface. Subclasses are required to implement this method. Args: blob: Blob instance Returns: Generator of documents """ def parse(self, blob: Blob) -> list[Document]: """Eagerly parse the blob into a document or documents. This is a convenience method for interactive development environment. Production applications should favor the lazy_parse method instead. Subclasses should generally not over-ride this parse method. Args: blob: Blob instance Returns: List of documents """ return list(self.lazy_parse(blob))
"""Abstract interface for document loader implementations.""" from __future__ import annotations from abc import ABC, abstractmethod from typing import TYPE_CHECKING, Optional from langchain_core.runnables import run_in_executor if TYPE_CHECKING: from collections.abc import AsyncIterator, Iterator from langchain_text_splitters import TextSplitter from langchain_core.documents import Document from langchain_core.documents.base import Blob class BaseLoader(ABC): # noqa: B024 """Interface for Document Loader. Implementations should implement the lazy-loading method using generators to avoid loading all Documents into memory at once. `load` is provided just for user convenience and should not be overridden. """ # Sub-classes should not implement this method directly. Instead, they # should implement the lazy load method. def load(self) -> list[Document]: """Load data into Document objects.""" return list(self.lazy_load()) async def aload(self) -> list[Document]: """Load data into Document objects.""" return [document async for document in self.alazy_load()] def load_and_split( self, text_splitter: Optional[TextSplitter] = None ) -> list[Document]: """Load Documents and split into chunks. Chunks are returned as Documents. Do not override this method. It should be considered to be deprecated! Args: text_splitter: TextSplitter instance to use for splitting documents. Defaults to RecursiveCharacterTextSplitter. Returns: List of Documents. """ if text_splitter is None: try: from langchain_text_splitters import RecursiveCharacterTextSplitter except ImportError as e: msg = ( "Unable to import from langchain_text_splitters. Please specify " "text_splitter or install langchain_text_splitters with " "`pip install -U langchain-text-splitters`." ) raise ImportError(msg) from e _text_splitter: TextSplitter = RecursiveCharacterTextSplitter() else: _text_splitter = text_splitter docs = self.load() return _text_splitter.split_documents(docs) # Attention: This method will be upgraded into an abstractmethod once it's # implemented in all the existing subclasses. def lazy_load(self) -> Iterator[Document]: """A lazy loader for Documents.""" if type(self).load != BaseLoader.load: return iter(self.load()) msg = f"{self.__class__.__name__} does not implement lazy_load()" raise NotImplementedError(msg) async def alazy_load(self) -> AsyncIterator[Document]: """A lazy loader for Documents.""" iterator = await run_in_executor(None, self.lazy_load) done = object() while True: doc = await run_in_executor(None, next, iterator, done) # type: ignore[call-arg, arg-type] if doc is done: break yield doc # type: ignore[misc] class BaseBlobParser(ABC): """Abstract interface for blob parsers. A blob parser provides a way to parse raw data stored in a blob into one or more documents. The parser can be composed with blob loaders, making it easy to reuse a parser independent of how the blob was originally loaded. """ @abstractmethod def lazy_parse(self, blob: Blob) -> Iterator[Document]: """Lazy parsing interface. Subclasses are required to implement this method. Args: blob: Blob instance Returns: Generator of documents """ def parse(self, blob: Blob) -> list[Document]: """Eagerly parse the blob into a document or documents. This is a convenience method for interactive development environment. Production applications should favor the lazy_parse method instead. Subclasses should generally not over-ride this parse method. Args: blob: Blob instance Returns: List of documents """ return list(self.lazy_parse(blob))
from keras.src import backend from keras.src import ops from keras.src.layers.input_spec import InputSpec from keras.src.layers.layer import Layer from keras.src.ops.operation_utils import compute_pooling_output_shape from keras.src.utils import argument_validation class BasePooling(Layer): """Base pooling layer.""" def __init__( self, pool_size, strides, pool_dimensions, pool_mode="max", padding="valid", data_format=None, name=None, **kwargs, ): super().__init__(name=name, **kwargs) self.pool_size = argument_validation.standardize_tuple( pool_size, pool_dimensions, "pool_size" ) strides = pool_size if strides is None else strides self.strides = argument_validation.standardize_tuple( strides, pool_dimensions, "strides", allow_zero=True ) self.pool_mode = pool_mode self.padding = padding self.data_format = backend.standardize_data_format(data_format) self.input_spec = InputSpec(ndim=pool_dimensions + 2) self.built = True def call(self, inputs): if self.pool_mode == "max": return ops.max_pool( inputs, pool_size=self.pool_size, strides=self.strides, padding=self.padding, data_format=self.data_format, ) elif self.pool_mode == "average": return ops.average_pool( inputs, pool_size=self.pool_size, strides=self.strides, padding=self.padding, data_format=self.data_format, ) else: raise ValueError( "`pool_mode` must be either 'max' or 'average'. Received: " f"{self.pool_mode}." ) def compute_output_shape(self, input_shape): return compute_pooling_output_shape( input_shape, self.pool_size, self.strides, self.padding, self.data_format, ) def get_config(self): config = super().get_config() config.update( { "pool_size": self.pool_size, "padding": self.padding, "strides": self.strides, "data_format": self.data_format, } ) return config
from keras.src import backend from keras.src import ops from keras.src.layers.input_spec import InputSpec from keras.src.layers.layer import Layer from keras.src.ops.operation_utils import compute_pooling_output_shape from keras.src.utils import argument_validation class BasePooling(Layer): """Base pooling layer.""" def __init__( self, pool_size, strides, pool_dimensions, pool_mode="max", padding="valid", data_format=None, name=None, **kwargs, ): super().__init__(name=name, **kwargs) self.pool_size = argument_validation.standardize_tuple( pool_size, pool_dimensions, "pool_size" ) strides = pool_size if strides is None else strides self.strides = argument_validation.standardize_tuple( strides, pool_dimensions, "strides", allow_zero=True ) self.pool_mode = pool_mode self.padding = padding self.data_format = backend.standardize_data_format(data_format) self.input_spec = InputSpec(ndim=pool_dimensions + 2) def call(self, inputs): if self.pool_mode == "max": return ops.max_pool( inputs, pool_size=self.pool_size, strides=self.strides, padding=self.padding, data_format=self.data_format, ) elif self.pool_mode == "average": return ops.average_pool( inputs, pool_size=self.pool_size, strides=self.strides, padding=self.padding, data_format=self.data_format, ) else: raise ValueError( "`pool_mode` must be either 'max' or 'average'. Received: " f"{self.pool_mode}." ) def compute_output_shape(self, input_shape): return compute_pooling_output_shape( input_shape, self.pool_size, self.strides, self.padding, self.data_format, ) def get_config(self): config = super().get_config() config.update( { "pool_size": self.pool_size, "padding": self.padding, "strides": self.strides, "data_format": self.data_format, } ) return config
import pytest from langchain_core.documents import Document from langchain.indexes._api import _HashedDocument def test_hashed_document_hashing() -> None: hashed_document = _HashedDocument( # type: ignore[call-arg] uid="123", page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) assert isinstance(hashed_document.hash_, str) def test_hashing_with_missing_content() -> None: """Check that ValueError is raised if page_content is missing.""" with pytest.raises(TypeError): _HashedDocument( # type: ignore[call-arg] metadata={"key": "value"}, ) def test_uid_auto_assigned_to_hash() -> None: """Test uid is auto-assigned to the hashed_document hash.""" hashed_document = _HashedDocument( # type: ignore[call-arg] page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) assert hashed_document.uid == hashed_document.hash_ def test_to_document() -> None: """Test to_document method.""" hashed_document = _HashedDocument( # type: ignore[call-arg] page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) doc = hashed_document.to_document() assert isinstance(doc, Document) assert doc.page_content == "Lorem ipsum dolor sit amet" assert doc.metadata == {"key": "value"} def test_from_document() -> None: """Test from document class method.""" document = Document( page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) hashed_document = _HashedDocument.from_document(document) # hash should be deterministic assert hashed_document.hash_ == "fd1dc827-051b-537d-a1fe-1fa043e8b276" assert hashed_document.uid == hashed_document.hash_
import pytest from langchain_core.documents import Document from langchain.indexes._api import _HashedDocument def test_hashed_document_hashing() -> None: hashed_document = _HashedDocument( # type: ignore[call-arg] uid="123", page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) assert isinstance(hashed_document.hash_, str) def test_hashing_with_missing_content() -> None: """Check that ValueError is raised if page_content is missing.""" with pytest.raises(TypeError): _HashedDocument( metadata={"key": "value"}, ) # type: ignore def test_uid_auto_assigned_to_hash() -> None: """Test uid is auto-assigned to the hashed_document hash.""" hashed_document = _HashedDocument( # type: ignore[call-arg] page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) assert hashed_document.uid == hashed_document.hash_ def test_to_document() -> None: """Test to_document method.""" hashed_document = _HashedDocument( # type: ignore[call-arg] page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) doc = hashed_document.to_document() assert isinstance(doc, Document) assert doc.page_content == "Lorem ipsum dolor sit amet" assert doc.metadata == {"key": "value"} def test_from_document() -> None: """Test from document class method.""" document = Document( page_content="Lorem ipsum dolor sit amet", metadata={"key": "value"} ) hashed_document = _HashedDocument.from_document(document) # hash should be deterministic assert hashed_document.hash_ == "fd1dc827-051b-537d-a1fe-1fa043e8b276" assert hashed_document.uid == hashed_document.hash_
import base64 import re from typing import Union from langchain_core.messages import ( AIMessage, BaseMessage, FunctionMessage, HumanMessage, SystemMessage, ) from pydantic import BaseModel from .core import Invoker, Prompty, SimpleModel class RoleMap: _ROLE_MAP: dict[str, type[BaseMessage]] = { "system": SystemMessage, "user": HumanMessage, "human": HumanMessage, "assistant": AIMessage, "ai": AIMessage, "function": FunctionMessage, } ROLES = _ROLE_MAP.keys() @classmethod def get_message_class(cls, role: str) -> type[BaseMessage]: return cls._ROLE_MAP[role] class PromptyChatParser(Invoker): """Parse a chat prompt into a list of messages.""" def __init__(self, prompty: Prompty) -> None: self.prompty = prompty self.roles = RoleMap.ROLES self.path = self.prompty.file.parent def inline_image(self, image_item: str) -> str: # pass through if it's a url or base64 encoded if image_item.startswith("http") or image_item.startswith("data"): return image_item # otherwise, it's a local file - need to base64 encode it else: image_path = self.path / image_item with open(image_path, "rb") as f: base64_image = base64.b64encode(f.read()).decode("utf-8") if image_path.suffix == ".png": return f"data:image/png;base64,{base64_image}" elif image_path.suffix == ".jpg": return f"data:image/jpeg;base64,{base64_image}" elif image_path.suffix == ".jpeg": return f"data:image/jpeg;base64,{base64_image}" else: raise ValueError( f"Invalid image format {image_path.suffix} - currently only .png " "and .jpg / .jpeg are supported." ) def parse_content(self, content: str) -> Union[str, list]: """for parsing inline images""" # regular expression to parse markdown images image = r"(?P<alt>!\[[^\]]*\])\((?P<filename>.*?)(?=\"|\))\)" matches = re.findall(image, content, flags=re.MULTILINE) if len(matches) > 0: content_items = [] content_chunks = re.split(image, content, flags=re.MULTILINE) current_chunk = 0 for i in range(len(content_chunks)): # image entry if ( current_chunk < len(matches) and content_chunks[i] == matches[current_chunk][0] ): content_items.append( { "type": "image_url", "image_url": { "url": self.inline_image( matches[current_chunk][1].split(" ")[0].strip() ) }, } ) # second part of image entry elif ( current_chunk < len(matches) and content_chunks[i] == matches[current_chunk][1] ): current_chunk += 1 # text entry else: if len(content_chunks[i].strip()) > 0: content_items.append( {"type": "text", "text": content_chunks[i].strip()} ) return content_items else: return content def invoke(self, data: BaseModel) -> BaseModel: assert isinstance(data, SimpleModel) messages = [] separator = r"(?i)^\s*#?\s*(" + "|".join(self.roles) + r")\s*:\s*\n" # get valid chunks - remove empty items chunks = [ item for item in re.split(separator, data.item, flags=re.MULTILINE) if len(item.strip()) > 0 ] # if no starter role, then inject system role if chunks[0].strip().lower() not in self.roles: chunks.insert(0, "system") # if last chunk is role entry, then remove (no content?) if chunks[-1].strip().lower() in self.roles: chunks.pop() if len(chunks) % 2 != 0: raise ValueError("Invalid prompt format") # create messages for i in range(0, len(chunks), 2): role = chunks[i].strip().lower() content = chunks[i + 1].strip() messages.append({"role": role, "content": self.parse_content(content)}) return SimpleModel[list](item=messages)
import base64 import re from typing import Dict, List, Type, Union from langchain_core.messages import ( AIMessage, BaseMessage, FunctionMessage, HumanMessage, SystemMessage, ) from pydantic import BaseModel from .core import Invoker, Prompty, SimpleModel class RoleMap: _ROLE_MAP: Dict[str, Type[BaseMessage]] = { "system": SystemMessage, "user": HumanMessage, "human": HumanMessage, "assistant": AIMessage, "ai": AIMessage, "function": FunctionMessage, } ROLES = _ROLE_MAP.keys() @classmethod def get_message_class(cls, role: str) -> Type[BaseMessage]: return cls._ROLE_MAP[role] class PromptyChatParser(Invoker): """Parse a chat prompt into a list of messages.""" def __init__(self, prompty: Prompty) -> None: self.prompty = prompty self.roles = RoleMap.ROLES self.path = self.prompty.file.parent def inline_image(self, image_item: str) -> str: # pass through if it's a url or base64 encoded if image_item.startswith("http") or image_item.startswith("data"): return image_item # otherwise, it's a local file - need to base64 encode it else: image_path = self.path / image_item with open(image_path, "rb") as f: base64_image = base64.b64encode(f.read()).decode("utf-8") if image_path.suffix == ".png": return f"data:image/png;base64,{base64_image}" elif image_path.suffix == ".jpg": return f"data:image/jpeg;base64,{base64_image}" elif image_path.suffix == ".jpeg": return f"data:image/jpeg;base64,{base64_image}" else: raise ValueError( f"Invalid image format {image_path.suffix} - currently only .png " "and .jpg / .jpeg are supported." ) def parse_content(self, content: str) -> Union[str, List]: """for parsing inline images""" # regular expression to parse markdown images image = r"(?P<alt>!\[[^\]]*\])\((?P<filename>.*?)(?=\"|\))\)" matches = re.findall(image, content, flags=re.MULTILINE) if len(matches) > 0: content_items = [] content_chunks = re.split(image, content, flags=re.MULTILINE) current_chunk = 0 for i in range(len(content_chunks)): # image entry if ( current_chunk < len(matches) and content_chunks[i] == matches[current_chunk][0] ): content_items.append( { "type": "image_url", "image_url": { "url": self.inline_image( matches[current_chunk][1].split(" ")[0].strip() ) }, } ) # second part of image entry elif ( current_chunk < len(matches) and content_chunks[i] == matches[current_chunk][1] ): current_chunk += 1 # text entry else: if len(content_chunks[i].strip()) > 0: content_items.append( {"type": "text", "text": content_chunks[i].strip()} ) return content_items else: return content def invoke(self, data: BaseModel) -> BaseModel: assert isinstance(data, SimpleModel) messages = [] separator = r"(?i)^\s*#?\s*(" + "|".join(self.roles) + r")\s*:\s*\n" # get valid chunks - remove empty items chunks = [ item for item in re.split(separator, data.item, flags=re.MULTILINE) if len(item.strip()) > 0 ] # if no starter role, then inject system role if chunks[0].strip().lower() not in self.roles: chunks.insert(0, "system") # if last chunk is role entry, then remove (no content?) if chunks[-1].strip().lower() in self.roles: chunks.pop() if len(chunks) % 2 != 0: raise ValueError("Invalid prompt format") # create messages for i in range(0, len(chunks), 2): role = chunks[i].strip().lower() content = chunks[i + 1].strip() messages.append({"role": role, "content": self.parse_content(content)}) return SimpleModel[list](item=messages)
# Copyright (c) OpenMMLab. All rights reserved. from typing import Tuple import torch.nn as nn from mmcv.cnn import ConvModule, bias_init_with_prob, normal_init from torch import Tensor from mmdet.registry import MODELS from mmdet.utils import OptConfigType, OptMultiConfig from .anchor_head import AnchorHead @MODELS.register_module() class RetinaSepBNHead(AnchorHead): """"RetinaHead with separate BN. In RetinaHead, conv/norm layers are shared across different FPN levels, while in RetinaSepBNHead, conv layers are shared across different FPN levels, but BN layers are separated. """ def __init__(self, num_classes: int, num_ins: int, in_channels: int, stacked_convs: int = 4, conv_cfg: OptConfigType = None, norm_cfg: OptConfigType = None, init_cfg: OptMultiConfig = None, **kwargs) -> None: assert init_cfg is None, 'To prevent abnormal initialization ' \ 'behavior, init_cfg is not allowed to be set' self.stacked_convs = stacked_convs self.conv_cfg = conv_cfg self.norm_cfg = norm_cfg self.num_ins = num_ins super().__init__( num_classes=num_classes, in_channels=in_channels, init_cfg=init_cfg, **kwargs) def _init_layers(self) -> None: """Initialize layers of the head.""" self.relu = nn.ReLU(inplace=True) self.cls_convs = nn.ModuleList() self.reg_convs = nn.ModuleList() for i in range(self.num_ins): cls_convs = nn.ModuleList() reg_convs = nn.ModuleList() for j in range(self.stacked_convs): chn = self.in_channels if j == 0 else self.feat_channels cls_convs.append( ConvModule( chn, self.feat_channels, 3, stride=1, padding=1, conv_cfg=self.conv_cfg, norm_cfg=self.norm_cfg)) reg_convs.append( ConvModule( chn, self.feat_channels, 3, stride=1, padding=1, conv_cfg=self.conv_cfg, norm_cfg=self.norm_cfg)) self.cls_convs.append(cls_convs) self.reg_convs.append(reg_convs) for i in range(self.stacked_convs): for j in range(1, self.num_ins): self.cls_convs[j][i].conv = self.cls_convs[0][i].conv self.reg_convs[j][i].conv = self.reg_convs[0][i].conv self.retina_cls = nn.Conv2d( self.feat_channels, self.num_base_priors * self.cls_out_channels, 3, padding=1) self.retina_reg = nn.Conv2d( self.feat_channels, self.num_base_priors * 4, 3, padding=1) def init_weights(self) -> None: """Initialize weights of the head.""" super().init_weights() for m in self.cls_convs[0]: normal_init(m.conv, std=0.01) for m in self.reg_convs[0]: normal_init(m.conv, std=0.01) bias_cls = bias_init_with_prob(0.01) normal_init(self.retina_cls, std=0.01, bias=bias_cls) normal_init(self.retina_reg, std=0.01) def forward(self, feats: Tuple[Tensor]) -> tuple: """Forward features from the upstream network. Args: feats (tuple[Tensor]): Features from the upstream network, each is a 4D-tensor. Returns: tuple: Usually a tuple of classification scores and bbox prediction - cls_scores (list[Tensor]): Classification scores for all scale levels, each is a 4D-tensor, the channels number is num_anchors * num_classes. - bbox_preds (list[Tensor]): Box energies / deltas for all scale levels, each is a 4D-tensor, the channels number is num_anchors * 4. """ cls_scores = [] bbox_preds = [] for i, x in enumerate(feats): cls_feat = feats[i] reg_feat = feats[i] for cls_conv in self.cls_convs[i]: cls_feat = cls_conv(cls_feat) for reg_conv in self.reg_convs[i]: reg_feat = reg_conv(reg_feat) cls_score = self.retina_cls(cls_feat) bbox_pred = self.retina_reg(reg_feat) cls_scores.append(cls_score) bbox_preds.append(bbox_pred) return cls_scores, bbox_preds
# Copyright (c) OpenMMLab. All rights reserved. from typing import Tuple import torch.nn as nn from mmcv.cnn import ConvModule, bias_init_with_prob, normal_init from torch import Tensor from mmdet.core.utils import OptConfigType, OptMultiConfig from mmdet.registry import MODELS from .anchor_head import AnchorHead @MODELS.register_module() class RetinaSepBNHead(AnchorHead): """"RetinaHead with separate BN. In RetinaHead, conv/norm layers are shared across different FPN levels, while in RetinaSepBNHead, conv layers are shared across different FPN levels, but BN layers are separated. """ def __init__(self, num_classes: int, num_ins: int, in_channels: int, stacked_convs: int = 4, conv_cfg: OptConfigType = None, norm_cfg: OptConfigType = None, init_cfg: OptMultiConfig = None, **kwargs) -> None: assert init_cfg is None, 'To prevent abnormal initialization ' \ 'behavior, init_cfg is not allowed to be set' self.stacked_convs = stacked_convs self.conv_cfg = conv_cfg self.norm_cfg = norm_cfg self.num_ins = num_ins super().__init__( num_classes=num_classes, in_channels=in_channels, init_cfg=init_cfg, **kwargs) def _init_layers(self) -> None: """Initialize layers of the head.""" self.relu = nn.ReLU(inplace=True) self.cls_convs = nn.ModuleList() self.reg_convs = nn.ModuleList() for i in range(self.num_ins): cls_convs = nn.ModuleList() reg_convs = nn.ModuleList() for j in range(self.stacked_convs): chn = self.in_channels if j == 0 else self.feat_channels cls_convs.append( ConvModule( chn, self.feat_channels, 3, stride=1, padding=1, conv_cfg=self.conv_cfg, norm_cfg=self.norm_cfg)) reg_convs.append( ConvModule( chn, self.feat_channels, 3, stride=1, padding=1, conv_cfg=self.conv_cfg, norm_cfg=self.norm_cfg)) self.cls_convs.append(cls_convs) self.reg_convs.append(reg_convs) for i in range(self.stacked_convs): for j in range(1, self.num_ins): self.cls_convs[j][i].conv = self.cls_convs[0][i].conv self.reg_convs[j][i].conv = self.reg_convs[0][i].conv self.retina_cls = nn.Conv2d( self.feat_channels, self.num_base_priors * self.cls_out_channels, 3, padding=1) self.retina_reg = nn.Conv2d( self.feat_channels, self.num_base_priors * 4, 3, padding=1) def init_weights(self) -> None: """Initialize weights of the head.""" super().init_weights() for m in self.cls_convs[0]: normal_init(m.conv, std=0.01) for m in self.reg_convs[0]: normal_init(m.conv, std=0.01) bias_cls = bias_init_with_prob(0.01) normal_init(self.retina_cls, std=0.01, bias=bias_cls) normal_init(self.retina_reg, std=0.01) def forward(self, feats: Tuple[Tensor]) -> tuple: """Forward features from the upstream network. Args: feats (tuple[Tensor]): Features from the upstream network, each is a 4D-tensor. Returns: tuple: Usually a tuple of classification scores and bbox prediction - cls_scores (list[Tensor]): Classification scores for all scale levels, each is a 4D-tensor, the channels number is num_anchors * num_classes. - bbox_preds (list[Tensor]): Box energies / deltas for all scale levels, each is a 4D-tensor, the channels number is num_anchors * 4. """ cls_scores = [] bbox_preds = [] for i, x in enumerate(feats): cls_feat = feats[i] reg_feat = feats[i] for cls_conv in self.cls_convs[i]: cls_feat = cls_conv(cls_feat) for reg_conv in self.reg_convs[i]: reg_feat = reg_conv(reg_feat) cls_score = self.retina_cls(cls_feat) bbox_pred = self.retina_reg(reg_feat) cls_scores.append(cls_score) bbox_preds.append(bbox_pred) return cls_scores, bbox_preds
# type: ignore """Script to generate migrations for the migration script.""" import json import os import pkgutil from typing import Optional import click from langchain_cli.namespaces.migrate.generate.generic import ( generate_simplified_migrations, ) from langchain_cli.namespaces.migrate.generate.grit import ( dump_migrations_as_grit, ) from langchain_cli.namespaces.migrate.generate.partner import ( get_migrations_for_partner_package, ) @click.group() def cli() -> None: """Migration scripts management.""" @cli.command() @click.option( "--pkg1", default="langchain", ) @click.option( "--pkg2", default="langchain_community", ) @click.option( "--output", default=None, help="Output file for the migration script.", ) @click.option( "--filter-by-all/--no-filter-by-all", default=True, help="Output file for the migration script.", ) @click.option( "--format", type=click.Choice(["json", "grit"], case_sensitive=False), default="json", help="The output format for the migration script (json or grit).", ) def generic( pkg1: str, pkg2: str, output: str, filter_by_all: bool, format: str ) -> None: """Generate a migration script.""" click.echo("Migration script generated.") migrations = generate_simplified_migrations(pkg1, pkg2, filter_by_all=filter_by_all) if output is not None: name = output.removesuffix(".json").removesuffix(".grit") else: name = f"{pkg1}_to_{pkg2}" if output is None: output = f"{name}.json" if format == "json" else f"{name}.grit" if format == "json": dumped = json.dumps(migrations, indent=2, sort_keys=True) else: dumped = dump_migrations_as_grit(name, migrations) with open(output, "w") as f: f.write(dumped) def handle_partner(pkg: str, output: Optional[str] = None) -> None: migrations = get_migrations_for_partner_package(pkg) # Run with python 3.9+ name = pkg.removeprefix("langchain_") data = dump_migrations_as_grit(name, migrations) output_name = f"{name}.grit" if output is None else output if migrations: with open(output_name, "w") as f: f.write(data) click.secho(f"LangChain migration script saved to {output_name}") else: click.secho(f"No migrations found for {pkg}", fg="yellow") @cli.command() @click.argument("pkg") @click.option("--output", default=None, help="Output file for the migration script.") def partner(pkg: str, output: str) -> None: """Generate migration scripts specifically for LangChain modules.""" click.echo("Migration script for LangChain generated.") handle_partner(pkg, output) @cli.command() @click.argument("json_file") def json_to_grit(json_file: str) -> None: """Generate a Grit migration from an old JSON migration file.""" with open(json_file) as f: migrations = json.load(f) name = os.path.basename(json_file).removesuffix(".json").removesuffix(".grit") data = dump_migrations_as_grit(name, migrations) output_name = f"{name}.grit" with open(output_name, "w") as f: f.write(data) click.secho(f"GritQL migration script saved to {output_name}") @cli.command() def all_installed_partner_pkgs() -> None: """Generate migration scripts for all LangChain modules.""" # Will generate migrations for all partner packages. # Define as "langchain_<partner_name>". # First let's determine which packages are installed in the environment # and then generate migrations for them. langchain_pkgs = [ name for _, name, _ in pkgutil.iter_modules() if name.startswith("langchain_") and name not in {"langchain_core", "langchain_cli", "langchain_community"} ] for pkg in langchain_pkgs: handle_partner(pkg) if __name__ == "__main__": cli()
# type: ignore """Script to generate migrations for the migration script.""" import json import os import pkgutil import click from langchain_cli.namespaces.migrate.generate.generic import ( generate_simplified_migrations, ) from langchain_cli.namespaces.migrate.generate.grit import ( dump_migrations_as_grit, ) from langchain_cli.namespaces.migrate.generate.partner import ( get_migrations_for_partner_package, ) @click.group() def cli(): """Migration scripts management.""" pass @cli.command() @click.option( "--pkg1", default="langchain", ) @click.option( "--pkg2", default="langchain_community", ) @click.option( "--output", default=None, help="Output file for the migration script.", ) @click.option( "--filter-by-all/--no-filter-by-all", default=True, help="Output file for the migration script.", ) @click.option( "--format", type=click.Choice(["json", "grit"], case_sensitive=False), default="json", help="The output format for the migration script (json or grit).", ) def generic( pkg1: str, pkg2: str, output: str, filter_by_all: bool, format: str ) -> None: """Generate a migration script.""" click.echo("Migration script generated.") migrations = generate_simplified_migrations(pkg1, pkg2, filter_by_all=filter_by_all) if output is not None: name = output.removesuffix(".json").removesuffix(".grit") else: name = f"{pkg1}_to_{pkg2}" if output is None: output = f"{name}.json" if format == "json" else f"{name}.grit" if format == "json": dumped = json.dumps(migrations, indent=2, sort_keys=True) else: dumped = dump_migrations_as_grit(name, migrations) with open(output, "w") as f: f.write(dumped) def handle_partner(pkg: str, output: str = None): migrations = get_migrations_for_partner_package(pkg) # Run with python 3.9+ name = pkg.removeprefix("langchain_") data = dump_migrations_as_grit(name, migrations) output_name = f"{name}.grit" if output is None else output if migrations: with open(output_name, "w") as f: f.write(data) click.secho(f"LangChain migration script saved to {output_name}") else: click.secho(f"No migrations found for {pkg}", fg="yellow") @cli.command() @click.argument("pkg") @click.option("--output", default=None, help="Output file for the migration script.") def partner(pkg: str, output: str) -> None: """Generate migration scripts specifically for LangChain modules.""" click.echo("Migration script for LangChain generated.") handle_partner(pkg, output) @cli.command() @click.argument("json_file") def json_to_grit(json_file: str) -> None: """Generate a Grit migration from an old JSON migration file.""" with open(json_file) as f: migrations = json.load(f) name = os.path.basename(json_file).removesuffix(".json").removesuffix(".grit") data = dump_migrations_as_grit(name, migrations) output_name = f"{name}.grit" with open(output_name, "w") as f: f.write(data) click.secho(f"GritQL migration script saved to {output_name}") @cli.command() def all_installed_partner_pkgs() -> None: """Generate migration scripts for all LangChain modules.""" # Will generate migrations for all partner packages. # Define as "langchain_<partner_name>". # First let's determine which packages are installed in the environment # and then generate migrations for them. langchain_pkgs = [ name for _, name, _ in pkgutil.iter_modules() if name.startswith("langchain_") and name not in {"langchain_core", "langchain_cli", "langchain_community"} ] for pkg in langchain_pkgs: handle_partner(pkg) if __name__ == "__main__": cli()
import os from typing import BinaryIO, Optional, Union import pyarrow.parquet as pq from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class ParquetDatasetReader(AbstractDatasetReader): def __init__( self, path_or_paths: NestedDataStructureLike[PathLike], split: Optional[NamedSplit] = None, features: Optional[Features] = None, cache_dir: str = None, keep_in_memory: bool = False, streaming: bool = False, num_proc: Optional[int] = None, **kwargs, ): super().__init__( path_or_paths, split=split, features=features, cache_dir=cache_dir, keep_in_memory=keep_in_memory, streaming=streaming, num_proc=num_proc, **kwargs, ) path_or_paths = path_or_paths if isinstance(path_or_paths, dict) else {self.split: path_or_paths} hash = _PACKAGED_DATASETS_MODULES["parquet"][1] self.builder = Parquet( cache_dir=cache_dir, data_files=path_or_paths, features=features, hash=hash, **kwargs, ) def read(self): # Build iterable dataset if self.streaming: dataset = self.builder.as_streaming_dataset(split=self.split) # Build regular (map-style) dataset else: download_config = None download_mode = None ignore_verifications = False base_path = None self.builder.download_and_prepare( download_config=download_config, download_mode=download_mode, ignore_verifications=ignore_verifications, # try_from_hf_gcs=try_from_hf_gcs, base_path=base_path, num_proc=self.num_proc, ) dataset = self.builder.as_dataset( split=self.split, ignore_verifications=ignore_verifications, in_memory=self.keep_in_memory ) return dataset class ParquetDatasetWriter: def __init__( self, dataset: Dataset, path_or_buf: Union[PathLike, BinaryIO], batch_size: Optional[int] = None, **parquet_writer_kwargs, ): self.dataset = dataset self.path_or_buf = path_or_buf self.batch_size = batch_size self.parquet_writer_kwargs = parquet_writer_kwargs def write(self) -> int: batch_size = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf, (str, bytes, os.PathLike)): with open(self.path_or_buf, "wb+") as buffer: written = self._write(file_obj=buffer, batch_size=batch_size, **self.parquet_writer_kwargs) else: written = self._write(file_obj=self.path_or_buf, batch_size=batch_size, **self.parquet_writer_kwargs) return written def _write(self, file_obj: BinaryIO, batch_size: int, **parquet_writer_kwargs) -> int: """Writes the pyarrow table as Parquet to a binary file handle. Caller is responsible for opening and closing the handle. """ written = 0 _ = parquet_writer_kwargs.pop("path_or_buf", None) schema = self.dataset.features.arrow_schema writer = pq.ParquetWriter(file_obj, schema=schema, **parquet_writer_kwargs) for offset in logging.tqdm( range(0, len(self.dataset), batch_size), unit="ba", disable=not logging.is_progress_bar_enabled(), desc="Creating parquet from Arrow format", ): batch = query_table( table=self.dataset._data, key=slice(offset, offset + batch_size), indices=self.dataset._indices if self.dataset._indices is not None else None, ) writer.write_table(batch) written += batch.nbytes writer.close() return written
import os from typing import BinaryIO, Optional, Union import pyarrow as pa import pyarrow.parquet as pq from .. import Dataset, Features, NamedSplit, config from ..formatting import query_table from ..packaged_modules import _PACKAGED_DATASETS_MODULES from ..packaged_modules.parquet.parquet import Parquet from ..utils import logging from ..utils.typing import NestedDataStructureLike, PathLike from .abc import AbstractDatasetReader class ParquetDatasetReader(AbstractDatasetReader): def __init__( self, path_or_paths: NestedDataStructureLike[PathLike], split: Optional[NamedSplit] = None, features: Optional[Features] = None, cache_dir: str = None, keep_in_memory: bool = False, streaming: bool = False, num_proc: Optional[int] = None, **kwargs, ): super().__init__( path_or_paths, split=split, features=features, cache_dir=cache_dir, keep_in_memory=keep_in_memory, streaming=streaming, num_proc=num_proc, **kwargs, ) path_or_paths = path_or_paths if isinstance(path_or_paths, dict) else {self.split: path_or_paths} hash = _PACKAGED_DATASETS_MODULES["parquet"][1] self.builder = Parquet( cache_dir=cache_dir, data_files=path_or_paths, features=features, hash=hash, **kwargs, ) def read(self): # Build iterable dataset if self.streaming: dataset = self.builder.as_streaming_dataset(split=self.split) # Build regular (map-style) dataset else: download_config = None download_mode = None ignore_verifications = False base_path = None self.builder.download_and_prepare( download_config=download_config, download_mode=download_mode, ignore_verifications=ignore_verifications, # try_from_hf_gcs=try_from_hf_gcs, base_path=base_path, num_proc=self.num_proc, ) dataset = self.builder.as_dataset( split=self.split, ignore_verifications=ignore_verifications, in_memory=self.keep_in_memory ) return dataset class ParquetDatasetWriter: def __init__( self, dataset: Dataset, path_or_buf: Union[PathLike, BinaryIO], batch_size: Optional[int] = None, **parquet_writer_kwargs, ): self.dataset = dataset self.path_or_buf = path_or_buf self.batch_size = batch_size self.parquet_writer_kwargs = parquet_writer_kwargs def write(self) -> int: batch_size = self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE if isinstance(self.path_or_buf, (str, bytes, os.PathLike)): with open(self.path_or_buf, "wb+") as buffer: written = self._write(file_obj=buffer, batch_size=batch_size, **self.parquet_writer_kwargs) else: written = self._write(file_obj=self.path_or_buf, batch_size=batch_size, **self.parquet_writer_kwargs) return written def _write(self, file_obj: BinaryIO, batch_size: int, **parquet_writer_kwargs) -> int: """Writes the pyarrow table as Parquet to a binary file handle. Caller is responsible for opening and closing the handle. """ written = 0 _ = parquet_writer_kwargs.pop("path_or_buf", None) schema = pa.schema(self.dataset.features.type) writer = pq.ParquetWriter(file_obj, schema=schema, **parquet_writer_kwargs) for offset in logging.tqdm( range(0, len(self.dataset), batch_size), unit="ba", disable=not logging.is_progress_bar_enabled(), desc="Creating parquet from Arrow format", ): batch = query_table( table=self.dataset._data, key=slice(offset, offset + batch_size), indices=self.dataset._indices if self.dataset._indices is not None else None, ) writer.write_table(batch) written += batch.nbytes writer.close() return written
# Copyright (c) OpenMMLab. All rights reserved. import mmcv import torch from mmcv.cnn import ConvModule, DepthwiseSeparableConvModule from mmdet.models.dense_heads import YOLOXHead def test_yolox_head_loss(): """Tests yolox head loss when truth is empty and non-empty.""" s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'scale_factor': 1, 'pad_shape': (s, s, 3) }] train_cfg = mmcv.Config( dict( assigner=dict( type='SimOTAAssigner', center_radius=2.5, candidate_topk=10, iou_weight=3.0, cls_weight=1.0))) self = YOLOXHead( num_classes=4, in_channels=1, use_depthwise=False, train_cfg=train_cfg) assert not self.use_l1 assert isinstance(self.multi_level_cls_convs[0][0], ConvModule) feat = [ torch.rand(1, 1, s // feat_size, s // feat_size) for feat_size in [4, 8, 16] ] cls_scores, bbox_preds, objectnesses = self.forward(feat) # Test that empty ground truth encourages the network to predict background gt_bboxes = [torch.empty((0, 4))] gt_labels = [torch.LongTensor([])] empty_gt_losses = self.loss(cls_scores, bbox_preds, objectnesses, gt_bboxes, gt_labels, img_metas) # When there is no truth, the cls loss should be nonzero but there should # be no box loss. empty_cls_loss = empty_gt_losses['loss_cls'].sum() empty_box_loss = empty_gt_losses['loss_bbox'].sum() empty_obj_loss = empty_gt_losses['loss_obj'].sum() assert empty_cls_loss.item() == 0, ( 'there should be no cls loss when there are no true boxes') assert empty_box_loss.item() == 0, ( 'there should be no box loss when there are no true boxes') assert empty_obj_loss.item() > 0, 'objectness loss should be non-zero' # When truth is non-empty then both cls and box loss should be nonzero for # random inputs self = YOLOXHead( num_classes=4, in_channels=1, use_depthwise=True, train_cfg=train_cfg) assert isinstance(self.multi_level_cls_convs[0][0], DepthwiseSeparableConvModule) self.use_l1 = True gt_bboxes = [ torch.Tensor([[23.6667, 23.8757, 238.6326, 151.8874]]), ] gt_labels = [torch.LongTensor([2])] one_gt_losses = self.loss(cls_scores, bbox_preds, objectnesses, gt_bboxes, gt_labels, img_metas) onegt_cls_loss = one_gt_losses['loss_cls'].sum() onegt_box_loss = one_gt_losses['loss_bbox'].sum() onegt_obj_loss = one_gt_losses['loss_obj'].sum() onegt_l1_loss = one_gt_losses['loss_l1'].sum() assert onegt_cls_loss.item() > 0, 'cls loss should be non-zero' assert onegt_box_loss.item() > 0, 'box loss should be non-zero' assert onegt_obj_loss.item() > 0, 'obj loss should be non-zero' assert onegt_l1_loss.item() > 0, 'l1 loss should be non-zero' # Test groud truth out of bound gt_bboxes = [torch.Tensor([[s * 4, s * 4, s * 4 + 10, s * 4 + 10]])] gt_labels = [torch.LongTensor([2])] empty_gt_losses = self.loss(cls_scores, bbox_preds, objectnesses, gt_bboxes, gt_labels, img_metas) # When gt_bboxes out of bound, the assign results should be empty, # so the cls and bbox loss should be zero. empty_cls_loss = empty_gt_losses['loss_cls'].sum() empty_box_loss = empty_gt_losses['loss_bbox'].sum() empty_obj_loss = empty_gt_losses['loss_obj'].sum() assert empty_cls_loss.item() == 0, ( 'there should be no cls loss when gt_bboxes out of bound') assert empty_box_loss.item() == 0, ( 'there should be no box loss when gt_bboxes out of bound') assert empty_obj_loss.item() > 0, 'objectness loss should be non-zero'
# Copyright (c) OpenMMLab. All rights reserved. import mmcv import torch from mmcv.cnn import ConvModule, DepthwiseSeparableConvModule from mmdet.models.dense_heads import YOLOXHead def test_yolox_head_loss(): """Tests yolox head loss when truth is empty and non-empty.""" s = 256 img_metas = [{ 'img_shape': (s, s, 3), 'scale_factor': 1, 'pad_shape': (s, s, 3) }] train_cfg = mmcv.Config( dict( assigner=dict( type='SimOTAAssigner', center_radius=2.5, candidate_topk=10, iou_weight=3.0, cls_weight=1.0))) self = YOLOXHead( num_classes=4, in_channels=1, use_depthwise=False, train_cfg=train_cfg) assert not self.use_l1 assert isinstance(self.multi_level_cls_convs[0][0], ConvModule) feat = [ torch.rand(1, 1, s // feat_size, s // feat_size) for feat_size in [4, 8, 16] ] cls_scores, bbox_preds, objectnesses = self.forward(feat) # Test that empty ground truth encourages the network to predict background gt_bboxes = [torch.empty((0, 4))] gt_labels = [torch.LongTensor([])] empty_gt_losses = self.loss(cls_scores, bbox_preds, objectnesses, gt_bboxes, gt_labels, img_metas) # When there is no truth, the cls loss should be nonzero but there should # be no box loss. empty_cls_loss = empty_gt_losses['loss_cls'].sum() empty_box_loss = empty_gt_losses['loss_bbox'].sum() empty_obj_loss = empty_gt_losses['loss_obj'].sum() assert empty_cls_loss.item() == 0, ( 'there should be no cls loss when there are no true boxes') assert empty_box_loss.item() == 0, ( 'there should be no box loss when there are no true boxes') assert empty_obj_loss.item() > 0, 'objectness loss should be non-zero' # When truth is non-empty then both cls and box loss should be nonzero for # random inputs self = YOLOXHead( num_classes=4, in_channels=1, use_depthwise=True, train_cfg=train_cfg) assert isinstance(self.multi_level_cls_convs[0][0], DepthwiseSeparableConvModule) self.use_l1 = True gt_bboxes = [ torch.Tensor([[23.6667, 23.8757, 238.6326, 151.8874]]), ] gt_labels = [torch.LongTensor([2])] one_gt_losses = self.loss(cls_scores, bbox_preds, objectnesses, gt_bboxes, gt_labels, img_metas) onegt_cls_loss = one_gt_losses['loss_cls'].sum() onegt_box_loss = one_gt_losses['loss_bbox'].sum() onegt_obj_loss = one_gt_losses['loss_obj'].sum() onegt_l1_loss = one_gt_losses['loss_l1'].sum() assert onegt_cls_loss.item() > 0, 'cls loss should be non-zero' assert onegt_box_loss.item() > 0, 'box loss should be non-zero' assert onegt_obj_loss.item() > 0, 'obj loss should be non-zero' assert onegt_l1_loss.item() > 0, 'l1 loss should be non-zero'
import pytest from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_doc.io.json import orjson_dumps from docarray.typing import AnyUrl @pytest.mark.proto def test_proto_any_url(): uri = parse_obj_as(AnyUrl, 'http://jina.ai/img.png') uri._to_node_protobuf() def test_json_schema(): schema_json_of(AnyUrl) def test_dump_json(): url = parse_obj_as(AnyUrl, 'http://jina.ai/img.png') orjson_dumps(url) @pytest.mark.parametrize( 'relative_path', [ 'data/05978.jpg', '../../data/05978.jpg', ], ) def test_relative_path(relative_path): # see issue: https://github.com/docarray/docarray/issues/978 url = parse_obj_as(AnyUrl, relative_path) assert url == relative_path def test_operators(): url = parse_obj_as(AnyUrl, 'data/05978.jpg') assert url == 'data/05978.jpg' assert url != 'aljdñjd' assert 'data' in url assert 'docarray' not in url
import pytest from pydantic.tools import parse_obj_as, schema_json_of from docarray.base_document.io.json import orjson_dumps from docarray.typing import AnyUrl @pytest.mark.proto def test_proto_any_url(): uri = parse_obj_as(AnyUrl, 'http://jina.ai/img.png') uri._to_node_protobuf() def test_json_schema(): schema_json_of(AnyUrl) def test_dump_json(): url = parse_obj_as(AnyUrl, 'http://jina.ai/img.png') orjson_dumps(url) @pytest.mark.parametrize( 'relative_path', [ 'data/05978.jpg', '../../data/05978.jpg', ], ) def test_relative_path(relative_path): # see issue: https://github.com/docarray/docarray/issues/978 url = parse_obj_as(AnyUrl, relative_path) assert url == relative_path def test_operators(): url = parse_obj_as(AnyUrl, 'data/05978.jpg') assert url == 'data/05978.jpg' assert url != 'aljdñjd' assert 'data' in url assert 'docarray' not in url
import pathlib from typing import Any, BinaryIO, Dict, Iterator, List, Tuple, Union from torchdata.datapipes.iter import Filter, IterDataPipe, Mapper, Zipper from torchvision.prototype.datasets.utils import Dataset, EncodedImage, HttpResource, OnlineResource from torchvision.prototype.datasets.utils._internal import ( hint_sharding, hint_shuffling, path_comparator, read_categories_file, read_mat, ) from torchvision.prototype.features import BoundingBox, Label from .._api import register_dataset, register_info class StanfordCarsLabelReader(IterDataPipe[Tuple[int, int, int, int, int, str]]): def __init__(self, datapipe: IterDataPipe[Dict[str, Any]]) -> None: self.datapipe = datapipe def __iter__(self) -> Iterator[Tuple[int, int, int, int, int, str]]: for _, file in self.datapipe: data = read_mat(file, squeeze_me=True) for ann in data["annotations"]: yield tuple(ann) # type: ignore[misc] NAME = "stanford-cars" @register_info(NAME) def _info() -> Dict[str, Any]: return dict(categories=read_categories_file(NAME)) @register_dataset(NAME) class StanfordCars(Dataset): """Stanford Cars dataset. homepage="https://ai.stanford.edu/~jkrause/cars/car_dataset.html", dependencies=scipy """ def __init__( self, root: Union[str, pathlib.Path], *, split: str = "train", skip_integrity_check: bool = False, ) -> None: self._split = self._verify_str_arg(split, "split", {"train", "test"}) self._categories = _info()["categories"] super().__init__(root, skip_integrity_check=skip_integrity_check, dependencies=("scipy",)) _URL_ROOT = "https://ai.stanford.edu/~jkrause/" _URLS = { "train": f"{_URL_ROOT}car196/cars_train.tgz", "test": f"{_URL_ROOT}car196/cars_test.tgz", "cars_test_annos_withlabels": f"{_URL_ROOT}car196/cars_test_annos_withlabels.mat", "car_devkit": f"{_URL_ROOT}cars/car_devkit.tgz", } _CHECKSUM = { "train": "b97deb463af7d58b6bfaa18b2a4de9829f0f79e8ce663dfa9261bf7810e9accd", "test": "bffea656d6f425cba3c91c6d83336e4c5f86c6cffd8975b0f375d3a10da8e243", "cars_test_annos_withlabels": "790f75be8ea34eeded134cc559332baf23e30e91367e9ddca97d26ed9b895f05", "car_devkit": "512b227b30e2f0a8aab9e09485786ab4479582073a144998da74d64b801fd288", } def _resources(self) -> List[OnlineResource]: resources: List[OnlineResource] = [HttpResource(self._URLS[self._split], sha256=self._CHECKSUM[self._split])] if self._split == "train": resources.append(HttpResource(url=self._URLS["car_devkit"], sha256=self._CHECKSUM["car_devkit"])) else: resources.append( HttpResource( self._URLS["cars_test_annos_withlabels"], sha256=self._CHECKSUM["cars_test_annos_withlabels"] ) ) return resources def _prepare_sample(self, data: Tuple[Tuple[str, BinaryIO], Tuple[int, int, int, int, int, str]]) -> Dict[str, Any]: image, target = data path, buffer = image image = EncodedImage.from_file(buffer) return dict( path=path, image=image, label=Label(target[4] - 1, categories=self._categories), bounding_box=BoundingBox(target[:4], format="xyxy", spatial_size=image.spatial_size), ) def _datapipe(self, resource_dps: List[IterDataPipe]) -> IterDataPipe[Dict[str, Any]]: images_dp, targets_dp = resource_dps if self._split == "train": targets_dp = Filter(targets_dp, path_comparator("name", "cars_train_annos.mat")) targets_dp = StanfordCarsLabelReader(targets_dp) dp = Zipper(images_dp, targets_dp) dp = hint_shuffling(dp) dp = hint_sharding(dp) return Mapper(dp, self._prepare_sample) def _generate_categories(self) -> List[str]: resources = self._resources() devkit_dp = resources[1].load(self._root) meta_dp = Filter(devkit_dp, path_comparator("name", "cars_meta.mat")) _, meta_file = next(iter(meta_dp)) return list(read_mat(meta_file, squeeze_me=True)["class_names"]) def __len__(self) -> int: return 8_144 if self._split == "train" else 8_041
import pathlib from typing import Any, BinaryIO, Dict, Iterator, List, Tuple, Union from torchdata.datapipes.iter import Filter, IterDataPipe, Mapper, Zipper from torchvision.prototype.datasets.utils import Dataset, HttpResource, OnlineResource from torchvision.prototype.datasets.utils._internal import ( hint_sharding, hint_shuffling, path_comparator, read_categories_file, read_mat, ) from torchvision.prototype.features import BoundingBox, EncodedImage, Label from .._api import register_dataset, register_info class StanfordCarsLabelReader(IterDataPipe[Tuple[int, int, int, int, int, str]]): def __init__(self, datapipe: IterDataPipe[Dict[str, Any]]) -> None: self.datapipe = datapipe def __iter__(self) -> Iterator[Tuple[int, int, int, int, int, str]]: for _, file in self.datapipe: data = read_mat(file, squeeze_me=True) for ann in data["annotations"]: yield tuple(ann) # type: ignore[misc] NAME = "stanford-cars" @register_info(NAME) def _info() -> Dict[str, Any]: return dict(categories=read_categories_file(NAME)) @register_dataset(NAME) class StanfordCars(Dataset): """Stanford Cars dataset. homepage="https://ai.stanford.edu/~jkrause/cars/car_dataset.html", dependencies=scipy """ def __init__( self, root: Union[str, pathlib.Path], *, split: str = "train", skip_integrity_check: bool = False, ) -> None: self._split = self._verify_str_arg(split, "split", {"train", "test"}) self._categories = _info()["categories"] super().__init__(root, skip_integrity_check=skip_integrity_check, dependencies=("scipy",)) _URL_ROOT = "https://ai.stanford.edu/~jkrause/" _URLS = { "train": f"{_URL_ROOT}car196/cars_train.tgz", "test": f"{_URL_ROOT}car196/cars_test.tgz", "cars_test_annos_withlabels": f"{_URL_ROOT}car196/cars_test_annos_withlabels.mat", "car_devkit": f"{_URL_ROOT}cars/car_devkit.tgz", } _CHECKSUM = { "train": "b97deb463af7d58b6bfaa18b2a4de9829f0f79e8ce663dfa9261bf7810e9accd", "test": "bffea656d6f425cba3c91c6d83336e4c5f86c6cffd8975b0f375d3a10da8e243", "cars_test_annos_withlabels": "790f75be8ea34eeded134cc559332baf23e30e91367e9ddca97d26ed9b895f05", "car_devkit": "512b227b30e2f0a8aab9e09485786ab4479582073a144998da74d64b801fd288", } def _resources(self) -> List[OnlineResource]: resources: List[OnlineResource] = [HttpResource(self._URLS[self._split], sha256=self._CHECKSUM[self._split])] if self._split == "train": resources.append(HttpResource(url=self._URLS["car_devkit"], sha256=self._CHECKSUM["car_devkit"])) else: resources.append( HttpResource( self._URLS["cars_test_annos_withlabels"], sha256=self._CHECKSUM["cars_test_annos_withlabels"] ) ) return resources def _prepare_sample(self, data: Tuple[Tuple[str, BinaryIO], Tuple[int, int, int, int, int, str]]) -> Dict[str, Any]: image, target = data path, buffer = image image = EncodedImage.from_file(buffer) return dict( path=path, image=image, label=Label(target[4] - 1, categories=self._categories), bounding_box=BoundingBox(target[:4], format="xyxy", spatial_size=image.spatial_size), ) def _datapipe(self, resource_dps: List[IterDataPipe]) -> IterDataPipe[Dict[str, Any]]: images_dp, targets_dp = resource_dps if self._split == "train": targets_dp = Filter(targets_dp, path_comparator("name", "cars_train_annos.mat")) targets_dp = StanfordCarsLabelReader(targets_dp) dp = Zipper(images_dp, targets_dp) dp = hint_shuffling(dp) dp = hint_sharding(dp) return Mapper(dp, self._prepare_sample) def _generate_categories(self) -> List[str]: resources = self._resources() devkit_dp = resources[1].load(self._root) meta_dp = Filter(devkit_dp, path_comparator("name", "cars_meta.mat")) _, meta_file = next(iter(meta_dp)) return list(read_mat(meta_file, squeeze_me=True)["class_names"]) def __len__(self) -> int: return 8_144 if self._split == "train" else 8_041
import numpy as np from sentence_transformers.sparse_encoder import SparseEncoder from sentence_transformers.sparse_encoder.models import MLMTransformer, SpladePooling def main(): # Initialize the SPLADE model model_name = "naver/splade-cocondenser-ensembledistil" # "naver/efficient-splade-V-large-doc" # "prithivida/Splade_PP_en_v1" # "naver/splade-cocondenser-ensembledistil" model = SparseEncoder( modules=[ MLMTransformer(model_name), SpladePooling(pooling_strategy="max"), # You can also use 'sum' ], device="cuda:0", ) # Sample texts texts = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium.", ] # Generate embeddings embeddings = model.encode(texts, convert_to_sparse_tensor=True) print(type(embeddings)) # Print embedding sim and sparsity print(f"Embedding dim: {model.get_sentence_embedding_dimension()}") print(f"Embedding sparsity: {model.get_sparsity_stats(embeddings)}") # Compute similarity matrix similarity_matrix = model.similarity(embeddings, embeddings) # Print similarity matrix print("\nSimilarity Matrix:") for i, text in enumerate(texts): print(f"{i}: {text[:50]}...") print("\n" + " " * 10 + " ".join([f"{i:5d}" for i in range(len(texts))])) for i, row in enumerate(similarity_matrix): print(f"{i:5d} " + " ".join([f"{val:.3f}" for val in row])) vocab_size = embeddings.shape[1] print(f"Vocabulary size: {vocab_size}") # Visualize top tokens for each text top_k = 20 print(f"\nTop tokens {top_k} for each text:") for i, text in enumerate(texts): # Get top k indices in sparse tensor # Get top k indices in sparse tensor (sorted from highest to lowest) top_indices = np.argsort(-embeddings[i].to_dense().cpu().numpy())[:top_k] top_values = embeddings[i].to_dense().cpu().numpy()[top_indices] top_tokens = [model.tokenizer.decode([idx]) for idx in top_indices] print(f"{i}: {text}") print(f"Top tokens: {top_tokens}") print(f"Top values: {top_values}") print() # Save the model model.push_to_hub( "sparse-embedding/splade_example", private=True, ) # Load the model loaded_model = SparseEncoder("sparse-embedding/splade_example") print(f"Loaded model: {loaded_model}") # Sample texts texts = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium.", ] # Generate embeddings embeddings = loaded_model.encode(texts, convert_to_sparse_tensor=True) print(type(embeddings)) # Print embedding shape and sparsity print(f"Embedding shape: {embeddings.shape}") print(f"Embedding sparsity: {loaded_model.get_sparsity_stats(embeddings)}%") # Compute similarity matrix similarity_matrix = loaded_model.similarity(embeddings, embeddings) # Print similarity matrix print("\nSimilarity Matrix:") for i, text in enumerate(texts): print(f"{i}: {text[:50]}...") print("\n" + " " * 10 + " ".join([f"{i:5d}" for i in range(len(texts))])) for i, row in enumerate(similarity_matrix): print(f"{i:5d} " + " ".join([f"{val:.3f}" for val in row])) vocab_size = embeddings.shape[1] print(f"Vocabulary size: {vocab_size}") # Visualize top tokens for each text top_k = 20 print(f"\nTop tokens {top_k} for each text:") for i, text in enumerate(texts): # Get top k indices in sparse tensor # Get top k indices in sparse tensor (sorted from highest to lowest) top_indices = np.argsort(-embeddings[i].to_dense().cpu().numpy())[:top_k] top_values = embeddings[i].to_dense().cpu().numpy()[top_indices] top_tokens = [model.tokenizer.decode([idx]) for idx in top_indices] print(f"{i}: {text}") print(f"Top tokens: {top_tokens}") print(f"Top values: {top_values}") print() if __name__ == "__main__": main()
import numpy as np from sentence_transformers.sparse_encoder import SparseEncoder from sentence_transformers.sparse_encoder.models import MLMTransformer, SpladePooling def main(): # Initialize the SPLADE model model_name = "opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill" # "naver/efficient-splade-V-large-doc" # "prithivida/Splade_PP_en_v1" # "naver/splade-cocondenser-ensembledistil" model = SparseEncoder( modules=[ MLMTransformer(model_name), SpladePooling(pooling_strategy="max"), # You can also use 'sum' ], device="cuda:0", ) # Sample texts texts = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium.", ] # Generate embeddings embeddings = model.encode(texts, convert_to_sparse_tensor=True) print(type(embeddings)) # Print embedding sim and sparsity print(f"Embedding dim: {model.get_sentence_embedding_dimension()}") print(f"Embedding sparsity: {model.get_sparsity_stats(embeddings)}") # Compute similarity matrix similarity_matrix = model.similarity(embeddings, embeddings) # Print similarity matrix print("\nSimilarity Matrix:") for i, text in enumerate(texts): print(f"{i}: {text[:50]}...") print("\n" + " " * 10 + " ".join([f"{i:5d}" for i in range(len(texts))])) for i, row in enumerate(similarity_matrix): print(f"{i:5d} " + " ".join([f"{val:.3f}" for val in row])) vocab_size = embeddings.shape[1] print(f"Vocabulary size: {vocab_size}") # Visualize top tokens for each text top_k = 20 print(f"\nTop tokens {top_k} for each text:") for i, text in enumerate(texts): # Get top k indices in sparse tensor # Get top k indices in sparse tensor (sorted from highest to lowest) top_indices = np.argsort(-embeddings[i].to_dense().cpu().numpy())[:top_k] top_values = embeddings[i].to_dense().cpu().numpy()[top_indices] top_tokens = [model.tokenizer.decode([idx]) for idx in top_indices] print(f"{i}: {text}") print(f"Top tokens: {top_tokens}") print(f"Top values: {top_values}") print() # Save the model model.push_to_hub( "sparse-embedding/splade_example", private=True, ) # Load the model loaded_model = SparseEncoder("sparse-embedding/splade_example") print(f"Loaded model: {loaded_model}") # Sample texts texts = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium.", ] # Generate embeddings embeddings = loaded_model.encode(texts, convert_to_sparse_tensor=True) print(type(embeddings)) # Print embedding shape and sparsity print(f"Embedding shape: {embeddings.shape}") print(f"Embedding sparsity: {loaded_model.get_sparsity_stats(embeddings)}%") # Compute similarity matrix similarity_matrix = loaded_model.similarity(embeddings, embeddings) # Print similarity matrix print("\nSimilarity Matrix:") for i, text in enumerate(texts): print(f"{i}: {text[:50]}...") print("\n" + " " * 10 + " ".join([f"{i:5d}" for i in range(len(texts))])) for i, row in enumerate(similarity_matrix): print(f"{i:5d} " + " ".join([f"{val:.3f}" for val in row])) vocab_size = embeddings.shape[1] print(f"Vocabulary size: {vocab_size}") # Visualize top tokens for each text top_k = 20 print(f"\nTop tokens {top_k} for each text:") for i, text in enumerate(texts): # Get top k indices in sparse tensor # Get top k indices in sparse tensor (sorted from highest to lowest) top_indices = np.argsort(-embeddings[i].to_dense().cpu().numpy())[:top_k] top_values = embeddings[i].to_dense().cpu().numpy()[top_indices] top_tokens = [model.tokenizer.decode([idx]) for idx in top_indices] print(f"{i}: {text}") print(f"Top tokens: {top_tokens}") print(f"Top values: {top_values}") print() if __name__ == "__main__": main()
from typing import Optional from docarray.typing.proto_register import _register_proto from docarray.typing.url.any_url import AnyUrl from docarray.typing.url.helper import _uri_to_blob @_register_proto(proto_type_name='text_url') class TextUrl(AnyUrl): """ URL to a text file. Can be remote (web) URL, or a local file path. """ def load_to_bytes(self, timeout: Optional[float] = None) -> bytes: """ Load the text file into a bytes object. EXAMPLE USAGE .. code-block:: python from docarray import BaseDocument from docarray.typing import TextUrl class MyDoc(BaseDocument): remote_url: TextUrl local_url: TextUrl doc = MyDoc( remote_url='https://de.wikipedia.org/wiki/Brixen', local_url='home/username/my_file.txt', ) remote_txt_bytes = doc.remote_url.load_to_bytes() local_txt_bytes = doc.local_url.load_to_bytes() :param timeout: timeout (sec) for urlopen network request. Only relevant if URL is not local :return: the text file content as bytes """ return _uri_to_blob(self, timeout=timeout) def load(self, charset: str = 'utf-8', timeout: Optional[float] = None) -> str: """ Load the text file into a string. EXAMPLE USAGE .. code-block:: python from docarray import BaseDocument from docarray.typing import TextUrl class MyDoc(BaseDocument): remote_url: TextUrl local_url: TextUrl doc = MyDoc( remote_url='https://de.wikipedia.org/wiki/Brixen', local_url='home/username/my_file.txt', ) remote_txt = doc.remote_url.load() print(remote_txt) # prints: ```<!DOCTYPE html>\n<html class="client-nojs" ... > ...``` local_txt = doc.local_url.load() print(local_txt) # prints content of my_file.txt :param timeout: timeout (sec) for urlopen network request. Only relevant if URL is not local :param charset: decoding charset; may be any character set registered with IANA :return: the text file content """ _bytes = _uri_to_blob(self, timeout=timeout) return _bytes.decode(charset)
from typing import TYPE_CHECKING, Optional if TYPE_CHECKING: from docarray.proto import NodeProto from docarray.typing.url.any_url import AnyUrl from docarray.typing.url.helper import _uri_to_blob class TextUrl(AnyUrl): """ URL to a text file. Can be remote (web) URL, or a local file path. """ def _to_node_protobuf(self) -> 'NodeProto': """Convert Document into a NodeProto protobuf message. This function should be called when the Document 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_url=str(self)) def load_to_bytes(self, timeout: Optional[float] = None) -> bytes: """ Load the text file into a bytes object. EXAMPLE USAGE .. code-block:: python from docarray import BaseDocument from docarray.typing import TextUrl class MyDoc(BaseDocument): remote_url: TextUrl local_url: TextUrl doc = MyDoc( remote_url='https://de.wikipedia.org/wiki/Brixen', local_url='home/username/my_file.txt', ) remote_txt_bytes = doc.remote_url.load_to_bytes() local_txt_bytes = doc.local_url.load_to_bytes() :param timeout: timeout (sec) for urlopen network request. Only relevant if URL is not local :return: the text file content as bytes """ return _uri_to_blob(self, timeout=timeout) def load(self, charset: str = 'utf-8', timeout: Optional[float] = None) -> str: """ Load the text file into a string. EXAMPLE USAGE .. code-block:: python from docarray import BaseDocument from docarray.typing import TextUrl class MyDoc(BaseDocument): remote_url: TextUrl local_url: TextUrl doc = MyDoc( remote_url='https://de.wikipedia.org/wiki/Brixen', local_url='home/username/my_file.txt', ) remote_txt = doc.remote_url.load() print(remote_txt) # prints: ```<!DOCTYPE html>\n<html class="client-nojs" ... > ...``` local_txt = doc.local_url.load() print(local_txt) # prints content of my_file.txt :param timeout: timeout (sec) for urlopen network request. Only relevant if URL is not local :param charset: decoding charset; may be any character set registered with IANA :return: the text file content """ _bytes = _uri_to_blob(self, timeout=timeout) return _bytes.decode(charset)
_base_ = './yolox_s_8x8_300e_coco.py' # model settings model = dict( random_size_range=(10, 20), backbone=dict(deepen_factor=0.33, widen_factor=0.375), neck=dict(in_channels=[96, 192, 384], out_channels=96), bbox_head=dict(in_channels=96, feat_channels=96)) img_scale = (640, 640) # height, width train_pipeline = [ dict(type='Mosaic', img_scale=img_scale, pad_val=114.0), dict( type='RandomAffine', scaling_ratio_range=(0.5, 1.5), border=(-img_scale[0] // 2, -img_scale[1] // 2)), dict(type='YOLOXHSVRandomAug'), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Resize', img_scale=img_scale, keep_ratio=True), dict( type='Pad', pad_to_square=True, pad_val=dict(img=(114.0, 114.0, 114.0))), dict(type='FilterAnnotations', min_gt_bbox_wh=(1, 1), keep_empty=False), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(416, 416), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Pad', pad_to_square=True, pad_val=dict(img=(114.0, 114.0, 114.0))), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img']) ]) ] train_dataset = dict(pipeline=train_pipeline) data = dict( train=train_dataset, val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline)) # NOTE: `auto_scale_lr` is for automatically scaling LR, # USER SHOULD NOT CHANGE ITS VALUES. # base_batch_size = (8 GPUs) x (8 samples per GPU) auto_scale_lr = dict(base_batch_size=64)
_base_ = './yolox_s_8x8_300e_coco.py' # model settings model = dict( random_size_range=(10, 20), backbone=dict(deepen_factor=0.33, widen_factor=0.375), neck=dict(in_channels=[96, 192, 384], out_channels=96), bbox_head=dict(in_channels=96, feat_channels=96)) img_scale = (640, 640) # height, width train_pipeline = [ dict(type='Mosaic', img_scale=img_scale, pad_val=114.0), dict( type='RandomAffine', scaling_ratio_range=(0.5, 1.5), border=(-img_scale[0] // 2, -img_scale[1] // 2)), dict(type='YOLOXHSVRandomAug'), dict(type='RandomFlip', flip_ratio=0.5), dict(type='Resize', img_scale=img_scale, keep_ratio=True), dict( type='Pad', pad_to_square=True, pad_val=dict(img=(114.0, 114.0, 114.0))), dict(type='FilterAnnotations', min_gt_bbox_wh=(1, 1), keep_empty=False), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(416, 416), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Pad', pad_to_square=True, pad_val=dict(img=(114.0, 114.0, 114.0))), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img']) ]) ] train_dataset = dict(pipeline=train_pipeline) data = dict( train=train_dataset, val=dict(pipeline=test_pipeline), test=dict(pipeline=test_pipeline))
import pytest @pytest.mark.compile def test_placeholder() -> None: """Used for compiling integration tests without running any real tests."""
import pytest @pytest.mark.compile def test_placeholder() -> None: """Used for compiling integration tests without running any real tests.""" pass
from ._dsp import adsr_envelope, extend_pitch, frequency_impulse_response, oscillator_bank, sinc_impulse_response from .functional import add_noise, barkscale_fbanks, convolve, deemphasis, fftconvolve, preemphasis, speed __all__ = [ "add_noise", "adsr_envelope", "barkscale_fbanks", "convolve", "deemphasis", "extend_pitch", "fftconvolve", "frequency_impulse_response", "oscillator_bank", "preemphasis", "sinc_impulse_response", "speed", ]
from ._dsp import adsr_envelope, extend_pitch, oscillator_bank, sinc_impulse_response from .functional import add_noise, barkscale_fbanks, convolve, deemphasis, fftconvolve, preemphasis, speed __all__ = [ "add_noise", "adsr_envelope", "barkscale_fbanks", "convolve", "deemphasis", "extend_pitch", "fftconvolve", "oscillator_bank", "preemphasis", "sinc_impulse_response", "speed", ]
from __future__ import annotations from collections.abc import Sequence from typing import Any, TypeVar import torch from torch.utils._pytree import tree_map from torchvision.tv_tensors._tv_tensor import TVTensor L = TypeVar("L", bound="_LabelBase") class _LabelBase(TVTensor): categories: Sequence[str] | None @classmethod def _wrap(cls: type[L], tensor: torch.Tensor, *, categories: Sequence[str] | None) -> L: label_base = tensor.as_subclass(cls) label_base.categories = categories return label_base def __new__( cls: type[L], data: Any, *, categories: Sequence[str] | None = None, dtype: torch.dtype | None = None, device: torch.device | str | int | None = None, requires_grad: bool | None = None, ) -> L: tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad) return cls._wrap(tensor, categories=categories) @classmethod def from_category( cls: type[L], category: str, *, categories: Sequence[str], **kwargs: Any, ) -> L: return cls(categories.index(category), categories=categories, **kwargs) class Label(_LabelBase): def to_categories(self) -> Any: if self.categories is None: raise RuntimeError("Label does not have categories") return tree_map(lambda idx: self.categories[idx], self.tolist()) # type: ignore[index] class OneHotLabel(_LabelBase): def __new__( cls, data: Any, *, categories: Sequence[str] | None = None, dtype: torch.dtype | None = None, device: torch.device | str | int | None = None, requires_grad: bool = False, ) -> OneHotLabel: one_hot_label = super().__new__( cls, data, categories=categories, dtype=dtype, device=device, requires_grad=requires_grad ) if categories is not None and len(categories) != one_hot_label.shape[-1]: raise ValueError() return one_hot_label
from __future__ import annotations from typing import Any, Optional, Sequence, Type, TypeVar, Union import torch from torch.utils._pytree import tree_map from torchvision.tv_tensors._tv_tensor import TVTensor L = TypeVar("L", bound="_LabelBase") class _LabelBase(TVTensor): categories: Optional[Sequence[str]] @classmethod def _wrap(cls: Type[L], tensor: torch.Tensor, *, categories: Optional[Sequence[str]]) -> L: label_base = tensor.as_subclass(cls) label_base.categories = categories return label_base def __new__( cls: Type[L], data: Any, *, categories: Optional[Sequence[str]] = None, dtype: Optional[torch.dtype] = None, device: Optional[Union[torch.device, str, int]] = None, requires_grad: Optional[bool] = None, ) -> L: tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad) return cls._wrap(tensor, categories=categories) @classmethod def from_category( cls: Type[L], category: str, *, categories: Sequence[str], **kwargs: Any, ) -> L: return cls(categories.index(category), categories=categories, **kwargs) class Label(_LabelBase): def to_categories(self) -> Any: if self.categories is None: raise RuntimeError("Label does not have categories") return tree_map(lambda idx: self.categories[idx], self.tolist()) # type: ignore[index] class OneHotLabel(_LabelBase): def __new__( cls, data: Any, *, categories: Optional[Sequence[str]] = None, dtype: Optional[torch.dtype] = None, device: Optional[Union[torch.device, str, int]] = None, requires_grad: bool = False, ) -> OneHotLabel: one_hot_label = super().__new__( cls, data, categories=categories, dtype=dtype, device=device, requires_grad=requires_grad ) if categories is not None and len(categories) != one_hot_label.shape[-1]: raise ValueError() return one_hot_label
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '0.7.3' def parse_version_info(version_str): """Parse the version information. Args: version_str (str): version string like '0.1.0'. Returns: tuple: version information contains major, minor, micro version. """ version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
# Copyright (c) OpenMMLab. All rights reserved. __version__ = '0.7.2' def parse_version_info(version_str): """Parse the version information. Args: version_str (str): version string like '0.1.0'. Returns: tuple: version information contains major, minor, micro version. """ version_info = [] for x in version_str.split('.'): if x.isdigit(): version_info.append(int(x)) elif x.find('rc') != -1: patch_version = x.split('rc') version_info.append(int(patch_version[0])) version_info.append(f'rc{patch_version[1]}') return tuple(version_info) version_info = parse_version_info(__version__)
# -*- coding: utf-8 -*- # Generated by the protocol buffer compiler. DO NOT EDIT! # source: add_voter.proto """Generated protocol buffer code.""" from google.protobuf.internal import builder as _builder from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( b'\n\x0f\x61\x64\x64_voter.proto\"-\n\rAwaitResponse\x12\r\n\x05\x65rror\x18\x01 \x01(\t\x12\r\n\x05index\x18\x02 \x01(\x04\"\x10\n\x0e\x46orgetResponse\"!\n\x06\x46uture\x12\x17\n\x0foperation_token\x18\x01 \x01(\t\"F\n\x0f\x41\x64\x64VoterRequest\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0f\n\x07\x61\x64\x64ress\x18\x02 \x01(\t\x12\x16\n\x0eprevious_index\x18\x03 \x01(\x04\x32~\n\tRaftAdmin\x12\'\n\x08\x41\x64\x64Voter\x12\x10.AddVoterRequest\x1a\x07.Future\"\x00\x12\"\n\x05\x41wait\x12\x07.Future\x1a\x0e.AwaitResponse\"\x00\x12$\n\x06\x46orget\x12\x07.Future\x1a\x0f.ForgetResponse\"\x00\x62\x06proto3' ) _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'add_voter_pb2', globals()) if _descriptor._USE_C_DESCRIPTORS == False: DESCRIPTOR._options = None _AWAITRESPONSE._serialized_start = 19 _AWAITRESPONSE._serialized_end = 64 _FORGETRESPONSE._serialized_start = 66 _FORGETRESPONSE._serialized_end = 82 _FUTURE._serialized_start = 84 _FUTURE._serialized_end = 117 _ADDVOTERREQUEST._serialized_start = 119 _ADDVOTERREQUEST._serialized_end = 189 _RAFTADMIN._serialized_start = 191 _RAFTADMIN._serialized_end = 317 # @@protoc_insertion_point(module_scope)
# -*- coding: utf-8 -*- # Generated by the protocol buffer compiler. DO NOT EDIT! # source: add_voter.proto """Generated protocol buffer code.""" from google.protobuf.internal import builder as _builder from google.protobuf import descriptor as _descriptor from google.protobuf import descriptor_pool as _descriptor_pool from google.protobuf import symbol_database as _symbol_database # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0f\x61\x64\x64_voter.proto\"-\n\rAwaitResponse\x12\r\n\x05\x65rror\x18\x01 \x01(\t\x12\r\n\x05index\x18\x02 \x01(\x04\"\x10\n\x0e\x46orgetResponse\"!\n\x06\x46uture\x12\x17\n\x0foperation_token\x18\x01 \x01(\t\"F\n\x0f\x41\x64\x64VoterRequest\x12\n\n\x02id\x18\x01 \x01(\t\x12\x0f\n\x07\x61\x64\x64ress\x18\x02 \x01(\t\x12\x16\n\x0eprevious_index\x18\x03 \x01(\x04\x32~\n\tRaftAdmin\x12\'\n\x08\x41\x64\x64Voter\x12\x10.AddVoterRequest\x1a\x07.Future\"\x00\x12\"\n\x05\x41wait\x12\x07.Future\x1a\x0e.AwaitResponse\"\x00\x12$\n\x06\x46orget\x12\x07.Future\x1a\x0f.ForgetResponse\"\x00\x62\x06proto3') _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'add_voter_pb2', globals()) if _descriptor._USE_C_DESCRIPTORS == False: DESCRIPTOR._options = None _AWAITRESPONSE._serialized_start=19 _AWAITRESPONSE._serialized_end=64 _FORGETRESPONSE._serialized_start=66 _FORGETRESPONSE._serialized_end=82 _FUTURE._serialized_start=84 _FUTURE._serialized_end=117 _ADDVOTERREQUEST._serialized_start=119 _ADDVOTERREQUEST._serialized_end=189 _RAFTADMIN._serialized_start=191 _RAFTADMIN._serialized_end=317 # @@protoc_insertion_point(module_scope)
# Copyright (c) OpenMMLab. All rights reserved. from .ade20k import ADE20KPanopticDataset from .base_det_dataset import BaseDetDataset from .base_video_dataset import BaseVideoDataset from .cityscapes import CityscapesDataset from .coco import CocoDataset from .coco_caption import COCOCaptionDataset from .coco_panoptic import CocoPanopticDataset from .crowdhuman import CrowdHumanDataset from .dataset_wrappers import MultiImageMixDataset from .deepfashion import DeepFashionDataset from .dsdl import DSDLDetDataset from .lvis import LVISDataset, LVISV1Dataset, LVISV05Dataset from .mot_challenge_dataset import MOTChallengeDataset from .objects365 import Objects365V1Dataset, Objects365V2Dataset from .openimages import OpenImagesChallengeDataset, OpenImagesDataset from .reid_dataset import ReIDDataset from .samplers import (AspectRatioBatchSampler, ClassAwareSampler, GroupMultiSourceSampler, MultiSourceSampler, TrackAspectRatioBatchSampler, TrackImgSampler) from .utils import get_loading_pipeline from .voc import VOCDataset from .wider_face import WIDERFaceDataset from .xml_style import XMLDataset from .youtube_vis_dataset import YouTubeVISDataset __all__ = [ 'XMLDataset', 'CocoDataset', 'DeepFashionDataset', 'VOCDataset', 'CityscapesDataset', 'LVISDataset', 'LVISV05Dataset', 'LVISV1Dataset', 'WIDERFaceDataset', 'get_loading_pipeline', 'CocoPanopticDataset', 'MultiImageMixDataset', 'OpenImagesDataset', 'OpenImagesChallengeDataset', 'AspectRatioBatchSampler', 'ClassAwareSampler', 'MultiSourceSampler', 'GroupMultiSourceSampler', 'BaseDetDataset', 'CrowdHumanDataset', 'Objects365V1Dataset', 'Objects365V2Dataset', 'DSDLDetDataset', 'BaseVideoDataset', 'MOTChallengeDataset', 'TrackImgSampler', 'ReIDDataset', 'YouTubeVISDataset', 'TrackAspectRatioBatchSampler', 'ADE20KPanopticDataset', 'COCOCaptionDataset' ]
# Copyright (c) OpenMMLab. All rights reserved. from .ade20k import ADE20KPanopticDataset from .base_det_dataset import BaseDetDataset from .base_video_dataset import BaseVideoDataset from .cityscapes import CityscapesDataset from .coco import CocoDataset from .coco_panoptic import CocoPanopticDataset from .crowdhuman import CrowdHumanDataset from .dataset_wrappers import MultiImageMixDataset from .deepfashion import DeepFashionDataset from .dsdl import DSDLDetDataset from .lvis import LVISDataset, LVISV1Dataset, LVISV05Dataset from .mot_challenge_dataset import MOTChallengeDataset from .objects365 import Objects365V1Dataset, Objects365V2Dataset from .openimages import OpenImagesChallengeDataset, OpenImagesDataset from .reid_dataset import ReIDDataset from .samplers import (AspectRatioBatchSampler, ClassAwareSampler, GroupMultiSourceSampler, MultiSourceSampler, TrackAspectRatioBatchSampler, TrackImgSampler) from .utils import get_loading_pipeline from .voc import VOCDataset from .wider_face import WIDERFaceDataset from .xml_style import XMLDataset from .youtube_vis_dataset import YouTubeVISDataset __all__ = [ 'XMLDataset', 'CocoDataset', 'DeepFashionDataset', 'VOCDataset', 'CityscapesDataset', 'LVISDataset', 'LVISV05Dataset', 'LVISV1Dataset', 'WIDERFaceDataset', 'get_loading_pipeline', 'CocoPanopticDataset', 'MultiImageMixDataset', 'OpenImagesDataset', 'OpenImagesChallengeDataset', 'AspectRatioBatchSampler', 'ClassAwareSampler', 'MultiSourceSampler', 'GroupMultiSourceSampler', 'BaseDetDataset', 'CrowdHumanDataset', 'Objects365V1Dataset', 'Objects365V2Dataset', 'DSDLDetDataset', 'BaseVideoDataset', 'MOTChallengeDataset', 'TrackImgSampler', 'ReIDDataset', 'YouTubeVISDataset', 'TrackAspectRatioBatchSampler', 'ADE20KPanopticDataset' ]
import json import os import subprocess import pytest from jina.checker import NetworkChecker from jina.jaml import JAML from jina.orchestrate.pods.factory import PodFactory from jina.parsers import set_deployment_parser, set_pod_parser from jina.parsers.ping import set_ping_parser from jina_cli.autocomplete import ac_table from jina_cli.export import api_to_dict from jina_cli.lookup import _build_lookup_table, lookup_and_print def test_export_api(tmpdir): with open(tmpdir / 'test.yml', 'w', encoding='utf8') as fp: JAML.dump(api_to_dict(), fp) with open(tmpdir / 'test.json', 'w', encoding='utf8') as fp: json.dump(api_to_dict(), fp) @pytest.mark.parametrize('cli', ac_table['commands']) def test_help_lookup(cli, capsys): nkw2kw, kw2info = _build_lookup_table() if cli not in {'--help', '--version', '--version-full'}: assert cli in nkw2kw lookup_and_print(cli) captured = capsys.readouterr() assert 'Traceback (most recent call last)' not in captured.out def test_main_cli(): subprocess.check_call(['jina']) def test_cli_help(): subprocess.check_call(['jina', 'help', 'deployment']) def test_cli_warn_unknown_args(): subprocess.check_call(['jina', 'help', 'deployment', '--abcdefg']) @pytest.mark.parametrize('cli', ac_table['commands']) def test_all_cli(cli): subprocess.check_call(['jina', cli, '--help']) @pytest.mark.parametrize('smethod', ['fork', 'spawn']) def test_all_start_method(smethod): s = subprocess.check_output( ['jina', '-v'], env=dict(os.environ, JINA_MP_START_METHOD=smethod), stderr=subprocess.STDOUT, ) assert 'UserWarning' in s.decode() assert smethod in s.decode() def test_parse_env_map(): a = set_deployment_parser().parse_args( ['--env', 'key1=value1', '--env', 'key2=value2'] ) assert a.env == {'key1': 'value1', 'key2': 'value2'} a = set_deployment_parser().parse_args( ['--env', 'key1=value1', 'key2=value2', 'key3=3'] ) assert a.env == {'key1': 'value1', 'key2': 'value2', 'key3': 3} @pytest.mark.slow def test_ping(): a1 = set_pod_parser().parse_args([]) a2 = set_ping_parser().parse_args(['0.0.0.0', str(a1.port)]) a3 = set_ping_parser().parse_args(['0.0.0.1', str(a1.port), '--timeout', '1000']) with pytest.raises(SystemExit) as cm: with PodFactory.build_pod(a1): NetworkChecker(a2) assert cm.value.code == 0 # test with bad address with pytest.raises(SystemExit) as cm: with PodFactory.build_pod(a1): NetworkChecker(a3) assert cm.value.code == 1
import json import os import subprocess import pytest from jina.checker import NetworkChecker from jina.jaml import JAML from jina.orchestrate.pods.factory import PodFactory from jina.parsers import set_deployment_parser, set_pod_parser from jina.parsers.ping import set_ping_parser from jina_cli.autocomplete import ac_table from jina_cli.export import api_to_dict from jina_cli.lookup import _build_lookup_table, lookup_and_print def test_export_api(tmpdir): with open(tmpdir / 'test.yml', 'w', encoding='utf8') as fp: JAML.dump(api_to_dict(), fp) with open(tmpdir / 'test.json', 'w', encoding='utf8') as fp: json.dump(api_to_dict(), fp) @pytest.mark.parametrize('cli', ac_table['commands']) def test_help_lookup(cli, capsys): nkw2kw, kw2info = _build_lookup_table() if cli not in {'--help', '--version', '--version-full'}: assert cli in nkw2kw lookup_and_print(cli) captured = capsys.readouterr() assert captured.out def test_main_cli(): subprocess.check_call(['jina']) def test_cli_help(): subprocess.check_call(['jina', 'help', 'deployment']) def test_cli_warn_unknown_args(): subprocess.check_call(['jina', 'help', 'deployment', '--abcdefg']) @pytest.mark.parametrize('cli', ac_table['commands']) def test_all_cli(cli): subprocess.check_call(['jina', cli, '--help']) @pytest.mark.parametrize('smethod', ['fork', 'spawn']) def test_all_start_method(smethod): s = subprocess.check_output( ['jina', '-v'], env=dict(os.environ, JINA_MP_START_METHOD=smethod), stderr=subprocess.STDOUT, ) assert 'UserWarning' in s.decode() assert smethod in s.decode() def test_parse_env_map(): a = set_deployment_parser().parse_args( ['--env', 'key1=value1', '--env', 'key2=value2'] ) assert a.env == {'key1': 'value1', 'key2': 'value2'} a = set_deployment_parser().parse_args( ['--env', 'key1=value1', 'key2=value2', 'key3=3'] ) assert a.env == {'key1': 'value1', 'key2': 'value2', 'key3': 3} @pytest.mark.slow def test_ping(): a1 = set_pod_parser().parse_args([]) a2 = set_ping_parser().parse_args(['0.0.0.0', str(a1.port)]) a3 = set_ping_parser().parse_args(['0.0.0.1', str(a1.port), '--timeout', '1000']) with pytest.raises(SystemExit) as cm: with PodFactory.build_pod(a1): NetworkChecker(a2) assert cm.value.code == 0 # test with bad address with pytest.raises(SystemExit) as cm: with PodFactory.build_pod(a1): NetworkChecker(a3) assert cm.value.code == 1
""" This is a simple application for sparse encoder: Computing embeddings. we have multiple sentences and we want to compute their embeddings. The embeddings are sparse, meaning that most of the values are zero. The embeddings are stored in a sparse matrix format, which is more efficient for storage and computation. we can also visualize the top tokens for each text.""" from sentence_transformers import SparseEncoder # Initialize the SPLADE model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Embed a list of sentences sentences = [ "This framework generates embeddings for each input sentence", "Sentences are passed as a list of string.", "The quick brown fox jumps over the lazy dog.", ] # Generate embeddings embeddings = model.encode(sentences) # Print embedding sim and sparsity print(f"Embedding dim: {model.get_sentence_embedding_dimension()}") stats = model.get_sparsity_stats(embeddings) print(f"Embedding sparsity: {stats}") print(f"Average non-zero dimensions: {stats['row_non_zero_mean']:.2f}") print(f"Sparsity percentage: {stats['row_sparsity_mean']:.2%}") """ Embedding dim: 30522 Embedding sparsity: {'num_rows': 3, 'num_cols': 30522, 'row_non_zero_mean': 56.66666793823242, 'row_sparsity_mean': 0.9981433749198914} Average non-zero dimensions: 56.67 Sparsity percentage: 99.81% """ # Visualize top tokens for each text top_k = 10 token_weights = model.decode(embeddings, top_k=top_k) print(f"\nTop tokens {top_k} for each text:") # The result is a list of sentence embeddings as numpy arrays for i, sentence in enumerate(sentences): token_scores = ", ".join([f'("{token.strip()}", {value:.2f})' for token, value in token_weights[i]]) print(f"{i}: {sentence} -> Top tokens: {token_scores}") """ Top tokens 10 for each text: 0: This framework generates embeddings for each input sentence -> Top tokens: ("framework", 2.19), ("##bed", 2.12), ("input", 1.99), ("each", 1.60), ("em", 1.58), ("sentence", 1.49), ("generate", 1.42), ("##ding", 1.33), ("sentences", 1.10), ("create", 0.93) 1: Sentences are passed as a list of string. -> Top tokens: ("string", 2.72), ("pass", 2.24), ("sentences", 2.15), ("passed", 2.07), ("sentence", 1.90), ("strings", 1.86), ("list", 1.84), ("lists", 1.49), ("as", 1.18), ("passing", 0.73) 2: The quick brown fox jumps over the lazy dog. -> Top tokens: ("lazy", 2.18), ("fox", 1.67), ("brown", 1.56), ("over", 1.52), ("dog", 1.50), ("quick", 1.49), ("jump", 1.39), ("dogs", 1.25), ("foxes", 0.99), ("jumping", 0.84) """ # Example of using max_active_dims during encoding print("\n--- Using max_active_dims during encoding ---") # Generate embeddings with limited active dimensions embeddings_limited = model.encode(sentences, max_active_dims=32) stats_limited = model.get_sparsity_stats(embeddings_limited) print(f"Limited embedding sparsity: {stats_limited}") print(f"Average non-zero dimensions: {stats_limited['row_non_zero_mean']:.2f}") print(f"Sparsity percentage: {stats_limited['row_sparsity_mean']:.2%}") """ --- Using max_active_dims during encoding --- Limited embedding sparsity: {'num_rows': 3, 'num_cols': 30522, 'row_non_zero_mean': 32.0, 'row_sparsity_mean': 0.9989516139030457} Average non-zero dimensions: 32.00 Sparsity percentage: 99.90% """ # Comparing memory usage print("\n--- Comparing memory usage ---") def get_memory_size(tensor): if tensor.is_sparse: # For sparse tensors, only count non-zero elements return ( tensor._values().element_size() * tensor._values().nelement() + tensor._indices().element_size() * tensor._indices().nelement() ) else: return tensor.element_size() * tensor.nelement() print(f"Original embeddings memory: {get_memory_size(embeddings) / 1024:.2f} KB") print(f"Embeddings with max_active_dims=32 memory: {get_memory_size(embeddings_limited) / 1024:.2f} KB") """ --- Comparing memory usage --- Original embeddings memory: 3.32 KB Embeddings with max_active_dims=32 memory: 1.88 KB """
""" This is a simple application for sparse encoder: Computing embeddings. we have multiple sentences and we want to compute their embeddings. The embeddings are sparse, meaning that most of the values are zero. The embeddings are stored in a sparse matrix format, which is more efficient for storage and computation. we can also visualize the top tokens for each text.""" from sentence_transformers import SparseEncoder # Initialize the SPLADE model model = SparseEncoder("naver/splade-cocondenser-ensembledistil") # Embed a list of sentences sentences = [ "This framework generates embeddings for each input sentence", "Sentences are passed as a list of string.", "The quick brown fox jumps over the lazy dog.", ] # Generate embeddings embeddings = model.encode(sentences) # Print embedding sim and sparsity print(f"Embedding dim: {model.get_sentence_embedding_dimension()}") print(f"Embedding sparsity: {model.get_sparsity_stats(embeddings)}") """ Embedding dim: 30522 Embedding sparsity: {'num_rows': 3, 'num_cols': 30522, 'row_non_zero_mean': 56.66666793823242, 'row_sparsity_mean': 0.9981433749198914} """ # Visualize top tokens for each text top_k = 10 token_weights = model.decode(embeddings, top_k=top_k) print(f"\nTop tokens {top_k} for each text:") # The result is a list of sentence embeddings as numpy arrays for i, sentence in enumerate(sentences): token_scores = ", ".join([f'("{token.strip()}", {value:.2f})' for token, value in token_weights[i]]) print(f"{i}: {sentence} -> Top tokens: {token_scores}") """ Top tokens 10 for each text: 0: This framework generates embeddings for each input sentence -> Top tokens: ("framework", 2.19), ("##bed", 2.12), ("input", 1.99), ("each", 1.60), ("em", 1.58), ("sentence", 1.49), ("generate", 1.42), ("##ding", 1.33), ("sentences", 1.10), ("create", 0.93) 1: Sentences are passed as a list of string. -> Top tokens: ("string", 2.72), ("pass", 2.24), ("sentences", 2.15), ("passed", 2.07), ("sentence", 1.90), ("strings", 1.86), ("list", 1.84), ("lists", 1.49), ("as", 1.18), ("passing", 0.73) 2: The quick brown fox jumps over the lazy dog. -> Top tokens: ("lazy", 2.18), ("fox", 1.67), ("brown", 1.56), ("over", 1.52), ("dog", 1.50), ("quick", 1.49), ("jump", 1.39), ("dogs", 1.25), ("foxes", 0.99), ("jumping", 0.84) """
from langchain_core.prompts.prompt import PromptTemplate _PROMPT_TEMPLATE = """ You are an agents controlling a browser. You are given: (1) an objective that you are trying to achieve (2) the URL of your current web page (3) a simplified text description of what's visible in the browser window (more on that below) You can issue these commands: SCROLL UP - scroll up one page SCROLL DOWN - scroll down one page CLICK X - click on a given element. You can only click on links, buttons, and inputs! TYPE X "TEXT" - type the specified text into the input with id X TYPESUBMIT X "TEXT" - same as TYPE above, except then it presses ENTER to submit the form The format of the browser content is highly simplified; all formatting elements are stripped. Interactive elements such as links, inputs, buttons are represented like this: <link id=1>text</link> <button id=2>text</button> <input id=3>text</input> Images are rendered as their alt text like this: <img id=4 alt=""/> Based on your given objective, issue whatever command you believe will get you closest to achieving your goal. You always start on Google; you should submit a search query to Google that will take you to the best page for achieving your objective. And then interact with that page to achieve your objective. If you find yourself on Google and there are no search results displayed yet, you should probably issue a command like "TYPESUBMIT 7 "search query"" to get to a more useful page. Then, if you find yourself on a Google search results page, you might issue the command "CLICK 24" to click on the first link in the search results. (If your previous command was a TYPESUBMIT your next command should probably be a CLICK.) Don't try to interact with elements that you can't see. Here are some examples: EXAMPLE 1: ================================================== CURRENT BROWSER CONTENT: ------------------ <link id=1>About</link> <link id=2>Store</link> <link id=3>Gmail</link> <link id=4>Images</link> <link id=5>(Google apps)</link> <link id=6>Sign in</link> <img id=7 alt="(Google)"/> <input id=8 alt="Search"></input> <button id=9>(Search by voice)</button> <button id=10>(Google Search)</button> <button id=11>(I'm Feeling Lucky)</button> <link id=12>Advertising</link> <link id=13>Business</link> <link id=14>How Search works</link> <link id=15>Carbon neutral since 2007</link> <link id=16>Privacy</link> <link id=17>Terms</link> <text id=18>Settings</text> ------------------ OBJECTIVE: Find a 2 bedroom house for sale in Anchorage AK for under $750k CURRENT URL: https://www.google.com/ YOUR COMMAND: TYPESUBMIT 8 "anchorage redfin" ================================================== EXAMPLE 2: ================================================== CURRENT BROWSER CONTENT: ------------------ <link id=1>About</link> <link id=2>Store</link> <link id=3>Gmail</link> <link id=4>Images</link> <link id=5>(Google apps)</link> <link id=6>Sign in</link> <img id=7 alt="(Google)"/> <input id=8 alt="Search"></input> <button id=9>(Search by voice)</button> <button id=10>(Google Search)</button> <button id=11>(I'm Feeling Lucky)</button> <link id=12>Advertising</link> <link id=13>Business</link> <link id=14>How Search works</link> <link id=15>Carbon neutral since 2007</link> <link id=16>Privacy</link> <link id=17>Terms</link> <text id=18>Settings</text> ------------------ OBJECTIVE: Make a reservation for 4 at Dorsia at 8pm CURRENT URL: https://www.google.com/ YOUR COMMAND: TYPESUBMIT 8 "dorsia nyc opentable" ================================================== EXAMPLE 3: ================================================== CURRENT BROWSER CONTENT: ------------------ <button id=1>For Businesses</button> <button id=2>Mobile</button> <button id=3>Help</button> <button id=4 alt="Language Picker">EN</button> <link id=5>OpenTable logo</link> <button id=6 alt ="search">Search</button> <text id=7>Find your table for any occasion</text> <button id=8>(Date selector)</button> <text id=9>Sep 28, 2022</text> <text id=10>7:00 PM</text> <text id=11>2 people</text> <input id=12 alt="Location, Restaurant, or Cuisine"></input> <button id=13>Let's go</button> <text id=14>It looks like you're in Peninsula. Not correct?</text> <button id=15>Get current location</button> <button id=16>Next</button> ------------------ OBJECTIVE: Make a reservation for 4 for dinner at Dorsia in New York City at 8pm CURRENT URL: https://www.opentable.com/ YOUR COMMAND: TYPESUBMIT 12 "dorsia new york city" ================================================== The current browser content, objective, and current URL follow. Reply with your next command to the browser. CURRENT BROWSER CONTENT: ------------------ {browser_content} ------------------ OBJECTIVE: {objective} CURRENT URL: {url} PREVIOUS COMMAND: {previous_command} YOUR COMMAND: """ # noqa: E501 PROMPT = PromptTemplate( input_variables=["browser_content", "url", "previous_command", "objective"], template=_PROMPT_TEMPLATE, )
# flake8: noqa from langchain_core.prompts.prompt import PromptTemplate _PROMPT_TEMPLATE = """ You are an agents controlling a browser. You are given: (1) an objective that you are trying to achieve (2) the URL of your current web page (3) a simplified text description of what's visible in the browser window (more on that below) You can issue these commands: SCROLL UP - scroll up one page SCROLL DOWN - scroll down one page CLICK X - click on a given element. You can only click on links, buttons, and inputs! TYPE X "TEXT" - type the specified text into the input with id X TYPESUBMIT X "TEXT" - same as TYPE above, except then it presses ENTER to submit the form The format of the browser content is highly simplified; all formatting elements are stripped. Interactive elements such as links, inputs, buttons are represented like this: <link id=1>text</link> <button id=2>text</button> <input id=3>text</input> Images are rendered as their alt text like this: <img id=4 alt=""/> Based on your given objective, issue whatever command you believe will get you closest to achieving your goal. You always start on Google; you should submit a search query to Google that will take you to the best page for achieving your objective. And then interact with that page to achieve your objective. If you find yourself on Google and there are no search results displayed yet, you should probably issue a command like "TYPESUBMIT 7 "search query"" to get to a more useful page. Then, if you find yourself on a Google search results page, you might issue the command "CLICK 24" to click on the first link in the search results. (If your previous command was a TYPESUBMIT your next command should probably be a CLICK.) Don't try to interact with elements that you can't see. Here are some examples: EXAMPLE 1: ================================================== CURRENT BROWSER CONTENT: ------------------ <link id=1>About</link> <link id=2>Store</link> <link id=3>Gmail</link> <link id=4>Images</link> <link id=5>(Google apps)</link> <link id=6>Sign in</link> <img id=7 alt="(Google)"/> <input id=8 alt="Search"></input> <button id=9>(Search by voice)</button> <button id=10>(Google Search)</button> <button id=11>(I'm Feeling Lucky)</button> <link id=12>Advertising</link> <link id=13>Business</link> <link id=14>How Search works</link> <link id=15>Carbon neutral since 2007</link> <link id=16>Privacy</link> <link id=17>Terms</link> <text id=18>Settings</text> ------------------ OBJECTIVE: Find a 2 bedroom house for sale in Anchorage AK for under $750k CURRENT URL: https://www.google.com/ YOUR COMMAND: TYPESUBMIT 8 "anchorage redfin" ================================================== EXAMPLE 2: ================================================== CURRENT BROWSER CONTENT: ------------------ <link id=1>About</link> <link id=2>Store</link> <link id=3>Gmail</link> <link id=4>Images</link> <link id=5>(Google apps)</link> <link id=6>Sign in</link> <img id=7 alt="(Google)"/> <input id=8 alt="Search"></input> <button id=9>(Search by voice)</button> <button id=10>(Google Search)</button> <button id=11>(I'm Feeling Lucky)</button> <link id=12>Advertising</link> <link id=13>Business</link> <link id=14>How Search works</link> <link id=15>Carbon neutral since 2007</link> <link id=16>Privacy</link> <link id=17>Terms</link> <text id=18>Settings</text> ------------------ OBJECTIVE: Make a reservation for 4 at Dorsia at 8pm CURRENT URL: https://www.google.com/ YOUR COMMAND: TYPESUBMIT 8 "dorsia nyc opentable" ================================================== EXAMPLE 3: ================================================== CURRENT BROWSER CONTENT: ------------------ <button id=1>For Businesses</button> <button id=2>Mobile</button> <button id=3>Help</button> <button id=4 alt="Language Picker">EN</button> <link id=5>OpenTable logo</link> <button id=6 alt ="search">Search</button> <text id=7>Find your table for any occasion</text> <button id=8>(Date selector)</button> <text id=9>Sep 28, 2022</text> <text id=10>7:00 PM</text> <text id=11>2 people</text> <input id=12 alt="Location, Restaurant, or Cuisine"></input> <button id=13>Let’s go</button> <text id=14>It looks like you're in Peninsula. Not correct?</text> <button id=15>Get current location</button> <button id=16>Next</button> ------------------ OBJECTIVE: Make a reservation for 4 for dinner at Dorsia in New York City at 8pm CURRENT URL: https://www.opentable.com/ YOUR COMMAND: TYPESUBMIT 12 "dorsia new york city" ================================================== The current browser content, objective, and current URL follow. Reply with your next command to the browser. CURRENT BROWSER CONTENT: ------------------ {browser_content} ------------------ OBJECTIVE: {objective} CURRENT URL: {url} PREVIOUS COMMAND: {previous_command} YOUR COMMAND: """ PROMPT = PromptTemplate( input_variables=["browser_content", "url", "previous_command", "objective"], template=_PROMPT_TEMPLATE, )
import pytest from llama_index.core.base.llms.types import ( ChatMessage, ChatResponse, CompletionResponse, ) from llama_index.llms.nvidia import NVIDIA @pytest.mark.integration def test_chat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") response = NVIDIA(model=chat_model, **mode).chat([message]) assert isinstance(response, ChatResponse) assert isinstance(response.message, ChatMessage) assert isinstance(response.message.content, str) @pytest.mark.integration def test_complete(chat_model: str, mode: dict) -> None: response = NVIDIA(model=chat_model, **mode).complete("Hello") assert isinstance(response, CompletionResponse) assert isinstance(response.text, str) @pytest.mark.integration def test_stream_chat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") gen = NVIDIA(model=chat_model, **mode).stream_chat([message]) assert all(isinstance(response, ChatResponse) for response in gen) assert all(isinstance(response.delta, str) for response in gen) @pytest.mark.integration def test_stream_complete(chat_model: str, mode: dict) -> None: gen = NVIDIA(model=chat_model, **mode).stream_complete("Hello") assert all(isinstance(response, CompletionResponse) for response in gen) assert all(isinstance(response.delta, str) for response in gen) @pytest.mark.integration @pytest.mark.asyncio async def test_achat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") response = await NVIDIA(model=chat_model, **mode).achat([message]) assert isinstance(response, ChatResponse) assert isinstance(response.message, ChatMessage) assert isinstance(response.message.content, str) @pytest.mark.integration @pytest.mark.asyncio async def test_acomplete(chat_model: str, mode: dict) -> None: response = await NVIDIA(model=chat_model, **mode).acomplete("Hello") assert isinstance(response, CompletionResponse) assert isinstance(response.text, str) @pytest.mark.integration @pytest.mark.asyncio async def test_astream_chat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") gen = await NVIDIA(model=chat_model, **mode).astream_chat([message]) responses = [response async for response in gen] assert all(isinstance(response, ChatResponse) for response in responses) assert all(isinstance(response.delta, str) for response in responses) @pytest.mark.integration @pytest.mark.asyncio async def test_astream_complete(chat_model: str, mode: dict) -> None: gen = await NVIDIA(model=chat_model, **mode).astream_complete("Hello") responses = [response async for response in gen] assert all(isinstance(response, CompletionResponse) for response in responses) assert all(isinstance(response.delta, str) for response in responses) @pytest.mark.integration @pytest.mark.parametrize( "excluded", [ "mistralai/mixtral-8x22b-v0.1", # not a /chat/completion endpoint ], ) def test_exclude_models(mode: dict, excluded: str) -> None: assert excluded not in [model.id for model in NVIDIA(**mode).available_models]
import pytest from llama_index.core.base.llms.types import ( ChatMessage, ChatResponse, CompletionResponse, ) from llama_index.llms.nvidia import NVIDIA @pytest.mark.integration() def test_chat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") response = NVIDIA(model=chat_model, **mode).chat([message]) assert isinstance(response, ChatResponse) assert isinstance(response.message, ChatMessage) assert isinstance(response.message.content, str) @pytest.mark.integration() def test_complete(chat_model: str, mode: dict) -> None: response = NVIDIA(model=chat_model, **mode).complete("Hello") assert isinstance(response, CompletionResponse) assert isinstance(response.text, str) @pytest.mark.integration() def test_stream_chat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") gen = NVIDIA(model=chat_model, **mode).stream_chat([message]) assert all(isinstance(response, ChatResponse) for response in gen) assert all(isinstance(response.delta, str) for response in gen) @pytest.mark.integration() def test_stream_complete(chat_model: str, mode: dict) -> None: gen = NVIDIA(model=chat_model, **mode).stream_complete("Hello") assert all(isinstance(response, CompletionResponse) for response in gen) assert all(isinstance(response.delta, str) for response in gen) @pytest.mark.integration() @pytest.mark.asyncio() async def test_achat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") response = await NVIDIA(model=chat_model, **mode).achat([message]) assert isinstance(response, ChatResponse) assert isinstance(response.message, ChatMessage) assert isinstance(response.message.content, str) @pytest.mark.integration() @pytest.mark.asyncio() async def test_acomplete(chat_model: str, mode: dict) -> None: response = await NVIDIA(model=chat_model, **mode).acomplete("Hello") assert isinstance(response, CompletionResponse) assert isinstance(response.text, str) @pytest.mark.integration() @pytest.mark.asyncio() async def test_astream_chat(chat_model: str, mode: dict) -> None: message = ChatMessage(content="Hello") gen = await NVIDIA(model=chat_model, **mode).astream_chat([message]) responses = [response async for response in gen] assert all(isinstance(response, ChatResponse) for response in responses) assert all(isinstance(response.delta, str) for response in responses) @pytest.mark.integration() @pytest.mark.asyncio() async def test_astream_complete(chat_model: str, mode: dict) -> None: gen = await NVIDIA(model=chat_model, **mode).astream_complete("Hello") responses = [response async for response in gen] assert all(isinstance(response, CompletionResponse) for response in responses) assert all(isinstance(response.delta, str) for response in responses) @pytest.mark.integration() @pytest.mark.parametrize( "excluded", [ "mistralai/mixtral-8x22b-v0.1", # not a /chat/completion endpoint ], ) def test_exclude_models(mode: dict, excluded: str) -> None: assert excluded not in [model.id for model in NVIDIA(**mode).available_models]
# Copyright 2024 The HuggingFace Inc. team. All rights reserved. # # 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 os from packaging import version from .. import __version__ from .constants import ( CONFIG_NAME, DEPRECATED_REVISION_ARGS, DIFFUSERS_DYNAMIC_MODULE_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MIN_PEFT_VERSION, ONNX_EXTERNAL_WEIGHTS_NAME, ONNX_WEIGHTS_NAME, SAFE_WEIGHTS_INDEX_NAME, SAFETENSORS_FILE_EXTENSION, SAFETENSORS_WEIGHTS_NAME, USE_PEFT_BACKEND, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ) from .deprecation_utils import deprecate from .doc_utils import replace_example_docstring from .dynamic_modules_utils import get_class_from_dynamic_module from .export_utils import export_to_gif, export_to_obj, export_to_ply, export_to_video from .hub_utils import ( PushToHubMixin, _add_variant, _get_checkpoint_shard_files, _get_model_file, extract_commit_hash, http_user_agent, ) from .import_utils import ( BACKENDS_MAPPING, DIFFUSERS_SLOW_IMPORT, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, get_objects_from_module, is_accelerate_available, is_accelerate_version, is_bitsandbytes_available, is_bitsandbytes_version, is_bs4_available, is_flax_available, is_ftfy_available, is_google_colab, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_matplotlib_available, is_note_seq_available, is_onnx_available, is_peft_available, is_peft_version, is_safetensors_available, is_scipy_available, is_sentencepiece_available, is_tensorboard_available, is_timm_available, is_torch_available, is_torch_npu_available, is_torch_version, is_torch_xla_available, is_torch_xla_version, is_torchsde_available, is_torchvision_available, is_transformers_available, is_transformers_version, is_unidecode_available, is_wandb_available, is_xformers_available, requires_backends, ) from .loading_utils import get_module_from_name, load_image, load_video from .logging import get_logger from .outputs import BaseOutput from .peft_utils import ( check_peft_version, delete_adapter_layers, get_adapter_name, get_peft_kwargs, recurse_remove_peft_layers, scale_lora_layers, set_adapter_layers, set_weights_and_activate_adapters, unscale_lora_layers, ) from .pil_utils import PIL_INTERPOLATION, make_image_grid, numpy_to_pil, pt_to_pil from .state_dict_utils import ( convert_all_state_dict_to_peft, convert_state_dict_to_diffusers, convert_state_dict_to_kohya, convert_state_dict_to_peft, convert_unet_state_dict_to_peft, ) logger = get_logger(__name__) def check_min_version(min_version): if version.parse(__version__) < version.parse(min_version): if "dev" in min_version: error_message = ( "This example requires a source install from HuggingFace diffusers (see " "`https://huggingface.co/docs/diffusers/installation#install-from-source`)," ) else: error_message = f"This example requires a minimum version of {min_version}," error_message += f" but the version found is {__version__}.\n" raise ImportError(error_message)
# Copyright 2024 The HuggingFace Inc. team. All rights reserved. # # 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 os from packaging import version from .. import __version__ from .constants import ( CONFIG_NAME, DEPRECATED_REVISION_ARGS, DIFFUSERS_DYNAMIC_MODULE_NAME, FLAX_WEIGHTS_NAME, HF_MODULES_CACHE, HUGGINGFACE_CO_RESOLVE_ENDPOINT, MIN_PEFT_VERSION, ONNX_EXTERNAL_WEIGHTS_NAME, ONNX_WEIGHTS_NAME, SAFE_WEIGHTS_INDEX_NAME, SAFETENSORS_FILE_EXTENSION, SAFETENSORS_WEIGHTS_NAME, USE_PEFT_BACKEND, WEIGHTS_INDEX_NAME, WEIGHTS_NAME, ) from .deprecation_utils import deprecate from .doc_utils import replace_example_docstring from .dynamic_modules_utils import get_class_from_dynamic_module from .export_utils import export_to_gif, export_to_obj, export_to_ply, export_to_video from .hub_utils import ( PushToHubMixin, _add_variant, _get_checkpoint_shard_files, _get_model_file, extract_commit_hash, http_user_agent, ) from .import_utils import ( BACKENDS_MAPPING, DIFFUSERS_SLOW_IMPORT, ENV_VARS_TRUE_AND_AUTO_VALUES, ENV_VARS_TRUE_VALUES, USE_JAX, USE_TF, USE_TORCH, DummyObject, OptionalDependencyNotAvailable, _LazyModule, get_objects_from_module, is_accelerate_available, is_accelerate_version, is_bitsandbytes_available, is_bitsandbytes_version, is_bs4_available, is_flax_available, is_ftfy_available, is_google_colab, is_inflect_available, is_invisible_watermark_available, is_k_diffusion_available, is_k_diffusion_version, is_librosa_available, is_matplotlib_available, is_note_seq_available, is_onnx_available, is_peft_available, is_peft_version, is_safetensors_available, is_scipy_available, is_sentencepiece_available, is_tensorboard_available, is_timm_available, is_torch_available, is_torch_npu_available, is_torch_version, is_torch_xla_available, is_torchsde_available, is_torchvision_available, is_transformers_available, is_transformers_version, is_unidecode_available, is_wandb_available, is_xformers_available, requires_backends, ) from .loading_utils import get_module_from_name, load_image, load_video from .logging import get_logger from .outputs import BaseOutput from .peft_utils import ( check_peft_version, delete_adapter_layers, get_adapter_name, get_peft_kwargs, recurse_remove_peft_layers, scale_lora_layers, set_adapter_layers, set_weights_and_activate_adapters, unscale_lora_layers, ) from .pil_utils import PIL_INTERPOLATION, make_image_grid, numpy_to_pil, pt_to_pil from .state_dict_utils import ( convert_all_state_dict_to_peft, convert_state_dict_to_diffusers, convert_state_dict_to_kohya, convert_state_dict_to_peft, convert_unet_state_dict_to_peft, ) logger = get_logger(__name__) def check_min_version(min_version): if version.parse(__version__) < version.parse(min_version): if "dev" in min_version: error_message = ( "This example requires a source install from HuggingFace diffusers (see " "`https://huggingface.co/docs/diffusers/installation#install-from-source`)," ) else: error_message = f"This example requires a minimum version of {min_version}," error_message += f" but the version found is {__version__}.\n" raise ImportError(error_message)
# TODO: enable ruff qa on this file when we figure out why it thinks weaviate_client is # redefined at each test that fixture # ruff: noqa import numpy as np import pytest import torch from pydantic import Field from docarray import BaseDoc from docarray.index.backends.weaviate import WeaviateDocumentIndex from docarray.typing import NdArray, TorchTensor from tests.index.weaviate.fixture_weaviate import ( # noqa: F401 start_storage, weaviate_client, ) pytestmark = [pytest.mark.slow, pytest.mark.index] def test_find_torch(weaviate_client): class TorchDoc(BaseDoc): tens: TorchTensor[10] = Field(dims=10, is_embedding=True) index = WeaviateDocumentIndex[TorchDoc]() index_docs = [ TorchDoc(tens=np.random.rand(10).astype(dtype=np.float32)) for _ in range(10) ] index.index(index_docs) query = index_docs[-1] docs, scores = index.find(query, limit=5) assert len(docs) == 5 assert len(scores) == 5 for doc in docs: assert isinstance(doc.tens, TorchTensor) assert docs[0].id == index_docs[-1].id assert torch.allclose(docs[0].tens, index_docs[-1].tens) @pytest.mark.tensorflow def test_find_tensorflow(): from docarray.typing import TensorFlowTensor class TfDoc(BaseDoc): tens: TensorFlowTensor[10] = Field(dims=10, is_embedding=True) index = WeaviateDocumentIndex[TfDoc]() index_docs = [ TfDoc(tens=np.random.rand(10).astype(dtype=np.float32)) for _ in range(10) ] index.index(index_docs) query = index_docs[-1] docs, scores = index.find(query, limit=5) assert len(docs) == 5 assert len(scores) == 5 for doc in docs: assert isinstance(doc.tens, TensorFlowTensor) assert docs[0].id == index_docs[-1].id assert np.allclose( docs[0].tens.unwrap().numpy(), index_docs[-1].tens.unwrap().numpy() ) def test_contain(): class SimpleDoc(BaseDoc): tens: NdArray[10] = Field(dims=1000) class SimpleSchema(BaseDoc): tens: NdArray[10] index = WeaviateDocumentIndex[SimpleSchema]() index_docs = [SimpleDoc(tens=np.zeros(10)) for _ in range(10)] assert (index_docs[0] in index) is False index.index(index_docs) for doc in index_docs: assert (doc in index) is True index_docs_new = [SimpleDoc(tens=np.zeros(10)) for _ in range(10)] for doc in index_docs_new: assert (doc in index) is False
# TODO: enable ruff qa on this file when we figure out why it thinks weaviate_client is # redefined at each test that fixture # ruff: noqa import numpy as np import pytest import torch from pydantic import Field from docarray import BaseDoc from docarray.index.backends.weaviate import WeaviateDocumentIndex from docarray.typing import TorchTensor from tests.index.weaviate.fixture_weaviate import ( # noqa: F401 start_storage, weaviate_client, ) pytestmark = [pytest.mark.slow, pytest.mark.index] def test_find_torch(weaviate_client): class TorchDoc(BaseDoc): tens: TorchTensor[10] = Field(dims=10, is_embedding=True) index = WeaviateDocumentIndex[TorchDoc]() index_docs = [ TorchDoc(tens=np.random.rand(10).astype(dtype=np.float32)) for _ in range(10) ] index.index(index_docs) query = index_docs[-1] docs, scores = index.find(query, limit=5) assert len(docs) == 5 assert len(scores) == 5 for doc in docs: assert isinstance(doc.tens, TorchTensor) assert docs[0].id == index_docs[-1].id assert torch.allclose(docs[0].tens, index_docs[-1].tens) @pytest.mark.tensorflow def test_find_tensorflow(): from docarray.typing import TensorFlowTensor class TfDoc(BaseDoc): tens: TensorFlowTensor[10] = Field(dims=10, is_embedding=True) index = WeaviateDocumentIndex[TfDoc]() index_docs = [ TfDoc(tens=np.random.rand(10).astype(dtype=np.float32)) for _ in range(10) ] index.index(index_docs) query = index_docs[-1] docs, scores = index.find(query, limit=5) assert len(docs) == 5 assert len(scores) == 5 for doc in docs: assert isinstance(doc.tens, TensorFlowTensor) assert docs[0].id == index_docs[-1].id assert np.allclose( docs[0].tens.unwrap().numpy(), index_docs[-1].tens.unwrap().numpy() )
import warnings from typing import Any, Dict, List, Union import numpy as np import PIL.Image import torch from torchvision.prototype import datapoints from torchvision.prototype.transforms import Transform from torchvision.transforms import functional as _F from typing_extensions import Literal from ._transform import _RandomApplyTransform from .utils import is_simple_tensor, query_chw class ToTensor(Transform): _transformed_types = (PIL.Image.Image, np.ndarray) def __init__(self) -> None: warnings.warn( "The transform `ToTensor()` is deprecated and will be removed in a future release. " "Instead, please use `transforms.Compose([transforms.ToImageTensor(), transforms.ConvertImageDtype()])`." ) super().__init__() def _transform(self, inpt: Union[PIL.Image.Image, np.ndarray], params: Dict[str, Any]) -> torch.Tensor: return _F.to_tensor(inpt) class Grayscale(Transform): _transformed_types = ( datapoints.Image, PIL.Image.Image, is_simple_tensor, datapoints.Video, ) def __init__(self, num_output_channels: Literal[1, 3] = 1) -> None: deprecation_msg = ( f"The transform `Grayscale(num_output_channels={num_output_channels})` " f"is deprecated and will be removed in a future release." ) if num_output_channels == 1: replacement_msg = ( "transforms.ConvertImageColorSpace(old_color_space=ColorSpace.RGB, color_space=ColorSpace.GRAY)" ) else: replacement_msg = ( "transforms.Compose(\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.RGB, color_space=ColorSpace.GRAY),\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.GRAY, color_space=ColorSpace.RGB),\n" ")" ) warnings.warn(f"{deprecation_msg} Instead, please use\n\n{replacement_msg}") super().__init__() self.num_output_channels = num_output_channels def _transform( self, inpt: Union[datapoints.ImageType, datapoints.VideoType], params: Dict[str, Any] ) -> Union[datapoints.ImageType, datapoints.VideoType]: output = _F.rgb_to_grayscale(inpt, num_output_channels=self.num_output_channels) if isinstance(inpt, (datapoints.Image, datapoints.Video)): output = inpt.wrap_like(inpt, output, color_space=datapoints.ColorSpace.GRAY) # type: ignore[arg-type] return output class RandomGrayscale(_RandomApplyTransform): _transformed_types = ( datapoints.Image, PIL.Image.Image, is_simple_tensor, datapoints.Video, ) def __init__(self, p: float = 0.1) -> None: warnings.warn( "The transform `RandomGrayscale(p=...)` is deprecated and will be removed in a future release. " "Instead, please use\n\n" "transforms.RandomApply(\n" " transforms.Compose(\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.RGB, color_space=ColorSpace.GRAY),\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.GRAY, color_space=ColorSpace.RGB),\n" " )\n" " p=...,\n" ")" ) super().__init__(p=p) def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]: num_input_channels, *_ = query_chw(flat_inputs) return dict(num_input_channels=num_input_channels) def _transform( self, inpt: Union[datapoints.ImageType, datapoints.VideoType], params: Dict[str, Any] ) -> Union[datapoints.ImageType, datapoints.VideoType]: output = _F.rgb_to_grayscale(inpt, num_output_channels=params["num_input_channels"]) if isinstance(inpt, (datapoints.Image, datapoints.Video)): output = inpt.wrap_like(inpt, output, color_space=datapoints.ColorSpace.GRAY) # type: ignore[arg-type] return output
import warnings from typing import Any, Dict, List, Union import numpy as np import PIL.Image import torch from torchvision.prototype import features from torchvision.prototype.transforms import Transform from torchvision.transforms import functional as _F from typing_extensions import Literal from ._transform import _RandomApplyTransform from .utils import query_chw class ToTensor(Transform): _transformed_types = (PIL.Image.Image, np.ndarray) def __init__(self) -> None: warnings.warn( "The transform `ToTensor()` is deprecated and will be removed in a future release. " "Instead, please use `transforms.Compose([transforms.ToImageTensor(), transforms.ConvertImageDtype()])`." ) super().__init__() def _transform(self, inpt: Union[PIL.Image.Image, np.ndarray], params: Dict[str, Any]) -> torch.Tensor: return _F.to_tensor(inpt) class Grayscale(Transform): _transformed_types = (features.Image, PIL.Image.Image, features.is_simple_tensor, features.Video) def __init__(self, num_output_channels: Literal[1, 3] = 1) -> None: deprecation_msg = ( f"The transform `Grayscale(num_output_channels={num_output_channels})` " f"is deprecated and will be removed in a future release." ) if num_output_channels == 1: replacement_msg = ( "transforms.ConvertImageColorSpace(old_color_space=ColorSpace.RGB, color_space=ColorSpace.GRAY)" ) else: replacement_msg = ( "transforms.Compose(\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.RGB, color_space=ColorSpace.GRAY),\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.GRAY, color_space=ColorSpace.RGB),\n" ")" ) warnings.warn(f"{deprecation_msg} Instead, please use\n\n{replacement_msg}") super().__init__() self.num_output_channels = num_output_channels def _transform( self, inpt: Union[features.ImageType, features.VideoType], params: Dict[str, Any] ) -> Union[features.ImageType, features.VideoType]: output = _F.rgb_to_grayscale(inpt, num_output_channels=self.num_output_channels) if isinstance(inpt, (features.Image, features.Video)): output = inpt.wrap_like(inpt, output, color_space=features.ColorSpace.GRAY) # type: ignore[arg-type] return output class RandomGrayscale(_RandomApplyTransform): _transformed_types = (features.Image, PIL.Image.Image, features.is_simple_tensor, features.Video) def __init__(self, p: float = 0.1) -> None: warnings.warn( "The transform `RandomGrayscale(p=...)` is deprecated and will be removed in a future release. " "Instead, please use\n\n" "transforms.RandomApply(\n" " transforms.Compose(\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.RGB, color_space=ColorSpace.GRAY),\n" " transforms.ConvertImageColorSpace(old_color_space=ColorSpace.GRAY, color_space=ColorSpace.RGB),\n" " )\n" " p=...,\n" ")" ) super().__init__(p=p) def _get_params(self, flat_inputs: List[Any]) -> Dict[str, Any]: num_input_channels, *_ = query_chw(flat_inputs) return dict(num_input_channels=num_input_channels) def _transform( self, inpt: Union[features.ImageType, features.VideoType], params: Dict[str, Any] ) -> Union[features.ImageType, features.VideoType]: output = _F.rgb_to_grayscale(inpt, num_output_channels=params["num_input_channels"]) if isinstance(inpt, (features.Image, features.Video)): output = inpt.wrap_like(inpt, output, color_space=features.ColorSpace.GRAY) # type: ignore[arg-type] return output
"""Test in memory docstore.""" from langchain.output_parsers.regex_dict import RegexDictParser DEF_EXPECTED_RESULT = {"action": "Search", "action_input": "How to use this class?"} DEF_OUTPUT_KEY_TO_FORMAT = {"action": "Action", "action_input": "Action Input"} DEF_README = """We have just received a new result from the LLM, and our next step is to filter and read its format using regular expressions to identify specific fields, such as: - Action: Search - Action Input: How to use this class? - Additional Fields: "N/A" To assist us in this task, we use the regex_dict class. This class allows us to send a dictionary containing an output key and the expected format, which in turn enables us to retrieve the result of the matching formats and extract specific information from it. To exclude irrelevant information from our return dictionary, we can instruct the LLM to use a specific command that notifies us when it doesn't know the answer. We call this variable the "no_update_value", and for our current case, we set it to "N/A". Therefore, we expect the result to only contain the following fields: { {key = action, value = search} {key = action_input, value = "How to use this class?"}. }""" def test_regex_dict_result() -> None: """Test regex dict result.""" regex_dict_parser = RegexDictParser( output_key_to_format=DEF_OUTPUT_KEY_TO_FORMAT, no_update_value="N/A", ) result_dict = regex_dict_parser.parse(DEF_README) print("parse_result:", result_dict) # noqa: T201 assert result_dict == DEF_EXPECTED_RESULT def test_regex_dict_output_type() -> None: """Test regex dict output type.""" regex_dict_parser = RegexDictParser( output_key_to_format=DEF_OUTPUT_KEY_TO_FORMAT, no_update_value="N/A", ) assert regex_dict_parser.OutputType == dict[str, str]
"""Test in memory docstore.""" from langchain.output_parsers.regex_dict import RegexDictParser DEF_EXPECTED_RESULT = {"action": "Search", "action_input": "How to use this class?"} DEF_OUTPUT_KEY_TO_FORMAT = {"action": "Action", "action_input": "Action Input"} DEF_README = """We have just received a new result from the LLM, and our next step is to filter and read its format using regular expressions to identify specific fields, such as: - Action: Search - Action Input: How to use this class? - Additional Fields: "N/A" To assist us in this task, we use the regex_dict class. This class allows us to send a dictionary containing an output key and the expected format, which in turn enables us to retrieve the result of the matching formats and extract specific information from it. To exclude irrelevant information from our return dictionary, we can instruct the LLM to use a specific command that notifies us when it doesn't know the answer. We call this variable the "no_update_value", and for our current case, we set it to "N/A". Therefore, we expect the result to only contain the following fields: { {key = action, value = search} {key = action_input, value = "How to use this class?"}. }""" def test_regex_dict_result() -> None: """Test regex dict result.""" regex_dict_parser = RegexDictParser( output_key_to_format=DEF_OUTPUT_KEY_TO_FORMAT, no_update_value="N/A" ) result_dict = regex_dict_parser.parse(DEF_README) print("parse_result:", result_dict) # noqa: T201 assert result_dict == DEF_EXPECTED_RESULT def test_regex_dict_output_type() -> None: """Test regex dict output type.""" regex_dict_parser = RegexDictParser( output_key_to_format=DEF_OUTPUT_KEY_TO_FORMAT, no_update_value="N/A" ) assert regex_dict_parser.OutputType == dict[str, str]
from collections.abc import Awaitable from typing import TYPE_CHECKING, Callable, Optional, Union from langchain_core.runnables.config import ( RunnableConfig, acall_func_with_variable_args, call_func_with_variable_args, ) from langchain_core.tracers.base import AsyncBaseTracer, BaseTracer from langchain_core.tracers.schemas import Run if TYPE_CHECKING: from uuid import UUID Listener = Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] AsyncListener = Union[ Callable[[Run], Awaitable[None]], Callable[[Run, RunnableConfig], Awaitable[None]] ] class RootListenersTracer(BaseTracer): """Tracer that calls listeners on run start, end, and error. Parameters: log_missing_parent: Whether to log a warning if the parent is missing. Default is False. config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error. """ log_missing_parent = False def __init__( self, *, config: RunnableConfig, on_start: Optional[Listener], on_end: Optional[Listener], on_error: Optional[Listener], ) -> None: """Initialize the tracer. Args: config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error """ super().__init__(_schema_format="original+chat") self.config = config self._arg_on_start = on_start self._arg_on_end = on_end self._arg_on_error = on_error self.root_id: Optional[UUID] = None def _persist_run(self, run: Run) -> None: # This is a legacy method only called once for an entire run tree # therefore not useful here pass def _on_run_create(self, run: Run) -> None: if self.root_id is not None: return self.root_id = run.id if self._arg_on_start is not None: call_func_with_variable_args(self._arg_on_start, run, self.config) def _on_run_update(self, run: Run) -> None: if run.id != self.root_id: return if run.error is None: if self._arg_on_end is not None: call_func_with_variable_args(self._arg_on_end, run, self.config) else: if self._arg_on_error is not None: call_func_with_variable_args(self._arg_on_error, run, self.config) class AsyncRootListenersTracer(AsyncBaseTracer): """Async Tracer that calls listeners on run start, end, and error. Parameters: log_missing_parent: Whether to log a warning if the parent is missing. Default is False. config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error. """ log_missing_parent = False def __init__( self, *, config: RunnableConfig, on_start: Optional[AsyncListener], on_end: Optional[AsyncListener], on_error: Optional[AsyncListener], ) -> None: """Initialize the tracer. Args: config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error """ super().__init__(_schema_format="original+chat") self.config = config self._arg_on_start = on_start self._arg_on_end = on_end self._arg_on_error = on_error self.root_id: Optional[UUID] = None async def _persist_run(self, run: Run) -> None: # This is a legacy method only called once for an entire run tree # therefore not useful here pass async def _on_run_create(self, run: Run) -> None: if self.root_id is not None: return self.root_id = run.id if self._arg_on_start is not None: await acall_func_with_variable_args(self._arg_on_start, run, self.config) async def _on_run_update(self, run: Run) -> None: if run.id != self.root_id: return if run.error is None: if self._arg_on_end is not None: await acall_func_with_variable_args(self._arg_on_end, run, self.config) else: if self._arg_on_error is not None: await acall_func_with_variable_args( self._arg_on_error, run, self.config )
from collections.abc import Awaitable from typing import Callable, Optional, Union from uuid import UUID from langchain_core.runnables.config import ( RunnableConfig, acall_func_with_variable_args, call_func_with_variable_args, ) from langchain_core.tracers.base import AsyncBaseTracer, BaseTracer from langchain_core.tracers.schemas import Run Listener = Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]] AsyncListener = Union[ Callable[[Run], Awaitable[None]], Callable[[Run, RunnableConfig], Awaitable[None]] ] class RootListenersTracer(BaseTracer): """Tracer that calls listeners on run start, end, and error. Parameters: log_missing_parent: Whether to log a warning if the parent is missing. Default is False. config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error. """ log_missing_parent = False def __init__( self, *, config: RunnableConfig, on_start: Optional[Listener], on_end: Optional[Listener], on_error: Optional[Listener], ) -> None: """Initialize the tracer. Args: config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error """ super().__init__(_schema_format="original+chat") self.config = config self._arg_on_start = on_start self._arg_on_end = on_end self._arg_on_error = on_error self.root_id: Optional[UUID] = None def _persist_run(self, run: Run) -> None: # This is a legacy method only called once for an entire run tree # therefore not useful here pass def _on_run_create(self, run: Run) -> None: if self.root_id is not None: return self.root_id = run.id if self._arg_on_start is not None: call_func_with_variable_args(self._arg_on_start, run, self.config) def _on_run_update(self, run: Run) -> None: if run.id != self.root_id: return if run.error is None: if self._arg_on_end is not None: call_func_with_variable_args(self._arg_on_end, run, self.config) else: if self._arg_on_error is not None: call_func_with_variable_args(self._arg_on_error, run, self.config) class AsyncRootListenersTracer(AsyncBaseTracer): """Async Tracer that calls listeners on run start, end, and error. Parameters: log_missing_parent: Whether to log a warning if the parent is missing. Default is False. config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error. """ log_missing_parent = False def __init__( self, *, config: RunnableConfig, on_start: Optional[AsyncListener], on_end: Optional[AsyncListener], on_error: Optional[AsyncListener], ) -> None: """Initialize the tracer. Args: config: The runnable config. on_start: The listener to call on run start. on_end: The listener to call on run end. on_error: The listener to call on run error """ super().__init__(_schema_format="original+chat") self.config = config self._arg_on_start = on_start self._arg_on_end = on_end self._arg_on_error = on_error self.root_id: Optional[UUID] = None async def _persist_run(self, run: Run) -> None: # This is a legacy method only called once for an entire run tree # therefore not useful here pass async def _on_run_create(self, run: Run) -> None: if self.root_id is not None: return self.root_id = run.id if self._arg_on_start is not None: await acall_func_with_variable_args(self._arg_on_start, run, self.config) async def _on_run_update(self, run: Run) -> None: if run.id != self.root_id: return if run.error is None: if self._arg_on_end is not None: await acall_func_with_variable_args(self._arg_on_end, run, self.config) else: if self._arg_on_error is not None: await acall_func_with_variable_args( self._arg_on_error, run, self.config )
import itertools import torch from parameterized import parameterized from torchaudio_unittest.common_utils import ( get_asset_path, skipIfNoCtcDecoder, TempDirMixin, TorchaudioTestCase, ) NUM_TOKENS = 8 @skipIfNoCtcDecoder class CTCDecoderTest(TempDirMixin, TorchaudioTestCase): def _get_decoder(self, tokens=None, use_lm=True, use_lexicon=True, **kwargs): from torchaudio.prototype.ctc_decoder import ctc_decoder if use_lexicon: lexicon_file = get_asset_path("decoder/lexicon.txt") kenlm_file = get_asset_path("decoder/kenlm.arpa") if use_lm else None else: lexicon_file = None kenlm_file = get_asset_path("decoder/kenlm_char.arpa") if use_lm else None if tokens is None: tokens = get_asset_path("decoder/tokens.txt") return ctc_decoder( lexicon=lexicon_file, tokens=tokens, lm=kenlm_file, **kwargs, ) def _get_emissions(self): B, T, N = 4, 15, NUM_TOKENS torch.manual_seed(0) emissions = torch.rand(B, T, N) return emissions @parameterized.expand( list( itertools.product( [get_asset_path("decoder/tokens.txt"), ["-", "|", "f", "o", "b", "a", "r"]], [True, False], [True, False], ) ), ) def test_construct_decoder(self, tokens, use_lm, use_lexicon): self._get_decoder(tokens=tokens, use_lm=use_lm, use_lexicon=use_lexicon) @parameterized.expand( [(True,), (False,)], ) def test_shape(self, use_lexicon): emissions = self._get_emissions() decoder = self._get_decoder(use_lexicon=use_lexicon) results = decoder(emissions) self.assertEqual(len(results), emissions.shape[0]) @parameterized.expand( [(True,), (False,)], ) def test_timesteps_shape(self, use_lexicon): """Each token should correspond with a timestep""" emissions = self._get_emissions() decoder = self._get_decoder(use_lexicon=use_lexicon) results = decoder(emissions) for i in range(emissions.shape[0]): result = results[i][0] self.assertEqual(result.tokens.shape, result.timesteps.shape) def test_no_lm_decoder(self): """Check that using no LM produces the same result as using an LM with 0 lm_weight""" kenlm_decoder = self._get_decoder(lm_weight=0) zerolm_decoder = self._get_decoder(use_lm=False) emissions = self._get_emissions() kenlm_results = kenlm_decoder(emissions) zerolm_results = zerolm_decoder(emissions) self.assertEqual(kenlm_results, zerolm_results) def test_get_timesteps(self): unprocessed_tokens = torch.tensor([2, 2, 0, 3, 3, 3, 0, 3]) decoder = self._get_decoder() timesteps = decoder._get_timesteps(unprocessed_tokens) expected = [0, 3, 7] self.assertEqual(timesteps, expected) def test_get_tokens_and_idxs(self): unprocessed_tokens = torch.tensor([2, 2, 0, 3, 3, 3, 0, 3]) # ["f", "f", "-", "o", "o", "o", "-", "o"] decoder = self._get_decoder() token_ids = decoder._get_tokens(unprocessed_tokens) tokens = decoder.idxs_to_tokens(token_ids) expected_ids = [2, 3, 3] self.assertEqual(token_ids, expected_ids) expected_tokens = ["f", "o", "o"] self.assertEqual(tokens, expected_tokens) @parameterized.expand([(get_asset_path("decoder/tokens.txt"),), (["-", "|", "f", "o", "b", "a", "r"],)]) def test_index_to_tokens(self, tokens): # decoder tokens: '-' '|' 'f' 'o' 'b' 'a' 'r' decoder = self._get_decoder(tokens) idxs = torch.LongTensor((1, 2, 1, 3, 5)) tokens = decoder.idxs_to_tokens(idxs) expected_tokens = ["|", "f", "|", "o", "a"] self.assertEqual(tokens, expected_tokens)
import itertools import torch from parameterized import parameterized from torchaudio_unittest.common_utils import ( TempDirMixin, TorchaudioTestCase, get_asset_path, skipIfNoCtcDecoder, ) NUM_TOKENS = 8 @skipIfNoCtcDecoder class CTCDecoderTest(TempDirMixin, TorchaudioTestCase): def _get_decoder(self, tokens=None, use_lm=True, use_lexicon=True, **kwargs): from torchaudio.prototype.ctc_decoder import ctc_decoder if use_lexicon: lexicon_file = get_asset_path("decoder/lexicon.txt") kenlm_file = get_asset_path("decoder/kenlm.arpa") if use_lm else None else: lexicon_file = None kenlm_file = get_asset_path("decoder/kenlm_char.arpa") if use_lm else None if tokens is None: tokens = get_asset_path("decoder/tokens.txt") return ctc_decoder( lexicon=lexicon_file, tokens=tokens, lm=kenlm_file, **kwargs, ) def _get_emissions(self): B, T, N = 4, 15, NUM_TOKENS torch.manual_seed(0) emissions = torch.rand(B, T, N) return emissions @parameterized.expand( list( itertools.product( [get_asset_path("decoder/tokens.txt"), ["-", "|", "f", "o", "b", "a", "r"]], [True, False], [True, False], ) ), ) def test_construct_decoder(self, tokens, use_lm, use_lexicon): self._get_decoder(tokens=tokens, use_lm=use_lm, use_lexicon=use_lexicon) @parameterized.expand( [(True,), (False,)], ) def test_shape(self, use_lexicon): emissions = self._get_emissions() decoder = self._get_decoder(use_lexicon=use_lexicon) results = decoder(emissions) self.assertEqual(len(results), emissions.shape[0]) @parameterized.expand( [(True,), (False,)], ) def test_timesteps_shape(self, use_lexicon): """Each token should correspond with a timestep""" emissions = self._get_emissions() decoder = self._get_decoder(use_lexicon=use_lexicon) results = decoder(emissions) for i in range(emissions.shape[0]): result = results[i][0] self.assertEqual(result.tokens.shape, result.timesteps.shape) def test_no_lm_decoder(self): """Check that using no LM produces the same result as using an LM with 0 lm_weight""" kenlm_decoder = self._get_decoder(lm_weight=0) zerolm_decoder = self._get_decoder(use_lm=False) emissions = self._get_emissions() kenlm_results = kenlm_decoder(emissions) zerolm_results = zerolm_decoder(emissions) self.assertEqual(kenlm_results, zerolm_results) def test_get_timesteps(self): unprocessed_tokens = torch.tensor([2, 2, 0, 3, 3, 3, 0, 3]) decoder = self._get_decoder() timesteps = decoder._get_timesteps(unprocessed_tokens) expected = [0, 3, 7] self.assertEqual(timesteps, expected) def test_get_tokens_and_idxs(self): unprocessed_tokens = torch.tensor([2, 2, 0, 3, 3, 3, 0, 3]) # ["f", "f", "-", "o", "o", "o", "-", "o"] decoder = self._get_decoder() token_ids = decoder._get_tokens(unprocessed_tokens) tokens = decoder.idxs_to_tokens(token_ids) expected_ids = [2, 3, 3] self.assertEqual(token_ids, expected_ids) expected_tokens = ["f", "o", "o"] self.assertEqual(tokens, expected_tokens) @parameterized.expand([(get_asset_path("decoder/tokens.txt"),), (["-", "|", "f", "o", "b", "a", "r"],)]) def test_index_to_tokens(self, tokens): # decoder tokens: '-' '|' 'f' 'o' 'b' 'a' 'r' decoder = self._get_decoder(tokens) idxs = torch.LongTensor((1, 2, 1, 3, 5)) tokens = decoder.idxs_to_tokens(idxs) expected_tokens = ["|", "f", "|", "o", "a"] self.assertEqual(tokens, expected_tokens)
# coding: utf-8 """Find the path to LightGBM dynamic library files.""" from pathlib import Path from platform import system from typing import List __all__: List[str] = [] def find_lib_path() -> List[str]: """Find the path to LightGBM library files. Returns ------- lib_path: list of str List of all found library paths to LightGBM. """ curr_path = Path(__file__).absolute().parent dll_path = [curr_path, curr_path.parents[1], curr_path / 'compile', curr_path.parent / 'compile', curr_path.parents[1] / 'lib'] if system() in ('Windows', 'Microsoft'): dll_path.append(curr_path.parent / 'compile' / 'Release') dll_path.append(curr_path.parent / 'compile' / 'windows' / 'x64' / 'DLL') dll_path.append(curr_path.parents[1] / 'Release') dll_path.append(curr_path.parents[1] / 'windows' / 'x64' / 'DLL') dll_path = [p / 'lib_lightgbm.dll' for p in dll_path] else: dll_path = [p / 'lib_lightgbm.so' for p in dll_path] lib_path = [str(p) for p in dll_path if p.is_file()] if not lib_path: dll_path_joined = '\n'.join(map(str, dll_path)) raise Exception(f'Cannot find lightgbm library file in following paths:\n{dll_path_joined}') return lib_path
# coding: utf-8 """Find the path to LightGBM dynamic library files.""" from pathlib import Path from platform import system from typing import List __all__ = [] def find_lib_path() -> List[str]: """Find the path to LightGBM library files. Returns ------- lib_path: list of str List of all found library paths to LightGBM. """ curr_path = Path(__file__).absolute().parent dll_path = [curr_path, curr_path.parents[1], curr_path / 'compile', curr_path.parent / 'compile', curr_path.parents[1] / 'lib'] if system() in ('Windows', 'Microsoft'): dll_path.append(curr_path.parent / 'compile' / 'Release') dll_path.append(curr_path.parent / 'compile' / 'windows' / 'x64' / 'DLL') dll_path.append(curr_path.parents[1] / 'Release') dll_path.append(curr_path.parents[1] / 'windows' / 'x64' / 'DLL') dll_path = [p / 'lib_lightgbm.dll' for p in dll_path] else: dll_path = [p / 'lib_lightgbm.so' for p in dll_path] lib_path = [str(p) for p in dll_path if p.is_file()] if not lib_path: dll_path_joined = '\n'.join(map(str, dll_path)) raise Exception(f'Cannot find lightgbm library file in following paths:\n{dll_path_joined}') return lib_path
"""Configure global settings and get information about the working environment.""" # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause # Machine learning module for Python # ================================== # # sklearn is a Python module integrating classical machine # learning algorithms in the tightly-knit world of scientific Python # packages (numpy, scipy, matplotlib). # # It aims to provide simple and efficient solutions to learning problems # that are accessible to everybody and reusable in various contexts: # machine-learning as a versatile tool for science and engineering. # # See https://scikit-learn.org for complete documentation. import importlib as _importlib import logging import os import random from ._config import config_context, get_config, set_config logger = logging.getLogger(__name__) # PEP0440 compatible formatted version, see: # https://www.python.org/dev/peps/pep-0440/ # # Generic release markers: # X.Y.0 # For first release after an increment in Y # X.Y.Z # For bugfix releases # # Admissible pre-release markers: # X.Y.ZaN # Alpha release # X.Y.ZbN # Beta release # X.Y.ZrcN # Release Candidate # X.Y.Z # Final release # # Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer. # 'X.Y.dev0' is the canonical version of 'X.Y.dev' # __version__ = "1.7.dev0" # On OSX, we can get a runtime error due to multiple OpenMP libraries loaded # simultaneously. This can happen for instance when calling BLAS inside a # prange. Setting the following environment variable allows multiple OpenMP # libraries to be loaded. It should not degrade performances since we manually # take care of potential over-subcription performance issues, in sections of # the code where nested OpenMP loops can happen, by dynamically reconfiguring # the inner OpenMP runtime to temporarily disable it while under the scope of # the outer OpenMP parallel section. os.environ.setdefault("KMP_DUPLICATE_LIB_OK", "True") # Workaround issue discovered in intel-openmp 2019.5: # https://github.com/ContinuumIO/anaconda-issues/issues/11294 os.environ.setdefault("KMP_INIT_AT_FORK", "FALSE") # `_distributor_init` allows distributors to run custom init code. # For instance, for the Windows wheel, this is used to pre-load the # vcomp shared library runtime for OpenMP embedded in the sklearn/.libs # sub-folder. # It is necessary to do this prior to importing show_versions as the # later is linked to the OpenMP runtime to make it possible to introspect # it and importing it first would fail if the OpenMP dll cannot be found. from . import ( # noqa: F401 E402 __check_build, _distributor_init, ) from .base import clone # noqa: E402 from .utils._show_versions import show_versions # noqa: E402 _submodules = [ "calibration", "cluster", "covariance", "cross_decomposition", "datasets", "decomposition", "dummy", "ensemble", "exceptions", "experimental", "externals", "feature_extraction", "feature_selection", "frozen", "gaussian_process", "inspection", "isotonic", "kernel_approximation", "kernel_ridge", "linear_model", "manifold", "metrics", "mixture", "model_selection", "multiclass", "multioutput", "naive_bayes", "neighbors", "neural_network", "pipeline", "preprocessing", "random_projection", "semi_supervised", "svm", "tree", "discriminant_analysis", "impute", "compose", ] __all__ = _submodules + [ # Non-modules: "clone", "get_config", "set_config", "config_context", "show_versions", ] def __dir__(): return __all__ def __getattr__(name): if name in _submodules: return _importlib.import_module(f"sklearn.{name}") else: try: return globals()[name] except KeyError: raise AttributeError(f"Module 'sklearn' has no attribute '{name}'") _BUILT_WITH_MESON = False try: import sklearn._built_with_meson # noqa: F401 _BUILT_WITH_MESON = True except ModuleNotFoundError: pass def setup_module(module): """Fixture for the tests to assure globally controllable seeding of RNGs""" import numpy as np # Check if a random seed exists in the environment, if not create one. _random_seed = os.environ.get("SKLEARN_SEED", None) if _random_seed is None: _random_seed = np.random.uniform() * np.iinfo(np.int32).max _random_seed = int(_random_seed) print("I: Seeding RNGs with %r" % _random_seed) np.random.seed(_random_seed) random.seed(_random_seed)
"""Configure global settings and get information about the working environment.""" # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause # Machine learning module for Python # ================================== # # sklearn is a Python module integrating classical machine # learning algorithms in the tightly-knit world of scientific Python # packages (numpy, scipy, matplotlib). # # It aims to provide simple and efficient solutions to learning problems # that are accessible to everybody and reusable in various contexts: # machine-learning as a versatile tool for science and engineering. # # See https://scikit-learn.org for complete documentation. import importlib as _importlib import logging import os import random from ._config import config_context, get_config, set_config logger = logging.getLogger(__name__) # PEP0440 compatible formatted version, see: # https://www.python.org/dev/peps/pep-0440/ # # Generic release markers: # X.Y.0 # For first release after an increment in Y # X.Y.Z # For bugfix releases # # Admissible pre-release markers: # X.Y.ZaN # Alpha release # X.Y.ZbN # Beta release # X.Y.ZrcN # Release Candidate # X.Y.Z # Final release # # Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer. # 'X.Y.dev0' is the canonical version of 'X.Y.dev' # __version__ = "1.6.dev0" # On OSX, we can get a runtime error due to multiple OpenMP libraries loaded # simultaneously. This can happen for instance when calling BLAS inside a # prange. Setting the following environment variable allows multiple OpenMP # libraries to be loaded. It should not degrade performances since we manually # take care of potential over-subcription performance issues, in sections of # the code where nested OpenMP loops can happen, by dynamically reconfiguring # the inner OpenMP runtime to temporarily disable it while under the scope of # the outer OpenMP parallel section. os.environ.setdefault("KMP_DUPLICATE_LIB_OK", "True") # Workaround issue discovered in intel-openmp 2019.5: # https://github.com/ContinuumIO/anaconda-issues/issues/11294 os.environ.setdefault("KMP_INIT_AT_FORK", "FALSE") # `_distributor_init` allows distributors to run custom init code. # For instance, for the Windows wheel, this is used to pre-load the # vcomp shared library runtime for OpenMP embedded in the sklearn/.libs # sub-folder. # It is necessary to do this prior to importing show_versions as the # later is linked to the OpenMP runtime to make it possible to introspect # it and importing it first would fail if the OpenMP dll cannot be found. from . import ( # noqa: F401 E402 __check_build, _distributor_init, ) from .base import clone # noqa: E402 from .utils._show_versions import show_versions # noqa: E402 _submodules = [ "calibration", "cluster", "covariance", "cross_decomposition", "datasets", "decomposition", "dummy", "ensemble", "exceptions", "experimental", "externals", "feature_extraction", "feature_selection", "frozen", "gaussian_process", "inspection", "isotonic", "kernel_approximation", "kernel_ridge", "linear_model", "manifold", "metrics", "mixture", "model_selection", "multiclass", "multioutput", "naive_bayes", "neighbors", "neural_network", "pipeline", "preprocessing", "random_projection", "semi_supervised", "svm", "tree", "discriminant_analysis", "impute", "compose", ] __all__ = _submodules + [ # Non-modules: "clone", "get_config", "set_config", "config_context", "show_versions", ] def __dir__(): return __all__ def __getattr__(name): if name in _submodules: return _importlib.import_module(f"sklearn.{name}") else: try: return globals()[name] except KeyError: raise AttributeError(f"Module 'sklearn' has no attribute '{name}'") _BUILT_WITH_MESON = False try: import sklearn._built_with_meson # noqa: F401 _BUILT_WITH_MESON = True except ModuleNotFoundError: pass def setup_module(module): """Fixture for the tests to assure globally controllable seeding of RNGs""" import numpy as np # Check if a random seed exists in the environment, if not create one. _random_seed = os.environ.get("SKLEARN_SEED", None) if _random_seed is None: _random_seed = np.random.uniform() * np.iinfo(np.int32).max _random_seed = int(_random_seed) print("I: Seeding RNGs with %r" % _random_seed) np.random.seed(_random_seed) random.seed(_random_seed)
from ...utils import is_torch_available if is_torch_available(): from .auraflow_transformer_2d import AuraFlowTransformer2DModel from .cogvideox_transformer_3d import CogVideoXTransformer3DModel from .consisid_transformer_3d import ConsisIDTransformer3DModel from .dit_transformer_2d import DiTTransformer2DModel from .dual_transformer_2d import DualTransformer2DModel from .hunyuan_transformer_2d import HunyuanDiT2DModel from .latte_transformer_3d import LatteTransformer3DModel from .lumina_nextdit2d import LuminaNextDiT2DModel from .pixart_transformer_2d import PixArtTransformer2DModel from .prior_transformer import PriorTransformer from .sana_transformer import SanaTransformer2DModel from .stable_audio_transformer import StableAudioDiTModel from .t5_film_transformer import T5FilmDecoder from .transformer_2d import Transformer2DModel from .transformer_allegro import AllegroTransformer3DModel from .transformer_cogview3plus import CogView3PlusTransformer2DModel from .transformer_cogview4 import CogView4Transformer2DModel from .transformer_easyanimate import EasyAnimateTransformer3DModel from .transformer_flux import FluxTransformer2DModel from .transformer_hidream_image import HiDreamImageTransformer2DModel from .transformer_hunyuan_video import HunyuanVideoTransformer3DModel from .transformer_ltx import LTXVideoTransformer3DModel from .transformer_lumina2 import Lumina2Transformer2DModel from .transformer_mochi import MochiTransformer3DModel from .transformer_omnigen import OmniGenTransformer2DModel from .transformer_sd3 import SD3Transformer2DModel from .transformer_temporal import TransformerTemporalModel from .transformer_wan import WanTransformer3DModel
from ...utils import is_torch_available if is_torch_available(): from .auraflow_transformer_2d import AuraFlowTransformer2DModel from .cogvideox_transformer_3d import CogVideoXTransformer3DModel from .consisid_transformer_3d import ConsisIDTransformer3DModel from .dit_transformer_2d import DiTTransformer2DModel from .dual_transformer_2d import DualTransformer2DModel from .hunyuan_transformer_2d import HunyuanDiT2DModel from .latte_transformer_3d import LatteTransformer3DModel from .lumina_nextdit2d import LuminaNextDiT2DModel from .pixart_transformer_2d import PixArtTransformer2DModel from .prior_transformer import PriorTransformer from .sana_transformer import SanaTransformer2DModel from .stable_audio_transformer import StableAudioDiTModel from .t5_film_transformer import T5FilmDecoder from .transformer_2d import Transformer2DModel from .transformer_allegro import AllegroTransformer3DModel from .transformer_cogview3plus import CogView3PlusTransformer2DModel from .transformer_cogview4 import CogView4Transformer2DModel from .transformer_easyanimate import EasyAnimateTransformer3DModel from .transformer_flux import FluxTransformer2DModel from .transformer_hunyuan_video import HunyuanVideoTransformer3DModel from .transformer_ltx import LTXVideoTransformer3DModel from .transformer_lumina2 import Lumina2Transformer2DModel from .transformer_mochi import MochiTransformer3DModel from .transformer_omnigen import OmniGenTransformer2DModel from .transformer_sd3 import SD3Transformer2DModel from .transformer_temporal import TransformerTemporalModel from .transformer_wan import WanTransformer3DModel
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" import subprocess import pytest from jina import Document, DocumentArray, Flow from ...spacy_text_encoder import SpacyTextEncoder _EMBEDDING_DIM = 96 @pytest.mark.parametrize('request_size', [1, 10, 50, 100]) def test_integration(request_size: int): docs = DocumentArray( [Document(text='just some random text here') for _ in range(50)] ) with Flow(return_results=True).add(uses=SpacyTextEncoder) as flow: resp = flow.post( on='/index', inputs=docs, request_size=request_size, return_results=True, ) assert sum(len(resp_batch.docs) for resp_batch in resp) == 50 for r in resp: for doc in r.docs: assert doc.embedding.shape == (_EMBEDDING_DIM,) @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, )
__copyright__ = "Copyright (c) 2020-2021 Jina AI Limited. All rights reserved." __license__ = "Apache-2.0" from jina import Document, Flow, DocumentArray try: from spacy_text_encoder import SpacyTextEncoder except: from ...spacy_text_encoder import SpacyTextEncoder def test_spacy_text_encoder(): docs = DocumentArray([Document(text='Han likes eating pizza'), Document(text='Han likes pizza'), Document(text='Jina rocks')]) f = Flow().add(uses=SpacyTextEncoder) with f: resp = f.post(on='/test', inputs=docs, return_results=True) docs = resp[0].docs assert len(docs) == 3 for doc in docs: assert doc.embedding.shape == (96,)
from ...utils import is_flax_available, is_torch_available if is_torch_available(): from .controlnet import ControlNetModel, ControlNetOutput from .controlnet_flux import FluxControlNetModel, FluxControlNetOutput, FluxMultiControlNetModel from .controlnet_hunyuan import ( HunyuanControlNetOutput, HunyuanDiT2DControlNetModel, HunyuanDiT2DMultiControlNetModel, ) from .controlnet_sd3 import SD3ControlNetModel, SD3ControlNetOutput, SD3MultiControlNetModel from .controlnet_sparsectrl import ( SparseControlNetConditioningEmbedding, SparseControlNetModel, SparseControlNetOutput, ) from .controlnet_union import ControlNetUnionModel from .controlnet_xs import ControlNetXSAdapter, ControlNetXSOutput, UNetControlNetXSModel from .multicontrolnet import MultiControlNetModel from .multicontrolnet_union import MultiControlNetUnionModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel
from ...utils import is_flax_available, is_torch_available if is_torch_available(): from .controlnet import ControlNetModel, ControlNetOutput from .controlnet_flux import FluxControlNetModel, FluxControlNetOutput, FluxMultiControlNetModel from .controlnet_hunyuan import ( HunyuanControlNetOutput, HunyuanDiT2DControlNetModel, HunyuanDiT2DMultiControlNetModel, ) from .controlnet_sd3 import SD3ControlNetModel, SD3ControlNetOutput, SD3MultiControlNetModel from .controlnet_sparsectrl import ( SparseControlNetConditioningEmbedding, SparseControlNetModel, SparseControlNetOutput, ) from .controlnet_union import ControlNetUnionModel from .controlnet_xs import ControlNetXSAdapter, ControlNetXSOutput, UNetControlNetXSModel from .multicontrolnet import MultiControlNetModel if is_flax_available(): from .controlnet_flax import FlaxControlNetModel